blob: 1dbbf15d9d7be96d6a57070c6a405ed3e9d7a989 [file] [log] [blame]
Nate Begemaneb883af2006-08-23 21:08:52 +00001//===-- MachOWriter.cpp - Target-independent Mach-O Writer code -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begemaneb883af2006-08-23 21:08:52 +00007//
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 Wendling8f84f1f2007-02-08 01:35:27 +000025#include "MachOWriter.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000026#include "MachOCodeEmitter.h"
Nate Begemanbfaaaa62006-12-11 02:20:45 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000029#include "llvm/Module.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000030#include "llvm/PassManager.h"
31#include "llvm/CodeGen/FileWriters.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000032#include "llvm/CodeGen/MachineCodeEmitter.h"
33#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman019f8512006-09-10 23:03:44 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Nate Begemanbfaaaa62006-12-11 02:20:45 +000035#include "llvm/Target/TargetAsmInfo.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000036#include "llvm/Target/TargetJITInfo.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000037#include "llvm/Support/Mangler.h"
Nate Begemanf8f2c5a2006-08-25 06:36:58 +000038#include "llvm/Support/MathExtras.h"
Bill Wendling203d3e42007-01-17 22:22:31 +000039#include "llvm/Support/OutputBuffer.h"
Nate Begemanbfaaaa62006-12-11 02:20:45 +000040#include "llvm/Support/Streams.h"
Owen Andersoncb371882008-08-21 00:14:44 +000041#include "llvm/Support/raw_ostream.h"
Nate Begemand2030e62006-08-26 15:46:34 +000042#include <algorithm>
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000043#include <cstring>
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000044
45namespace llvm {
Nate Begemaneb883af2006-08-23 21:08:52 +000046
Bill Wendling8f84f1f2007-02-08 01:35:27 +000047/// AddMachOWriter - Concrete function to add the Mach-O writer to the function
48/// pass manager.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000049ObjectCodeEmitter *AddMachOWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000050 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000051 TargetMachine &TM) {
52 MachOWriter *MOW = new MachOWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000053 PM.add(MOW);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000054 return MOW->getObjectCodeEmitter();
Bill Wendling8f84f1f2007-02-08 01:35:27 +000055}
56
Nate Begemaneb883af2006-08-23 21:08:52 +000057//===----------------------------------------------------------------------===//
Nate Begemaneb883af2006-08-23 21:08:52 +000058// MachOWriter Implementation
59//===----------------------------------------------------------------------===//
60
Devang Patel19974732007-05-03 01:11:54 +000061char MachOWriter::ID = 0;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000062
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000063MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm)
64 : MachineFunctionPass(&ID), O(o), TM(tm)
65 {
Nate Begemaneb883af2006-08-23 21:08:52 +000066 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
67 isLittleEndian = TM.getTargetData()->isLittleEndian();
68
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000069 TAI = TM.getTargetAsmInfo();
70
Nate Begemaneb883af2006-08-23 21:08:52 +000071 // Create the machine code emitter object for this target.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000072
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000073 MachOCE = new MachOCodeEmitter(*this, *getTextSection(true));
Nate Begemaneb883af2006-08-23 21:08:52 +000074}
75
76MachOWriter::~MachOWriter() {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000077 delete MachOCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000078}
79
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000080bool MachOWriter::doInitialization(Module &M) {
81 // Set the magic value, now that we know the pointer size and endianness
82 Header.setMagic(isLittleEndian, is64Bit);
83
84 // Set the file type
85 // FIXME: this only works for object files, we do not support the creation
86 // of dynamic libraries or executables at this time.
87 Header.filetype = MachOHeader::MH_OBJECT;
88
89 Mang = new Mangler(M);
90 return false;
91}
92
93bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
94 return false;
95}
96
97/// doFinalization - Now that the module has been completely processed, emit
98/// the Mach-O file to 'O'.
99bool MachOWriter::doFinalization(Module &M) {
100 // FIXME: we don't handle debug info yet, we should probably do that.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000101 // Okay, the.text section has been completed, build the .data, .bss, and
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000102 // "common" sections next.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000103
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000104 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
105 I != E; ++I)
106 EmitGlobal(I);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000107
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000108 // Emit the header and load commands.
109 EmitHeaderAndLoadCommands();
110
111 // Emit the various sections and their relocation info.
112 EmitSections();
113 EmitRelocations();
114
115 // Write the symbol table and the string table to the end of the file.
116 O.write((char*)&SymT[0], SymT.size());
117 O.write((char*)&StrT[0], StrT.size());
118
119 // We are done with the abstract symbols.
120 SectionList.clear();
121 SymbolTable.clear();
122 DynamicSymbolTable.clear();
123
124 // Release the name mangler object.
125 delete Mang; Mang = 0;
126 return false;
127}
128
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000129void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000130 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000131 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Duncan Sandsd1025932008-01-29 06:23:44 +0000132 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
133
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000134 // Reserve space in the .bss section for this symbol while maintaining the
135 // desired section alignment, which must be at least as much as required by
136 // this symbol.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000137 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000138
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000139 if (Align) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000140 Align = Log2_32(Align);
141 Sec->align = std::max(unsigned(Sec->align), Align);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000142
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000143 Sec->emitAlignment(Sec->align);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000144 }
Nate Begemanfec910c2007-02-28 07:40:50 +0000145 // Globals without external linkage apparently do not go in the symbol table.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000146 if (!GV->hasLocalLinkage()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000147 MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TAI);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000148 Sym.n_value = Sec->size();
Nate Begemanfec910c2007-02-28 07:40:50 +0000149 SymbolTable.push_back(Sym);
150 }
151
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000152 // Record the offset of the symbol, and then allocate space for it.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000153 // FIXME: remove when we have unified size + output buffer
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000154
155 // Now that we know what section the GlovalVariable is going to be emitted
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000156 // into, update our mappings.
157 // FIXME: We may also need to update this when outputting non-GlobalVariable
158 // GlobalValues such as functions.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000159
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000160 GVSection[GV] = Sec;
161 GVOffset[GV] = Sec->size();
162
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000163 // Allocate space in the section for the global.
164 for (unsigned i = 0; i < Size; ++i)
Bill Wendling203d3e42007-01-17 22:22:31 +0000165 SecDataOut.outbyte(0);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000166}
167
Nate Begemaneb883af2006-08-23 21:08:52 +0000168void MachOWriter::EmitGlobal(GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000169 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000170 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000171 bool NoInit = !GV->hasInitializer();
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000172
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000173 // If this global has a zero initializer, it is part of the .bss or common
174 // section.
175 if (NoInit || GV->getInitializer()->isNullValue()) {
176 // If this global is part of the common block, add it now. Variables are
177 // part of the common block if they are zero initialized and allowed to be
178 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000179 if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
180 GV->hasCommonLinkage()) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000181 MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV), MachOSym::NO_SECT, TAI);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000182 // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
183 // bytes of the symbol.
184 ExtOrCommonSym.n_value = Size;
Nate Begemanfec910c2007-02-28 07:40:50 +0000185 SymbolTable.push_back(ExtOrCommonSym);
186 // Remember that we've seen this symbol
187 GVOffset[GV] = Size;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000188 return;
189 }
190 // Otherwise, this symbol is part of the .bss section.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000191 MachOSection *BSS = getBSSSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000192 AddSymbolToSection(BSS, GV);
193 return;
194 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000195
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000196 // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
197 // 16 bytes, or a cstring. Other read only data goes into a regular const
198 // section. Read-write data goes in the data section.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000199 MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) :
Nate Begeman1257c852007-01-29 21:20:42 +0000200 getDataSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000201 AddSymbolToSection(Sec, GV);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000202 InitMem(GV->getInitializer(), GVOffset[GV], TM.getTargetData(), Sec);
Nate Begemaneb883af2006-08-23 21:08:52 +0000203}
204
205
Nate Begemaneb883af2006-08-23 21:08:52 +0000206
207void MachOWriter::EmitHeaderAndLoadCommands() {
208 // Step #0: Fill in the segment load command size, since we need it to figure
209 // out the rest of the header fields
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000210
Nate Begemaneb883af2006-08-23 21:08:52 +0000211 MachOSegment SEG("", is64Bit);
212 SEG.nsects = SectionList.size();
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000213 SEG.cmdsize = SEG.cmdSize(is64Bit) +
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000214 SEG.nsects * SectionList[0]->cmdSize(is64Bit);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000215
Nate Begemaneb883af2006-08-23 21:08:52 +0000216 // Step #1: calculate the number of load commands. We always have at least
217 // one, for the LC_SEGMENT load command, plus two for the normal
218 // and dynamic symbol tables, if there are any symbols.
219 Header.ncmds = SymbolTable.empty() ? 1 : 3;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000220
Nate Begemaneb883af2006-08-23 21:08:52 +0000221 // Step #2: calculate the size of the load commands
222 Header.sizeofcmds = SEG.cmdsize;
223 if (!SymbolTable.empty())
224 Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000225
Nate Begemaneb883af2006-08-23 21:08:52 +0000226 // Step #3: write the header to the file
227 // Local alias to shortenify coming code.
228 DataBuffer &FH = Header.HeaderData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000229 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000230
231 FHOut.outword(Header.magic);
Bill Wendling2b721822007-01-24 07:13:56 +0000232 FHOut.outword(TM.getMachOWriterInfo()->getCPUType());
233 FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType());
Bill Wendling203d3e42007-01-17 22:22:31 +0000234 FHOut.outword(Header.filetype);
235 FHOut.outword(Header.ncmds);
236 FHOut.outword(Header.sizeofcmds);
237 FHOut.outword(Header.flags);
Nate Begemaneb883af2006-08-23 21:08:52 +0000238 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000239 FHOut.outword(Header.reserved);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000240
Nate Begemaneb883af2006-08-23 21:08:52 +0000241 // Step #4: Finish filling in the segment load command and write it out
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000242 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000243 E = SectionList.end(); I != E; ++I)
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000244 SEG.filesize += (*I)->size();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000245
Nate Begemaneb883af2006-08-23 21:08:52 +0000246 SEG.vmsize = SEG.filesize;
247 SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000248
Bill Wendling203d3e42007-01-17 22:22:31 +0000249 FHOut.outword(SEG.cmd);
250 FHOut.outword(SEG.cmdsize);
251 FHOut.outstring(SEG.segname, 16);
252 FHOut.outaddr(SEG.vmaddr);
253 FHOut.outaddr(SEG.vmsize);
254 FHOut.outaddr(SEG.fileoff);
255 FHOut.outaddr(SEG.filesize);
256 FHOut.outword(SEG.maxprot);
257 FHOut.outword(SEG.initprot);
258 FHOut.outword(SEG.nsects);
259 FHOut.outword(SEG.flags);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000260
261 // Step #5: Finish filling in the fields of the MachOSections
Nate Begeman94be2482006-09-08 22:42:09 +0000262 uint64_t currentAddr = 0;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000263 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000264 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000265 MachOSection *MOS = *I;
266 MOS->addr = currentAddr;
267 MOS->offset = currentAddr + SEG.fileoff;
Nate Begeman94be2482006-09-08 22:42:09 +0000268 // FIXME: do we need to do something with alignment here?
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000269 currentAddr += MOS->size();
Nate Begeman94be2482006-09-08 22:42:09 +0000270 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000271
Nate Begemanfec910c2007-02-28 07:40:50 +0000272 // Step #6: Emit the symbol table to temporary buffers, so that we know the
273 // size of the string table when we write the next load command. This also
274 // sorts and assigns indices to each of the symbols, which is necessary for
275 // emitting relocations to externally-defined objects.
276 BufferSymbolAndStringTable();
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000277
Nate Begemanfec910c2007-02-28 07:40:50 +0000278 // Step #7: Calculate the number of relocations for each section and write out
Nate Begeman94be2482006-09-08 22:42:09 +0000279 // the section commands for each section
280 currentAddr += SEG.fileoff;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000281 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman94be2482006-09-08 22:42:09 +0000282 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000283 MachOSection *MOS = *I;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000284
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000285 // Convert the relocations to target-specific relocations, and fill in the
286 // relocation offset for this section.
287 CalculateRelocations(*MOS);
288 MOS->reloff = MOS->nreloc ? currentAddr : 0;
289 currentAddr += MOS->nreloc * 8;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000290
Nate Begeman94be2482006-09-08 22:42:09 +0000291 // write the finalized section command to the output buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000292 FHOut.outstring(MOS->sectname, 16);
293 FHOut.outstring(MOS->segname, 16);
294 FHOut.outaddr(MOS->addr);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000295 FHOut.outaddr(MOS->size());
Bill Wendling203d3e42007-01-17 22:22:31 +0000296 FHOut.outword(MOS->offset);
297 FHOut.outword(MOS->align);
298 FHOut.outword(MOS->reloff);
299 FHOut.outword(MOS->nreloc);
300 FHOut.outword(MOS->flags);
301 FHOut.outword(MOS->reserved1);
302 FHOut.outword(MOS->reserved2);
Nate Begemaneb883af2006-08-23 21:08:52 +0000303 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000304 FHOut.outword(MOS->reserved3);
Nate Begemaneb883af2006-08-23 21:08:52 +0000305 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000306
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000307 // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands
Nate Begeman94be2482006-09-08 22:42:09 +0000308 SymTab.symoff = currentAddr;
Nate Begemaneb883af2006-08-23 21:08:52 +0000309 SymTab.nsyms = SymbolTable.size();
Nate Begemand2030e62006-08-26 15:46:34 +0000310 SymTab.stroff = SymTab.symoff + SymT.size();
311 SymTab.strsize = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000312 FHOut.outword(SymTab.cmd);
313 FHOut.outword(SymTab.cmdsize);
314 FHOut.outword(SymTab.symoff);
315 FHOut.outword(SymTab.nsyms);
316 FHOut.outword(SymTab.stroff);
317 FHOut.outword(SymTab.strsize);
Nate Begemaneb883af2006-08-23 21:08:52 +0000318
319 // FIXME: set DySymTab fields appropriately
Nate Begemand2030e62006-08-26 15:46:34 +0000320 // We should probably just update these in BufferSymbolAndStringTable since
321 // thats where we're partitioning up the different kinds of symbols.
Bill Wendling203d3e42007-01-17 22:22:31 +0000322 FHOut.outword(DySymTab.cmd);
323 FHOut.outword(DySymTab.cmdsize);
324 FHOut.outword(DySymTab.ilocalsym);
325 FHOut.outword(DySymTab.nlocalsym);
326 FHOut.outword(DySymTab.iextdefsym);
327 FHOut.outword(DySymTab.nextdefsym);
328 FHOut.outword(DySymTab.iundefsym);
329 FHOut.outword(DySymTab.nundefsym);
330 FHOut.outword(DySymTab.tocoff);
331 FHOut.outword(DySymTab.ntoc);
332 FHOut.outword(DySymTab.modtaboff);
333 FHOut.outword(DySymTab.nmodtab);
334 FHOut.outword(DySymTab.extrefsymoff);
335 FHOut.outword(DySymTab.nextrefsyms);
336 FHOut.outword(DySymTab.indirectsymoff);
337 FHOut.outword(DySymTab.nindirectsyms);
338 FHOut.outword(DySymTab.extreloff);
339 FHOut.outword(DySymTab.nextrel);
340 FHOut.outword(DySymTab.locreloff);
341 FHOut.outword(DySymTab.nlocrel);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000342
Nate Begemaneb883af2006-08-23 21:08:52 +0000343 O.write((char*)&FH[0], FH.size());
344}
345
346/// EmitSections - Now that we have constructed the file header and load
347/// commands, emit the data for each section to the file.
348void MachOWriter::EmitSections() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000349 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000350 E = SectionList.end(); I != E; ++I)
351 // Emit the contents of each section
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000352 if ((*I)->size())
353 O.write((char*)&(*I)->getData()[0], (*I)->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000354}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000355
356/// EmitRelocations - emit relocation data from buffer.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000357void MachOWriter::EmitRelocations() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000358 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000359 E = SectionList.end(); I != E; ++I)
360 // Emit the relocation entry data for each section.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000361 if ((*I)->RelocBuffer.size())
362 O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
Nate Begemaneb883af2006-08-23 21:08:52 +0000363}
364
Nate Begemand2030e62006-08-26 15:46:34 +0000365/// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
366/// each a string table index so that they appear in the correct order in the
367/// output file.
368void MachOWriter::BufferSymbolAndStringTable() {
369 // The order of the symbol table is:
370 // 1. local symbols
371 // 2. defined external symbols (sorted by name)
372 // 3. undefined external symbols (sorted by name)
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000373
Nate Begemanfec910c2007-02-28 07:40:50 +0000374 // Before sorting the symbols, check the PendingGlobals for any undefined
375 // globals that need to be put in the symbol table.
376 for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(),
377 E = PendingGlobals.end(); I != E; ++I) {
378 if (GVOffset[*I] == 0 && GVSection[*I] == 0) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000379 MachOSym UndfSym(*I, Mang->getValueName(*I), MachOSym::NO_SECT, TAI);
Nate Begemanfec910c2007-02-28 07:40:50 +0000380 SymbolTable.push_back(UndfSym);
381 GVOffset[*I] = -1;
382 }
383 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000384
Nate Begemand2030e62006-08-26 15:46:34 +0000385 // Sort the symbols by name, so that when we partition the symbols by scope
386 // of definition, we won't have to sort by name within each partition.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000387 std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp());
388
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000389 // Parition the symbol table entries so that all local symbols come before
Nate Begemand2030e62006-08-26 15:46:34 +0000390 // all symbols with external linkage. { 1 | 2 3 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000391 std::partition(SymbolTable.begin(), SymbolTable.end(), MachOSym::PartitionByLocal);
392
Nate Begemand2030e62006-08-26 15:46:34 +0000393 // Advance iterator to beginning of external symbols and partition so that
394 // all external symbols defined in this module come before all external
395 // symbols defined elsewhere. { 1 | 2 | 3 }
396 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
397 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000398 if (!MachOSym::PartitionByLocal(*I)) {
399 std::partition(I, E, MachOSym::PartitionByDefined);
Nate Begemand2030e62006-08-26 15:46:34 +0000400 break;
401 }
402 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000403
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000404 // Calculate the starting index for each of the local, extern defined, and
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000405 // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB
406 // load command.
407 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
408 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000409 if (MachOSym::PartitionByLocal(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000410 ++DySymTab.nlocalsym;
411 ++DySymTab.iextdefsym;
Nate Begeman6635f352007-01-26 22:39:48 +0000412 ++DySymTab.iundefsym;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000413 } else if (MachOSym::PartitionByDefined(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000414 ++DySymTab.nextdefsym;
415 ++DySymTab.iundefsym;
416 } else {
417 ++DySymTab.nundefsym;
418 }
419 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000420
Nate Begemaneb883af2006-08-23 21:08:52 +0000421 // Write out a leading zero byte when emitting string table, for n_strx == 0
422 // which means an empty string.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000423 OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000424 StrTOut.outbyte(0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000425
Nate Begemand2030e62006-08-26 15:46:34 +0000426 // The order of the string table is:
427 // 1. strings for external symbols
428 // 2. strings for local symbols
429 // Since this is the opposite order from the symbol table, which we have just
430 // sorted, we can walk the symbol table backwards to output the string table.
431 for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(),
432 E = SymbolTable.rend(); I != E; ++I) {
433 if (I->GVName == "") {
434 I->n_strx = 0;
435 } else {
436 I->n_strx = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000437 StrTOut.outstring(I->GVName, I->GVName.length()+1);
Nate Begemand2030e62006-08-26 15:46:34 +0000438 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000439 }
Nate Begemand2030e62006-08-26 15:46:34 +0000440
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000441 OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000442
Nate Begemanfec910c2007-02-28 07:40:50 +0000443 unsigned index = 0;
Nate Begemand2030e62006-08-26 15:46:34 +0000444 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
Nate Begemanfec910c2007-02-28 07:40:50 +0000445 E = SymbolTable.end(); I != E; ++I, ++index) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000446 // Add the section base address to the section offset in the n_value field
447 // to calculate the full address.
448 // FIXME: handle symbols where the n_value field is not the address
449 GlobalValue *GV = const_cast<GlobalValue*>(I->GV);
450 if (GV && GVSection[GV])
451 I->n_value += GVSection[GV]->addr;
Nate Begemanfec910c2007-02-28 07:40:50 +0000452 if (GV && (GVOffset[GV] == -1))
453 GVOffset[GV] = index;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000454
Nate Begemand2030e62006-08-26 15:46:34 +0000455 // Emit nlist to buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000456 SymTOut.outword(I->n_strx);
457 SymTOut.outbyte(I->n_type);
458 SymTOut.outbyte(I->n_sect);
459 SymTOut.outhalf(I->n_desc);
460 SymTOut.outaddr(I->n_value);
Nate Begemand2030e62006-08-26 15:46:34 +0000461 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000462}
Nate Begeman94be2482006-09-08 22:42:09 +0000463
Nate Begeman019f8512006-09-10 23:03:44 +0000464/// CalculateRelocations - For each MachineRelocation in the current section,
465/// calculate the index of the section containing the object to be relocated,
466/// and the offset into that section. From this information, create the
467/// appropriate target-specific MachORelocation type and add buffer it to be
468/// written out after we are finished writing out sections.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000469void MachOWriter::CalculateRelocations(MachOSection &MOS) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000470 std::vector<MachineRelocation> Relocations = MOS.getRelocations();
471 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
472 MachineRelocation &MR = Relocations[i];
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000473 unsigned TargetSection = MR.getConstantVal();
Nate Begemanaf806382007-03-03 06:18:18 +0000474 unsigned TargetAddr = 0;
475 unsigned TargetIndex = 0;
Nate Begeman6635f352007-01-26 22:39:48 +0000476
477 // This is a scattered relocation entry if it points to a global value with
478 // a non-zero offset.
479 bool Scattered = false;
Nate Begemanfec910c2007-02-28 07:40:50 +0000480 bool Extern = false;
Nate Begemanaf806382007-03-03 06:18:18 +0000481
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000482 // Since we may not have seen the GlobalValue we were interested in yet at
483 // the time we emitted the relocation for it, fix it up now so that it
484 // points to the offset into the correct section.
485 if (MR.isGlobalValue()) {
486 GlobalValue *GV = MR.getGlobalValue();
487 MachOSection *MOSPtr = GVSection[GV];
Nate Begeman6635f352007-01-26 22:39:48 +0000488 intptr_t Offset = GVOffset[GV];
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000489
Nate Begemanfec910c2007-02-28 07:40:50 +0000490 // If we have never seen the global before, it must be to a symbol
491 // defined in another module (N_UNDF).
Nate Begeman1257c852007-01-29 21:20:42 +0000492 if (!MOSPtr) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000493 // FIXME: need to append stub suffix
494 Extern = true;
495 TargetAddr = 0;
496 TargetIndex = GVOffset[GV];
497 } else {
498 Scattered = TargetSection != 0;
499 TargetSection = MOSPtr->Index;
Nate Begemanaf806382007-03-03 06:18:18 +0000500 }
501 MR.setResultPointer((void*)Offset);
502 }
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000503
Nate Begemanaf806382007-03-03 06:18:18 +0000504 // If the symbol is locally defined, pass in the address of the section and
505 // the section index to the code which will generate the target relocation.
506 if (!Extern) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000507 MachOSection &To = *SectionList[TargetSection - 1];
508 TargetAddr = To.addr;
509 TargetIndex = To.Index;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000510 }
Bill Wendling886b4122007-02-03 02:39:40 +0000511
512 OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000513 OutputBuffer SecOut(MOS.getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000514
Nate Begemanfec910c2007-02-28 07:40:50 +0000515 MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex,
516 RelocOut, SecOut, Scattered, Extern);
Nate Begeman019f8512006-09-10 23:03:44 +0000517 }
Nate Begeman019f8512006-09-10 23:03:44 +0000518}
519
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000520// InitMem - Write the value of a Constant to the specified memory location,
521// converting it into bytes and relocations.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000522void MachOWriter::InitMem(const Constant *C, uintptr_t Offset,
523 const TargetData *TD, MachOSection* mos) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000524 typedef std::pair<const Constant*, intptr_t> CPair;
525 std::vector<CPair> WorkList;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000526 uint8_t *Addr = &mos->getData()[0];
527
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000528 WorkList.push_back(CPair(C,(intptr_t)Addr + Offset));
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000529
Nate Begeman6635f352007-01-26 22:39:48 +0000530 intptr_t ScatteredOffset = 0;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000531
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000532 while (!WorkList.empty()) {
533 const Constant *PC = WorkList.back().first;
534 intptr_t PA = WorkList.back().second;
535 WorkList.pop_back();
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000536
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000537 if (isa<UndefValue>(PC)) {
538 continue;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000539 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000540 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000541 TD->getTypeAllocSize(CP->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000542 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
543 WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize));
544 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) {
545 //
546 // FIXME: Handle ConstantExpression. See EE::getConstantValue()
547 //
548 switch (CE->getOpcode()) {
Nate Begeman6635f352007-01-26 22:39:48 +0000549 case Instruction::GetElementPtr: {
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000550 SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
Nate Begeman6635f352007-01-26 22:39:48 +0000551 ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(),
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000552 &Indices[0], Indices.size());
Nate Begeman6635f352007-01-26 22:39:48 +0000553 WorkList.push_back(CPair(CE->getOperand(0), PA));
554 break;
555 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000556 case Instruction::Add:
557 default:
558 cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
559 abort();
560 break;
561 }
Dan Gohman399101a2008-05-23 00:17:26 +0000562 } else if (PC->getType()->isSingleValueType()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000563 unsigned char *ptr = (unsigned char *)PA;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000564 switch (PC->getType()->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000565 case Type::IntegerTyID: {
566 unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth();
567 uint64_t val = cast<ConstantInt>(PC)->getZExtValue();
568 if (NumBits <= 8)
569 ptr[0] = val;
570 else if (NumBits <= 16) {
571 if (TD->isBigEndian())
572 val = ByteSwap_16(val);
573 ptr[0] = val;
574 ptr[1] = val >> 8;
575 } else if (NumBits <= 32) {
576 if (TD->isBigEndian())
577 val = ByteSwap_32(val);
578 ptr[0] = val;
579 ptr[1] = val >> 8;
580 ptr[2] = val >> 16;
581 ptr[3] = val >> 24;
582 } else if (NumBits <= 64) {
583 if (TD->isBigEndian())
584 val = ByteSwap_64(val);
585 ptr[0] = val;
586 ptr[1] = val >> 8;
587 ptr[2] = val >> 16;
588 ptr[3] = val >> 24;
589 ptr[4] = val >> 32;
590 ptr[5] = val >> 40;
591 ptr[6] = val >> 48;
592 ptr[7] = val >> 56;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000593 } else {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000594 assert(0 && "Not implemented: bit widths > 64");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000595 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000596 break;
597 }
598 case Type::FloatTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000599 uint32_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000600 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000601 if (TD->isBigEndian())
602 val = ByteSwap_32(val);
603 ptr[0] = val;
604 ptr[1] = val >> 8;
605 ptr[2] = val >> 16;
606 ptr[3] = val >> 24;
607 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000608 }
609 case Type::DoubleTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000610 uint64_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000611 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000612 if (TD->isBigEndian())
613 val = ByteSwap_64(val);
614 ptr[0] = val;
615 ptr[1] = val >> 8;
616 ptr[2] = val >> 16;
617 ptr[3] = val >> 24;
618 ptr[4] = val >> 32;
619 ptr[5] = val >> 40;
620 ptr[6] = val >> 48;
621 ptr[7] = val >> 56;
622 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000623 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000624 case Type::PointerTyID:
Nate Begeman6635f352007-01-26 22:39:48 +0000625 if (isa<ConstantPointerNull>(PC))
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000626 memset(ptr, 0, TD->getPointerSize());
Nate Begeman6635f352007-01-26 22:39:48 +0000627 else if (const GlobalValue* GV = dyn_cast<GlobalValue>(PC)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000628 // FIXME: what about function stubs?
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000629 mos->addRelocation(MachineRelocation::getGV(PA-(intptr_t)Addr,
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000630 MachineRelocation::VANILLA,
Nate Begeman6635f352007-01-26 22:39:48 +0000631 const_cast<GlobalValue*>(GV),
632 ScatteredOffset));
633 ScatteredOffset = 0;
634 } else
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000635 assert(0 && "Unknown constant pointer type!");
636 break;
637 default:
638 cerr << "ERROR: Constant unimp for type: " << *PC->getType() << "\n";
639 abort();
640 }
641 } else if (isa<ConstantAggregateZero>(PC)) {
Duncan Sands777d2302009-05-09 07:06:46 +0000642 memset((void*)PA, 0, (size_t)TD->getTypeAllocSize(PC->getType()));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000643 } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000644 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000645 TD->getTypeAllocSize(CPA->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000646 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
647 WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize));
648 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) {
649 const StructLayout *SL =
650 TD->getStructLayout(cast<StructType>(CPS->getType()));
651 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattnerb1919e22007-02-10 19:55:17 +0000652 WorkList.push_back(CPair(CPS->getOperand(i),
653 PA+SL->getElementOffset(i)));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000654 } else {
655 cerr << "Bad Type: " << *PC->getType() << "\n";
656 assert(0 && "Unknown constant type to initialize memory with!");
657 }
658 }
659}
660
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000661//===----------------------------------------------------------------------===//
662// MachOSym Implementation
663//===----------------------------------------------------------------------===//
664
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000665MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000666 const TargetAsmInfo *TAI) :
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000667 GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect),
668 n_desc(0), n_value(0) {
669
Nate Begeman94be2482006-09-08 22:42:09 +0000670 switch (GV->getLinkage()) {
671 default:
672 assert(0 && "Unexpected linkage type!");
673 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000674 case GlobalValue::WeakAnyLinkage:
675 case GlobalValue::WeakODRLinkage:
676 case GlobalValue::LinkOnceAnyLinkage:
677 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000678 case GlobalValue::CommonLinkage:
Nate Begeman94be2482006-09-08 22:42:09 +0000679 assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
680 case GlobalValue::ExternalLinkage:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000681 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman6635f352007-01-26 22:39:48 +0000682 n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT;
Nate Begeman94be2482006-09-08 22:42:09 +0000683 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000684 case GlobalValue::PrivateLinkage:
685 GVName = TAI->getPrivateGlobalPrefix() + name;
686 break;
Nate Begeman94be2482006-09-08 22:42:09 +0000687 case GlobalValue::InternalLinkage:
Nate Begeman6635f352007-01-26 22:39:48 +0000688 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman94be2482006-09-08 22:42:09 +0000689 break;
690 }
691}
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000692
693} // end namespace llvm
694