1 //===-- llvm/Support/ELF.h - ELF constants and data structures --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header contains common, non-processor-specific data structures and
11 // constants for the ELF file format.
13 // The details of the ELF32 bits in this file are largely based on the Tool
14 // Interface Standard (TIS) Executable and Linking Format (ELF) Specification
15 // Version 1.2, May 1995. The ELF64 stuff is based on ELF-64 Object File Format
16 // Version 1.5, Draft 2, May 1998 as well as OpenBSD header files.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_SUPPORT_ELF_H
21 #define LLVM_SUPPORT_ELF_H
23 #include "llvm/System/DataTypes.h"
30 typedef uint32_t Elf32_Addr; // Program address
31 typedef uint16_t Elf32_Half;
32 typedef uint32_t Elf32_Off; // File offset
33 typedef int32_t Elf32_Sword;
34 typedef uint32_t Elf32_Word;
36 typedef uint64_t Elf64_Addr;
37 typedef uint64_t Elf64_Off;
38 typedef int32_t Elf64_Shalf;
39 typedef int32_t Elf64_Sword;
40 typedef uint32_t Elf64_Word;
41 typedef int64_t Elf64_Sxword;
42 typedef uint64_t Elf64_Xword;
43 typedef uint32_t Elf64_Half;
44 typedef uint16_t Elf64_Quarter;
46 // Object file magic string.
47 static const char ElfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' };
49 // e_ident size and indices.
51 EI_MAG0 = 0, // File identification index.
52 EI_MAG1 = 1, // File identification index.
53 EI_MAG2 = 2, // File identification index.
54 EI_MAG3 = 3, // File identification index.
55 EI_CLASS = 4, // File class.
56 EI_DATA = 5, // Data encoding.
57 EI_VERSION = 6, // File version.
58 EI_OSABI = 7, // OS/ABI identification.
59 EI_ABIVERSION = 8, // ABI version.
60 EI_PAD = 9, // Start of padding bytes.
61 EI_NIDENT = 16 // Number of bytes in e_ident.
65 unsigned char e_ident[EI_NIDENT]; // ELF Identification bytes
66 Elf32_Half e_type; // Type of file (see ET_* below)
67 Elf32_Half e_machine; // Required architecture for this file (see EM_*)
68 Elf32_Word e_version; // Must be equal to 1
69 Elf32_Addr e_entry; // Address to jump to in order to start program
70 Elf32_Off e_phoff; // Program header table's file offset, in bytes
71 Elf32_Off e_shoff; // Section header table's file offset, in bytes
72 Elf32_Word e_flags; // Processor-specific flags
73 Elf32_Half e_ehsize; // Size of ELF header, in bytes
74 Elf32_Half e_phentsize; // Size of an entry in the program header table
75 Elf32_Half e_phnum; // Number of entries in the program header table
76 Elf32_Half e_shentsize; // Size of an entry in the section header table
77 Elf32_Half e_shnum; // Number of entries in the section header table
78 Elf32_Half e_shstrndx; // Sect hdr table index of sect name string table
79 bool checkMagic() const {
80 return (memcmp(e_ident, ElfMagic, strlen(ElfMagic))) == 0;
82 unsigned char getFileClass() const { return e_ident[EI_CLASS]; }
83 unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
86 // 64-bit ELF header. Fields are the same as for ELF32, but with different
89 unsigned char e_ident[EI_NIDENT];
91 Elf64_Quarter e_machine;
97 Elf64_Quarter e_ehsize;
98 Elf64_Quarter e_phentsize;
99 Elf64_Quarter e_phnum;
100 Elf64_Quarter e_shentsize;
101 Elf64_Quarter e_shnum;
102 Elf64_Quarter e_shstrndx;
103 bool checkMagic() const {
104 return (memcmp(e_ident, ElfMagic, strlen(ElfMagic))) == 0;
106 unsigned char getFileClass() const { return e_ident[EI_CLASS]; }
107 unsigned char getDataEncoding() const { return e_ident[EI_DATA]; }
112 ET_NONE = 0, // No file type
113 ET_REL = 1, // Relocatable file
114 ET_EXEC = 2, // Executable file
115 ET_DYN = 3, // Shared object file
116 ET_CORE = 4, // Core file
117 ET_LOPROC = 0xff00, // Beginning of processor-specific codes
118 ET_HIPROC = 0xffff // Processor-specific
127 // Machine architectures
129 EM_NONE = 0, // No machine
130 EM_M32 = 1, // AT&T WE 32100
131 EM_SPARC = 2, // SPARC
132 EM_386 = 3, // Intel 386
133 EM_68K = 4, // Motorola 68000
134 EM_88K = 5, // Motorola 88000
135 EM_486 = 6, // Intel 486 (deprecated)
136 EM_860 = 7, // Intel 80860
137 EM_MIPS = 8, // MIPS R3000
138 EM_PPC = 20, // PowerPC
139 EM_PPC64 = 21, // PowerPC64
141 EM_ALPHA = 41, // DEC Alpha
142 EM_SPARCV9 = 43, // SPARC V9
143 EM_X86_64 = 62 // AMD64
146 // Object file classes.
148 ELFCLASS32 = 1, // 32-bit object file
149 ELFCLASS64 = 2 // 64-bit object file
152 // Object file byte orderings.
154 ELFDATANONE = 0, // Invalid data encoding.
155 ELFDATA2LSB = 1, // Little-endian object file
156 ELFDATA2MSB = 2 // Big-endian object file
159 // OS ABI identification.
161 ELFOSABI_NONE = 0, // UNIX System V ABI
162 ELFOSABI_HPUX = 1, // HP-UX operating system
163 ELFOSABI_NETBSD = 2, // NetBSD
164 ELFOSABI_LINUX = 3, // GNU/Linux
165 ELFOSABI_HURD = 4, // GNU/Hurd
166 ELFOSABI_SOLARIS = 6, // Solaris
167 ELFOSABI_AIX = 7, // AIX
168 ELFOSABI_IRIX = 8, // IRIX
169 ELFOSABI_FREEBSD = 9, // FreeBSD
170 ELFOSABI_TRU64 = 10, // TRU64 UNIX
171 ELFOSABI_MODESTO = 11, // Novell Modesto
172 ELFOSABI_OPENBSD = 12, // OpenBSD
173 ELFOSABI_OPENVMS = 13, // OpenVMS
174 ELFOSABI_NSK = 14, // Hewlett-Packard Non-Stop Kernel
175 ELFOSABI_AROS = 15, // AROS
176 ELFOSABI_FENIXOS = 16, // FenixOS
177 ELFOSABI_C6000_ELFABI = 64, // Bare-metal TMS320C6000
178 ELFOSABI_C6000_LINUX = 65, // Linux TMS320C6000
179 ELFOSABI_ARM = 97, // ARM
180 ELFOSABI_STANDALONE = 255 // Standalone (embedded) application
183 // X86_64 relocations.
191 R_X86_64_GLOB_DAT = 6,
192 R_X86_64_JUMP_SLOT = 7,
193 R_X86_64_RELATIVE = 8,
194 R_X86_64_GOTPCREL = 9,
201 R_X86_64_DTPMOD64 = 16,
202 R_X86_64_DTPOFF64 = 17,
203 R_X86_64_TPOFF64 = 18,
206 R_X86_64_DTPOFF32 = 21,
207 R_X86_64_GOTTPOFF = 22,
208 R_X86_64_TPOFF32 = 23,
210 R_X86_64_GOTOFF64 = 25,
211 R_X86_64_GOTPC32 = 26,
212 R_X86_64_SIZE32 = 32,
213 R_X86_64_SIZE64 = 33,
214 R_X86_64_GOTPC32_TLSDESC = 34,
215 R_X86_64_TLSDESC_CALL = 35,
216 R_X86_64_TLSDESC = 36
221 Elf32_Word sh_name; // Section name (index into string table)
222 Elf32_Word sh_type; // Section type (SHT_*)
223 Elf32_Word sh_flags; // Section flags (SHF_*)
224 Elf32_Addr sh_addr; // Address where section is to be loaded
225 Elf32_Off sh_offset; // File offset of section data, in bytes
226 Elf32_Word sh_size; // Size of section, in bytes
227 Elf32_Word sh_link; // Section type-specific header table index link
228 Elf32_Word sh_info; // Section type-specific extra information
229 Elf32_Word sh_addralign; // Section address alignment
230 Elf32_Word sh_entsize; // Size of records contained within the section
233 // Section header for ELF64 - same fields as ELF32, different types.
237 Elf64_Xword sh_flags;
243 Elf64_Xword sh_addralign;
244 Elf64_Xword sh_entsize;
247 // Special section indices.
249 SHN_UNDEF = 0, // Undefined, missing, irrelevant, or meaningless
250 SHN_LORESERVE = 0xff00, // Lowest reserved index
251 SHN_LOPROC = 0xff00, // Lowest processor-specific index
252 SHN_HIPROC = 0xff1f, // Highest processor-specific index
253 SHN_ABS = 0xfff1, // Symbol has absolute value; does not need relocation
254 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables
255 SHN_HIRESERVE = 0xffff // Highest reserved index
260 SHT_NULL = 0, // No associated section (inactive entry).
261 SHT_PROGBITS = 1, // Program-defined contents.
262 SHT_SYMTAB = 2, // Symbol table.
263 SHT_STRTAB = 3, // String table.
264 SHT_RELA = 4, // Relocation entries; explicit addends.
265 SHT_HASH = 5, // Symbol hash table.
266 SHT_DYNAMIC = 6, // Information for dynamic linking.
267 SHT_NOTE = 7, // Information about the file.
268 SHT_NOBITS = 8, // Data occupies no space in the file.
269 SHT_REL = 9, // Relocation entries; no explicit addends.
270 SHT_SHLIB = 10, // Reserved.
271 SHT_DYNSYM = 11, // Symbol table.
272 SHT_LOPROC = 0x70000000, // Lowest processor architecture-specific type.
273 SHT_HIPROC = 0x7fffffff, // Highest processor architecture-specific type.
274 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications.
275 SHT_HIUSER = 0xffffffff // Highest type reserved for applications.
280 SHF_WRITE = 0x1, // Section data should be writable during execution.
281 SHF_ALLOC = 0x2, // Section occupies memory during program execution.
282 SHF_EXECINSTR = 0x4, // Section contains executable machine instructions.
283 SHF_MASKPROC = 0xf0000000 // Bits indicating processor-specific flags.
286 // Symbol table entries for ELF32.
288 Elf32_Word st_name; // Symbol name (index into string table)
289 Elf32_Addr st_value; // Value or address associated with the symbol
290 Elf32_Word st_size; // Size of the symbol
291 unsigned char st_info; // Symbol's type and binding attributes
292 unsigned char st_other; // Must be zero; reserved
293 Elf32_Half st_shndx; // Which section (header table index) it's defined in
295 // These accessors and mutators correspond to the ELF32_ST_BIND,
296 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification:
297 unsigned char getBinding() const { return st_info >> 4; }
298 unsigned char getType() const { return st_info & 0x0f; }
299 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
300 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
301 void setBindingAndType(unsigned char b, unsigned char t) {
302 st_info = (b << 4) + (t & 0x0f);
306 // Symbol table entries for ELF64.
308 Elf64_Word st_name; // Symbol name (index into string table)
309 unsigned char st_info; // Symbol's type and binding attributes
310 unsigned char st_other; // Must be zero; reserved
311 Elf64_Half st_shndx; // Which section (header table index) it's defined in
312 Elf64_Addr st_value; // Value or address associated with the symbol
313 Elf64_Xword st_size; // Size of the symbol
315 // These accessors and mutators are identical to those defined for ELF32
316 // symbol table entries.
317 unsigned char getBinding() const { return st_info >> 4; }
318 unsigned char getType() const { return st_info & 0x0f; }
319 void setBinding(unsigned char b) { setBindingAndType(b, getType()); }
320 void setType(unsigned char t) { setBindingAndType(getBinding(), t); }
321 void setBindingAndType(unsigned char b, unsigned char t) {
322 st_info = (b << 4) + (t & 0x0f);
328 STB_LOCAL = 0, // Local symbol, not visible outside obj file containing def
329 STB_GLOBAL = 1, // Global symbol, visible to all object files being combined
330 STB_WEAK = 2, // Weak symbol, like global but lower-precedence
331 STB_LOPROC = 13, // Lowest processor-specific binding type
332 STB_HIPROC = 15 // Highest processor-specific binding type
337 STT_NOTYPE = 0, // Symbol's type is not specified
338 STT_OBJECT = 1, // Symbol is a data object (variable, array, etc.)
339 STT_FUNC = 2, // Symbol is executable code (function, etc.)
340 STT_SECTION = 3, // Symbol refers to a section
341 STT_FILE = 4, // Local, absolute symbol that refers to a file
342 STT_COMMON = 5, // An uninitialised common block
343 STT_TLS = 6, // Thread local data object
344 STT_LOPROC = 13, // Lowest processor-specific symbol type
345 STT_HIPROC = 15 // Highest processor-specific symbol type
349 STV_DEFAULT = 0, // Visibility is specified by binding type
350 STV_INTERNAL = 1, // Defined by processor supplements
351 STV_HIDDEN = 2, // Not visible to other components
352 STV_PROTECTED = 3 // Visible in other components but not preemptable
355 // Relocation entry, without explicit addend.
357 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr)
358 Elf32_Word r_info; // Symbol table index and type of relocation to apply
360 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
361 // and ELF32_R_INFO macros defined in the ELF specification:
362 Elf32_Word getSymbol() const { return (r_info >> 8); }
363 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
364 void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
365 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
366 void setSymbolAndType(Elf32_Word s, unsigned char t) {
367 r_info = (s << 8) + t;
371 // Relocation entry with explicit addend.
373 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr)
374 Elf32_Word r_info; // Symbol table index and type of relocation to apply
375 Elf32_Sword r_addend; // Compute value for relocatable field by adding this
377 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE,
378 // and ELF32_R_INFO macros defined in the ELF specification:
379 Elf32_Word getSymbol() const { return (r_info >> 8); }
380 unsigned char getType() const { return (unsigned char) (r_info & 0x0ff); }
381 void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
382 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
383 void setSymbolAndType(Elf32_Word s, unsigned char t) {
384 r_info = (s << 8) + t;
388 // Relocation entry, without explicit addend.
390 Elf64_Addr r_offset; // Location (file byte offset, or program virtual addr).
391 Elf64_Xword r_info; // Symbol table index and type of relocation to apply.
393 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
394 // and ELF64_R_INFO macros defined in the ELF specification:
395 Elf64_Xword getSymbol() const { return (r_info >> 32); }
396 unsigned char getType() const {
397 return (unsigned char) (r_info & 0xffffffffL);
399 void setSymbol(Elf32_Word s) { setSymbolAndType(s, getType()); }
400 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
401 void setSymbolAndType(Elf64_Xword s, unsigned char t) {
402 r_info = (s << 32) + (t&0xffffffffL);
406 // Relocation entry with explicit addend.
408 Elf64_Addr r_offset; // Location (file byte offset, or program virtual addr).
409 Elf64_Xword r_info; // Symbol table index and type of relocation to apply.
410 Elf64_Sxword r_addend; // Compute value for relocatable field by adding this.
412 // These accessors and mutators correspond to the ELF64_R_SYM, ELF64_R_TYPE,
413 // and ELF64_R_INFO macros defined in the ELF specification:
414 Elf64_Xword getSymbol() const { return (r_info >> 32); }
415 unsigned char getType() const {
416 return (unsigned char) (r_info & 0xffffffffL);
418 void setSymbol(Elf64_Xword s) { setSymbolAndType(s, getType()); }
419 void setType(unsigned char t) { setSymbolAndType(getSymbol(), t); }
420 void setSymbolAndType(Elf64_Xword s, unsigned char t) {
421 r_info = (s << 32) + (t&0xffffffffL);
425 // Program header for ELF32.
427 Elf32_Word p_type; // Type of segment
428 Elf32_Off p_offset; // File offset where segment is located, in bytes
429 Elf32_Addr p_vaddr; // Virtual address of beginning of segment
430 Elf32_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
431 Elf32_Word p_filesz; // Num. of bytes in file image of segment (may be zero)
432 Elf32_Word p_memsz; // Num. of bytes in mem image of segment (may be zero)
433 Elf32_Word p_flags; // Segment flags
434 Elf32_Word p_align; // Segment alignment constraint
437 // Program header for ELF64.
439 Elf64_Word p_type; // Type of segment
440 Elf64_Word p_flags; // Segment flags
441 Elf64_Off p_offset; // File offset where segment is located, in bytes
442 Elf64_Addr p_vaddr; // Virtual address of beginning of segment
443 Elf64_Addr p_paddr; // Physical address of beginning of segment (OS-specific)
444 Elf64_Xword p_filesz; // Num. of bytes in file image of segment (may be zero)
445 Elf64_Xword p_memsz; // Num. of bytes in mem image of segment (may be zero)
446 Elf64_Xword p_align; // Segment alignment constraint
451 PT_NULL = 0, // Unused segment.
452 PT_LOAD = 1, // Loadable segment.
453 PT_DYNAMIC = 2, // Dynamic linking information.
454 PT_INTERP = 3, // Interpreter pathname.
455 PT_NOTE = 4, // Auxiliary information.
456 PT_SHLIB = 5, // Reserved.
457 PT_PHDR = 6, // The program header table itself.
458 PT_LOPROC = 0x70000000, // Lowest processor-specific program hdr entry type.
459 PT_HIPROC = 0x7fffffff // Highest processor-specific program hdr entry type.
462 // Segment flag bits.
467 PF_MASKPROC = 0xf0000000 // Unspecified
470 // Dynamic table entry for ELF32.
473 Elf32_Sword d_tag; // Type of dynamic table entry.
476 Elf32_Word d_val; // Integer value of entry.
477 Elf32_Addr d_ptr; // Pointer value of entry.
481 // Dynamic table entry for ELF64.
484 Elf64_Sxword d_tag; // Type of dynamic table entry.
487 Elf64_Xword d_val; // Integer value of entry.
488 Elf64_Addr d_ptr; // Pointer value of entry.
492 // Dynamic table entry tags.
494 DT_NULL = 0, // Marks end of dynamic array.
495 DT_NEEDED = 1, // String table offset of needed library.
496 DT_PLTRELSZ = 2, // Size of relocation entries in PLT.
497 DT_PLTGOT = 3, // Address associated with linkage table.
498 DT_HASH = 4, // Address of symbolic hash table.
499 DT_STRTAB = 5, // Address of dynamic string table.
500 DT_SYMTAB = 6, // Address of dynamic symbol table.
501 DT_RELA = 7, // Address of relocation table (Rela entries).
502 DT_RELASZ = 8, // Size of Rela relocation table.
503 DT_RELAENT = 9, // Size of a Rela relocation entry.
504 DT_STRSZ = 10, // Total size of the string table.
505 DT_SYMENT = 11, // Size of a symbol table entry.
506 DT_INIT = 12, // Address of initialization function.
507 DT_FINI = 13, // Address of termination function.
508 DT_SONAME = 14, // String table offset of a shared objects name.
509 DT_RPATH = 15, // String table offset of library search path.
510 DT_SYMBOLIC = 16, // Changes symbol resolution algorithm.
511 DT_REL = 17, // Address of relocation table (Rel entries).
512 DT_RELSZ = 18, // Size of Rel relocation table.
513 DT_RELENT = 19, // Size of a Rel relocation entry.
514 DT_PLTREL = 20, // Type of relocation entry used for linking.
515 DT_DEBUG = 21, // Reserved for debugger.
516 DT_TEXTREL = 22, // Relocations exist for non-writable segements.
517 DT_JMPREL = 23, // Address of relocations associated with PLT.
518 DT_BIND_NOW = 24, // Process all relocations before execution.
519 DT_INIT_ARRAY = 25, // Pointer to array of initialization functions.
520 DT_FINI_ARRAY = 26, // Pointer to array of termination functions.
521 DT_INIT_ARRAYSZ = 27, // Size of DT_INIT_ARRAY.
522 DT_FINI_ARRAYSZ = 28, // Size of DT_FINI_ARRAY.
523 DT_LOOS = 0x60000000, // Start of environment specific tags.
524 DT_HIOS = 0x6FFFFFFF, // End of environment specific tags.
525 DT_LOPROC = 0x70000000, // Start of processor specific tags.
526 DT_HIPROC = 0x7FFFFFFF // End of processor specific tags.
529 } // end namespace ELF
531 } // end namespace llvm