LLVM API Documentation

lib/CodeGen/ELF.h

Go to the documentation of this file.
00001 //===-- lib/CodeGen/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 the Tool
00014 // Interface Standard (TIS) Executable and Linking Format (ELF) Specification
00015 // Version 1.2, May 1995. The ELF64 is based on HP/Intel definition of the
00016 // ELF-64 object file format document, Version 1.5 Draft 2 May 27, 1998
00017 //
00018 //===----------------------------------------------------------------------===//
00019 
00020 #ifndef CODEGEN_ELF_H
00021 #define CODEGEN_ELF_H
00022 
00023 #include "llvm/CodeGen/BinaryObject.h"
00024 #include "llvm/CodeGen/MachineRelocation.h"
00025 #include "llvm/Support/DataTypes.h"
00026 
00027 namespace llvm {
00028   class GlobalValue;
00029 
00030   // Identification Indexes
00031   enum {
00032     EI_MAG0 = 0,
00033     EI_MAG1 = 1,
00034     EI_MAG2 = 2,
00035     EI_MAG3 = 3
00036   };
00037 
00038   // File types
00039   enum {
00040     ET_NONE   = 0,      // No file type
00041     ET_REL    = 1,      // Relocatable file
00042     ET_EXEC   = 2,      // Executable file
00043     ET_DYN    = 3,      // Shared object file
00044     ET_CORE   = 4,      // Core file
00045     ET_LOPROC = 0xff00, // Beginning of processor-specific codes
00046     ET_HIPROC = 0xffff  // Processor-specific
00047   };
00048 
00049   // Versioning
00050   enum {
00051     EV_NONE = 0,
00052     EV_CURRENT = 1
00053   };
00054 
00055   /// ELFSym - This struct contains information about each symbol that is
00056   /// added to logical symbol table for the module.  This is eventually
00057   /// turned into a real symbol table in the file.
00058   struct ELFSym {
00059 
00060     // ELF symbols are related to llvm ones by being one of the two llvm
00061     // types, for the other ones (section, file, func) a null pointer is
00062     // assumed by default.
00063     union {
00064       const GlobalValue *GV;  // If this is a pointer to a GV
00065       const char *Ext;        // If this is a pointer to a named symbol
00066     } Source;
00067 
00068     // Describes from which source type this ELF symbol comes from,
00069     // they can be GlobalValue, ExternalSymbol or neither.
00070     enum {
00071       isGV,      // The Source.GV field is valid.
00072       isExtSym,  // The Source.ExtSym field is valid.
00073       isOther    // Not a GlobalValue or External Symbol
00074     };
00075     unsigned SourceType;
00076 
00077     bool isGlobalValue() const { return SourceType == isGV; }
00078     bool isExternalSym() const { return SourceType == isExtSym; }
00079 
00080     // getGlobalValue - If this is a global value which originated the
00081     // elf symbol, return a reference to it.
00082     const GlobalValue *getGlobalValue() const {
00083       assert(SourceType == isGV && "This is not a global value");
00084       return Source.GV;
00085     };
00086 
00087     // getExternalSym - If this is an external symbol which originated the
00088     // elf symbol, return a reference to it.
00089     const char *getExternalSymbol() const {
00090       assert(SourceType == isExtSym && "This is not an external symbol");
00091       return Source.Ext;
00092     };
00093 
00094     // getGV - From a global value return a elf symbol to represent it
00095     static ELFSym *getGV(const GlobalValue *GV, unsigned Bind,
00096                          unsigned Type, unsigned Visibility) {
00097       ELFSym *Sym = new ELFSym();
00098       Sym->Source.GV = GV;
00099       Sym->setBind(Bind);
00100       Sym->setType(Type);
00101       Sym->setVisibility(Visibility);
00102       Sym->SourceType = isGV;
00103       return Sym;
00104     }
00105 
00106     // getExtSym - Create and return an elf symbol to represent an
00107     // external symbol
00108     static ELFSym *getExtSym(const char *Ext) {
00109       ELFSym *Sym = new ELFSym();
00110       Sym->Source.Ext = Ext;
00111       Sym->setBind(STB_GLOBAL);
00112       Sym->setType(STT_NOTYPE);
00113       Sym->setVisibility(STV_DEFAULT);
00114       Sym->SourceType = isExtSym;
00115       return Sym;
00116     }
00117 
00118     // getSectionSym - Returns a elf symbol to represent an elf section
00119     static ELFSym *getSectionSym() {
00120       ELFSym *Sym = new ELFSym();
00121       Sym->setBind(STB_LOCAL);
00122       Sym->setType(STT_SECTION);
00123       Sym->setVisibility(STV_DEFAULT);
00124       Sym->SourceType = isOther;
00125       return Sym;
00126     }
00127 
00128     // getFileSym - Returns a elf symbol to represent the module identifier
00129     static ELFSym *getFileSym() {
00130       ELFSym *Sym = new ELFSym();
00131       Sym->setBind(STB_LOCAL);
00132       Sym->setType(STT_FILE);
00133       Sym->setVisibility(STV_DEFAULT);
00134       Sym->SectionIdx = 0xfff1;  // ELFSection::SHN_ABS;
00135       Sym->SourceType = isOther;
00136       return Sym;
00137     }
00138 
00139     // getUndefGV - Returns a STT_NOTYPE symbol
00140     static ELFSym *getUndefGV(const GlobalValue *GV, unsigned Bind) {
00141       ELFSym *Sym = new ELFSym();
00142       Sym->Source.GV = GV;
00143       Sym->setBind(Bind);
00144       Sym->setType(STT_NOTYPE);
00145       Sym->setVisibility(STV_DEFAULT);
00146       Sym->SectionIdx = 0;  //ELFSection::SHN_UNDEF;
00147       Sym->SourceType = isGV;
00148       return Sym;
00149     }
00150 
00151     // ELF specific fields
00152     unsigned NameIdx;         // Index in .strtab of name, once emitted.
00153     uint64_t Value;
00154     unsigned Size;
00155     uint8_t Info;
00156     uint8_t Other;
00157     unsigned short SectionIdx;
00158 
00159     // Symbol index into the Symbol table
00160     unsigned SymTabIdx;
00161 
00162     enum {
00163       STB_LOCAL = 0,  // Local sym, not visible outside obj file containing def
00164       STB_GLOBAL = 1, // Global sym, visible to all object files being combined
00165       STB_WEAK = 2    // Weak symbol, like global but lower-precedence
00166     };
00167 
00168     enum {
00169       STT_NOTYPE = 0,  // Symbol's type is not specified
00170       STT_OBJECT = 1,  // Symbol is a data object (variable, array, etc.)
00171       STT_FUNC = 2,    // Symbol is executable code (function, etc.)
00172       STT_SECTION = 3, // Symbol refers to a section
00173       STT_FILE = 4     // Local, absolute symbol that refers to a file
00174     };
00175 
00176     enum {
00177       STV_DEFAULT = 0,  // Visibility is specified by binding type
00178       STV_INTERNAL = 1, // Defined by processor supplements
00179       STV_HIDDEN = 2,   // Not visible to other components
00180       STV_PROTECTED = 3 // Visible in other components but not preemptable
00181     };
00182 
00183     ELFSym() : SourceType(isOther), NameIdx(0), Value(0),
00184                Size(0), Info(0), Other(STV_DEFAULT), SectionIdx(0),
00185                SymTabIdx(0) {}
00186 
00187     unsigned getBind() const { return (Info >> 4) & 0xf; }
00188     unsigned getType() const { return Info & 0xf; }
00189     bool isLocalBind() const { return getBind() == STB_LOCAL; }
00190     bool isFileType() const { return getType() == STT_FILE; }
00191 
00192     void setBind(unsigned X) {
00193       assert(X == (X & 0xF) && "Bind value out of range!");
00194       Info = (Info & 0x0F) | (X << 4);
00195     }
00196 
00197     void setType(unsigned X) {
00198       assert(X == (X & 0xF) && "Type value out of range!");
00199       Info = (Info & 0xF0) | X;
00200     }
00201 
00202     void setVisibility(unsigned V) {
00203       assert(V == (V & 0x3) && "Visibility value out of range!");
00204       Other = V;
00205     }
00206   };
00207 
00208   /// ELFSection - This struct contains information about each section that is
00209   /// emitted to the file.  This is eventually turned into the section header
00210   /// table at the end of the file.
00211   class ELFSection : public BinaryObject {
00212     public:
00213     // ELF specific fields
00214     unsigned NameIdx;   // sh_name - .shstrtab idx of name, once emitted.
00215     unsigned Type;      // sh_type - Section contents & semantics 
00216     unsigned Flags;     // sh_flags - Section flags.
00217     uint64_t Addr;      // sh_addr - The mem addr this section is in.
00218     unsigned Offset;    // sh_offset - Offset from the file start
00219     unsigned Size;      // sh_size - The section size.
00220     unsigned Link;      // sh_link - Section header table index link.
00221     unsigned Info;      // sh_info - Auxillary information.
00222     unsigned Align;     // sh_addralign - Alignment of section.
00223     unsigned EntSize;   // sh_entsize - Size of entries in the section e
00224 
00225     // Section Header Flags
00226     enum {
00227       SHF_WRITE            = 1 << 0, // Writable
00228       SHF_ALLOC            = 1 << 1, // Mapped into the process addr space
00229       SHF_EXECINSTR        = 1 << 2, // Executable
00230       SHF_MERGE            = 1 << 4, // Might be merged if equal
00231       SHF_STRINGS          = 1 << 5, // Contains null-terminated strings
00232       SHF_INFO_LINK        = 1 << 6, // 'sh_info' contains SHT index
00233       SHF_LINK_ORDER       = 1 << 7, // Preserve order after combining
00234       SHF_OS_NONCONFORMING = 1 << 8, // nonstandard OS support required
00235       SHF_GROUP            = 1 << 9, // Section is a member of a group
00236       SHF_TLS              = 1 << 10 // Section holds thread-local data
00237     };
00238 
00239     // Section Types
00240     enum {
00241       SHT_NULL     = 0,  // No associated section (inactive entry).
00242       SHT_PROGBITS = 1,  // Program-defined contents.
00243       SHT_SYMTAB   = 2,  // Symbol table.
00244       SHT_STRTAB   = 3,  // String table.
00245       SHT_RELA     = 4,  // Relocation entries; explicit addends.
00246       SHT_HASH     = 5,  // Symbol hash table.
00247       SHT_DYNAMIC  = 6,  // Information for dynamic linking.
00248       SHT_NOTE     = 7,  // Information about the file.
00249       SHT_NOBITS   = 8,  // Data occupies no space in the file.
00250       SHT_REL      = 9,  // Relocation entries; no explicit addends.
00251       SHT_SHLIB    = 10, // Reserved.
00252       SHT_DYNSYM   = 11, // Symbol table.
00253       SHT_LOPROC   = 0x70000000, // Lowest processor arch-specific type.
00254       SHT_HIPROC   = 0x7fffffff, // Highest processor arch-specific type.
00255       SHT_LOUSER   = 0x80000000, // Lowest type reserved for applications.
00256       SHT_HIUSER   = 0xffffffff  // Highest type reserved for applications.
00257     };
00258 
00259     // Special section indices.
00260     enum {
00261       SHN_UNDEF     = 0,      // Undefined, missing, irrelevant
00262       SHN_LORESERVE = 0xff00, // Lowest reserved index
00263       SHN_LOPROC    = 0xff00, // Lowest processor-specific index
00264       SHN_HIPROC    = 0xff1f, // Highest processor-specific index
00265       SHN_ABS       = 0xfff1, // Symbol has absolute value; no relocation
00266       SHN_COMMON    = 0xfff2, // FORTRAN COMMON or C external global variables
00267       SHN_HIRESERVE = 0xffff  // Highest reserved index
00268     };
00269 
00270     /// SectionIdx - The number of the section in the Section Table.
00271     unsigned short SectionIdx;
00272 
00273     /// Sym - The symbol to represent this section if it has one.
00274     ELFSym *Sym;
00275 
00276     /// getSymIndex - Returns the symbol table index of the symbol
00277     /// representing this section.
00278     unsigned getSymbolTableIndex() const {
00279       assert(Sym && "section not present in the symbol table");
00280       return Sym->SymTabIdx;
00281     }
00282 
00283     ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
00284       : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
00285         Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
00286   };
00287 
00288   /// ELFRelocation - This class contains all the information necessary to
00289   /// to generate any 32-bit or 64-bit ELF relocation entry.
00290   class ELFRelocation {
00291     uint64_t r_offset;    // offset in the section of the object this applies to
00292     uint32_t r_symidx;    // symbol table index of the symbol to use
00293     uint32_t r_type;      // machine specific relocation type
00294     int64_t  r_add;       // explicit relocation addend
00295     bool     r_rela;      // if true then the addend is part of the entry
00296                           // otherwise the addend is at the location specified
00297                           // by r_offset
00298   public:
00299     uint64_t getInfo(bool is64Bit) const {
00300       if (is64Bit)
00301         return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
00302       else
00303         return (r_symidx << 8)  + (r_type & 0xFFL);
00304     }
00305 
00306     uint64_t getOffset() const { return r_offset; }
00307     int64_t getAddend() const { return r_add; }
00308 
00309     ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
00310                   bool rela = true, int64_t addend = 0) :
00311       r_offset(off), r_symidx(sym), r_type(type),
00312       r_add(addend), r_rela(rela) {}
00313   };
00314 
00315 } // end namespace llvm
00316 
00317 #endif