diff options
Diffstat (limited to 'src/core/loader')
-rw-r--r-- | src/core/loader/elf.cpp | 1 | ||||
-rw-r--r-- | src/core/loader/linker.cpp | 147 | ||||
-rw-r--r-- | src/core/loader/linker.h | 36 | ||||
-rw-r--r-- | src/core/loader/nro.cpp | 1 | ||||
-rw-r--r-- | src/core/loader/nro.h | 4 | ||||
-rw-r--r-- | src/core/loader/nso.cpp | 114 | ||||
-rw-r--r-- | src/core/loader/nso.h | 43 |
7 files changed, 100 insertions, 246 deletions
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 6057c7f26..8b1920f22 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp @@ -9,6 +9,7 @@ #include "common/common_types.h" #include "common/file_util.h" #include "common/logging/log.h" +#include "core/hle/kernel/code_set.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/vm_manager.h" #include "core/loader/elf.h" diff --git a/src/core/loader/linker.cpp b/src/core/loader/linker.cpp deleted file mode 100644 index 57ca8c3ee..000000000 --- a/src/core/loader/linker.cpp +++ /dev/null @@ -1,147 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include <vector> - -#include "common/common_funcs.h" -#include "common/logging/log.h" -#include "common/swap.h" -#include "core/loader/linker.h" -#include "core/memory.h" - -namespace Loader { - -enum class RelocationType : u32 { ABS64 = 257, GLOB_DAT = 1025, JUMP_SLOT = 1026, RELATIVE = 1027 }; - -enum DynamicType : u32 { - DT_NULL = 0, - DT_PLTRELSZ = 2, - DT_STRTAB = 5, - DT_SYMTAB = 6, - DT_RELA = 7, - DT_RELASZ = 8, - DT_STRSZ = 10, - DT_JMPREL = 23, -}; - -struct Elf64_Rela { - u64_le offset; - RelocationType type; - u32_le symbol; - s64_le addend; -}; -static_assert(sizeof(Elf64_Rela) == 0x18, "Elf64_Rela has incorrect size."); - -struct Elf64_Dyn { - u64_le tag; - u64_le value; -}; -static_assert(sizeof(Elf64_Dyn) == 0x10, "Elf64_Dyn has incorrect size."); - -struct Elf64_Sym { - u32_le name; - INSERT_PADDING_BYTES(0x2); - u16_le shndx; - u64_le value; - u64_le size; -}; -static_assert(sizeof(Elf64_Sym) == 0x18, "Elf64_Sym has incorrect size."); - -void Linker::WriteRelocations(std::vector<u8>& program_image, const std::vector<Symbol>& symbols, - u64 relocation_offset, u64 size, VAddr load_base) { - for (u64 i = 0; i < size; i += sizeof(Elf64_Rela)) { - Elf64_Rela rela; - std::memcpy(&rela, &program_image[relocation_offset + i], sizeof(Elf64_Rela)); - - const Symbol& symbol = symbols[rela.symbol]; - switch (rela.type) { - case RelocationType::RELATIVE: { - const u64 value = load_base + rela.addend; - if (!symbol.name.empty()) { - exports[symbol.name] = value; - } - std::memcpy(&program_image[rela.offset], &value, sizeof(u64)); - break; - } - case RelocationType::JUMP_SLOT: - case RelocationType::GLOB_DAT: - if (!symbol.value) { - imports[symbol.name] = {rela.offset + load_base, 0}; - } else { - exports[symbol.name] = symbol.value; - std::memcpy(&program_image[rela.offset], &symbol.value, sizeof(u64)); - } - break; - case RelocationType::ABS64: - if (!symbol.value) { - imports[symbol.name] = {rela.offset + load_base, rela.addend}; - } else { - const u64 value = symbol.value + rela.addend; - exports[symbol.name] = value; - std::memcpy(&program_image[rela.offset], &value, sizeof(u64)); - } - break; - default: - LOG_CRITICAL(Loader, "Unknown relocation type: {}", static_cast<int>(rela.type)); - break; - } - } -} - -void Linker::Relocate(std::vector<u8>& program_image, u32 dynamic_section_offset, VAddr load_base) { - std::map<u64, u64> dynamic; - while (dynamic_section_offset < program_image.size()) { - Elf64_Dyn dyn; - std::memcpy(&dyn, &program_image[dynamic_section_offset], sizeof(Elf64_Dyn)); - dynamic_section_offset += sizeof(Elf64_Dyn); - - if (dyn.tag == DT_NULL) { - break; - } - dynamic[dyn.tag] = dyn.value; - } - - u64 offset = dynamic[DT_SYMTAB]; - std::vector<Symbol> symbols; - while (offset < program_image.size()) { - Elf64_Sym sym; - std::memcpy(&sym, &program_image[offset], sizeof(Elf64_Sym)); - offset += sizeof(Elf64_Sym); - - if (sym.name >= dynamic[DT_STRSZ]) { - break; - } - - std::string name = reinterpret_cast<char*>(&program_image[dynamic[DT_STRTAB] + sym.name]); - if (sym.value) { - exports[name] = load_base + sym.value; - symbols.emplace_back(std::move(name), load_base + sym.value); - } else { - symbols.emplace_back(std::move(name), 0); - } - } - - if (dynamic.find(DT_RELA) != dynamic.end()) { - WriteRelocations(program_image, symbols, dynamic[DT_RELA], dynamic[DT_RELASZ], load_base); - } - - if (dynamic.find(DT_JMPREL) != dynamic.end()) { - WriteRelocations(program_image, symbols, dynamic[DT_JMPREL], dynamic[DT_PLTRELSZ], - load_base); - } -} - -void Linker::ResolveImports() { - // Resolve imports - for (const auto& import : imports) { - const auto& search = exports.find(import.first); - if (search != exports.end()) { - Memory::Write64(import.second.ea, search->second + import.second.addend); - } else { - LOG_ERROR(Loader, "Unresolved import: {}", import.first); - } - } -} - -} // namespace Loader diff --git a/src/core/loader/linker.h b/src/core/loader/linker.h deleted file mode 100644 index 107625837..000000000 --- a/src/core/loader/linker.h +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2018 yuzu emulator team -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include <map> -#include <string> -#include "common/common_types.h" - -namespace Loader { - -class Linker { -protected: - struct Symbol { - Symbol(std::string&& name, u64 value) : name(std::move(name)), value(value) {} - std::string name; - u64 value; - }; - - struct Import { - VAddr ea; - s64 addend; - }; - - void WriteRelocations(std::vector<u8>& program_image, const std::vector<Symbol>& symbols, - u64 relocation_offset, u64 size, VAddr load_base); - void Relocate(std::vector<u8>& program_image, u32 dynamic_section_offset, VAddr load_base); - - void ResolveImports(); - - std::map<std::string, Import> imports; - std::map<std::string, VAddr> exports; -}; - -} // namespace Loader diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index 4fad0c0dd..5de02a94b 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -14,6 +14,7 @@ #include "core/file_sys/romfs_factory.h" #include "core/file_sys/vfs_offset.h" #include "core/gdbstub/gdbstub.h" +#include "core/hle/kernel/code_set.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/vm_manager.h" #include "core/hle/service/filesystem/filesystem.h" diff --git a/src/core/loader/nro.h b/src/core/loader/nro.h index 013d629c0..85b0ed644 100644 --- a/src/core/loader/nro.h +++ b/src/core/loader/nro.h @@ -4,10 +4,10 @@ #pragma once +#include <memory> #include <string> #include <vector> #include "common/common_types.h" -#include "core/loader/linker.h" #include "core/loader/loader.h" namespace FileSys { @@ -21,7 +21,7 @@ class Process; namespace Loader { /// Loads an NRO file -class AppLoader_NRO final : public AppLoader, Linker { +class AppLoader_NRO final : public AppLoader { public: explicit AppLoader_NRO(FileSys::VirtualFile file); ~AppLoader_NRO() override; diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp index 6ded0b707..714d85a59 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -7,10 +7,13 @@ #include <lz4.h> #include "common/common_funcs.h" #include "common/file_util.h" +#include "common/hex_util.h" #include "common/logging/log.h" #include "common/swap.h" +#include "core/core.h" #include "core/file_sys/patch_manager.h" #include "core/gdbstub/gdbstub.h" +#include "core/hle/kernel/code_set.h" #include "core/hle/kernel/process.h" #include "core/hle/kernel/vm_manager.h" #include "core/loader/nso.h" @@ -18,36 +21,8 @@ #include "core/settings.h" namespace Loader { - -struct NsoSegmentHeader { - u32_le offset; - u32_le location; - u32_le size; - union { - u32_le alignment; - u32_le bss_size; - }; -}; -static_assert(sizeof(NsoSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect size."); - -struct NsoHeader { - u32_le magic; - u32_le version; - INSERT_PADDING_WORDS(1); - u8 flags; - std::array<NsoSegmentHeader, 3> segments; // Text, RoData, Data (in that order) - std::array<u8, 0x20> build_id; - std::array<u32_le, 3> segments_compressed_size; - - bool IsSegmentCompressed(size_t segment_num) const { - ASSERT_MSG(segment_num < 3, "Invalid segment {}", segment_num); - return ((flags >> segment_num) & 1); - } -}; -static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size."); -static_assert(std::is_trivially_copyable_v<NsoHeader>, "NsoHeader isn't trivially copyable."); - -struct ModHeader { +namespace { +struct MODHeader { u32_le magic; u32_le dynamic_offset; u32_le bss_start_offset; @@ -56,25 +31,10 @@ struct ModHeader { u32_le eh_frame_hdr_end_offset; u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base }; -static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size."); - -AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {} +static_assert(sizeof(MODHeader) == 0x1c, "MODHeader has incorrect size."); -FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) { - u32 magic = 0; - if (file->ReadObject(&magic) != sizeof(magic)) { - return FileType::Error; - } - - if (Common::MakeMagic('N', 'S', 'O', '0') != magic) { - return FileType::Error; - } - - return FileType::NSO; -} - -static std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data, - const NsoSegmentHeader& header) { +std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data, + const NSOSegmentHeader& header) { std::vector<u8> uncompressed_data(header.size); const int bytes_uncompressed = LZ4_decompress_safe(reinterpret_cast<const char*>(compressed_data.data()), @@ -88,23 +48,47 @@ static std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data, return uncompressed_data; } -static constexpr u32 PageAlignSize(u32 size) { +constexpr u32 PageAlignSize(u32 size) { return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; } +} // Anonymous namespace + +bool NSOHeader::IsSegmentCompressed(size_t segment_num) const { + ASSERT_MSG(segment_num < 3, "Invalid segment {}", segment_num); + return ((flags >> segment_num) & 1) != 0; +} + +AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {} + +FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) { + u32 magic = 0; + if (file->ReadObject(&magic) != sizeof(magic)) { + return FileType::Error; + } + + if (Common::MakeMagic('N', 'S', 'O', '0') != magic) { + return FileType::Error; + } + + return FileType::NSO; +} std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, const FileSys::VfsFile& file, VAddr load_base, bool should_pass_arguments, std::optional<FileSys::PatchManager> pm) { - if (file.GetSize() < sizeof(NsoHeader)) + if (file.GetSize() < sizeof(NSOHeader)) { return {}; + } - NsoHeader nso_header{}; - if (sizeof(NsoHeader) != file.ReadObject(&nso_header)) + NSOHeader nso_header{}; + if (sizeof(NSOHeader) != file.ReadObject(&nso_header)) { return {}; + } - if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) + if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) { return {}; + } // Build program image Kernel::CodeSet codeset; @@ -140,10 +124,10 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, std::memcpy(&module_offset, program_image.data() + 4, sizeof(u32)); // Read MOD header - ModHeader mod_header{}; + MODHeader mod_header{}; // Default .bss to size in segment header if MOD0 section doesn't exist u32 bss_size{PageAlignSize(nso_header.segments[2].bss_size)}; - std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader)); + std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(MODHeader)); const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')}; if (has_mod_header) { // Resize program image to include .bss section and page align each section @@ -155,13 +139,25 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process, // Apply patches if necessary if (pm && (pm->HasNSOPatch(nso_header.build_id) || Settings::values.dump_nso)) { - std::vector<u8> pi_header(program_image.size() + 0x100); - std::memcpy(pi_header.data(), &nso_header, sizeof(NsoHeader)); - std::memcpy(pi_header.data() + 0x100, program_image.data(), program_image.size()); + std::vector<u8> pi_header(sizeof(NSOHeader) + program_image.size()); + pi_header.insert(pi_header.begin(), reinterpret_cast<u8*>(&nso_header), + reinterpret_cast<u8*>(&nso_header) + sizeof(NSOHeader)); + pi_header.insert(pi_header.begin() + sizeof(NSOHeader), program_image.begin(), + program_image.end()); pi_header = pm->PatchNSO(pi_header); - std::memcpy(program_image.data(), pi_header.data() + 0x100, program_image.size()); + std::copy(pi_header.begin() + sizeof(NSOHeader), pi_header.end(), program_image.begin()); + } + + // Apply cheats if they exist and the program has a valid title ID + if (pm) { + auto& system = Core::System::GetInstance(); + const auto cheats = pm->CreateCheatList(system, nso_header.build_id); + if (!cheats.empty()) { + system.RegisterCheatList(cheats, Common::HexArrayToString(nso_header.build_id), + load_base, load_base + program_image.size()); + } } // Load codeset for current process diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h index 135b6ea5a..4674c3724 100644 --- a/src/core/loader/nso.h +++ b/src/core/loader/nso.h @@ -4,10 +4,12 @@ #pragma once +#include <array> #include <optional> +#include <type_traits> #include "common/common_types.h" +#include "common/swap.h" #include "core/file_sys/patch_manager.h" -#include "core/loader/linker.h" #include "core/loader/loader.h" namespace Kernel { @@ -16,6 +18,43 @@ class Process; namespace Loader { +struct NSOSegmentHeader { + u32_le offset; + u32_le location; + u32_le size; + union { + u32_le alignment; + u32_le bss_size; + }; +}; +static_assert(sizeof(NSOSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect size."); + +struct NSOHeader { + using SHA256Hash = std::array<u8, 0x20>; + + struct RODataRelativeExtent { + u32_le data_offset; + u32_le size; + }; + + u32_le magic; + u32_le version; + u32 reserved; + u32_le flags; + std::array<NSOSegmentHeader, 3> segments; // Text, RoData, Data (in that order) + std::array<u8, 0x20> build_id; + std::array<u32_le, 3> segments_compressed_size; + std::array<u8, 0x1C> padding; + RODataRelativeExtent api_info_extent; + RODataRelativeExtent dynstr_extent; + RODataRelativeExtent dynsyn_extent; + std::array<SHA256Hash, 3> segment_hashes; + + bool IsSegmentCompressed(size_t segment_num) const; +}; +static_assert(sizeof(NSOHeader) == 0x100, "NSOHeader has incorrect size."); +static_assert(std::is_trivially_copyable_v<NSOHeader>, "NSOHeader must be trivially copyable."); + constexpr u64 NSO_ARGUMENT_DATA_ALLOCATION_SIZE = 0x9000; struct NSOArgumentHeader { @@ -26,7 +65,7 @@ struct NSOArgumentHeader { static_assert(sizeof(NSOArgumentHeader) == 0x20, "NSOArgumentHeader has incorrect size."); /// Loads an NSO file -class AppLoader_NSO final : public AppLoader, Linker { +class AppLoader_NSO final : public AppLoader { public: explicit AppLoader_NSO(FileSys::VirtualFile file); |