ELF.h revision fe4591bfc2b147c10d6a7b1a5af56454fb78dace
1//===-- llvm/Support/ELF.h - ELF constants and data structures --*- 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// This header contains common, non-processor-specific data structures and 11// constants for the ELF file format. 12// 13// The details of the ELF32 bits in this file are largely based on 14// the Tool Interface Standard (TIS) Executable and Linking Format 15// (ELF) Specification Version 1.2, May 1995. The ELF64 stuff is not 16// standardized, as far as I can tell. It was largely based on information 17// I found in OpenBSD header files. 18// 19//===----------------------------------------------------------------------===// 20 21#ifndef LLVM_SUPPORT_ELF_H 22#define LLVM_SUPPORT_ELF_H 23 24#include "llvm/System/DataTypes.h" 25#include <cstring> 26 27namespace llvm { 28 29namespace ELF { 30 31typedef uint32_t Elf32_Addr; // Program address 32typedef uint16_t Elf32_Half; 33typedef uint32_t Elf32_Off; // File offset 34typedef int32_t Elf32_Sword; 35typedef uint32_t Elf32_Word; 36 37typedef uint64_t Elf64_Addr; 38typedef uint64_t Elf64_Off; 39typedef int32_t Elf64_Shalf; 40typedef int32_t Elf64_Sword; 41typedef uint32_t Elf64_Word; 42typedef int64_t Elf64_Sxword; 43typedef uint64_t Elf64_Xword; 44typedef uint32_t Elf64_Half; 45typedef uint16_t Elf64_Quarter; 46 47// Object file magic string. 48static const char ElfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' }; 49 50struct Elf32_Ehdr { 51 unsigned char e_ident[16]; // ELF Identification bytes 52 Elf32_Half e_type; // Type of file (see ET_* below) 53 Elf32_Half e_machine; // Required architecture for this file (see EM_*) 54 Elf32_Word e_version; // Must be equal to 1 55 Elf32_Addr e_entry; // Address to jump to in order to start program 56 Elf32_Off e_phoff; // Program header table's file offset, in bytes 57 Elf32_Off e_shoff; // Section header table's file offset, in bytes 58 Elf32_Word e_flags; // Processor-specific flags 59 Elf32_Half e_ehsize; // Size of ELF header, in bytes 60 Elf32_Half e_phentsize; // Size of an entry in the program header table 61 Elf32_Half e_phnum; // Number of entries in the program header table 62 Elf32_Half e_shentsize; // Size of an entry in the section header table 63 Elf32_Half e_shnum; // Number of entries in the section header table 64 Elf32_Half e_shstrndx; // Sect hdr table index of sect name string table 65 bool checkMagic() const { 66 return (memcmp (e_ident, ElfMagic, strlen (ElfMagic))) == 0; 67 } 68 unsigned char getFileClass () const { return e_ident[4]; } 69 unsigned char getDataEncoding () { return e_ident[5]; } 70}; 71 72// 64-bit ELF header. Fields are the same as for ELF32, but with different 73// types (see above). 74struct Elf64_Ehdr { 75 unsigned char e_ident[16]; 76 Elf64_Quarter e_type; 77 Elf64_Quarter e_machine; 78 Elf64_Half e_version; 79 Elf64_Addr e_entry; 80 Elf64_Off e_phoff; 81 Elf64_Off e_shoff; 82 Elf64_Half e_flags; 83 Elf64_Quarter e_ehsize; 84 Elf64_Quarter e_phentsize; 85 Elf64_Quarter e_phnum; 86 Elf64_Quarter e_shentsize; 87 Elf64_Quarter e_shnum; 88 Elf64_Quarter e_shstrndx; 89}; 90 91// File types 92enum { 93 ET_NONE = 0, // No file type 94 ET_REL = 1, // Relocatable file 95 ET_EXEC = 2, // Executable file 96 ET_DYN = 3, // Shared object file 97 ET_CORE = 4, // Core file 98 ET_LOPROC = 0xff00, // Beginning of processor-specific codes 99 ET_HIPROC = 0xffff // Processor-specific 100}; 101 102// Versioning 103enum { 104 EV_NONE = 0, 105 EV_CURRENT = 1 106}; 107 108// Machine architectures 109enum { 110 EM_NONE = 0, // No machine 111 EM_M32 = 1, // AT&T WE 32100 112 EM_SPARC = 2, // SPARC 113 EM_386 = 3, // Intel 386 114 EM_68K = 4, // Motorola 68000 115 EM_88K = 5, // Motorola 88000 116 EM_486 = 6, // Intel 486 (deprecated) 117 EM_860 = 7, // Intel 80860 118 EM_MIPS = 8, // MIPS R3000 119 EM_PPC = 20, // PowerPC 120 EM_ARM = 40, // ARM 121 EM_ALPHA = 41, // DEC Alpha 122 EM_SPARCV9 = 43, // SPARC V9 123 EM_X86_64 = 62 // AMD64 124}; 125 126// Object file classes. 127enum { 128 ELFCLASS32 = 1, // 32-bit object file 129 ELFCLASS64 = 2 // 64-bit object file 130}; 131 132// Object file byte orderings. 133enum { 134 ELFDATA2LSB = 1, // Little-endian object file 135 ELFDATA2MSB = 2 // Big-endian object file 136}; 137 138// OS ABI identification. 139enum { 140 ELFOSABI_NONE = 0, // UNIX System V ABI 141 ELFOSABI_HPUX = 1, // HP-UX operating system 142 ELFOSABI_NETBSD = 2, // NetBSD 143 ELFOSABI_LINUX = 3, // GNU/Linux 144 ELFOSABI_HURD = 4, // GNU/Hurd 145 ELFOSABI_SOLARIS = 6, // Solaris 146 ELFOSABI_AIX = 7, // AIX 147 ELFOSABI_IRIX = 8, // IRIX 148 ELFOSABI_FREEBSD = 9, // FreeBSD 149 ELFOSABI_TRU64 = 10, // TRU64 UNIX 150 ELFOSABI_MODESTO = 11, // Novell Modesto 151 ELFOSABI_OPENBSD = 12, // OpenBSD 152 ELFOSABI_OPENVMS = 13, // OpenVMS 153 ELFOSABI_NSK = 14, // Hewlett-Packard Non-Stop Kernel 154 ELFOSABI_AROS = 15, // AROS 155 ELFOSABI_FENIXOS = 16, // FenixOS 156 ELFOSABI_C6000_ELFABI = 64, // Bare-metal TMS320C6000 157 ELFOSABI_C6000_LINUX = 65, // Linux TMS320C6000 158 ELFOSABI_ARM = 97, // ARM 159 ELFOSABI_STANDALONE = 255 // Standalone (embedded) application 160}; 161 162// X86_64 relocations. 163enum { 164 R_X86_64_NONE = 0, 165 R_X86_64_64 = 1, 166 R_X86_64_PC32 = 2, 167 R_X86_64_GOT32 = 3, 168 R_X86_64_PLT32 = 4, 169 R_X86_64_COPY = 5, 170 R_X86_64_GLOB_DAT = 6, 171 R_X86_64_JUMP_SLOT = 7, 172 R_X86_64_RELATIVE = 8, 173 R_X86_64_GOTPCREL = 9, 174 R_X86_64_32 = 10, 175 R_X86_64_32S = 11, 176 R_X86_64_16 = 12, 177 R_X86_64_PC16 = 13, 178 R_X86_64_8 = 14, 179 R_X86_64_PC8 = 15, 180 R_X86_64_DTPMOD64 = 16, 181 R_X86_64_DTPOFF64 = 17, 182 R_X86_64_TPOFF64 = 18, 183 R_X86_64_TLSGD = 19, 184 R_X86_64_TLSLD = 20, 185 R_X86_64_DTPOFF32 = 21, 186 R_X86_64_GOTTPOFF = 22, 187 R_X86_64_TPOFF32 = 23, 188 R_X86_64_PC64 = 24, 189 R_X86_64_GOTOFF64 = 25, 190 R_X86_64_GOTPC32 = 26, 191 R_X86_64_SIZE32 = 32, 192 R_X86_64_SIZE64 = 33, 193 R_X86_64_GOTPC32_TLSDESC = 34, 194 R_X86_64_TLSDESC_CALL = 35, 195 R_X86_64_TLSDESC = 36 196}; 197 198// Section header. 199struct Elf32_Shdr { 200 Elf32_Word sh_name; // Section name (index into string table) 201 Elf32_Word sh_type; // Section type (SHT_*) 202 Elf32_Word sh_flags; // Section flags (SHF_*) 203 Elf32_Addr sh_addr; // Address where section is to be loaded 204 Elf32_Off sh_offset; // File offset of section data, in bytes 205 Elf32_Word sh_size; // Size of section, in bytes 206 Elf32_Word sh_link; // Section type-specific header table index link 207 Elf32_Word sh_info; // Section type-specific extra information 208 Elf32_Word sh_addralign; // Section address alignment 209 Elf32_Word sh_entsize; // Size of records contained within the section 210}; 211 212// Section header for ELF64 - same fields as ELF32, different types. 213struct Elf64_Shdr { 214 Elf64_Half sh_name; 215 Elf64_Half sh_type; 216 Elf64_Xword sh_flags; 217 Elf64_Addr sh_addr; 218 Elf64_Off sh_offset; 219 Elf64_Xword sh_size; 220 Elf64_Half sh_link; 221 Elf64_Half sh_info; 222 Elf64_Xword sh_addralign; 223 Elf64_Xword sh_entsize; 224}; 225 226// Special section indices. 227enum { 228 SHN_UNDEF = 0, // Undefined, missing, irrelevant, or meaningless 229 SHN_LORESERVE = 0xff00, // Lowest reserved index 230 SHN_LOPROC = 0xff00, // Lowest processor-specific index 231 SHN_HIPROC = 0xff1f, // Highest processor-specific index 232 SHN_ABS = 0xfff1, // Symbol has absolute value; does not need relocation 233 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables 234 SHN_HIRESERVE = 0xffff // Highest reserved index 235}; 236 237// Section types. 238enum { 239 SHT_NULL = 0, // No associated section (inactive entry). 240 SHT_PROGBITS = 1, // Program-defined contents. 241 SHT_SYMTAB = 2, // Symbol table. 242 SHT_STRTAB = 3, // String table. 243 SHT_RELA = 4, // Relocation entries; explicit addends. 244 SHT_HASH = 5, // Symbol hash table. 245 SHT_DYNAMIC = 6, // Information for dynamic linking. 246 SHT_NOTE = 7, // Information about the file. 247 SHT_NOBITS = 8, // Data occupies no space in the file. 248 SHT_REL = 9, // Relocation entries; no explicit addends. 249 SHT_SHLIB = 10, // Reserved. 250 SHT_DYNSYM = 11, // Symbol table. 251 SHT_LOPROC = 0x70000000, // Lowest processor architecture-specific type. 252 SHT_HIPROC = 0x7fffffff, // Highest processor architecture-specific type. 253 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications. 254 SHT_HIUSER = 0xffffffff // Highest type reserved for applications. 255}; 256 257// Section flags. 258enum { 259 SHF_WRITE = 0x1, // Section data should be writable during execution. 260 SHF_ALLOC = 0x2, // Section occupies memory during program execution. 261 SHF_EXECINSTR = 0x4, // Section contains executable machine instructions. 262 SHF_MASKPROC = 0xf0000000 // Bits indicating processor-specific flags. 263}; 264 265// Symbol table entries. 266struct Elf32_Sym { 267 Elf32_Word st_name; // Symbol name (index into string table) 268 Elf32_Addr st_value; // Value or address associated with the symbol 269 Elf32_Word st_size; // Size of the symbol 270 unsigned char st_info; // Symbol's type and binding attributes 271 unsigned char st_other; // Must be zero; reserved 272 Elf32_Half st_shndx; // Which section (header table index) it's defined in 273 274 // These accessors and mutators correspond to the ELF32_ST_BIND, 275 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: 276 unsigned char getBinding () const { return st_info >> 4; } 277 unsigned char getType () const { return st_info & 0x0f; } 278 void setBinding (unsigned char b) { setBindingAndType (b, getType ()); } 279 void setType (unsigned char t) { setBindingAndType (getBinding (), t); } 280 void setBindingAndType (unsigned char b, unsigned char t) { 281 st_info = (b << 4) + (t & 0x0f); 282 } 283}; 284 285// Symbol bindings. 286enum { 287 STB_LOCAL = 0, // Local symbol, not visible outside obj file containing def 288 STB_GLOBAL = 1, // Global symbol, visible to all object files being combined 289 STB_WEAK = 2, // Weak symbol, like global but lower-precedence 290 STB_LOPROC = 13, // Lowest processor-specific binding type 291 STB_HIPROC = 15 // Highest processor-specific binding type 292}; 293 294// Symbol types. 295enum { 296 STT_NOTYPE = 0, // Symbol's type is not specified 297 STT_OBJECT = 1, // Symbol is a data object (variable, array, etc.) 298 STT_FUNC = 2, // Symbol is executable code (function, etc.) 299 STT_SECTION = 3, // Symbol refers to a section 300 STT_FILE = 4, // Local, absolute symbol that refers to a file 301 STT_LOPROC = 13, // Lowest processor-specific symbol type 302 STT_HIPROC = 15 // Highest processor-specific symbol type 303}; 304 305// Relocation entry, without explicit addend. 306struct Elf32_Rel { 307 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr) 308 Elf32_Word r_info; // Symbol table index and type of relocation to apply 309 310 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 311 // and ELF32_R_INFO macros defined in the ELF specification: 312 Elf32_Word getSymbol () const { return (r_info >> 8); } 313 unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); } 314 void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); } 315 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 316 void setSymbolAndType (Elf32_Word s, unsigned char t) { 317 r_info = (s << 8) + t; 318 }; 319}; 320 321// Relocation entry with explicit addend. 322struct Elf32_Rela { 323 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr) 324 Elf32_Word r_info; // Symbol table index and type of relocation to apply 325 Elf32_Sword r_addend; // Compute value for relocatable field by adding this 326 327 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 328 // and ELF32_R_INFO macros defined in the ELF specification: 329 Elf32_Word getSymbol () const { return (r_info >> 8); } 330 unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); } 331 void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); } 332 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 333 void setSymbolAndType (Elf32_Word s, unsigned char t) { 334 r_info = (s << 8) + t; 335 }; 336}; 337 338// Relocation entry, without explicit addend. 339struct Elf64_Rel { 340 Elf64_Addr r_offset; // Location (file byte offset, or program virtual addr). 341 Elf64_Xword r_info; // Symbol table index and type of relocation to apply. 342 343 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, 344 // and ELF64_R_INFO macros defined in the ELF specification: 345 Elf64_Xword getSymbol () const { return (r_info >> 32); } 346 unsigned char getType () const { 347 return (unsigned char) (r_info & 0xffffffffL); 348 } 349 void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); } 350 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 351 void setSymbolAndType (Elf64_Xword s, unsigned char t) { 352 r_info = (s << 32) + (t&0xffffffffL); 353 }; 354}; 355 356// Relocation entry with explicit addend. 357struct Elf64_Rela { 358 Elf64_Addr r_offset; // Location (file byte offset, or program virtual addr). 359 Elf64_Xword r_info; // Symbol table index and type of relocation to apply. 360 Elf64_Sxword r_addend; // Compute value for relocatable field by adding this. 361 362 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE, 363 // and ELF64_R_INFO macros defined in the ELF specification: 364 Elf64_Xword getSymbol () const { return (r_info >> 32); } 365 unsigned char getType () const { 366 return (unsigned char) (r_info & 0xffffffffL); 367 } 368 void setSymbol (Elf64_Xword s) { setSymbolAndType (s, getType ()); } 369 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 370 void setSymbolAndType (Elf64_Xword s, unsigned char t) { 371 r_info = (s << 32) + (t&0xffffffffL); 372 }; 373}; 374 375// Program header. 376struct Elf32_Phdr { 377 Elf32_Word p_type; // Type of segment 378 Elf32_Off p_offset; // File offset where segment is located, in bytes 379 Elf32_Addr p_vaddr; // Virtual address of beginning of segment 380 Elf32_Addr p_paddr; // Physical address of beginning of segment (OS-specific) 381 Elf32_Word p_filesz; // Num. of bytes in file image of segment (may be zero) 382 Elf32_Word p_memsz; // Num. of bytes in mem image of segment (may be zero) 383 Elf32_Word p_flags; // Segment flags 384 Elf32_Word p_align; // Segment alignment constraint 385}; 386 387// Segment types. 388enum { 389 PT_NULL = 0, // Unused segment. 390 PT_LOAD = 1, // Loadable segment. 391 PT_DYNAMIC = 2, // Dynamic linking information. 392 PT_INTERP = 3, // Interpreter pathname. 393 PT_NOTE = 4, // Auxiliary information. 394 PT_SHLIB = 5, // Reserved. 395 PT_PHDR = 6, // The program header table itself. 396 PT_LOPROC = 0x70000000, // Lowest processor-specific program hdr entry type. 397 PT_HIPROC = 0x7fffffff // Highest processor-specific program hdr entry type. 398}; 399 400// Segment flag bits. 401enum { 402 PF_X = 1, // Execute 403 PF_W = 2, // Write 404 PF_R = 4, // Read 405 PF_MASKPROC = 0xf0000000 // Unspecified 406}; 407 408} // end namespace ELF 409 410} // end namespace llvm 411 412#endif 413