nso.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cinttypes>
  5. #include <cstring>
  6. #include <vector>
  7. #include "common/common_funcs.h"
  8. #include "common/file_util.h"
  9. #include "common/hex_util.h"
  10. #include "common/logging/log.h"
  11. #include "common/lz4_compression.h"
  12. #include "common/swap.h"
  13. #include "core/core.h"
  14. #include "core/file_sys/patch_manager.h"
  15. #include "core/gdbstub/gdbstub.h"
  16. #include "core/hle/kernel/code_set.h"
  17. #include "core/hle/kernel/process.h"
  18. #include "core/hle/kernel/vm_manager.h"
  19. #include "core/loader/nso.h"
  20. #include "core/memory.h"
  21. #include "core/settings.h"
  22. namespace Loader {
  23. namespace {
  24. struct MODHeader {
  25. u32_le magic;
  26. u32_le dynamic_offset;
  27. u32_le bss_start_offset;
  28. u32_le bss_end_offset;
  29. u32_le eh_frame_hdr_start_offset;
  30. u32_le eh_frame_hdr_end_offset;
  31. u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base
  32. };
  33. static_assert(sizeof(MODHeader) == 0x1c, "MODHeader has incorrect size.");
  34. std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data,
  35. const NSOSegmentHeader& header) {
  36. const std::vector<u8> uncompressed_data =
  37. Common::Compression::DecompressDataLZ4(compressed_data, header.size);
  38. ASSERT_MSG(uncompressed_data.size() == header.size, "{} != {}", header.size,
  39. uncompressed_data.size());
  40. return uncompressed_data;
  41. }
  42. constexpr u32 PageAlignSize(u32 size) {
  43. return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
  44. }
  45. } // Anonymous namespace
  46. bool NSOHeader::IsSegmentCompressed(size_t segment_num) const {
  47. ASSERT_MSG(segment_num < 3, "Invalid segment {}", segment_num);
  48. return ((flags >> segment_num) & 1) != 0;
  49. }
  50. AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {}
  51. FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) {
  52. u32 magic = 0;
  53. if (file->ReadObject(&magic) != sizeof(magic)) {
  54. return FileType::Error;
  55. }
  56. if (Common::MakeMagic('N', 'S', 'O', '0') != magic) {
  57. return FileType::Error;
  58. }
  59. return FileType::NSO;
  60. }
  61. std::optional<VAddr> AppLoader_NSO::LoadModule(Kernel::Process& process,
  62. const FileSys::VfsFile& file, VAddr load_base,
  63. bool should_pass_arguments,
  64. std::optional<FileSys::PatchManager> pm) {
  65. if (file.GetSize() < sizeof(NSOHeader)) {
  66. return {};
  67. }
  68. NSOHeader nso_header{};
  69. if (sizeof(NSOHeader) != file.ReadObject(&nso_header)) {
  70. return {};
  71. }
  72. if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0')) {
  73. return {};
  74. }
  75. // Build program image
  76. Kernel::CodeSet codeset;
  77. Kernel::PhysicalMemory program_image;
  78. for (std::size_t i = 0; i < nso_header.segments.size(); ++i) {
  79. std::vector<u8> data =
  80. file.ReadBytes(nso_header.segments_compressed_size[i], nso_header.segments[i].offset);
  81. if (nso_header.IsSegmentCompressed(i)) {
  82. data = DecompressSegment(data, nso_header.segments[i]);
  83. }
  84. program_image.resize(nso_header.segments[i].location + static_cast<u32>(data.size()));
  85. std::memcpy(program_image.data() + nso_header.segments[i].location, data.data(),
  86. data.size());
  87. codeset.segments[i].addr = nso_header.segments[i].location;
  88. codeset.segments[i].offset = nso_header.segments[i].location;
  89. codeset.segments[i].size = nso_header.segments[i].size;
  90. }
  91. if (should_pass_arguments) {
  92. std::vector<u8> arg_data{Settings::values.program_args.begin(),
  93. Settings::values.program_args.end()};
  94. if (arg_data.empty()) {
  95. arg_data.resize(NSO_ARGUMENT_DEFAULT_SIZE);
  96. }
  97. codeset.DataSegment().size += NSO_ARGUMENT_DATA_ALLOCATION_SIZE;
  98. NSOArgumentHeader args_header{
  99. NSO_ARGUMENT_DATA_ALLOCATION_SIZE, static_cast<u32_le>(arg_data.size()), {}};
  100. const auto end_offset = program_image.size();
  101. program_image.resize(static_cast<u32>(program_image.size()) +
  102. NSO_ARGUMENT_DATA_ALLOCATION_SIZE);
  103. std::memcpy(program_image.data() + end_offset, &args_header, sizeof(NSOArgumentHeader));
  104. std::memcpy(program_image.data() + end_offset + sizeof(NSOArgumentHeader), arg_data.data(),
  105. arg_data.size());
  106. }
  107. codeset.DataSegment().size += nso_header.segments[2].bss_size;
  108. const u32 image_size{
  109. PageAlignSize(static_cast<u32>(program_image.size()) + nso_header.segments[2].bss_size)};
  110. program_image.resize(image_size);
  111. for (std::size_t i = 0; i < nso_header.segments.size(); ++i) {
  112. codeset.segments[i].size = PageAlignSize(codeset.segments[i].size);
  113. }
  114. // Apply patches if necessary
  115. if (pm && (pm->HasNSOPatch(nso_header.build_id) || Settings::values.dump_nso)) {
  116. std::vector<u8> pi_header;
  117. pi_header.insert(pi_header.begin(), reinterpret_cast<u8*>(&nso_header),
  118. reinterpret_cast<u8*>(&nso_header) + sizeof(NSOHeader));
  119. pi_header.insert(pi_header.begin() + sizeof(NSOHeader), program_image.data(),
  120. program_image.data() + program_image.size());
  121. pi_header = pm->PatchNSO(pi_header, file.GetName());
  122. std::copy(pi_header.begin() + sizeof(NSOHeader), pi_header.end(), program_image.data());
  123. }
  124. // Apply cheats if they exist and the program has a valid title ID
  125. if (pm) {
  126. auto& system = Core::System::GetInstance();
  127. system.SetCurrentProcessBuildID(nso_header.build_id);
  128. const auto cheats = pm->CreateCheatList(system, nso_header.build_id);
  129. if (!cheats.empty()) {
  130. system.RegisterCheatList(cheats, nso_header.build_id, load_base, image_size);
  131. }
  132. }
  133. // Load codeset for current process
  134. codeset.memory = std::move(program_image);
  135. process.LoadModule(std::move(codeset), load_base);
  136. // Register module with GDBStub
  137. GDBStub::RegisterModule(file.GetName(), load_base, load_base);
  138. return load_base + image_size;
  139. }
  140. AppLoader_NSO::LoadResult AppLoader_NSO::Load(Kernel::Process& process) {
  141. if (is_loaded) {
  142. return {ResultStatus::ErrorAlreadyLoaded, {}};
  143. }
  144. modules.clear();
  145. // Load module
  146. const VAddr base_address = process.VMManager().GetCodeRegionBaseAddress();
  147. if (!LoadModule(process, *file, base_address, true)) {
  148. return {ResultStatus::ErrorLoadingNSO, {}};
  149. }
  150. modules.insert_or_assign(base_address, file->GetName());
  151. LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), base_address);
  152. is_loaded = true;
  153. return {ResultStatus::Success,
  154. LoadParameters{Kernel::THREADPRIO_DEFAULT, Memory::DEFAULT_STACK_SIZE}};
  155. }
  156. ResultStatus AppLoader_NSO::ReadNSOModules(Modules& modules) {
  157. modules = this->modules;
  158. return ResultStatus::Success;
  159. }
  160. } // namespace Loader