From 23ce4f5afc66eb04a7aafc4f89685b8109b8d5c6 Mon Sep 17 00:00:00 2001 From: bunnei Date: Mon, 9 Oct 2017 21:39:32 -0400 Subject: [PATCH] loader: Various improvements for NSO/NRO loaders. --- src/core/hle/kernel/process.cpp | 6 ++-- src/core/hle/kernel/thread.cpp | 2 +- src/core/loader/loader.cpp | 4 +-- src/core/loader/nro.cpp | 17 ++--------- src/core/loader/nro.h | 7 ++--- src/core/loader/nso.cpp | 50 ++++++++++++++++----------------- src/core/loader/nso.h | 8 ++---- src/core/memory.h | 4 +-- 8 files changed, 40 insertions(+), 58 deletions(-) diff --git a/src/core/hle/kernel/process.cpp b/src/core/hle/kernel/process.cpp index 2a80c24920..84ebdbc582 100644 --- a/src/core/hle/kernel/process.cpp +++ b/src/core/hle/kernel/process.cpp @@ -147,9 +147,9 @@ void Process::LoadModule(SharedPtr module_, VAddr base_addr) { }; // Map CodeSet segments - MapSegment(module_->code, VMAPermission::ReadExecute, MemoryState::Code); - MapSegment(module_->rodata, VMAPermission::Read, MemoryState::Code); - MapSegment(module_->data, VMAPermission::ReadWrite, MemoryState::Private); + MapSegment(module_->code, VMAPermission::ReadWrite, MemoryState::Private); + MapSegment(module_->rodata, VMAPermission::Read, MemoryState::Static); + MapSegment(module_->data, VMAPermission::ReadWrite, MemoryState::Static); } VAddr Process::GetLinearHeapAreaAddress() const { diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp index 736be50dbf..c01d08ebb8 100644 --- a/src/core/hle/kernel/thread.cpp +++ b/src/core/hle/kernel/thread.cpp @@ -429,7 +429,7 @@ ResultVal> Thread::Create(std::string name, VAddr entry_point, // Map the page to the current process' address space. // TODO(Subv): Find the correct MemoryState for this region. vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE, - linheap_memory, offset, Memory::PAGE_SIZE, MemoryState::Private); + linheap_memory, offset, Memory::PAGE_SIZE, MemoryState::Static); } // Mark the slot as used diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index d96b9f1f06..73318c5844 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -121,11 +121,11 @@ static std::unique_ptr GetFileLoader(FileUtil::IOFile&& file, FileTyp // NX NSO file format. case FileType::NSO: - return std::make_unique(std::move(file), filename, filepath); + return std::make_unique(std::move(file), filepath); // NX NRO file format. case FileType::NRO: - return std::make_unique(std::move(file), filename, filepath); + return std::make_unique(std::move(file), filepath); default: return nullptr; diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index ed638e1fac..753e7e08b6 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -75,17 +75,6 @@ static std::vector ReadSegment(FileUtil::IOFile& file, const NroSegmentHeade return data; } -VAddr AppLoader_NRO::GetEntryPoint(VAddr load_base) const { - // Find nnMain function, set entrypoint to that address - const auto& search = exports.find("nnMain"); - if (search != exports.end()) { - return load_base + search->second; - } - const VAddr entry_point{load_base + sizeof(NroHeader)}; - LOG_ERROR(Loader, "Unable to find entrypoint, defaulting to: 0x%llx", entry_point); - return entry_point; -} - bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { FileUtil::IOFile file(path, "rb"); if (!file.IsOpen()) { @@ -152,9 +141,9 @@ ResultStatus AppLoader_NRO::Load() { } // Load and relocate "main" and "sdk" NSO - static constexpr VAddr main_base{0x10000000}; + static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR}; Kernel::g_current_process = Kernel::Process::Create("main"); - if (!LoadNro(filepath, main_base)) { + if (!LoadNro(filepath, base_addr)) { return ResultStatus::ErrorInvalidFormat; } @@ -162,7 +151,7 @@ ResultStatus AppLoader_NRO::Load() { Kernel::g_current_process->address_mappings = default_address_mappings; Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); - Kernel::g_current_process->Run(GetEntryPoint(main_base), 48, Kernel::DEFAULT_STACK_SIZE); + Kernel::g_current_process->Run(base_addr, 48, Kernel::DEFAULT_STACK_SIZE); ResolveImports(); diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h index d145b68d57..c3c7622fd8 100644 --- a/src/core/loader/nro.h +++ b/src/core/loader/nro.h @@ -17,9 +17,8 @@ namespace Loader { /// Loads an NRO file class AppLoader_NRO final : public AppLoader, Linker { public: - AppLoader_NRO(FileUtil::IOFile&& file, std::string filename, std::string filepath) - : AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) { - } + AppLoader_NRO(FileUtil::IOFile&& file, std::string filepath) + : AppLoader(std::move(file)), filepath(std::move(filepath)) {} /** * Returns the type of the file @@ -35,10 +34,8 @@ public: ResultStatus Load() override; private: - VAddr GetEntryPoint(VAddr load_base) const; bool LoadNro(const std::string& path, VAddr load_base); - std::string filename; std::string filepath; }; diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 4d885fef72..ac8d12ecc3 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -70,31 +70,21 @@ static std::vector ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeade std::vector uncompressed_data; uncompressed_data.resize(header.size); - const int bytes_uncompressed = - LZ4_decompress_safe_partial(reinterpret_cast(compressed_data.data()), - reinterpret_cast(uncompressed_data.data()), - compressed_size, header.size, header.size); + const int bytes_uncompressed = LZ4_decompress_safe( + reinterpret_cast(compressed_data.data()), + reinterpret_cast(uncompressed_data.data()), compressed_size, header.size); - ASSERT_MSG(bytes_uncompressed == header.size, "%d != %d", bytes_uncompressed, header.size); + ASSERT_MSG(bytes_uncompressed == header.size && bytes_uncompressed == uncompressed_data.size(), + "%d != %d != %d", bytes_uncompressed, header.size, uncompressed_data.size()); return uncompressed_data; } -VAddr AppLoader_NSO::GetEntryPoint(VAddr load_base) const { - // Find nnMain function, set entrypoint to that address - const auto& search = exports.find("nnMain"); - if (search != exports.end()) { - return search->second; - } - LOG_ERROR(Loader, "Unable to find entrypoint, defaulting to: 0x%llx", load_base); - return load_base; -} - static constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } -bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) { +VAddr AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base, bool relocate) { FileUtil::IOFile file(path, "rb"); if (!file.IsOpen()) { return {}; @@ -137,11 +127,12 @@ bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) { bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); codeset->data.size += bss_size; } - program_image.resize(PageAlignSize(static_cast(program_image.size()) + bss_size)); + const u32 image_size{PageAlignSize(static_cast(program_image.size()) + bss_size)}; + program_image.resize(image_size); // Relocate symbols if there was a proper MOD header - This must happen after the image has been // loaded into memory - if (has_mod_header) { + if (has_mod_header && relocate) { Relocate(program_image, module_offset + mod_header.dynamic_offset, load_base); } @@ -150,7 +141,7 @@ bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) { codeset->memory = std::make_shared>(std::move(program_image)); Kernel::g_current_process->LoadModule(codeset, load_base); - return true; + return load_base + image_size; } ResultStatus AppLoader_NSO::Load() { @@ -161,22 +152,29 @@ ResultStatus AppLoader_NSO::Load() { return ResultStatus::Error; } - // Load and relocate "main" and "sdk" NSO - static constexpr VAddr main_base{0x710000000}; + // Load and relocate "rtld" NSO + static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR}; Kernel::g_current_process = Kernel::Process::Create("main"); - if (!LoadNso(filepath, main_base)) { + VAddr next_base_addr{LoadNso(filepath, base_addr)}; + if (!next_base_addr) { return ResultStatus::ErrorInvalidFormat; } - const std::string sdkpath = filepath.substr(0, filepath.find_last_of("/\\")) + "/sdk"; - if (!LoadNso(sdkpath, 0x720000000)) { - LOG_WARNING(Loader, "failed to find SDK NSO"); + + // Load and relocate remaining submodules + for (const auto& module_name : {"main", "sdk", "subsdk0", "subsdk1"}) { + const std::string module_path = + filepath.substr(0, filepath.find_last_of("/\\")) + "/" + module_name; + next_base_addr = LoadNso(module_path, next_base_addr); + if (!next_base_addr) { + LOG_WARNING(Loader, "failed to find load module: %s", module_name); + } } Kernel::g_current_process->svc_access_mask.set(); Kernel::g_current_process->address_mappings = default_address_mappings; Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); - Kernel::g_current_process->Run(GetEntryPoint(main_base), 48, Kernel::DEFAULT_STACK_SIZE); + Kernel::g_current_process->Run(base_addr, 48, Kernel::DEFAULT_STACK_SIZE); ResolveImports(); diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 431b960b1a..c29803d812 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -17,8 +17,8 @@ namespace Loader { /// Loads an NSO file class AppLoader_NSO final : public AppLoader, Linker { public: - AppLoader_NSO(FileUtil::IOFile&& file, std::string filename, std::string filepath) - : AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) { + AppLoader_NSO(FileUtil::IOFile&& file, std::string filepath) + : AppLoader(std::move(file)), filepath(std::move(filepath)) { } /** @@ -35,10 +35,8 @@ public: ResultStatus Load() override; private: - VAddr GetEntryPoint(VAddr load_base) const; - bool LoadNso(const std::string& path, VAddr load_base); + VAddr LoadNso(const std::string& path, VAddr load_base, bool relocate = false); - std::string filename; std::string filepath; }; diff --git a/src/core/memory.h b/src/core/memory.h index e8d796d244..e14d686546 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -65,8 +65,8 @@ enum : PAddr { /// Virtual user-space memory regions enum : VAddr { /// Where the application text, data and bss reside. - PROCESS_IMAGE_VADDR = 0x00100000, - PROCESS_IMAGE_MAX_SIZE = 0x03F00000, + PROCESS_IMAGE_VADDR = 0x08000000, + PROCESS_IMAGE_MAX_SIZE = 0x08000000, PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE, /// Area where IPC buffers are mapped onto.