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