diff options
Diffstat (limited to 'src/common/elf.h')
| -rw-r--r-- | src/common/elf.h | 333 |
1 files changed, 333 insertions, 0 deletions
diff --git a/src/common/elf.h b/src/common/elf.h new file mode 100644 index 000000000..14a5e9597 --- /dev/null +++ b/src/common/elf.h | |||
| @@ -0,0 +1,333 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2022 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | #include <array> | ||
| 7 | #include <cstddef> | ||
| 8 | |||
| 9 | #include "common_types.h" | ||
| 10 | |||
| 11 | namespace Common { | ||
| 12 | namespace ELF { | ||
| 13 | |||
| 14 | /* Type for a 16-bit quantity. */ | ||
| 15 | using Elf32_Half = u16; | ||
| 16 | using Elf64_Half = u16; | ||
| 17 | |||
| 18 | /* Types for signed and unsigned 32-bit quantities. */ | ||
| 19 | using Elf32_Word = u32; | ||
| 20 | using Elf32_Sword = s32; | ||
| 21 | using Elf64_Word = u32; | ||
| 22 | using Elf64_Sword = s32; | ||
| 23 | |||
| 24 | /* Types for signed and unsigned 64-bit quantities. */ | ||
| 25 | using Elf32_Xword = u64; | ||
| 26 | using Elf32_Sxword = s64; | ||
| 27 | using Elf64_Xword = u64; | ||
| 28 | using Elf64_Sxword = s64; | ||
| 29 | |||
| 30 | /* Type of addresses. */ | ||
| 31 | using Elf32_Addr = u32; | ||
| 32 | using Elf64_Addr = u64; | ||
| 33 | |||
| 34 | /* Type of file offsets. */ | ||
| 35 | using Elf32_Off = u32; | ||
| 36 | using Elf64_Off = u64; | ||
| 37 | |||
| 38 | /* Type for section indices, which are 16-bit quantities. */ | ||
| 39 | using Elf32_Section = u16; | ||
| 40 | using Elf64_Section = u16; | ||
| 41 | |||
| 42 | /* Type for version symbol information. */ | ||
| 43 | using Elf32_Versym = Elf32_Half; | ||
| 44 | using Elf64_Versym = Elf64_Half; | ||
| 45 | |||
| 46 | constexpr size_t ElfIdentSize = 16; | ||
| 47 | |||
| 48 | /* The ELF file header. This appears at the start of every ELF file. */ | ||
| 49 | |||
| 50 | struct Elf32_Ehdr { | ||
| 51 | std::array<u8, ElfIdentSize> e_ident; /* Magic number and other info */ | ||
| 52 | Elf32_Half e_type; /* Object file type */ | ||
| 53 | Elf32_Half e_machine; /* Architecture */ | ||
| 54 | Elf32_Word e_version; /* Object file version */ | ||
| 55 | Elf32_Addr e_entry; /* Entry point virtual address */ | ||
| 56 | Elf32_Off e_phoff; /* Program header table file offset */ | ||
| 57 | Elf32_Off e_shoff; /* Section header table file offset */ | ||
| 58 | Elf32_Word e_flags; /* Processor-specific flags */ | ||
| 59 | Elf32_Half e_ehsize; /* ELF header size in bytes */ | ||
| 60 | Elf32_Half e_phentsize; /* Program header table entry size */ | ||
| 61 | Elf32_Half e_phnum; /* Program header table entry count */ | ||
| 62 | Elf32_Half e_shentsize; /* Section header table entry size */ | ||
| 63 | Elf32_Half e_shnum; /* Section header table entry count */ | ||
| 64 | Elf32_Half e_shstrndx; /* Section header string table index */ | ||
| 65 | }; | ||
| 66 | |||
| 67 | struct Elf64_Ehdr { | ||
| 68 | std::array<u8, ElfIdentSize> e_ident; /* Magic number and other info */ | ||
| 69 | Elf64_Half e_type; /* Object file type */ | ||
| 70 | Elf64_Half e_machine; /* Architecture */ | ||
| 71 | Elf64_Word e_version; /* Object file version */ | ||
| 72 | Elf64_Addr e_entry; /* Entry point virtual address */ | ||
| 73 | Elf64_Off e_phoff; /* Program header table file offset */ | ||
| 74 | Elf64_Off e_shoff; /* Section header table file offset */ | ||
| 75 | Elf64_Word e_flags; /* Processor-specific flags */ | ||
| 76 | Elf64_Half e_ehsize; /* ELF header size in bytes */ | ||
| 77 | Elf64_Half e_phentsize; /* Program header table entry size */ | ||
| 78 | Elf64_Half e_phnum; /* Program header table entry count */ | ||
| 79 | Elf64_Half e_shentsize; /* Section header table entry size */ | ||
| 80 | Elf64_Half e_shnum; /* Section header table entry count */ | ||
| 81 | Elf64_Half e_shstrndx; /* Section header string table index */ | ||
| 82 | }; | ||
| 83 | |||
| 84 | constexpr u8 ElfClass32 = 1; /* 32-bit objects */ | ||
| 85 | constexpr u8 ElfClass64 = 2; /* 64-bit objects */ | ||
| 86 | constexpr u8 ElfData2Lsb = 1; /* 2's complement, little endian */ | ||
| 87 | constexpr u8 ElfVersionCurrent = 1; /* EV_CURRENT */ | ||
| 88 | constexpr u8 ElfOsAbiNone = 0; /* System V ABI */ | ||
| 89 | |||
| 90 | constexpr u16 ElfTypeNone = 0; /* No file type */ | ||
| 91 | constexpr u16 ElfTypeRel = 0; /* Relocatable file */ | ||
| 92 | constexpr u16 ElfTypeExec = 0; /* Executable file */ | ||
| 93 | constexpr u16 ElfTypeDyn = 0; /* Shared object file */ | ||
| 94 | |||
| 95 | constexpr u16 ElfMachineArm = 40; /* ARM */ | ||
| 96 | constexpr u16 ElfMachineAArch64 = 183; /* ARM AARCH64 */ | ||
| 97 | |||
| 98 | constexpr std::array<u8, ElfIdentSize> Elf32Ident{ | ||
| 99 | 0x7f, 'E', 'L', 'F', ElfClass32, ElfData2Lsb, ElfVersionCurrent, ElfOsAbiNone}; | ||
| 100 | |||
| 101 | constexpr std::array<u8, ElfIdentSize> Elf64Ident{ | ||
| 102 | 0x7f, 'E', 'L', 'F', ElfClass64, ElfData2Lsb, ElfVersionCurrent, ElfOsAbiNone}; | ||
| 103 | |||
| 104 | /* Section header. */ | ||
| 105 | |||
| 106 | struct Elf32_Shdr { | ||
| 107 | Elf32_Word sh_name; /* Section name (string tbl index) */ | ||
| 108 | Elf32_Word sh_type; /* Section type */ | ||
| 109 | Elf32_Word sh_flags; /* Section flags */ | ||
| 110 | Elf32_Addr sh_addr; /* Section virtual addr at execution */ | ||
| 111 | Elf32_Off sh_offset; /* Section file offset */ | ||
| 112 | Elf32_Word sh_size; /* Section size in bytes */ | ||
| 113 | Elf32_Word sh_link; /* Link to another section */ | ||
| 114 | Elf32_Word sh_info; /* Additional section information */ | ||
| 115 | Elf32_Word sh_addralign; /* Section alignment */ | ||
| 116 | Elf32_Word sh_entsize; /* Entry size if section holds table */ | ||
| 117 | }; | ||
| 118 | |||
| 119 | struct Elf64_Shdr { | ||
| 120 | Elf64_Word sh_name; /* Section name (string tbl index) */ | ||
| 121 | Elf64_Word sh_type; /* Section type */ | ||
| 122 | Elf64_Xword sh_flags; /* Section flags */ | ||
| 123 | Elf64_Addr sh_addr; /* Section virtual addr at execution */ | ||
| 124 | Elf64_Off sh_offset; /* Section file offset */ | ||
| 125 | Elf64_Xword sh_size; /* Section size in bytes */ | ||
| 126 | Elf64_Word sh_link; /* Link to another section */ | ||
| 127 | Elf64_Word sh_info; /* Additional section information */ | ||
| 128 | Elf64_Xword sh_addralign; /* Section alignment */ | ||
| 129 | Elf64_Xword sh_entsize; /* Entry size if section holds table */ | ||
| 130 | }; | ||
| 131 | |||
| 132 | constexpr u32 ElfShnUndef = 0; /* Undefined section */ | ||
| 133 | |||
| 134 | constexpr u32 ElfShtNull = 0; /* Section header table entry unused */ | ||
| 135 | constexpr u32 ElfShtProgBits = 1; /* Program data */ | ||
| 136 | constexpr u32 ElfShtSymtab = 2; /* Symbol table */ | ||
| 137 | constexpr u32 ElfShtStrtab = 3; /* String table */ | ||
| 138 | constexpr u32 ElfShtRela = 4; /* Relocation entries with addends */ | ||
| 139 | constexpr u32 ElfShtDynamic = 6; /* Dynamic linking information */ | ||
| 140 | constexpr u32 ElfShtNobits = 7; /* Program space with no data (bss) */ | ||
| 141 | constexpr u32 ElfShtRel = 9; /* Relocation entries, no addends */ | ||
| 142 | constexpr u32 ElfShtDynsym = 11; /* Dynamic linker symbol table */ | ||
| 143 | |||
| 144 | /* Symbol table entry. */ | ||
| 145 | |||
| 146 | struct Elf32_Sym { | ||
| 147 | Elf32_Word st_name; /* Symbol name (string tbl index) */ | ||
| 148 | Elf32_Addr st_value; /* Symbol value */ | ||
| 149 | Elf32_Word st_size; /* Symbol size */ | ||
| 150 | u8 st_info; /* Symbol type and binding */ | ||
| 151 | u8 st_other; /* Symbol visibility */ | ||
| 152 | Elf32_Section st_shndx; /* Section index */ | ||
| 153 | }; | ||
| 154 | |||
| 155 | struct Elf64_Sym { | ||
| 156 | Elf64_Word st_name; /* Symbol name (string tbl index) */ | ||
| 157 | u8 st_info; /* Symbol type and binding */ | ||
| 158 | u8 st_other; /* Symbol visibility */ | ||
| 159 | Elf64_Section st_shndx; /* Section index */ | ||
| 160 | Elf64_Addr st_value; /* Symbol value */ | ||
| 161 | Elf64_Xword st_size; /* Symbol size */ | ||
| 162 | }; | ||
| 163 | |||
| 164 | /* How to extract and insert information held in the st_info field. */ | ||
| 165 | |||
| 166 | static inline u8 ElfStBind(u8 st_info) { | ||
| 167 | return st_info >> 4; | ||
| 168 | } | ||
| 169 | static inline u8 ElfStType(u8 st_info) { | ||
| 170 | return st_info & 0xf; | ||
| 171 | } | ||
| 172 | static inline u8 ElfStInfo(u8 st_bind, u8 st_type) { | ||
| 173 | return static_cast<u8>((st_bind << 4) + (st_type & 0xf)); | ||
| 174 | } | ||
| 175 | |||
| 176 | constexpr u8 ElfBindLocal = 0; /* Local symbol */ | ||
| 177 | constexpr u8 ElfBindGlobal = 1; /* Global symbol */ | ||
| 178 | constexpr u8 ElfBindWeak = 2; /* Weak symbol */ | ||
| 179 | |||
| 180 | constexpr u8 ElfTypeUnspec = 0; /* Symbol type is unspecified */ | ||
| 181 | constexpr u8 ElfTypeObject = 1; /* Symbol is a data object */ | ||
| 182 | constexpr u8 ElfTypeFunc = 2; /* Symbol is a code object */ | ||
| 183 | |||
| 184 | static inline u8 ElfStVisibility(u8 st_other) { | ||
| 185 | return static_cast<u8>(st_other & 0x3); | ||
| 186 | } | ||
| 187 | |||
| 188 | constexpr u8 ElfVisibilityDefault = 0; /* Default symbol visibility rules */ | ||
| 189 | constexpr u8 ElfVisibilityInternal = 1; /* Processor specific hidden class */ | ||
| 190 | constexpr u8 ElfVisibilityHidden = 2; /* Sym unavailable in other modules */ | ||
| 191 | constexpr u8 ElfVisibilityProtected = 3; /* Not preemptible, not exported */ | ||
| 192 | |||
| 193 | /* Relocation table entry without addend (in section of type ShtRel). */ | ||
| 194 | |||
| 195 | struct Elf32_Rel { | ||
| 196 | Elf32_Addr r_offset; /* Address */ | ||
| 197 | Elf32_Word r_info; /* Relocation type and symbol index */ | ||
| 198 | }; | ||
| 199 | |||
| 200 | /* Relocation table entry with addend (in section of type ShtRela). */ | ||
| 201 | |||
| 202 | struct Elf32_Rela { | ||
| 203 | Elf32_Addr r_offset; /* Address */ | ||
| 204 | Elf32_Word r_info; /* Relocation type and symbol index */ | ||
| 205 | Elf32_Sword r_addend; /* Addend */ | ||
| 206 | }; | ||
| 207 | |||
| 208 | struct Elf64_Rela { | ||
| 209 | Elf64_Addr r_offset; /* Address */ | ||
| 210 | Elf64_Xword r_info; /* Relocation type and symbol index */ | ||
| 211 | Elf64_Sxword r_addend; /* Addend */ | ||
| 212 | }; | ||
| 213 | |||
| 214 | /* How to extract and insert information held in the r_info field. */ | ||
| 215 | |||
| 216 | static inline u32 Elf32RelSymIndex(Elf32_Word r_info) { | ||
| 217 | return r_info >> 8; | ||
| 218 | } | ||
| 219 | static inline u8 Elf32RelType(Elf32_Word r_info) { | ||
| 220 | return static_cast<u8>(r_info & 0xff); | ||
| 221 | } | ||
| 222 | static inline Elf32_Word Elf32RelInfo(u32 sym_index, u8 type) { | ||
| 223 | return (sym_index << 8) + type; | ||
| 224 | } | ||
| 225 | static inline u32 Elf64RelSymIndex(Elf64_Xword r_info) { | ||
| 226 | return static_cast<u32>(r_info >> 32); | ||
| 227 | } | ||
| 228 | static inline u32 Elf64RelType(Elf64_Xword r_info) { | ||
| 229 | return r_info & 0xffffffff; | ||
| 230 | } | ||
| 231 | static inline Elf64_Xword Elf64RelInfo(u32 sym_index, u32 type) { | ||
| 232 | return (static_cast<Elf64_Xword>(sym_index) << 32) + type; | ||
| 233 | } | ||
| 234 | |||
| 235 | constexpr u32 ElfArmCopy = 20; /* Copy symbol at runtime */ | ||
| 236 | constexpr u32 ElfArmGlobDat = 21; /* Create GOT entry */ | ||
| 237 | constexpr u32 ElfArmJumpSlot = 22; /* Create PLT entry */ | ||
| 238 | constexpr u32 ElfArmRelative = 23; /* Adjust by program base */ | ||
| 239 | |||
| 240 | constexpr u32 ElfAArch64Copy = 1024; /* Copy symbol at runtime */ | ||
| 241 | constexpr u32 ElfAArch64GlobDat = 1025; /* Create GOT entry */ | ||
| 242 | constexpr u32 ElfAArch64JumpSlot = 1026; /* Create PLT entry */ | ||
| 243 | constexpr u32 ElfAArch64Relative = 1027; /* Adjust by program base */ | ||
| 244 | |||
| 245 | /* Program segment header. */ | ||
| 246 | |||
| 247 | struct Elf32_Phdr { | ||
| 248 | Elf32_Word p_type; /* Segment type */ | ||
| 249 | Elf32_Off p_offset; /* Segment file offset */ | ||
| 250 | Elf32_Addr p_vaddr; /* Segment virtual address */ | ||
| 251 | Elf32_Addr p_paddr; /* Segment physical address */ | ||
| 252 | Elf32_Word p_filesz; /* Segment size in file */ | ||
| 253 | Elf32_Word p_memsz; /* Segment size in memory */ | ||
| 254 | Elf32_Word p_flags; /* Segment flags */ | ||
| 255 | Elf32_Word p_align; /* Segment alignment */ | ||
| 256 | }; | ||
| 257 | |||
| 258 | struct Elf64_Phdr { | ||
| 259 | Elf64_Word p_type; /* Segment type */ | ||
| 260 | Elf64_Word p_flags; /* Segment flags */ | ||
| 261 | Elf64_Off p_offset; /* Segment file offset */ | ||
| 262 | Elf64_Addr p_vaddr; /* Segment virtual address */ | ||
| 263 | Elf64_Addr p_paddr; /* Segment physical address */ | ||
| 264 | Elf64_Xword p_filesz; /* Segment size in file */ | ||
| 265 | Elf64_Xword p_memsz; /* Segment size in memory */ | ||
| 266 | Elf64_Xword p_align; /* Segment alignment */ | ||
| 267 | }; | ||
| 268 | |||
| 269 | /* Legal values for p_type (segment type). */ | ||
| 270 | |||
| 271 | constexpr u32 ElfPtNull = 0; /* Program header table entry unused */ | ||
| 272 | constexpr u32 ElfPtLoad = 1; /* Loadable program segment */ | ||
| 273 | constexpr u32 ElfPtDynamic = 2; /* Dynamic linking information */ | ||
| 274 | constexpr u32 ElfPtInterp = 3; /* Program interpreter */ | ||
| 275 | constexpr u32 ElfPtNote = 4; /* Auxiliary information */ | ||
| 276 | constexpr u32 ElfPtPhdr = 6; /* Entry for header table itself */ | ||
| 277 | constexpr u32 ElfPtTls = 7; /* Thread-local storage segment */ | ||
| 278 | |||
| 279 | /* Legal values for p_flags (segment flags). */ | ||
| 280 | |||
| 281 | constexpr u32 ElfPfExec = 0; /* Segment is executable */ | ||
| 282 | constexpr u32 ElfPfWrite = 1; /* Segment is writable */ | ||
| 283 | constexpr u32 ElfPfRead = 2; /* Segment is readable */ | ||
| 284 | |||
| 285 | /* Dynamic section entry. */ | ||
| 286 | |||
| 287 | struct Elf32_Dyn { | ||
| 288 | Elf32_Sword d_tag; /* Dynamic entry type */ | ||
| 289 | union { | ||
| 290 | Elf32_Word d_val; /* Integer value */ | ||
| 291 | Elf32_Addr d_ptr; /* Address value */ | ||
| 292 | } d_un; | ||
| 293 | }; | ||
| 294 | |||
| 295 | struct Elf64_Dyn { | ||
| 296 | Elf64_Sxword d_tag; /* Dynamic entry type */ | ||
| 297 | union { | ||
| 298 | Elf64_Xword d_val; /* Integer value */ | ||
| 299 | Elf64_Addr d_ptr; /* Address value */ | ||
| 300 | } d_un; | ||
| 301 | }; | ||
| 302 | |||
| 303 | /* Legal values for d_tag (dynamic entry type). */ | ||
| 304 | |||
| 305 | constexpr u32 ElfDtNull = 0; /* Marks end of dynamic section */ | ||
| 306 | constexpr u32 ElfDtNeeded = 1; /* Name of needed library */ | ||
| 307 | constexpr u32 ElfDtPltRelSz = 2; /* Size in bytes of PLT relocs */ | ||
| 308 | constexpr u32 ElfDtPltGot = 3; /* Processor defined value */ | ||
| 309 | constexpr u32 ElfDtHash = 4; /* Address of symbol hash table */ | ||
| 310 | constexpr u32 ElfDtStrtab = 5; /* Address of string table */ | ||
| 311 | constexpr u32 ElfDtSymtab = 6; /* Address of symbol table */ | ||
| 312 | constexpr u32 ElfDtRela = 7; /* Address of Rela relocs */ | ||
| 313 | constexpr u32 ElfDtRelasz = 8; /* Total size of Rela relocs */ | ||
| 314 | constexpr u32 ElfDtRelaent = 9; /* Size of one Rela reloc */ | ||
| 315 | constexpr u32 ElfDtStrsz = 10; /* Size of string table */ | ||
| 316 | constexpr u32 ElfDtSyment = 11; /* Size of one symbol table entry */ | ||
| 317 | constexpr u32 ElfDtInit = 12; /* Address of init function */ | ||
| 318 | constexpr u32 ElfDtFini = 13; /* Address of termination function */ | ||
| 319 | constexpr u32 ElfDtRel = 17; /* Address of Rel relocs */ | ||
| 320 | constexpr u32 ElfDtRelsz = 18; /* Total size of Rel relocs */ | ||
| 321 | constexpr u32 ElfDtRelent = 19; /* Size of one Rel reloc */ | ||
| 322 | constexpr u32 ElfDtPltRel = 20; /* Type of reloc in PLT */ | ||
| 323 | constexpr u32 ElfDtTextRel = 22; /* Reloc might modify .text */ | ||
| 324 | constexpr u32 ElfDtJmpRel = 23; /* Address of PLT relocs */ | ||
| 325 | constexpr u32 ElfDtBindNow = 24; /* Process relocations of object */ | ||
| 326 | constexpr u32 ElfDtInitArray = 25; /* Array with addresses of init fct */ | ||
| 327 | constexpr u32 ElfDtFiniArray = 26; /* Array with addresses of fini fct */ | ||
| 328 | constexpr u32 ElfDtInitArraySz = 27; /* Size in bytes of DT_INIT_ARRAY */ | ||
| 329 | constexpr u32 ElfDtFiniArraySz = 28; /* Size in bytes of DT_FINI_ARRAY */ | ||
| 330 | constexpr u32 ElfDtSymtabShndx = 34; /* Address of SYMTAB_SHNDX section */ | ||
| 331 | |||
| 332 | } // namespace ELF | ||
| 333 | } // namespace Common | ||