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