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