summaryrefslogtreecommitdiff
path: root/src/core/loader/nso.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2017-10-09 21:39:32 -0400
committerGravatar bunnei2017-10-09 21:39:32 -0400
commit23ce4f5afc66eb04a7aafc4f89685b8109b8d5c6 (patch)
tree168e7793c6d68eb8b195850a056443ea98f430a9 /src/core/loader/nso.cpp
parentloader: Add support for NRO, as well as various fixes and shared linker. (diff)
downloadyuzu-23ce4f5afc66eb04a7aafc4f89685b8109b8d5c6.tar.gz
yuzu-23ce4f5afc66eb04a7aafc4f89685b8109b8d5c6.tar.xz
yuzu-23ce4f5afc66eb04a7aafc4f89685b8109b8d5c6.zip
loader: Various improvements for NSO/NRO loaders.
Diffstat (limited to 'src/core/loader/nso.cpp')
-rw-r--r--src/core/loader/nso.cpp50
1 files changed, 24 insertions, 26 deletions
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
70 70
71 std::vector<u8> uncompressed_data; 71 std::vector<u8> uncompressed_data;
72 uncompressed_data.resize(header.size); 72 uncompressed_data.resize(header.size);
73 const int bytes_uncompressed = 73 const int bytes_uncompressed = LZ4_decompress_safe(
74 LZ4_decompress_safe_partial(reinterpret_cast<const char*>(compressed_data.data()), 74 reinterpret_cast<const char*>(compressed_data.data()),
75 reinterpret_cast<char*>(uncompressed_data.data()), 75 reinterpret_cast<char*>(uncompressed_data.data()), compressed_size, header.size);
76 compressed_size, header.size, header.size);
77 76
78 ASSERT_MSG(bytes_uncompressed == header.size, "%d != %d", bytes_uncompressed, header.size); 77 ASSERT_MSG(bytes_uncompressed == header.size && bytes_uncompressed == uncompressed_data.size(),
78 "%d != %d != %d", bytes_uncompressed, header.size, uncompressed_data.size());
79 79
80 return uncompressed_data; 80 return uncompressed_data;
81} 81}
82 82
83VAddr AppLoader_NSO::GetEntryPoint(VAddr load_base) const {
84 // Find nnMain function, set entrypoint to that address
85 const auto& search = exports.find("nnMain");
86 if (search != exports.end()) {
87 return search->second;
88 }
89 LOG_ERROR(Loader, "Unable to find entrypoint, defaulting to: 0x%llx", load_base);
90 return load_base;
91}
92
93static constexpr u32 PageAlignSize(u32 size) { 83static constexpr u32 PageAlignSize(u32 size) {
94 return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK; 84 return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
95} 85}
96 86
97bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) { 87VAddr AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base, bool relocate) {
98 FileUtil::IOFile file(path, "rb"); 88 FileUtil::IOFile file(path, "rb");
99 if (!file.IsOpen()) { 89 if (!file.IsOpen()) {
100 return {}; 90 return {};
@@ -137,11 +127,12 @@ bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) {
137 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset); 127 bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
138 codeset->data.size += bss_size; 128 codeset->data.size += bss_size;
139 } 129 }
140 program_image.resize(PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)); 130 const u32 image_size{PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)};
131 program_image.resize(image_size);
141 132
142 // Relocate symbols if there was a proper MOD header - This must happen after the image has been 133 // Relocate symbols if there was a proper MOD header - This must happen after the image has been
143 // loaded into memory 134 // loaded into memory
144 if (has_mod_header) { 135 if (has_mod_header && relocate) {
145 Relocate(program_image, module_offset + mod_header.dynamic_offset, load_base); 136 Relocate(program_image, module_offset + mod_header.dynamic_offset, load_base);
146 } 137 }
147 138
@@ -150,7 +141,7 @@ bool AppLoader_NSO::LoadNso(const std::string& path, VAddr load_base) {
150 codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image)); 141 codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
151 Kernel::g_current_process->LoadModule(codeset, load_base); 142 Kernel::g_current_process->LoadModule(codeset, load_base);
152 143
153 return true; 144 return load_base + image_size;
154} 145}
155 146
156ResultStatus AppLoader_NSO::Load() { 147ResultStatus AppLoader_NSO::Load() {
@@ -161,22 +152,29 @@ ResultStatus AppLoader_NSO::Load() {
161 return ResultStatus::Error; 152 return ResultStatus::Error;
162 } 153 }
163 154
164 // Load and relocate "main" and "sdk" NSO 155 // Load and relocate "rtld" NSO
165 static constexpr VAddr main_base{0x710000000}; 156 static constexpr VAddr base_addr{Memory::PROCESS_IMAGE_VADDR};
166 Kernel::g_current_process = Kernel::Process::Create("main"); 157 Kernel::g_current_process = Kernel::Process::Create("main");
167 if (!LoadNso(filepath, main_base)) { 158 VAddr next_base_addr{LoadNso(filepath, base_addr)};
159 if (!next_base_addr) {
168 return ResultStatus::ErrorInvalidFormat; 160 return ResultStatus::ErrorInvalidFormat;
169 } 161 }
170 const std::string sdkpath = filepath.substr(0, filepath.find_last_of("/\\")) + "/sdk"; 162
171 if (!LoadNso(sdkpath, 0x720000000)) { 163 // Load and relocate remaining submodules
172 LOG_WARNING(Loader, "failed to find SDK NSO"); 164 for (const auto& module_name : {"main", "sdk", "subsdk0", "subsdk1"}) {
165 const std::string module_path =
166 filepath.substr(0, filepath.find_last_of("/\\")) + "/" + module_name;
167 next_base_addr = LoadNso(module_path, next_base_addr);
168 if (!next_base_addr) {
169 LOG_WARNING(Loader, "failed to find load module: %s", module_name);
170 }
173 } 171 }
174 172
175 Kernel::g_current_process->svc_access_mask.set(); 173 Kernel::g_current_process->svc_access_mask.set();
176 Kernel::g_current_process->address_mappings = default_address_mappings; 174 Kernel::g_current_process->address_mappings = default_address_mappings;
177 Kernel::g_current_process->resource_limit = 175 Kernel::g_current_process->resource_limit =
178 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION); 176 Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
179 Kernel::g_current_process->Run(GetEntryPoint(main_base), 48, Kernel::DEFAULT_STACK_SIZE); 177 Kernel::g_current_process->Run(base_addr, 48, Kernel::DEFAULT_STACK_SIZE);
180 178
181 ResolveImports(); 179 ResolveImports();
182 180