diff options
| author | bunnei <bunneidev@gmail.com> | 2017-10-09 21:39:32 -0400 | 
|---|---|---|
| committer | bunnei <bunneidev@gmail.com> | 2017-10-09 21:39:32 -0400 | 
| commit | 23ce4f5afc66eb04a7aafc4f89685b8109b8d5c6 (patch) | |
| tree | 168e7793c6d68eb8b195850a056443ea98f430a9 /src/core/loader | |
| parent | 33ea53094cc1f34c27ca295472f01f8dd09a300b (diff) | |
loader: Various improvements for NSO/NRO loaders.
Diffstat (limited to 'src/core/loader')
| -rw-r--r-- | src/core/loader/loader.cpp | 4 | ||||
| -rw-r--r-- | src/core/loader/nro.cpp | 17 | ||||
| -rw-r--r-- | src/core/loader/nro.h | 7 | ||||
| -rw-r--r-- | src/core/loader/nso.cpp | 50 | ||||
| -rw-r--r-- | src/core/loader/nso.h | 8 | 
5 files changed, 34 insertions, 52 deletions
| diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index d96b9f1f0..73318c584 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -121,11 +121,11 @@ static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileTyp      // NX NSO file format.      case FileType::NSO: -        return std::make_unique<AppLoader_NSO>(std::move(file), filename, filepath); +        return std::make_unique<AppLoader_NSO>(std::move(file), filepath);      // NX NRO file format.      case FileType::NRO: -        return std::make_unique<AppLoader_NRO>(std::move(file), filename, filepath); +        return std::make_unique<AppLoader_NRO>(std::move(file), filepath);      default:          return nullptr; diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp index ed638e1fa..753e7e08b 100644 --- a/src/core/loader/nro.cpp +++ b/src/core/loader/nro.cpp @@ -75,17 +75,6 @@ static std::vector<u8> 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 d145b68d5..c3c7622fd 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 4d885fef7..ac8d12ecc 100644 --- a/src/core/loader/nso.cpp +++ b/src/core/loader/nso.cpp @@ -70,31 +70,21 @@ static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeade      std::vector<u8> uncompressed_data;      uncompressed_data.resize(header.size); -    const int bytes_uncompressed = -        LZ4_decompress_safe_partial(reinterpret_cast<const char*>(compressed_data.data()), -                                    reinterpret_cast<char*>(uncompressed_data.data()), -                                    compressed_size, header.size, header.size); +    const int bytes_uncompressed = LZ4_decompress_safe( +        reinterpret_cast<const char*>(compressed_data.data()), +        reinterpret_cast<char*>(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<u32>(program_image.size()) + bss_size)); +    const u32 image_size{PageAlignSize(static_cast<u32>(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::vector<u8>>(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 431b960b1..c29803d81 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;  }; | 
