Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 1 | //=== MachOWriter.h - Target-independent Mach-O writer support --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the MachOWriter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Bill Wendling | 4b2ca1a | 2007-02-08 01:30:50 +0000 | [diff] [blame] | 14 | #ifndef MACHOWRITER_H |
| 15 | #define MACHOWRITER_H |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 16 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 18 | #include <vector> |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 19 | #include <map> |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 20 | |
| 21 | namespace llvm { |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 22 | class Constant; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 23 | class GlobalVariable; |
| 24 | class Mangler; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 25 | class MachineRelocation; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 26 | class MachOCodeEmitter; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 27 | class MachODySymTab; |
| 28 | class MachOHeader; |
| 29 | class MachOSection; |
| 30 | class MachOSym; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 31 | class TargetData; |
| 32 | class TargetMachine; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 33 | class TargetAsmInfo; |
| 34 | class ObjectCodeEmitter; |
Bill Wendling | 0f43b22 | 2007-02-03 02:37:51 +0000 | [diff] [blame] | 35 | class OutputBuffer; |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 36 | class raw_ostream; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 37 | |
| 38 | /// MachOWriter - This class implements the common target-independent code for |
| 39 | /// writing Mach-O files. Targets should derive a class from this to |
| 40 | /// parameterize the output format. |
| 41 | /// |
| 42 | class MachOWriter : public MachineFunctionPass { |
| 43 | friend class MachOCodeEmitter; |
| 44 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 45 | static char ID; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 46 | |
| 47 | ObjectCodeEmitter *getObjectCodeEmitter() { |
| 48 | return reinterpret_cast<ObjectCodeEmitter*>(MachOCE); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 49 | } |
Bill Wendling | 4b2ca1a | 2007-02-08 01:30:50 +0000 | [diff] [blame] | 50 | |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 51 | MachOWriter(raw_ostream &O, TargetMachine &TM); |
Bill Wendling | 2b72182 | 2007-01-24 07:13:56 +0000 | [diff] [blame] | 52 | virtual ~MachOWriter(); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 53 | |
Bill Wendling | 2b72182 | 2007-01-24 07:13:56 +0000 | [diff] [blame] | 54 | virtual const char *getPassName() const { |
| 55 | return "Mach-O Writer"; |
| 56 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 57 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 58 | protected: |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 59 | /// Output stream to send the resultant object file to. |
| 60 | /// |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 61 | raw_ostream &O; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 62 | |
| 63 | /// Target machine description. |
| 64 | /// |
| 65 | TargetMachine &TM; |
| 66 | |
| 67 | /// Mang - The object used to perform name mangling for this module. |
| 68 | /// |
| 69 | Mangler *Mang; |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 70 | |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 71 | /// MachOCE - The MachineCodeEmitter object that we are exposing to emit |
| 72 | /// machine code for functions to the .o file. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 73 | MachOCodeEmitter *MachOCE; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 74 | |
| 75 | /// is64Bit/isLittleEndian - This information is inferred from the target |
| 76 | /// machine directly, indicating what header values and flags to set. |
| 77 | bool is64Bit, isLittleEndian; |
| 78 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 79 | // Target Asm Info |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 80 | const TargetAsmInfo *TAI; |
| 81 | |
| 82 | /// Header - An instance of MachOHeader that we will update while we build |
| 83 | /// the file, and then emit during finalization. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 84 | MachOHeader Header; |
| 85 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 86 | /// doInitialization - Emit the file header and all of the global variables |
| 87 | /// for the module to the Mach-O file. |
| 88 | bool doInitialization(Module &M); |
| 89 | |
| 90 | bool runOnMachineFunction(MachineFunction &MF); |
| 91 | |
| 92 | /// doFinalization - Now that the module has been completely processed, emit |
| 93 | /// the Mach-O file to 'O'. |
| 94 | bool doFinalization(Module &M); |
| 95 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 96 | private: |
| 97 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 98 | /// SectionList - This is the list of sections that we have emitted to the |
| 99 | /// file. Once the file has been completely built, the segment load command |
| 100 | /// SectionCommands are constructed from this info. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 101 | std::vector<MachOSection*> SectionList; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 102 | |
| 103 | /// SectionLookup - This is a mapping from section name to SectionList entry |
| 104 | std::map<std::string, MachOSection*> SectionLookup; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 105 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 106 | /// GVSection - This is a mapping from a GlobalValue to a MachOSection, |
| 107 | /// to aid in emitting relocations. |
| 108 | std::map<GlobalValue*, MachOSection*> GVSection; |
| 109 | |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 110 | /// GVOffset - This is a mapping from a GlobalValue to an offset from the |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 111 | /// start of the section in which the GV resides, to aid in emitting |
| 112 | /// relocations. |
| 113 | std::map<GlobalValue*, intptr_t> GVOffset; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 114 | |
| 115 | /// getSection - Return the section with the specified name, creating a new |
| 116 | /// section if one does not already exist. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 117 | MachOSection *getSection(const std::string &seg, const std::string §, |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 118 | unsigned Flags = 0); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 119 | |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 120 | /// getTextSection - Return text section with different flags for code/data |
| 121 | MachOSection *getTextSection(bool isCode = true); |
| 122 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 123 | MachOSection *getDataSection() { |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 124 | return getSection("__DATA", "__data"); |
| 125 | } |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 126 | |
| 127 | MachOSection *getBSSSection(); |
| 128 | MachOSection *getConstSection(Constant *C); |
| 129 | MachOSection *getJumpTableSection(); |
| 130 | |
| 131 | /// MachOSymTab - This struct contains information about the offsets and |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 132 | /// size of symbol table information. |
| 133 | /// segment. |
| 134 | struct MachOSymTab { |
| 135 | uint32_t cmd; // LC_SYMTAB |
| 136 | uint32_t cmdsize; // sizeof( MachOSymTab ) |
| 137 | uint32_t symoff; // symbol table offset |
| 138 | uint32_t nsyms; // number of symbol table entries |
| 139 | uint32_t stroff; // string table offset |
| 140 | uint32_t strsize; // string table size in bytes |
| 141 | |
| 142 | // Constants for the cmd field |
| 143 | // see <mach-o/loader.h> |
| 144 | enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info |
| 145 | }; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 146 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 147 | MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0), |
| 148 | nsyms(0), stroff(0), strsize(0) { } |
| 149 | }; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 150 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 151 | /// SymTab - The "stab" style symbol table information |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 152 | MachOSymTab SymTab; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 153 | /// DySymTab - symbol table info for the dynamic link editor |
| 154 | MachODySymTab DySymTab; |
| 155 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 156 | protected: |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 157 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 158 | /// SymbolTable - This is the list of symbols we have emitted to the file. |
| 159 | /// This actually gets rearranged before emission to the file (to put the |
| 160 | /// local symbols first in the list). |
| 161 | std::vector<MachOSym> SymbolTable; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 162 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 163 | /// SymT - A buffer to hold the symbol table before we write it out at the |
| 164 | /// appropriate location in the file. |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 165 | std::vector<unsigned char> SymT; |
| 166 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 167 | /// StrT - A buffer to hold the string table before we write it out at the |
| 168 | /// appropriate location in the file. |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 169 | std::vector<unsigned char> StrT; |
| 170 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 171 | /// PendingSyms - This is a list of externally defined symbols that we have |
| 172 | /// been asked to emit, but have not seen a reference to. When a reference |
| 173 | /// is seen, the symbol will move from this list to the SymbolTable. |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 174 | std::vector<GlobalValue*> PendingGlobals; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 175 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 176 | /// DynamicSymbolTable - This is just a vector of indices into |
| 177 | /// SymbolTable to aid in emitting the DYSYMTAB load command. |
| 178 | std::vector<unsigned> DynamicSymbolTable; |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 179 | |
| 180 | static void InitMem(const Constant *C, uintptr_t Offset, |
| 181 | const TargetData *TD, MachOSection* mos); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 182 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 183 | private: |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 184 | void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 185 | void EmitGlobal(GlobalVariable *GV); |
| 186 | void EmitHeaderAndLoadCommands(); |
| 187 | void EmitSections(); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 188 | void EmitRelocations(); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 189 | void BufferSymbolAndStringTable(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 190 | void CalculateRelocations(MachOSection &MOS); |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 191 | |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 192 | // GetJTRelocation - Get a relocation a new BB relocation based |
| 193 | // on target information. |
Bill Wendling | 40fab40 | 2007-01-24 03:37:18 +0000 | [diff] [blame] | 194 | MachineRelocation GetJTRelocation(unsigned Offset, |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 195 | MachineBasicBlock *MBB) const; |
Bill Wendling | 0f43b22 | 2007-02-03 02:37:51 +0000 | [diff] [blame] | 196 | |
| 197 | /// GetTargetRelocation - Returns the number of relocations. |
Bruno Cardoso Lopes | 752e928 | 2009-07-06 06:40:51 +0000 | [diff] [blame^] | 198 | unsigned GetTargetRelocation(MachineRelocation &MR, unsigned FromIdx, |
| 199 | unsigned ToAddr, unsigned ToIndex, |
| 200 | OutputBuffer &RelocOut, OutputBuffer &SecOut, |
| 201 | bool Scattered, bool Extern); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 202 | }; |
| 203 | } |
| 204 | |
| 205 | #endif |