diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/loader/loader.cpp | 6 | ||||
| -rw-r--r-- | src/core/loader/loader.h | 1 | ||||
| -rw-r--r-- | src/core/loader/nso.cpp | 295 | ||||
| -rw-r--r-- | src/core/loader/nso.h | 38 |
5 files changed, 342 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 44d7f670b..f8bc1fdd0 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -176,6 +176,7 @@ set(SRCS | |||
| 176 | loader/elf.cpp | 176 | loader/elf.cpp |
| 177 | loader/loader.cpp | 177 | loader/loader.cpp |
| 178 | loader/ncch.cpp | 178 | loader/ncch.cpp |
| 179 | loader/nso.cpp | ||
| 179 | loader/smdh.cpp | 180 | loader/smdh.cpp |
| 180 | tracer/recorder.cpp | 181 | tracer/recorder.cpp |
| 181 | memory.cpp | 182 | memory.cpp |
| @@ -375,6 +376,7 @@ set(HEADERS | |||
| 375 | loader/elf.h | 376 | loader/elf.h |
| 376 | loader/loader.h | 377 | loader/loader.h |
| 377 | loader/ncch.h | 378 | loader/ncch.h |
| 379 | loader/nso.h | ||
| 378 | loader/smdh.h | 380 | loader/smdh.h |
| 379 | tracer/recorder.h | 381 | tracer/recorder.h |
| 380 | tracer/citrace.h | 382 | tracer/citrace.h |
diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index be719d74c..d309e7c9e 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp | |||
| @@ -10,6 +10,7 @@ | |||
| 10 | #include "core/loader/3dsx.h" | 10 | #include "core/loader/3dsx.h" |
| 11 | #include "core/loader/elf.h" | 11 | #include "core/loader/elf.h" |
| 12 | #include "core/loader/ncch.h" | 12 | #include "core/loader/ncch.h" |
| 13 | #include "core/loader/nso.h" | ||
| 13 | 14 | ||
| 14 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 15 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 15 | 16 | ||
| @@ -32,6 +33,7 @@ FileType IdentifyFile(FileUtil::IOFile& file) { | |||
| 32 | CHECK_TYPE(THREEDSX) | 33 | CHECK_TYPE(THREEDSX) |
| 33 | CHECK_TYPE(ELF) | 34 | CHECK_TYPE(ELF) |
| 34 | CHECK_TYPE(NCCH) | 35 | CHECK_TYPE(NCCH) |
| 36 | CHECK_TYPE(NSO) | ||
| 35 | 37 | ||
| 36 | #undef CHECK_TYPE | 38 | #undef CHECK_TYPE |
| 37 | 39 | ||
| @@ -115,6 +117,10 @@ static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileTyp | |||
| 115 | case FileType::CCI: | 117 | case FileType::CCI: |
| 116 | return std::make_unique<AppLoader_NCCH>(std::move(file), filepath); | 118 | return std::make_unique<AppLoader_NCCH>(std::move(file), filepath); |
| 117 | 119 | ||
| 120 | // NX NSO file format. | ||
| 121 | case FileType::NSO: | ||
| 122 | return std::make_unique<AppLoader_NSO>(std::move(file), filename, filepath); | ||
| 123 | |||
| 118 | default: | 124 | default: |
| 119 | return nullptr; | 125 | return nullptr; |
| 120 | } | 126 | } |
diff --git a/src/core/loader/loader.h b/src/core/loader/loader.h index 48bbf687d..79a3bd498 100644 --- a/src/core/loader/loader.h +++ b/src/core/loader/loader.h | |||
| @@ -32,6 +32,7 @@ enum class FileType { | |||
| 32 | CIA, | 32 | CIA, |
| 33 | ELF, | 33 | ELF, |
| 34 | THREEDSX, // 3DSX | 34 | THREEDSX, // 3DSX |
| 35 | NSO, | ||
| 35 | }; | 36 | }; |
| 36 | 37 | ||
| 37 | /** | 38 | /** |
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp new file mode 100644 index 000000000..ca8c59cd5 --- /dev/null +++ b/src/core/loader/nso.cpp | |||
| @@ -0,0 +1,295 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <map> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | #include <lz4.h> | ||
| 9 | |||
| 10 | #include "common/logging/log.h" | ||
| 11 | #include "common/swap.h" | ||
| 12 | #include "core/hle/kernel/process.h" | ||
| 13 | #include "core/hle/kernel/resource_limit.h" | ||
| 14 | #include "core/loader/nso.h" | ||
| 15 | #include "core/memory.h" | ||
| 16 | |||
| 17 | using Kernel::CodeSet; | ||
| 18 | using Kernel::SharedPtr; | ||
| 19 | |||
| 20 | namespace Loader { | ||
| 21 | |||
| 22 | FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) { | ||
| 23 | u32 magic = 0; | ||
| 24 | file.Seek(0, SEEK_SET); | ||
| 25 | if (1 != file.ReadArray<u32>(&magic, 1)) | ||
| 26 | return FileType::Error; | ||
| 27 | |||
| 28 | if (MakeMagic('N', 'S', 'O', '0') == magic) | ||
| 29 | return FileType::NSO; | ||
| 30 | |||
| 31 | return FileType::Error; | ||
| 32 | } | ||
| 33 | |||
| 34 | struct NsoSegmentHeader { | ||
| 35 | u32_le offset; | ||
| 36 | u32_le location; | ||
| 37 | u32_le size; | ||
| 38 | u32_le alignment; | ||
| 39 | }; | ||
| 40 | static_assert(sizeof(NsoSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect size."); | ||
| 41 | |||
| 42 | struct NsoHeader { | ||
| 43 | u32_le magic; | ||
| 44 | INSERT_PADDING_BYTES(0xc); | ||
| 45 | std::array<NsoSegmentHeader, 3> segments; // Text, Data, RoData (in that order) | ||
| 46 | INSERT_PADDING_BYTES(0x20); | ||
| 47 | std::array<u32_le, 3> segments_compressed_size; | ||
| 48 | }; | ||
| 49 | |||
| 50 | static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size."); | ||
| 51 | |||
| 52 | static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeader& header, | ||
| 53 | int compressed_size) { | ||
| 54 | std::vector<u8> compressed_data; | ||
| 55 | compressed_data.resize(compressed_size); | ||
| 56 | |||
| 57 | file.Seek(header.offset, SEEK_SET); | ||
| 58 | if (compressed_size != file.ReadBytes(compressed_data.data(), compressed_size)) { | ||
| 59 | LOG_CRITICAL(Loader, "Failed to read %d NSO LZ4 compressed bytes", compressed_size); | ||
| 60 | return {}; | ||
| 61 | } | ||
| 62 | |||
| 63 | std::vector<u8> uncompressed_data; | ||
| 64 | uncompressed_data.resize(header.size); | ||
| 65 | const int bytes_uncompressed = | ||
| 66 | LZ4_decompress_safe_partial(reinterpret_cast<const char*>(compressed_data.data()), | ||
| 67 | reinterpret_cast<char*>(uncompressed_data.data()), | ||
| 68 | compressed_size, header.size, header.size); | ||
| 69 | |||
| 70 | ASSERT_MSG(bytes_uncompressed == header.size, "%d != %d", bytes_uncompressed, header.size); | ||
| 71 | |||
| 72 | return uncompressed_data; | ||
| 73 | } | ||
| 74 | |||
| 75 | struct Symbol { | ||
| 76 | Symbol(std::string&& name, u64 value) : name(std::move(name)), value(value) {} | ||
| 77 | std::string name; | ||
| 78 | u64 value; | ||
| 79 | }; | ||
| 80 | |||
| 81 | struct Import { | ||
| 82 | VAddr ea; | ||
| 83 | s64 addend; | ||
| 84 | }; | ||
| 85 | |||
| 86 | enum class RelocationType : u32 { | ||
| 87 | ABS64 = 257, | ||
| 88 | GLOB_DAT = 1025, | ||
| 89 | JUMP_SLOT = 1026, | ||
| 90 | RELATIVE = 1027 | ||
| 91 | }; | ||
| 92 | |||
| 93 | enum 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 | |||
| 104 | void 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) { | ||
| 108 | VAddr addr = loadbase + roff + i; | ||
| 109 | u64 offset = Memory::Read64(addr); | ||
| 110 | u64 info = Memory::Read64(addr + 8); | ||
| 111 | u64 addend_unsigned = Memory::Read64(addr + 16); | ||
| 112 | s64 addend{}; | ||
| 113 | std::memcpy(&addend, &addend_unsigned, sizeof(u64)); | ||
| 114 | |||
| 115 | RelocationType rtype = static_cast<RelocationType>(info & 0xFFFFFFFF); | ||
| 116 | u32 rsym = static_cast<u32>(info >> 32); | ||
| 117 | VAddr ea = loadbase + offset; | ||
| 118 | |||
| 119 | const Symbol& symbol = symbols[rsym]; | ||
| 120 | |||
| 121 | switch (rtype) { | ||
| 122 | case RelocationType::RELATIVE: | ||
| 123 | if (!symbol.name.empty()) { | ||
| 124 | exports[symbol.name] = loadbase + addend; | ||
| 125 | } | ||
| 126 | Memory::Write64(ea, loadbase + addend); | ||
| 127 | break; | ||
| 128 | case RelocationType::JUMP_SLOT: | ||
| 129 | case RelocationType::GLOB_DAT: | ||
| 130 | if (!symbol.value) { | ||
| 131 | imports[symbol.name] = {ea, 0}; | ||
| 132 | } else { | ||
| 133 | exports[symbol.name] = symbol.value; | ||
| 134 | Memory::Write64(ea, symbol.value); | ||
| 135 | } | ||
| 136 | break; | ||
| 137 | case RelocationType::ABS64: | ||
| 138 | if (!symbol.value) { | ||
| 139 | imports[symbol.name] = {ea, addend}; | ||
| 140 | } else { | ||
| 141 | exports[symbol.name] = symbol.value + addend; | ||
| 142 | Memory::Write64(ea, symbol.value + addend); | ||
| 143 | } | ||
| 144 | break; | ||
| 145 | default: | ||
| 146 | LOG_CRITICAL(Loader, "Unknown relocation type: %d", rtype); | ||
| 147 | break; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | void Relocate(VAddr loadbase, std::map<std::string, Import>& imports, | ||
| 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; | ||
| 160 | while (1) { | ||
| 161 | u64 tag = Memory::Read64(dynoff); | ||
| 162 | u64 value = Memory::Read64(dynoff + 8); | ||
| 163 | dynoff += 16; | ||
| 164 | |||
| 165 | if (tag == DT_NULL) { | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | dynamic[tag] = value; | ||
| 169 | } | ||
| 170 | |||
| 171 | u64 strtabsize = dynamic[DT_STRSZ]; | ||
| 172 | std::vector<u8> strtab; | ||
| 173 | strtab.resize(strtabsize); | ||
| 174 | Memory::ReadBlock(loadbase + dynamic[DT_STRTAB], strtab.data(), strtabsize); | ||
| 175 | |||
| 176 | VAddr addr = loadbase + dynamic[DT_SYMTAB]; | ||
| 177 | std::vector<Symbol> symbols; | ||
| 178 | while (1) { | ||
| 179 | const u32 stname = Memory::Read32(addr); | ||
| 180 | const u16 stshndx = Memory::Read16(addr + 6); | ||
| 181 | const u64 stvalue = Memory::Read64(addr + 8); | ||
| 182 | addr += 24; | ||
| 183 | |||
| 184 | if (stname >= strtabsize) { | ||
| 185 | break; | ||
| 186 | } | ||
| 187 | |||
| 188 | std::string name = reinterpret_cast<char*>(&strtab[stname]); | ||
| 189 | if (stvalue) { | ||
| 190 | exports[name] = loadbase + stvalue; | ||
| 191 | symbols.emplace_back(std::move(name), loadbase + stvalue); | ||
| 192 | } else { | ||
| 193 | symbols.emplace_back(std::move(name), 0); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | if (dynamic.find(DT_RELA) != dynamic.end()) { | ||
| 198 | WriteRelocations(symbols, loadbase, dynamic[DT_RELA], dynamic[DT_RELASZ], false, imports, | ||
| 199 | exports); | ||
| 200 | } | ||
| 201 | |||
| 202 | if (dynamic.find(DT_JMPREL) != dynamic.end()) { | ||
| 203 | WriteRelocations(symbols, loadbase, dynamic[DT_JMPREL], dynamic[DT_PLTRELSZ], true, imports, | ||
| 204 | exports); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | static VAddr GetEntryPoint(const std::map<std::string, VAddr>& exports) { | ||
| 209 | // Find nnMain function, set entrypoint to that address | ||
| 210 | const auto& search = exports.find("nnMain"); | ||
| 211 | if (search != exports.end()) { | ||
| 212 | return search->second; | ||
| 213 | } | ||
| 214 | return {}; | ||
| 215 | } | ||
| 216 | |||
| 217 | static SharedPtr<CodeSet> LoadModule(const std::string& filepath, VAddr loadbase, | ||
| 218 | std::map<std::string, Import>& imports, | ||
| 219 | std::map<std::string, VAddr>& exports) { | ||
| 220 | FileUtil::IOFile file(filepath, "rb"); | ||
| 221 | |||
| 222 | if (!file.IsOpen()) | ||
| 223 | return {}; | ||
| 224 | |||
| 225 | NsoHeader header{}; | ||
| 226 | file.Seek(0, SEEK_SET); | ||
| 227 | if (sizeof(NsoHeader) != file.ReadBytes(&header, sizeof(NsoHeader))) | ||
| 228 | return {}; | ||
| 229 | |||
| 230 | // Build program image | ||
| 231 | SharedPtr<CodeSet> codeset = CodeSet::Create("", 0); | ||
| 232 | std::vector<u8> program_image; | ||
| 233 | for (int i = 0; i < header.segments.size(); ++i) { | ||
| 234 | std::vector<u8> data = | ||
| 235 | ReadSegment(file, header.segments[i], header.segments_compressed_size[i]); | ||
| 236 | program_image.resize(header.segments[i].location); | ||
| 237 | program_image.insert(program_image.end(), data.begin(), data.end()); | ||
| 238 | codeset->segments[i].addr = header.segments[i].location; | ||
| 239 | codeset->segments[i].offset = header.segments[i].location; | ||
| 240 | codeset->segments[i].size = (data.size() + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; | ||
| 241 | } | ||
| 242 | program_image.resize((program_image.size() + Memory::PAGE_MASK) & ~Memory::PAGE_MASK); | ||
| 243 | |||
| 244 | codeset->name = filepath; | ||
| 245 | codeset->entrypoint = 0; // Set after relocation | ||
| 246 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); | ||
| 247 | |||
| 248 | return codeset; | ||
| 249 | } | ||
| 250 | |||
| 251 | ResultStatus AppLoader_NSO::Load() { | ||
| 252 | if (is_loaded) | ||
| 253 | return ResultStatus::ErrorAlreadyLoaded; | ||
| 254 | |||
| 255 | if (!file.IsOpen()) | ||
| 256 | return ResultStatus::Error; | ||
| 257 | |||
| 258 | static constexpr VAddr loadbase = 0x7100000000; | ||
| 259 | std::map<std::string, Import> imports; | ||
| 260 | std::map<std::string, VAddr> exports; | ||
| 261 | |||
| 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(); | ||
| 266 | Kernel::g_current_process->address_mappings = default_address_mappings; | ||
| 267 | Kernel::g_current_process->resource_limit = | ||
| 268 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | ||
| 269 | Kernel::g_current_process->LoadModule(codeset, loadbase); | ||
| 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 | |||
| 281 | // Resolve imports | ||
| 282 | for (const auto& import : imports) { | ||
| 283 | const auto& search = exports.find(import.first); | ||
| 284 | if (search != exports.end()) { | ||
| 285 | Memory::Write64(import.second.ea, search->second + import.second.addend); | ||
| 286 | } else { | ||
| 287 | LOG_CRITICAL(Loader, "Unresolved import: %s", import.first.c_str()); | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | is_loaded = true; | ||
| 292 | return ResultStatus::Success; | ||
| 293 | } | ||
| 294 | |||
| 295 | } // namespace Loader | ||
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h new file mode 100644 index 000000000..39a9bd3d9 --- /dev/null +++ b/src/core/loader/nso.h | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <string> | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "common/file_util.h" | ||
| 10 | #include "core/loader/loader.h" | ||
| 11 | |||
| 12 | namespace Loader { | ||
| 13 | |||
| 14 | /// Loads an NSO file | ||
| 15 | class AppLoader_NSO final : public AppLoader { | ||
| 16 | public: | ||
| 17 | AppLoader_NSO(FileUtil::IOFile&& file, std::string filename, std::string filepath) | ||
| 18 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) {} | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Returns the type of the file | ||
| 22 | * @param file FileUtil::IOFile open file | ||
| 23 | * @return FileType found, or FileType::Error if this loader doesn't know it | ||
| 24 | */ | ||
| 25 | static FileType IdentifyType(FileUtil::IOFile& file); | ||
| 26 | |||
| 27 | FileType GetFileType() override { | ||
| 28 | return IdentifyType(file); | ||
| 29 | } | ||
| 30 | |||
| 31 | ResultStatus Load() override; | ||
| 32 | |||
| 33 | private: | ||
| 34 | std::string filename; | ||
| 35 | std::string filepath; | ||
| 36 | }; | ||
| 37 | |||
| 38 | } // namespace Loader | ||