LLVM API Documentation

ELF.h

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



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.