3dsx.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // File header
  44. #pragma pack(1)
  45. struct THREEDSX_Header
  46. {
  47. u32 magic;
  48. u16 header_size, reloc_hdr_size;
  49. u32 format_ver;
  50. u32 flags;
  51. // Sizes of the code, rodata and data segments +
  52. // size of the BSS section (uninitialized latter half of the data segment)
  53. u32 code_seg_size, rodata_seg_size, data_seg_size, bss_size;
  54. };
  55. // Relocation header: all fields (even extra unknown fields) are guaranteed to be relocation counts.
  56. struct THREEDSX_RelocHdr
  57. {
  58. // # of absolute relocations (that is, fix address to post-relocation memory layout)
  59. u32 cross_segment_absolute;
  60. // # of cross-segment relative relocations (that is, 32bit signed offsets that need to be patched)
  61. u32 cross_segment_relative;
  62. // more?
  63. // Relocations are written in this order:
  64. // - Absolute relocations
  65. // - Relative relocations
  66. };
  67. // Relocation entry: from the current pointer, skip X words and patch Y words
  68. struct THREEDSX_Reloc
  69. {
  70. u16 skip, patch;
  71. };
  72. #pragma pack()
  73. struct THREEloadinfo
  74. {
  75. u8* seg_ptrs[3]; // code, rodata & data
  76. u32 seg_addrs[3];
  77. u32 seg_sizes[3];
  78. };
  79. static u32 TranslateAddr(u32 addr, const THREEloadinfo *loadinfo, u32* offsets)
  80. {
  81. if (addr < offsets[0])
  82. return loadinfo->seg_addrs[0] + addr;
  83. if (addr < offsets[1])
  84. return loadinfo->seg_addrs[1] + addr - offsets[0];
  85. return loadinfo->seg_addrs[2] + addr - offsets[1];
  86. }
  87. static THREEDSX_Error Load3DSXFile(FileUtil::IOFile& file, u32 base_addr)
  88. {
  89. if (!file.IsOpen())
  90. return ERROR_FILE;
  91. // Reset read pointer in case this file has been read before.
  92. file.Seek(0, SEEK_SET);
  93. THREEDSX_Header hdr;
  94. if (file.ReadBytes(&hdr, sizeof(hdr)) != sizeof(hdr))
  95. return ERROR_READ;
  96. THREEloadinfo loadinfo;
  97. //loadinfo segments must be a multiple of 0x1000
  98. loadinfo.seg_sizes[0] = (hdr.code_seg_size + 0xFFF) &~0xFFF;
  99. loadinfo.seg_sizes[1] = (hdr.rodata_seg_size + 0xFFF) &~0xFFF;
  100. loadinfo.seg_sizes[2] = (hdr.data_seg_size + 0xFFF) &~0xFFF;
  101. u32 offsets[2] = { loadinfo.seg_sizes[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] };
  102. u32 data_load_size = (hdr.data_seg_size - hdr.bss_size + 0xFFF) &~0xFFF;
  103. u32 bss_load_size = loadinfo.seg_sizes[2] - data_load_size;
  104. u32 n_reloc_tables = hdr.reloc_hdr_size / 4;
  105. std::vector<u8> all_mem(loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2] + 3 * n_reloc_tables);
  106. loadinfo.seg_addrs[0] = base_addr;
  107. loadinfo.seg_addrs[1] = loadinfo.seg_addrs[0] + loadinfo.seg_sizes[0];
  108. loadinfo.seg_addrs[2] = loadinfo.seg_addrs[1] + loadinfo.seg_sizes[1];
  109. loadinfo.seg_ptrs[0] = &all_mem[0];
  110. loadinfo.seg_ptrs[1] = loadinfo.seg_ptrs[0] + loadinfo.seg_sizes[0];
  111. loadinfo.seg_ptrs[2] = loadinfo.seg_ptrs[1] + loadinfo.seg_sizes[1];
  112. // Skip header for future compatibility
  113. file.Seek(hdr.header_size, SEEK_SET);
  114. // Read the relocation headers
  115. u32* relocs = (u32*)(loadinfo.seg_ptrs[2] + hdr.data_seg_size);
  116. for (unsigned current_segment : {0, 1, 2}) {
  117. size_t size = n_reloc_tables * 4;
  118. if (file.ReadBytes(&relocs[current_segment * n_reloc_tables], size) != size)
  119. return ERROR_READ;
  120. }
  121. // Read the segments
  122. if (file.ReadBytes(loadinfo.seg_ptrs[0], hdr.code_seg_size) != hdr.code_seg_size)
  123. return ERROR_READ;
  124. if (file.ReadBytes(loadinfo.seg_ptrs[1], hdr.rodata_seg_size) != hdr.rodata_seg_size)
  125. return ERROR_READ;
  126. if (file.ReadBytes(loadinfo.seg_ptrs[2], hdr.data_seg_size - hdr.bss_size) != hdr.data_seg_size - hdr.bss_size)
  127. return ERROR_READ;
  128. // BSS clear
  129. memset((char*)loadinfo.seg_ptrs[2] + hdr.data_seg_size - hdr.bss_size, 0, hdr.bss_size);
  130. // Relocate the segments
  131. for (unsigned current_segment : {0, 1, 2}) {
  132. for (unsigned current_segment_reloc_table = 0; current_segment_reloc_table < n_reloc_tables; current_segment_reloc_table++) {
  133. u32 n_relocs = relocs[current_segment * n_reloc_tables + current_segment_reloc_table];
  134. if (current_segment_reloc_table >= 2) {
  135. // We are not using this table - ignore it because we don't know what it dose
  136. file.Seek(n_relocs*sizeof(THREEDSX_Reloc), SEEK_CUR);
  137. continue;
  138. }
  139. static THREEDSX_Reloc reloc_table[RELOCBUFSIZE];
  140. u32* pos = (u32*)loadinfo.seg_ptrs[current_segment];
  141. const u32* end_pos = pos + (loadinfo.seg_sizes[current_segment] / 4);
  142. while (n_relocs) {
  143. u32 remaining = std::min(RELOCBUFSIZE, n_relocs);
  144. n_relocs -= remaining;
  145. if (file.ReadBytes(reloc_table, remaining * sizeof(THREEDSX_Reloc)) != remaining * sizeof(THREEDSX_Reloc))
  146. return ERROR_READ;
  147. for (unsigned current_inprogress = 0; current_inprogress < remaining && pos < end_pos; current_inprogress++) {
  148. const auto& table = reloc_table[current_inprogress];
  149. LOG_TRACE(Loader, "(t=%d,skip=%u,patch=%u)\n", current_segment_reloc_table,
  150. (u32)table.skip, (u32)table.patch);
  151. pos += table.skip;
  152. s32 num_patches = table.patch;
  153. while (0 < num_patches && pos < end_pos) {
  154. u32 in_addr = (char*)pos - (char*)&all_mem[0];
  155. u32 addr = TranslateAddr(*pos, &loadinfo, offsets);
  156. LOG_TRACE(Loader, "Patching %08X <-- rel(%08X,%d) (%08X)\n",
  157. base_addr + in_addr, addr, current_segment_reloc_table, *pos);
  158. switch (current_segment_reloc_table) {
  159. case 0:
  160. *pos = (addr);
  161. break;
  162. case 1:
  163. *pos = (addr - in_addr);
  164. break;
  165. default:
  166. break; //this should never happen
  167. }
  168. pos++;
  169. num_patches--;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. // Write the data
  176. memcpy(Memory::GetPointer(base_addr), &all_mem[0], loadinfo.seg_sizes[0] + loadinfo.seg_sizes[1] + loadinfo.seg_sizes[2]);
  177. LOG_DEBUG(Loader, "CODE: %u pages\n", loadinfo.seg_sizes[0] / 0x1000);
  178. LOG_DEBUG(Loader, "RODATA: %u pages\n", loadinfo.seg_sizes[1] / 0x1000);
  179. LOG_DEBUG(Loader, "DATA: %u pages\n", data_load_size / 0x1000);
  180. LOG_DEBUG(Loader, "BSS: %u pages\n", bss_load_size / 0x1000);
  181. return ERROR_NONE;
  182. }
  183. FileType AppLoader_THREEDSX::IdentifyType(FileUtil::IOFile& file) {
  184. u32 magic;
  185. file.Seek(0, SEEK_SET);
  186. if (1 != file.ReadArray<u32>(&magic, 1))
  187. return FileType::Error;
  188. if (MakeMagic('3', 'D', 'S', 'X') == magic)
  189. return FileType::THREEDSX;
  190. return FileType::Error;
  191. }
  192. ResultStatus AppLoader_THREEDSX::Load() {
  193. if (is_loaded)
  194. return ResultStatus::ErrorAlreadyLoaded;
  195. if (!file->IsOpen())
  196. return ResultStatus::Error;
  197. Kernel::g_current_process = Kernel::Process::Create(filename, 0);
  198. Kernel::g_current_process->svc_access_mask.set();
  199. Kernel::g_current_process->address_mappings = default_address_mappings;
  200. // Attach the default resource limit (APPLICATION) to the process
  201. Kernel::g_current_process->resource_limit = Kernel::ResourceLimit::GetForCategory(Kernel::ResourceLimitCategory::APPLICATION);
  202. Load3DSXFile(*file, Memory::PROCESS_IMAGE_VADDR);
  203. Kernel::g_current_process->Run(Memory::PROCESS_IMAGE_VADDR, 48, Kernel::DEFAULT_STACK_SIZE);
  204. is_loaded = true;
  205. return ResultStatus::Success;
  206. }
  207. } // namespace Loader