From c100a4b8d45d09494d79b5f5e3ece7dee9f841e9 Mon Sep 17 00:00:00 2001
From: Zach Hilman <zachhilman@gmail.com>
Date: Sat, 22 Dec 2018 21:31:07 -0500
Subject: [PATCH] loader/nso: Set main code region in VMManager For rom
 directories (and by extension, XCI/NSP/NAX/NCA) this is for the NSO with name
 'main', for regular NSOs, this is the NSO.

---
 .../loader/deconstructed_rom_directory.cpp     |  4 +++-
 src/core/loader/nso.cpp                        | 18 +++++++++++++++++-
 src/core/loader/nso.h                          |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/src/core/loader/deconstructed_rom_directory.cpp b/src/core/loader/deconstructed_rom_directory.cpp
index 07aa7a1cd6..ef9a577e39 100644
--- a/src/core/loader/deconstructed_rom_directory.cpp
+++ b/src/core/loader/deconstructed_rom_directory.cpp
@@ -147,8 +147,10 @@ ResultStatus AppLoader_DeconstructedRomDirectory::Load(Kernel::Process& process)
 
         const VAddr load_addr = next_load_addr;
         const bool should_pass_arguments = std::strcmp(module, "rtld") == 0;
+        const bool should_register_data_segment = std::strcmp(module, "main") == 0;
         const auto tentative_next_load_addr =
-            AppLoader_NSO::LoadModule(process, *module_file, load_addr, should_pass_arguments, pm);
+            AppLoader_NSO::LoadModule(process, *module_file, load_addr, should_pass_arguments,
+                                      should_register_data_segment, pm);
         if (!tentative_next_load_addr) {
             return ResultStatus::ErrorLoadingNSO;
         }
diff --git a/src/core/loader/nso.cpp b/src/core/loader/nso.cpp
index 6ded0b7073..2721d85b15 100644
--- a/src/core/loader/nso.cpp
+++ b/src/core/loader/nso.cpp
@@ -7,8 +7,10 @@
 #include <lz4.h>
 #include "common/common_funcs.h"
 #include "common/file_util.h"
+#include "common/hex_util.h"
 #include "common/logging/log.h"
 #include "common/swap.h"
+#include "core/core.h"
 #include "core/file_sys/patch_manager.h"
 #include "core/gdbstub/gdbstub.h"
 #include "core/hle/kernel/process.h"
@@ -95,6 +97,7 @@ static constexpr u32 PageAlignSize(u32 size) {
 std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
                                                const FileSys::VfsFile& file, VAddr load_base,
                                                bool should_pass_arguments,
+                                               bool should_register_data_region,
                                                std::optional<FileSys::PatchManager> pm) {
     if (file.GetSize() < sizeof(NsoHeader))
         return {};
@@ -153,6 +156,10 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
     const u32 image_size{PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)};
     program_image.resize(image_size);
 
+    if (should_register_data_region) {
+        process.VMManager().SetMainCodeRegion(load_base, load_base + program_image.size());
+    }
+
     // Apply patches if necessary
     if (pm && (pm->HasNSOPatch(nso_header.build_id) || Settings::values.dump_nso)) {
         std::vector<u8> pi_header(program_image.size() + 0x100);
@@ -164,6 +171,15 @@ std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
         std::memcpy(program_image.data(), pi_header.data() + 0x100, program_image.size());
     }
 
+    // Apply cheats if they exist and the program has a valid title ID
+    if (pm) {
+        const auto cheats = pm->CreateCheatList(nso_header.build_id);
+        if (!cheats.empty()) {
+            Core::System::GetInstance().RegisterCheatList(
+                cheats, Common::HexArrayToString(nso_header.build_id));
+        }
+    }
+
     // Load codeset for current process
     codeset.memory = std::make_shared<std::vector<u8>>(std::move(program_image));
     process.LoadModule(std::move(codeset), load_base);
@@ -181,7 +197,7 @@ ResultStatus AppLoader_NSO::Load(Kernel::Process& process) {
 
     // Load module
     const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
-    if (!LoadModule(process, *file, base_address, true)) {
+    if (!LoadModule(process, *file, base_address, true, true)) {
         return ResultStatus::ErrorLoadingNSO;
     }
     LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
diff --git a/src/core/loader/nso.h b/src/core/loader/nso.h
index 135b6ea5af..858e346c6e 100644
--- a/src/core/loader/nso.h
+++ b/src/core/loader/nso.h
@@ -43,6 +43,7 @@ public:
 
     static std::optional<VAddr> LoadModule(Kernel::Process& process, const FileSys::VfsFile& file,
                                            VAddr load_base, bool should_pass_arguments,
+                                           bool should_register_data_segment,
                                            std::optional<FileSys::PatchManager> pm = {});
 
     ResultStatus Load(Kernel::Process& process) override;