diff options
| author | 2017-10-05 23:30:08 -0400 | |
|---|---|---|
| committer | 2017-10-05 23:30:08 -0400 | |
| commit | 33ea53094cc1f34c27ca295472f01f8dd09a300b (patch) | |
| tree | c45a8eefd68222c390b28ab2dfba3d787c4634ac /src/core/loader/nro.cpp | |
| parent | nso: Fixes to support homebrew NSOs without a MOD header. (diff) | |
| download | yuzu-33ea53094cc1f34c27ca295472f01f8dd09a300b.tar.gz yuzu-33ea53094cc1f34c27ca295472f01f8dd09a300b.tar.xz yuzu-33ea53094cc1f34c27ca295472f01f8dd09a300b.zip | |
loader: Add support for NRO, as well as various fixes and shared linker.
Diffstat (limited to 'src/core/loader/nro.cpp')
| -rw-r--r-- | src/core/loader/nro.cpp | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp new file mode 100644 index 000000000..ed638e1fa --- /dev/null +++ b/src/core/loader/nro.cpp | |||
| @@ -0,0 +1,173 @@ | |||
| 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 <vector> | ||
| 6 | |||
| 7 | #include "common/logging/log.h" | ||
| 8 | #include "common/swap.h" | ||
| 9 | #include "core/hle/kernel/process.h" | ||
| 10 | #include "core/hle/kernel/resource_limit.h" | ||
| 11 | #include "core/loader/nro.h" | ||
| 12 | #include "core/memory.h" | ||
| 13 | |||
| 14 | namespace Loader { | ||
| 15 | |||
| 16 | struct NroSegmentHeader { | ||
| 17 | u32_le offset; | ||
| 18 | u32_le size; | ||
| 19 | }; | ||
| 20 | static_assert(sizeof(NroSegmentHeader) == 0x8, "NroSegmentHeader has incorrect size."); | ||
| 21 | |||
| 22 | struct NroHeader { | ||
| 23 | INSERT_PADDING_BYTES(0x4); | ||
| 24 | u32_le module_header_offset; | ||
| 25 | INSERT_PADDING_BYTES(0x8); | ||
| 26 | u32_le magic; | ||
| 27 | INSERT_PADDING_BYTES(0x4); | ||
| 28 | u32_le file_size; | ||
| 29 | INSERT_PADDING_BYTES(0x4); | ||
| 30 | std::array<NroSegmentHeader, 3> segments; // Text, RoData, Data (in that order) | ||
| 31 | u32_le bss_size; | ||
| 32 | INSERT_PADDING_BYTES(0x44); | ||
| 33 | }; | ||
| 34 | static_assert(sizeof(NroHeader) == 0x80, "NroHeader has incorrect size."); | ||
| 35 | |||
| 36 | struct ModHeader { | ||
| 37 | u32_le magic; | ||
| 38 | u32_le dynamic_offset; | ||
| 39 | u32_le bss_start_offset; | ||
| 40 | u32_le bss_end_offset; | ||
| 41 | u32_le unwind_start_offset; | ||
| 42 | u32_le unwind_end_offset; | ||
| 43 | u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base | ||
| 44 | }; | ||
| 45 | static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size."); | ||
| 46 | |||
| 47 | FileType AppLoader_NRO::IdentifyType(FileUtil::IOFile& file) { | ||
| 48 | // Read NSO header | ||
| 49 | NroHeader nro_header{}; | ||
| 50 | file.Seek(0, SEEK_SET); | ||
| 51 | if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { | ||
| 52 | return FileType::Error; | ||
| 53 | } | ||
| 54 | if (nro_header.magic == MakeMagic('N', 'R', 'O', '0')) { | ||
| 55 | return FileType::NRO; | ||
| 56 | } | ||
| 57 | return FileType::Error; | ||
| 58 | } | ||
| 59 | |||
| 60 | static constexpr u32 PageAlignSize(u32 size) { | ||
| 61 | return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; | ||
| 62 | } | ||
| 63 | |||
| 64 | static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NroSegmentHeader& header) { | ||
| 65 | std::vector<u8> data; | ||
| 66 | data.resize(header.size); | ||
| 67 | |||
| 68 | file.Seek(header.offset + sizeof(NroHeader), SEEK_SET); | ||
| 69 | size_t bytes_read{file.ReadBytes(data.data(), header.size)}; | ||
| 70 | if (header.size != PageAlignSize(static_cast<u32>(bytes_read))) { | ||
| 71 | LOG_CRITICAL(Loader, "Failed to read NRO segment bytes", header.size); | ||
| 72 | return {}; | ||
| 73 | } | ||
| 74 | |||
| 75 | return data; | ||
| 76 | } | ||
| 77 | |||
| 78 | VAddr AppLoader_NRO::GetEntryPoint(VAddr load_base) const { | ||
| 79 | // Find nnMain function, set entrypoint to that address | ||
| 80 | const auto& search = exports.find("nnMain"); | ||
| 81 | if (search != exports.end()) { | ||
| 82 | return load_base + search->second; | ||
| 83 | } | ||
| 84 | const VAddr entry_point{load_base + sizeof(NroHeader)}; | ||
| 85 | LOG_ERROR(Loader, "Unable to find entrypoint, defaulting to: 0x%llx", entry_point); | ||
| 86 | return entry_point; | ||
| 87 | } | ||
| 88 | |||
| 89 | bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { | ||
| 90 | FileUtil::IOFile file(path, "rb"); | ||
| 91 | if (!file.IsOpen()) { | ||
| 92 | return {}; | ||
| 93 | } | ||
| 94 | |||
| 95 | // Read NSO header | ||
| 96 | NroHeader nro_header{}; | ||
| 97 | file.Seek(0, SEEK_SET); | ||
| 98 | if (sizeof(NroHeader) != file.ReadBytes(&nro_header, sizeof(NroHeader))) { | ||
| 99 | return {}; | ||
| 100 | } | ||
| 101 | if (nro_header.magic != MakeMagic('N', 'R', 'O', '0')) { | ||
| 102 | return {}; | ||
| 103 | } | ||
| 104 | |||
| 105 | // Build program image | ||
| 106 | Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create("", 0); | ||
| 107 | std::vector<u8> program_image; | ||
| 108 | program_image.resize(PageAlignSize(nro_header.file_size + nro_header.bss_size)); | ||
| 109 | file.Seek(0, SEEK_SET); | ||
| 110 | file.ReadBytes(program_image.data(), nro_header.file_size); | ||
| 111 | |||
| 112 | for (int i = 0; i < nro_header.segments.size(); ++i) { | ||
| 113 | codeset->segments[i].addr = nro_header.segments[i].offset; | ||
| 114 | codeset->segments[i].offset = nro_header.segments[i].offset; | ||
| 115 | codeset->segments[i].size = PageAlignSize(nro_header.segments[i].size); | ||
| 116 | } | ||
| 117 | |||
| 118 | // Read MOD header | ||
| 119 | ModHeader mod_header{}; | ||
| 120 | u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist | ||
| 121 | std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset, | ||
| 122 | sizeof(ModHeader)); | ||
| 123 | const bool has_mod_header{mod_header.magic == MakeMagic('M', 'O', 'D', '0')}; | ||
| 124 | if (has_mod_header) { | ||
| 125 | // Resize program image to include .bss section and page align each section | ||
| 126 | bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); | ||
| 127 | codeset->data.size += bss_size; | ||
| 128 | } | ||
| 129 | program_image.resize(PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)); | ||
| 130 | |||
| 131 | // Relocate symbols if there was a proper MOD header - This must happen after the image has been | ||
| 132 | // loaded into memory | ||
| 133 | if (has_mod_header) { | ||
| 134 | Relocate(program_image, nro_header.module_header_offset + mod_header.dynamic_offset, | ||
| 135 | load_base); | ||
| 136 | } | ||
| 137 | |||
| 138 | // Load codeset for current process | ||
| 139 | codeset->name = path; | ||
| 140 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); | ||
| 141 | Kernel::g_current_process->LoadModule(codeset, load_base); | ||
| 142 | |||
| 143 | return true; | ||
| 144 | } | ||
| 145 | |||
| 146 | ResultStatus AppLoader_NRO::Load() { | ||
| 147 | if (is_loaded) { | ||
| 148 | return ResultStatus::ErrorAlreadyLoaded; | ||
| 149 | } | ||
| 150 | if (!file.IsOpen()) { | ||
| 151 | return ResultStatus::Error; | ||
| 152 | } | ||
| 153 | |||
| 154 | // Load and relocate "main" and "sdk" NSO | ||
| 155 | static constexpr VAddr main_base{0x10000000}; | ||
| 156 | Kernel::g_current_process = Kernel::Process::Create("main"); | ||
| 157 | if (!LoadNro(filepath, main_base)) { | ||
| 158 | return ResultStatus::ErrorInvalidFormat; | ||
| 159 | } | ||
| 160 | |||
| 161 | Kernel::g_current_process->svc_access_mask.set(); | ||
| 162 | Kernel::g_current_process->address_mappings = default_address_mappings; | ||
| 163 | Kernel::g_current_process->resource_limit = | ||
| 164 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | ||
| 165 | Kernel::g_current_process->Run(GetEntryPoint(main_base), 48, Kernel::DEFAULT_STACK_SIZE); | ||
| 166 | |||
| 167 | ResolveImports(); | ||
| 168 | |||
| 169 | is_loaded = true; | ||
| 170 | return ResultStatus::Success; | ||
| 171 | } | ||
| 172 | |||
| 173 | } // namespace Loader | ||