diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/file_sys/archive_savedata.cpp | 9 | ||||
| -rw-r--r-- | src/core/hle/kernel/process.cpp | 9 | ||||
| -rw-r--r-- | src/core/hle/kernel/process.h | 9 | ||||
| -rw-r--r-- | src/core/loader/3dsx.cpp | 5 | ||||
| -rw-r--r-- | src/core/loader/elf.cpp | 5 | ||||
| -rw-r--r-- | src/core/loader/ncch.cpp | 5 | ||||
| -rw-r--r-- | src/core/loader/nso.cpp | 223 | ||||
| -rw-r--r-- | src/core/loader/nso.h | 25 | ||||
| -rw-r--r-- | src/tests/core/hle/kernel/hle_ipc.cpp | 4 |
9 files changed, 162 insertions, 132 deletions
diff --git a/src/core/file_sys/archive_savedata.cpp b/src/core/file_sys/archive_savedata.cpp index 61f7654f7..67076c73f 100644 --- a/src/core/file_sys/archive_savedata.cpp +++ b/src/core/file_sys/archive_savedata.cpp | |||
| @@ -15,16 +15,19 @@ ArchiveFactory_SaveData::ArchiveFactory_SaveData( | |||
| 15 | : sd_savedata_source(sd_savedata) {} | 15 | : sd_savedata_source(sd_savedata) {} |
| 16 | 16 | ||
| 17 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const Path& path) { | 17 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const Path& path) { |
| 18 | return sd_savedata_source->Open(Kernel::g_current_process->codeset->program_id); | 18 | UNIMPLEMENTED(); |
| 19 | return {}; //sd_savedata_source->Open(Kernel::g_current_process->codeset->program_id); | ||
| 19 | } | 20 | } |
| 20 | 21 | ||
| 21 | ResultCode ArchiveFactory_SaveData::Format(const Path& path, | 22 | ResultCode ArchiveFactory_SaveData::Format(const Path& path, |
| 22 | const FileSys::ArchiveFormatInfo& format_info) { | 23 | const FileSys::ArchiveFormatInfo& format_info) { |
| 23 | return sd_savedata_source->Format(Kernel::g_current_process->codeset->program_id, format_info); | 24 | UNIMPLEMENTED(); |
| 25 | return RESULT_SUCCESS; //sd_savedata_source->Format(Kernel::g_current_process->codeset->program_id, format_info); | ||
| 24 | } | 26 | } |
| 25 | 27 | ||
| 26 | ResultVal<ArchiveFormatInfo> ArchiveFactory_SaveData::GetFormatInfo(const Path& path) const { | 28 | ResultVal<ArchiveFormatInfo> ArchiveFactory_SaveData::GetFormatInfo(const Path& path) const { |
| 27 | return sd_savedata_source->GetFormatInfo(Kernel::g_current_process->codeset->program_id); | 29 | UNIMPLEMENTED(); |
| 30 | return {}; //sd_savedata_source->GetFormatInfo(Kernel::g_current_process->codeset->program_id); | ||
| 28 | } | 31 | } |
| 29 | 32 | ||
| 30 | } // namespace FileSys | 33 | } // namespace FileSys |
diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 2b6634069..2a80c2492 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp | |||
| @@ -30,10 +30,10 @@ CodeSet::~CodeSet() {} | |||
| 30 | 30 | ||
| 31 | u32 Process::next_process_id; | 31 | u32 Process::next_process_id; |
| 32 | 32 | ||
| 33 | SharedPtr<Process> Process::Create(SharedPtr<CodeSet> code_set) { | 33 | SharedPtr<Process> Process::Create(std::string&& name) { |
| 34 | SharedPtr<Process> process(new Process); | 34 | SharedPtr<Process> process(new Process); |
| 35 | 35 | ||
| 36 | process->codeset = code_set; | 36 | process->name = std::move(name); |
| 37 | process->flags.raw = 0; | 37 | process->flags.raw = 0; |
| 38 | process->flags.memory_region.Assign(MemoryRegion::APPLICATION); | 38 | process->flags.memory_region.Assign(MemoryRegion::APPLICATION); |
| 39 | 39 | ||
| @@ -112,7 +112,7 @@ void Process::ParseKernelCaps(const u32* kernel_caps, size_t len) { | |||
| 112 | } | 112 | } |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | void Process::Run(s32 main_thread_priority, u32 stack_size) { | 115 | void Process::Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size) { |
| 116 | // Allocate and map stack | 116 | // Allocate and map stack |
| 117 | vm_manager | 117 | vm_manager |
| 118 | .MapMemoryBlock(Memory::HEAP_VADDR_END - stack_size, | 118 | .MapMemoryBlock(Memory::HEAP_VADDR_END - stack_size, |
| @@ -129,7 +129,8 @@ void Process::Run(s32 main_thread_priority, u32 stack_size) { | |||
| 129 | } | 129 | } |
| 130 | 130 | ||
| 131 | vm_manager.LogLayout(Log::Level::Debug); | 131 | vm_manager.LogLayout(Log::Level::Debug); |
| 132 | Kernel::SetupMainThread(codeset->entrypoint, main_thread_priority); | 132 | Kernel::SetupMainThread(entry_point, main_thread_priority); |
| 133 | } | ||
| 133 | 134 | ||
| 134 | void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) { | 135 | void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) { |
| 135 | memory_region = GetMemoryRegion(flags.memory_region); | 136 | memory_region = GetMemoryRegion(flags.memory_region); |
diff --git a/src/core/hle/kernel/process.h b/src/core/hle/kernel/process.h index 7350c6c41..f05f2703e 100644 --- a/src/core/hle/kernel/process.h +++ b/src/core/hle/kernel/process.h | |||
| @@ -93,13 +93,13 @@ private: | |||
| 93 | 93 | ||
| 94 | class Process final : public Object { | 94 | class Process final : public Object { |
| 95 | public: | 95 | public: |
| 96 | static SharedPtr<Process> Create(SharedPtr<CodeSet> code_set); | 96 | static SharedPtr<Process> Create(std::string&& name); |
| 97 | 97 | ||
| 98 | std::string GetTypeName() const override { | 98 | std::string GetTypeName() const override { |
| 99 | return "Process"; | 99 | return "Process"; |
| 100 | } | 100 | } |
| 101 | std::string GetName() const override { | 101 | std::string GetName() const override { |
| 102 | return codeset->name; | 102 | return name; |
| 103 | } | 103 | } |
| 104 | 104 | ||
| 105 | static const HandleType HANDLE_TYPE = HandleType::Process; | 105 | static const HandleType HANDLE_TYPE = HandleType::Process; |
| @@ -109,7 +109,6 @@ public: | |||
| 109 | 109 | ||
| 110 | static u32 next_process_id; | 110 | static u32 next_process_id; |
| 111 | 111 | ||
| 112 | SharedPtr<CodeSet> codeset; | ||
| 113 | /// Resource limit descriptor for this process | 112 | /// Resource limit descriptor for this process |
| 114 | SharedPtr<ResourceLimit> resource_limit; | 113 | SharedPtr<ResourceLimit> resource_limit; |
| 115 | 114 | ||
| @@ -138,7 +137,7 @@ public: | |||
| 138 | /** | 137 | /** |
| 139 | * Applies address space changes and launches the process main thread. | 138 | * Applies address space changes and launches the process main thread. |
| 140 | */ | 139 | */ |
| 141 | void Run(s32 main_thread_priority, u32 stack_size); | 140 | void Run(VAddr entry_point, s32 main_thread_priority, u32 stack_size); |
| 142 | 141 | ||
| 143 | void LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr); | 142 | void LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr); |
| 144 | 143 | ||
| @@ -166,6 +165,8 @@ public: | |||
| 166 | /// This vector will grow as more pages are allocated for new threads. | 165 | /// This vector will grow as more pages are allocated for new threads. |
| 167 | std::vector<std::bitset<8>> tls_slots; | 166 | std::vector<std::bitset<8>> tls_slots; |
| 168 | 167 | ||
| 168 | std::string name; | ||
| 169 | |||
| 169 | VAddr GetLinearHeapAreaAddress() const; | 170 | VAddr GetLinearHeapAreaAddress() const; |
| 170 | VAddr GetLinearHeapBase() const; | 171 | VAddr GetLinearHeapBase() const; |
| 171 | VAddr GetLinearHeapLimit() const; | 172 | VAddr GetLinearHeapLimit() const; |
diff --git a/src/core/loader/3dsx.cpp b/src/core/loader/3dsx.cpp index 74e336487..209328347 100644 --- a/src/core/loader/3dsx.cpp +++ b/src/core/loader/3dsx.cpp | |||
| @@ -267,7 +267,8 @@ ResultStatus AppLoader_THREEDSX::Load() { | |||
| 267 | return ResultStatus::Error; | 267 | return ResultStatus::Error; |
| 268 | codeset->name = filename; | 268 | codeset->name = filename; |
| 269 | 269 | ||
| 270 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); | 270 | Kernel::g_current_process = Kernel::Process::Create("main"); |
| 271 | Kernel::g_current_process->LoadModule(codeset, codeset->entrypoint); | ||
| 271 | Kernel::g_current_process->svc_access_mask.set(); | 272 | Kernel::g_current_process->svc_access_mask.set(); |
| 272 | Kernel::g_current_process->address_mappings = default_address_mappings; | 273 | Kernel::g_current_process->address_mappings = default_address_mappings; |
| 273 | 274 | ||
| @@ -275,7 +276,7 @@ ResultStatus AppLoader_THREEDSX::Load() { | |||
| 275 | Kernel::g_current_process->resource_limit = | 276 | Kernel::g_current_process->resource_limit = |
| 276 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | 277 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); |
| 277 | 278 | ||
| 278 | Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); | 279 | Kernel::g_current_process->Run(codeset->entrypoint, 48, Kernel::DEFAULT_STACK_SIZE); |
| 279 | 280 | ||
| 280 | Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_SelfNCCH>(*this), | 281 | Service::FS::RegisterArchiveType(std::make_unique<FileSys::ArchiveFactory_SelfNCCH>(*this), |
| 281 | Service::FS::ArchiveIdCode::SelfNCCH); | 282 | Service::FS::ArchiveIdCode::SelfNCCH); |
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 055bc39de..2efc67ff8 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp | |||
| @@ -401,7 +401,8 @@ ResultStatus AppLoader_ELF::Load() { | |||
| 401 | SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); | 401 | SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR); |
| 402 | codeset->name = filename; | 402 | codeset->name = filename; |
| 403 | 403 | ||
| 404 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); | 404 | Kernel::g_current_process = Kernel::Process::Create("main"); |
| 405 | Kernel::g_current_process->LoadModule(codeset, codeset->entrypoint); | ||
| 405 | Kernel::g_current_process->svc_access_mask.set(); | 406 | Kernel::g_current_process->svc_access_mask.set(); |
| 406 | Kernel::g_current_process->address_mappings = default_address_mappings; | 407 | Kernel::g_current_process->address_mappings = default_address_mappings; |
| 407 | 408 | ||
| @@ -409,7 +410,7 @@ ResultStatus AppLoader_ELF::Load() { | |||
| 409 | Kernel::g_current_process->resource_limit = | 410 | Kernel::g_current_process->resource_limit = |
| 410 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); | 411 | Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); |
| 411 | 412 | ||
| 412 | Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE); | 413 | Kernel::g_current_process->Run(codeset->entrypoint, 48, Kernel::DEFAULT_STACK_SIZE); |
| 413 | 414 | ||
| 414 | is_loaded = true; | 415 | is_loaded = true; |
| 415 | return ResultStatus::Success; | 416 | return ResultStatus::Success; |
diff --git a/src/core/loader/ncch.cpp b/src/core/loader/ncch.cpp index fc4d14a59..728886ea8 100644 --- a/src/core/loader/ncch.cpp +++ b/src/core/loader/ncch.cpp | |||
| @@ -168,7 +168,8 @@ ResultStatus AppLoader_NCCH::LoadExec() { | |||
| 168 | codeset->entrypoint = codeset->code.addr; | 168 | codeset->entrypoint = codeset->code.addr; |
| 169 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); | 169 | codeset->memory = std::make_shared<std::vector<u8>>(std::move(code)); |
| 170 | 170 | ||
| 171 | Kernel::g_current_process = Kernel::Process::Create(std::move(codeset)); | 171 | Kernel::g_current_process = Kernel::Process::Create("main"); |
| 172 | Kernel::g_current_process->LoadModule(codeset, codeset->entrypoint); | ||
| 172 | 173 | ||
| 173 | // Attach a resource limit to the process based on the resource limit category | 174 | // Attach a resource limit to the process based on the resource limit category |
| 174 | Kernel::g_current_process->resource_limit = | 175 | Kernel::g_current_process->resource_limit = |
| @@ -187,7 +188,7 @@ ResultStatus AppLoader_NCCH::LoadExec() { | |||
| 187 | 188 | ||
| 188 | s32 priority = exheader_header.arm11_system_local_caps.priority; | 189 | s32 priority = exheader_header.arm11_system_local_caps.priority; |
| 189 | u32 stack_size = exheader_header.codeset_info.stack_size; | 190 | u32 stack_size = exheader_header.codeset_info.stack_size; |
| 190 | Kernel::g_current_process->Run(priority, stack_size); | 191 | Kernel::g_current_process->Run(codeset->entrypoint, priority, stack_size); |
| 191 | return ResultStatus::Success; | 192 | return ResultStatus::Success; |
| 192 | } | 193 | } |
| 193 | return ResultStatus::Error; | 194 | return ResultStatus::Error; |
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 | ||
| 17 | using Kernel::CodeSet; | ||
| 18 | using Kernel::SharedPtr; | ||
| 19 | |||
| 20 | namespace Loader { | 15 | namespace Loader { |
| 21 | 16 | ||
| 22 | FileType AppLoader_NSO::IdentifyType(FileUtil::IOFile& file) { | 17 | enum 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) | 19 | enum 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 | ||
| 34 | struct NsoSegmentHeader { | 30 | struct NsoSegmentHeader { |
| 35 | u32_le offset; | 31 | u32_le offset; |
| @@ -42,13 +38,40 @@ static_assert(sizeof(NsoSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect | |||
| 42 | struct NsoHeader { | 38 | struct 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 | |||
| 50 | static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size."); | 46 | static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size."); |
| 51 | 47 | ||
| 48 | struct 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 | }; | ||
| 59 | static_assert(sizeof(ModHeader) == 0x24, "ModHeader has incorrect size."); | ||
| 60 | |||
| 61 | FileType 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 | |||
| 52 | static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeader& header, | 75 | static 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 | ||
| 75 | struct Symbol { | 98 | void 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 | |||
| 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) { | 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 | ||
| 152 | void Relocate(VAddr loadbase, std::map<std::string, Import>& imports, | 145 | void 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 | ||
| 208 | static VAddr GetEntryPoint(const std::map<std::string, VAddr>& exports) { | 193 | VAddr 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 | ||
| 217 | static SharedPtr<CodeSet> LoadModule(const std::string& filepath, VAddr loadbase, | 203 | static 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()) | 207 | bool 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 | ||
| 251 | ResultStatus AppLoader_NSO::Load() { | 259 | ResultStatus 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 | ||
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 39a9bd3d9..2d9e60ad7 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h | |||
| @@ -4,9 +4,11 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <map> | ||
| 7 | #include <string> | 8 | #include <string> |
| 8 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 9 | #include "common/file_util.h" | 10 | #include "common/file_util.h" |
| 11 | #include "core/hle/kernel/kernel.h" | ||
| 10 | #include "core/loader/loader.h" | 12 | #include "core/loader/loader.h" |
| 11 | 13 | ||
| 12 | namespace Loader { | 14 | namespace Loader { |
| @@ -15,7 +17,8 @@ namespace Loader { | |||
| 15 | class AppLoader_NSO final : public AppLoader { | 17 | class AppLoader_NSO final : public AppLoader { |
| 16 | public: | 18 | public: |
| 17 | AppLoader_NSO(FileUtil::IOFile&& file, std::string filename, std::string filepath) | 19 | AppLoader_NSO(FileUtil::IOFile&& file, std::string filename, std::string filepath) |
| 18 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) {} | 20 | : AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) { |
| 21 | } | ||
| 19 | 22 | ||
| 20 | /** | 23 | /** |
| 21 | * Returns the type of the file | 24 | * Returns the type of the file |
| @@ -31,6 +34,26 @@ public: | |||
| 31 | ResultStatus Load() override; | 34 | ResultStatus Load() override; |
| 32 | 35 | ||
| 33 | private: | 36 | private: |
| 37 | struct Symbol { | ||
| 38 | Symbol(std::string&& name, u64 value) : name(std::move(name)), value(value) {} | ||
| 39 | std::string name; | ||
| 40 | u64 value; | ||
| 41 | }; | ||
| 42 | |||
| 43 | struct Import { | ||
| 44 | VAddr ea; | ||
| 45 | s64 addend; | ||
| 46 | }; | ||
| 47 | |||
| 48 | void WriteRelocations(const std::vector<Symbol>& symbols, VAddr load_base, | ||
| 49 | u64 relocation_offset, u64 size, bool is_jump_relocation); | ||
| 50 | VAddr GetEntryPoint() const; | ||
| 51 | bool LoadNso(const std::string& path, VAddr load_base); | ||
| 52 | void Relocate(VAddr load_base, VAddr dynamic_section_addr); | ||
| 53 | |||
| 54 | std::map<std::string, Import> imports; | ||
| 55 | std::map<std::string, VAddr> exports; | ||
| 56 | |||
| 34 | std::string filename; | 57 | std::string filename; |
| 35 | std::string filepath; | 58 | std::string filepath; |
| 36 | }; | 59 | }; |
diff --git a/src/tests/core/hle/kernel/hle_ipc.cpp b/src/tests/core/hle/kernel/hle_ipc.cpp index 52336d027..4143a3ab8 100644 --- a/src/tests/core/hle/kernel/hle_ipc.cpp +++ b/src/tests/core/hle/kernel/hle_ipc.cpp | |||
| @@ -22,7 +22,7 @@ TEST_CASE("HLERequestContext::PopulateFromIncomingCommandBuffer", "[core][kernel | |||
| 22 | auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); | 22 | auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); |
| 23 | HLERequestContext context(std::move(session)); | 23 | HLERequestContext context(std::move(session)); |
| 24 | 24 | ||
| 25 | auto process = Process::Create(CodeSet::Create("", 0)); | 25 | auto process = Process::Create(""); |
| 26 | HandleTable handle_table; | 26 | HandleTable handle_table; |
| 27 | 27 | ||
| 28 | SECTION("works with empty cmdbuf") { | 28 | SECTION("works with empty cmdbuf") { |
| @@ -142,7 +142,7 @@ TEST_CASE("HLERequestContext::WriteToOutgoingCommandBuffer", "[core][kernel]") { | |||
| 142 | auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); | 142 | auto session = std::get<SharedPtr<ServerSession>>(ServerSession::CreateSessionPair()); |
| 143 | HLERequestContext context(std::move(session)); | 143 | HLERequestContext context(std::move(session)); |
| 144 | 144 | ||
| 145 | auto process = Process::Create(CodeSet::Create("", 0)); | 145 | auto process = Process::Create(""); |
| 146 | HandleTable handle_table; | 146 | HandleTable handle_table; |
| 147 | auto* input = context.CommandBuffer(); | 147 | auto* input = context.CommandBuffer(); |
| 148 | u32_le output[IPC::COMMAND_BUFFER_LENGTH]; | 148 | u32_le output[IPC::COMMAND_BUFFER_LENGTH]; |