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