elf.cpp 11 KB

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