elf.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <string>
  5. #include <memory>
  6. #include "common/common_types.h"
  7. #include "common/file_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/symbols.h"
  10. #include "core/mem_map.h"
  11. #include "core/loader/elf.h"
  12. #include "core/hle/kernel/kernel.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // ELF Header Constants
  15. // File type
  16. enum ElfType {
  17. ET_NONE = 0,
  18. ET_REL = 1,
  19. ET_EXEC = 2,
  20. ET_DYN = 3,
  21. ET_CORE = 4,
  22. ET_LOPROC = 0xFF00,
  23. ET_HIPROC = 0xFFFF,
  24. };
  25. // Machine/Architecture
  26. enum ElfMachine {
  27. EM_NONE = 0,
  28. EM_M32 = 1,
  29. EM_SPARC = 2,
  30. EM_386 = 3,
  31. EM_68K = 4,
  32. EM_88K = 5,
  33. EM_860 = 7,
  34. EM_MIPS = 8
  35. };
  36. // File version
  37. #define EV_NONE 0
  38. #define EV_CURRENT 1
  39. // Identification index
  40. #define EI_MAG0 0
  41. #define EI_MAG1 1
  42. #define EI_MAG2 2
  43. #define EI_MAG3 3
  44. #define EI_CLASS 4
  45. #define EI_DATA 5
  46. #define EI_VERSION 6
  47. #define EI_PAD 7
  48. #define EI_NIDENT 16
  49. // Sections constants
  50. // Section types
  51. #define SHT_NULL 0
  52. #define SHT_PROGBITS 1
  53. #define SHT_SYMTAB 2
  54. #define SHT_STRTAB 3
  55. #define SHT_RELA 4
  56. #define SHT_HASH 5
  57. #define SHT_DYNAMIC 6
  58. #define SHT_NOTE 7
  59. #define SHT_NOBITS 8
  60. #define SHT_REL 9
  61. #define SHT_SHLIB 10
  62. #define SHT_DYNSYM 11
  63. #define SHT_LOPROC 0x70000000
  64. #define SHT_HIPROC 0x7FFFFFFF
  65. #define SHT_LOUSER 0x80000000
  66. #define SHT_HIUSER 0xFFFFFFFF
  67. // Section flags
  68. enum ElfSectionFlags
  69. {
  70. SHF_WRITE = 0x1,
  71. SHF_ALLOC = 0x2,
  72. SHF_EXECINSTR = 0x4,
  73. SHF_MASKPROC = 0xF0000000,
  74. };
  75. // Segment types
  76. #define PT_NULL 0
  77. #define PT_LOAD 1
  78. #define PT_DYNAMIC 2
  79. #define PT_INTERP 3
  80. #define PT_NOTE 4
  81. #define PT_SHLIB 5
  82. #define PT_PHDR 6
  83. #define PT_LOPROC 0x70000000
  84. #define PT_HIPROC 0x7FFFFFFF
  85. typedef unsigned int Elf32_Addr;
  86. typedef unsigned short Elf32_Half;
  87. typedef unsigned int Elf32_Off;
  88. typedef signed int Elf32_Sword;
  89. typedef unsigned int Elf32_Word;
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////
  91. // ELF file header
  92. struct Elf32_Ehdr {
  93. unsigned char e_ident[EI_NIDENT];
  94. Elf32_Half e_type;
  95. Elf32_Half e_machine;
  96. Elf32_Word e_version;
  97. Elf32_Addr e_entry;
  98. Elf32_Off e_phoff;
  99. Elf32_Off e_shoff;
  100. Elf32_Word e_flags;
  101. Elf32_Half e_ehsize;
  102. Elf32_Half e_phentsize;
  103. Elf32_Half e_phnum;
  104. Elf32_Half e_shentsize;
  105. Elf32_Half e_shnum;
  106. Elf32_Half e_shstrndx;
  107. };
  108. // Section header
  109. struct Elf32_Shdr {
  110. Elf32_Word sh_name;
  111. Elf32_Word sh_type;
  112. Elf32_Word sh_flags;
  113. Elf32_Addr sh_addr;
  114. Elf32_Off sh_offset;
  115. Elf32_Word sh_size;
  116. Elf32_Word sh_link;
  117. Elf32_Word sh_info;
  118. Elf32_Word sh_addralign;
  119. Elf32_Word sh_entsize;
  120. };
  121. // Segment header
  122. struct Elf32_Phdr {
  123. Elf32_Word p_type;
  124. Elf32_Off p_offset;
  125. Elf32_Addr p_vaddr;
  126. Elf32_Addr p_paddr;
  127. Elf32_Word p_filesz;
  128. Elf32_Word p_memsz;
  129. Elf32_Word p_flags;
  130. Elf32_Word p_align;
  131. };
  132. // Symbol table entry
  133. struct Elf32_Sym {
  134. Elf32_Word st_name;
  135. Elf32_Addr st_value;
  136. Elf32_Word st_size;
  137. unsigned char st_info;
  138. unsigned char st_other;
  139. Elf32_Half st_shndx;
  140. };
  141. // Relocation entries
  142. struct Elf32_Rel {
  143. Elf32_Addr r_offset;
  144. Elf32_Word r_info;
  145. };
  146. ////////////////////////////////////////////////////////////////////////////////////////////////////
  147. // ElfReader class
  148. typedef int SectionID;
  149. class ElfReader {
  150. private:
  151. char *base;
  152. u32 *base32;
  153. Elf32_Ehdr *header;
  154. Elf32_Phdr *segments;
  155. Elf32_Shdr *sections;
  156. u32 *sectionAddrs;
  157. bool relocate;
  158. u32 entryPoint;
  159. public:
  160. ElfReader(void *ptr);
  161. u32 Read32(int off) const { return base32[off >> 2]; }
  162. // Quick accessors
  163. ElfType GetType() const { return (ElfType)(header->e_type); }
  164. ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); }
  165. u32 GetEntryPoint() const { return entryPoint; }
  166. u32 GetFlags() const { return (u32)(header->e_flags); }
  167. void LoadInto(u32 vaddr);
  168. bool LoadSymbols();
  169. int GetNumSegments() const { return (int)(header->e_phnum); }
  170. int GetNumSections() const { return (int)(header->e_shnum); }
  171. const u8 *GetPtr(int offset) const { return (u8*)base + offset; }
  172. const char *GetSectionName(int section) const;
  173. const u8 *GetSectionDataPtr(int section) const {
  174. if (section < 0 || section >= header->e_shnum)
  175. return nullptr;
  176. if (sections[section].sh_type != SHT_NOBITS)
  177. return GetPtr(sections[section].sh_offset);
  178. else
  179. return nullptr;
  180. }
  181. bool IsCodeSection(int section) const {
  182. return sections[section].sh_type == SHT_PROGBITS;
  183. }
  184. const u8 *GetSegmentPtr(int segment) {
  185. return GetPtr(segments[segment].p_offset);
  186. }
  187. u32 GetSectionAddr(SectionID section) const { return sectionAddrs[section]; }
  188. unsigned int GetSectionSize(SectionID section) const { return sections[section].sh_size; }
  189. SectionID GetSectionByName(const char *name, int firstSection = 0) const; //-1 for not found
  190. bool DidRelocate() const {
  191. return relocate;
  192. }
  193. };
  194. ElfReader::ElfReader(void *ptr) {
  195. base = (char*)ptr;
  196. base32 = (u32*)ptr;
  197. header = (Elf32_Ehdr*)ptr;
  198. segments = (Elf32_Phdr*)(base + header->e_phoff);
  199. sections = (Elf32_Shdr*)(base + header->e_shoff);
  200. entryPoint = header->e_entry;
  201. LoadSymbols();
  202. }
  203. const char *ElfReader::GetSectionName(int section) const {
  204. if (sections[section].sh_type == SHT_NULL)
  205. return nullptr;
  206. int name_offset = sections[section].sh_name;
  207. const char* ptr = (char*)GetSectionDataPtr(header->e_shstrndx);
  208. if (ptr)
  209. return ptr + name_offset;
  210. return nullptr;
  211. }
  212. void ElfReader::LoadInto(u32 vaddr) {
  213. LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx);
  214. // Should we relocate?
  215. relocate = (header->e_type != ET_EXEC);
  216. if (relocate) {
  217. LOG_DEBUG(Loader, "Relocatable module");
  218. entryPoint += vaddr;
  219. } else {
  220. LOG_DEBUG(Loader, "Prerelocated executable");
  221. }
  222. LOG_DEBUG(Loader, "%i segments:", header->e_phnum);
  223. // First pass : Get the bits into RAM
  224. u32 segment_addr[32];
  225. u32 base_addr = relocate ? vaddr : 0;
  226. for (unsigned i = 0; i < header->e_phnum; i++) {
  227. Elf32_Phdr* p = segments + i;
  228. LOG_DEBUG(Loader, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr,
  229. p->p_filesz, p->p_memsz);
  230. if (p->p_type == PT_LOAD) {
  231. segment_addr[i] = base_addr + p->p_vaddr;
  232. memcpy(Memory::GetPointer(segment_addr[i]), GetSegmentPtr(i), p->p_filesz);
  233. LOG_DEBUG(Loader, "Loadable Segment Copied to %08x, size %08x", segment_addr[i],
  234. p->p_memsz);
  235. }
  236. }
  237. LOG_DEBUG(Loader, "Done loading.");
  238. }
  239. SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const {
  240. for (int i = firstSection; i < header->e_shnum; i++) {
  241. const char *secname = GetSectionName(i);
  242. if (secname != nullptr && strcmp(name, secname) == 0)
  243. return i;
  244. }
  245. return -1;
  246. }
  247. bool ElfReader::LoadSymbols() {
  248. bool hasSymbols = false;
  249. SectionID sec = GetSectionByName(".symtab");
  250. if (sec != -1) {
  251. int stringSection = sections[sec].sh_link;
  252. const char *stringBase = (const char *)GetSectionDataPtr(stringSection);
  253. //We have a symbol table!
  254. Elf32_Sym* symtab = (Elf32_Sym *)(GetSectionDataPtr(sec));
  255. unsigned int numSymbols = sections[sec].sh_size / sizeof(Elf32_Sym);
  256. for (unsigned sym = 0; sym < numSymbols; sym++) {
  257. int size = symtab[sym].st_size;
  258. if (size == 0)
  259. continue;
  260. int type = symtab[sym].st_info & 0xF;
  261. const char *name = stringBase + symtab[sym].st_name;
  262. Symbols::Add(symtab[sym].st_value, name, size, type);
  263. hasSymbols = true;
  264. }
  265. }
  266. return hasSymbols;
  267. }
  268. ////////////////////////////////////////////////////////////////////////////////////////////////////
  269. // Loader namespace
  270. namespace Loader {
  271. FileType AppLoader_ELF::IdentifyType(FileUtil::IOFile& file) {
  272. u32 magic;
  273. file.Seek(0, SEEK_SET);
  274. if (1 != file.ReadArray<u32>(&magic, 1))
  275. return FileType::Error;
  276. if (MakeMagic('\x7f', 'E', 'L', 'F') == magic)
  277. return FileType::ELF;
  278. return FileType::Error;
  279. }
  280. ResultStatus AppLoader_ELF::Load() {
  281. if (is_loaded)
  282. return ResultStatus::ErrorAlreadyLoaded;
  283. if (!file->IsOpen())
  284. return ResultStatus::Error;
  285. // Reset read pointer in case this file has been read before.
  286. file->Seek(0, SEEK_SET);
  287. u32 size = static_cast<u32>(file->GetSize());
  288. std::unique_ptr<u8[]> buffer(new u8[size]);
  289. if (file->ReadBytes(&buffer[0], size) != size)
  290. return ResultStatus::Error;
  291. ElfReader elf_reader(&buffer[0]);
  292. elf_reader.LoadInto(0x00100000);
  293. Kernel::LoadExec(elf_reader.GetEntryPoint());
  294. is_loaded = true;
  295. return ResultStatus::Success;
  296. }
  297. } // namespace Loader