Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- elf.h ---------------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef __elf_h__ |
| 11 | #define __elf_h__ |
| 12 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 13 | //---------------------------------------------------------------------- |
| 14 | // Typedefs for ELF32. |
| 15 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | typedef uint16_t Elf32_Half; |
| 17 | typedef uint32_t Elf32_Word; |
| 18 | typedef int32_t Elf32_Sword; |
| 19 | typedef uint32_t Elf32_Addr; |
| 20 | typedef uint32_t Elf32_Off; |
| 21 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 22 | //---------------------------------------------------------------------- |
| 23 | // Typedefs for ELF64. |
| 24 | //---------------------------------------------------------------------- |
| 25 | typedef uint16_t Elf64_Half; |
| 26 | typedef uint32_t Elf64_Word; |
| 27 | typedef int32_t Elf64_Sword; |
| 28 | typedef uint64_t Elf64_Xword; |
| 29 | typedef int64_t Elf64_Sxword; |
| 30 | typedef uint64_t Elf64_Addr; |
| 31 | typedef uint64_t Elf64_Off; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
| 33 | #define EI_NIDENT 16 |
| 34 | |
| 35 | //---------------------------------------------------------------------- |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 36 | // ELF Headers |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | //---------------------------------------------------------------------- |
| 38 | typedef struct Elf32_Ehdr_Tag |
| 39 | { |
| 40 | unsigned char e_ident[EI_NIDENT]; |
| 41 | Elf32_Half e_type; |
| 42 | Elf32_Half e_machine; |
| 43 | Elf32_Word e_version; |
| 44 | Elf32_Addr e_entry; |
| 45 | Elf32_Off e_phoff; |
| 46 | Elf32_Off e_shoff; |
| 47 | Elf32_Word e_flags; |
| 48 | Elf32_Half e_ehsize; |
| 49 | Elf32_Half e_phentsize; |
| 50 | Elf32_Half e_phnum; |
| 51 | Elf32_Half e_shentsize; |
| 52 | Elf32_Half e_shnum; |
| 53 | Elf32_Half e_shstrndx; |
| 54 | } Elf32_Ehdr; |
| 55 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 56 | typedef struct Elf64_Ehdr_Tag |
| 57 | { |
| 58 | unsigned char e_ident[EI_NIDENT]; |
| 59 | Elf64_Half e_type; |
| 60 | Elf64_Half e_machine; |
| 61 | Elf64_Word e_version; |
| 62 | Elf64_Addr e_entry; |
| 63 | Elf64_Off e_phoff; |
| 64 | Elf64_Off e_shoff; |
| 65 | Elf64_Word e_flags; |
| 66 | Elf64_Half e_ehsize; |
| 67 | Elf64_Half e_phentsize; |
| 68 | Elf64_Half e_phnum; |
| 69 | Elf64_Half e_shentsize; |
| 70 | Elf64_Half e_shnum; |
| 71 | Elf64_Half e_shstrndx; |
| 72 | } Elf64_Ehdr; |
| 73 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | //---------------------------------------------------------------------- |
| 75 | // e_type |
| 76 | // |
| 77 | // This member identifies the object file type. |
| 78 | //---------------------------------------------------------------------- |
| 79 | #define ET_NONE 0 // No file type |
| 80 | #define ET_REL 1 // Relocatable file |
| 81 | #define ET_EXEC 2 // Executable file |
| 82 | #define ET_DYN 3 // Shared object file |
| 83 | #define ET_CORE 4 // Core file |
| 84 | #define ET_LOPROC 0xff00 // Processor-specific |
| 85 | #define ET_HIPROC 0xffff // Processor-specific |
| 86 | |
| 87 | //---------------------------------------------------------------------- |
| 88 | // e_machine |
| 89 | // |
| 90 | // Machine Type |
| 91 | //---------------------------------------------------------------------- |
| 92 | #define EM_NONE 0 // No machine |
| 93 | #define EM_M32 1 // AT&T WE 32100 |
| 94 | #define EM_SPARC 2 // SPARC |
| 95 | #define EM_386 3 // Intel 80386 |
| 96 | #define EM_68K 4 // Motorola 68000 |
| 97 | #define EM_88K 5 // Motorola 88000 |
| 98 | #define EM_860 7 // Intel 80860 |
| 99 | #define EM_MIPS 8 // MIPS RS3000 |
| 100 | #define EM_PPC 20 // PowerPC |
| 101 | #define EM_PPC64 21 // PowerPC64 |
| 102 | #define EM_ARM 40 // ARM |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 103 | #define EM_X86_64 62 // AMD x86-64 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | |
| 105 | |
| 106 | //---------------------------------------------------------------------- |
| 107 | // e_ident indexes |
| 108 | //---------------------------------------------------------------------- |
| 109 | #define EI_MAG0 0 // File identification |
| 110 | #define EI_MAG1 1 // File identification |
| 111 | #define EI_MAG2 2 // File identification |
| 112 | #define EI_MAG3 3 // File identification |
| 113 | #define EI_CLASS 4 // File class |
| 114 | #define EI_DATA 5 // Data encoding |
| 115 | #define EI_VERSION 6 // File version |
| 116 | #define EI_PAD 7 // Start of padding bytes |
| 117 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 118 | //---------------------------------------------------------------------- |
| 119 | // EI_CLASS definitions |
| 120 | //---------------------------------------------------------------------- |
| 121 | #define ELFCLASS32 1 // 32-bit object file |
| 122 | #define ELFCLASS64 2 // 64-bit object file |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | |
| 124 | //---------------------------------------------------------------------- |
| 125 | // EI_DATA definitions |
| 126 | //---------------------------------------------------------------------- |
| 127 | #define ELFDATANONE 0 // Invalid data encoding |
| 128 | #define ELFDATA2LSB 1 // Little Endian |
| 129 | #define ELFDATA2MSB 2 // Big Endian |
| 130 | |
| 131 | //---------------------------------------------------------------------- |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 132 | // Section Headers |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | //---------------------------------------------------------------------- |
| 134 | typedef struct Elf32_Shdr_Tag |
| 135 | { |
| 136 | Elf32_Word sh_name; |
| 137 | Elf32_Word sh_type; |
| 138 | Elf32_Word sh_flags; |
| 139 | Elf32_Addr sh_addr; |
| 140 | Elf32_Off sh_offset; |
| 141 | Elf32_Word sh_size; |
| 142 | Elf32_Word sh_link; |
| 143 | Elf32_Word sh_info; |
| 144 | Elf32_Word sh_addralign; |
| 145 | Elf32_Word sh_entsize; |
| 146 | } Elf32_Shdr; |
| 147 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 148 | typedef struct Elf64_Shdr_Tag |
| 149 | { |
| 150 | Elf64_Word sh_name; |
| 151 | Elf64_Word sh_type; |
| 152 | Elf64_Xword sh_flags; |
| 153 | Elf64_Addr sh_addr; |
| 154 | Elf64_Off sh_offset; |
| 155 | Elf64_Xword sh_size; |
| 156 | Elf64_Word sh_link; |
| 157 | Elf64_Word sh_info; |
| 158 | Elf64_Xword sh_addralign; |
| 159 | Elf64_Xword sh_entsize; |
| 160 | } Elf64_Shdr; |
| 161 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | //---------------------------------------------------------------------- |
| 163 | // Section Types (sh_type) |
| 164 | //---------------------------------------------------------------------- |
| 165 | #define SHT_NULL 0 |
| 166 | #define SHT_PROGBITS 1 |
| 167 | #define SHT_SYMTAB 2 |
| 168 | #define SHT_STRTAB 3 |
| 169 | #define SHT_RELA 4 |
| 170 | #define SHT_HASH 5 |
| 171 | #define SHT_DYNAMIC 6 |
| 172 | #define SHT_NOTE 7 |
| 173 | #define SHT_NOBITS 8 |
| 174 | #define SHT_REL 9 |
| 175 | #define SHT_SHLIB 10 |
| 176 | #define SHT_DYNSYM 11 |
| 177 | #define SHT_LOPROC 0x70000000 |
| 178 | #define SHT_HIPROC 0x7fffffff |
| 179 | #define SHT_LOUSER 0x80000000 |
| 180 | #define SHT_HIUSER 0xffffffff |
| 181 | |
| 182 | //---------------------------------------------------------------------- |
| 183 | // Special Section Indexes |
| 184 | //---------------------------------------------------------------------- |
| 185 | #define SHN_UNDEF 0 |
| 186 | #define SHN_LORESERVE 0xff00 |
| 187 | #define SHN_LOPROC 0xff00 |
| 188 | #define SHN_HIPROC 0xff1f |
| 189 | #define SHN_ABS 0xfff1 |
| 190 | #define SHN_COMMON 0xfff2 |
| 191 | #define SHN_HIRESERVE 0xffff |
| 192 | |
| 193 | //---------------------------------------------------------------------- |
| 194 | // Section Attribute Flags (sh_flags) |
| 195 | //---------------------------------------------------------------------- |
| 196 | #define SHF_WRITE 0x1 |
| 197 | #define SHF_ALLOC 0x2 |
| 198 | #define SHF_EXECINSTR 0x4 |
| 199 | #define SHF_MASKPROC 0xf0000000 |
| 200 | |
| 201 | |
| 202 | //---------------------------------------------------------------------- |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 203 | // Symbol Table Entry Headers |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | //---------------------------------------------------------------------- |
| 205 | typedef struct Elf32_Sym_Tag |
| 206 | { |
| 207 | Elf32_Word st_name; |
| 208 | Elf32_Addr st_value; |
| 209 | Elf32_Word st_size; |
| 210 | unsigned char st_info; |
| 211 | unsigned char st_other; |
| 212 | Elf32_Half st_shndx; |
| 213 | } Elf32_Sym; |
| 214 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 215 | typedef struct Elf64_Sym_Tag |
| 216 | { |
| 217 | Elf64_Word st_name; |
| 218 | unsigned char st_info; |
| 219 | unsigned char st_other; |
| 220 | Elf64_Half st_shndx; |
| 221 | Elf64_Addr st_value; |
| 222 | Elf64_Xword st_size; |
| 223 | } Elf64_Sym; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 225 | //---------------------------------------------------------------------- |
| 226 | // Accessors to the binding and type bits in the st_info field of a |
| 227 | // symbol table entry. Valid for both 32 and 64 bit variations. |
| 228 | //---------------------------------------------------------------------- |
| 229 | #define ELF_ST_BIND(i) ((i)>>4) |
| 230 | #define ELF_ST_TYPE(i) ((i)&0xf) |
| 231 | #define ELF_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | |
| 233 | // ST_BIND |
| 234 | #define STB_LOCAL 0 |
| 235 | #define STB_GLOBAL 1 |
| 236 | #define STB_WEAK 2 |
| 237 | #define STB_LOPROC 13 |
| 238 | #define STB_HIPROC 15 |
| 239 | |
| 240 | // ST_TYPE |
| 241 | #define STT_NOTYPE 0 |
| 242 | #define STT_OBJECT 1 |
| 243 | #define STT_FUNC 2 |
| 244 | #define STT_SECTION 3 |
| 245 | #define STT_FILE 4 |
| 246 | #define STT_LOPROC 13 |
| 247 | #define STT_HIPROC 15 |
| 248 | |
| 249 | |
| 250 | //---------------------------------------------------------------------- |
| 251 | // Relocation Entries |
| 252 | //---------------------------------------------------------------------- |
| 253 | typedef struct Elf32_Rel_Tag |
| 254 | { |
| 255 | Elf32_Addr r_offset; |
| 256 | Elf32_Word r_info; |
| 257 | } Elf32_Rel; |
| 258 | |
| 259 | typedef struct Elf32_Rela_Tag |
| 260 | { |
| 261 | Elf32_Addr r_offset; |
| 262 | Elf32_Word r_info; |
| 263 | Elf32_Sword r_addend; |
| 264 | } Elf32_Rela; |
| 265 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 266 | typedef struct Elf64_Rel_Tag |
| 267 | { |
| 268 | Elf64_Addr r_offset; |
| 269 | Elf64_Word r_info; |
| 270 | } Elf64_Rel; |
| 271 | |
| 272 | typedef struct Elf64_Rela_Tag |
| 273 | { |
| 274 | Elf64_Addr r_offset; |
| 275 | Elf64_Word r_info; |
| 276 | Elf64_Sword r_addend; |
| 277 | } Elf64_Rela; |
| 278 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | #define ELF32_R_SYM(i) ((i)>>8) |
| 280 | #define ELF32_R_TYPE(i) ((unsignedchar)(i)) |
| 281 | #define ELF32_R_INFO(s,t) (((s)<<8)+(unsignedchar)(t)) |
| 282 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 283 | //---------------------------------------------------------------------- |
| 284 | // Dynamic Table Entry Headers |
| 285 | //---------------------------------------------------------------------- |
| 286 | typedef struct Elf64_Dyn_Tag |
| 287 | { |
| 288 | Elf64_Sxword d_tag; |
| 289 | union |
| 290 | { |
| 291 | Elf64_Xword d_val; |
| 292 | Elf64_Addr d_ptr; |
| 293 | } d_un; |
| 294 | } Elf64_Dyn; |
| 295 | |
| 296 | #define DT_NULL 0 // Marks end of dynamic array. |
| 297 | #define DT_NEEDED 1 // String table offset of needed library. |
| 298 | #define DT_PLTRELSZ 2 // Size of relocation entries in PLT. |
| 299 | #define DT_PLTGOT 3 // Address associated with linkage table. |
| 300 | #define DT_HASH 4 // Address of symbolic hash table. |
| 301 | #define DT_STRTAB 5 // Address of dynamic string table. |
| 302 | #define DT_SYMTAB 6 // Address of dynamic symbol table. |
| 303 | #define DT_RELA 7 // Address of relocation table (Rela entries). |
| 304 | #define DT_RELASZ 8 // Size of Rela relocation table. |
| 305 | #define DT_RELAENT 9 // Size of a Rela relocation entry. |
| 306 | #define DT_STRSZ 10 // Total size of the string table. |
| 307 | #define DT_SYMENT 11 // Size of a symbol table entry. |
| 308 | #define DT_INIT 12 // Address of initialization function. |
| 309 | #define DT_FINI 13 // Address of termination function. |
| 310 | #define DT_SONAME 14 // String table offset of a shared objects name. |
| 311 | #define DT_RPATH 15 // String table offset of library search path. |
| 312 | #define DT_SYMBOLIC 16 // Changes symbol resolution algorithm. |
| 313 | #define DT_REL 17 // Address of relocation table (Rel entries). |
| 314 | #define DT_RELSZ 18 // Size of Rel relocation table. |
| 315 | #define DT_RELENT 19 // Size of a Rel relocation entry. |
| 316 | #define DT_PLTREL 20 // Type of relocation entry used for linking. |
| 317 | #define DT_DEBUG 21 // Reserved for debugger. |
| 318 | #define DT_TEXTREL 22 // Relocations exist for non-writable segements. |
| 319 | #define DT_JMPREL 23 // Address of relocations associated with PLT. |
| 320 | #define DT_BIND_NOW 24 // Process all relocations before execution. |
| 321 | #define DT_INIT_ARRAY 25 // Pointer to array of initialization functions. |
| 322 | #define DT_FINI_ARRAY 26 // Pointer to array of termination functions. |
| 323 | #define DT_INIT_ARRAYSZ 27 // Size of DT_INIT_ARRAY. |
| 324 | #define DT_FINI_ARRAYSZ 28 // Size of DT_FINI_ARRAY. |
| 325 | #define DT_LOOS 0x60000000 // Start of environment specific tags. |
| 326 | #define DT_HIOS 0x6FFFFFFF // End of environment specific tags. |
| 327 | #define DT_LOPROC 0x70000000 // Start of processor specific tags. |
| 328 | #define DT_HIPROC 0x7FFFFFFF // End of processor specific tags. |
| 329 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | |
| 331 | //---------------------------------------------------------------------- |
| 332 | // Program Headers |
| 333 | //---------------------------------------------------------------------- |
| 334 | typedef struct Elf32_Phdr_Tag |
| 335 | { |
| 336 | Elf32_Word p_type; |
| 337 | Elf32_Off p_offset; |
| 338 | Elf32_Addr p_vaddr; |
| 339 | Elf32_Addr p_paddr; |
| 340 | Elf32_Word p_filesz; |
| 341 | Elf32_Word p_memsz; |
| 342 | Elf32_Word p_flags; |
| 343 | Elf32_Word p_align; |
| 344 | } Elf32_Phdr; |
| 345 | |
Greg Clayton | 51dc188 | 2010-07-07 21:52:01 +0000 | [diff] [blame^] | 346 | typedef struct Elf64_Phdr_Tag |
| 347 | { |
| 348 | Elf64_Word p_type; |
| 349 | Elf64_Off p_offset; |
| 350 | Elf64_Addr p_vaddr; |
| 351 | Elf64_Addr p_paddr; |
| 352 | Elf64_Word p_filesz; |
| 353 | Elf64_Word p_memsz; |
| 354 | Elf64_Word p_flags; |
| 355 | Elf64_Word p_align; |
| 356 | } Elf64_Phdr; |
| 357 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | //---------------------------------------------------------------------- |
| 359 | // Program Header Type (p_type) |
| 360 | //---------------------------------------------------------------------- |
| 361 | #define PT_NULL 0 |
| 362 | #define PT_LOAD 1 |
| 363 | #define PT_DYNAMIC 2 |
| 364 | #define PT_INTERP 3 |
| 365 | #define PT_NOTE 4 |
| 366 | #define PT_SHLIB 5 |
| 367 | #define PT_PHDR 6 |
| 368 | #define PT_LOPROC 0x70000000 |
| 369 | #define PT_HIPROC 0x7fffffff |
| 370 | |
| 371 | #define PF_X (1 << 0) // executable |
| 372 | #define PF_W (1 << 1) // writable |
| 373 | #define PF_R (1 << 2) // readable |
| 374 | |
| 375 | |
| 376 | #endif // __elf_h__ |