summaryrefslogtreecommitdiff
path: root/src/core/loader/nro.cpp
diff options
context:
space:
mode:
authorGravatar gdkchan2018-01-17 20:16:09 -0300
committerGravatar gdkchan2018-01-17 20:16:09 -0300
commitd3e63e42204c8e8be02af651fd6281ac204ccba5 (patch)
tree663e403c56816990ed057b0aad4b254f4cdfa811 /src/core/loader/nro.cpp
parentMerge pull request #76 from Rozelette/master (diff)
downloadyuzu-d3e63e42204c8e8be02af651fd6281ac204ccba5.tar.gz
yuzu-d3e63e42204c8e8be02af651fd6281ac204ccba5.tar.xz
yuzu-d3e63e42204c8e8be02af651fd6281ac204ccba5.zip
Fix NRO loading
Diffstat (limited to 'src/core/loader/nro.cpp')
-rw-r--r--src/core/loader/nro.cpp29
1 files changed, 9 insertions, 20 deletions
diff --git a/src/core/loader/nro.cpp b/src/core/loader/nro.cpp
index 66c61b038..6864a1926 100644
--- a/src/core/loader/nro.cpp
+++ b/src/core/loader/nro.cpp
@@ -62,20 +62,6 @@ static constexpr u32 PageAlignSize(u32 size) {
62 return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; 62 return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
63} 63}
64 64
65static std::vector<u8> ReadSegment(FileUtil::IOFile& file, const NroSegmentHeader& header) {
66 std::vector<u8> data;
67 data.resize(header.size);
68
69 file.Seek(header.offset + sizeof(NroHeader), SEEK_SET);
70 size_t bytes_read{file.ReadBytes(data.data(), header.size)};
71 if (header.size != PageAlignSize(static_cast<u32>(bytes_read))) {
72 LOG_CRITICAL(Loader, "Failed to read NRO segment bytes", header.size);
73 return {};
74 }
75
76 return data;
77}
78
79bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) { 65bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
80 FileUtil::IOFile file(path, "rb"); 66 FileUtil::IOFile file(path, "rb");
81 if (!file.IsOpen()) { 67 if (!file.IsOpen()) {
@@ -95,7 +81,7 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
95 // Build program image 81 // Build program image
96 Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create("", 0); 82 Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create("", 0);
97 std::vector<u8> program_image; 83 std::vector<u8> program_image;
98 program_image.resize(PageAlignSize(nro_header.file_size + nro_header.bss_size)); 84 program_image.resize(PageAlignSize(nro_header.file_size));
99 file.Seek(0, SEEK_SET); 85 file.Seek(0, SEEK_SET);
100 file.ReadBytes(program_image.data(), nro_header.file_size); 86 file.ReadBytes(program_image.data(), nro_header.file_size);
101 87
@@ -107,15 +93,16 @@ bool AppLoader_NRO::LoadNro(const std::string& path, VAddr load_base) {
107 93
108 // Read MOD header 94 // Read MOD header
109 ModHeader mod_header{}; 95 ModHeader mod_header{};
110 u32 bss_size{Memory::PAGE_SIZE}; // Default .bss to page size if MOD0 section doesn't exist 96 // Default .bss to NRO header bss size if MOD0 section doesn't exist
97 u32 bss_size{PageAlignSize(nro_header.bss_size)};
111 std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset, 98 std::memcpy(&mod_header, program_image.data() + nro_header.module_header_offset,
112 sizeof(ModHeader)); 99 sizeof(ModHeader));
113 const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')}; 100 const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
114 if (has_mod_header) { 101 if (has_mod_header) {
115 // Resize program image to include .bss section and page align each section 102 // Resize program image to include .bss section and page align each section
116 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); 103 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
117 codeset->data.size += bss_size;
118 } 104 }
105 codeset->data.size += bss_size;
119 program_image.resize(PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)); 106 program_image.resize(PageAlignSize(static_cast<u32>(program_image.size()) + bss_size));
120 107
121 // Load codeset for current process 108 // Load codeset for current process
@@ -134,9 +121,11 @@ ResultStatus AppLoader_NRO::Load(Kernel::SharedPtr<Kernel::Process>& process) {
134 return ResultStatus::Error; 121 return ResultStatus::Error;
135 } 122 }
136 123
137 // Load and relocate "main" and "sdk" NSO
138 static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR};
139 process = Kernel::Process::Create("main"); 124 process = Kernel::Process::Create("main");
125
126 // Load NRO
127 static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR};
128
140 if (!LoadNro(filepath, base_addr)) { 129 if (!LoadNro(filepath, base_addr)) {
141 return ResultStatus::ErrorInvalidFormat; 130 return ResultStatus::ErrorInvalidFormat;
142 } 131 }
@@ -145,7 +134,7 @@ ResultStatus AppLoader_NRO::Load(Kernel::SharedPtr<Kernel::Process>& process) {
145 process->address_mappings = default_address_mappings; 134 process->address_mappings = default_address_mappings;
146 process->resource_limit = 135 process->resource_limit =
147 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); 136 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
148 process->Run(base_addr, 48, Kernel::DEFAULT_STACK_SIZE); 137 process->Run(base_addr + sizeof(NroHeader), 48, Kernel::DEFAULT_STACK_SIZE);
149 138
150 is_loaded = true; 139 is_loaded = true;
151 return ResultStatus::Success; 140 return ResultStatus::Success;