3dsx.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "core/file_sys/archive_romfs.h"
  7. #include "core/loader/elf.h"
  8. #include "core/loader/ncch.h"
  9. #include "core/hle/service/fs/archive.h"
  10. #include "core/mem_map.h"
  11. #include "3dsx.h"
  12. namespace Loader {
  13. /**
  14. * File layout:
  15. * - File header
  16. * - Code, rodata and data relocation table headers
  17. * - Code segment
  18. * - Rodata segment
  19. * - Loadable (non-BSS) part of the data segment
  20. * - Code relocation table
  21. * - Rodata relocation table
  22. * - Data relocation table
  23. *
  24. * Memory layout before relocations are applied:
  25. * [0..codeSegSize) -> code segment
  26. * [codeSegSize..rodataSegSize) -> rodata segment
  27. * [rodataSegSize..dataSegSize) -> data segment
  28. *
  29. * Memory layout after relocations are applied: well, however the loader sets it up :)
  30. * The entrypoint is always the start of the code segment.
  31. * The BSS section must be cleared manually by the application.
  32. */
  33. enum THREEDSX_Error {
  34. ERROR_NONE = 0,
  35. ERROR_READ = 1,
  36. ERROR_FILE = 2,
  37. ERROR_ALLOC = 3
  38. };
  39. static const u32 RELOCBUFSIZE = 512;
  40. // File header
  41. static const u32 THREEDSX_MAGIC = 0x58534433; // '3DSX'
  42. #pragma pack(1)
  43. struct THREEDSX_Header
  44. {
  45. u32 magic;
  46. u16 header_size, reloc_hdr_size;
  47. u32 format_ver;
  48. u32 flags;
  49. // Sizes of the code, rodata and data segments +
  50. // size of the BSS section (uninitialized latter half of the data segment)
  51. u32 code_seg_size, rodata_seg_size, data_seg_size, bss_size;
  52. };
  53. // Relocation header: all fields (even extra unknown fields) are guaranteed to be relocation counts.
  54. struct THREEDSX_RelocHdr
  55. {
  56. // # of absolute relocations (that is, fix address to post-relocation memory layout)
  57. u32 cross_segment_absolute;
  58. // # of cross-segment relative relocations (that is, 32bit signed offsets that need to be patched)
  59. u32 cross_segment_relative;
  60. // more?
  61. // Relocations are written in this order:
  62. // - Absolute relocations
  63. // - Relative relocations
  64. };
  65. // Relocation entry: from the current pointer, skip X words and patch Y words
  66. struct THREEDSX_Reloc
  67. {
  68. u16 skip, patch;
  69. };
  70. #pragma pack()
  71. struct THREEloadinfo
  72. {
  73. u8* seg_ptrs[3]; // code, rodata & data
  74. u32 seg_addrs[3];
  75. u32 seg_sizes[3];
  76. };
  77. static u32 TranslateAddr(u32 addr, THREEloadinfo *loadinfo, u32* offsets)
  78. {
  79. if (addr < offsets[0])
  80. return loadinfo->seg_addrs[0] + addr;
  81. if (addr < offsets[1])
  82. return loadinfo->seg_addrs[1] + addr - offsets[0];
  83. return loadinfo->seg_addrs[2] + addr - offsets[1];
  84. }
  85. static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
  86. {
  87. if (!file.IsOpen())
  88. return ERROR_FILE;
  89. THREEDSX_Header hdr;
  90. if (file.ReadBytes(&hdr, sizeof(hdr)) != sizeof(hdr))
  91. return ERROR_READ;
  92. THREEloadinfo loadinfo;
  93. //loadinfo segments must be a multiple of 0x1000
  94. loadinfo.seg_sizes[0] = (hdr.code_seg_size + 0xFFF) &~0xFFF;
  95. loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF;
  96. loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF;
  97. u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] };
  98. u32 data_load_size = (hdr.data_seg_size - hdr.bss_size + 0xFFF) &~0xFFF;
  99. u32 bss_load_size = loadinfo.seg_sizes[2] - data_load_size;
  100. u32 n_reloc_tables = hdr.reloc_hdr_size / 4;
  101. std::vector<u8> all_mem(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2] + 3 * n_reloc_tables);
  102. loadinfo.seg_addrs[0] = base_addr;
  103. loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
  104. loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
  105. loadinfo.seg_ptrs[0] = &all_mem[0];
  106. loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
  107. loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
  108. // Skip header for future compatibility
  109. file.Seek(hdr.header_size, SEEK_SET);
  110. // Read the relocation headers
  111. u32* relocs = (u32*)(loadinfo.seg_ptrs[2] + hdr.data_seg_size);
  112. for (u32 current_segment = 0; current_segment < 3; current_segment++) {
  113. if (file.ReadBytes(&relocs[current_segment*n_reloc_tables], n_reloc_tables * 4) != n_reloc_tables * 4)
  114. return ERROR_READ;
  115. }
  116. // Read the segments
  117. if (file.ReadBytes(loadinfo.seg_ptrs[0], hdr.code_seg_size) != hdr.code_seg_size)
  118. return ERROR_READ;
  119. if (file.ReadBytes(loadinfo.seg_ptrs[1], hdr.rodata_seg_size) != hdr.rodata_seg_size)
  120. return ERROR_READ;
  121. if (file.ReadBytes(loadinfo.seg_ptrs[2], hdr.data_seg_size - hdr.bss_size) != hdr.data_seg_size - hdr.bss_size)
  122. return ERROR_READ;
  123. // BSS clear
  124. memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
  125. // Relocate the segments
  126. for (u32 current_segment = 0; current_segment < 3; current_segment++) {
  127. for (u32 current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) {
  128. u32 n_relocs = relocs[current_segment*n_reloc_tables + current_segment_reloc_table];
  129. if (current_segment_reloc_table >= 2) {
  130. // We are not using this table - ignore it because we don't know what it dose
  131. file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR);
  132. continue;
  133. }
  134. static THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
  135. u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
  136. u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
  137. while (n_relocs) {
  138. u32 remaining = std::min(RELOCBUFSIZE, n_relocs);
  139. n_relocs -= remaining;
  140. if (file.ReadBytes(reloc_table, remaining*sizeof(THREEDSX_Reloc)) != remaining*sizeof(THREEDSX_Reloc))
  141. return ERROR_READ;
  142. for (u32 current_inprogress = 0; current_inprogress < remaining && pos < end_pos; current_inprogress++) {
  143. LOG_TRACE(Loader, "(t=%d,skip=%u,patch=%u)\n",
  144. current_segment_reloc_table, (u32)reloc_table[current_inprogress].skip, (u32)reloc_table[current_inprogress].patch);
  145. pos += reloc_table[current_inprogress].skip;
  146. s32 num_patches = reloc_table[current_inprogress].patch;
  147. while (0 < num_patches && pos < end_pos) {
  148. u32 in_addr = (char*)pos - (char*)&all_mem[0];
  149. u32 addr = TranslateAddr(*pos, &loadinfo, offsets);
  150. LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n",
  151. base_addr + in_addr, addr, current_segment_reloc_table, *pos);
  152. switch (current_segment_reloc_table) {
  153. case 0: *pos = (addr); break;
  154. case 1: *pos = (addr - in_addr); break;
  155. default: break; //this should never happen
  156. }
  157. pos++;
  158. num_patches--;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. // Write the data
  165. memcpy(Memory::GetPointer(base_addr), &all_mem[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
  166. LOG_DEBUG(Loader, "CODE: %u pages\n", loadinfo.seg_sizes[0] / 0x1000);
  167. LOG_DEBUG(Loader, "RODATA: %u pages\n", loadinfo.seg_sizes[1] / 0x1000);
  168. LOG_DEBUG(Loader, "DATA: %u pages\n", data_load_size / 0x1000);
  169. LOG_DEBUG(Loader, "BSS: %u pages\n", bss_load_size / 0x1000);
  170. return ERROR_NONE;
  171. }
  172. /// AppLoader_DSX constructor
  173. AppLoader_THREEDSX::AppLoader_THREEDSX(const std::string& filename) : filename(filename) {
  174. }
  175. /// AppLoader_DSX destructor
  176. AppLoader_THREEDSX::~AppLoader_THREEDSX() {
  177. }
  178. ResultStatus AppLoader_THREEDSX::Load() {
  179. LOG_INFO(Loader, "Loading 3DSX file %s...", filename.c_str());
  180. if (is_loaded)
  181. return ResultStatus::ErrorAlreadyLoaded;
  182. FileUtil::IOFile file(filename, "rb");
  183. if (file.IsOpen()) {
  184. Load3DSXFile(file, 0x00100000);
  185. Kernel::LoadExec(0x00100000);
  186. } else {
  187. return ResultStatus::Error;
  188. }
  189. is_loaded = true;
  190. return ResultStatus::Success;
  191. }
  192. } // namespace Loader