summaryrefslogtreecommitdiff
path: root/src/core/loader/nso.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2017-09-30 14:15:09 -0400
committerGravatar bunnei2017-09-30 14:33:58 -0400
commit8c92435ded5c9c89290604a03e307c038b4d4117 (patch)
tree89640df0ae25ef5c78b79c1d8169f5ad6c25d63b /src/core/loader/nso.cpp
parentprocess: Support loading multiple codesets. (diff)
downloadyuzu-8c92435ded5c9c89290604a03e307c038b4d4117.tar.gz
yuzu-8c92435ded5c9c89290604a03e307c038b4d4117.tar.xz
yuzu-8c92435ded5c9c89290604a03e307c038b4d4117.zip
nso: Refactor and allocate .bss section.
Diffstat (limited to 'src/core/loader/nso.cpp')
-rw-r--r--src/core/loader/nso.cpp223
1 files changed, 111 insertions, 112 deletions
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index ca8c59cd5..f5083d122 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -2,8 +2,6 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <map>
6#include <string>
7#include <vector> 5#include <vector>
8#include <lz4.h> 6#include <lz4.h>
9 7
@@ -14,22 +12,20 @@
14#include "core/loader/nso.h" 12#include "core/loader/nso.h"
15#include "core/memory.h" 13#include "core/memory.h"
16 14
17using Kernel::CodeSet;
18using Kernel::SharedPtr;
19
20namespace Loader { 15namespace Loader {
21 16
22FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) { 17enum class RelocationType : u32 { ABS64 = 257, GLOB_DAT = 1025, JUMP_SLOT = 1026, RELATIVE = 1027 };
23 u32 magic = 0;
24 file.Seek(0, SEEK_SET);
25 if (1 != file.ReadArray<u32>(&magic, 1))
26 return FileType::Error;
27 18
28 if (MakeMagic('N', 'S', 'O', '0') == magic) 19enum DynamicType : u32 {
29 return FileType::NSO; 20 DT_NULL = 0,
30 21 DT_PLTRELSZ = 2,
31 return FileType::Error; 22 DT_STRTAB = 5,
32} 23 DT_SYMTAB = 6,
24 DT_RELA = 7,
25 DT_RELASZ = 8,
26 DT_STRSZ = 10,
27 DT_JMPREL = 23,
28};
33 29
34struct NsoSegmentHeader { 30struct NsoSegmentHeader {
35 u32_le offset; 31 u32_le offset;
@@ -42,13 +38,40 @@ static_assert(sizeof(NsoSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect
42struct NsoHeader { 38struct NsoHeader {
43 u32_le magic; 39 u32_le magic;
44 INSERT_PADDING_BYTES(0xc); 40 INSERT_PADDING_BYTES(0xc);
45 std::array<NsoSegmentHeader, 3> segments; // Text, Data, RoData (in that order) 41 std::array<NsoSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
46 INSERT_PADDING_BYTES(0x20); 42 u32_le bss_size;
43 INSERT_PADDING_BYTES(0x1c);
47 std::array<u32_le, 3> segments_compressed_size; 44 std::array<u32_le, 3> segments_compressed_size;
48}; 45};
49
50static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size."); 46static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size.");
51 47
48struct ModHeader {
49 INSERT_PADDING_BYTES(0x4);
50 u32_le offset_to_start; // Always 8
51 u32_le magic;
52 u32_le dynamic_offset;
53 u32_le bss_start_offset;
54 u32_le bss_end_offset;
55 u32_le eh_frame_hdr_start_offset;
56 u32_le eh_frame_hdr_end_offset;
57 u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base
58};
59static_assert(sizeof(ModHeader) == 0x24, "ModHeader has incorrect size.");
60
61FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) {
62 u32 magic = 0;
63 file.Seek(0, SEEK_SET);
64 if (1 != file.ReadArray<u32>(&magic, 1)) {
65 return FileType::Error;
66 }
67
68 if (MakeMagic('N', 'S', 'O', '0') == magic) {
69 return FileType::NSO;
70 }
71
72 return FileType::Error;
73}
74
52static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeader& header, 75static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeader& header,
53 int compressed_size) { 76 int compressed_size) {
54 std::vector<u8> compressed_data; 77 std::vector<u8> compressed_data;
@@ -72,40 +95,10 @@ static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeade
72 return uncompressed_data; 95 return uncompressed_data;
73} 96}
74 97
75struct Symbol { 98void AppLoader_NSO::WriteRelocations(const std::vector<Symbol>& symbols, VAddr load_base,
76 Symbol(std::string&& name, u64 value) : name(std::move(name)), value(value) {} 99 u64 relocation_offset, u64 size, bool is_jump_relocation) {
77 std::string name;
78 u64 value;
79};
80
81struct Import {
82 VAddr ea;
83 s64 addend;
84};
85
86enum class RelocationType : u32 {
87 ABS64 = 257,
88 GLOB_DAT = 1025,
89 JUMP_SLOT = 1026,
90 RELATIVE = 1027
91};
92
93enum DynamicType : u32 {
94 DT_NULL = 0,
95 DT_PLTRELSZ = 2,
96 DT_STRTAB = 5,
97 DT_SYMTAB = 6,
98 DT_RELA = 7,
99 DT_RELASZ = 8,
100 DT_STRSZ = 10,
101 DT_JMPREL = 23,
102};
103
104void WriteRelocations(const std::vector<Symbol>& symbols, VAddr loadbase, u64 roff, u64 size,
105 bool is_jump_relocation, std::map<std::string, Import>& imports,
106 std::map<std::string, VAddr>& exports) {
107 for (u64 i = 0; i < size; i += 0x18) { 100 for (u64 i = 0; i < size; i += 0x18) {
108 VAddr addr = loadbase + roff + i; 101 VAddr addr = load_base + relocation_offset + i;
109 u64 offset = Memory::Read64(addr); 102 u64 offset = Memory::Read64(addr);
110 u64 info = Memory::Read64(addr + 8); 103 u64 info = Memory::Read64(addr + 8);
111 u64 addend_unsigned = Memory::Read64(addr + 16); 104 u64 addend_unsigned = Memory::Read64(addr + 16);
@@ -114,16 +107,16 @@ void WriteRelocations(const std::vector<Symbol>& symbols, VAddr loadbase, u64 ro
114 107
115 RelocationType rtype = static_cast<RelocationType>(info & 0xFFFFFFFF); 108 RelocationType rtype = static_cast<RelocationType>(info & 0xFFFFFFFF);
116 u32 rsym = static_cast<u32>(info >> 32); 109 u32 rsym = static_cast<u32>(info >> 32);
117 VAddr ea = loadbase + offset; 110 VAddr ea = load_base + offset;
118 111
119 const Symbol& symbol = symbols[rsym]; 112 const Symbol& symbol = symbols[rsym];
120 113
121 switch (rtype) { 114 switch (rtype) {
122 case RelocationType::RELATIVE: 115 case RelocationType::RELATIVE:
123 if (!symbol.name.empty()) { 116 if (!symbol.name.empty()) {
124 exports[symbol.name] = loadbase + addend; 117 exports[symbol.name] = load_base + addend;
125 } 118 }
126 Memory::Write64(ea, loadbase + addend); 119 Memory::Write64(ea, load_base + addend);
127 break; 120 break;
128 case RelocationType::JUMP_SLOT: 121 case RelocationType::JUMP_SLOT:
129 case RelocationType::GLOB_DAT: 122 case RelocationType::GLOB_DAT:
@@ -149,18 +142,12 @@ void WriteRelocations(const std::vector<Symbol>& symbols, VAddr loadbase, u64 ro
149 } 142 }
150} 143}
151 144
152void Relocate(VAddr loadbase, std::map<std::string, Import>& imports, 145void AppLoader_NSO::Relocate(VAddr load_base, VAddr dynamic_section_addr) {
153 std::map<std::string, VAddr>& exports) {
154 u32 modoff = Memory::Read32(loadbase + 4);
155 ASSERT_MSG(Memory::Read32(loadbase + modoff) == MakeMagic('M', 'O', 'D', '0'),
156 "Expected MOD section");
157
158 u64 dynoff = loadbase + modoff + Memory::Read32(loadbase + modoff + 4);
159 std::map<u64, u64> dynamic; 146 std::map<u64, u64> dynamic;
160 while (1) { 147 while (1) {
161 u64 tag = Memory::Read64(dynoff); 148 u64 tag = Memory::Read64(dynamic_section_addr);
162 u64 value = Memory::Read64(dynoff + 8); 149 u64 value = Memory::Read64(dynamic_section_addr + 8);
163 dynoff += 16; 150 dynamic_section_addr += 16;
164 151
165 if (tag == DT_NULL) { 152 if (tag == DT_NULL) {
166 break; 153 break;
@@ -171,9 +158,9 @@ void Relocate(VAddr loadbase, std::map<std::string, Import>& imports,
171 u64 strtabsize = dynamic[DT_STRSZ]; 158 u64 strtabsize = dynamic[DT_STRSZ];
172 std::vector<u8> strtab; 159 std::vector<u8> strtab;
173 strtab.resize(strtabsize); 160 strtab.resize(strtabsize);
174 Memory::ReadBlock(loadbase + dynamic[DT_STRTAB], strtab.data(), strtabsize); 161 Memory::ReadBlock(load_base + dynamic[DT_STRTAB], strtab.data(), strtabsize);
175 162
176 VAddr addr = loadbase + dynamic[DT_SYMTAB]; 163 VAddr addr = load_base + dynamic[DT_SYMTAB];
177 std::vector<Symbol> symbols; 164 std::vector<Symbol> symbols;
178 while (1) { 165 while (1) {
179 const u32 stname = Memory::Read32(addr); 166 const u32 stname = Memory::Read32(addr);
@@ -187,96 +174,108 @@ void Relocate(VAddr loadbase, std::map<std::string, Import>& imports,
187 174
188 std::string name = reinterpret_cast<char*>(&strtab[stname]); 175 std::string name = reinterpret_cast<char*>(&strtab[stname]);
189 if (stvalue) { 176 if (stvalue) {
190 exports[name] = loadbase + stvalue; 177 exports[name] = load_base + stvalue;
191 symbols.emplace_back(std::move(name), loadbase + stvalue); 178 symbols.emplace_back(std::move(name), load_base + stvalue);
192 } else { 179 } else {
193 symbols.emplace_back(std::move(name), 0); 180 symbols.emplace_back(std::move(name), 0);
194 } 181 }
195 } 182 }
196 183
197 if (dynamic.find(DT_RELA) != dynamic.end()) { 184 if (dynamic.find(DT_RELA) != dynamic.end()) {
198 WriteRelocations(symbols, loadbase, dynamic[DT_RELA], dynamic[DT_RELASZ], false, imports, 185 WriteRelocations(symbols, load_base, dynamic[DT_RELA], dynamic[DT_RELASZ], false);
199 exports);
200 } 186 }
201 187
202 if (dynamic.find(DT_JMPREL) != dynamic.end()) { 188 if (dynamic.find(DT_JMPREL) != dynamic.end()) {
203 WriteRelocations(symbols, loadbase, dynamic[DT_JMPREL], dynamic[DT_PLTRELSZ], true, imports, 189 WriteRelocations(symbols, load_base, dynamic[DT_JMPREL], dynamic[DT_PLTRELSZ], true);
204 exports);
205 } 190 }
206} 191}
207 192
208static VAddr GetEntryPoint(const std::map<std::string, VAddr>& exports) { 193VAddr AppLoader_NSO::GetEntryPoint() const {
209 // Find nnMain function, set entrypoint to that address 194 // Find nnMain function, set entrypoint to that address
210 const auto& search = exports.find("nnMain"); 195 const auto& search = exports.find("nnMain");
211 if (search != exports.end()) { 196 if (search != exports.end()) {
212 return search->second; 197 return search->second;
213 } 198 }
199 ASSERT_MSG(false, "Unable to find entrypoint");
214 return {}; 200 return {};
215} 201}
216 202
217static SharedPtr<CodeSet> LoadModule(const std::string& filepath, VAddr loadbase, 203static constexpr u32 PageAlignSize(u32 size) {
218 std::map<std::string, Import>& imports, 204 return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
219 std::map<std::string, VAddr>& exports) { 205}
220 FileUtil::IOFile file(filepath, "rb");
221 206
222 if (!file.IsOpen()) 207bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) {
208 FileUtil::IOFile file(path, "rb");
209 if (!file.IsOpen()) {
223 return {}; 210 return {};
211 }
224 212
225 NsoHeader header{}; 213 // Read NSO header
214 NsoHeader nso_header{};
226 file.Seek(0, SEEK_SET); 215 file.Seek(0, SEEK_SET);
227 if (sizeof(NsoHeader) != file.ReadBytes(&header, sizeof(NsoHeader))) 216 if (sizeof(NsoHeader) != file.ReadBytes(&nso_header, sizeof(NsoHeader))) {
217 return {};
218 }
219 if (nso_header.magic != MakeMagic('N', 'S', 'O', '0')) {
228 return {}; 220 return {};
221 }
229 222
230 // Build program image 223 // Build program image
231 SharedPtr<CodeSet> codeset = CodeSet::Create("", 0); 224 Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create("", 0);
232 std::vector<u8> program_image; 225 std::vector<u8> program_image;
233 for (int i = 0; i < header.segments.size(); ++i) { 226 for (int i = 0; i < nso_header.segments.size(); ++i) {
234 std::vector<u8> data = 227 std::vector<u8> data =
235 ReadSegment(file, header.segments[i], header.segments_compressed_size[i]); 228 ReadSegment(file, nso_header.segments[i], nso_header.segments_compressed_size[i]);
236 program_image.resize(header.segments[i].location); 229 program_image.resize(nso_header.segments[i].location);
237 program_image.insert(program_image.end(), data.begin(), data.end()); 230 program_image.insert(program_image.end(), data.begin(), data.end());
238 codeset->segments[i].addr = header.segments[i].location; 231 codeset->segments[i].addr = nso_header.segments[i].location;
239 codeset->segments[i].offset = header.segments[i].location; 232 codeset->segments[i].offset = nso_header.segments[i].location;
240 codeset->segments[i].size = (data.size() + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; 233 codeset->segments[i].size = static_cast<u32>(data.size());
234 }
235
236 // Read MOD header
237 ModHeader mod_header{};
238 std::memcpy(&mod_header, program_image.data(), sizeof(ModHeader));
239 if (mod_header.magic != MakeMagic('M', 'O', 'D', '0')) {
240 return {};
241 } 241 }
242 program_image.resize((program_image.size() + Memory::PAGE_MASK) & ~Memory::PAGE_MASK);
243 242
244 codeset->name = filepath; 243 // Resize program image to include .bss section and page align each section
245 codeset->entrypoint = 0; // Set after relocation 244 const u32 bss_size = mod_header.bss_end_offset - mod_header.bss_start_offset;
245 codeset->code.size = PageAlignSize(codeset->code.size);
246 codeset->rodata.size = PageAlignSize(codeset->rodata.size);
247 codeset->data.size = PageAlignSize(codeset->data.size + bss_size);
248 program_image.resize(PageAlignSize(static_cast<u32>(program_image.size()) + bss_size));
249
250 // Load codeset for current process
251 codeset->name = path;
246 codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); 252 codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
253 Kernel::g_current_process->LoadModule(codeset, load_base);
254 Relocate(load_base, load_base + mod_header.offset_to_start + mod_header.dynamic_offset);
247 255
248 return codeset; 256 return true;
249} 257}
250 258
251ResultStatus AppLoader_NSO::Load() { 259ResultStatus AppLoader_NSO::Load() {
252 if (is_loaded) 260 if (is_loaded) {
253 return ResultStatus::ErrorAlreadyLoaded; 261 return ResultStatus::ErrorAlreadyLoaded;
254 262 }
255 if (!file.IsOpen()) 263 if (!file.IsOpen()) {
256 return ResultStatus::Error; 264 return ResultStatus::Error;
265 }
257 266
258 static constexpr VAddr loadbase = 0x7100000000; 267 // Load and relocate "main" and "sdk" NSO
259 std::map<std::string, Import> imports; 268 const std::string sdkpath = filepath.substr(0, filepath.find_last_of("/\\")) + "/sdk";
260 std::map<std::string, VAddr> exports; 269 Kernel::g_current_process = Kernel::Process::Create("main");
270 if (!LoadNso(filepath, 0x10000000) || !LoadNso(sdkpath, 0x20000000)) {
271 return ResultStatus::ErrorInvalidFormat;
272 }
261 273
262 // Load and relocate "main" NSO
263 auto codeset = LoadModule(filepath, loadbase, imports, exports);
264 Kernel::g_current_process = Kernel::Process::Create(codeset);
265 Kernel::g_current_process->svc_access_mask.set(); 274 Kernel::g_current_process->svc_access_mask.set();
266 Kernel::g_current_process->address_mappings = default_address_mappings; 275 Kernel::g_current_process->address_mappings = default_address_mappings;
267 Kernel::g_current_process->resource_limit = 276 Kernel::g_current_process->resource_limit =
268 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); 277 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
269 Kernel::g_current_process->LoadModule(codeset, loadbase); 278 Kernel::g_current_process->Run(GetEntryPoint(), 48, Kernel::DEFAULT_STACK_SIZE);
270 Relocate(loadbase, imports, exports);
271 codeset->entrypoint = GetEntryPoint(exports);
272 Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
273
274 // Load and relocate "sdk" NSO
275 static constexpr VAddr sdkbase = 0x7200000000;
276 const std::string sdkpath = filepath.substr(0, filepath.find_last_of("/\\")) + "/sdk";
277 auto sdk_codeset = LoadModule(sdkpath, sdkbase, imports, exports);
278 Kernel::g_current_process->LoadModule(sdk_codeset, sdkbase);
279 Relocate(sdkbase, imports, exports);
280 279
281 // Resolve imports 280 // Resolve imports
282 for (const auto& import : imports) { 281 for (const auto& import : imports) {
@@ -284,7 +283,7 @@ ResultStatus AppLoader_NSO::Load() {
284 if (search != exports.end()) { 283 if (search != exports.end()) {
285 Memory::Write64(import.second.ea, search->second + import.second.addend); 284 Memory::Write64(import.second.ea, search->second + import.second.addend);
286 } else { 285 } else {
287 LOG_CRITICAL(Loader, "Unresolved import: %s", import.first.c_str()); 286 LOG_ERROR(Loader, "Unresolved import: %s", import.first.c_str());
288 } 287 }
289 } 288 }
290 289