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 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 17 | #include "MachO.h" |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/ObjectCodeEmitter.h" |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
Bill Wendling | 40fab40 | 2007-01-24 03:37:18 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachOWriterInfo.h" |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 25 | #include <vector> |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 26 | #include <map> |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 27 | |
| 28 | namespace llvm { |
| 29 | class GlobalVariable; |
| 30 | class Mangler; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 31 | class MachineRelocation; |
| 32 | class ObjectCodeEmitter; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 33 | class MachOCodeEmitter; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 34 | class TargetData; |
| 35 | class TargetMachine; |
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 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 39 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 40 | /// MachOWriter - This class implements the common target-independent code for |
| 41 | /// writing Mach-O files. Targets should derive a class from this to |
| 42 | /// parameterize the output format. |
| 43 | /// |
| 44 | class MachOWriter : public MachineFunctionPass { |
| 45 | friend class MachOCodeEmitter; |
| 46 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 47 | static char ID; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 48 | |
| 49 | ObjectCodeEmitter *getObjectCodeEmitter() { |
| 50 | return reinterpret_cast<ObjectCodeEmitter*>(MachOCE); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 51 | } |
Bill Wendling | 4b2ca1a | 2007-02-08 01:30:50 +0000 | [diff] [blame] | 52 | |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 53 | MachOWriter(raw_ostream &O, TargetMachine &TM); |
Bill Wendling | 2b72182 | 2007-01-24 07:13:56 +0000 | [diff] [blame] | 54 | virtual ~MachOWriter(); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 55 | |
Bill Wendling | 2b72182 | 2007-01-24 07:13:56 +0000 | [diff] [blame] | 56 | virtual const char *getPassName() const { |
| 57 | return "Mach-O Writer"; |
| 58 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 59 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 60 | protected: |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 61 | /// Output stream to send the resultant object file to. |
| 62 | /// |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 63 | raw_ostream &O; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 64 | |
| 65 | /// Target machine description. |
| 66 | /// |
| 67 | TargetMachine &TM; |
| 68 | |
| 69 | /// Mang - The object used to perform name mangling for this module. |
| 70 | /// |
| 71 | Mangler *Mang; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 72 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 73 | /// MachOCE - The MachineCodeEmitter object that we are exposing to emit machine |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 74 | /// code for functions to the .o file. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 75 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 76 | MachOCodeEmitter *MachOCE; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 77 | |
| 78 | /// is64Bit/isLittleEndian - This information is inferred from the target |
| 79 | /// machine directly, indicating what header values and flags to set. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 80 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 81 | bool is64Bit, isLittleEndian; |
| 82 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 83 | // Target Asm Info |
| 84 | |
| 85 | const TargetAsmInfo *TAI; |
| 86 | |
| 87 | /// Header - An instance of MachOHeader that we will update while we build |
| 88 | /// the file, and then emit during finalization. |
| 89 | |
| 90 | MachOHeader Header; |
| 91 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 92 | /// doInitialization - Emit the file header and all of the global variables |
| 93 | /// for the module to the Mach-O file. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 94 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 95 | bool doInitialization(Module &M); |
| 96 | |
| 97 | bool runOnMachineFunction(MachineFunction &MF); |
| 98 | |
| 99 | /// doFinalization - Now that the module has been completely processed, emit |
| 100 | /// the Mach-O file to 'O'. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 101 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 102 | bool doFinalization(Module &M); |
| 103 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 104 | private: |
| 105 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 106 | /// SectionList - This is the list of sections that we have emitted to the |
| 107 | /// file. Once the file has been completely built, the segment load command |
| 108 | /// SectionCommands are constructed from this info. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 109 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 110 | std::vector<MachOSection*> SectionList; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 111 | |
| 112 | /// SectionLookup - This is a mapping from section name to SectionList entry |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 113 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 114 | std::map<std::string, MachOSection*> SectionLookup; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 115 | |
| 116 | /// GVSection - This is a mapping from a GlobalValue to a MachOSection, |
| 117 | /// to aid in emitting relocations. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 118 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 119 | std::map<GlobalValue*, MachOSection*> GVSection; |
| 120 | |
| 121 | /// GVOffset - This is a mapping from a GlobalValue to an offset from the |
| 122 | /// start of the section in which the GV resides, to aid in emitting |
| 123 | /// relocations. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 124 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 125 | std::map<GlobalValue*, intptr_t> GVOffset; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 126 | |
| 127 | /// getSection - Return the section with the specified name, creating a new |
| 128 | /// section if one does not already exist. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 129 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 130 | MachOSection *getSection(const std::string &seg, const std::string §, |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 131 | unsigned Flags = 0) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 132 | MachOSection *MOS = SectionLookup[seg+sect]; |
| 133 | if (MOS) return MOS; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 134 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 135 | MOS = new MachOSection(seg, sect); |
| 136 | SectionList.push_back(MOS); |
| 137 | MOS->Index = SectionList.size(); |
| 138 | MOS->flags = MachOSection::S_REGULAR | Flags; |
| 139 | SectionLookup[seg+sect] = MOS; |
| 140 | return MOS; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 141 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 142 | MachOSection *getTextSection(bool isCode = true) { |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 143 | if (isCode) |
| 144 | return getSection("__TEXT", "__text", |
| 145 | MachOSection::S_ATTR_PURE_INSTRUCTIONS | |
| 146 | MachOSection::S_ATTR_SOME_INSTRUCTIONS); |
| 147 | else |
| 148 | return getSection("__TEXT", "__text"); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 149 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 150 | MachOSection *getBSSSection() { |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 151 | return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL); |
| 152 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 153 | MachOSection *getDataSection() { |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 154 | return getSection("__DATA", "__data"); |
| 155 | } |
Nate Begeman | 1257c85 | 2007-01-29 21:20:42 +0000 | [diff] [blame] | 156 | MachOSection *getConstSection(Constant *C) { |
| 157 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
| 158 | if (CVA && CVA->isCString()) |
| 159 | return getSection("__TEXT", "__cstring", |
| 160 | MachOSection::S_CSTRING_LITERALS); |
| 161 | |
| 162 | const Type *Ty = C->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 163 | if (Ty->isPrimitiveType() || Ty->isInteger()) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 164 | unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 165 | switch(Size) { |
| 166 | default: break; // Fall through to __TEXT,__const |
| 167 | case 4: |
| 168 | return getSection("__TEXT", "__literal4", |
| 169 | MachOSection::S_4BYTE_LITERALS); |
| 170 | case 8: |
| 171 | return getSection("__TEXT", "__literal8", |
| 172 | MachOSection::S_8BYTE_LITERALS); |
| 173 | case 16: |
| 174 | return getSection("__TEXT", "__literal16", |
| 175 | MachOSection::S_16BYTE_LITERALS); |
| 176 | } |
| 177 | } |
| 178 | return getSection("__TEXT", "__const"); |
| 179 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 180 | MachOSection *getJumpTableSection() { |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 181 | if (TM.getRelocationModel() == Reloc::PIC_) |
| 182 | return getTextSection(false); |
| 183 | else |
| 184 | return getSection("__TEXT", "__const"); |
| 185 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 186 | |
| 187 | /// MachOSymTab - This struct contains information about the offsets and |
| 188 | /// size of symbol table information. |
| 189 | /// segment. |
| 190 | struct MachOSymTab { |
| 191 | uint32_t cmd; // LC_SYMTAB |
| 192 | uint32_t cmdsize; // sizeof( MachOSymTab ) |
| 193 | uint32_t symoff; // symbol table offset |
| 194 | uint32_t nsyms; // number of symbol table entries |
| 195 | uint32_t stroff; // string table offset |
| 196 | uint32_t strsize; // string table size in bytes |
| 197 | |
| 198 | // Constants for the cmd field |
| 199 | // see <mach-o/loader.h> |
| 200 | enum { LC_SYMTAB = 0x02 // link-edit stab symbol table info |
| 201 | }; |
| 202 | |
| 203 | MachOSymTab() : cmd(LC_SYMTAB), cmdsize(6 * sizeof(uint32_t)), symoff(0), |
| 204 | nsyms(0), stroff(0), strsize(0) { } |
| 205 | }; |
| 206 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 207 | /// SymTab - The "stab" style symbol table information |
| 208 | MachOSymTab SymTab; |
| 209 | /// DySymTab - symbol table info for the dynamic link editor |
| 210 | MachODySymTab DySymTab; |
| 211 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 212 | protected: |
| 213 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 214 | /// SymbolTable - This is the list of symbols we have emitted to the file. |
| 215 | /// This actually gets rearranged before emission to the file (to put the |
| 216 | /// local symbols first in the list). |
| 217 | std::vector<MachOSym> SymbolTable; |
| 218 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 219 | /// SymT - A buffer to hold the symbol table before we write it out at the |
| 220 | /// appropriate location in the file. |
| 221 | DataBuffer SymT; |
| 222 | |
| 223 | /// StrT - A buffer to hold the string table before we write it out at the |
| 224 | /// appropriate location in the file. |
| 225 | DataBuffer StrT; |
| 226 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 227 | /// PendingSyms - This is a list of externally defined symbols that we have |
| 228 | /// been asked to emit, but have not seen a reference to. When a reference |
| 229 | /// is seen, the symbol will move from this list to the SymbolTable. |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 230 | std::vector<GlobalValue*> PendingGlobals; |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 231 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 232 | /// DynamicSymbolTable - This is just a vector of indices into |
| 233 | /// SymbolTable to aid in emitting the DYSYMTAB load command. |
| 234 | std::vector<unsigned> DynamicSymbolTable; |
| 235 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 236 | static void InitMem(const Constant *C, |
| 237 | uintptr_t Offset, |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 238 | const TargetData *TD, |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 239 | MachOSection* mos); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 240 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 241 | private: |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 242 | void AddSymbolToSection(MachOSection *MOS, GlobalVariable *GV); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 243 | void EmitGlobal(GlobalVariable *GV); |
| 244 | void EmitHeaderAndLoadCommands(); |
| 245 | void EmitSections(); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 246 | void EmitRelocations(); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 247 | void BufferSymbolAndStringTable(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 248 | void CalculateRelocations(MachOSection &MOS); |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 249 | |
Bill Wendling | 40fab40 | 2007-01-24 03:37:18 +0000 | [diff] [blame] | 250 | MachineRelocation GetJTRelocation(unsigned Offset, |
| 251 | MachineBasicBlock *MBB) const { |
| 252 | return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB); |
| 253 | } |
Bill Wendling | 0f43b22 | 2007-02-03 02:37:51 +0000 | [diff] [blame] | 254 | |
| 255 | /// GetTargetRelocation - Returns the number of relocations. |
| 256 | unsigned GetTargetRelocation(MachineRelocation &MR, |
| 257 | unsigned FromIdx, |
| 258 | unsigned ToAddr, |
| 259 | unsigned ToIndex, |
| 260 | OutputBuffer &RelocOut, |
| 261 | OutputBuffer &SecOut, |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 262 | bool Scattered, |
| 263 | bool Extern) { |
Bill Wendling | 0f43b22 | 2007-02-03 02:37:51 +0000 | [diff] [blame] | 264 | return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr, |
| 265 | ToIndex, RelocOut, |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 266 | SecOut, Scattered, |
| 267 | Extern); |
Bill Wendling | 0f43b22 | 2007-02-03 02:37:51 +0000 | [diff] [blame] | 268 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 269 | }; |
| 270 | } |
| 271 | |
| 272 | #endif |