LLVM API Documentation

ELFWriter.h

Go to the documentation of this file.
00001 //===-- ELFWriter.h - Target-independent ELF writer support -----*- 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 file defines the ELFWriter class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef ELFWRITER_H
00015 #define ELFWRITER_H
00016 
00017 #include "llvm/ADT/SetVector.h"
00018 #include "llvm/CodeGen/MachineFunctionPass.h"
00019 #include <map>
00020 
00021 namespace llvm {
00022   class BinaryObject;
00023   class Constant;
00024   class ConstantInt;
00025   class ConstantStruct;
00026   class ELFCodeEmitter;
00027   class ELFRelocation;
00028   class ELFSection;
00029   struct ELFSym;
00030   class GlobalVariable;
00031   class Mangler;
00032   class MachineCodeEmitter;
00033   class MachineConstantPoolEntry;
00034   class ObjectCodeEmitter;
00035   class TargetAsmInfo;
00036   class TargetELFWriterInfo;
00037   class TargetLoweringObjectFile;
00038   class raw_ostream;
00039   class SectionKind;
00040   class MCContext;
00041 
00042   typedef std::vector<ELFSym*>::iterator ELFSymIter;
00043   typedef std::vector<ELFSection*>::iterator ELFSectionIter;
00044   typedef SetVector<const GlobalValue*>::const_iterator PendingGblsIter;
00045   typedef SetVector<const char *>::const_iterator PendingExtsIter;
00046   typedef std::pair<const Constant *, int64_t> CstExprResTy;
00047 
00048   /// ELFWriter - This class implements the common target-independent code for
00049   /// writing ELF files.  Targets should derive a class from this to
00050   /// parameterize the output format.
00051   ///
00052   class ELFWriter : public MachineFunctionPass {
00053     friend class ELFCodeEmitter;
00054   public:
00055     static char ID;
00056 
00057     /// Return the ELFCodeEmitter as an instance of ObjectCodeEmitter
00058     ObjectCodeEmitter *getObjectCodeEmitter() {
00059       return reinterpret_cast<ObjectCodeEmitter*>(ElfCE);
00060     }
00061 
00062     ELFWriter(raw_ostream &O, TargetMachine &TM);
00063     ~ELFWriter();
00064 
00065   protected:
00066     /// Output stream to send the resultant object file to.
00067     raw_ostream &O;
00068 
00069     /// Target machine description.
00070     TargetMachine &TM;
00071 
00072     /// Context object for machine code objects.
00073     MCContext &OutContext;
00074     
00075     /// Target Elf Writer description.
00076     const TargetELFWriterInfo *TEW;
00077 
00078     /// Mang - The object used to perform name mangling for this module.
00079     Mangler *Mang;
00080 
00081     /// MCE - The MachineCodeEmitter object that we are exposing to emit machine
00082     /// code for functions to the .o file.
00083     ELFCodeEmitter *ElfCE;
00084 
00085     /// TLOF - Target Lowering Object File, provide section names for globals 
00086     /// and other object file specific stuff
00087     const TargetLoweringObjectFile &TLOF;
00088 
00089     /// TAI - Target Asm Info, provide information about section names for
00090     /// globals and other target specific stuff.
00091     const TargetAsmInfo *TAI;
00092 
00093     //===------------------------------------------------------------------===//
00094     // Properties inferred automatically from the target machine.
00095     //===------------------------------------------------------------------===//
00096 
00097     /// is64Bit/isLittleEndian - This information is inferred from the target
00098     /// machine directly, indicating whether to emit a 32- or 64-bit ELF file.
00099     bool is64Bit, isLittleEndian;
00100 
00101     /// doInitialization - Emit the file header and all of the global variables
00102     /// for the module to the ELF file.
00103     bool doInitialization(Module &M);
00104     bool runOnMachineFunction(MachineFunction &MF);
00105 
00106     /// doFinalization - Now that the module has been completely processed, emit
00107     /// the ELF file to 'O'.
00108     bool doFinalization(Module &M);
00109 
00110   private:
00111     /// Blob containing the Elf header
00112     BinaryObject ElfHdr;
00113 
00114     /// SectionList - This is the list of sections that we have emitted to the
00115     /// file. Once the file has been completely built, the section header table
00116     /// is constructed from this info.
00117     std::vector<ELFSection*> SectionList;
00118     unsigned NumSections;   // Always = SectionList.size()
00119 
00120     /// SectionLookup - This is a mapping from section name to section number in
00121     /// the SectionList. Used to quickly gather the Section Index from TAI names
00122     std::map<std::string, ELFSection*> SectionLookup;
00123 
00124     /// PendingGlobals - Globals not processed as symbols yet.
00125     SetVector<const GlobalValue*> PendingGlobals;
00126 
00127     /// GblSymLookup - This is a mapping from global value to a symbol index
00128     /// in the symbol table or private symbols list. This is useful since reloc
00129     /// symbol references must be quickly mapped to their indices on the lists.
00130     std::map<const GlobalValue*, uint32_t> GblSymLookup;
00131 
00132     /// PendingExternals - Externals not processed as symbols yet.
00133     SetVector<const char *> PendingExternals;
00134 
00135     /// ExtSymLookup - This is a mapping from externals to a symbol index
00136     /// in the symbol table list. This is useful since reloc symbol references
00137     /// must be quickly mapped to their symbol table indices.
00138     std::map<const char *, uint32_t> ExtSymLookup;
00139 
00140     /// SymbolList - This is the list of symbols emitted to the symbol table.
00141     /// When the SymbolList is finally built, local symbols must be placed in
00142     /// the beginning while non-locals at the end.
00143     std::vector<ELFSym*> SymbolList;
00144 
00145     /// PrivateSyms - Record private symbols, every symbol here must never be
00146     /// present in the SymbolList.
00147     std::vector<ELFSym*> PrivateSyms;
00148 
00149     /// getSection - Return the section with the specified name, creating a new
00150     /// section if one does not already exist.
00151     ELFSection &getSection(const std::string &Name, unsigned Type,
00152                            unsigned Flags = 0, unsigned Align = 0) {
00153       ELFSection *&SN = SectionLookup[Name];
00154       if (SN) return *SN;
00155 
00156       SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
00157       SN = SectionList.back();
00158       SN->SectionIdx = NumSections++;
00159       SN->Type = Type;
00160       SN->Flags = Flags;
00161       SN->Link = ELFSection::SHN_UNDEF;
00162       SN->Align = Align;
00163       return *SN;
00164     }
00165 
00166     ELFSection &getNonExecStackSection() {
00167       return getSection(".note.GNU-stack", ELFSection::SHT_PROGBITS, 0, 1);
00168     }
00169 
00170     ELFSection &getSymbolTableSection() {
00171       return getSection(".symtab", ELFSection::SHT_SYMTAB, 0);
00172     }
00173 
00174     ELFSection &getStringTableSection() {
00175       return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
00176     }
00177 
00178     ELFSection &getSectionHeaderStringTableSection() {
00179       return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
00180     }
00181 
00182     ELFSection &getNullSection() {
00183       return getSection("", ELFSection::SHT_NULL, 0);
00184     }
00185 
00186     ELFSection &getDataSection();
00187     ELFSection &getBSSSection();
00188     ELFSection &getCtorSection();
00189     ELFSection &getDtorSection();
00190     ELFSection &getJumpTableSection();
00191     ELFSection &getConstantPoolSection(MachineConstantPoolEntry &CPE);
00192     ELFSection &getTextSection(Function *F);
00193     ELFSection &getRelocSection(ELFSection &S);
00194 
00195     // Helpers for obtaining ELF specific info.
00196     unsigned getGlobalELFBinding(const GlobalValue *GV);
00197     unsigned getGlobalELFType(const GlobalValue *GV);
00198     unsigned getGlobalELFVisibility(const GlobalValue *GV);
00199 
00200     // AddPendingGlobalSymbol - Add a global to be processed and to
00201     // the global symbol lookup, use a zero index because the table
00202     // index will be determined later.
00203     void AddPendingGlobalSymbol(const GlobalValue *GV, 
00204                                 bool AddToLookup = false);
00205     
00206     // AddPendingExternalSymbol - Add the external to be processed
00207     // and to the external symbol lookup, use a zero index because
00208     // the symbol table index will be determined later.
00209     void AddPendingExternalSymbol(const char *External);
00210 
00211     // AddToSymbolList - Update the symbol lookup and If the symbol is 
00212     // private add it to PrivateSyms list, otherwise to SymbolList. 
00213     void AddToSymbolList(ELFSym *GblSym);
00214 
00215     // As we complete the ELF file, we need to update fields in the ELF header
00216     // (e.g. the location of the section table).  These members keep track of
00217     // the offset in ELFHeader of these various pieces to update and other
00218     // locations in the file.
00219     unsigned ELFHdr_e_shoff_Offset;     // e_shoff    in ELF header.
00220     unsigned ELFHdr_e_shstrndx_Offset;  // e_shstrndx in ELF header.
00221     unsigned ELFHdr_e_shnum_Offset;     // e_shnum    in ELF header.
00222 
00223   private:
00224     void EmitGlobal(const GlobalValue *GV);
00225     void EmitGlobalConstant(const Constant *C, ELFSection &GblS);
00226     void EmitGlobalConstantStruct(const ConstantStruct *CVS,
00227                                   ELFSection &GblS);
00228     void EmitGlobalConstantLargeInt(const ConstantInt *CI, ELFSection &S);
00229     void EmitGlobalDataRelocation(const GlobalValue *GV, unsigned Size, 
00230                                   ELFSection &GblS, int64_t Offset = 0);
00231     bool EmitSpecialLLVMGlobal(const GlobalVariable *GV);
00232     void EmitXXStructorList(Constant *List, ELFSection &Xtor);
00233     void EmitRelocations();
00234     void EmitRelocation(BinaryObject &RelSec, ELFRelocation &Rel, bool HasRelA);
00235     void EmitSectionHeader(BinaryObject &SHdrTab, const ELFSection &SHdr);
00236     void EmitSectionTableStringTable();
00237     void EmitSymbol(BinaryObject &SymbolTable, ELFSym &Sym);
00238     void EmitSymbolTable();
00239     void EmitStringTable(const std::string &ModuleName);
00240     void OutputSectionsAndSectionTable();
00241     void RelocateField(BinaryObject &BO, uint32_t Offset, int64_t Value,
00242                        unsigned Size);
00243     unsigned SortSymbols();
00244     CstExprResTy ResolveConstantExpr(const Constant *CV);
00245   };
00246 }
00247 
00248 #endif