Home | History | Annotate | Download | only in CodeGen
      1 //===-- lib/CodeGen/ELF.h - ELF constants and data structures ---*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // 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 the Tool
     14 // Interface Standard (TIS) Executable and Linking Format (ELF) Specification
     15 // Version 1.2, May 1995. The ELF64 is based on HP/Intel definition of the
     16 // ELF-64 object file format document, Version 1.5 Draft 2 May 27, 1998
     17 //
     18 //===----------------------------------------------------------------------===//
     19 
     20 #ifndef CODEGEN_ELF_H
     21 #define CODEGEN_ELF_H
     22 
     23 #include "llvm/CodeGen/BinaryObject.h"
     24 #include "llvm/CodeGen/MachineRelocation.h"
     25 #include "llvm/Support/ELF.h"
     26 #include "llvm/Support/DataTypes.h"
     27 
     28 namespace llvm {
     29   class GlobalValue;
     30 
     31   /// ELFSym - This struct contains information about each symbol that is
     32   /// added to logical symbol table for the module.  This is eventually
     33   /// turned into a real symbol table in the file.
     34   struct ELFSym {
     35 
     36     // ELF symbols are related to llvm ones by being one of the two llvm
     37     // types, for the other ones (section, file, func) a null pointer is
     38     // assumed by default.
     39     union {
     40       const GlobalValue *GV;  // If this is a pointer to a GV
     41       const char *Ext;        // If this is a pointer to a named symbol
     42     } Source;
     43 
     44     // Describes from which source type this ELF symbol comes from,
     45     // they can be GlobalValue, ExternalSymbol or neither.
     46     enum {
     47       isGV,      // The Source.GV field is valid.
     48       isExtSym,  // The Source.ExtSym field is valid.
     49       isOther    // Not a GlobalValue or External Symbol
     50     };
     51     unsigned SourceType;
     52 
     53     bool isGlobalValue() const { return SourceType == isGV; }
     54     bool isExternalSym() const { return SourceType == isExtSym; }
     55 
     56     // getGlobalValue - If this is a global value which originated the
     57     // elf symbol, return a reference to it.
     58     const GlobalValue *getGlobalValue() const {
     59       assert(SourceType == isGV && "This is not a global value");
     60       return Source.GV;
     61     }
     62 
     63     // getExternalSym - If this is an external symbol which originated the
     64     // elf symbol, return a reference to it.
     65     const char *getExternalSymbol() const {
     66       assert(SourceType == isExtSym && "This is not an external symbol");
     67       return Source.Ext;
     68     }
     69 
     70     // getGV - From a global value return a elf symbol to represent it
     71     static ELFSym *getGV(const GlobalValue *GV, unsigned Bind,
     72                          unsigned Type, unsigned Visibility) {
     73       ELFSym *Sym = new ELFSym();
     74       Sym->Source.GV = GV;
     75       Sym->setBind(Bind);
     76       Sym->setType(Type);
     77       Sym->setVisibility(Visibility);
     78       Sym->SourceType = isGV;
     79       return Sym;
     80     }
     81 
     82     // getExtSym - Create and return an elf symbol to represent an
     83     // external symbol
     84     static ELFSym *getExtSym(const char *Ext) {
     85       ELFSym *Sym = new ELFSym();
     86       Sym->Source.Ext = Ext;
     87       Sym->setBind(ELF::STB_GLOBAL);
     88       Sym->setType(ELF::STT_NOTYPE);
     89       Sym->setVisibility(ELF::STV_DEFAULT);
     90       Sym->SourceType = isExtSym;
     91       return Sym;
     92     }
     93 
     94     // getSectionSym - Returns a elf symbol to represent an elf section
     95     static ELFSym *getSectionSym() {
     96       ELFSym *Sym = new ELFSym();
     97       Sym->setBind(ELF::STB_LOCAL);
     98       Sym->setType(ELF::STT_SECTION);
     99       Sym->setVisibility(ELF::STV_DEFAULT);
    100       Sym->SourceType = isOther;
    101       return Sym;
    102     }
    103 
    104     // getFileSym - Returns a elf symbol to represent the module identifier
    105     static ELFSym *getFileSym() {
    106       ELFSym *Sym = new ELFSym();
    107       Sym->setBind(ELF::STB_LOCAL);
    108       Sym->setType(ELF::STT_FILE);
    109       Sym->setVisibility(ELF::STV_DEFAULT);
    110       Sym->SectionIdx = 0xfff1;  // ELFSection::SHN_ABS;
    111       Sym->SourceType = isOther;
    112       return Sym;
    113     }
    114 
    115     // getUndefGV - Returns a STT_NOTYPE symbol
    116     static ELFSym *getUndefGV(const GlobalValue *GV, unsigned Bind) {
    117       ELFSym *Sym = new ELFSym();
    118       Sym->Source.GV = GV;
    119       Sym->setBind(Bind);
    120       Sym->setType(ELF::STT_NOTYPE);
    121       Sym->setVisibility(ELF::STV_DEFAULT);
    122       Sym->SectionIdx = 0;  //ELFSection::SHN_UNDEF;
    123       Sym->SourceType = isGV;
    124       return Sym;
    125     }
    126 
    127     // ELF specific fields
    128     unsigned NameIdx;         // Index in .strtab of name, once emitted.
    129     uint64_t Value;
    130     unsigned Size;
    131     uint8_t Info;
    132     uint8_t Other;
    133     unsigned short SectionIdx;
    134 
    135     // Symbol index into the Symbol table
    136     unsigned SymTabIdx;
    137 
    138     ELFSym() : SourceType(isOther), NameIdx(0), Value(0),
    139                Size(0), Info(0), Other(ELF::STV_DEFAULT), SectionIdx(0),
    140                SymTabIdx(0) {}
    141 
    142     unsigned getBind() const { return (Info >> 4) & 0xf; }
    143     unsigned getType() const { return Info & 0xf; }
    144     bool isLocalBind() const { return getBind() == ELF::STB_LOCAL; }
    145     bool isFileType() const { return getType() == ELF::STT_FILE; }
    146 
    147     void setBind(unsigned X) {
    148       assert(X == (X & 0xF) && "Bind value out of range!");
    149       Info = (Info & 0x0F) | (X << 4);
    150     }
    151 
    152     void setType(unsigned X) {
    153       assert(X == (X & 0xF) && "Type value out of range!");
    154       Info = (Info & 0xF0) | X;
    155     }
    156 
    157     void setVisibility(unsigned V) {
    158       assert(V == (V & 0x3) && "Visibility value out of range!");
    159       Other = V;
    160     }
    161   };
    162 
    163   /// ELFSection - This struct contains information about each section that is
    164   /// emitted to the file.  This is eventually turned into the section header
    165   /// table at the end of the file.
    166   class ELFSection : public BinaryObject {
    167     public:
    168     // ELF specific fields
    169     unsigned NameIdx;   // sh_name - .shstrtab idx of name, once emitted.
    170     unsigned Type;      // sh_type - Section contents & semantics
    171     unsigned Flags;     // sh_flags - Section flags.
    172     uint64_t Addr;      // sh_addr - The mem addr this section is in.
    173     unsigned Offset;    // sh_offset - Offset from the file start
    174     unsigned Size;      // sh_size - The section size.
    175     unsigned Link;      // sh_link - Section header table index link.
    176     unsigned Info;      // sh_info - Auxiliary information.
    177     unsigned Align;     // sh_addralign - Alignment of section.
    178     unsigned EntSize;   // sh_entsize - Size of entries in the section e
    179 
    180     /// SectionIdx - The number of the section in the Section Table.
    181     unsigned short SectionIdx;
    182 
    183     /// Sym - The symbol to represent this section if it has one.
    184     ELFSym *Sym;
    185 
    186     /// getSymIndex - Returns the symbol table index of the symbol
    187     /// representing this section.
    188     unsigned getSymbolTableIndex() const {
    189       assert(Sym && "section not present in the symbol table");
    190       return Sym->SymTabIdx;
    191     }
    192 
    193     ELFSection(const std::string &name, bool isLittleEndian, bool is64Bit)
    194       : BinaryObject(name, isLittleEndian, is64Bit), Type(0), Flags(0), Addr(0),
    195         Offset(0), Size(0), Link(0), Info(0), Align(0), EntSize(0), Sym(0) {}
    196   };
    197 
    198   /// ELFRelocation - This class contains all the information necessary to
    199   /// to generate any 32-bit or 64-bit ELF relocation entry.
    200   class ELFRelocation {
    201     uint64_t r_offset;    // offset in the section of the object this applies to
    202     uint32_t r_symidx;    // symbol table index of the symbol to use
    203     uint32_t r_type;      // machine specific relocation type
    204     int64_t  r_add;       // explicit relocation addend
    205     bool     r_rela;      // if true then the addend is part of the entry
    206                           // otherwise the addend is at the location specified
    207                           // by r_offset
    208   public:
    209     uint64_t getInfo(bool is64Bit) const {
    210       if (is64Bit)
    211         return ((uint64_t)r_symidx << 32) + ((uint64_t)r_type & 0xFFFFFFFFL);
    212       else
    213         return (r_symidx << 8)  + (r_type & 0xFFL);
    214     }
    215 
    216     uint64_t getOffset() const { return r_offset; }
    217     int64_t getAddend() const { return r_add; }
    218 
    219     ELFRelocation(uint64_t off, uint32_t sym, uint32_t type,
    220                   bool rela = true, int64_t addend = 0) :
    221       r_offset(off), r_symidx(sym), r_type(type),
    222       r_add(addend), r_rela(rela) {}
    223   };
    224 
    225 } // end namespace llvm
    226 
    227 #endif
    228