blob: 163df6994aa625d02bd81671df6771345bbb4ad7 [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 Lopesa321dcd2009-06-03 03:43:31 +000049MachineCodeEmitter *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);
Bill Wendling8f84f1f2007-02-08 01:35:27 +000054 return &MOW->getMachineCodeEmitter();
55}
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
63MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm)
Dan Gohmanae73dc12008-09-04 17:05:41 +000064 : MachineFunctionPass(&ID), O(o), TM(tm) {
Nate Begemaneb883af2006-08-23 21:08:52 +000065 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
66 isLittleEndian = TM.getTargetData()->isLittleEndian();
67
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000068 TAI = TM.getTargetAsmInfo();
69
Nate Begemaneb883af2006-08-23 21:08:52 +000070 // Create the machine code emitter object for this target.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000071
Bill Wendlinge9116152007-01-17 09:06:13 +000072 MCE = new MachOCodeEmitter(*this);
Nate Begemaneb883af2006-08-23 21:08:52 +000073}
74
75MachOWriter::~MachOWriter() {
76 delete MCE;
77}
78
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000079bool MachOWriter::doInitialization(Module &M) {
80 // Set the magic value, now that we know the pointer size and endianness
81 Header.setMagic(isLittleEndian, is64Bit);
82
83 // Set the file type
84 // FIXME: this only works for object files, we do not support the creation
85 // of dynamic libraries or executables at this time.
86 Header.filetype = MachOHeader::MH_OBJECT;
87
88 Mang = new Mangler(M);
89 return false;
90}
91
92bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
93 return false;
94}
95
96/// doFinalization - Now that the module has been completely processed, emit
97/// the Mach-O file to 'O'.
98bool MachOWriter::doFinalization(Module &M) {
99 // FIXME: we don't handle debug info yet, we should probably do that.
100
101 // Okay, the.text section has been completed, build the .data, .bss, and
102 // "common" sections next.
103 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
104 I != E; ++I)
105 EmitGlobal(I);
106
107 // Emit the header and load commands.
108 EmitHeaderAndLoadCommands();
109
110 // Emit the various sections and their relocation info.
111 EmitSections();
112 EmitRelocations();
113
114 // Write the symbol table and the string table to the end of the file.
115 O.write((char*)&SymT[0], SymT.size());
116 O.write((char*)&StrT[0], StrT.size());
117
118 // We are done with the abstract symbols.
119 SectionList.clear();
120 SymbolTable.clear();
121 DynamicSymbolTable.clear();
122
123 // Release the name mangler object.
124 delete Mang; Mang = 0;
125 return false;
126}
127
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000128void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000129 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000130 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Duncan Sandsd1025932008-01-29 06:23:44 +0000131 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
132
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000133 // Reserve space in the .bss section for this symbol while maintaining the
134 // desired section alignment, which must be at least as much as required by
135 // this symbol.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000136 OutputBuffer SecDataOut(Sec->SectionData, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000137
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000138 if (Align) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000139 uint64_t OrigSize = Sec->size;
140 Align = Log2_32(Align);
141 Sec->align = std::max(unsigned(Sec->align), Align);
142 Sec->size = (Sec->size + Align - 1) & ~(Align-1);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000143
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000144 // Add alignment padding to buffer as well.
145 // FIXME: remove when we have unified size + output buffer
146 unsigned AlignedSize = Sec->size - OrigSize;
147 for (unsigned i = 0; i < AlignedSize; ++i)
Bill Wendling203d3e42007-01-17 22:22:31 +0000148 SecDataOut.outbyte(0);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000149 }
Nate Begemanfec910c2007-02-28 07:40:50 +0000150 // Globals without external linkage apparently do not go in the symbol table.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000151 if (!GV->hasLocalLinkage()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000152 MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TAI);
Nate Begemanfec910c2007-02-28 07:40:50 +0000153 Sym.n_value = Sec->size;
154 SymbolTable.push_back(Sym);
155 }
156
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000157 // Record the offset of the symbol, and then allocate space for it.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000158 // FIXME: remove when we have unified size + output buffer
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000159 Sec->size += Size;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000160
161 // Now that we know what section the GlovalVariable is going to be emitted
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000162 // into, update our mappings.
163 // FIXME: We may also need to update this when outputting non-GlobalVariable
164 // GlobalValues such as functions.
165 GVSection[GV] = Sec;
166 GVOffset[GV] = Sec->SectionData.size();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000167
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000168 // Allocate space in the section for the global.
169 for (unsigned i = 0; i < Size; ++i)
Bill Wendling203d3e42007-01-17 22:22:31 +0000170 SecDataOut.outbyte(0);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000171}
172
Nate Begemaneb883af2006-08-23 21:08:52 +0000173void MachOWriter::EmitGlobal(GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000174 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000175 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000176 bool NoInit = !GV->hasInitializer();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000177
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000178 // If this global has a zero initializer, it is part of the .bss or common
179 // section.
180 if (NoInit || GV->getInitializer()->isNullValue()) {
181 // If this global is part of the common block, add it now. Variables are
182 // part of the common block if they are zero initialized and allowed to be
183 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000184 if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
185 GV->hasCommonLinkage()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000186 MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV),
187 MachOSym::NO_SECT, TAI);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000188 // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
189 // bytes of the symbol.
190 ExtOrCommonSym.n_value = Size;
Nate Begemanfec910c2007-02-28 07:40:50 +0000191 SymbolTable.push_back(ExtOrCommonSym);
192 // Remember that we've seen this symbol
193 GVOffset[GV] = Size;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000194 return;
195 }
196 // Otherwise, this symbol is part of the .bss section.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000197 MachOSection *BSS = getBSSSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000198 AddSymbolToSection(BSS, GV);
199 return;
200 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000201
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000202 // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
203 // 16 bytes, or a cstring. Other read only data goes into a regular const
204 // section. Read-write data goes in the data section.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000205 MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) :
Nate Begeman1257c852007-01-29 21:20:42 +0000206 getDataSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000207 AddSymbolToSection(Sec, GV);
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000208 InitMem(GV->getInitializer(), &Sec->SectionData[0], GVOffset[GV],
209 TM.getTargetData(), Sec->Relocations);
Nate Begemaneb883af2006-08-23 21:08:52 +0000210}
211
212
Nate Begemaneb883af2006-08-23 21:08:52 +0000213
214void MachOWriter::EmitHeaderAndLoadCommands() {
215 // Step #0: Fill in the segment load command size, since we need it to figure
216 // out the rest of the header fields
217 MachOSegment SEG("", is64Bit);
218 SEG.nsects = SectionList.size();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000219 SEG.cmdsize = SEG.cmdSize(is64Bit) +
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000220 SEG.nsects * SectionList[0]->cmdSize(is64Bit);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000221
Nate Begemaneb883af2006-08-23 21:08:52 +0000222 // Step #1: calculate the number of load commands. We always have at least
223 // one, for the LC_SEGMENT load command, plus two for the normal
224 // and dynamic symbol tables, if there are any symbols.
225 Header.ncmds = SymbolTable.empty() ? 1 : 3;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000226
Nate Begemaneb883af2006-08-23 21:08:52 +0000227 // Step #2: calculate the size of the load commands
228 Header.sizeofcmds = SEG.cmdsize;
229 if (!SymbolTable.empty())
230 Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000231
Nate Begemaneb883af2006-08-23 21:08:52 +0000232 // Step #3: write the header to the file
233 // Local alias to shortenify coming code.
234 DataBuffer &FH = Header.HeaderData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000235 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000236
237 FHOut.outword(Header.magic);
Bill Wendling2b721822007-01-24 07:13:56 +0000238 FHOut.outword(TM.getMachOWriterInfo()->getCPUType());
239 FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType());
Bill Wendling203d3e42007-01-17 22:22:31 +0000240 FHOut.outword(Header.filetype);
241 FHOut.outword(Header.ncmds);
242 FHOut.outword(Header.sizeofcmds);
243 FHOut.outword(Header.flags);
Nate Begemaneb883af2006-08-23 21:08:52 +0000244 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000245 FHOut.outword(Header.reserved);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000246
Nate Begemaneb883af2006-08-23 21:08:52 +0000247 // Step #4: Finish filling in the segment load command and write it out
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000248 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000249 E = SectionList.end(); I != E; ++I)
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000250 SEG.filesize += (*I)->size;
251
Nate Begemaneb883af2006-08-23 21:08:52 +0000252 SEG.vmsize = SEG.filesize;
253 SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000254
Bill Wendling203d3e42007-01-17 22:22:31 +0000255 FHOut.outword(SEG.cmd);
256 FHOut.outword(SEG.cmdsize);
257 FHOut.outstring(SEG.segname, 16);
258 FHOut.outaddr(SEG.vmaddr);
259 FHOut.outaddr(SEG.vmsize);
260 FHOut.outaddr(SEG.fileoff);
261 FHOut.outaddr(SEG.filesize);
262 FHOut.outword(SEG.maxprot);
263 FHOut.outword(SEG.initprot);
264 FHOut.outword(SEG.nsects);
265 FHOut.outword(SEG.flags);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000266
267 // Step #5: Finish filling in the fields of the MachOSections
Nate Begeman94be2482006-09-08 22:42:09 +0000268 uint64_t currentAddr = 0;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000269 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000270 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000271 MachOSection *MOS = *I;
272 MOS->addr = currentAddr;
273 MOS->offset = currentAddr + SEG.fileoff;
Nate Begeman019f8512006-09-10 23:03:44 +0000274
Nate Begeman94be2482006-09-08 22:42:09 +0000275 // FIXME: do we need to do something with alignment here?
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000276 currentAddr += MOS->size;
Nate Begeman94be2482006-09-08 22:42:09 +0000277 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000278
Nate Begemanfec910c2007-02-28 07:40:50 +0000279 // Step #6: Emit the symbol table to temporary buffers, so that we know the
280 // size of the string table when we write the next load command. This also
281 // sorts and assigns indices to each of the symbols, which is necessary for
282 // emitting relocations to externally-defined objects.
283 BufferSymbolAndStringTable();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000284
Nate Begemanfec910c2007-02-28 07:40:50 +0000285 // Step #7: Calculate the number of relocations for each section and write out
Nate Begeman94be2482006-09-08 22:42:09 +0000286 // the section commands for each section
287 currentAddr += SEG.fileoff;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000288 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman94be2482006-09-08 22:42:09 +0000289 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000290 MachOSection *MOS = *I;
291 // Convert the relocations to target-specific relocations, and fill in the
292 // relocation offset for this section.
293 CalculateRelocations(*MOS);
294 MOS->reloff = MOS->nreloc ? currentAddr : 0;
295 currentAddr += MOS->nreloc * 8;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000296
Nate Begeman94be2482006-09-08 22:42:09 +0000297 // write the finalized section command to the output buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000298 FHOut.outstring(MOS->sectname, 16);
299 FHOut.outstring(MOS->segname, 16);
300 FHOut.outaddr(MOS->addr);
301 FHOut.outaddr(MOS->size);
302 FHOut.outword(MOS->offset);
303 FHOut.outword(MOS->align);
304 FHOut.outword(MOS->reloff);
305 FHOut.outword(MOS->nreloc);
306 FHOut.outword(MOS->flags);
307 FHOut.outword(MOS->reserved1);
308 FHOut.outword(MOS->reserved2);
Nate Begemaneb883af2006-08-23 21:08:52 +0000309 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000310 FHOut.outword(MOS->reserved3);
Nate Begemaneb883af2006-08-23 21:08:52 +0000311 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000312
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000313 // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands
Nate Begeman94be2482006-09-08 22:42:09 +0000314 SymTab.symoff = currentAddr;
Nate Begemaneb883af2006-08-23 21:08:52 +0000315 SymTab.nsyms = SymbolTable.size();
Nate Begemand2030e62006-08-26 15:46:34 +0000316 SymTab.stroff = SymTab.symoff + SymT.size();
317 SymTab.strsize = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000318 FHOut.outword(SymTab.cmd);
319 FHOut.outword(SymTab.cmdsize);
320 FHOut.outword(SymTab.symoff);
321 FHOut.outword(SymTab.nsyms);
322 FHOut.outword(SymTab.stroff);
323 FHOut.outword(SymTab.strsize);
Nate Begemaneb883af2006-08-23 21:08:52 +0000324
325 // FIXME: set DySymTab fields appropriately
Nate Begemand2030e62006-08-26 15:46:34 +0000326 // We should probably just update these in BufferSymbolAndStringTable since
327 // thats where we're partitioning up the different kinds of symbols.
Bill Wendling203d3e42007-01-17 22:22:31 +0000328 FHOut.outword(DySymTab.cmd);
329 FHOut.outword(DySymTab.cmdsize);
330 FHOut.outword(DySymTab.ilocalsym);
331 FHOut.outword(DySymTab.nlocalsym);
332 FHOut.outword(DySymTab.iextdefsym);
333 FHOut.outword(DySymTab.nextdefsym);
334 FHOut.outword(DySymTab.iundefsym);
335 FHOut.outword(DySymTab.nundefsym);
336 FHOut.outword(DySymTab.tocoff);
337 FHOut.outword(DySymTab.ntoc);
338 FHOut.outword(DySymTab.modtaboff);
339 FHOut.outword(DySymTab.nmodtab);
340 FHOut.outword(DySymTab.extrefsymoff);
341 FHOut.outword(DySymTab.nextrefsyms);
342 FHOut.outword(DySymTab.indirectsymoff);
343 FHOut.outword(DySymTab.nindirectsyms);
344 FHOut.outword(DySymTab.extreloff);
345 FHOut.outword(DySymTab.nextrel);
346 FHOut.outword(DySymTab.locreloff);
347 FHOut.outword(DySymTab.nlocrel);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000348
Nate Begemaneb883af2006-08-23 21:08:52 +0000349 O.write((char*)&FH[0], FH.size());
350}
351
352/// EmitSections - Now that we have constructed the file header and load
353/// commands, emit the data for each section to the file.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000354
Nate Begemaneb883af2006-08-23 21:08:52 +0000355void MachOWriter::EmitSections() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000356 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000357 E = SectionList.end(); I != E; ++I)
358 // Emit the contents of each section
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000359 O.write((char*)&(*I)->SectionData[0], (*I)->size);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000360}
361void MachOWriter::EmitRelocations() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000362 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000363 E = SectionList.end(); I != E; ++I)
364 // Emit the relocation entry data for each section.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000365 O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
Nate Begemaneb883af2006-08-23 21:08:52 +0000366}
367
Nate Begemand2030e62006-08-26 15:46:34 +0000368/// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
369/// each a string table index so that they appear in the correct order in the
370/// output file.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000371
Nate Begemand2030e62006-08-26 15:46:34 +0000372void MachOWriter::BufferSymbolAndStringTable() {
373 // The order of the symbol table is:
374 // 1. local symbols
375 // 2. defined external symbols (sorted by name)
376 // 3. undefined external symbols (sorted by name)
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000377
Nate Begemanfec910c2007-02-28 07:40:50 +0000378 // Before sorting the symbols, check the PendingGlobals for any undefined
379 // globals that need to be put in the symbol table.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000380
Nate Begemanfec910c2007-02-28 07:40:50 +0000381 for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(),
382 E = PendingGlobals.end(); I != E; ++I) {
383 if (GVOffset[*I] == 0 && GVSection[*I] == 0) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000384 MachOSym UndfSym(*I, Mang->getValueName(*I), MachOSym::NO_SECT, TAI);
Nate Begemanfec910c2007-02-28 07:40:50 +0000385 SymbolTable.push_back(UndfSym);
386 GVOffset[*I] = -1;
387 }
388 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000389
Nate Begemand2030e62006-08-26 15:46:34 +0000390 // Sort the symbols by name, so that when we partition the symbols by scope
391 // of definition, we won't have to sort by name within each partition.
Nate Begemand2030e62006-08-26 15:46:34 +0000392
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000393 std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp());
394
395 // Parition the symbol table entries so that all local symbols come before
Nate Begemand2030e62006-08-26 15:46:34 +0000396 // all symbols with external linkage. { 1 | 2 3 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000397
398 std::partition(SymbolTable.begin(), SymbolTable.end(),
399 MachOSym::PartitionByLocal);
400
Nate Begemand2030e62006-08-26 15:46:34 +0000401 // Advance iterator to beginning of external symbols and partition so that
402 // all external symbols defined in this module come before all external
403 // symbols defined elsewhere. { 1 | 2 | 3 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000404
Nate Begemand2030e62006-08-26 15:46:34 +0000405 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
406 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000407 if (!MachOSym::PartitionByLocal(*I)) {
408 std::partition(I, E, MachOSym::PartitionByDefined);
Nate Begemand2030e62006-08-26 15:46:34 +0000409 break;
410 }
411 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000412
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000413 // Calculate the starting index for each of the local, extern defined, and
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000414 // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB
415 // load command.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000416
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000417 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
418 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000419 if (MachOSym::PartitionByLocal(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000420 ++DySymTab.nlocalsym;
421 ++DySymTab.iextdefsym;
Nate Begeman6635f352007-01-26 22:39:48 +0000422 ++DySymTab.iundefsym;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000423 } else if (MachOSym::PartitionByDefined(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000424 ++DySymTab.nextdefsym;
425 ++DySymTab.iundefsym;
426 } else {
427 ++DySymTab.nundefsym;
428 }
429 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000430
Nate Begemaneb883af2006-08-23 21:08:52 +0000431 // Write out a leading zero byte when emitting string table, for n_strx == 0
432 // which means an empty string.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000433
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000434 OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000435 StrTOut.outbyte(0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000436
Nate Begemand2030e62006-08-26 15:46:34 +0000437 // The order of the string table is:
438 // 1. strings for external symbols
439 // 2. strings for local symbols
440 // Since this is the opposite order from the symbol table, which we have just
441 // sorted, we can walk the symbol table backwards to output the string table.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000442
Nate Begemand2030e62006-08-26 15:46:34 +0000443 for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(),
444 E = SymbolTable.rend(); I != E; ++I) {
445 if (I->GVName == "") {
446 I->n_strx = 0;
447 } else {
448 I->n_strx = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000449 StrTOut.outstring(I->GVName, I->GVName.length()+1);
Nate Begemand2030e62006-08-26 15:46:34 +0000450 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000451 }
Nate Begemand2030e62006-08-26 15:46:34 +0000452
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000453 OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000454
Nate Begemanfec910c2007-02-28 07:40:50 +0000455 unsigned index = 0;
Nate Begemand2030e62006-08-26 15:46:34 +0000456 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
Nate Begemanfec910c2007-02-28 07:40:50 +0000457 E = SymbolTable.end(); I != E; ++I, ++index) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000458 // Add the section base address to the section offset in the n_value field
459 // to calculate the full address.
460 // FIXME: handle symbols where the n_value field is not the address
461 GlobalValue *GV = const_cast<GlobalValue*>(I->GV);
462 if (GV && GVSection[GV])
463 I->n_value += GVSection[GV]->addr;
Nate Begemanfec910c2007-02-28 07:40:50 +0000464 if (GV && (GVOffset[GV] == -1))
465 GVOffset[GV] = index;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000466
Nate Begemand2030e62006-08-26 15:46:34 +0000467 // Emit nlist to buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000468 SymTOut.outword(I->n_strx);
469 SymTOut.outbyte(I->n_type);
470 SymTOut.outbyte(I->n_sect);
471 SymTOut.outhalf(I->n_desc);
472 SymTOut.outaddr(I->n_value);
Nate Begemand2030e62006-08-26 15:46:34 +0000473 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000474}
Nate Begeman94be2482006-09-08 22:42:09 +0000475
Nate Begeman019f8512006-09-10 23:03:44 +0000476/// CalculateRelocations - For each MachineRelocation in the current section,
477/// calculate the index of the section containing the object to be relocated,
478/// and the offset into that section. From this information, create the
479/// appropriate target-specific MachORelocation type and add buffer it to be
480/// written out after we are finished writing out sections.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000481
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000482void MachOWriter::CalculateRelocations(MachOSection &MOS) {
Nate Begeman019f8512006-09-10 23:03:44 +0000483 for (unsigned i = 0, e = MOS.Relocations.size(); i != e; ++i) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000484 MachineRelocation &MR = MOS.Relocations[i];
485 unsigned TargetSection = MR.getConstantVal();
Nate Begemanaf806382007-03-03 06:18:18 +0000486 unsigned TargetAddr = 0;
487 unsigned TargetIndex = 0;
Nate Begeman6635f352007-01-26 22:39:48 +0000488
489 // This is a scattered relocation entry if it points to a global value with
490 // a non-zero offset.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000491
Nate Begeman6635f352007-01-26 22:39:48 +0000492 bool Scattered = false;
Nate Begemanfec910c2007-02-28 07:40:50 +0000493 bool Extern = false;
Nate Begemanaf806382007-03-03 06:18:18 +0000494
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000495 // Since we may not have seen the GlobalValue we were interested in yet at
496 // the time we emitted the relocation for it, fix it up now so that it
497 // points to the offset into the correct section.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000498
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000499 if (MR.isGlobalValue()) {
500 GlobalValue *GV = MR.getGlobalValue();
501 MachOSection *MOSPtr = GVSection[GV];
Nate Begeman6635f352007-01-26 22:39:48 +0000502 intptr_t Offset = GVOffset[GV];
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000503
Nate Begemanfec910c2007-02-28 07:40:50 +0000504 // If we have never seen the global before, it must be to a symbol
505 // defined in another module (N_UNDF).
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000506
Nate Begeman1257c852007-01-29 21:20:42 +0000507 if (!MOSPtr) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000508 // FIXME: need to append stub suffix
509 Extern = true;
510 TargetAddr = 0;
511 TargetIndex = GVOffset[GV];
512 } else {
513 Scattered = TargetSection != 0;
514 TargetSection = MOSPtr->Index;
Nate Begemanaf806382007-03-03 06:18:18 +0000515 }
516 MR.setResultPointer((void*)Offset);
517 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000518
Nate Begemanaf806382007-03-03 06:18:18 +0000519 // If the symbol is locally defined, pass in the address of the section and
520 // the section index to the code which will generate the target relocation.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000521
Nate Begemanaf806382007-03-03 06:18:18 +0000522 if (!Extern) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000523 MachOSection &To = *SectionList[TargetSection - 1];
524 TargetAddr = To.addr;
525 TargetIndex = To.Index;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000526 }
Bill Wendling886b4122007-02-03 02:39:40 +0000527
528 OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian);
529 OutputBuffer SecOut(MOS.SectionData, is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000530
Nate Begemanfec910c2007-02-28 07:40:50 +0000531 MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex,
532 RelocOut, SecOut, Scattered, Extern);
Nate Begeman019f8512006-09-10 23:03:44 +0000533 }
Nate Begeman019f8512006-09-10 23:03:44 +0000534}
535
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000536// InitMem - Write the value of a Constant to the specified memory location,
537// converting it into bytes and relocations.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000538
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000539void MachOWriter::InitMem(const Constant *C, void *Addr, intptr_t Offset,
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000540 const TargetData *TD,
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000541 std::vector<MachineRelocation> &MRs) {
542 typedef std::pair<const Constant*, intptr_t> CPair;
543 std::vector<CPair> WorkList;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000544
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000545 WorkList.push_back(CPair(C,(intptr_t)Addr + Offset));
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000546
Nate Begeman6635f352007-01-26 22:39:48 +0000547 intptr_t ScatteredOffset = 0;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000548
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000549 while (!WorkList.empty()) {
550 const Constant *PC = WorkList.back().first;
551 intptr_t PA = WorkList.back().second;
552 WorkList.pop_back();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000553
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000554 if (isa<UndefValue>(PC)) {
555 continue;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000556 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000557 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000558 TD->getTypeAllocSize(CP->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000559 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
560 WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize));
561 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) {
562 //
563 // FIXME: Handle ConstantExpression. See EE::getConstantValue()
564 //
565 switch (CE->getOpcode()) {
Nate Begeman6635f352007-01-26 22:39:48 +0000566 case Instruction::GetElementPtr: {
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000567 SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
Nate Begeman6635f352007-01-26 22:39:48 +0000568 ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(),
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000569 &Indices[0], Indices.size());
Nate Begeman6635f352007-01-26 22:39:48 +0000570 WorkList.push_back(CPair(CE->getOperand(0), PA));
571 break;
572 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000573 case Instruction::Add:
574 default:
575 cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
576 abort();
577 break;
578 }
Dan Gohman399101a2008-05-23 00:17:26 +0000579 } else if (PC->getType()->isSingleValueType()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000580 unsigned char *ptr = (unsigned char *)PA;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000581 switch (PC->getType()->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000582 case Type::IntegerTyID: {
583 unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth();
584 uint64_t val = cast<ConstantInt>(PC)->getZExtValue();
585 if (NumBits <= 8)
586 ptr[0] = val;
587 else if (NumBits <= 16) {
588 if (TD->isBigEndian())
589 val = ByteSwap_16(val);
590 ptr[0] = val;
591 ptr[1] = val >> 8;
592 } else if (NumBits <= 32) {
593 if (TD->isBigEndian())
594 val = ByteSwap_32(val);
595 ptr[0] = val;
596 ptr[1] = val >> 8;
597 ptr[2] = val >> 16;
598 ptr[3] = val >> 24;
599 } else if (NumBits <= 64) {
600 if (TD->isBigEndian())
601 val = ByteSwap_64(val);
602 ptr[0] = val;
603 ptr[1] = val >> 8;
604 ptr[2] = val >> 16;
605 ptr[3] = val >> 24;
606 ptr[4] = val >> 32;
607 ptr[5] = val >> 40;
608 ptr[6] = val >> 48;
609 ptr[7] = val >> 56;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000610 } else {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000611 assert(0 && "Not implemented: bit widths > 64");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000612 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000613 break;
614 }
615 case Type::FloatTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000616 uint32_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000617 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000618 if (TD->isBigEndian())
619 val = ByteSwap_32(val);
620 ptr[0] = val;
621 ptr[1] = val >> 8;
622 ptr[2] = val >> 16;
623 ptr[3] = val >> 24;
624 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000625 }
626 case Type::DoubleTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000627 uint64_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000628 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000629 if (TD->isBigEndian())
630 val = ByteSwap_64(val);
631 ptr[0] = val;
632 ptr[1] = val >> 8;
633 ptr[2] = val >> 16;
634 ptr[3] = val >> 24;
635 ptr[4] = val >> 32;
636 ptr[5] = val >> 40;
637 ptr[6] = val >> 48;
638 ptr[7] = val >> 56;
639 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000640 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000641 case Type::PointerTyID:
Nate Begeman6635f352007-01-26 22:39:48 +0000642 if (isa<ConstantPointerNull>(PC))
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000643 memset(ptr, 0, TD->getPointerSize());
Nate Begeman6635f352007-01-26 22:39:48 +0000644 else if (const GlobalValue* GV = dyn_cast<GlobalValue>(PC)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000645 // FIXME: what about function stubs?
646 MRs.push_back(MachineRelocation::getGV(PA-(intptr_t)Addr,
647 MachineRelocation::VANILLA,
Nate Begeman6635f352007-01-26 22:39:48 +0000648 const_cast<GlobalValue*>(GV),
649 ScatteredOffset));
650 ScatteredOffset = 0;
651 } else
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000652 assert(0 && "Unknown constant pointer type!");
653 break;
654 default:
655 cerr << "ERROR: Constant unimp for type: " << *PC->getType() << "\n";
656 abort();
657 }
658 } else if (isa<ConstantAggregateZero>(PC)) {
Duncan Sands777d2302009-05-09 07:06:46 +0000659 memset((void*)PA, 0, (size_t)TD->getTypeAllocSize(PC->getType()));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000660 } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000661 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000662 TD->getTypeAllocSize(CPA->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000663 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
664 WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize));
665 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) {
666 const StructLayout *SL =
667 TD->getStructLayout(cast<StructType>(CPS->getType()));
668 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattnerb1919e22007-02-10 19:55:17 +0000669 WorkList.push_back(CPair(CPS->getOperand(i),
670 PA+SL->getElementOffset(i)));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000671 } else {
672 cerr << "Bad Type: " << *PC->getType() << "\n";
673 assert(0 && "Unknown constant type to initialize memory with!");
674 }
675 }
676}
677
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000678//===----------------------------------------------------------------------===//
679// MachOSym Implementation
680//===----------------------------------------------------------------------===//
681
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000682MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000683 const TargetAsmInfo *TAI) :
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000684 GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect),
685 n_desc(0), n_value(0) {
686
Nate Begeman94be2482006-09-08 22:42:09 +0000687 switch (GV->getLinkage()) {
688 default:
689 assert(0 && "Unexpected linkage type!");
690 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000691 case GlobalValue::WeakAnyLinkage:
692 case GlobalValue::WeakODRLinkage:
693 case GlobalValue::LinkOnceAnyLinkage:
694 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000695 case GlobalValue::CommonLinkage:
Nate Begeman94be2482006-09-08 22:42:09 +0000696 assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
697 case GlobalValue::ExternalLinkage:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000698 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman6635f352007-01-26 22:39:48 +0000699 n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT;
Nate Begeman94be2482006-09-08 22:42:09 +0000700 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000701 case GlobalValue::PrivateLinkage:
702 GVName = TAI->getPrivateGlobalPrefix() + name;
703 break;
Nate Begeman94be2482006-09-08 22:42:09 +0000704 case GlobalValue::InternalLinkage:
Nate Begeman6635f352007-01-26 22:39:48 +0000705 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman94be2482006-09-08 22:42:09 +0000706 break;
707 }
708}
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000709
710} // end namespace llvm
711