diff --git a/Source/Atmosphere/stratosphere/loader/source/ldr_process_creation.cpp b/Source/Atmosphere/stratosphere/loader/source/ldr_process_creation.cpp index a63a84c1..7ecbf857 100644 --- a/Source/Atmosphere/stratosphere/loader/source/ldr_process_creation.cpp +++ b/Source/Atmosphere/stratosphere/loader/source/ldr_process_creation.cpp @@ -1,8 +1,6 @@ /* * Copyright (c) Atmosphère-NX * - * Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors - * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, * version 2, as published by the Free Software Foundation. @@ -95,6 +93,8 @@ namespace ams::ldr { struct ProcessInfo { os::NativeHandle process_handle; + uintptr_t code_address; + size_t total_size; uintptr_t args_address; size_t args_size; uintptr_t nso_address[Nso_Count]; @@ -106,7 +106,16 @@ namespace ams::ldr { bool has_main; bool has_sdk; bool has_subsdk; - bool has_nso[Nso_Count]; + s8 nso_indices[Nso_Count]; + }; + + struct AutoLoadModuleContext { + NsoHeader *headers; + int nso_count; + int rtld_idx; + int main_nso_idx; + int sdk_nso_idx; + AutoLoadModuleInfo ali; }; /* Global NSO header cache. */ @@ -116,6 +125,10 @@ namespace ams::ldr { bool g_is_pcv; bool g_is_ptm; + /* Global Zstd decompression context. */ + constexpr size_t ZstdDctxWorkspaceSize = 0x176E8; + alignas(8) u8 g_zstd_dctx_workspace[ZstdDctxWorkspaceSize]; + Result ValidateProgramVersion(ncm::ProgramId program_id, u32 version) { /* No version verification is done before 8.1.0. */ R_SUCCEED_IF(hos::GetVersion() < hos::Version_8_1_0); @@ -176,10 +189,15 @@ namespace ams::ldr { return static_cast((meta->acid->flags & Acid::AcidFlag_PoolPartitionMask) >> Acid::AcidFlag_PoolPartitionShift); } - Result LoadAutoLoadHeaders(NsoHeader *nso_headers, AutoLoadModuleInfo *ali, u32 acid_flags) { + Result LoadAutoLoadHeaders(AutoLoadModuleContext &ctx, u32 acid_flags) { /* Clear NSOs. */ - std::memset(nso_headers, 0, sizeof(*nso_headers) * Nso_Count); - *ali = {}; + std::memset(g_nso_headers, 0, sizeof(g_nso_headers)); + ctx.headers = g_nso_headers; + ctx.nso_count = 0; + ctx.rtld_idx = -1; + ctx.main_nso_idx = -1; + ctx.sdk_nso_idx = -1; + ctx.ali = {}; for (size_t i = 0; i < Nso_Count; i++) { /* Only load browser DLLs if acid flags say to do so. */ @@ -206,16 +224,18 @@ namespace ams::ldr { /* Read NSO header. */ size_t read_size; - R_TRY(fs::ReadFile(std::addressof(read_size), file, 0, nso_headers + i, sizeof(*nso_headers))); - R_UNLESS(read_size == sizeof(*nso_headers), ldr::ResultInvalidNso()); + R_TRY(fs::ReadFile(std::addressof(read_size), file, 0, g_nso_headers + ctx.nso_count, sizeof(NsoHeader))); + R_UNLESS(read_size == sizeof(NsoHeader), ldr::ResultInvalidNso()); /* Note nso is present. */ switch (i) { case Nso_Rtld: - ali->has_rtld = true; + ctx.rtld_idx = ctx.nso_count; + ctx.ali.has_rtld = true; break; case Nso_Main: - ali->has_main = true; + ctx.main_nso_idx = ctx.nso_count; + ctx.ali.has_main = true; break; case Nso_SubSdk0: case Nso_SubSdk1: @@ -227,64 +247,65 @@ namespace ams::ldr { case Nso_SubSdk7: case Nso_SubSdk8: case Nso_SubSdk9: - ali->has_subsdk = true; + ctx.ali.has_subsdk = true; break; case Nso_Sdk: - ali->has_sdk = true; + ctx.sdk_nso_idx = ctx.nso_count; + ctx.ali.has_sdk = true; break; } - ali->has_nso[i] = true; + ctx.ali.nso_indices[ctx.nso_count] = static_cast(i); + ctx.nso_count++; } } R_SUCCEED(); } - Result CheckAutoLoad(const NsoHeader *nso_headers, const AutoLoadModuleInfo *ali, u32 acid_flags) { + Result CheckAutoLoad(const AutoLoadModuleContext &ctx, u32 acid_flags) { /* We must always have a main. */ - R_UNLESS(ali->has_main, ldr::ResultInvalidNso()); + R_UNLESS(ctx.ali.has_main, ldr::ResultInvalidNso()); - /* All NSOs must not be --X. */ - /* This is "probably" not checked on Ounce? */ - for (size_t i = 0; i < Nso_Count; ++i) { - R_UNLESS((nso_headers[i].flags & NsoHeader::Flag_PreventCodeReads) == 0, ldr::ResultInvalidNso()); - } + /* Validate flags and extents for all present NSOs. */ + for (int i = 0; i < ctx.nso_count; ++i) { + const auto &hdr = ctx.headers[i]; - /* If we don't have an RTLD, we must only have a main. */ - const bool has_browser_dll = (acid_flags & Acid::AcidFlag_LoadBrowserCoreDll) != 0; - if (!ali->has_rtld) { - /* If don't have rtld, we must also not have sdk. */ - R_UNLESS(!ali->has_sdk, ldr::ResultInvalidNso()); + /* All NSOs must not be --X. */ + /* This is "probably" not checked on Ounce? */ + R_UNLESS((hdr.flags & NsoHeader::Flag_PreventCodeReads) == 0, ldr::ResultInvalidNso()); - /* We must also not have both subsdk and browser dll. */ - R_UNLESS(!(ali->has_subsdk && has_browser_dll), ldr::ResultInvalidNso()); - } else { - /* If we have rtld, we must not have browser core dll. */ - R_UNLESS(!has_browser_dll, ldr::ResultInvalidNso()); - } - - /* Check NSO extents. */ - for (size_t i = 0; i < Nso_Count; i++) { - /* Only validate the nsos we have. */ - if (!ali->has_nso[i]) { - continue; + /* Zstd compression only allowed on main, and only when both rtld+sdk are present. */ + if (i != ctx.main_nso_idx || ctx.rtld_idx < 0 || ctx.sdk_nso_idx < 0) { + R_UNLESS((hdr.flags & NsoHeader::Flag_UseZbicCompression) == 0, ldr::ResultInvalidNso()); } /* NSOs must have page-aligned segments. */ - R_UNLESS(util::IsAligned(nso_headers[i].text_dst_offset, os::MemoryPageSize), ldr::ResultInvalidNso()); - R_UNLESS(util::IsAligned(nso_headers[i].ro_dst_offset, os::MemoryPageSize), ldr::ResultInvalidNso()); - R_UNLESS(util::IsAligned(nso_headers[i].rw_dst_offset, os::MemoryPageSize), ldr::ResultInvalidNso()); + R_UNLESS(util::IsAligned(hdr.text_dst_offset, os::MemoryPageSize), ldr::ResultInvalidNso()); + R_UNLESS(util::IsAligned(hdr.ro_dst_offset, os::MemoryPageSize), ldr::ResultInvalidNso()); + R_UNLESS(util::IsAligned(hdr.rw_dst_offset, os::MemoryPageSize), ldr::ResultInvalidNso()); /* NSOs must have zero text offset. */ - R_UNLESS(nso_headers[i].text_dst_offset == 0, ldr::ResultInvalidNso()); + R_UNLESS(hdr.text_dst_offset == 0, ldr::ResultInvalidNso()); /* NSO .text must precede .rodata. */ - const size_t text_end = static_cast(nso_headers[i].text_dst_offset) + static_cast(nso_headers[i].text_size); - R_UNLESS(text_end <= static_cast(nso_headers[i].ro_dst_offset), ldr::ResultInvalidNso()); + const size_t text_end = static_cast(hdr.text_dst_offset) + static_cast(hdr.text_size); + R_UNLESS(text_end <= static_cast(hdr.ro_dst_offset), ldr::ResultInvalidNso()); /* NSO .rodata must precede .rwdata. */ - const size_t ro_end = static_cast(nso_headers[i].ro_dst_offset) + static_cast(nso_headers[i].ro_size); - R_UNLESS(ro_end <= static_cast(nso_headers[i].rw_dst_offset), ldr::ResultInvalidNso()); + const size_t ro_end = static_cast(hdr.ro_dst_offset) + static_cast(hdr.ro_size); + R_UNLESS(ro_end <= static_cast(hdr.rw_dst_offset), ldr::ResultInvalidNso()); + } + + const bool has_browser_dll = (acid_flags & Acid::AcidFlag_LoadBrowserCoreDll) != 0; + if (ctx.ali.has_rtld || ctx.ali.has_sdk) { + /* If we have sdk we must have rtld. */ + R_UNLESS(ctx.ali.has_rtld, ldr::ResultInvalidNso()); + + /* If we have rtld, we must not have browser core dll. */ + R_UNLESS(!has_browser_dll, ldr::ResultInvalidNso()); + } else { + /* We must not have both subsdk and browser dll. */ + R_UNLESS(!(ctx.ali.has_subsdk && has_browser_dll), ldr::ResultInvalidNso()); } R_SUCCEED(); @@ -300,8 +321,8 @@ namespace ams::ldr { { 0x010049900F546001 }, /* Super Mario 3D All-Stars: Super Mario 64 */ { 0x010057D00ECE4000 }, /* Nintendo Switch Online (Nintendo 64) [for Japan] */ { 0x01006F8002326000 }, /* Animal Crossing: New Horizons */ - { 0x01006FB00F50E000 }, /* [???] */ - { 0x010070300F50C000 }, /* [???] */ + { 0x01006FB00F50E000 }, /* 宝可梦 走吧!伊布 [Pokemon: Let's Go, Eevee! for China] */ + { 0x010070300F50C000 }, /* 宝可梦 走吧!皮卡丘 [Pokemon: Let's Go, Pikachu! for China] */ { 0x010075100E8EC000 }, /* 马力欧卡丁车8 豪华版 [Mario Kart 8 Deluxe for China] */ { 0x01008DB008C2C000 }, /* Pokemon Shield */ { 0x01009AD008C4C000 }, /* Pokemon: Let's Go, Pikachu! [Kiosk] */ @@ -525,7 +546,7 @@ namespace ams::ldr { return rand % (max + 1); } - Result DecideAddressSpaceLayout(ProcessInfo *out, svc::CreateProcessParameter *out_param, const NsoHeader *nso_headers, const AutoLoadModuleInfo *ali, const ArgumentStore::Entry *argument) { + Result DecideAddressSpaceLayout(ProcessInfo *out, svc::CreateProcessParameter *out_param, const AutoLoadModuleContext &ctx, const ArgumentStore::Entry *argument) { /* Clear output. */ out->args_address = 0; out->args_size = 0; @@ -536,35 +557,33 @@ namespace ams::ldr { bool argument_allocated = false; /* Calculate base offsets. */ - for (size_t i = 0; i < Nso_Count; i++) { - if (ali->has_nso[i]) { - out->nso_address[i] = total_size; - const size_t text_end = static_cast(nso_headers[i].text_dst_offset) + static_cast(nso_headers[i].text_size); - const size_t ro_end = static_cast(nso_headers[i].ro_dst_offset) + static_cast(nso_headers[i].ro_size); - const size_t rw_end = static_cast(nso_headers[i].rw_dst_offset) + static_cast(nso_headers[i].rw_size); - out->nso_size[i] = text_end; - out->nso_size[i] = std::max(out->nso_size[i], ro_end); - out->nso_size[i] = std::max(out->nso_size[i], rw_end); - out->nso_size[i] += static_cast(nso_headers[i].bss_size); + for (int i = 0; i < ctx.nso_count; i++) { + out->nso_address[i] = total_size; + const size_t text_end = static_cast(ctx.headers[i].text_dst_offset) + static_cast(ctx.headers[i].text_size); + const size_t ro_end = static_cast(ctx.headers[i].ro_dst_offset) + static_cast(ctx.headers[i].ro_size); + const size_t rw_end = static_cast(ctx.headers[i].rw_dst_offset) + static_cast(ctx.headers[i].rw_size); + out->nso_size[i] = text_end; + out->nso_size[i] = std::max(out->nso_size[i], ro_end); + out->nso_size[i] = std::max(out->nso_size[i], rw_end); + out->nso_size[i] += static_cast(ctx.headers[i].bss_size); - const size_t aligned_up_size = util::AlignUp(out->nso_size[i], os::MemoryPageSize) & (AutoLoadModuleSizeMax - 1); - R_UNLESS(out->nso_size[i] <= aligned_up_size, ldr::ResultInvalidNso()); - R_UNLESS(aligned_up_size > 0, ldr::ResultInvalidNso()); + const size_t aligned_up_size = util::AlignUp(out->nso_size[i], os::MemoryPageSize) & (AutoLoadModuleSizeMax - 1); + R_UNLESS(out->nso_size[i] <= aligned_up_size, ldr::ResultInvalidNso()); + R_UNLESS(aligned_up_size > 0, ldr::ResultInvalidNso()); - out->nso_size[i] = aligned_up_size; + out->nso_size[i] = aligned_up_size; - R_UNLESS(util::CanAddWithoutOverflow(total_size, out->nso_size[i]), ldr::ResultInvalidNso()); - total_size += out->nso_size[i]; + R_UNLESS(util::CanAddWithoutOverflow(total_size, out->nso_size[i]), ldr::ResultInvalidNso()); + total_size += out->nso_size[i]; - if (!argument_allocated && argument != nullptr) { - out->args_address = total_size; - out->args_size = util::AlignUp(2 * sizeof(u32) + argument->argument_size * 2 + ArgumentStore::ArgumentBufferSize, os::MemoryPageSize); + if (!argument_allocated && argument != nullptr) { + out->args_address = total_size; + out->args_size = util::AlignUp(2 * sizeof(u32) + argument->argument_size * 2 + ArgumentStore::ArgumentBufferSize, os::MemoryPageSize); - R_UNLESS(util::CanAddWithoutOverflow(total_size, out->args_size), ldr::ResultInvalidNso()); - total_size += out->args_size; + R_UNLESS(util::CanAddWithoutOverflow(total_size, out->args_size), ldr::ResultInvalidNso()); + total_size += out->args_size; - argument_allocated = true; - } + argument_allocated = true; } } @@ -609,11 +628,9 @@ namespace ams::ldr { /* Set out. */ aslr_start += aslr_slide; - for (size_t i = 0; i < Nso_Count; i++) { - if (ali->has_nso[i]) { - R_UNLESS(util::CanAddWithoutOverflow(out->nso_address[i], aslr_start), ldr::ResultInvalidNso()); - out->nso_address[i] += aslr_start; - } + for (int i = 0; i < ctx.nso_count; i++) { + R_UNLESS(util::CanAddWithoutOverflow(out->nso_address[i], aslr_start), ldr::ResultInvalidNso()); + out->nso_address[i] += aslr_start; } if (out->args_address) { R_UNLESS(util::CanAddWithoutOverflow(out->args_address, aslr_start), ldr::ResultInvalidNso()); @@ -622,69 +639,88 @@ namespace ams::ldr { out_param->code_address = aslr_start; out_param->code_num_pages = total_size >> 12; + out->total_size = total_size; R_SUCCEED(); } - Result LoadAutoLoadModuleSegment(fs::FileHandle file, const NsoHeader::SegmentInfo *segment, size_t file_size, const u8 *file_hash, bool is_compressed, bool check_hash, uintptr_t map_base, uintptr_t map_end) { + Result LoadAutoLoadModuleSegment(fs::FileHandle file, size_t file_offset, size_t compressed_size, size_t segment_size, bool is_compressed, bool is_zstd, uintptr_t map_base, uintptr_t map_end) { /* Select read size based on compression. */ - if (!is_compressed) { - file_size = segment->size; - } + size_t file_size = is_compressed ? compressed_size : segment_size; /* Validate size. */ - R_UNLESS(file_size <= segment->size, ldr::ResultInvalidNso()); - R_UNLESS(segment->size <= std::numeric_limits::max(), ldr::ResultInvalidNso()); + R_UNLESS(file_size <= segment_size, ldr::ResultInvalidNso()); + R_UNLESS(file_size <= std::numeric_limits::max(), ldr::ResultInvalidNso()); + R_UNLESS(segment_size <= std::numeric_limits::max(), ldr::ResultInvalidNso()); /* Load data from file. */ - uintptr_t load_address = is_compressed ? map_end - file_size : map_base; + uintptr_t load_address = is_compressed ? map_end - compressed_size : map_base; size_t read_size; - R_TRY(fs::ReadFile(std::addressof(read_size), file, segment->file_offset, reinterpret_cast(load_address), file_size)); + R_TRY(fs::ReadFile(std::addressof(read_size), file, file_offset, reinterpret_cast(load_address), file_size)); R_UNLESS(read_size == file_size, ldr::ResultInvalidNso()); /* Uncompress if necessary. */ - if (is_compressed) { - bool decompressed = (util::DecompressLZ4(reinterpret_cast(map_base), segment->size, reinterpret_cast(load_address), file_size) == static_cast(segment->size)); + R_SUCCEED_IF(!is_compressed); + + auto compressed_data_buf = reinterpret_cast(load_address); + + if (is_zstd) { + bool decompressed = util::DecompressZstdForLoader(reinterpret_cast(g_zstd_dctx_workspace), ZstdDctxWorkspaceSize, reinterpret_cast(map_base), static_cast(map_end - map_base), segment_size, compressed_data_buf, file_size); + R_UNLESS(decompressed, ldr::ResultInvalidNso()); + } else { + bool decompressed = (util::DecompressLZ4(reinterpret_cast(map_base), segment_size, compressed_data_buf, file_size) == static_cast(segment_size)); R_UNLESS(decompressed, ldr::ResultInvalidNso()); - } - - /* Check hash if necessary. */ - if (check_hash) { - u8 hash[crypto::Sha256Generator::HashSize]; - crypto::GenerateSha256(hash, sizeof(hash), reinterpret_cast(map_base), segment->size); - - R_UNLESS(std::memcmp(hash, file_hash, sizeof(hash)) == 0, ldr::ResultInvalidNso()); } R_SUCCEED(); } - Result LoadAutoLoadModule(os::NativeHandle process_handle, fs::FileHandle file, const NsoHeader *nso_header, uintptr_t nso_address, size_t nso_size) { + Result CheckSegmentHash(const NsoHeader *nso_header, uintptr_t map_address, NsoHeader::Segment segment) { + if ((nso_header->flags & (NsoHeader::Flag_CheckHashText << segment)) == 0) { + R_SUCCEED(); + } + + u8 hash[crypto::Sha256Generator::HashSize]; + crypto::GenerateSha256(hash, sizeof(hash), + reinterpret_cast(map_address + nso_header->segments[segment].dst_offset), + nso_header->segments[segment].size); + R_UNLESS(std::memcmp(hash, nso_header->segment_hashes[segment], sizeof(hash)) == 0, ldr::ResultInvalidNso()); + R_SUCCEED(); + } + + Result LoadAutoLoadModule(os::NativeHandle process_handle, fs::FileHandle file, const NsoHeader *nso_header, uintptr_t nso_address, size_t nso_size, size_t map_size) { + const bool is_zstd = (nso_header->flags & NsoHeader::Flag_UseZbicCompression) != 0; + /* Map and read data from file. */ { /* Map the process memory. */ void *mapped_memory = nullptr; - R_TRY(os::MapProcessMemory(std::addressof(mapped_memory), process_handle, nso_address, nso_size, GenerateSecureRandom)); - ON_SCOPE_EXIT { os::UnmapProcessMemory(mapped_memory, process_handle, nso_address, nso_size); }; + R_TRY(os::MapProcessMemory(std::addressof(mapped_memory), process_handle, nso_address, map_size, GenerateSecureRandom)); + ON_SCOPE_EXIT { os::UnmapProcessMemory(mapped_memory, process_handle, nso_address, map_size); }; const uintptr_t map_address = reinterpret_cast(mapped_memory); + const uintptr_t map_end = map_address + map_size; /* Load NSO segments. */ - R_TRY(LoadAutoLoadModuleSegment(file, std::addressof(nso_header->segments[NsoHeader::Segment_Text]), nso_header->text_compressed_size, nso_header->text_hash, (nso_header->flags & NsoHeader::Flag_CompressedText) != 0, - (nso_header->flags & NsoHeader::Flag_CheckHashText) != 0, map_address + nso_header->text_dst_offset, map_address + nso_size)); - R_TRY(LoadAutoLoadModuleSegment(file, std::addressof(nso_header->segments[NsoHeader::Segment_Ro]), nso_header->ro_compressed_size, nso_header->ro_hash, (nso_header->flags & NsoHeader::Flag_CompressedRo) != 0, - (nso_header->flags & NsoHeader::Flag_CheckHashRo) != 0, map_address + nso_header->ro_dst_offset, map_address + nso_size)); - R_TRY(LoadAutoLoadModuleSegment(file, std::addressof(nso_header->segments[NsoHeader::Segment_Rw]), nso_header->rw_compressed_size, nso_header->rw_hash, (nso_header->flags & NsoHeader::Flag_CompressedRw) != 0, - (nso_header->flags & NsoHeader::Flag_CheckHashRw) != 0, map_address + nso_header->rw_dst_offset, map_address + nso_size)); + R_TRY(LoadAutoLoadModuleSegment(file, nso_header->segments[NsoHeader::Segment_Text].file_offset, nso_header->text_compressed_size, nso_header->text_size, + (nso_header->flags & NsoHeader::Flag_CompressedText) != 0, is_zstd, map_address + nso_header->text_dst_offset, map_end)); + R_TRY(LoadAutoLoadModuleSegment(file, nso_header->segments[NsoHeader::Segment_Ro].file_offset, nso_header->ro_compressed_size, nso_header->ro_size, + (nso_header->flags & NsoHeader::Flag_CompressedRo) != 0, is_zstd, map_address + nso_header->ro_dst_offset, map_end)); + R_TRY(LoadAutoLoadModuleSegment(file, nso_header->segments[NsoHeader::Segment_Rw].file_offset, nso_header->rw_compressed_size, nso_header->rw_size, + (nso_header->flags & NsoHeader::Flag_CompressedRw) != 0, is_zstd, map_address + nso_header->rw_dst_offset, map_end)); /* Clear unused space to zero. */ const size_t text_end = static_cast(nso_header->text_dst_offset) + static_cast(nso_header->text_size); const size_t ro_end = static_cast(nso_header->ro_dst_offset) + static_cast(nso_header->ro_size); const size_t rw_end = static_cast(nso_header->rw_dst_offset) + static_cast(nso_header->rw_size); - std::memset(reinterpret_cast(map_address + 0), 0, nso_header->text_dst_offset); std::memset(reinterpret_cast(map_address + text_end), 0, nso_header->ro_dst_offset - text_end); std::memset(reinterpret_cast(map_address + ro_end), 0, nso_header->rw_dst_offset - ro_end); - std::memset(reinterpret_cast(map_address + rw_end), 0, nso_header->bss_size); + std::memset(reinterpret_cast(map_address + rw_end), 0, nso_size - rw_end); + + /* Check segment hashes. */ + R_TRY(CheckSegmentHash(nso_header, map_address, NsoHeader::Segment_Text)); + R_TRY(CheckSegmentHash(nso_header, map_address, NsoHeader::Segment_Ro)); + R_TRY(CheckSegmentHash(nso_header, map_address, NsoHeader::Segment_Rw)); /* Apply embedded patches. */ ApplyEmbeddedPatchesToModule(nso_header->module_id, map_address, nso_size); @@ -711,25 +747,31 @@ namespace ams::ldr { R_TRY(os::SetProcessMemoryPermission(process_handle, nso_address + nso_header->text_dst_offset, text_size, prevent_code_reads ? os::MemoryPermission_ExecuteOnly : os::MemoryPermission_ReadExecute)); } if (ro_size) { - R_TRY(os::SetProcessMemoryPermission(process_handle, nso_address + nso_header->ro_dst_offset, ro_size, os::MemoryPermission_ReadOnly)); + R_TRY(os::SetProcessMemoryPermission(process_handle, nso_address + nso_header->ro_dst_offset, ro_size, os::MemoryPermission_ReadOnly)); } if (rw_size) { - R_TRY(os::SetProcessMemoryPermission(process_handle, nso_address + nso_header->rw_dst_offset, rw_size, os::MemoryPermission_ReadWrite)); + R_TRY(os::SetProcessMemoryPermission(process_handle, nso_address + nso_header->rw_dst_offset, rw_size, os::MemoryPermission_ReadWrite)); } R_SUCCEED(); } - Result LoadAutoLoadModules(const ProcessInfo *process_info, const NsoHeader *nso_headers, const AutoLoadModuleInfo *ali, const ArgumentStore::Entry *argument) { + Result LoadAutoLoadModules(const ProcessInfo *process_info, const AutoLoadModuleContext &ctx, const ArgumentStore::Entry *argument) { /* Load each NSO. */ - for (size_t i = 0; i < Nso_Count; i++) { - if (ali->has_nso[i]) { - fs::FileHandle file; - R_TRY(fs::OpenFile(std::addressof(file), GetNsoPath(i), fs::OpenMode_Read)); - ON_SCOPE_EXIT { fs::CloseFile(file); }; + const uintptr_t total_end = process_info->code_address + process_info->total_size; - R_TRY(LoadAutoLoadModule(process_info->process_handle, file, nso_headers + i, process_info->nso_address[i], process_info->nso_size[i])); - } + for (int i = 0; i < ctx.nso_count; i++) { + const NsoIndex nso_idx = static_cast(ctx.ali.nso_indices[i]); + + fs::FileHandle file; + R_TRY(fs::OpenFile(std::addressof(file), GetNsoPath(nso_idx), fs::OpenMode_Read)); + ON_SCOPE_EXIT { fs::CloseFile(file); }; + + const bool is_zstd = (ctx.headers[i].flags & NsoHeader::Flag_UseZbicCompression) != 0; + const size_t map_size = is_zstd ? (total_end - process_info->nso_address[i]) : process_info->nso_size[i]; + + R_TRY(LoadAutoLoadModule(process_info->process_handle, file, ctx.headers + i, + process_info->nso_address[i], process_info->nso_size[i], map_size)); } /* Load arguments, if present. */ @@ -755,13 +797,13 @@ namespace ams::ldr { R_SUCCEED(); } - Result CreateProcessAndLoadAutoLoadModules(ProcessInfo *out, const Meta *meta, const NsoHeader *nso_headers, const AutoLoadModuleInfo *ali, const ArgumentStore::Entry *argument, u32 flags, os::NativeHandle resource_limit) { + Result CreateProcessAndLoadAutoLoadModules(ProcessInfo *out, const Meta *meta, const AutoLoadModuleContext &ctx, const ArgumentStore::Entry *argument, u32 flags, os::NativeHandle resource_limit) { /* Get CreateProcessParameter. */ svc::CreateProcessParameter param; R_TRY(GetCreateProcessParameter(std::addressof(param), meta, flags, resource_limit)); /* Decide on an NSO layout. */ - R_TRY(DecideAddressSpaceLayout(out, std::addressof(param), nso_headers, ali, argument)); + R_TRY(DecideAddressSpaceLayout(out, std::addressof(param), ctx, argument)); /* Actually create process. */ svc::Handle process_handle; @@ -769,10 +811,11 @@ namespace ams::ldr { /* Set the output handle, and ensure that if we fail after this point we clean it up. */ out->process_handle = process_handle; + out->code_address = param.code_address; ON_RESULT_FAILURE { svc::CloseHandle(process_handle); }; /* Load all auto load modules. */ - R_RETURN(LoadAutoLoadModules(out, nso_headers, ali, argument)); + R_RETURN(LoadAutoLoadModules(out, ctx, argument)); } } @@ -813,13 +856,13 @@ namespace ams::ldr { } /* Load, validate NSO headers. */ - AutoLoadModuleInfo auto_load_info = {}; - R_TRY(LoadAutoLoadHeaders(g_nso_headers, std::addressof(auto_load_info), meta.acid->flags)); - R_TRY(CheckAutoLoad(g_nso_headers, std::addressof(auto_load_info), meta.acid->flags)); + AutoLoadModuleContext ctx; + R_TRY(LoadAutoLoadHeaders(ctx, meta.acid->flags)); + R_TRY(CheckAutoLoad(ctx, meta.acid->flags)); /* Actually create the process and load NSOs into process memory. */ ProcessInfo info; - R_TRY(CreateProcessAndLoadAutoLoadModules(std::addressof(info), std::addressof(meta), g_nso_headers, std::addressof(auto_load_info), argument, flags, resource_limit)); + R_TRY(CreateProcessAndLoadAutoLoadModules(std::addressof(info), std::addressof(meta), ctx, argument, flags, resource_limit)); /* Register NSOs with the RoManager. */ { @@ -831,10 +874,8 @@ namespace ams::ldr { RoManager::GetInstance().RegisterProcess(pin_id, process_id, meta.aci->program_id, as_type == Npdm::AddressSpaceType_64Bit || as_type == Npdm::AddressSpaceType_64BitDeprecated); /* Register all NSOs. */ - for (size_t i = 0; i < Nso_Count; i++) { - if (auto_load_info.has_nso[i]) { - RoManager::GetInstance().AddNso(pin_id, g_nso_headers[i].module_id, info.nso_address[i], info.nso_size[i]); - } + for (int i = 0; i < ctx.nso_count; i++) { + RoManager::GetInstance().AddNso(pin_id, ctx.headers[i].module_id, info.nso_address[i], info.nso_size[i]); } } diff --git a/ams_ver.txt b/ams_ver.txt index 5ad2491c..1cac385c 100644 --- a/ams_ver.txt +++ b/ams_ver.txt @@ -1 +1 @@ -1.10.2 +1.11.0