2018-01-13 21:22:39 +00:00
|
|
|
// Copyright 2018 yuzu emulator team
|
2017-10-06 04:30:08 +01:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2017-10-15 05:11:38 +01:00
|
|
|
#include "common/common_funcs.h"
|
2018-01-20 20:48:37 +00:00
|
|
|
#include "common/file_util.h"
|
2017-10-06 04:30:08 +01:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/swap.h"
|
2018-03-13 21:49:59 +00:00
|
|
|
#include "core/core.h"
|
2018-07-13 04:22:59 +01:00
|
|
|
#include "core/gdbstub/gdbstub.h"
|
2017-10-06 04:30:08 +01:00
|
|
|
#include "core/hle/kernel/process.h"
|
|
|
|
#include "core/hle/kernel/resource_limit.h"
|
|
|
|
#include "core/loader/nro.h"
|
|
|
|
#include "core/memory.h"
|
|
|
|
|
|
|
|
namespace Loader {
|
|
|
|
|
|
|
|
struct NroSegmentHeader {
|
|
|
|
u32_le offset;
|
|
|
|
u32_le size;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NroSegmentHeader) == 0x8, "NroSegmentHeader has incorrect size.");
|
|
|
|
|
|
|
|
struct NroHeader {
|
|
|
|
INSERT_PADDING_BYTES(0x4);
|
|
|
|
u32_le module_header_offset;
|
|
|
|
INSERT_PADDING_BYTES(0x8);
|
|
|
|
u32_le magic;
|
|
|
|
INSERT_PADDING_BYTES(0x4);
|
|
|
|
u32_le file_size;
|
|
|
|
INSERT_PADDING_BYTES(0x4);
|
|
|
|
std::array<NroSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
|
|
|
|
u32_le bss_size;
|
|
|
|
INSERT_PADDING_BYTES(0x44);
|
|
|
|
};
|
|
|
|
static_assert(sizeof(NroHeader) == 0x80, "NroHeader has incorrect size.");
|
|
|
|
|
|
|
|
struct ModHeader {
|
|
|
|
u32_le magic;
|
|
|
|
u32_le dynamic_offset;
|
|
|
|
u32_le bss_start_offset;
|
|
|
|
u32_le bss_end_offset;
|
|
|
|
u32_le unwind_start_offset;
|
|
|
|
u32_le unwind_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.");
|
|
|
|
|
2018-07-19 02:07:11 +01:00
|
|
|
AppLoader_NRO::AppLoader_NRO(FileSys::VirtualFile file) : AppLoader(file) {}
|
2018-01-20 20:48:37 +00:00
|
|
|
|
2018-07-19 02:07:11 +01:00
|
|
|
FileType AppLoader_NRO::IdentifyType(const FileSys::VirtualFile& file) {
|
2017-10-06 04:30:08 +01:00
|
|
|
// Read NSO header
|
|
|
|
NroHeader nro_header{};
|
2018-07-19 02:07:11 +01:00
|
|
|
if (sizeof(NroHeader) != file->ReadObject(&nro_header)) {
|
2017-10-06 04:30:08 +01:00
|
|
|
return FileType::Error;
|
|
|
|
}
|
2017-10-15 05:11:38 +01:00
|
|
|
if (nro_header.magic == Common::MakeMagic('N', 'R', 'O', '0')) {
|
2017-10-06 04:30:08 +01:00
|
|
|
return FileType::NRO;
|
|
|
|
}
|
|
|
|
return FileType::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr u32 PageAlignSize(u32 size) {
|
|
|
|
return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
|
|
|
|
}
|
|
|
|
|
2018-07-19 02:07:11 +01:00
|
|
|
bool AppLoader_NRO::LoadNro(FileSys::VirtualFile file, VAddr load_base) {
|
2017-10-06 04:30:08 +01:00
|
|
|
// Read NSO header
|
|
|
|
NroHeader nro_header{};
|
2018-07-19 02:07:11 +01:00
|
|
|
if (sizeof(NroHeader) != file->ReadObject(&nro_header)) {
|
2017-10-06 04:30:08 +01:00
|
|
|
return {};
|
|
|
|
}
|
2017-10-15 05:11:38 +01:00
|
|
|
if (nro_header.magic != Common::MakeMagic('N', 'R', 'O', '0')) {
|
2017-10-06 04:30:08 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build program image
|
2018-02-27 15:22:15 +00:00
|
|
|
Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create("");
|
2018-07-19 02:07:11 +01:00
|
|
|
std::vector<u8> program_image = file->ReadBytes(PageAlignSize(nro_header.file_size));
|
|
|
|
if (program_image.size() != PageAlignSize(nro_header.file_size))
|
|
|
|
return {};
|
2017-10-06 04:30:08 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < nro_header.segments.size(); ++i) {
|
|
|
|
codeset->segments[i].addr = nro_header.segments[i].offset;
|
|
|
|
codeset->segments[i].offset = nro_header.segments[i].offset;
|
|
|
|
codeset->segments[i].size = PageAlignSize(nro_header.segments[i].size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read MOD header
|
|
|
|
ModHeader mod_header{};
|
2018-01-17 23:16:09 +00:00
|
|
|
// Default .bss to NRO header bss size if MOD0 section doesn't exist
|
|
|
|
u32 bss_size{PageAlignSize(nro_header.bss_size)};
|
2017-10-06 04:30:08 +01:00
|
|
|
std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset,
|
|
|
|
sizeof(ModHeader));
|
2017-10-15 05:11:38 +01:00
|
|
|
const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
|
2017-10-06 04:30:08 +01:00
|
|
|
if (has_mod_header) {
|
|
|
|
// Resize program image to include .bss section and page align each section
|
|
|
|
bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
|
|
|
|
}
|
2018-01-17 23:16:09 +00:00
|
|
|
codeset->data.size += bss_size;
|
2018-01-18 20:18:43 +00:00
|
|
|
program_image.resize(static_cast<u32>(program_image.size()) + bss_size);
|
2017-10-06 04:30:08 +01:00
|
|
|
|
|
|
|
// Load codeset for current process
|
2018-07-19 02:07:11 +01:00
|
|
|
codeset->name = file->GetName();
|
2017-10-06 04:30:08 +01:00
|
|
|
codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
|
2018-03-13 21:49:59 +00:00
|
|
|
Core::CurrentProcess()->LoadModule(codeset, load_base);
|
2017-10-06 04:30:08 +01:00
|
|
|
|
2018-07-13 04:22:59 +01:00
|
|
|
// Register module with GDBStub
|
|
|
|
GDBStub::RegisterModule(codeset->name, load_base, load_base);
|
|
|
|
|
2017-10-06 04:30:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-10 04:56:20 +01:00
|
|
|
ResultStatus AppLoader_NRO::Load(Kernel::SharedPtr<Kernel::Process>& process) {
|
2017-10-06 04:30:08 +01:00
|
|
|
if (is_loaded) {
|
|
|
|
return ResultStatus::ErrorAlreadyLoaded;
|
|
|
|
}
|
|
|
|
|
2018-01-17 23:16:09 +00:00
|
|
|
// Load NRO
|
|
|
|
static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR};
|
|
|
|
|
2018-07-19 02:07:11 +01:00
|
|
|
if (!LoadNro(file, base_addr)) {
|
2017-10-06 04:30:08 +01:00
|
|
|
return ResultStatus::ErrorInvalidFormat;
|
|
|
|
}
|
|
|
|
|
2017-10-10 04:56:20 +01:00
|
|
|
process->svc_access_mask.set();
|
|
|
|
process->address_mappings = default_address_mappings;
|
|
|
|
process->resource_limit =
|
2017-10-06 04:30:08 +01:00
|
|
|
Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
|
2018-04-20 23:41:11 +01:00
|
|
|
process->Run(base_addr, THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE);
|
2017-10-06 04:30:08 +01:00
|
|
|
|
|
|
|
is_loaded = true;
|
|
|
|
return ResultStatus::Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Loader
|