elf.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <memory>
  6. #include <string>
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "common/file_util.h"
  10. #include "common/logging/log.h"
  11. #include "core/core.h"
  12. #include "core/hle/kernel/process.h"
  13. #include "core/hle/kernel/resource_limit.h"
  14. #include "core/loader/elf.h"
  15. #include "core/memory.h"
  16. using Kernel::CodeSet;
  17. using Kernel::SharedPtr;
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. // ELF Header Constants
  20. // File type
  21. enum ElfType {
  22. ET_NONE = 0,
  23. ET_REL = 1,
  24. ET_EXEC = 2,
  25. ET_DYN = 3,
  26. ET_CORE = 4,
  27. ET_LOPROC = 0xFF00,
  28. ET_HIPROC = 0xFFFF,
  29. };
  30. // Machine/Architecture
  31. enum ElfMachine {
  32. EM_NONE = 0,
  33. EM_M32 = 1,
  34. EM_SPARC = 2,
  35. EM_386 = 3,
  36. EM_68K = 4,
  37. EM_88K = 5,
  38. EM_860 = 7,
  39. EM_MIPS = 8
  40. };
  41. // File version
  42. #define EV_NONE 0
  43. #define EV_CURRENT 1
  44. // Identification index
  45. #define EI_MAG0 0
  46. #define EI_MAG1 1
  47. #define EI_MAG2 2
  48. #define EI_MAG3 3
  49. #define EI_CLASS 4
  50. #define EI_DATA 5
  51. #define EI_VERSION 6
  52. #define EI_PAD 7
  53. #define EI_NIDENT 16
  54. // Sections constants
  55. // Section types
  56. #define SHT_NULL 0
  57. #define SHT_PROGBITS 1
  58. #define SHT_SYMTAB 2
  59. #define SHT_STRTAB 3
  60. #define SHT_RELA 4
  61. #define SHT_HASH 5
  62. #define SHT_DYNAMIC 6
  63. #define SHT_NOTE 7
  64. #define SHT_NOBITS 8
  65. #define SHT_REL 9
  66. #define SHT_SHLIB 10
  67. #define SHT_DYNSYM 11
  68. #define SHT_LOPROC 0x70000000
  69. #define SHT_HIPROC 0x7FFFFFFF
  70. #define SHT_LOUSER 0x80000000
  71. #define SHT_HIUSER 0xFFFFFFFF
  72. // Section flags
  73. enum ElfSectionFlags {
  74. SHF_WRITE = 0x1,
  75. SHF_ALLOC = 0x2,
  76. SHF_EXECINSTR = 0x4,
  77. SHF_MASKPROC = 0xF0000000,
  78. };
  79. // Segment types
  80. #define PT_NULL 0
  81. #define PT_LOAD 1
  82. #define PT_DYNAMIC 2
  83. #define PT_INTERP 3
  84. #define PT_NOTE 4
  85. #define PT_SHLIB 5
  86. #define PT_PHDR 6
  87. #define PT_LOPROC 0x70000000
  88. #define PT_HIPROC 0x7FFFFFFF
  89. // Segment flags
  90. #define PF_X 0x1
  91. #define PF_W 0x2
  92. #define PF_R 0x4
  93. #define PF_MASKPROC 0xF0000000
  94. typedef unsigned int Elf32_Addr;
  95. typedef unsigned short Elf32_Half;
  96. typedef unsigned int Elf32_Off;
  97. typedef signed int Elf32_Sword;
  98. typedef unsigned int Elf32_Word;
  99. ////////////////////////////////////////////////////////////////////////////////////////////////////
  100. // ELF file header
  101. struct Elf32_Ehdr {
  102. unsigned char e_ident[EI_NIDENT];
  103. Elf32_Half e_type;
  104. Elf32_Half e_machine;
  105. Elf32_Word e_version;
  106. Elf32_Addr e_entry;
  107. Elf32_Off e_phoff;
  108. Elf32_Off e_shoff;
  109. Elf32_Word e_flags;
  110. Elf32_Half e_ehsize;
  111. Elf32_Half e_phentsize;
  112. Elf32_Half e_phnum;
  113. Elf32_Half e_shentsize;
  114. Elf32_Half e_shnum;
  115. Elf32_Half e_shstrndx;
  116. };
  117. // Section header
  118. struct Elf32_Shdr {
  119. Elf32_Word sh_name;
  120. Elf32_Word sh_type;
  121. Elf32_Word sh_flags;
  122. Elf32_Addr sh_addr;
  123. Elf32_Off sh_offset;
  124. Elf32_Word sh_size;
  125. Elf32_Word sh_link;
  126. Elf32_Word sh_info;
  127. Elf32_Word sh_addralign;
  128. Elf32_Word sh_entsize;
  129. };
  130. // Segment header
  131. struct Elf32_Phdr {
  132. Elf32_Word p_type;
  133. Elf32_Off p_offset;
  134. Elf32_Addr p_vaddr;
  135. Elf32_Addr p_paddr;
  136. Elf32_Word p_filesz;
  137. Elf32_Word p_memsz;
  138. Elf32_Word p_flags;
  139. Elf32_Word p_align;
  140. };
  141. // Symbol table entry
  142. struct Elf32_Sym {
  143. Elf32_Word st_name;
  144. Elf32_Addr st_value;
  145. Elf32_Word st_size;
  146. unsigned char st_info;
  147. unsigned char st_other;
  148. Elf32_Half st_shndx;
  149. };
  150. // Relocation entries
  151. struct Elf32_Rel {
  152. Elf32_Addr r_offset;
  153. Elf32_Word r_info;
  154. };
  155. ////////////////////////////////////////////////////////////////////////////////////////////////////
  156. // ElfReader class
  157. typedef int SectionID;
  158. class ElfReader {
  159. private:
  160. char* base;
  161. u32* base32;
  162. Elf32_Ehdr* header;
  163. Elf32_Phdr* segments;
  164. Elf32_Shdr* sections;
  165. u32* sectionAddrs;
  166. bool relocate;
  167. u32 entryPoint;
  168. public:
  169. explicit ElfReader(void* ptr);
  170. u32 Read32(int off) const {
  171. return base32[off >> 2];
  172. }
  173. // Quick accessors
  174. ElfType GetType() const {
  175. return (ElfType)(header->e_type);
  176. }
  177. ElfMachine GetMachine() const {
  178. return (ElfMachine)(header->e_machine);
  179. }
  180. u32 GetEntryPoint() const {
  181. return entryPoint;
  182. }
  183. u32 GetFlags() const {
  184. return (u32)(header->e_flags);
  185. }
  186. SharedPtr<CodeSet> LoadInto(u32 vaddr);
  187. int GetNumSegments() const {
  188. return (int)(header->e_phnum);
  189. }
  190. int GetNumSections() const {
  191. return (int)(header->e_shnum);
  192. }
  193. const u8* GetPtr(int offset) const {
  194. return (u8*)base + offset;
  195. }
  196. const char* GetSectionName(int section) const;
  197. const u8* GetSectionDataPtr(int section) const {
  198. if (section < 0 || section >= header->e_shnum)
  199. return nullptr;
  200. if (sections[section].sh_type != SHT_NOBITS)
  201. return GetPtr(sections[section].sh_offset);
  202. else
  203. return nullptr;
  204. }
  205. bool IsCodeSection(int section) const {
  206. return sections[section].sh_type == SHT_PROGBITS;
  207. }
  208. const u8* GetSegmentPtr(int segment) {
  209. return GetPtr(segments[segment].p_offset);
  210. }
  211. u32 GetSectionAddr(SectionID section) const {
  212. return sectionAddrs[section];
  213. }
  214. unsigned int GetSectionSize(SectionID section) const {
  215. return sections[section].sh_size;
  216. }
  217. SectionID GetSectionByName(const char* name, int firstSection = 0) const; //-1 for not found
  218. bool DidRelocate() const {
  219. return relocate;
  220. }
  221. };
  222. ElfReader::ElfReader(void* ptr) {
  223. base = (char*)ptr;
  224. base32 = (u32*)ptr;
  225. header = (Elf32_Ehdr*)ptr;
  226. segments = (Elf32_Phdr*)(base + header->e_phoff);
  227. sections = (Elf32_Shdr*)(base + header->e_shoff);
  228. entryPoint = header->e_entry;
  229. }
  230. const char* ElfReader::GetSectionName(int section) const {
  231. if (sections[section].sh_type == SHT_NULL)
  232. return nullptr;
  233. int name_offset = sections[section].sh_name;
  234. const char* ptr = reinterpret_cast<const char*>(GetSectionDataPtr(header->e_shstrndx));
  235. if (ptr)
  236. return ptr + name_offset;
  237. return nullptr;
  238. }
  239. SharedPtr<CodeSet> ElfReader::LoadInto(u32 vaddr) {
  240. LOG_DEBUG(Loader, "String section: {}", header->e_shstrndx);
  241. // Should we relocate?
  242. relocate = (header->e_type != ET_EXEC);
  243. if (relocate) {
  244. LOG_DEBUG(Loader, "Relocatable module");
  245. entryPoint += vaddr;
  246. } else {
  247. LOG_DEBUG(Loader, "Prerelocated executable");
  248. }
  249. LOG_DEBUG(Loader, "{} segments:", header->e_phnum);
  250. // First pass : Get the bits into RAM
  251. u32 base_addr = relocate ? vaddr : 0;
  252. u32 total_image_size = 0;
  253. for (unsigned int i = 0; i < header->e_phnum; ++i) {
  254. Elf32_Phdr* p = &segments[i];
  255. if (p->p_type == PT_LOAD) {
  256. total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF;
  257. }
  258. }
  259. std::vector<u8> program_image(total_image_size);
  260. size_t current_image_position = 0;
  261. auto& kernel = Core::System::GetInstance().Kernel();
  262. SharedPtr<CodeSet> codeset = CodeSet::Create(kernel, "");
  263. for (unsigned int i = 0; i < header->e_phnum; ++i) {
  264. Elf32_Phdr* p = &segments[i];
  265. LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type,
  266. p->p_vaddr, p->p_filesz, p->p_memsz);
  267. if (p->p_type == PT_LOAD) {
  268. CodeSet::Segment* codeset_segment;
  269. u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X);
  270. if (permission_flags == (PF_R | PF_X)) {
  271. codeset_segment = &codeset->CodeSegment();
  272. } else if (permission_flags == (PF_R)) {
  273. codeset_segment = &codeset->RODataSegment();
  274. } else if (permission_flags == (PF_R | PF_W)) {
  275. codeset_segment = &codeset->DataSegment();
  276. } else {
  277. LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id {} with flags {:X}", i,
  278. p->p_flags);
  279. continue;
  280. }
  281. if (codeset_segment->size != 0) {
  282. LOG_ERROR(Loader,
  283. "ELF has more than one segment of the same type. Skipping extra "
  284. "segment (id {})",
  285. i);
  286. continue;
  287. }
  288. u32 segment_addr = base_addr + p->p_vaddr;
  289. u32 aligned_size = (p->p_memsz + 0xFFF) & ~0xFFF;
  290. codeset_segment->offset = current_image_position;
  291. codeset_segment->addr = segment_addr;
  292. codeset_segment->size = aligned_size;
  293. memcpy(&program_image[current_image_position], GetSegmentPtr(i), p->p_filesz);
  294. current_image_position += aligned_size;
  295. }
  296. }
  297. codeset->entrypoint = base_addr + header->e_entry;
  298. codeset->memory = std::make_shared<std::vector<u8>>(std::move(program_image));
  299. LOG_DEBUG(Loader, "Done loading.");
  300. return codeset;
  301. }
  302. SectionID ElfReader::GetSectionByName(const char* name, int firstSection) const {
  303. for (int i = firstSection; i < header->e_shnum; i++) {
  304. const char* secname = GetSectionName(i);
  305. if (secname != nullptr && strcmp(name, secname) == 0)
  306. return i;
  307. }
  308. return -1;
  309. }
  310. ////////////////////////////////////////////////////////////////////////////////////////////////////
  311. // Loader namespace
  312. namespace Loader {
  313. AppLoader_ELF::AppLoader_ELF(FileSys::VirtualFile file) : AppLoader(std::move(file)) {}
  314. FileType AppLoader_ELF::IdentifyType(const FileSys::VirtualFile& file) {
  315. static constexpr u16 ELF_MACHINE_ARM{0x28};
  316. u32 magic = 0;
  317. if (4 != file->ReadObject(&magic))
  318. return FileType::Error;
  319. u16 machine = 0;
  320. if (2 != file->ReadObject(&machine, 18))
  321. return FileType::Error;
  322. if (Common::MakeMagic('\x7f', 'E', 'L', 'F') == magic && ELF_MACHINE_ARM == machine)
  323. return FileType::ELF;
  324. return FileType::Error;
  325. }
  326. ResultStatus AppLoader_ELF::Load(Kernel::SharedPtr<Kernel::Process>& process) {
  327. if (is_loaded)
  328. return ResultStatus::ErrorAlreadyLoaded;
  329. std::vector<u8> buffer = file->ReadAllBytes();
  330. if (buffer.size() != file->GetSize())
  331. return ResultStatus::ErrorIncorrectELFFileSize;
  332. ElfReader elf_reader(&buffer[0]);
  333. SharedPtr<CodeSet> codeset = elf_reader.LoadInto(Memory::PROCESS_IMAGE_VADDR);
  334. codeset->name = file->GetName();
  335. process->LoadModule(codeset, codeset->entrypoint);
  336. process->svc_access_mask.set();
  337. // Attach the default resource limit (APPLICATION) to the process
  338. auto& kernel = Core::System::GetInstance().Kernel();
  339. process->resource_limit =
  340. kernel.ResourceLimitForCategory(Kernel::ResourceLimitCategory::APPLICATION);
  341. process->Run(codeset->entrypoint, 48, Memory::DEFAULT_STACK_SIZE);
  342. is_loaded = true;
  343. return ResultStatus::Success;
  344. }
  345. } // namespace Loader