loader: Various improvements for NSO/NRO loaders.

master
bunnei 2017-10-09 21:39:32 +07:00
parent 33ea53094c
commit 23ce4f5afc
8 changed files with 40 additions and 58 deletions

@ -147,9 +147,9 @@ void Process::LoadModule(SharedPtr<CodeSet> 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 {

@ -429,7 +429,7 @@ ResultVal<SharedPtr<Thread>> 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

@ -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;

@ -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();

@ -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;
};

@ -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();

@ -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;
};

@ -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.