LLVM API Documentation

MachOWriter.h

Go to the documentation of this file.
00001 //=== MachOWriter.h - Target-independent Mach-O 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 MachOWriter class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef MACHOWRITER_H
00015 #define MACHOWRITER_H
00016 
00017 #include "llvm/CodeGen/MachineFunctionPass.h"
00018 #include <vector>
00019 #include <map>
00020 
00021 namespace llvm {
00022   class Constant;
00023   class GlobalVariable;
00024   class Mangler;
00025   class MachineBasicBlock;
00026   class MachineRelocation;
00027   class MachOCodeEmitter;
00028   struct MachODySymTab;
00029   struct MachOHeader;
00030   struct MachOSection;
00031   struct MachOSym;
00032   class TargetData;
00033   class TargetMachine;
00034   class TargetAsmInfo;
00035   class ObjectCodeEmitter;
00036   class OutputBuffer;
00037   class raw_ostream;
00038 
00039   /// MachOWriter - This class implements the common target-independent code for
00040   /// writing Mach-O files.  Targets should derive a class from this to
00041   /// parameterize the output format.
00042   ///
00043   class MachOWriter : public MachineFunctionPass {
00044     friend class MachOCodeEmitter;
00045   public:
00046     static char ID;
00047 
00048     ObjectCodeEmitter *getObjectCodeEmitter() {
00049       return reinterpret_cast<ObjectCodeEmitter*>(MachOCE);
00050     }
00051 
00052     MachOWriter(raw_ostream &O, TargetMachine &TM);
00053     virtual ~MachOWriter();
00054 
00055     virtual const char *getPassName() const {
00056       return "Mach-O Writer";
00057     }
00058 
00059   protected:
00060     /// Output stream to send the resultant object file to.
00061     ///
00062     raw_ostream &O;
00063 
00064     /// Target machine description.
00065     ///
00066     TargetMachine &TM;
00067 
00068     /// Mang - The object used to perform name mangling for this module.
00069     ///
00070     Mangler *Mang;
00071 
00072     /// MachOCE - The MachineCodeEmitter object that we are exposing to emit
00073     /// machine code for functions to the .o file.
00074     MachOCodeEmitter *MachOCE;
00075 
00076     /// is64Bit/isLittleEndian - This information is inferred from the target
00077     /// machine directly, indicating what header values and flags to set.
00078     bool is64Bit, isLittleEndian;
00079 
00080     // Target Asm Info
00081     const TargetAsmInfo *TAI;
00082 
00083     /// Header - An instance of MachOHeader that we will update while we build
00084     /// the file, and then emit during finalization.
00085     MachOHeader Header;
00086 
00087     /// doInitialization - Emit the file header and all of the global variables
00088     /// for the module to the Mach-O file.
00089     bool doInitialization(Module &M);
00090 
00091     bool runOnMachineFunction(MachineFunction &MF);
00092 
00093     /// doFinalization - Now that the module has been completely processed, emit
00094     /// the Mach-O file to 'O'.
00095     bool doFinalization(Module &M);
00096 
00097   private:
00098 
00099     /// SectionList - This is the list of sections that we have emitted to the
00100     /// file.  Once the file has been completely built, the segment load command
00101     /// SectionCommands are constructed from this info.
00102     std::vector<MachOSection*> SectionList;
00103 
00104     /// SectionLookup - This is a mapping from section name to SectionList entry
00105     std::map<std::string, MachOSection*> SectionLookup;
00106 
00107     /// GVSection - This is a mapping from a GlobalValue to a MachOSection,
00108     /// to aid in emitting relocations.
00109     std::map<GlobalValue*, MachOSection*> GVSection;
00110 
00111     /// GVOffset - This is a mapping from a GlobalValue to an offset from the
00112     /// start of the section in which the GV resides, to aid in emitting
00113     /// relocations.
00114     std::map<GlobalValue*, intptr_t> GVOffset;
00115 
00116     /// getSection - Return the section with the specified name, creating a new
00117     /// section if one does not already exist.
00118     MachOSection *getSection(const std::string &seg, const std::string &sect,
00119                              unsigned Flags = 0);
00120 
00121     /// getTextSection - Return text section with different flags for code/data
00122     MachOSection *getTextSection(bool isCode = true);
00123 
00124     MachOSection *getDataSection() {
00125       return getSection("__DATA", "__data");
00126     }
00127 
00128     MachOSection *getBSSSection();
00129     MachOSection *getConstSection(Constant *C);
00130     MachOSection *getJumpTableSection();
00131 
00132     /// MachOSymTab - This struct contains information about the offsets and
00133     /// size of symbol table information.
00134     /// segment.
00135     struct MachOSymTab {
00136       uint32_t cmd;     // LC_SYMTAB
00137       uint32_t cmdsize; // sizeof( MachOSymTab )
00138       uint32_t symoff;  // symbol table offset
00139       uint32_t nsyms;   // number of symbol table entries
00140       uint32_t stroff;  // string table offset
00141       uint32_t strsize; // string table size in bytes
00142 
00143       // Constants for the cmd field
00144       // see <mach-o/loader.h>
00145       enum { LC_SYMTAB = 0x02  // link-edit stab symbol table info
00146       };
00147 
00148       MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0),
00149         nsyms(0), stroff(0), strsize(0) { }
00150     };
00151 
00152     /// SymTab - The "stab" style symbol table information
00153     MachOSymTab SymTab;
00154     /// DySymTab - symbol table info for the dynamic link editor
00155     MachODySymTab DySymTab;
00156 
00157   protected:
00158 
00159     /// SymbolTable - This is the list of symbols we have emitted to the file.
00160     /// This actually gets rearranged before emission to the file (to put the
00161     /// local symbols first in the list).
00162     std::vector<MachOSym> SymbolTable;
00163 
00164     /// SymT - A buffer to hold the symbol table before we write it out at the
00165     /// appropriate location in the file.
00166     std::vector<unsigned char> SymT;
00167 
00168     /// StrT - A buffer to hold the string table before we write it out at the
00169     /// appropriate location in the file.
00170     std::vector<unsigned char> StrT;
00171 
00172     /// PendingSyms - This is a list of externally defined symbols that we have
00173     /// been asked to emit, but have not seen a reference to.  When a reference
00174     /// is seen, the symbol will move from this list to the SymbolTable.
00175     std::vector<GlobalValue*> PendingGlobals;
00176 
00177     /// DynamicSymbolTable - This is just a vector of indices into
00178     /// SymbolTable to aid in emitting the DYSYMTAB load command.
00179     std::vector<unsigned> DynamicSymbolTable;
00180 
00181     static void InitMem(const Constant *C, uintptr_t Offset,
00182                         const TargetData *TD, MachOSection* mos);
00183 
00184   private:
00185     void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV);
00186     void EmitGlobal(GlobalVariable *GV);
00187     void EmitHeaderAndLoadCommands();
00188     void EmitSections();
00189     void EmitRelocations();
00190     void BufferSymbolAndStringTable();
00191     void CalculateRelocations(MachOSection &MOS);
00192 
00193     // GetJTRelocation - Get a relocation a new BB relocation based
00194     // on target information.
00195     MachineRelocation GetJTRelocation(unsigned Offset,
00196                                       MachineBasicBlock *MBB) const;
00197 
00198     /// GetTargetRelocation - Returns the number of relocations.
00199     unsigned GetTargetRelocation(MachineRelocation &MR, unsigned FromIdx,
00200                                  unsigned ToAddr, unsigned ToIndex,
00201                                  OutputBuffer &RelocOut, OutputBuffer &SecOut,
00202                                  bool Scattered, bool Extern);
00203   };
00204 }
00205 
00206 #endif