LLVM API Documentation
00001 //===-- llvm/Support/ELF.h - ELF constants and data structures --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This header contains common, non-processor-specific data structures and 00011 // constants for the ELF file format. 00012 // 00013 // The details of the ELF32 bits in this file are largely based on 00014 // the Tool Interface Standard (TIS) Executable and Linking Format 00015 // (ELF) Specification Version 1.2, May 1995. The ELF64 stuff is not 00016 // standardized, as far as I can tell. It was largely based on information 00017 // I found in OpenBSD header files. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #ifndef LLVM_SUPPORT_ELF_H 00022 #define LLVM_SUPPORT_ELF_H 00023 00024 #include "llvm/Support/DataTypes.h" 00025 #include <cstring> 00026 #include <cstdlib> 00027 00028 namespace llvm { 00029 00030 namespace ELF { 00031 00032 typedef uint32_t Elf32_Addr; // Program address 00033 typedef uint16_t Elf32_Half; 00034 typedef uint32_t Elf32_Off; // File offset 00035 typedef int32_t Elf32_Sword; 00036 typedef uint32_t Elf32_Word; 00037 00038 typedef uint64_t Elf64_Addr; 00039 typedef uint64_t Elf64_Off; 00040 typedef int32_t Elf64_Shalf; 00041 typedef int32_t Elf64_Sword; 00042 typedef uint32_t Elf64_Word; 00043 typedef int64_t Elf64_Sxword; 00044 typedef uint64_t Elf64_Xword; 00045 typedef uint32_t Elf64_Half; 00046 typedef uint16_t Elf64_Quarter; 00047 00048 // Object file magic string. 00049 static const char ElfMagic[] = { 0x7f, 'E', 'L', 'F', '\0' }; 00050 00051 struct Elf32_Ehdr { 00052 unsigned char e_ident[16]; // ELF Identification bytes 00053 Elf32_Half e_type; // Type of file (see ET_* below) 00054 Elf32_Half e_machine; // Required architecture for this file (see EM_*) 00055 Elf32_Word e_version; // Must be equal to 1 00056 Elf32_Addr e_entry; // Address to jump to in order to start program 00057 Elf32_Off e_phoff; // Program header table's file offset, in bytes 00058 Elf32_Off e_shoff; // Section header table's file offset, in bytes 00059 Elf32_Word e_flags; // Processor-specific flags 00060 Elf32_Half e_ehsize; // Size of ELF header, in bytes 00061 Elf32_Half e_phentsize; // Size of an entry in the program header table 00062 Elf32_Half e_phnum; // Number of entries in the program header table 00063 Elf32_Half e_shentsize; // Size of an entry in the section header table 00064 Elf32_Half e_shnum; // Number of entries in the section header table 00065 Elf32_Half e_shstrndx; // Sect hdr table index of sect name string table 00066 bool checkMagic () const { 00067 return (memcmp (e_ident, ElfMagic, strlen (ElfMagic))) == 0; 00068 } 00069 unsigned char getFileClass () const { return e_ident[4]; } 00070 unsigned char getDataEncoding () { return e_ident[5]; } 00071 }; 00072 00073 // 64-bit ELF header. Fields are the same as for ELF32, but with different 00074 // types (see above). 00075 struct Elf64_Ehdr { 00076 unsigned char e_ident[16]; 00077 Elf64_Quarter e_type; 00078 Elf64_Quarter e_machine; 00079 Elf64_Half e_version; 00080 Elf64_Addr e_entry; 00081 Elf64_Off e_phoff; 00082 Elf64_Off e_shoff; 00083 Elf64_Half e_flags; 00084 Elf64_Quarter e_ehsize; 00085 Elf64_Quarter e_phentsize; 00086 Elf64_Quarter e_phnum; 00087 Elf64_Quarter e_shentsize; 00088 Elf64_Quarter e_shnum; 00089 Elf64_Quarter e_shstrndx; 00090 }; 00091 00092 // File types 00093 enum { 00094 ET_NONE = 0, // No file type 00095 ET_REL = 1, // Relocatable file 00096 ET_EXEC = 2, // Executable file 00097 ET_DYN = 3, // Shared object file 00098 ET_CORE = 4, // Core file 00099 ET_LOPROC = 0xff00, // Beginning of processor-specific codes 00100 ET_HIPROC = 0xffff // Processor-specific 00101 }; 00102 00103 // Machine architectures 00104 enum { 00105 EM_NONE = 0, // No machine 00106 EM_M32 = 1, // AT&T WE 32100 00107 EM_SPARC = 2, // SPARC 00108 EM_386 = 3, // Intel 386 00109 EM_68K = 4, // Motorola 68000 00110 EM_88K = 5, // Motorola 88000 00111 EM_486 = 6, // Intel 486 (deprecated) 00112 EM_860 = 7, // Intel 80860 00113 EM_MIPS = 8, // MIPS R3000 00114 EM_PPC = 20, // PowerPC 00115 EM_ARM = 40, // ARM 00116 EM_ALPHA = 41, // DEC Alpha 00117 EM_SPARCV9 = 43 // SPARC V9 00118 }; 00119 00120 // Object file classes. 00121 enum { 00122 ELFCLASS32 = 1, // 32-bit object file 00123 ELFCLASS64 = 2 // 64-bit object file 00124 }; 00125 00126 // Object file byte orderings. 00127 enum { 00128 ELFDATA2LSB = 1, // Little-endian object file 00129 ELFDATA2MSB = 2 // Big-endian object file 00130 }; 00131 00132 // Section header. 00133 struct Elf32_Shdr { 00134 Elf32_Word sh_name; // Section name (index into string table) 00135 Elf32_Word sh_type; // Section type (SHT_*) 00136 Elf32_Word sh_flags; // Section flags (SHF_*) 00137 Elf32_Addr sh_addr; // Address where section is to be loaded 00138 Elf32_Off sh_offset; // File offset of section data, in bytes 00139 Elf32_Word sh_size; // Size of section, in bytes 00140 Elf32_Word sh_link; // Section type-specific header table index link 00141 Elf32_Word sh_info; // Section type-specific extra information 00142 Elf32_Word sh_addralign; // Section address alignment 00143 Elf32_Word sh_entsize; // Size of records contained within the section 00144 }; 00145 00146 // Section header for ELF64 - same fields as ELF32, different types. 00147 struct Elf64_Shdr { 00148 Elf64_Half sh_name; 00149 Elf64_Half sh_type; 00150 Elf64_Xword sh_flags; 00151 Elf64_Addr sh_addr; 00152 Elf64_Off sh_offset; 00153 Elf64_Xword sh_size; 00154 Elf64_Half sh_link; 00155 Elf64_Half sh_info; 00156 Elf64_Xword sh_addralign; 00157 Elf64_Xword sh_entsize; 00158 }; 00159 00160 // Special section indices. 00161 enum { 00162 SHN_UNDEF = 0, // Undefined, missing, irrelevant, or meaningless 00163 SHN_LORESERVE = 0xff00, // Lowest reserved index 00164 SHN_LOPROC = 0xff00, // Lowest processor-specific index 00165 SHN_HIPROC = 0xff1f, // Highest processor-specific index 00166 SHN_ABS = 0xfff1, // Symbol has absolute value; does not need relocation 00167 SHN_COMMON = 0xfff2, // FORTRAN COMMON or C external global variables 00168 SHN_HIRESERVE = 0xffff // Highest reserved index 00169 }; 00170 00171 // Section types. 00172 enum { 00173 SHT_NULL = 0, // No associated section (inactive entry). 00174 SHT_PROGBITS = 1, // Program-defined contents. 00175 SHT_SYMTAB = 2, // Symbol table. 00176 SHT_STRTAB = 3, // String table. 00177 SHT_RELA = 4, // Relocation entries; explicit addends. 00178 SHT_HASH = 5, // Symbol hash table. 00179 SHT_DYNAMIC = 6, // Information for dynamic linking. 00180 SHT_NOTE = 7, // Information about the file. 00181 SHT_NOBITS = 8, // Data occupies no space in the file. 00182 SHT_REL = 9, // Relocation entries; no explicit addends. 00183 SHT_SHLIB = 10, // Reserved. 00184 SHT_DYNSYM = 11, // Symbol table. 00185 SHT_LOPROC = 0x70000000, // Lowest processor architecture-specific type. 00186 SHT_HIPROC = 0x7fffffff, // Highest processor architecture-specific type. 00187 SHT_LOUSER = 0x80000000, // Lowest type reserved for applications. 00188 SHT_HIUSER = 0xffffffff // Highest type reserved for applications. 00189 }; 00190 00191 // Section flags. 00192 enum { 00193 SHF_WRITE = 0x1, // Section data should be writable during execution. 00194 SHF_ALLOC = 0x2, // Section occupies memory during program execution. 00195 SHF_EXECINSTR = 0x4, // Section contains executable machine instructions. 00196 SHF_MASKPROC = 0xf0000000 // Bits indicating processor-specific flags. 00197 }; 00198 00199 // Symbol table entries. 00200 struct Elf32_Sym { 00201 Elf32_Word st_name; // Symbol name (index into string table) 00202 Elf32_Addr st_value; // Value or address associated with the symbol 00203 Elf32_Word st_size; // Size of the symbol 00204 unsigned char st_info; // Symbol's type and binding attributes 00205 unsigned char st_other; // Must be zero; reserved 00206 Elf32_Half st_shndx; // Which section (header table index) it's defined in 00207 00208 // These accessors and mutators correspond to the ELF32_ST_BIND, 00209 // ELF32_ST_TYPE, and ELF32_ST_INFO macros defined in the ELF specification: 00210 unsigned char getBinding () const { return st_info >> 4; } 00211 unsigned char getType () const { return st_info & 0x0f; } 00212 void setBinding (unsigned char b) { setBindingAndType (b, getType ()); } 00213 void setType (unsigned char t) { setBindingAndType (getBinding (), t); } 00214 void setBindingAndType (unsigned char b, unsigned char t) { 00215 st_info = (b << 4) + (t & 0x0f); 00216 } 00217 }; 00218 00219 // Symbol bindings. 00220 enum { 00221 STB_LOCAL = 0, // Local symbol, not visible outside obj file containing def 00222 STB_GLOBAL = 1, // Global symbol, visible to all object files being combined 00223 STB_WEAK = 2, // Weak symbol, like global but lower-precedence 00224 STB_LOPROC = 13, // Lowest processor-specific binding type 00225 STB_HIPROC = 15 // Highest processor-specific binding type 00226 }; 00227 00228 // Symbol types. 00229 enum { 00230 STT_NOTYPE = 0, // Symbol's type is not specified 00231 STT_OBJECT = 1, // Symbol is a data object (variable, array, etc.) 00232 STT_FUNC = 2, // Symbol is executable code (function, etc.) 00233 STT_SECTION = 3, // Symbol refers to a section 00234 STT_FILE = 4, // Local, absolute symbol that refers to a file 00235 STT_LOPROC = 13, // Lowest processor-specific symbol type 00236 STT_HIPROC = 15 // Highest processor-specific symbol type 00237 }; 00238 00239 // Relocation entry, without explicit addend. 00240 struct Elf32_Rel { 00241 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr) 00242 Elf32_Word r_info; // Symbol table index and type of relocation to apply 00243 00244 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 00245 // and ELF32_R_INFO macros defined in the ELF specification: 00246 Elf32_Word getSymbol () const { return (r_info >> 8); } 00247 unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); } 00248 void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); } 00249 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 00250 void setSymbolAndType (Elf32_Word s, unsigned char t) { 00251 r_info = (s << 8) + t; 00252 }; 00253 }; 00254 00255 // Relocation entry with explicit addend. 00256 struct Elf32_Rela { 00257 Elf32_Addr r_offset; // Location (file byte offset, or program virtual addr) 00258 Elf32_Word r_info; // Symbol table index and type of relocation to apply 00259 Elf32_Sword r_addend; // Compute value for relocatable field by adding this 00260 00261 // These accessors and mutators correspond to the ELF32_R_SYM, ELF32_R_TYPE, 00262 // and ELF32_R_INFO macros defined in the ELF specification: 00263 Elf32_Word getSymbol () const { return (r_info >> 8); } 00264 unsigned char getType () const { return (unsigned char) (r_info & 0x0ff); } 00265 void setSymbol (Elf32_Word s) { setSymbolAndType (s, getType ()); } 00266 void setType (unsigned char t) { setSymbolAndType (getSymbol(), t); } 00267 void setSymbolAndType (Elf32_Word s, unsigned char t) { 00268 r_info = (s << 8) + t; 00269 }; 00270 }; 00271 00272 // Program header. 00273 struct Elf32_Phdr { 00274 Elf32_Word p_type; // Type of segment 00275 Elf32_Off p_offset; // File offset where segment is located, in bytes 00276 Elf32_Addr p_vaddr; // Virtual address of beginning of segment 00277 Elf32_Addr p_paddr; // Physical address of beginning of segment (OS-specific) 00278 Elf32_Word p_filesz; // Num. of bytes in file image of segment (may be zero) 00279 Elf32_Word p_memsz; // Num. of bytes in mem image of segment (may be zero) 00280 Elf32_Word p_flags; // Segment flags 00281 Elf32_Word p_align; // Segment alignment constraint 00282 }; 00283 00284 enum { 00285 PT_NULL = 0, // Unused segment. 00286 PT_LOAD = 1, // Loadable segment. 00287 PT_DYNAMIC = 2, // Dynamic linking information. 00288 PT_INTERP = 3, // Interpreter pathname. 00289 PT_NOTE = 4, // Auxiliary information. 00290 PT_SHLIB = 5, // Reserved. 00291 PT_PHDR = 6, // The program header table itself. 00292 PT_LOPROC = 0x70000000, // Lowest processor-specific program hdr entry type. 00293 PT_HIPROC = 0x7fffffff // Highest processor-specific program hdr entry type. 00294 }; 00295 00296 } // end namespace ELF 00297 00298 } // end namespace llvm 00299 00300 #endif