ELF.h revision e9f6f2c0492af8097166f1b7d62f131f20ca5714
1//===-- Support/ELF.h - ELF constants and data structures -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source 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#include "Support/DataTypes.h" 22#include <cstring> 23#include <cstdlib> 24 25namespace llvm { 26 27namespace ELF { 28 29typedef uint32_t Elf32_Addr; // Program address 30typedef uint16_t Elf32_Half; 31typedef uint32_t Elf32_Off; // File offset 32typedef int32_t Elf32_Sword; 33typedef uint32_t Elf32_Word; 34 35typedef uint64_t Elf64_Addr; 36typedef uint64_t Elf64_Off; 37typedef int32_t Elf64_Shalf; 38typedef int32_t Elf64_Sword; 39typedef uint32_t Elf64_Word; 40typedef int64_t Elf64_Sxword; 41typedef uint64_t Elf64_Xword; 42typedef uint32_t Elf64_Half; 43typedef uint16_t Elf64_Quarter; 44 45// Object file magic string. 46static const char ElfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' }; 47 48struct Elf32_Ehdr { 49 unsigned char e_ident[16]; // ELF Identification bytes 50 Elf32_Half e_type; // Type of file (see ET_* below) 51 Elf32_Half e_machine; // Required architecture for this file (see EM_*) 52 Elf32_Word e_version; // Must be equal to 1 53 Elf32_Addr e_entry; // Address to jump to in order to start program 54 Elf32_Off e_phoff; // Program header table's file offset, in bytes 55 Elf32_Off e_shoff; // Section header table's file offset, in bytes 56 Elf32_Word e_flags; // Processor-specific flags 57 Elf32_Half e_ehsize; // Size of ELF header, in bytes 58 Elf32_Half e_phentsize; // Size of an entry in the program header table 59 Elf32_Half e_phnum; // Number of entries in the program header table 60 Elf32_Half e_shentsize; // Size of an entry in the section header table 61 Elf32_Half e_shnum; // Number of entries in the section header table 62 Elf32_Half e_shstrndx; // Sect hdr table index of sect name string table 63 bool checkMagic () const { 64 return (memcmp (e_ident, ElfMagic, strlen (ElfMagic))) == 0; 65 } 66 unsigned char getFileClass () const { return e_ident[4]; } 67 unsigned char getDataEncoding () { return e_ident[5]; } 68}; 69 70// 64-bit ELF header. Fields are the same as for ELF32, but with different 71// types (see above). 72struct Elf64_Ehdr { 73 unsigned char e_ident[16]; 74 Elf64_Quarter e_type; 75 Elf64_Quarter e_machine; 76 Elf64_Half e_version; 77 Elf64_Addr e_entry; 78 Elf64_Off e_phoff; 79 Elf64_Off e_shoff; 80 Elf64_Half e_flags; 81 Elf64_Quarter e_ehsize; 82 Elf64_Quarter e_phentsize; 83 Elf64_Quarter e_phnum; 84 Elf64_Quarter e_shentsize; 85 Elf64_Quarter e_shnum; 86 Elf64_Quarter e_shstrndx; 87}; 88 89// File types 90enum { 91 ET_NONE = 0, // No file type 92 ET_REL = 1, // Relocatable file 93 ET_EXEC = 2, // Executable file 94 ET_DYN = 3, // Shared object file 95 ET_CORE = 4, // Core file 96 ET_LOPROC = 0xff00, // Beginning of processor-specific codes 97 ET_HIPROC = 0xffff // Processor-specific 98}; 99 100// Machine architectures 101enum { 102 EM_NONE = 0, // No machine 103 EM_M32 = 1, // AT&T WE 32100 104 EM_SPARC = 2, // SPARC 105 EM_386 = 3, // Intel 386 106 EM_68K = 4, // Motorola 68000 107 EM_88K = 5, // Motorola 88000 108 EM_486 = 6, // Intel 486 (deprecated) 109 EM_860 = 7, // Intel 80860 110 EM_MIPS = 8, // MIPS R3000 111 EM_PPC = 20, // PowerPC 112 EM_ARM = 40, // ARM 113 EM_ALPHA = 41, // DEC Alpha 114 EM_SPARCV9 = 43 // SPARC V9 115}; 116 117// Object file classes. 118enum { 119 ELFCLASS32 = 1, // 32-bit object file 120 ELFCLASS64 = 2 // 64-bit object file 121}; 122 123// Object file byte orderings. 124enum { 125 ELFDATA2LSB = 1, // Little-endian object file 126 ELFDATA2MSB = 2 // Big-endian object file 127}; 128 129// Section header. 130struct Elf32_Shdr { 131 Elf32_Word sh_name; // Section name (index into string table) 132 Elf32_Word sh_type; // Section type (SHT_*) 133 Elf32_Word sh_flags; // Section flags (SHF_*) 134 Elf32_Addr sh_addr; // Address where section is to be loaded 135 Elf32_Off sh_offset; // File offset of section data, in bytes 136 Elf32_Word sh_size; // Size of section, in bytes 137 Elf32_Word sh_link; // Section type-specific header table index link 138 Elf32_Word sh_info; // Section type-specific extra information 139 Elf32_Word sh_addralign; // Section address alignment 140 Elf32_Word sh_entsize; // Size of records contained within the section 141}; 142 143// Section header for ELF64 - same fields as ELF32, different types. 144struct Elf64_Shdr { 145 Elf64_Half sh_name; 146 Elf64_Half sh_type; 147 Elf64_Xword sh_flags; 148 Elf64_Addr sh_addr; 149 Elf64_Off sh_offset; 150 Elf64_Xword sh_size; 151 Elf64_Half sh_link; 152 Elf64_Half sh_info; 153 Elf64_Xword sh_addralign; 154 Elf64_Xword sh_entsize; 155}; 156 157// Special section indices. 158enum { 159 SHN_UNDEF = 0, // Undefined, missing, irrelevant, or meaningless 160 SHN_LORESERVE = 0xff00, // Lowest reserved index 161 SHN_LOPROC = 0xff00, // Lowest processor-specific index 162 SHN_HIPROC = 0xff1f, // Highest processor-specific index 163 SHN_ABS = 0xfff1, // Symbol has absolute value; does not need relocation 164 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables 165 SHN_HIRESERVE = 0xffff // Highest reserved index 166}; 167 168// Section types. 169enum { 170 SHT_NULL = 0, // No associated section (inactive entry). 171 SHT_PROGBITS = 1, // Program-defined contents. 172 SHT_SYMTAB = 2, // Symbol table. 173 SHT_STRTAB = 3, // String table. 174 SHT_RELA = 4, // Relocation entries; explicit addends. 175 SHT_HASH = 5, // Symbol hash table. 176 SHT_DYNAMIC = 6, // Information for dynamic linking. 177 SHT_NOTE = 7, // Information about the file. 178 SHT_NOBITS = 8, // Data occupies no space in the file. 179 SHT_REL = 9, // Relocation entries; no explicit addends. 180 SHT_SHLIB = 10, // Reserved. 181 SHT_DYNSYM = 11, // Symbol table. 182 SHT_LOPROC = 0x70000000, // Lowest processor architecture-specific type. 183 SHT_HIPROC = 0x7fffffff, // Highest processor architecture-specific type. 184 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications. 185 SHT_HIUSER = 0xffffffff // Highest type reserved for applications. 186}; 187 188// Section flags. 189enum { 190 SHF_WRITE = 0x1, // Section data should be writable during execution. 191 SHF_ALLOC = 0x2, // Section occupies memory during program execution. 192 SHF_EXECINSTR = 0x4, // Section contains executable machine instructions. 193 SHF_MASKPROC = 0xf0000000 // Bits indicating processor-specific flags. 194}; 195 196// Symbol table entries. 197struct Elf32_Sym { 198 Elf32_Word st_name; // Symbol name (index into string table) 199 Elf32_Addr st_value; // Value or address associated with the symbol 200 Elf32_Word st_size; // Size of the symbol 201 unsigned char st_info; // Symbol's type and binding attributes 202 unsigned char st_other; // Must be zero; reserved 203 Elf32_Half st_shndx; // Which section (header table index) it's defined in 204 205 // These accessors and mutators correspond to the ELF32_ST_BIND, 206 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: 207 unsigned char getBinding () const { return st_info >> 4; } 208 unsigned char getType () const { return st_info & 0x0f; } 209 void setBinding (unsigned char b) { setBindingAndType (b, getType ()); } 210 void setType (unsigned char t) { setBindingAndType (getBinding (), t); } 211 void setBindingAndType (unsigned char b, unsigned char t) { 212 st_info = (b << 4) + (t & 0x0f); 213 } 214}; 215 216// Symbol bindings. 217enum { 218 STB_LOCAL = 0, // Local symbol, not visible outside obj file containing def 219 STB_GLOBAL = 1, // Global symbol, visible to all object files being combined 220 STB_WEAK = 2, // Weak symbol, like global but lower-precedence 221 STB_LOPROC = 13, // Lowest processor-specific binding type 222 STB_HIPROC = 15 // Highest processor-specific binding type 223}; 224 225// Symbol types. 226enum { 227 STT_NOTYPE = 0, // Symbol's type is not specified 228 STT_OBJECT = 1, // Symbol is a data object (variable, array, etc.) 229 STT_FUNC = 2, // Symbol is executable code (function, etc.) 230 STT_SECTION = 3, // Symbol refers to a section 231 STT_FILE = 4, // Local, absolute symbol that refers to a file 232 STT_LOPROC = 13, // Lowest processor-specific symbol type 233 STT_HIPROC = 15 // Highest processor-specific symbol type 234}; 235 236// Relocation entry, without explicit addend. 237struct Elf32_Rel { 238 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr) 239 Elf32_Word r_info; // Symbol table index and type of relocation to apply 240 241 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 242 // and ELF32_R_INFO macros defined in the ELF specification: 243 Elf32_Word getSymbol () const { return (r_info >> 8); } 244 unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); } 245 void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); } 246 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 247 void setSymbolAndType (Elf32_Word s, unsigned char t) { 248 r_info = (s << 8) + t; 249 }; 250}; 251 252// Relocation entry with explicit addend. 253struct Elf32_Rela { 254 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr) 255 Elf32_Word r_info; // Symbol table index and type of relocation to apply 256 Elf32_Sword r_addend; // Compute value for relocatable field by adding this 257 258 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 259 // and ELF32_R_INFO macros defined in the ELF specification: 260 Elf32_Word getSymbol () const { return (r_info >> 8); } 261 unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); } 262 void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); } 263 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 264 void setSymbolAndType (Elf32_Word s, unsigned char t) { 265 r_info = (s << 8) + t; 266 }; 267}; 268 269// Program header. 270struct Elf32_Phdr { 271 Elf32_Word p_type; // Type of segment 272 Elf32_Off p_offset; // File offset where segment is located, in bytes 273 Elf32_Addr p_vaddr; // Virtual address of beginning of segment 274 Elf32_Addr p_paddr; // Physical address of beginning of segment (OS-specific) 275 Elf32_Word p_filesz; // Num. of bytes in file image of segment (may be zero) 276 Elf32_Word p_memsz; // Num. of bytes in mem image of segment (may be zero) 277 Elf32_Word p_flags; // Segment flags 278 Elf32_Word p_align; // Segment alignment constraint 279}; 280 281enum { 282 PT_NULL = 0, // Unused segment. 283 PT_LOAD = 1, // Loadable segment. 284 PT_DYNAMIC = 2, // Dynamic linking information. 285 PT_INTERP = 3, // Interpreter pathname. 286 PT_NOTE = 4, // Auxiliary information. 287 PT_SHLIB = 5, // Reserved. 288 PT_PHDR = 6, // The program header table itself. 289 PT_LOPROC = 0x70000000, // Lowest processor-specific program hdr entry type. 290 PT_HIPROC = 0x7fffffff // Highest processor-specific program hdr entry type. 291}; 292 293} // end namespace ELF 294 295} // end namespace llvm 296