Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 1 | //===-- MachOWriter.cpp - Target-independent Mach-O Writer code -----------===// |
| 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 implements the target-independent Mach-O writer. This file writes |
| 11 | // out the Mach-O file in the following order: |
| 12 | // |
| 13 | // #1 FatHeader (universal-only) |
| 14 | // #2 FatArch (universal-only, 1 per universal arch) |
| 15 | // Per arch: |
| 16 | // #3 Header |
| 17 | // #4 Load Commands |
| 18 | // #5 Sections |
| 19 | // #6 Relocations |
| 20 | // #7 Symbols |
| 21 | // #8 Strings |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 25 | #include "MachOWriter.h" |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 26 | #include "MachOCodeEmitter.h" |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 27 | #include "llvm/Constants.h" |
| 28 | #include "llvm/DerivedTypes.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 30 | #include "llvm/PassManager.h" |
| 31 | #include "llvm/CodeGen/FileWriters.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 33 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetAsmInfo.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetJITInfo.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Mangler.h" |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 39 | #include "llvm/Support/OutputBuffer.h" |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Streams.h" |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 42 | #include <algorithm> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 43 | #include <cstring> |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 44 | |
| 45 | namespace llvm { |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 46 | |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 47 | /// AddMachOWriter - Concrete function to add the Mach-O writer to the function |
| 48 | /// pass manager. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 49 | ObjectCodeEmitter *AddMachOWriter(PassManagerBase &PM, |
Owen Anderson | cb37188 | 2008-08-21 00:14:44 +0000 | [diff] [blame] | 50 | raw_ostream &O, |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 51 | TargetMachine &TM) { |
| 52 | MachOWriter *MOW = new MachOWriter(O, TM); |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 53 | PM.add(MOW); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 54 | return MOW->getObjectCodeEmitter(); |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 55 | } |
| 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 | // MachOWriter Implementation |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 61 | char MachOWriter::ID = 0; |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 62 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 63 | MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm) |
| 64 | : MachineFunctionPass(&ID), O(o), TM(tm) |
| 65 | { |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 66 | is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64; |
| 67 | isLittleEndian = TM.getTargetData()->isLittleEndian(); |
| 68 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 69 | TAI = TM.getTargetAsmInfo(); |
| 70 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 71 | // Create the machine code emitter object for this target. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 72 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 73 | MachOCE = new MachOCodeEmitter(*this, *getTextSection(true)); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | MachOWriter::~MachOWriter() { |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 77 | delete MachOCE; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 80 | bool MachOWriter::doInitialization(Module &M) { |
| 81 | // Set the magic value, now that we know the pointer size and endianness |
| 82 | Header.setMagic(isLittleEndian, is64Bit); |
| 83 | |
| 84 | // Set the file type |
| 85 | // FIXME: this only works for object files, we do not support the creation |
| 86 | // of dynamic libraries or executables at this time. |
| 87 | Header.filetype = MachOHeader::MH_OBJECT; |
| 88 | |
| 89 | Mang = new Mangler(M); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | bool MachOWriter::runOnMachineFunction(MachineFunction &MF) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | /// doFinalization - Now that the module has been completely processed, emit |
| 98 | /// the Mach-O file to 'O'. |
| 99 | bool MachOWriter::doFinalization(Module &M) { |
| 100 | // FIXME: we don't handle debug info yet, we should probably do that. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 101 | // Okay, the.text section has been completed, build the .data, .bss, and |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 102 | // "common" sections next. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 103 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 104 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 105 | I != E; ++I) |
| 106 | EmitGlobal(I); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 107 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 108 | // Emit the header and load commands. |
| 109 | EmitHeaderAndLoadCommands(); |
| 110 | |
| 111 | // Emit the various sections and their relocation info. |
| 112 | EmitSections(); |
| 113 | EmitRelocations(); |
| 114 | |
| 115 | // Write the symbol table and the string table to the end of the file. |
| 116 | O.write((char*)&SymT[0], SymT.size()); |
| 117 | O.write((char*)&StrT[0], StrT.size()); |
| 118 | |
| 119 | // We are done with the abstract symbols. |
| 120 | SectionList.clear(); |
| 121 | SymbolTable.clear(); |
| 122 | DynamicSymbolTable.clear(); |
| 123 | |
| 124 | // Release the name mangler object. |
| 125 | delete Mang; Mang = 0; |
| 126 | return false; |
| 127 | } |
| 128 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 129 | void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) { |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 130 | const Type *Ty = GV->getType()->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 131 | unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty); |
Duncan Sands | d102593 | 2008-01-29 06:23:44 +0000 | [diff] [blame] | 132 | unsigned Align = TM.getTargetData()->getPreferredAlignment(GV); |
| 133 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 134 | // Reserve space in the .bss section for this symbol while maintaining the |
| 135 | // desired section alignment, which must be at least as much as required by |
| 136 | // this symbol. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 137 | OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 138 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 139 | if (Align) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 140 | Align = Log2_32(Align); |
| 141 | Sec->align = std::max(unsigned(Sec->align), Align); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 142 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 143 | Sec->emitAlignment(Sec->align); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 144 | } |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 145 | // Globals without external linkage apparently do not go in the symbol table. |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 146 | if (!GV->hasLocalLinkage()) { |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 147 | MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TAI); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 148 | Sym.n_value = Sec->size(); |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 149 | SymbolTable.push_back(Sym); |
| 150 | } |
| 151 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 152 | // Record the offset of the symbol, and then allocate space for it. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 153 | // FIXME: remove when we have unified size + output buffer |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 154 | |
| 155 | // Now that we know what section the GlovalVariable is going to be emitted |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 156 | // into, update our mappings. |
| 157 | // FIXME: We may also need to update this when outputting non-GlobalVariable |
| 158 | // GlobalValues such as functions. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 159 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 160 | GVSection[GV] = Sec; |
| 161 | GVOffset[GV] = Sec->size(); |
| 162 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 163 | // Allocate space in the section for the global. |
| 164 | for (unsigned i = 0; i < Size; ++i) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 165 | SecDataOut.outbyte(0); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 168 | void MachOWriter::EmitGlobal(GlobalVariable *GV) { |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 169 | const Type *Ty = GV->getType()->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 170 | unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 171 | bool NoInit = !GV->hasInitializer(); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 172 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 173 | // If this global has a zero initializer, it is part of the .bss or common |
| 174 | // section. |
| 175 | if (NoInit || GV->getInitializer()->isNullValue()) { |
| 176 | // If this global is part of the common block, add it now. Variables are |
| 177 | // part of the common block if they are zero initialized and allowed to be |
| 178 | // merged with other symbols. |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 179 | if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || |
| 180 | GV->hasCommonLinkage()) { |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 181 | MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV), MachOSym::NO_SECT, TAI); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 182 | // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in |
| 183 | // bytes of the symbol. |
| 184 | ExtOrCommonSym.n_value = Size; |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 185 | SymbolTable.push_back(ExtOrCommonSym); |
| 186 | // Remember that we've seen this symbol |
| 187 | GVOffset[GV] = Size; |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | // Otherwise, this symbol is part of the .bss section. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 191 | MachOSection *BSS = getBSSSection(); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 192 | AddSymbolToSection(BSS, GV); |
| 193 | return; |
| 194 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 195 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 196 | // Scalar read-only data goes in a literal section if the scalar is 4, 8, or |
| 197 | // 16 bytes, or a cstring. Other read only data goes into a regular const |
| 198 | // section. Read-write data goes in the data section. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 199 | MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) : |
Nate Begeman | 1257c85 | 2007-01-29 21:20:42 +0000 | [diff] [blame] | 200 | getDataSection(); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 201 | AddSymbolToSection(Sec, GV); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 202 | InitMem(GV->getInitializer(), GVOffset[GV], TM.getTargetData(), Sec); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 206 | |
| 207 | void MachOWriter::EmitHeaderAndLoadCommands() { |
| 208 | // Step #0: Fill in the segment load command size, since we need it to figure |
| 209 | // out the rest of the header fields |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 210 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 211 | MachOSegment SEG("", is64Bit); |
| 212 | SEG.nsects = SectionList.size(); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 213 | SEG.cmdsize = SEG.cmdSize(is64Bit) + |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 214 | SEG.nsects * SectionList[0]->cmdSize(is64Bit); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 215 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 216 | // Step #1: calculate the number of load commands. We always have at least |
| 217 | // one, for the LC_SEGMENT load command, plus two for the normal |
| 218 | // and dynamic symbol tables, if there are any symbols. |
| 219 | Header.ncmds = SymbolTable.empty() ? 1 : 3; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 220 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 221 | // Step #2: calculate the size of the load commands |
| 222 | Header.sizeofcmds = SEG.cmdsize; |
| 223 | if (!SymbolTable.empty()) |
| 224 | Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 225 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 226 | // Step #3: write the header to the file |
| 227 | // Local alias to shortenify coming code. |
| 228 | DataBuffer &FH = Header.HeaderData; |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 229 | OutputBuffer FHOut(FH, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 230 | |
| 231 | FHOut.outword(Header.magic); |
Bill Wendling | 2b72182 | 2007-01-24 07:13:56 +0000 | [diff] [blame] | 232 | FHOut.outword(TM.getMachOWriterInfo()->getCPUType()); |
| 233 | FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType()); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 234 | FHOut.outword(Header.filetype); |
| 235 | FHOut.outword(Header.ncmds); |
| 236 | FHOut.outword(Header.sizeofcmds); |
| 237 | FHOut.outword(Header.flags); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 238 | if (is64Bit) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 239 | FHOut.outword(Header.reserved); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 240 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 241 | // Step #4: Finish filling in the segment load command and write it out |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 242 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 243 | E = SectionList.end(); I != E; ++I) |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 244 | SEG.filesize += (*I)->size(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 245 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 246 | SEG.vmsize = SEG.filesize; |
| 247 | SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 248 | |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 249 | FHOut.outword(SEG.cmd); |
| 250 | FHOut.outword(SEG.cmdsize); |
| 251 | FHOut.outstring(SEG.segname, 16); |
| 252 | FHOut.outaddr(SEG.vmaddr); |
| 253 | FHOut.outaddr(SEG.vmsize); |
| 254 | FHOut.outaddr(SEG.fileoff); |
| 255 | FHOut.outaddr(SEG.filesize); |
| 256 | FHOut.outword(SEG.maxprot); |
| 257 | FHOut.outword(SEG.initprot); |
| 258 | FHOut.outword(SEG.nsects); |
| 259 | FHOut.outword(SEG.flags); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 260 | |
| 261 | // Step #5: Finish filling in the fields of the MachOSections |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 262 | uint64_t currentAddr = 0; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 263 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 264 | E = SectionList.end(); I != E; ++I) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 265 | MachOSection *MOS = *I; |
| 266 | MOS->addr = currentAddr; |
| 267 | MOS->offset = currentAddr + SEG.fileoff; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 268 | // FIXME: do we need to do something with alignment here? |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 269 | currentAddr += MOS->size(); |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 270 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 271 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 272 | // Step #6: Emit the symbol table to temporary buffers, so that we know the |
| 273 | // size of the string table when we write the next load command. This also |
| 274 | // sorts and assigns indices to each of the symbols, which is necessary for |
| 275 | // emitting relocations to externally-defined objects. |
| 276 | BufferSymbolAndStringTable(); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 277 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 278 | // Step #7: Calculate the number of relocations for each section and write out |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 279 | // the section commands for each section |
| 280 | currentAddr += SEG.fileoff; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 281 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 282 | E = SectionList.end(); I != E; ++I) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 283 | MachOSection *MOS = *I; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 284 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 285 | // Convert the relocations to target-specific relocations, and fill in the |
| 286 | // relocation offset for this section. |
| 287 | CalculateRelocations(*MOS); |
| 288 | MOS->reloff = MOS->nreloc ? currentAddr : 0; |
| 289 | currentAddr += MOS->nreloc * 8; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 290 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 291 | // write the finalized section command to the output buffer |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 292 | FHOut.outstring(MOS->sectname, 16); |
| 293 | FHOut.outstring(MOS->segname, 16); |
| 294 | FHOut.outaddr(MOS->addr); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 295 | FHOut.outaddr(MOS->size()); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 296 | FHOut.outword(MOS->offset); |
| 297 | FHOut.outword(MOS->align); |
| 298 | FHOut.outword(MOS->reloff); |
| 299 | FHOut.outword(MOS->nreloc); |
| 300 | FHOut.outword(MOS->flags); |
| 301 | FHOut.outword(MOS->reserved1); |
| 302 | FHOut.outword(MOS->reserved2); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 303 | if (is64Bit) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 304 | FHOut.outword(MOS->reserved3); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 305 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 306 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 307 | // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 308 | SymTab.symoff = currentAddr; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 309 | SymTab.nsyms = SymbolTable.size(); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 310 | SymTab.stroff = SymTab.symoff + SymT.size(); |
| 311 | SymTab.strsize = StrT.size(); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 312 | FHOut.outword(SymTab.cmd); |
| 313 | FHOut.outword(SymTab.cmdsize); |
| 314 | FHOut.outword(SymTab.symoff); |
| 315 | FHOut.outword(SymTab.nsyms); |
| 316 | FHOut.outword(SymTab.stroff); |
| 317 | FHOut.outword(SymTab.strsize); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 318 | |
| 319 | // FIXME: set DySymTab fields appropriately |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 320 | // We should probably just update these in BufferSymbolAndStringTable since |
| 321 | // thats where we're partitioning up the different kinds of symbols. |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 322 | FHOut.outword(DySymTab.cmd); |
| 323 | FHOut.outword(DySymTab.cmdsize); |
| 324 | FHOut.outword(DySymTab.ilocalsym); |
| 325 | FHOut.outword(DySymTab.nlocalsym); |
| 326 | FHOut.outword(DySymTab.iextdefsym); |
| 327 | FHOut.outword(DySymTab.nextdefsym); |
| 328 | FHOut.outword(DySymTab.iundefsym); |
| 329 | FHOut.outword(DySymTab.nundefsym); |
| 330 | FHOut.outword(DySymTab.tocoff); |
| 331 | FHOut.outword(DySymTab.ntoc); |
| 332 | FHOut.outword(DySymTab.modtaboff); |
| 333 | FHOut.outword(DySymTab.nmodtab); |
| 334 | FHOut.outword(DySymTab.extrefsymoff); |
| 335 | FHOut.outword(DySymTab.nextrefsyms); |
| 336 | FHOut.outword(DySymTab.indirectsymoff); |
| 337 | FHOut.outword(DySymTab.nindirectsyms); |
| 338 | FHOut.outword(DySymTab.extreloff); |
| 339 | FHOut.outword(DySymTab.nextrel); |
| 340 | FHOut.outword(DySymTab.locreloff); |
| 341 | FHOut.outword(DySymTab.nlocrel); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 342 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 343 | O.write((char*)&FH[0], FH.size()); |
| 344 | } |
| 345 | |
| 346 | /// EmitSections - Now that we have constructed the file header and load |
| 347 | /// commands, emit the data for each section to the file. |
| 348 | void MachOWriter::EmitSections() { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 349 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 350 | E = SectionList.end(); I != E; ++I) |
| 351 | // Emit the contents of each section |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 352 | if ((*I)->size()) |
| 353 | O.write((char*)&(*I)->getData()[0], (*I)->size()); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 354 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 355 | |
| 356 | /// EmitRelocations - emit relocation data from buffer. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 357 | void MachOWriter::EmitRelocations() { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 358 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 359 | E = SectionList.end(); I != E; ++I) |
| 360 | // Emit the relocation entry data for each section. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 361 | if ((*I)->RelocBuffer.size()) |
| 362 | O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size()); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 365 | /// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them |
| 366 | /// each a string table index so that they appear in the correct order in the |
| 367 | /// output file. |
| 368 | void MachOWriter::BufferSymbolAndStringTable() { |
| 369 | // The order of the symbol table is: |
| 370 | // 1. local symbols |
| 371 | // 2. defined external symbols (sorted by name) |
| 372 | // 3. undefined external symbols (sorted by name) |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 373 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 374 | // Before sorting the symbols, check the PendingGlobals for any undefined |
| 375 | // globals that need to be put in the symbol table. |
| 376 | for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(), |
| 377 | E = PendingGlobals.end(); I != E; ++I) { |
| 378 | if (GVOffset[*I] == 0 && GVSection[*I] == 0) { |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 379 | MachOSym UndfSym(*I, Mang->getValueName(*I), MachOSym::NO_SECT, TAI); |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 380 | SymbolTable.push_back(UndfSym); |
| 381 | GVOffset[*I] = -1; |
| 382 | } |
| 383 | } |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 384 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 385 | // Sort the symbols by name, so that when we partition the symbols by scope |
| 386 | // of definition, we won't have to sort by name within each partition. |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 387 | std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp()); |
| 388 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 389 | // Parition the symbol table entries so that all local symbols come before |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 390 | // all symbols with external linkage. { 1 | 2 3 } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 391 | std::partition(SymbolTable.begin(), SymbolTable.end(), MachOSym::PartitionByLocal); |
| 392 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 393 | // Advance iterator to beginning of external symbols and partition so that |
| 394 | // all external symbols defined in this module come before all external |
| 395 | // symbols defined elsewhere. { 1 | 2 | 3 } |
| 396 | for (std::vector<MachOSym>::iterator I = SymbolTable.begin(), |
| 397 | E = SymbolTable.end(); I != E; ++I) { |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 398 | if (!MachOSym::PartitionByLocal(*I)) { |
| 399 | std::partition(I, E, MachOSym::PartitionByDefined); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 400 | break; |
| 401 | } |
| 402 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 403 | |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 404 | // Calculate the starting index for each of the local, extern defined, and |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 405 | // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB |
| 406 | // load command. |
| 407 | for (std::vector<MachOSym>::iterator I = SymbolTable.begin(), |
| 408 | E = SymbolTable.end(); I != E; ++I) { |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 409 | if (MachOSym::PartitionByLocal(*I)) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 410 | ++DySymTab.nlocalsym; |
| 411 | ++DySymTab.iextdefsym; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 412 | ++DySymTab.iundefsym; |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 413 | } else if (MachOSym::PartitionByDefined(*I)) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 414 | ++DySymTab.nextdefsym; |
| 415 | ++DySymTab.iundefsym; |
| 416 | } else { |
| 417 | ++DySymTab.nundefsym; |
| 418 | } |
| 419 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 420 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 421 | // Write out a leading zero byte when emitting string table, for n_strx == 0 |
| 422 | // which means an empty string. |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 423 | OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 424 | StrTOut.outbyte(0); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 425 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 426 | // The order of the string table is: |
| 427 | // 1. strings for external symbols |
| 428 | // 2. strings for local symbols |
| 429 | // Since this is the opposite order from the symbol table, which we have just |
| 430 | // sorted, we can walk the symbol table backwards to output the string table. |
| 431 | for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(), |
| 432 | E = SymbolTable.rend(); I != E; ++I) { |
| 433 | if (I->GVName == "") { |
| 434 | I->n_strx = 0; |
| 435 | } else { |
| 436 | I->n_strx = StrT.size(); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 437 | StrTOut.outstring(I->GVName, I->GVName.length()+1); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 438 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 439 | } |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 440 | |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 441 | OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 442 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 443 | unsigned index = 0; |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 444 | for (std::vector<MachOSym>::iterator I = SymbolTable.begin(), |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 445 | E = SymbolTable.end(); I != E; ++I, ++index) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 446 | // Add the section base address to the section offset in the n_value field |
| 447 | // to calculate the full address. |
| 448 | // FIXME: handle symbols where the n_value field is not the address |
| 449 | GlobalValue *GV = const_cast<GlobalValue*>(I->GV); |
| 450 | if (GV && GVSection[GV]) |
| 451 | I->n_value += GVSection[GV]->addr; |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 452 | if (GV && (GVOffset[GV] == -1)) |
| 453 | GVOffset[GV] = index; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 454 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 455 | // Emit nlist to buffer |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 456 | SymTOut.outword(I->n_strx); |
| 457 | SymTOut.outbyte(I->n_type); |
| 458 | SymTOut.outbyte(I->n_sect); |
| 459 | SymTOut.outhalf(I->n_desc); |
| 460 | SymTOut.outaddr(I->n_value); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 461 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 462 | } |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 463 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 464 | /// CalculateRelocations - For each MachineRelocation in the current section, |
| 465 | /// calculate the index of the section containing the object to be relocated, |
| 466 | /// and the offset into that section. From this information, create the |
| 467 | /// appropriate target-specific MachORelocation type and add buffer it to be |
| 468 | /// written out after we are finished writing out sections. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 469 | void MachOWriter::CalculateRelocations(MachOSection &MOS) { |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 470 | std::vector<MachineRelocation> Relocations = MOS.getRelocations(); |
| 471 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 472 | MachineRelocation &MR = Relocations[i]; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 473 | unsigned TargetSection = MR.getConstantVal(); |
Nate Begeman | af80638 | 2007-03-03 06:18:18 +0000 | [diff] [blame] | 474 | unsigned TargetAddr = 0; |
| 475 | unsigned TargetIndex = 0; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 476 | |
| 477 | // This is a scattered relocation entry if it points to a global value with |
| 478 | // a non-zero offset. |
| 479 | bool Scattered = false; |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 480 | bool Extern = false; |
Nate Begeman | af80638 | 2007-03-03 06:18:18 +0000 | [diff] [blame] | 481 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 482 | // Since we may not have seen the GlobalValue we were interested in yet at |
| 483 | // the time we emitted the relocation for it, fix it up now so that it |
| 484 | // points to the offset into the correct section. |
| 485 | if (MR.isGlobalValue()) { |
| 486 | GlobalValue *GV = MR.getGlobalValue(); |
| 487 | MachOSection *MOSPtr = GVSection[GV]; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 488 | intptr_t Offset = GVOffset[GV]; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 489 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 490 | // If we have never seen the global before, it must be to a symbol |
| 491 | // defined in another module (N_UNDF). |
Nate Begeman | 1257c85 | 2007-01-29 21:20:42 +0000 | [diff] [blame] | 492 | if (!MOSPtr) { |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 493 | // FIXME: need to append stub suffix |
| 494 | Extern = true; |
| 495 | TargetAddr = 0; |
| 496 | TargetIndex = GVOffset[GV]; |
| 497 | } else { |
| 498 | Scattered = TargetSection != 0; |
| 499 | TargetSection = MOSPtr->Index; |
Nate Begeman | af80638 | 2007-03-03 06:18:18 +0000 | [diff] [blame] | 500 | } |
| 501 | MR.setResultPointer((void*)Offset); |
| 502 | } |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 503 | |
Nate Begeman | af80638 | 2007-03-03 06:18:18 +0000 | [diff] [blame] | 504 | // If the symbol is locally defined, pass in the address of the section and |
| 505 | // the section index to the code which will generate the target relocation. |
| 506 | if (!Extern) { |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 507 | MachOSection &To = *SectionList[TargetSection - 1]; |
| 508 | TargetAddr = To.addr; |
| 509 | TargetIndex = To.Index; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 510 | } |
Bill Wendling | 886b412 | 2007-02-03 02:39:40 +0000 | [diff] [blame] | 511 | |
| 512 | OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 513 | OutputBuffer SecOut(MOS.getData(), is64Bit, isLittleEndian); |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 514 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 515 | MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex, |
| 516 | RelocOut, SecOut, Scattered, Extern); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 517 | } |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 520 | // InitMem - Write the value of a Constant to the specified memory location, |
| 521 | // converting it into bytes and relocations. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 522 | void MachOWriter::InitMem(const Constant *C, uintptr_t Offset, |
| 523 | const TargetData *TD, MachOSection* mos) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 524 | typedef std::pair<const Constant*, intptr_t> CPair; |
| 525 | std::vector<CPair> WorkList; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 526 | uint8_t *Addr = &mos->getData()[0]; |
| 527 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 528 | WorkList.push_back(CPair(C,(intptr_t)Addr + Offset)); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 529 | |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 530 | intptr_t ScatteredOffset = 0; |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 531 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 532 | while (!WorkList.empty()) { |
| 533 | const Constant *PC = WorkList.back().first; |
| 534 | intptr_t PA = WorkList.back().second; |
| 535 | WorkList.pop_back(); |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 536 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 537 | if (isa<UndefValue>(PC)) { |
| 538 | continue; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 539 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) { |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 540 | unsigned ElementSize = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 541 | TD->getTypeAllocSize(CP->getType()->getElementType()); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 542 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 543 | WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize)); |
| 544 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) { |
| 545 | // |
| 546 | // FIXME: Handle ConstantExpression. See EE::getConstantValue() |
| 547 | // |
| 548 | switch (CE->getOpcode()) { |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 549 | case Instruction::GetElementPtr: { |
Chris Lattner | 7f6b9d2 | 2007-02-10 20:31:59 +0000 | [diff] [blame] | 550 | SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 551 | ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(), |
Chris Lattner | 7f6b9d2 | 2007-02-10 20:31:59 +0000 | [diff] [blame] | 552 | &Indices[0], Indices.size()); |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 553 | WorkList.push_back(CPair(CE->getOperand(0), PA)); |
| 554 | break; |
| 555 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 556 | case Instruction::Add: |
| 557 | default: |
| 558 | cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; |
| 559 | abort(); |
| 560 | break; |
| 561 | } |
Dan Gohman | 399101a | 2008-05-23 00:17:26 +0000 | [diff] [blame] | 562 | } else if (PC->getType()->isSingleValueType()) { |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 563 | unsigned char *ptr = (unsigned char *)PA; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 564 | switch (PC->getType()->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 565 | case Type::IntegerTyID: { |
| 566 | unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth(); |
| 567 | uint64_t val = cast<ConstantInt>(PC)->getZExtValue(); |
| 568 | if (NumBits <= 8) |
| 569 | ptr[0] = val; |
| 570 | else if (NumBits <= 16) { |
| 571 | if (TD->isBigEndian()) |
| 572 | val = ByteSwap_16(val); |
| 573 | ptr[0] = val; |
| 574 | ptr[1] = val >> 8; |
| 575 | } else if (NumBits <= 32) { |
| 576 | if (TD->isBigEndian()) |
| 577 | val = ByteSwap_32(val); |
| 578 | ptr[0] = val; |
| 579 | ptr[1] = val >> 8; |
| 580 | ptr[2] = val >> 16; |
| 581 | ptr[3] = val >> 24; |
| 582 | } else if (NumBits <= 64) { |
| 583 | if (TD->isBigEndian()) |
| 584 | val = ByteSwap_64(val); |
| 585 | ptr[0] = val; |
| 586 | ptr[1] = val >> 8; |
| 587 | ptr[2] = val >> 16; |
| 588 | ptr[3] = val >> 24; |
| 589 | ptr[4] = val >> 32; |
| 590 | ptr[5] = val >> 40; |
| 591 | ptr[6] = val >> 48; |
| 592 | ptr[7] = val >> 56; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 593 | } else { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 594 | assert(0 && "Not implemented: bit widths > 64"); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 595 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 596 | break; |
| 597 | } |
| 598 | case Type::FloatTyID: { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 599 | uint32_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt(). |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 600 | getZExtValue(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 601 | if (TD->isBigEndian()) |
| 602 | val = ByteSwap_32(val); |
| 603 | ptr[0] = val; |
| 604 | ptr[1] = val >> 8; |
| 605 | ptr[2] = val >> 16; |
| 606 | ptr[3] = val >> 24; |
| 607 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 608 | } |
| 609 | case Type::DoubleTyID: { |
Dale Johannesen | 7111b02 | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 610 | uint64_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt(). |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 611 | getZExtValue(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 612 | if (TD->isBigEndian()) |
| 613 | val = ByteSwap_64(val); |
| 614 | ptr[0] = val; |
| 615 | ptr[1] = val >> 8; |
| 616 | ptr[2] = val >> 16; |
| 617 | ptr[3] = val >> 24; |
| 618 | ptr[4] = val >> 32; |
| 619 | ptr[5] = val >> 40; |
| 620 | ptr[6] = val >> 48; |
| 621 | ptr[7] = val >> 56; |
| 622 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 623 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 624 | case Type::PointerTyID: |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 625 | if (isa<ConstantPointerNull>(PC)) |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 626 | memset(ptr, 0, TD->getPointerSize()); |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 627 | else if (const GlobalValue* GV = dyn_cast<GlobalValue>(PC)) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 628 | // FIXME: what about function stubs? |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame^] | 629 | mos->addRelocation(MachineRelocation::getGV(PA-(intptr_t)Addr, |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 630 | MachineRelocation::VANILLA, |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 631 | const_cast<GlobalValue*>(GV), |
| 632 | ScatteredOffset)); |
| 633 | ScatteredOffset = 0; |
| 634 | } else |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 635 | assert(0 && "Unknown constant pointer type!"); |
| 636 | break; |
| 637 | default: |
| 638 | cerr << "ERROR: Constant unimp for type: " << *PC->getType() << "\n"; |
| 639 | abort(); |
| 640 | } |
| 641 | } else if (isa<ConstantAggregateZero>(PC)) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 642 | memset((void*)PA, 0, (size_t)TD->getTypeAllocSize(PC->getType())); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 643 | } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) { |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 644 | unsigned ElementSize = |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 645 | TD->getTypeAllocSize(CPA->getType()->getElementType()); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 646 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 647 | WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize)); |
| 648 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) { |
| 649 | const StructLayout *SL = |
| 650 | TD->getStructLayout(cast<StructType>(CPS->getType())); |
| 651 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 652 | WorkList.push_back(CPair(CPS->getOperand(i), |
| 653 | PA+SL->getElementOffset(i))); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 654 | } else { |
| 655 | cerr << "Bad Type: " << *PC->getType() << "\n"; |
| 656 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 661 | //===----------------------------------------------------------------------===// |
| 662 | // MachOSym Implementation |
| 663 | //===----------------------------------------------------------------------===// |
| 664 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 665 | MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect, |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 666 | const TargetAsmInfo *TAI) : |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 667 | GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect), |
| 668 | n_desc(0), n_value(0) { |
| 669 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 670 | switch (GV->getLinkage()) { |
| 671 | default: |
| 672 | assert(0 && "Unexpected linkage type!"); |
| 673 | break; |
Duncan Sands | 667d4b8 | 2009-03-07 15:45:40 +0000 | [diff] [blame] | 674 | case GlobalValue::WeakAnyLinkage: |
| 675 | case GlobalValue::WeakODRLinkage: |
| 676 | case GlobalValue::LinkOnceAnyLinkage: |
| 677 | case GlobalValue::LinkOnceODRLinkage: |
Duncan Sands | 4dc2b39 | 2009-03-11 20:14:15 +0000 | [diff] [blame] | 678 | case GlobalValue::CommonLinkage: |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 679 | assert(!isa<Function>(gv) && "Unexpected linkage type for Function!"); |
| 680 | case GlobalValue::ExternalLinkage: |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 681 | GVName = TAI->getGlobalPrefix() + name; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 682 | n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 683 | break; |
Rafael Espindola | bb46f52 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 684 | case GlobalValue::PrivateLinkage: |
| 685 | GVName = TAI->getPrivateGlobalPrefix() + name; |
| 686 | break; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 687 | case GlobalValue::InternalLinkage: |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 688 | GVName = TAI->getGlobalPrefix() + name; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 689 | break; |
| 690 | } |
| 691 | } |
Bruno Cardoso Lopes | a321dcd | 2009-06-03 03:43:31 +0000 | [diff] [blame] | 692 | |
| 693 | } // end namespace llvm |
| 694 | |