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" |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 26 | #include "llvm/Constants.h" |
| 27 | #include "llvm/DerivedTypes.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 28 | #include "llvm/Module.h" |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 29 | #include "llvm/PassManager.h" |
| 30 | #include "llvm/CodeGen/FileWriters.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 32 | #include "llvm/CodeGen/MachineConstantPool.h" |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetAsmInfo.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetJITInfo.h" |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Mangler.h" |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 38 | #include "llvm/Support/OutputBuffer.h" |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Streams.h" |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 40 | #include <algorithm> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 41 | #include <cstring> |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 42 | using namespace llvm; |
| 43 | |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 44 | /// AddMachOWriter - Concrete function to add the Mach-O writer to the function |
| 45 | /// pass manager. |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 46 | MachineCodeEmitter *llvm::AddMachOWriter(PassManagerBase &PM, |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 47 | std::ostream &O, |
| 48 | TargetMachine &TM) { |
| 49 | MachOWriter *MOW = new MachOWriter(O, TM); |
Dan Gohman | bfae831 | 2008-03-11 22:29:46 +0000 | [diff] [blame] | 50 | PM.add(MOW); |
Bill Wendling | 8f84f1f | 2007-02-08 01:35:27 +0000 | [diff] [blame] | 51 | return &MOW->getMachineCodeEmitter(); |
| 52 | } |
| 53 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 54 | //===----------------------------------------------------------------------===// |
| 55 | // MachOCodeEmitter Implementation |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | |
| 58 | namespace llvm { |
| 59 | /// MachOCodeEmitter - This class is used by the MachOWriter to emit the code |
| 60 | /// for functions to the Mach-O file. |
| 61 | class MachOCodeEmitter : public MachineCodeEmitter { |
| 62 | MachOWriter &MOW; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 63 | |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 64 | /// Target machine description. |
| 65 | TargetMachine &TM; |
| 66 | |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 67 | /// is64Bit/isLittleEndian - This information is inferred from the target |
| 68 | /// machine directly, indicating what header values and flags to set. |
| 69 | bool is64Bit, isLittleEndian; |
| 70 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 71 | /// Relocations - These are the relocations that the function needs, as |
| 72 | /// emitted. |
| 73 | std::vector<MachineRelocation> Relocations; |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 74 | |
| 75 | /// CPLocations - This is a map of constant pool indices to offsets from the |
| 76 | /// start of the section for that constant pool index. |
| 77 | std::vector<intptr_t> CPLocations; |
| 78 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 79 | /// CPSections - This is a map of constant pool indices to the MachOSection |
| 80 | /// containing the constant pool entry for that index. |
| 81 | std::vector<unsigned> CPSections; |
| 82 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 83 | /// JTLocations - This is a map of jump table indices to offsets from the |
| 84 | /// start of the section for that jump table index. |
| 85 | std::vector<intptr_t> JTLocations; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 86 | |
| 87 | /// MBBLocations - This vector is a mapping from MBB ID's to their address. |
| 88 | /// It is filled in by the StartMachineBasicBlock callback and queried by |
| 89 | /// the getMachineBasicBlockAddress callback. |
| 90 | std::vector<intptr_t> MBBLocations; |
| 91 | |
| 92 | public: |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 93 | MachOCodeEmitter(MachOWriter &mow) : MOW(mow), TM(MOW.TM) { |
| 94 | is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64; |
| 95 | isLittleEndian = TM.getTargetData()->isLittleEndian(); |
| 96 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 97 | |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 98 | virtual void startFunction(MachineFunction &MF); |
| 99 | virtual bool finishFunction(MachineFunction &MF); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 100 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 101 | virtual void addRelocation(const MachineRelocation &MR) { |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 102 | Relocations.push_back(MR); |
| 103 | } |
| 104 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 105 | void emitConstantPool(MachineConstantPool *MCP); |
| 106 | void emitJumpTables(MachineJumpTableInfo *MJTI); |
| 107 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 108 | virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 109 | assert(CPLocations.size() > Index && "CP not emitted!"); |
Nate Begeman | a0a6278 | 2007-02-28 09:16:38 +0000 | [diff] [blame] | 110 | return CPLocations[Index]; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 111 | } |
| 112 | virtual intptr_t getJumpTableEntryAddress(unsigned Index) const { |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 113 | assert(JTLocations.size() > Index && "JT not emitted!"); |
| 114 | return JTLocations[Index]; |
| 115 | } |
| 116 | |
| 117 | virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) { |
| 118 | if (MBBLocations.size() <= (unsigned)MBB->getNumber()) |
| 119 | MBBLocations.resize((MBB->getNumber()+1)*2); |
| 120 | MBBLocations[MBB->getNumber()] = getCurrentPCOffset(); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const { |
| 124 | assert(MBBLocations.size() > (unsigned)MBB->getNumber() && |
| 125 | MBBLocations[MBB->getNumber()] && "MBB not emitted!"); |
| 126 | return MBBLocations[MBB->getNumber()]; |
| 127 | } |
| 128 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 129 | virtual intptr_t getLabelAddress(uint64_t Label) const { |
| 130 | assert(0 && "get Label not implemented"); |
| 131 | abort(); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | virtual void emitLabel(uint64_t LabelID) { |
| 136 | assert(0 && "emit Label not implemented"); |
| 137 | abort(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { } |
| 142 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 143 | /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE! |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 144 | virtual void startFunctionStub(const GlobalValue* F, unsigned StubSize, |
| 145 | unsigned Alignment = 1) { |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 146 | assert(0 && "JIT specific function called!"); |
| 147 | abort(); |
| 148 | } |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 149 | virtual void *finishFunctionStub(const GlobalValue* F) { |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 150 | assert(0 && "JIT specific function called!"); |
| 151 | abort(); |
| 152 | return 0; |
| 153 | } |
| 154 | }; |
| 155 | } |
| 156 | |
| 157 | /// startFunction - This callback is invoked when a new machine function is |
| 158 | /// about to be emitted. |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 159 | void MachOCodeEmitter::startFunction(MachineFunction &MF) { |
| 160 | const TargetData *TD = TM.getTargetData(); |
| 161 | const Function *F = MF.getFunction(); |
| 162 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 163 | // Align the output buffer to the appropriate alignment, power of 2. |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 164 | unsigned FnAlign = F->getAlignment(); |
Chris Lattner | d2b7cec | 2007-02-14 05:52:17 +0000 | [diff] [blame] | 165 | unsigned TDAlign = TD->getPrefTypeAlignment(F->getType()); |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 166 | unsigned Align = Log2_32(std::max(FnAlign, TDAlign)); |
| 167 | assert(!(Align & (Align-1)) && "Alignment is not a power of two!"); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 168 | |
| 169 | // Get the Mach-O Section that this function belongs in. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 170 | MachOWriter::MachOSection *MOS = MOW.getTextSection(); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 171 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 172 | // FIXME: better memory management |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 173 | MOS->SectionData.reserve(4096); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 174 | BufferBegin = &MOS->SectionData[0]; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 175 | BufferEnd = BufferBegin + MOS->SectionData.capacity(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 176 | |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 177 | // Upgrade the section alignment if required. |
| 178 | if (MOS->align < Align) MOS->align = Align; |
| 179 | |
| 180 | // Round the size up to the correct alignment for starting the new function. |
| 181 | if ((MOS->size & ((1 << Align) - 1)) != 0) { |
| 182 | MOS->size += (1 << Align); |
| 183 | MOS->size &= ~((1 << Align) - 1); |
| 184 | } |
| 185 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 186 | // FIXME: Using MOS->size directly here instead of calculating it from the |
| 187 | // output buffer size (impossible because the code emitter deals only in raw |
| 188 | // bytes) forces us to manually synchronize size and write padding zero bytes |
| 189 | // to the output buffer for all non-text sections. For text sections, we do |
| 190 | // not synchonize the output buffer, and we just blow up if anyone tries to |
| 191 | // write non-code to it. An assert should probably be added to |
| 192 | // AddSymbolToSection to prevent calling it on the text section. |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 193 | CurBufferPtr = BufferBegin + MOS->size; |
| 194 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 195 | // Clear per-function data structures. |
| 196 | CPLocations.clear(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 197 | CPSections.clear(); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 198 | JTLocations.clear(); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 199 | MBBLocations.clear(); |
| 200 | } |
| 201 | |
| 202 | /// finishFunction - This callback is invoked after the function is completely |
| 203 | /// finished. |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 204 | bool MachOCodeEmitter::finishFunction(MachineFunction &MF) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 205 | // Get the Mach-O Section that this function belongs in. |
| 206 | MachOWriter::MachOSection *MOS = MOW.getTextSection(); |
| 207 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 208 | // Get a symbol for the function to add to the symbol table |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 209 | // FIXME: it seems like we should call something like AddSymbolToSection |
| 210 | // in startFunction rather than changing the section size and symbol n_value |
| 211 | // here. |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 212 | const GlobalValue *FuncV = MF.getFunction(); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 213 | MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index, TM); |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 214 | FnSym.n_value = MOS->size; |
| 215 | MOS->size = CurBufferPtr - BufferBegin; |
| 216 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 217 | // Emit constant pool to appropriate section(s) |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 218 | emitConstantPool(MF.getConstantPool()); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 219 | |
| 220 | // Emit jump tables to appropriate section |
Nate Begeman | c2b2d6a | 2007-02-07 05:47:16 +0000 | [diff] [blame] | 221 | emitJumpTables(MF.getJumpTableInfo()); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 222 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 223 | // If we have emitted any relocations to function-specific objects such as |
| 224 | // basic blocks, constant pools entries, or jump tables, record their |
| 225 | // addresses now so that we can rewrite them with the correct addresses |
| 226 | // later. |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 227 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 228 | MachineRelocation &MR = Relocations[i]; |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 229 | intptr_t Addr; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 230 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 231 | if (MR.isBasicBlock()) { |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 232 | Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 233 | MR.setConstantVal(MOS->Index); |
| 234 | MR.setResultPointer((void*)Addr); |
| 235 | } else if (MR.isJumpTableIndex()) { |
| 236 | Addr = getJumpTableEntryAddress(MR.getJumpTableIndex()); |
| 237 | MR.setConstantVal(MOW.getJumpTableSection()->Index); |
| 238 | MR.setResultPointer((void*)Addr); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 239 | } else if (MR.isConstantPoolIndex()) { |
| 240 | Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex()); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 241 | MR.setConstantVal(CPSections[MR.getConstantPoolIndex()]); |
| 242 | MR.setResultPointer((void*)Addr); |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 243 | } else if (MR.isGlobalValue()) { |
| 244 | // FIXME: This should be a set or something that uniques |
| 245 | MOW.PendingGlobals.push_back(MR.getGlobalValue()); |
| 246 | } else { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 247 | assert(0 && "Unhandled relocation type"); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 248 | } |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 249 | MOS->Relocations.push_back(MR); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 250 | } |
| 251 | Relocations.clear(); |
| 252 | |
| 253 | // Finally, add it to the symtab. |
| 254 | MOW.SymbolTable.push_back(FnSym); |
| 255 | return false; |
| 256 | } |
| 257 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 258 | /// emitConstantPool - For each constant pool entry, figure out which section |
| 259 | /// the constant should live in, allocate space for it, and emit it to the |
| 260 | /// Section data buffer. |
| 261 | void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 262 | const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants(); |
| 263 | if (CP.empty()) return; |
| 264 | |
| 265 | // FIXME: handle PIC codegen |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 266 | bool isPIC = TM.getRelocationModel() == Reloc::PIC_; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 267 | assert(!isPIC && "PIC codegen not yet handled for mach-o jump tables!"); |
| 268 | |
| 269 | // Although there is no strict necessity that I am aware of, we will do what |
| 270 | // gcc for OS X does and put each constant pool entry in a section of constant |
| 271 | // objects of a certain size. That means that float constants go in the |
| 272 | // literal4 section, and double objects go in literal8, etc. |
| 273 | // |
| 274 | // FIXME: revisit this decision if we ever do the "stick everything into one |
| 275 | // "giant object for PIC" optimization. |
| 276 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 277 | const Type *Ty = CP[i].getType(); |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 278 | unsigned Size = TM.getTargetData()->getABITypeSize(Ty); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 279 | |
Nate Begeman | 1257c85 | 2007-01-29 21:20:42 +0000 | [diff] [blame] | 280 | MachOWriter::MachOSection *Sec = MOW.getConstSection(CP[i].Val.ConstVal); |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 281 | OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 282 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 283 | CPLocations.push_back(Sec->SectionData.size()); |
| 284 | CPSections.push_back(Sec->Index); |
| 285 | |
| 286 | // FIXME: remove when we have unified size + output buffer |
| 287 | Sec->size += Size; |
| 288 | |
| 289 | // Allocate space in the section for the global. |
| 290 | // FIXME: need alignment? |
| 291 | // FIXME: share between here and AddSymbolToSection? |
| 292 | for (unsigned j = 0; j < Size; ++j) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 293 | SecDataOut.outbyte(0); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 294 | |
| 295 | MOW.InitMem(CP[i].Val.ConstVal, &Sec->SectionData[0], CPLocations[i], |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 296 | TM.getTargetData(), Sec->Relocations); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 297 | } |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /// emitJumpTables - Emit all the jump tables for a given jump table info |
| 301 | /// record to the appropriate section. |
| 302 | void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) { |
| 303 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 304 | if (JT.empty()) return; |
| 305 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 306 | // FIXME: handle PIC codegen |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 307 | bool isPIC = TM.getRelocationModel() == Reloc::PIC_; |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 308 | assert(!isPIC && "PIC codegen not yet handled for mach-o jump tables!"); |
| 309 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 310 | MachOWriter::MachOSection *Sec = MOW.getJumpTableSection(); |
| 311 | unsigned TextSecIndex = MOW.getTextSection()->Index; |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 312 | OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 313 | |
| 314 | for (unsigned i = 0, e = JT.size(); i != e; ++i) { |
| 315 | // For each jump table, record its offset from the start of the section, |
| 316 | // reserve space for the relocations to the MBBs, and add the relocations. |
| 317 | const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 318 | JTLocations.push_back(Sec->SectionData.size()); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 319 | for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 320 | MachineRelocation MR(MOW.GetJTRelocation(Sec->SectionData.size(), |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 321 | MBBs[mi])); |
| 322 | MR.setResultPointer((void *)JTLocations[i]); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 323 | MR.setConstantVal(TextSecIndex); |
| 324 | Sec->Relocations.push_back(MR); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 325 | SecDataOut.outaddr(0); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 328 | // FIXME: remove when we have unified size + output buffer |
| 329 | Sec->size = Sec->SectionData.size(); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 332 | //===----------------------------------------------------------------------===// |
| 333 | // MachOWriter Implementation |
| 334 | //===----------------------------------------------------------------------===// |
| 335 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 336 | char MachOWriter::ID = 0; |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 337 | MachOWriter::MachOWriter(std::ostream &o, TargetMachine &tm) |
| 338 | : MachineFunctionPass((intptr_t)&ID), O(o), TM(tm) { |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 339 | is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64; |
| 340 | isLittleEndian = TM.getTargetData()->isLittleEndian(); |
| 341 | |
| 342 | // Create the machine code emitter object for this target. |
Bill Wendling | e911615 | 2007-01-17 09:06:13 +0000 | [diff] [blame] | 343 | MCE = new MachOCodeEmitter(*this); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | MachOWriter::~MachOWriter() { |
| 347 | delete MCE; |
| 348 | } |
| 349 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 350 | void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) { |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 351 | const Type *Ty = GV->getType()->getElementType(); |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 352 | unsigned Size = TM.getTargetData()->getABITypeSize(Ty); |
Duncan Sands | d102593 | 2008-01-29 06:23:44 +0000 | [diff] [blame] | 353 | unsigned Align = TM.getTargetData()->getPreferredAlignment(GV); |
| 354 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 355 | // Reserve space in the .bss section for this symbol while maintaining the |
| 356 | // desired section alignment, which must be at least as much as required by |
| 357 | // this symbol. |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 358 | OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 359 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 360 | if (Align) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 361 | uint64_t OrigSize = Sec->size; |
| 362 | Align = Log2_32(Align); |
| 363 | Sec->align = std::max(unsigned(Sec->align), Align); |
| 364 | Sec->size = (Sec->size + Align - 1) & ~(Align-1); |
| 365 | |
| 366 | // Add alignment padding to buffer as well. |
| 367 | // FIXME: remove when we have unified size + output buffer |
| 368 | unsigned AlignedSize = Sec->size - OrigSize; |
| 369 | for (unsigned i = 0; i < AlignedSize; ++i) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 370 | SecDataOut.outbyte(0); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 371 | } |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 372 | // Globals without external linkage apparently do not go in the symbol table. |
| 373 | if (GV->getLinkage() != GlobalValue::InternalLinkage) { |
| 374 | MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TM); |
| 375 | Sym.n_value = Sec->size; |
| 376 | SymbolTable.push_back(Sym); |
| 377 | } |
| 378 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 379 | // Record the offset of the symbol, and then allocate space for it. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 380 | // FIXME: remove when we have unified size + output buffer |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 381 | Sec->size += Size; |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 382 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 383 | // Now that we know what section the GlovalVariable is going to be emitted |
| 384 | // into, update our mappings. |
| 385 | // FIXME: We may also need to update this when outputting non-GlobalVariable |
| 386 | // GlobalValues such as functions. |
| 387 | GVSection[GV] = Sec; |
| 388 | GVOffset[GV] = Sec->SectionData.size(); |
| 389 | |
| 390 | // Allocate space in the section for the global. |
| 391 | for (unsigned i = 0; i < Size; ++i) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 392 | SecDataOut.outbyte(0); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 395 | void MachOWriter::EmitGlobal(GlobalVariable *GV) { |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 396 | const Type *Ty = GV->getType()->getElementType(); |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 397 | unsigned Size = TM.getTargetData()->getABITypeSize(Ty); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 398 | bool NoInit = !GV->hasInitializer(); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 399 | |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 400 | // If this global has a zero initializer, it is part of the .bss or common |
| 401 | // section. |
| 402 | if (NoInit || GV->getInitializer()->isNullValue()) { |
| 403 | // If this global is part of the common block, add it now. Variables are |
| 404 | // part of the common block if they are zero initialized and allowed to be |
| 405 | // merged with other symbols. |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 406 | if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() || |
| 407 | GV->hasCommonLinkage()) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 408 | MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV), MachOSym::NO_SECT,TM); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 409 | // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in |
| 410 | // bytes of the symbol. |
| 411 | ExtOrCommonSym.n_value = Size; |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 412 | SymbolTable.push_back(ExtOrCommonSym); |
| 413 | // Remember that we've seen this symbol |
| 414 | GVOffset[GV] = Size; |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 415 | return; |
| 416 | } |
| 417 | // Otherwise, this symbol is part of the .bss section. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 418 | MachOSection *BSS = getBSSSection(); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 419 | AddSymbolToSection(BSS, GV); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | // Scalar read-only data goes in a literal section if the scalar is 4, 8, or |
| 424 | // 16 bytes, or a cstring. Other read only data goes into a regular const |
| 425 | // section. Read-write data goes in the data section. |
Nate Begeman | 1257c85 | 2007-01-29 21:20:42 +0000 | [diff] [blame] | 426 | MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) : |
| 427 | getDataSection(); |
Nate Begeman | f8f2c5a | 2006-08-25 06:36:58 +0000 | [diff] [blame] | 428 | AddSymbolToSection(Sec, GV); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 429 | InitMem(GV->getInitializer(), &Sec->SectionData[0], GVOffset[GV], |
| 430 | TM.getTargetData(), Sec->Relocations); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
| 434 | bool MachOWriter::runOnMachineFunction(MachineFunction &MF) { |
| 435 | // Nothing to do here, this is all done through the MCE object. |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | bool MachOWriter::doInitialization(Module &M) { |
| 440 | // Set the magic value, now that we know the pointer size and endianness |
| 441 | Header.setMagic(isLittleEndian, is64Bit); |
| 442 | |
| 443 | // Set the file type |
| 444 | // FIXME: this only works for object files, we do not support the creation |
| 445 | // of dynamic libraries or executables at this time. |
| 446 | Header.filetype = MachOHeader::MH_OBJECT; |
| 447 | |
| 448 | Mang = new Mangler(M); |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | /// doFinalization - Now that the module has been completely processed, emit |
| 453 | /// the Mach-O file to 'O'. |
| 454 | bool MachOWriter::doFinalization(Module &M) { |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 455 | // FIXME: we don't handle debug info yet, we should probably do that. |
| 456 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 457 | // Okay, the.text section has been completed, build the .data, .bss, and |
| 458 | // "common" sections next. |
| 459 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 460 | I != E; ++I) |
| 461 | EmitGlobal(I); |
| 462 | |
| 463 | // Emit the header and load commands. |
| 464 | EmitHeaderAndLoadCommands(); |
| 465 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 466 | // Emit the various sections and their relocation info. |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 467 | EmitSections(); |
| 468 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 469 | // Write the symbol table and the string table to the end of the file. |
| 470 | O.write((char*)&SymT[0], SymT.size()); |
| 471 | O.write((char*)&StrT[0], StrT.size()); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 472 | |
| 473 | // We are done with the abstract symbols. |
| 474 | SectionList.clear(); |
| 475 | SymbolTable.clear(); |
| 476 | DynamicSymbolTable.clear(); |
| 477 | |
| 478 | // Release the name mangler object. |
| 479 | delete Mang; Mang = 0; |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | void MachOWriter::EmitHeaderAndLoadCommands() { |
| 484 | // Step #0: Fill in the segment load command size, since we need it to figure |
| 485 | // out the rest of the header fields |
| 486 | MachOSegment SEG("", is64Bit); |
| 487 | SEG.nsects = SectionList.size(); |
| 488 | SEG.cmdsize = SEG.cmdSize(is64Bit) + |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 489 | SEG.nsects * SectionList[0]->cmdSize(is64Bit); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 490 | |
| 491 | // Step #1: calculate the number of load commands. We always have at least |
| 492 | // one, for the LC_SEGMENT load command, plus two for the normal |
| 493 | // and dynamic symbol tables, if there are any symbols. |
| 494 | Header.ncmds = SymbolTable.empty() ? 1 : 3; |
| 495 | |
| 496 | // Step #2: calculate the size of the load commands |
| 497 | Header.sizeofcmds = SEG.cmdsize; |
| 498 | if (!SymbolTable.empty()) |
| 499 | Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize; |
| 500 | |
| 501 | // Step #3: write the header to the file |
| 502 | // Local alias to shortenify coming code. |
| 503 | DataBuffer &FH = Header.HeaderData; |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 504 | OutputBuffer FHOut(FH, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 505 | |
| 506 | FHOut.outword(Header.magic); |
Bill Wendling | 2b72182 | 2007-01-24 07:13:56 +0000 | [diff] [blame] | 507 | FHOut.outword(TM.getMachOWriterInfo()->getCPUType()); |
| 508 | FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType()); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 509 | FHOut.outword(Header.filetype); |
| 510 | FHOut.outword(Header.ncmds); |
| 511 | FHOut.outword(Header.sizeofcmds); |
| 512 | FHOut.outword(Header.flags); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 513 | if (is64Bit) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 514 | FHOut.outword(Header.reserved); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 515 | |
| 516 | // 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] | 517 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 518 | E = SectionList.end(); I != E; ++I) |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 519 | SEG.filesize += (*I)->size; |
| 520 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 521 | SEG.vmsize = SEG.filesize; |
| 522 | SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds; |
| 523 | |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 524 | FHOut.outword(SEG.cmd); |
| 525 | FHOut.outword(SEG.cmdsize); |
| 526 | FHOut.outstring(SEG.segname, 16); |
| 527 | FHOut.outaddr(SEG.vmaddr); |
| 528 | FHOut.outaddr(SEG.vmsize); |
| 529 | FHOut.outaddr(SEG.fileoff); |
| 530 | FHOut.outaddr(SEG.filesize); |
| 531 | FHOut.outword(SEG.maxprot); |
| 532 | FHOut.outword(SEG.initprot); |
| 533 | FHOut.outword(SEG.nsects); |
| 534 | FHOut.outword(SEG.flags); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 535 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 536 | // Step #5: Finish filling in the fields of the MachOSections |
| 537 | uint64_t currentAddr = 0; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 538 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 539 | E = SectionList.end(); I != E; ++I) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 540 | MachOSection *MOS = *I; |
| 541 | MOS->addr = currentAddr; |
| 542 | MOS->offset = currentAddr + SEG.fileoff; |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 543 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 544 | // FIXME: do we need to do something with alignment here? |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 545 | currentAddr += MOS->size; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 548 | // Step #6: Emit the symbol table to temporary buffers, so that we know the |
| 549 | // size of the string table when we write the next load command. This also |
| 550 | // sorts and assigns indices to each of the symbols, which is necessary for |
| 551 | // emitting relocations to externally-defined objects. |
| 552 | BufferSymbolAndStringTable(); |
| 553 | |
| 554 | // 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] | 555 | // the section commands for each section |
| 556 | currentAddr += SEG.fileoff; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 557 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 558 | E = SectionList.end(); I != E; ++I) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 559 | MachOSection *MOS = *I; |
| 560 | // Convert the relocations to target-specific relocations, and fill in the |
| 561 | // relocation offset for this section. |
| 562 | CalculateRelocations(*MOS); |
| 563 | MOS->reloff = MOS->nreloc ? currentAddr : 0; |
| 564 | currentAddr += MOS->nreloc * 8; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 565 | |
| 566 | // write the finalized section command to the output buffer |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 567 | FHOut.outstring(MOS->sectname, 16); |
| 568 | FHOut.outstring(MOS->segname, 16); |
| 569 | FHOut.outaddr(MOS->addr); |
| 570 | FHOut.outaddr(MOS->size); |
| 571 | FHOut.outword(MOS->offset); |
| 572 | FHOut.outword(MOS->align); |
| 573 | FHOut.outword(MOS->reloff); |
| 574 | FHOut.outword(MOS->nreloc); |
| 575 | FHOut.outword(MOS->flags); |
| 576 | FHOut.outword(MOS->reserved1); |
| 577 | FHOut.outword(MOS->reserved2); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 578 | if (is64Bit) |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 579 | FHOut.outword(MOS->reserved3); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 582 | // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 583 | SymTab.symoff = currentAddr; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 584 | SymTab.nsyms = SymbolTable.size(); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 585 | SymTab.stroff = SymTab.symoff + SymT.size(); |
| 586 | SymTab.strsize = StrT.size(); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 587 | FHOut.outword(SymTab.cmd); |
| 588 | FHOut.outword(SymTab.cmdsize); |
| 589 | FHOut.outword(SymTab.symoff); |
| 590 | FHOut.outword(SymTab.nsyms); |
| 591 | FHOut.outword(SymTab.stroff); |
| 592 | FHOut.outword(SymTab.strsize); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 593 | |
| 594 | // FIXME: set DySymTab fields appropriately |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 595 | // We should probably just update these in BufferSymbolAndStringTable since |
| 596 | // thats where we're partitioning up the different kinds of symbols. |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 597 | FHOut.outword(DySymTab.cmd); |
| 598 | FHOut.outword(DySymTab.cmdsize); |
| 599 | FHOut.outword(DySymTab.ilocalsym); |
| 600 | FHOut.outword(DySymTab.nlocalsym); |
| 601 | FHOut.outword(DySymTab.iextdefsym); |
| 602 | FHOut.outword(DySymTab.nextdefsym); |
| 603 | FHOut.outword(DySymTab.iundefsym); |
| 604 | FHOut.outword(DySymTab.nundefsym); |
| 605 | FHOut.outword(DySymTab.tocoff); |
| 606 | FHOut.outword(DySymTab.ntoc); |
| 607 | FHOut.outword(DySymTab.modtaboff); |
| 608 | FHOut.outword(DySymTab.nmodtab); |
| 609 | FHOut.outword(DySymTab.extrefsymoff); |
| 610 | FHOut.outword(DySymTab.nextrefsyms); |
| 611 | FHOut.outword(DySymTab.indirectsymoff); |
| 612 | FHOut.outword(DySymTab.nindirectsyms); |
| 613 | FHOut.outword(DySymTab.extreloff); |
| 614 | FHOut.outword(DySymTab.nextrel); |
| 615 | FHOut.outword(DySymTab.locreloff); |
| 616 | FHOut.outword(DySymTab.nlocrel); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 617 | |
| 618 | O.write((char*)&FH[0], FH.size()); |
| 619 | } |
| 620 | |
| 621 | /// EmitSections - Now that we have constructed the file header and load |
| 622 | /// commands, emit the data for each section to the file. |
| 623 | void MachOWriter::EmitSections() { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 624 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 625 | E = SectionList.end(); I != E; ++I) |
| 626 | // Emit the contents of each section |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 627 | O.write((char*)&(*I)->SectionData[0], (*I)->size); |
| 628 | for (std::vector<MachOSection*>::iterator I = SectionList.begin(), |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 629 | E = SectionList.end(); I != E; ++I) |
| 630 | // Emit the relocation entry data for each section. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 631 | O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size()); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 634 | /// PartitionByLocal - Simple boolean predicate that returns true if Sym is |
| 635 | /// a local symbol rather than an external symbol. |
| 636 | bool MachOWriter::PartitionByLocal(const MachOSym &Sym) { |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 637 | return (Sym.n_type & (MachOSym::N_EXT | MachOSym::N_PEXT)) == 0; |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 640 | /// PartitionByDefined - Simple boolean predicate that returns true if Sym is |
| 641 | /// defined in this module. |
| 642 | bool MachOWriter::PartitionByDefined(const MachOSym &Sym) { |
| 643 | // FIXME: Do N_ABS or N_INDR count as defined? |
| 644 | return (Sym.n_type & MachOSym::N_SECT) == MachOSym::N_SECT; |
| 645 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 646 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 647 | /// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them |
| 648 | /// each a string table index so that they appear in the correct order in the |
| 649 | /// output file. |
| 650 | void MachOWriter::BufferSymbolAndStringTable() { |
| 651 | // The order of the symbol table is: |
| 652 | // 1. local symbols |
| 653 | // 2. defined external symbols (sorted by name) |
| 654 | // 3. undefined external symbols (sorted by name) |
| 655 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 656 | // Before sorting the symbols, check the PendingGlobals for any undefined |
| 657 | // globals that need to be put in the symbol table. |
| 658 | for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(), |
| 659 | E = PendingGlobals.end(); I != E; ++I) { |
| 660 | if (GVOffset[*I] == 0 && GVSection[*I] == 0) { |
| 661 | MachOSym UndfSym(*I, Mang->getValueName(*I), MachOSym::NO_SECT, TM); |
| 662 | SymbolTable.push_back(UndfSym); |
| 663 | GVOffset[*I] = -1; |
| 664 | } |
| 665 | } |
| 666 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 667 | // Sort the symbols by name, so that when we partition the symbols by scope |
| 668 | // of definition, we won't have to sort by name within each partition. |
| 669 | std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSymCmp()); |
| 670 | |
| 671 | // Parition the symbol table entries so that all local symbols come before |
| 672 | // all symbols with external linkage. { 1 | 2 3 } |
| 673 | std::partition(SymbolTable.begin(), SymbolTable.end(), PartitionByLocal); |
| 674 | |
| 675 | // Advance iterator to beginning of external symbols and partition so that |
| 676 | // all external symbols defined in this module come before all external |
| 677 | // symbols defined elsewhere. { 1 | 2 | 3 } |
| 678 | for (std::vector<MachOSym>::iterator I = SymbolTable.begin(), |
| 679 | E = SymbolTable.end(); I != E; ++I) { |
| 680 | if (!PartitionByLocal(*I)) { |
| 681 | std::partition(I, E, PartitionByDefined); |
| 682 | break; |
| 683 | } |
| 684 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 685 | |
| 686 | // Calculate the starting index for each of the local, extern defined, and |
| 687 | // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB |
| 688 | // load command. |
| 689 | for (std::vector<MachOSym>::iterator I = SymbolTable.begin(), |
| 690 | E = SymbolTable.end(); I != E; ++I) { |
| 691 | if (PartitionByLocal(*I)) { |
| 692 | ++DySymTab.nlocalsym; |
| 693 | ++DySymTab.iextdefsym; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 694 | ++DySymTab.iundefsym; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 695 | } else if (PartitionByDefined(*I)) { |
| 696 | ++DySymTab.nextdefsym; |
| 697 | ++DySymTab.iundefsym; |
| 698 | } else { |
| 699 | ++DySymTab.nundefsym; |
| 700 | } |
| 701 | } |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 702 | |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 703 | // Write out a leading zero byte when emitting string table, for n_strx == 0 |
| 704 | // which means an empty string. |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 705 | OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 706 | StrTOut.outbyte(0); |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 707 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 708 | // The order of the string table is: |
| 709 | // 1. strings for external symbols |
| 710 | // 2. strings for local symbols |
| 711 | // Since this is the opposite order from the symbol table, which we have just |
| 712 | // sorted, we can walk the symbol table backwards to output the string table. |
| 713 | for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(), |
| 714 | E = SymbolTable.rend(); I != E; ++I) { |
| 715 | if (I->GVName == "") { |
| 716 | I->n_strx = 0; |
| 717 | } else { |
| 718 | I->n_strx = StrT.size(); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 719 | StrTOut.outstring(I->GVName, I->GVName.length()+1); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 720 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 721 | } |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 722 | |
Bill Wendling | c904a5b | 2007-01-18 01:23:11 +0000 | [diff] [blame] | 723 | OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian); |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 724 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 725 | unsigned index = 0; |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 726 | for (std::vector<MachOSym>::iterator I = SymbolTable.begin(), |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 727 | E = SymbolTable.end(); I != E; ++I, ++index) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 728 | // Add the section base address to the section offset in the n_value field |
| 729 | // to calculate the full address. |
| 730 | // FIXME: handle symbols where the n_value field is not the address |
| 731 | GlobalValue *GV = const_cast<GlobalValue*>(I->GV); |
| 732 | if (GV && GVSection[GV]) |
| 733 | I->n_value += GVSection[GV]->addr; |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 734 | if (GV && (GVOffset[GV] == -1)) |
| 735 | GVOffset[GV] = index; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 736 | |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 737 | // Emit nlist to buffer |
Bill Wendling | 203d3e4 | 2007-01-17 22:22:31 +0000 | [diff] [blame] | 738 | SymTOut.outword(I->n_strx); |
| 739 | SymTOut.outbyte(I->n_type); |
| 740 | SymTOut.outbyte(I->n_sect); |
| 741 | SymTOut.outhalf(I->n_desc); |
| 742 | SymTOut.outaddr(I->n_value); |
Nate Begeman | d2030e6 | 2006-08-26 15:46:34 +0000 | [diff] [blame] | 743 | } |
Nate Begeman | eb883af | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 744 | } |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 745 | |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 746 | /// CalculateRelocations - For each MachineRelocation in the current section, |
| 747 | /// calculate the index of the section containing the object to be relocated, |
| 748 | /// and the offset into that section. From this information, create the |
| 749 | /// appropriate target-specific MachORelocation type and add buffer it to be |
| 750 | /// written out after we are finished writing out sections. |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 751 | void MachOWriter::CalculateRelocations(MachOSection &MOS) { |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 752 | for (unsigned i = 0, e = MOS.Relocations.size(); i != e; ++i) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 753 | MachineRelocation &MR = MOS.Relocations[i]; |
| 754 | unsigned TargetSection = MR.getConstantVal(); |
Nate Begeman | af80638 | 2007-03-03 06:18:18 +0000 | [diff] [blame] | 755 | unsigned TargetAddr = 0; |
| 756 | unsigned TargetIndex = 0; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 757 | |
| 758 | // This is a scattered relocation entry if it points to a global value with |
| 759 | // a non-zero offset. |
| 760 | bool Scattered = false; |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 761 | bool Extern = false; |
Nate Begeman | af80638 | 2007-03-03 06:18:18 +0000 | [diff] [blame] | 762 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 763 | // Since we may not have seen the GlobalValue we were interested in yet at |
| 764 | // the time we emitted the relocation for it, fix it up now so that it |
| 765 | // points to the offset into the correct section. |
| 766 | if (MR.isGlobalValue()) { |
| 767 | GlobalValue *GV = MR.getGlobalValue(); |
| 768 | MachOSection *MOSPtr = GVSection[GV]; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 769 | intptr_t Offset = GVOffset[GV]; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 770 | |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 771 | // If we have never seen the global before, it must be to a symbol |
| 772 | // defined in another module (N_UNDF). |
Nate Begeman | 1257c85 | 2007-01-29 21:20:42 +0000 | [diff] [blame] | 773 | if (!MOSPtr) { |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 774 | // FIXME: need to append stub suffix |
| 775 | Extern = true; |
| 776 | TargetAddr = 0; |
| 777 | TargetIndex = GVOffset[GV]; |
| 778 | } else { |
| 779 | Scattered = TargetSection != 0; |
| 780 | TargetSection = MOSPtr->Index; |
Nate Begeman | af80638 | 2007-03-03 06:18:18 +0000 | [diff] [blame] | 781 | } |
| 782 | MR.setResultPointer((void*)Offset); |
| 783 | } |
| 784 | |
| 785 | // If the symbol is locally defined, pass in the address of the section and |
| 786 | // the section index to the code which will generate the target relocation. |
| 787 | if (!Extern) { |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 788 | MachOSection &To = *SectionList[TargetSection - 1]; |
| 789 | TargetAddr = To.addr; |
| 790 | TargetIndex = To.Index; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 791 | } |
Bill Wendling | 886b412 | 2007-02-03 02:39:40 +0000 | [diff] [blame] | 792 | |
| 793 | OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian); |
| 794 | OutputBuffer SecOut(MOS.SectionData, is64Bit, isLittleEndian); |
Nate Begeman | fec910c | 2007-02-28 07:40:50 +0000 | [diff] [blame] | 795 | |
| 796 | MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex, |
| 797 | RelocOut, SecOut, Scattered, Extern); |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 798 | } |
Nate Begeman | 019f851 | 2006-09-10 23:03:44 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 801 | // InitMem - Write the value of a Constant to the specified memory location, |
| 802 | // converting it into bytes and relocations. |
| 803 | void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset, |
| 804 | const TargetData *TD, |
| 805 | std::vector<MachineRelocation> &MRs) { |
| 806 | typedef std::pair<const Constant*, intptr_t> CPair; |
| 807 | std::vector<CPair> WorkList; |
| 808 | |
| 809 | WorkList.push_back(CPair(C,(intptr_t)Addr + Offset)); |
| 810 | |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 811 | intptr_t ScatteredOffset = 0; |
| 812 | |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 813 | while (!WorkList.empty()) { |
| 814 | const Constant *PC = WorkList.back().first; |
| 815 | intptr_t PA = WorkList.back().second; |
| 816 | WorkList.pop_back(); |
| 817 | |
| 818 | if (isa<UndefValue>(PC)) { |
| 819 | continue; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 820 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) { |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 821 | unsigned ElementSize = |
| 822 | TD->getABITypeSize(CP->getType()->getElementType()); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 823 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 824 | WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize)); |
| 825 | } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) { |
| 826 | // |
| 827 | // FIXME: Handle ConstantExpression. See EE::getConstantValue() |
| 828 | // |
| 829 | switch (CE->getOpcode()) { |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 830 | case Instruction::GetElementPtr: { |
Chris Lattner | 7f6b9d2 | 2007-02-10 20:31:59 +0000 | [diff] [blame] | 831 | SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 832 | ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(), |
Chris Lattner | 7f6b9d2 | 2007-02-10 20:31:59 +0000 | [diff] [blame] | 833 | &Indices[0], Indices.size()); |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 834 | WorkList.push_back(CPair(CE->getOperand(0), PA)); |
| 835 | break; |
| 836 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 837 | case Instruction::Add: |
| 838 | default: |
| 839 | cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; |
| 840 | abort(); |
| 841 | break; |
| 842 | } |
Dan Gohman | 399101a | 2008-05-23 00:17:26 +0000 | [diff] [blame^] | 843 | } else if (PC->getType()->isSingleValueType()) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 844 | unsigned char *ptr = (unsigned char *)PA; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 845 | switch (PC->getType()->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 846 | case Type::IntegerTyID: { |
| 847 | unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth(); |
| 848 | uint64_t val = cast<ConstantInt>(PC)->getZExtValue(); |
| 849 | if (NumBits <= 8) |
| 850 | ptr[0] = val; |
| 851 | else if (NumBits <= 16) { |
| 852 | if (TD->isBigEndian()) |
| 853 | val = ByteSwap_16(val); |
| 854 | ptr[0] = val; |
| 855 | ptr[1] = val >> 8; |
| 856 | } else if (NumBits <= 32) { |
| 857 | if (TD->isBigEndian()) |
| 858 | val = ByteSwap_32(val); |
| 859 | ptr[0] = val; |
| 860 | ptr[1] = val >> 8; |
| 861 | ptr[2] = val >> 16; |
| 862 | ptr[3] = val >> 24; |
| 863 | } else if (NumBits <= 64) { |
| 864 | if (TD->isBigEndian()) |
| 865 | val = ByteSwap_64(val); |
| 866 | ptr[0] = val; |
| 867 | ptr[1] = val >> 8; |
| 868 | ptr[2] = val >> 16; |
| 869 | ptr[3] = val >> 24; |
| 870 | ptr[4] = val >> 32; |
| 871 | ptr[5] = val >> 40; |
| 872 | ptr[6] = val >> 48; |
| 873 | ptr[7] = val >> 56; |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 874 | } else { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 875 | assert(0 && "Not implemented: bit widths > 64"); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 876 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | case Type::FloatTyID: { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 880 | uint32_t val = cast<ConstantFP>(PC)->getValueAPF().convertToAPInt(). |
| 881 | getZExtValue(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 882 | if (TD->isBigEndian()) |
| 883 | val = ByteSwap_32(val); |
| 884 | ptr[0] = val; |
| 885 | ptr[1] = val >> 8; |
| 886 | ptr[2] = val >> 16; |
| 887 | ptr[3] = val >> 24; |
| 888 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 889 | } |
| 890 | case Type::DoubleTyID: { |
Dale Johannesen | 9d5f456 | 2007-09-12 03:30:33 +0000 | [diff] [blame] | 891 | uint64_t val = cast<ConstantFP>(PC)->getValueAPF().convertToAPInt(). |
| 892 | getZExtValue(); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 893 | if (TD->isBigEndian()) |
| 894 | val = ByteSwap_64(val); |
| 895 | ptr[0] = val; |
| 896 | ptr[1] = val >> 8; |
| 897 | ptr[2] = val >> 16; |
| 898 | ptr[3] = val >> 24; |
| 899 | ptr[4] = val >> 32; |
| 900 | ptr[5] = val >> 40; |
| 901 | ptr[6] = val >> 48; |
| 902 | ptr[7] = val >> 56; |
| 903 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 904 | } |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 905 | case Type::PointerTyID: |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 906 | if (isa<ConstantPointerNull>(PC)) |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 907 | memset(ptr, 0, TD->getPointerSize()); |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 908 | else if (const GlobalValue* GV = dyn_cast<GlobalValue>(PC)) { |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 909 | // FIXME: what about function stubs? |
| 910 | MRs.push_back(MachineRelocation::getGV(PA-(intptr_t)Addr, |
| 911 | MachineRelocation::VANILLA, |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 912 | const_cast<GlobalValue*>(GV), |
| 913 | ScatteredOffset)); |
| 914 | ScatteredOffset = 0; |
| 915 | } else |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 916 | assert(0 && "Unknown constant pointer type!"); |
| 917 | break; |
| 918 | default: |
| 919 | cerr << "ERROR: Constant unimp for type: " << *PC->getType() << "\n"; |
| 920 | abort(); |
| 921 | } |
| 922 | } else if (isa<ConstantAggregateZero>(PC)) { |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 923 | memset((void*)PA, 0, (size_t)TD->getABITypeSize(PC->getType())); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 924 | } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) { |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 925 | unsigned ElementSize = |
| 926 | TD->getABITypeSize(CPA->getType()->getElementType()); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 927 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 928 | WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize)); |
| 929 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) { |
| 930 | const StructLayout *SL = |
| 931 | TD->getStructLayout(cast<StructType>(CPS->getType())); |
| 932 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 933 | WorkList.push_back(CPair(CPS->getOperand(i), |
| 934 | PA+SL->getElementOffset(i))); |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 935 | } else { |
| 936 | cerr << "Bad Type: " << *PC->getType() << "\n"; |
| 937 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect, |
| 943 | TargetMachine &TM) : |
| 944 | GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect), |
| 945 | n_desc(0), n_value(0) { |
| 946 | |
| 947 | const TargetAsmInfo *TAI = TM.getTargetAsmInfo(); |
| 948 | |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 949 | switch (GV->getLinkage()) { |
| 950 | default: |
| 951 | assert(0 && "Unexpected linkage type!"); |
| 952 | break; |
| 953 | case GlobalValue::WeakLinkage: |
| 954 | case GlobalValue::LinkOnceLinkage: |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 955 | case GlobalValue::CommonLinkage: |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 956 | assert(!isa<Function>(gv) && "Unexpected linkage type for Function!"); |
| 957 | case GlobalValue::ExternalLinkage: |
Nate Begeman | bfaaaa6 | 2006-12-11 02:20:45 +0000 | [diff] [blame] | 958 | GVName = TAI->getGlobalPrefix() + name; |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 959 | n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 960 | break; |
| 961 | case GlobalValue::InternalLinkage: |
Nate Begeman | 6635f35 | 2007-01-26 22:39:48 +0000 | [diff] [blame] | 962 | GVName = TAI->getGlobalPrefix() + name; |
Nate Begeman | 94be248 | 2006-09-08 22:42:09 +0000 | [diff] [blame] | 963 | break; |
| 964 | } |
| 965 | } |