mirror of https://git.suyu.dev/suyu/suyu
loader: Various improvements for NSO/NRO loaders.
This commit is contained in:
parent
33ea53094c
commit
23ce4f5afc
|
@ -147,9 +147,9 @@ void Process::LoadModule(SharedPtr<CodeSet> module_, VAddr base_addr) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Map CodeSet segments
|
// Map CodeSet segments
|
||||||
MapSegment(module_->code, VMAPermission::ReadExecute, MemoryState::Code);
|
MapSegment(module_->code, VMAPermission::ReadWrite, MemoryState::Private);
|
||||||
MapSegment(module_->rodata, VMAPermission::Read, MemoryState::Code);
|
MapSegment(module_->rodata, VMAPermission::Read, MemoryState::Static);
|
||||||
MapSegment(module_->data, VMAPermission::ReadWrite, MemoryState::Private);
|
MapSegment(module_->data, VMAPermission::ReadWrite, MemoryState::Static);
|
||||||
}
|
}
|
||||||
|
|
||||||
VAddr Process::GetLinearHeapAreaAddress() const {
|
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.
|
// Map the page to the current process' address space.
|
||||||
// TODO(Subv): Find the correct MemoryState for this region.
|
// TODO(Subv): Find the correct MemoryState for this region.
|
||||||
vm_manager.MapMemoryBlock(Memory::TLS_AREA_VADDR + available_page * Memory::PAGE_SIZE,
|
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
|
// Mark the slot as used
|
||||||
|
|
|
@ -121,11 +121,11 @@ static std::unique_ptr<AppLoader> GetFileLoader(FileUtil::IOFile&& file, FileTyp
|
||||||
|
|
||||||
// NX NSO file format.
|
// NX NSO file format.
|
||||||
case FileType::NSO:
|
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.
|
// NX NRO file format.
|
||||||
case FileType::NRO:
|
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:
|
default:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
|
@ -75,17 +75,6 @@ static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NroSegmentHeade
|
||||||
return data;
|
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) {
|
bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
|
||||||
FileUtil::IOFile file(path, "rb");
|
FileUtil::IOFile file(path, "rb");
|
||||||
if (!file.IsOpen()) {
|
if (!file.IsOpen()) {
|
||||||
|
@ -152,9 +141,9 @@ ResultStatus AppLoader_NRO::Load() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load and relocate "main" and "sdk" NSO
|
// 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");
|
Kernel::g_current_process = Kernel::Process::Create("main");
|
||||||
if (!LoadNro(filepath, main_base)) {
|
if (!LoadNro(filepath, base_addr)) {
|
||||||
return ResultStatus::ErrorInvalidFormat;
|
return ResultStatus::ErrorInvalidFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,7 +151,7 @@ ResultStatus AppLoader_NRO::Load() {
|
||||||
Kernel::g_current_process->address_mappings = default_address_mappings;
|
Kernel::g_current_process->address_mappings = default_address_mappings;
|
||||||
Kernel::g_current_process->resource_limit =
|
Kernel::g_current_process->resource_limit =
|
||||||
Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
|
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();
|
ResolveImports();
|
||||||
|
|
||||||
|
|
|
@ -17,9 +17,8 @@ namespace Loader {
|
||||||
/// Loads an NRO file
|
/// Loads an NRO file
|
||||||
class AppLoader_NRO final : public AppLoader, Linker {
|
class AppLoader_NRO final : public AppLoader, Linker {
|
||||||
public:
|
public:
|
||||||
AppLoader_NRO(FileUtil::IOFile&& file, std::string filename, std::string filepath)
|
AppLoader_NRO(FileUtil::IOFile&& file, std::string filepath)
|
||||||
: AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) {
|
: AppLoader(std::move(file)), filepath(std::move(filepath)) {}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the type of the file
|
* Returns the type of the file
|
||||||
|
@ -35,10 +34,8 @@ public:
|
||||||
ResultStatus Load() override;
|
ResultStatus Load() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VAddr GetEntryPoint(VAddr load_base) const;
|
|
||||||
bool LoadNro(const std::string& path, VAddr load_base);
|
bool LoadNro(const std::string& path, VAddr load_base);
|
||||||
|
|
||||||
std::string filename;
|
|
||||||
std::string filepath;
|
std::string filepath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -70,31 +70,21 @@ static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NsoSegmentHeade
|
||||||
|
|
||||||
std::vector<u8> uncompressed_data;
|
std::vector<u8> uncompressed_data;
|
||||||
uncompressed_data.resize(header.size);
|
uncompressed_data.resize(header.size);
|
||||||
const int bytes_uncompressed =
|
const int bytes_uncompressed = LZ4_decompress_safe(
|
||||||
LZ4_decompress_safe_partial(reinterpret_cast<const char*>(compressed_data.data()),
|
reinterpret_cast<const char*>(compressed_data.data()),
|
||||||
reinterpret_cast<char*>(uncompressed_data.data()),
|
reinterpret_cast<char*>(uncompressed_data.data()), compressed_size, header.size);
|
||||||
compressed_size, header.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;
|
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) {
|
static constexpr u32 PageAlignSize(u32 size) {
|
||||||
return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
|
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");
|
FileUtil::IOFile file(path, "rb");
|
||||||
if (!file.IsOpen()) {
|
if (!file.IsOpen()) {
|
||||||
return {};
|
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);
|
bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
|
||||||
codeset->data.size += bss_size;
|
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
|
// Relocate symbols if there was a proper MOD header - This must happen after the image has been
|
||||||
// loaded into memory
|
// loaded into memory
|
||||||
if (has_mod_header) {
|
if (has_mod_header && relocate) {
|
||||||
Relocate(program_image, module_offset + mod_header.dynamic_offset, load_base);
|
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));
|
codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
|
||||||
Kernel::g_current_process->LoadModule(codeset, load_base);
|
Kernel::g_current_process->LoadModule(codeset, load_base);
|
||||||
|
|
||||||
return true;
|
return load_base + image_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
ResultStatus AppLoader_NSO::Load() {
|
ResultStatus AppLoader_NSO::Load() {
|
||||||
|
@ -161,22 +152,29 @@ ResultStatus AppLoader_NSO::Load() {
|
||||||
return ResultStatus::Error;
|
return ResultStatus::Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load and relocate "main" and "sdk" NSO
|
// Load and relocate "rtld" NSO
|
||||||
static constexpr VAddr main_base{0x710000000};
|
static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR};
|
||||||
Kernel::g_current_process = Kernel::Process::Create("main");
|
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;
|
return ResultStatus::ErrorInvalidFormat;
|
||||||
}
|
}
|
||||||
const std::string sdkpath = filepath.substr(0, filepath.find_last_of("/\\")) + "/sdk";
|
|
||||||
if (!LoadNso(sdkpath, 0x720000000)) {
|
// Load and relocate remaining submodules
|
||||||
LOG_WARNING(Loader, "failed to find SDK NSO");
|
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->svc_access_mask.set();
|
||||||
Kernel::g_current_process->address_mappings = default_address_mappings;
|
Kernel::g_current_process->address_mappings = default_address_mappings;
|
||||||
Kernel::g_current_process->resource_limit =
|
Kernel::g_current_process->resource_limit =
|
||||||
Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
|
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();
|
ResolveImports();
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,8 @@ namespace Loader {
|
||||||
/// Loads an NSO file
|
/// Loads an NSO file
|
||||||
class AppLoader_NSO final : public AppLoader, Linker {
|
class AppLoader_NSO final : public AppLoader, Linker {
|
||||||
public:
|
public:
|
||||||
AppLoader_NSO(FileUtil::IOFile&& file, std::string filename, std::string filepath)
|
AppLoader_NSO(FileUtil::IOFile&& file, std::string filepath)
|
||||||
: AppLoader(std::move(file)), filename(std::move(filename)), filepath(std::move(filepath)) {
|
: AppLoader(std::move(file)), filepath(std::move(filepath)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,10 +35,8 @@ public:
|
||||||
ResultStatus Load() override;
|
ResultStatus Load() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VAddr GetEntryPoint(VAddr load_base) const;
|
VAddr LoadNso(const std::string& path, VAddr load_base, bool relocate = false);
|
||||||
bool LoadNso(const std::string& path, VAddr load_base);
|
|
||||||
|
|
||||||
std::string filename;
|
|
||||||
std::string filepath;
|
std::string filepath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -65,8 +65,8 @@ enum : PAddr {
|
||||||
/// Virtual user-space memory regions
|
/// Virtual user-space memory regions
|
||||||
enum : VAddr {
|
enum : VAddr {
|
||||||
/// Where the application text, data and bss reside.
|
/// Where the application text, data and bss reside.
|
||||||
PROCESS_IMAGE_VADDR = 0x00100000,
|
PROCESS_IMAGE_VADDR = 0x08000000,
|
||||||
PROCESS_IMAGE_MAX_SIZE = 0x03F00000,
|
PROCESS_IMAGE_MAX_SIZE = 0x08000000,
|
||||||
PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE,
|
PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE,
|
||||||
|
|
||||||
/// Area where IPC buffers are mapped onto.
|
/// Area where IPC buffers are mapped onto.
|
||||||
|
|
Loading…
Reference in New Issue