diff options
Diffstat (limited to 'src/core/loader/elf.cpp')
| -rw-r--r-- | src/core/loader/elf.cpp | 183 |
1 files changed, 16 insertions, 167 deletions
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index cf5933699..dfb10c34f 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include <memory> | 6 | #include <memory> |
| 7 | #include "common/common_funcs.h" | 7 | #include "common/common_funcs.h" |
| 8 | #include "common/common_types.h" | 8 | #include "common/common_types.h" |
| 9 | #include "common/elf.h" | ||
| 9 | #include "common/logging/log.h" | 10 | #include "common/logging/log.h" |
| 10 | #include "core/hle/kernel/code_set.h" | 11 | #include "core/hle/kernel/code_set.h" |
| 11 | #include "core/hle/kernel/k_page_table.h" | 12 | #include "core/hle/kernel/k_page_table.h" |
| @@ -13,159 +14,7 @@ | |||
| 13 | #include "core/loader/elf.h" | 14 | #include "core/loader/elf.h" |
| 14 | #include "core/memory.h" | 15 | #include "core/memory.h" |
| 15 | 16 | ||
| 16 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 17 | using namespace Common::ELF; |
| 17 | // ELF Header Constants | ||
| 18 | |||
| 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 | |||
| 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 | |||
| 42 | // File version | ||
| 43 | #define EV_NONE 0 | ||
| 44 | #define EV_CURRENT 1 | ||
| 45 | |||
| 46 | // Identification index | ||
| 47 | #define EI_MAG0 0 | ||
| 48 | #define EI_MAG1 1 | ||
| 49 | #define EI_MAG2 2 | ||
| 50 | #define EI_MAG3 3 | ||
| 51 | #define EI_CLASS 4 | ||
| 52 | #define EI_DATA 5 | ||
| 53 | #define EI_VERSION 6 | ||
| 54 | #define EI_PAD 7 | ||
| 55 | #define EI_NIDENT 16 | ||
| 56 | |||
| 57 | // Sections constants | ||
| 58 | |||
| 59 | // Section types | ||
| 60 | #define SHT_NULL 0 | ||
| 61 | #define SHT_PROGBITS 1 | ||
| 62 | #define SHT_SYMTAB 2 | ||
| 63 | #define SHT_STRTAB 3 | ||
| 64 | #define SHT_RELA 4 | ||
| 65 | #define SHT_HASH 5 | ||
| 66 | #define SHT_DYNAMIC 6 | ||
| 67 | #define SHT_NOTE 7 | ||
| 68 | #define SHT_NOBITS 8 | ||
| 69 | #define SHT_REL 9 | ||
| 70 | #define SHT_SHLIB 10 | ||
| 71 | #define SHT_DYNSYM 11 | ||
| 72 | #define SHT_LOPROC 0x70000000 | ||
| 73 | #define SHT_HIPROC 0x7FFFFFFF | ||
| 74 | #define SHT_LOUSER 0x80000000 | ||
| 75 | #define SHT_HIUSER 0xFFFFFFFF | ||
| 76 | |||
| 77 | // Section flags | ||
| 78 | enum ElfSectionFlags { | ||
| 79 | SHF_WRITE = 0x1, | ||
| 80 | SHF_ALLOC = 0x2, | ||
| 81 | SHF_EXECINSTR = 0x4, | ||
| 82 | SHF_MASKPROC = 0xF0000000, | ||
| 83 | }; | ||
| 84 | |||
| 85 | // Segment types | ||
| 86 | #define PT_NULL 0 | ||
| 87 | #define PT_LOAD 1 | ||
| 88 | #define PT_DYNAMIC 2 | ||
| 89 | #define PT_INTERP 3 | ||
| 90 | #define PT_NOTE 4 | ||
| 91 | #define PT_SHLIB 5 | ||
| 92 | #define PT_PHDR 6 | ||
| 93 | #define PT_LOPROC 0x70000000 | ||
| 94 | #define PT_HIPROC 0x7FFFFFFF | ||
| 95 | |||
| 96 | // Segment flags | ||
| 97 | #define PF_X 0x1 | ||
| 98 | #define PF_W 0x2 | ||
| 99 | #define PF_R 0x4 | ||
| 100 | #define PF_MASKPROC 0xF0000000 | ||
| 101 | |||
| 102 | typedef unsigned int Elf32_Addr; | ||
| 103 | typedef unsigned short Elf32_Half; | ||
| 104 | typedef unsigned int Elf32_Off; | ||
| 105 | typedef signed int Elf32_Sword; | ||
| 106 | typedef unsigned int Elf32_Word; | ||
| 107 | |||
| 108 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 109 | // ELF file header | ||
| 110 | |||
| 111 | struct Elf32_Ehdr { | ||
| 112 | unsigned char e_ident[EI_NIDENT]; | ||
| 113 | Elf32_Half e_type; | ||
| 114 | Elf32_Half e_machine; | ||
| 115 | Elf32_Word e_version; | ||
| 116 | Elf32_Addr e_entry; | ||
| 117 | Elf32_Off e_phoff; | ||
| 118 | Elf32_Off e_shoff; | ||
| 119 | Elf32_Word e_flags; | ||
| 120 | Elf32_Half e_ehsize; | ||
| 121 | Elf32_Half e_phentsize; | ||
| 122 | Elf32_Half e_phnum; | ||
| 123 | Elf32_Half e_shentsize; | ||
| 124 | Elf32_Half e_shnum; | ||
| 125 | Elf32_Half e_shstrndx; | ||
| 126 | }; | ||
| 127 | |||
| 128 | // Section header | ||
| 129 | struct Elf32_Shdr { | ||
| 130 | Elf32_Word sh_name; | ||
| 131 | Elf32_Word sh_type; | ||
| 132 | Elf32_Word sh_flags; | ||
| 133 | Elf32_Addr sh_addr; | ||
| 134 | Elf32_Off sh_offset; | ||
| 135 | Elf32_Word sh_size; | ||
| 136 | Elf32_Word sh_link; | ||
| 137 | Elf32_Word sh_info; | ||
| 138 | Elf32_Word sh_addralign; | ||
| 139 | Elf32_Word sh_entsize; | ||
| 140 | }; | ||
| 141 | |||
| 142 | // Segment header | ||
| 143 | struct Elf32_Phdr { | ||
| 144 | Elf32_Word p_type; | ||
| 145 | Elf32_Off p_offset; | ||
| 146 | Elf32_Addr p_vaddr; | ||
| 147 | Elf32_Addr p_paddr; | ||
| 148 | Elf32_Word p_filesz; | ||
| 149 | Elf32_Word p_memsz; | ||
| 150 | Elf32_Word p_flags; | ||
| 151 | Elf32_Word p_align; | ||
| 152 | }; | ||
| 153 | |||
| 154 | // Symbol table entry | ||
| 155 | struct Elf32_Sym { | ||
| 156 | Elf32_Word st_name; | ||
| 157 | Elf32_Addr st_value; | ||
| 158 | Elf32_Word st_size; | ||
| 159 | unsigned char st_info; | ||
| 160 | unsigned char st_other; | ||
| 161 | Elf32_Half st_shndx; | ||
| 162 | }; | ||
| 163 | |||
| 164 | // Relocation entries | ||
| 165 | struct Elf32_Rel { | ||
| 166 | Elf32_Addr r_offset; | ||
| 167 | Elf32_Word r_info; | ||
| 168 | }; | ||
| 169 | 18 | ||
| 170 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 19 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 171 | // ElfReader class | 20 | // ElfReader class |
| @@ -193,11 +42,11 @@ public: | |||
| 193 | } | 42 | } |
| 194 | 43 | ||
| 195 | // Quick accessors | 44 | // Quick accessors |
| 196 | ElfType GetType() const { | 45 | u16 GetType() const { |
| 197 | return (ElfType)(header->e_type); | 46 | return header->e_type; |
| 198 | } | 47 | } |
| 199 | ElfMachine GetMachine() const { | 48 | u16 GetMachine() const { |
| 200 | return (ElfMachine)(header->e_machine); | 49 | return header->e_machine; |
| 201 | } | 50 | } |
| 202 | VAddr GetEntryPoint() const { | 51 | VAddr GetEntryPoint() const { |
| 203 | return entryPoint; | 52 | return entryPoint; |
| @@ -220,13 +69,13 @@ public: | |||
| 220 | const u8* GetSectionDataPtr(int section) const { | 69 | const u8* GetSectionDataPtr(int section) const { |
| 221 | if (section < 0 || section >= header->e_shnum) | 70 | if (section < 0 || section >= header->e_shnum) |
| 222 | return nullptr; | 71 | return nullptr; |
| 223 | if (sections[section].sh_type != SHT_NOBITS) | 72 | if (sections[section].sh_type != ElfShtNobits) |
| 224 | return GetPtr(sections[section].sh_offset); | 73 | return GetPtr(sections[section].sh_offset); |
| 225 | else | 74 | else |
| 226 | return nullptr; | 75 | return nullptr; |
| 227 | } | 76 | } |
| 228 | bool IsCodeSection(int section) const { | 77 | bool IsCodeSection(int section) const { |
| 229 | return sections[section].sh_type == SHT_PROGBITS; | 78 | return sections[section].sh_type == ElfShtProgBits; |
| 230 | } | 79 | } |
| 231 | const u8* GetSegmentPtr(int segment) { | 80 | const u8* GetSegmentPtr(int segment) { |
| 232 | return GetPtr(segments[segment].p_offset); | 81 | return GetPtr(segments[segment].p_offset); |
| @@ -256,7 +105,7 @@ ElfReader::ElfReader(void* ptr) { | |||
| 256 | } | 105 | } |
| 257 | 106 | ||
| 258 | const char* ElfReader::GetSectionName(int section) const { | 107 | const char* ElfReader::GetSectionName(int section) const { |
| 259 | if (sections[section].sh_type == SHT_NULL) | 108 | if (sections[section].sh_type == ElfShtNull) |
| 260 | return nullptr; | 109 | return nullptr; |
| 261 | 110 | ||
| 262 | int name_offset = sections[section].sh_name; | 111 | int name_offset = sections[section].sh_name; |
| @@ -272,7 +121,7 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { | |||
| 272 | LOG_DEBUG(Loader, "String section: {}", header->e_shstrndx); | 121 | LOG_DEBUG(Loader, "String section: {}", header->e_shstrndx); |
| 273 | 122 | ||
| 274 | // Should we relocate? | 123 | // Should we relocate? |
| 275 | relocate = (header->e_type != ET_EXEC); | 124 | relocate = (header->e_type != ElfTypeExec); |
| 276 | 125 | ||
| 277 | if (relocate) { | 126 | if (relocate) { |
| 278 | LOG_DEBUG(Loader, "Relocatable module"); | 127 | LOG_DEBUG(Loader, "Relocatable module"); |
| @@ -288,7 +137,7 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { | |||
| 288 | u64 total_image_size = 0; | 137 | u64 total_image_size = 0; |
| 289 | for (unsigned int i = 0; i < header->e_phnum; ++i) { | 138 | for (unsigned int i = 0; i < header->e_phnum; ++i) { |
| 290 | const Elf32_Phdr* p = &segments[i]; | 139 | const Elf32_Phdr* p = &segments[i]; |
| 291 | if (p->p_type == PT_LOAD) { | 140 | if (p->p_type == ElfPtLoad) { |
| 292 | total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF; | 141 | total_image_size += (p->p_memsz + 0xFFF) & ~0xFFF; |
| 293 | } | 142 | } |
| 294 | } | 143 | } |
| @@ -303,14 +152,14 @@ Kernel::CodeSet ElfReader::LoadInto(VAddr vaddr) { | |||
| 303 | LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type, | 152 | LOG_DEBUG(Loader, "Type: {} Vaddr: {:08X} Filesz: {:08X} Memsz: {:08X} ", p->p_type, |
| 304 | p->p_vaddr, p->p_filesz, p->p_memsz); | 153 | p->p_vaddr, p->p_filesz, p->p_memsz); |
| 305 | 154 | ||
| 306 | if (p->p_type == PT_LOAD) { | 155 | if (p->p_type == ElfPtLoad) { |
| 307 | Kernel::CodeSet::Segment* codeset_segment; | 156 | Kernel::CodeSet::Segment* codeset_segment; |
| 308 | u32 permission_flags = p->p_flags & (PF_R | PF_W | PF_X); | 157 | u32 permission_flags = p->p_flags & (ElfPfRead | ElfPfWrite | ElfPfExec); |
| 309 | if (permission_flags == (PF_R | PF_X)) { | 158 | if (permission_flags == (ElfPfRead | ElfPfExec)) { |
| 310 | codeset_segment = &codeset.CodeSegment(); | 159 | codeset_segment = &codeset.CodeSegment(); |
| 311 | } else if (permission_flags == (PF_R)) { | 160 | } else if (permission_flags == (ElfPfRead)) { |
| 312 | codeset_segment = &codeset.RODataSegment(); | 161 | codeset_segment = &codeset.RODataSegment(); |
| 313 | } else if (permission_flags == (PF_R | PF_W)) { | 162 | } else if (permission_flags == (ElfPfRead | ElfPfWrite)) { |
| 314 | codeset_segment = &codeset.DataSegment(); | 163 | codeset_segment = &codeset.DataSegment(); |
| 315 | } else { | 164 | } else { |
| 316 | LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id {} with flags {:X}", i, | 165 | LOG_ERROR(Loader, "Unexpected ELF PT_LOAD segment id {} with flags {:X}", i, |