nso.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <vector>
  6. #include <lz4.h>
  7. #include "common/common_funcs.h"
  8. #include "common/file_util.h"
  9. #include "common/logging/log.h"
  10. #include "common/swap.h"
  11. #include "core/core.h"
  12. #include "core/gdbstub/gdbstub.h"
  13. #include "core/hle/kernel/kernel.h"
  14. #include "core/hle/kernel/process.h"
  15. #include "core/hle/kernel/resource_limit.h"
  16. #include "core/loader/nso.h"
  17. #include "core/memory.h"
  18. namespace Loader {
  19. struct NsoSegmentHeader {
  20. u32_le offset;
  21. u32_le location;
  22. u32_le size;
  23. union {
  24. u32_le alignment;
  25. u32_le bss_size;
  26. };
  27. };
  28. static_assert(sizeof(NsoSegmentHeader) == 0x10, "NsoSegmentHeader has incorrect size.");
  29. struct NsoHeader {
  30. u32_le magic;
  31. INSERT_PADDING_BYTES(0xc);
  32. std::array<NsoSegmentHeader, 3> segments; // Text, RoData, Data (in that order)
  33. u32_le bss_size;
  34. INSERT_PADDING_BYTES(0x1c);
  35. std::array<u32_le, 3> segments_compressed_size;
  36. };
  37. static_assert(sizeof(NsoHeader) == 0x6c, "NsoHeader has incorrect size.");
  38. static_assert(std::is_trivially_copyable_v<NsoHeader>, "NsoHeader isn't trivially copyable.");
  39. struct ModHeader {
  40. u32_le magic;
  41. u32_le dynamic_offset;
  42. u32_le bss_start_offset;
  43. u32_le bss_end_offset;
  44. u32_le eh_frame_hdr_start_offset;
  45. u32_le eh_frame_hdr_end_offset;
  46. u32_le module_offset; // Offset to runtime-generated module object. typically equal to .bss base
  47. };
  48. static_assert(sizeof(ModHeader) == 0x1c, "ModHeader has incorrect size.");
  49. AppLoader_NSO::AppLoader_NSO(FileSys::VirtualFile file) : AppLoader(std::move(file)) {}
  50. FileType AppLoader_NSO::IdentifyType(const FileSys::VirtualFile& file) {
  51. u32 magic = 0;
  52. if (file->ReadObject(&magic) != sizeof(magic)) {
  53. return FileType::Error;
  54. }
  55. if (Common::MakeMagic('N', 'S', 'O', '0') != magic) {
  56. return FileType::Error;
  57. }
  58. return FileType::NSO;
  59. }
  60. static std::vector<u8> DecompressSegment(const std::vector<u8>& compressed_data,
  61. const NsoSegmentHeader& header) {
  62. std::vector<u8> uncompressed_data(header.size);
  63. const int bytes_uncompressed =
  64. LZ4_decompress_safe(reinterpret_cast<const char*>(compressed_data.data()),
  65. reinterpret_cast<char*>(uncompressed_data.data()),
  66. static_cast<int>(compressed_data.size()), header.size);
  67. ASSERT_MSG(bytes_uncompressed == static_cast<int>(header.size) &&
  68. bytes_uncompressed == static_cast<int>(uncompressed_data.size()),
  69. "{} != {} != {}", bytes_uncompressed, header.size, uncompressed_data.size());
  70. return uncompressed_data;
  71. }
  72. static constexpr u32 PageAlignSize(u32 size) {
  73. return (size + Memory::PAGE_MASK) & ~Memory::PAGE_MASK;
  74. }
  75. VAddr AppLoader_NSO::LoadModule(FileSys::VirtualFile file, VAddr load_base) {
  76. if (file == nullptr)
  77. return {};
  78. if (file->GetSize() < sizeof(NsoHeader))
  79. return {};
  80. NsoHeader nso_header{};
  81. if (sizeof(NsoHeader) != file->ReadObject(&nso_header))
  82. return {};
  83. if (nso_header.magic != Common::MakeMagic('N', 'S', 'O', '0'))
  84. return {};
  85. // Build program image
  86. auto& kernel = Core::System::GetInstance().Kernel();
  87. Kernel::SharedPtr<Kernel::CodeSet> codeset = Kernel::CodeSet::Create(kernel, "");
  88. std::vector<u8> program_image;
  89. for (std::size_t i = 0; i < nso_header.segments.size(); ++i) {
  90. const std::vector<u8> compressed_data =
  91. file->ReadBytes(nso_header.segments_compressed_size[i], nso_header.segments[i].offset);
  92. std::vector<u8> data = DecompressSegment(compressed_data, nso_header.segments[i]);
  93. program_image.resize(nso_header.segments[i].location);
  94. program_image.insert(program_image.end(), data.begin(), data.end());
  95. codeset->segments[i].addr = nso_header.segments[i].location;
  96. codeset->segments[i].offset = nso_header.segments[i].location;
  97. codeset->segments[i].size = PageAlignSize(static_cast<u32>(data.size()));
  98. }
  99. // MOD header pointer is at .text offset + 4
  100. u32 module_offset;
  101. std::memcpy(&module_offset, program_image.data() + 4, sizeof(u32));
  102. // Read MOD header
  103. ModHeader mod_header{};
  104. // Default .bss to size in segment header if MOD0 section doesn't exist
  105. u32 bss_size{PageAlignSize(nso_header.segments[2].bss_size)};
  106. std::memcpy(&mod_header, program_image.data() + module_offset, sizeof(ModHeader));
  107. const bool has_mod_header{mod_header.magic == Common::MakeMagic('M', 'O', 'D', '0')};
  108. if (has_mod_header) {
  109. // Resize program image to include .bss section and page align each section
  110. bss_size = PageAlignSize(mod_header.bss_end_offset - mod_header.bss_start_offset);
  111. }
  112. codeset->DataSegment().size += bss_size;
  113. const u32 image_size{PageAlignSize(static_cast<u32>(program_image.size()) + bss_size)};
  114. program_image.resize(image_size);
  115. // Load codeset for current process
  116. codeset->name = file->GetName();
  117. codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
  118. Core::CurrentProcess()->LoadModule(codeset, load_base);
  119. // Register module with GDBStub
  120. GDBStub::RegisterModule(codeset->name, load_base, load_base);
  121. return load_base + image_size;
  122. }
  123. ResultStatus AppLoader_NSO::Load(Kernel::SharedPtr<Kernel::Process>& process) {
  124. if (is_loaded) {
  125. return ResultStatus::ErrorAlreadyLoaded;
  126. }
  127. // Load module
  128. LoadModule(file, Memory::PROCESS_IMAGE_VADDR);
  129. LOG_DEBUG(Loader, "loaded module {} @ 0x{:X}", file->GetName(), Memory::PROCESS_IMAGE_VADDR);
  130. auto& kernel = Core::System::GetInstance().Kernel();
  131. process->svc_access_mask.set();
  132. process->resource_limit =
  133. kernel.ResourceLimitForCategory(Kernel::ResourceLimitCategory::APPLICATION);
  134. process->Run(Memory::PROCESS_IMAGE_VADDR, Kernel::THREADPRIO_DEFAULT,
  135. Memory::DEFAULT_STACK_SIZE);
  136. is_loaded = true;
  137. return ResultStatus::Success;
  138. }
  139. } // namespace Loader