3dsx.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <vector>
  6. #include "common/logging/log.h"
  7. #include "core/file_sys/archive_romfs.h"
  8. #include "core/hle/kernel/process.h"
  9. #include "core/hle/kernel/resource_limit.h"
  10. #include "core/hle/service/fs/archive.h"
  11. #include "core/loader/elf.h"
  12. #include "core/loader/ncch.h"
  13. #include "core/memory.h"
  14. #include "3dsx.h"
  15. namespace Loader {
  16. /*
  17. * File layout:
  18. * - File header
  19. * - Code, rodata and data relocation table headers
  20. * - Code segment
  21. * - Rodata segment
  22. * - Loadable (non-BSS) part of the data segment
  23. * - Code relocation table
  24. * - Rodata relocation table
  25. * - Data relocation table
  26. *
  27. * Memory layout before relocations are applied:
  28. * [0..codeSegSize) -> code segment
  29. * [codeSegSize..rodataSegSize) -> rodata segment
  30. * [rodataSegSize..dataSegSize) -> data segment
  31. *
  32. * Memory layout after relocations are applied: well, however the loader sets it up :)
  33. * The entrypoint is always the start of the code segment.
  34. * The BSS section must be cleared manually by the application.
  35. */
  36. enum THREEDSX_Error {
  37. ERROR_NONE = 0,
  38. ERROR_READ = 1,
  39. ERROR_FILE = 2,
  40. ERROR_ALLOC = 3
  41. };
  42. static const u32 RELOCBUFSIZE = 512;
  43. static const unsigned int NUM_SEGMENTS = 3;
  44. // File header
  45. #pragma pack(1)
  46. struct THREEDSX_Header
  47. {
  48. u32 magic;
  49. u16 header_size, reloc_hdr_size;
  50. u32 format_ver;
  51. u32 flags;
  52. // Sizes of the code, rodata and data segments +
  53. // size of the BSS section (uninitialized latter half of the data segment)
  54. u32 code_seg_size, rodata_seg_size, data_seg_size, bss_size;
  55. };
  56. // Relocation header: all fields (even extra unknown fields) are guaranteed to be relocation counts.
  57. struct THREEDSX_RelocHdr
  58. {
  59. // # of absolute relocations (that is, fix address to post-relocation memory layout)
  60. u32 cross_segment_absolute;
  61. // # of cross-segment relative relocations (that is, 32bit signed offsets that need to be patched)
  62. u32 cross_segment_relative;
  63. // more?
  64. // Relocations are written in this order:
  65. // - Absolute relocations
  66. // - Relative relocations
  67. };
  68. // Relocation entry: from the current pointer, skip X words and patch Y words
  69. struct THREEDSX_Reloc
  70. {
  71. u16 skip, patch;
  72. };
  73. #pragma pack()
  74. struct THREEloadinfo
  75. {
  76. u8* seg_ptrs[3]; // code, rodata & data
  77. u32 seg_addrs[3];
  78. u32 seg_sizes[3];
  79. };
  80. static u32 TranslateAddr(u32 addr, const THREEloadinfo *loadinfo, u32* offsets)
  81. {
  82. if (addr < offsets[0])
  83. return loadinfo->seg_addrs[0] + addr;
  84. if (addr < offsets[1])
  85. return loadinfo->seg_addrs[1] + addr - offsets[0];
  86. return loadinfo->seg_addrs[2] + addr - offsets[1];
  87. }
  88. using Kernel::SharedPtr;
  89. using Kernel::CodeSet;
  90. static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr, SharedPtr<CodeSet>* out_codeset)
  91. {
  92. if (!file.IsOpen())
  93. return ERROR_FILE;
  94. // Reset read pointer in case this file has been read before.
  95. file.Seek(0, SEEK_SET);
  96. THREEDSX_Header hdr;
  97. if (file.ReadBytes(&hdr, sizeof(hdr)) != sizeof(hdr))
  98. return ERROR_READ;
  99. THREEloadinfo loadinfo;
  100. //loadinfo segments must be a multiple of 0x1000
  101. loadinfo.seg_sizes[0] = (hdr.code_seg_size + 0xFFF) &~0xFFF;
  102. loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF;
  103. loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF;
  104. u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] };
  105. u32 n_reloc_tables = hdr.reloc_hdr_size / sizeof(u32);
  106. std::vector<u8> program_image(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
  107. loadinfo.seg_addrs[0] = base_addr;
  108. loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
  109. loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
  110. loadinfo.seg_ptrs[0] = program_image.data();
  111. loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
  112. loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
  113. // Skip header for future compatibility
  114. file.Seek(hdr.header_size, SEEK_SET);
  115. // Read the relocation headers
  116. std::vector<u32> relocs(n_reloc_tables * NUM_SEGMENTS);
  117. for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
  118. size_t size = n_reloc_tables * sizeof(u32);
  119. if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size)
  120. return ERROR_READ;
  121. }
  122. // Read the segments
  123. if (file.ReadBytes(loadinfo.seg_ptrs[0], hdr.code_seg_size) != hdr.code_seg_size)
  124. return ERROR_READ;
  125. if (file.ReadBytes(loadinfo.seg_ptrs[1], hdr.rodata_seg_size) != hdr.rodata_seg_size)
  126. return ERROR_READ;
  127. if (file.ReadBytes(loadinfo.seg_ptrs[2], hdr.data_seg_size - hdr.bss_size) != hdr.data_seg_size - hdr.bss_size)
  128. return ERROR_READ;
  129. // BSS clear
  130. memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
  131. // Relocate the segments
  132. for (unsigned int current_segment = 0; current_segment < NUM_SEGMENTS; ++current_segment) {
  133. for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) {
  134. u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table];
  135. if (current_segment_reloc_table >= 2) {
  136. // We are not using this table - ignore it because we don't know what it dose
  137. file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR);
  138. continue;
  139. }
  140. THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
  141. u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
  142. const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
  143. while (n_relocs) {
  144. u32 remaining = std::min(RELOCBUFSIZE, n_relocs);
  145. n_relocs -= remaining;
  146. if (file.ReadBytes(reloc_table, remaining * sizeof(THREEDSX_Reloc)) != remaining * sizeof(THREEDSX_Reloc))
  147. return ERROR_READ;
  148. for (unsigned current_inprogress = 0; current_inprogress < remaining && pos < end_pos; current_inprogress++) {
  149. const auto& table = reloc_table[current_inprogress];
  150. LOG_TRACE(Loader, "(t=%d,skip=%u,patch=%u)\n", current_segment_reloc_table,
  151. (u32)table.skip, (u32)table.patch);
  152. pos += table.skip;
  153. s32 num_patches = table.patch;
  154. while (0 < num_patches && pos < end_pos) {
  155. u32 in_addr = (u8*)pos - program_image.data();
  156. u32 addr = TranslateAddr(*pos, &loadinfo, offsets);
  157. LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n",
  158. base_addr + in_addr, addr, current_segment_reloc_table, *pos);
  159. switch (current_segment_reloc_table) {
  160. case 0:
  161. *pos = (addr);
  162. break;
  163. case 1:
  164. *pos = (addr - in_addr);
  165. break;
  166. default:
  167. break; //this should never happen
  168. }
  169. pos++;
  170. num_patches--;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. // Create the CodeSet
  177. SharedPtr<CodeSet> code_set = CodeSet::Create("", 0);
  178. code_set->code.offset = loadinfo.seg_ptrs[0] - program_image.data();
  179. code_set->code.addr = loadinfo.seg_addrs[0];
  180. code_set->code.size = loadinfo.seg_sizes[0];
  181. code_set->rodata.offset = loadinfo.seg_ptrs[1] - program_image.data();
  182. code_set->rodata.addr = loadinfo.seg_addrs[1];
  183. code_set->rodata.size = loadinfo.seg_sizes[1];
  184. code_set->data.offset = loadinfo.seg_ptrs[2] - program_image.data();
  185. code_set->data.addr = loadinfo.seg_addrs[2];
  186. code_set->data.size = loadinfo.seg_sizes[2];
  187. code_set->entrypoint = code_set->code.addr;
  188. code_set->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
  189. LOG_DEBUG(Loader, "code size: 0x%X", loadinfo.seg_sizes[0]);
  190. LOG_DEBUG(Loader, "rodata size: 0x%X", loadinfo.seg_sizes[1]);
  191. LOG_DEBUG(Loader, "data size: 0x%X (including 0x%X of bss)", loadinfo.seg_sizes[2], hdr.bss_size);
  192. *out_codeset = code_set;
  193. return ERROR_NONE;
  194. }
  195. FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) {
  196. u32 magic;
  197. file.Seek(0, SEEK_SET);
  198. if (1 != file.ReadArray<u32>(&magic, 1))
  199. return FileType::Error;
  200. if (MakeMagic('3', 'D', 'S', 'X') == magic)
  201. return FileType::THREEDSX;
  202. return FileType::Error;
  203. }
  204. ResultStatus AppLoader_THREEDSX::Load() {
  205. if (is_loaded)
  206. return ResultStatus::ErrorAlreadyLoaded;
  207. if (!file.IsOpen())
  208. return ResultStatus::Error;
  209. SharedPtr<CodeSet> codeset;
  210. if (Load3DSXFile(file, Memory::PROCESS_IMAGE_VADDR, &codeset) != ERROR_NONE)
  211. return ResultStatus::Error;
  212. codeset->name = filename;
  213. Kernel::g_current_process = Kernel::Process::Create(std::move(codeset));
  214. Kernel::g_current_process->svc_access_mask.set();
  215. Kernel::g_current_process->address_mappings = default_address_mappings;
  216. // Attach the default resource limit (APPLICATION) to the process
  217. Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
  218. Kernel::g_current_process->Run(48, Kernel::DEFAULT_STACK_SIZE);
  219. is_loaded = true;
  220. return ResultStatus::Success;
  221. }
  222. } // namespace Loader