blob: 5cbe6fd5c93a7ff11053ac366b40905c8c3db3a5 [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
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000025#include "MachO.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000026#include "MachOWriter.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000027#include "MachOCodeEmitter.h"
Nate Begemanbfaaaa62006-12-11 02:20:45 +000028#include "llvm/Constants.h"
29#include "llvm/DerivedTypes.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000030#include "llvm/Module.h"
Bill Wendling8f84f1f2007-02-08 01:35:27 +000031#include "llvm/PassManager.h"
Nate Begemanbfaaaa62006-12-11 02:20:45 +000032#include "llvm/Target/TargetAsmInfo.h"
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000033#include "llvm/Target/TargetData.h"
34#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetMachOWriterInfo.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000036#include "llvm/Support/Mangler.h"
Bill Wendling203d3e42007-01-17 22:22:31 +000037#include "llvm/Support/OutputBuffer.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000038#include "llvm/Support/ErrorHandling.h"
Owen Andersoncb371882008-08-21 00:14:44 +000039#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000040
41namespace llvm {
Nate Begemaneb883af2006-08-23 21:08:52 +000042
Bill Wendling8f84f1f2007-02-08 01:35:27 +000043/// AddMachOWriter - Concrete function to add the Mach-O writer to the function
44/// pass manager.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000045ObjectCodeEmitter *AddMachOWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000046 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000047 TargetMachine &TM) {
48 MachOWriter *MOW = new MachOWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000049 PM.add(MOW);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000050 return MOW->getObjectCodeEmitter();
Bill Wendling8f84f1f2007-02-08 01:35:27 +000051}
52
Nate Begemaneb883af2006-08-23 21:08:52 +000053//===----------------------------------------------------------------------===//
Nate Begemaneb883af2006-08-23 21:08:52 +000054// MachOWriter Implementation
55//===----------------------------------------------------------------------===//
56
Devang Patel19974732007-05-03 01:11:54 +000057char MachOWriter::ID = 0;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000058
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000059MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm)
60 : MachineFunctionPass(&ID), O(o), TM(tm) {
Nate Begemaneb883af2006-08-23 21:08:52 +000061 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
62 isLittleEndian = TM.getTargetData()->isLittleEndian();
63
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000064 TAI = TM.getTargetAsmInfo();
65
Nate Begemaneb883af2006-08-23 21:08:52 +000066 // Create the machine code emitter object for this target.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000067 MachOCE = new MachOCodeEmitter(*this, *getTextSection(true));
Nate Begemaneb883af2006-08-23 21:08:52 +000068}
69
70MachOWriter::~MachOWriter() {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000071 delete MachOCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000072}
73
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000074bool MachOWriter::doInitialization(Module &M) {
75 // Set the magic value, now that we know the pointer size and endianness
76 Header.setMagic(isLittleEndian, is64Bit);
77
78 // Set the file type
79 // FIXME: this only works for object files, we do not support the creation
80 // of dynamic libraries or executables at this time.
81 Header.filetype = MachOHeader::MH_OBJECT;
82
83 Mang = new Mangler(M);
84 return false;
85}
86
87bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
88 return false;
89}
90
91/// doFinalization - Now that the module has been completely processed, emit
92/// the Mach-O file to 'O'.
93bool MachOWriter::doFinalization(Module &M) {
94 // FIXME: we don't handle debug info yet, we should probably do that.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000095 // Okay, the.text section has been completed, build the .data, .bss, and
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000096 // "common" sections next.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000097
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000098 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
99 I != E; ++I)
100 EmitGlobal(I);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000101
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000102 // Emit the header and load commands.
103 EmitHeaderAndLoadCommands();
104
105 // Emit the various sections and their relocation info.
106 EmitSections();
107 EmitRelocations();
108
109 // Write the symbol table and the string table to the end of the file.
110 O.write((char*)&SymT[0], SymT.size());
111 O.write((char*)&StrT[0], StrT.size());
112
113 // We are done with the abstract symbols.
114 SectionList.clear();
115 SymbolTable.clear();
116 DynamicSymbolTable.clear();
117
118 // Release the name mangler object.
119 delete Mang; Mang = 0;
120 return false;
121}
122
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000123// getConstSection - Get constant section for Constant 'C'
124MachOSection *MachOWriter::getConstSection(Constant *C) {
125 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Owen Anderson1ca29d32009-07-13 21:27:19 +0000126 if (CVA && CVA->isCString())
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000127 return getSection("__TEXT", "__cstring",
128 MachOSection::S_CSTRING_LITERALS);
129
130 const Type *Ty = C->getType();
131 if (Ty->isPrimitiveType() || Ty->isInteger()) {
132 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
133 switch(Size) {
134 default: break; // Fall through to __TEXT,__const
135 case 4:
136 return getSection("__TEXT", "__literal4",
137 MachOSection::S_4BYTE_LITERALS);
138 case 8:
139 return getSection("__TEXT", "__literal8",
140 MachOSection::S_8BYTE_LITERALS);
141 case 16:
142 return getSection("__TEXT", "__literal16",
143 MachOSection::S_16BYTE_LITERALS);
144 }
145 }
146 return getSection("__TEXT", "__const");
147}
148
149// getJumpTableSection - Select the Jump Table section
150MachOSection *MachOWriter::getJumpTableSection() {
151 if (TM.getRelocationModel() == Reloc::PIC_)
152 return getTextSection(false);
153 else
154 return getSection("__TEXT", "__const");
155}
156
157// getSection - Return the section with the specified name, creating a new
158// section if one does not already exist.
159MachOSection *MachOWriter::getSection(const std::string &seg,
160 const std::string &sect,
161 unsigned Flags /* = 0 */ ) {
162 MachOSection *MOS = SectionLookup[seg+sect];
163 if (MOS) return MOS;
164
165 MOS = new MachOSection(seg, sect);
166 SectionList.push_back(MOS);
167 MOS->Index = SectionList.size();
168 MOS->flags = MachOSection::S_REGULAR | Flags;
169 SectionLookup[seg+sect] = MOS;
170 return MOS;
171}
172
173// getTextSection - Return text section with different flags for code/data
174MachOSection *MachOWriter::getTextSection(bool isCode /* = true */ ) {
175 if (isCode)
176 return getSection("__TEXT", "__text",
177 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
178 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
179 else
180 return getSection("__TEXT", "__text");
181}
182
183MachOSection *MachOWriter::getBSSSection() {
184 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
185}
186
187// GetJTRelocation - Get a relocation a new BB relocation based
188// on target information.
189MachineRelocation MachOWriter::GetJTRelocation(unsigned Offset,
190 MachineBasicBlock *MBB) const {
191 return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
192}
193
194// GetTargetRelocation - Returns the number of relocations.
195unsigned MachOWriter::GetTargetRelocation(MachineRelocation &MR,
196 unsigned FromIdx, unsigned ToAddr,
197 unsigned ToIndex, OutputBuffer &RelocOut,
198 OutputBuffer &SecOut, bool Scattered,
199 bool Extern) {
200 return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
201 ToIndex, RelocOut,
202 SecOut, Scattered,
203 Extern);
204}
205
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000206void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000207 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000208 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Duncan Sandsd1025932008-01-29 06:23:44 +0000209 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
210
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000211 // Reserve space in the .bss section for this symbol while maintaining the
212 // desired section alignment, which must be at least as much as required by
213 // this symbol.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000214 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000215
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000216 if (Align) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000217 Align = Log2_32(Align);
218 Sec->align = std::max(unsigned(Sec->align), Align);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000219
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000220 Sec->emitAlignment(Sec->align);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000221 }
Nate Begemanfec910c2007-02-28 07:40:50 +0000222 // Globals without external linkage apparently do not go in the symbol table.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000223 if (!GV->hasLocalLinkage()) {
Daniel Dunbar1f316e32009-07-14 16:25:11 +0000224 MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TAI);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000225 Sym.n_value = Sec->size();
Nate Begemanfec910c2007-02-28 07:40:50 +0000226 SymbolTable.push_back(Sym);
227 }
228
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000229 // Record the offset of the symbol, and then allocate space for it.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000230 // FIXME: remove when we have unified size + output buffer
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000231
232 // Now that we know what section the GlovalVariable is going to be emitted
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000233 // into, update our mappings.
234 // FIXME: We may also need to update this when outputting non-GlobalVariable
235 // GlobalValues such as functions.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000236
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000237 GVSection[GV] = Sec;
238 GVOffset[GV] = Sec->size();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000239
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000240 // Allocate space in the section for the global.
241 for (unsigned i = 0; i < Size; ++i)
Bill Wendling203d3e42007-01-17 22:22:31 +0000242 SecDataOut.outbyte(0);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000243}
244
Nate Begemaneb883af2006-08-23 21:08:52 +0000245void MachOWriter::EmitGlobal(GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000246 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000247 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000248 bool NoInit = !GV->hasInitializer();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000249
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000250 // If this global has a zero initializer, it is part of the .bss or common
251 // section.
252 if (NoInit || GV->getInitializer()->isNullValue()) {
253 // If this global is part of the common block, add it now. Variables are
254 // part of the common block if they are zero initialized and allowed to be
255 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000256 if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
257 GV->hasCommonLinkage()) {
Daniel Dunbar1f316e32009-07-14 16:25:11 +0000258 MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV),
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000259 MachOSym::NO_SECT, TAI);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000260 // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
261 // bytes of the symbol.
262 ExtOrCommonSym.n_value = Size;
Nate Begemanfec910c2007-02-28 07:40:50 +0000263 SymbolTable.push_back(ExtOrCommonSym);
264 // Remember that we've seen this symbol
265 GVOffset[GV] = Size;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000266 return;
267 }
268 // Otherwise, this symbol is part of the .bss section.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000269 MachOSection *BSS = getBSSSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000270 AddSymbolToSection(BSS, GV);
271 return;
272 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000273
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000274 // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
275 // 16 bytes, or a cstring. Other read only data goes into a regular const
276 // section. Read-write data goes in the data section.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000277 MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) :
Nate Begeman1257c852007-01-29 21:20:42 +0000278 getDataSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000279 AddSymbolToSection(Sec, GV);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000280 InitMem(GV->getInitializer(), GVOffset[GV], TM.getTargetData(), Sec);
Nate Begemaneb883af2006-08-23 21:08:52 +0000281}
282
283
Nate Begemaneb883af2006-08-23 21:08:52 +0000284
285void MachOWriter::EmitHeaderAndLoadCommands() {
286 // Step #0: Fill in the segment load command size, since we need it to figure
287 // out the rest of the header fields
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000288
Nate Begemaneb883af2006-08-23 21:08:52 +0000289 MachOSegment SEG("", is64Bit);
290 SEG.nsects = SectionList.size();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000291 SEG.cmdsize = SEG.cmdSize(is64Bit) +
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000292 SEG.nsects * SectionList[0]->cmdSize(is64Bit);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000293
Nate Begemaneb883af2006-08-23 21:08:52 +0000294 // Step #1: calculate the number of load commands. We always have at least
295 // one, for the LC_SEGMENT load command, plus two for the normal
296 // and dynamic symbol tables, if there are any symbols.
297 Header.ncmds = SymbolTable.empty() ? 1 : 3;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000298
Nate Begemaneb883af2006-08-23 21:08:52 +0000299 // Step #2: calculate the size of the load commands
300 Header.sizeofcmds = SEG.cmdsize;
301 if (!SymbolTable.empty())
302 Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000303
Nate Begemaneb883af2006-08-23 21:08:52 +0000304 // Step #3: write the header to the file
305 // Local alias to shortenify coming code.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000306 std::vector<unsigned char> &FH = Header.HeaderData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000307 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000308
309 FHOut.outword(Header.magic);
Bill Wendling2b721822007-01-24 07:13:56 +0000310 FHOut.outword(TM.getMachOWriterInfo()->getCPUType());
311 FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType());
Bill Wendling203d3e42007-01-17 22:22:31 +0000312 FHOut.outword(Header.filetype);
313 FHOut.outword(Header.ncmds);
314 FHOut.outword(Header.sizeofcmds);
315 FHOut.outword(Header.flags);
Nate Begemaneb883af2006-08-23 21:08:52 +0000316 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000317 FHOut.outword(Header.reserved);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000318
Nate Begemaneb883af2006-08-23 21:08:52 +0000319 // Step #4: Finish filling in the segment load command and write it out
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000320 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000321 E = SectionList.end(); I != E; ++I)
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000322 SEG.filesize += (*I)->size();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000323
Nate Begemaneb883af2006-08-23 21:08:52 +0000324 SEG.vmsize = SEG.filesize;
325 SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000326
Bill Wendling203d3e42007-01-17 22:22:31 +0000327 FHOut.outword(SEG.cmd);
328 FHOut.outword(SEG.cmdsize);
329 FHOut.outstring(SEG.segname, 16);
330 FHOut.outaddr(SEG.vmaddr);
331 FHOut.outaddr(SEG.vmsize);
332 FHOut.outaddr(SEG.fileoff);
333 FHOut.outaddr(SEG.filesize);
334 FHOut.outword(SEG.maxprot);
335 FHOut.outword(SEG.initprot);
336 FHOut.outword(SEG.nsects);
337 FHOut.outword(SEG.flags);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000338
339 // Step #5: Finish filling in the fields of the MachOSections
Nate Begeman94be2482006-09-08 22:42:09 +0000340 uint64_t currentAddr = 0;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000341 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000342 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000343 MachOSection *MOS = *I;
344 MOS->addr = currentAddr;
345 MOS->offset = currentAddr + SEG.fileoff;
Nate Begeman94be2482006-09-08 22:42:09 +0000346 // FIXME: do we need to do something with alignment here?
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000347 currentAddr += MOS->size();
Nate Begeman94be2482006-09-08 22:42:09 +0000348 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000349
Nate Begemanfec910c2007-02-28 07:40:50 +0000350 // Step #6: Emit the symbol table to temporary buffers, so that we know the
351 // size of the string table when we write the next load command. This also
352 // sorts and assigns indices to each of the symbols, which is necessary for
353 // emitting relocations to externally-defined objects.
354 BufferSymbolAndStringTable();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000355
Nate Begemanfec910c2007-02-28 07:40:50 +0000356 // Step #7: Calculate the number of relocations for each section and write out
Nate Begeman94be2482006-09-08 22:42:09 +0000357 // the section commands for each section
358 currentAddr += SEG.fileoff;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000359 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman94be2482006-09-08 22:42:09 +0000360 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000361 MachOSection *MOS = *I;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000362
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000363 // Convert the relocations to target-specific relocations, and fill in the
364 // relocation offset for this section.
365 CalculateRelocations(*MOS);
366 MOS->reloff = MOS->nreloc ? currentAddr : 0;
367 currentAddr += MOS->nreloc * 8;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000368
Nate Begeman94be2482006-09-08 22:42:09 +0000369 // write the finalized section command to the output buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000370 FHOut.outstring(MOS->sectname, 16);
371 FHOut.outstring(MOS->segname, 16);
372 FHOut.outaddr(MOS->addr);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000373 FHOut.outaddr(MOS->size());
Bill Wendling203d3e42007-01-17 22:22:31 +0000374 FHOut.outword(MOS->offset);
375 FHOut.outword(MOS->align);
376 FHOut.outword(MOS->reloff);
377 FHOut.outword(MOS->nreloc);
378 FHOut.outword(MOS->flags);
379 FHOut.outword(MOS->reserved1);
380 FHOut.outword(MOS->reserved2);
Nate Begemaneb883af2006-08-23 21:08:52 +0000381 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000382 FHOut.outword(MOS->reserved3);
Nate Begemaneb883af2006-08-23 21:08:52 +0000383 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000384
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000385 // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands
Nate Begeman94be2482006-09-08 22:42:09 +0000386 SymTab.symoff = currentAddr;
Nate Begemaneb883af2006-08-23 21:08:52 +0000387 SymTab.nsyms = SymbolTable.size();
Nate Begemand2030e62006-08-26 15:46:34 +0000388 SymTab.stroff = SymTab.symoff + SymT.size();
389 SymTab.strsize = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000390 FHOut.outword(SymTab.cmd);
391 FHOut.outword(SymTab.cmdsize);
392 FHOut.outword(SymTab.symoff);
393 FHOut.outword(SymTab.nsyms);
394 FHOut.outword(SymTab.stroff);
395 FHOut.outword(SymTab.strsize);
Nate Begemaneb883af2006-08-23 21:08:52 +0000396
397 // FIXME: set DySymTab fields appropriately
Nate Begemand2030e62006-08-26 15:46:34 +0000398 // We should probably just update these in BufferSymbolAndStringTable since
399 // thats where we're partitioning up the different kinds of symbols.
Bill Wendling203d3e42007-01-17 22:22:31 +0000400 FHOut.outword(DySymTab.cmd);
401 FHOut.outword(DySymTab.cmdsize);
402 FHOut.outword(DySymTab.ilocalsym);
403 FHOut.outword(DySymTab.nlocalsym);
404 FHOut.outword(DySymTab.iextdefsym);
405 FHOut.outword(DySymTab.nextdefsym);
406 FHOut.outword(DySymTab.iundefsym);
407 FHOut.outword(DySymTab.nundefsym);
408 FHOut.outword(DySymTab.tocoff);
409 FHOut.outword(DySymTab.ntoc);
410 FHOut.outword(DySymTab.modtaboff);
411 FHOut.outword(DySymTab.nmodtab);
412 FHOut.outword(DySymTab.extrefsymoff);
413 FHOut.outword(DySymTab.nextrefsyms);
414 FHOut.outword(DySymTab.indirectsymoff);
415 FHOut.outword(DySymTab.nindirectsyms);
416 FHOut.outword(DySymTab.extreloff);
417 FHOut.outword(DySymTab.nextrel);
418 FHOut.outword(DySymTab.locreloff);
419 FHOut.outword(DySymTab.nlocrel);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000420
Nate Begemaneb883af2006-08-23 21:08:52 +0000421 O.write((char*)&FH[0], FH.size());
422}
423
424/// EmitSections - Now that we have constructed the file header and load
425/// commands, emit the data for each section to the file.
426void MachOWriter::EmitSections() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000427 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000428 E = SectionList.end(); I != E; ++I)
429 // Emit the contents of each section
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000430 if ((*I)->size())
431 O.write((char*)&(*I)->getData()[0], (*I)->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000432}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000433
434/// EmitRelocations - emit relocation data from buffer.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000435void MachOWriter::EmitRelocations() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000436 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000437 E = SectionList.end(); I != E; ++I)
438 // Emit the relocation entry data for each section.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000439 if ((*I)->RelocBuffer.size())
440 O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
Nate Begemaneb883af2006-08-23 21:08:52 +0000441}
442
Nate Begemand2030e62006-08-26 15:46:34 +0000443/// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
444/// each a string table index so that they appear in the correct order in the
445/// output file.
446void MachOWriter::BufferSymbolAndStringTable() {
447 // The order of the symbol table is:
448 // 1. local symbols
449 // 2. defined external symbols (sorted by name)
450 // 3. undefined external symbols (sorted by name)
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000451
Nate Begemanfec910c2007-02-28 07:40:50 +0000452 // Before sorting the symbols, check the PendingGlobals for any undefined
453 // globals that need to be put in the symbol table.
454 for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(),
455 E = PendingGlobals.end(); I != E; ++I) {
456 if (GVOffset[*I] == 0 && GVSection[*I] == 0) {
Daniel Dunbar1f316e32009-07-14 16:25:11 +0000457 MachOSym UndfSym(*I, Mang->getValueName(*I), MachOSym::NO_SECT, TAI);
Nate Begemanfec910c2007-02-28 07:40:50 +0000458 SymbolTable.push_back(UndfSym);
459 GVOffset[*I] = -1;
460 }
461 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000462
Nate Begemand2030e62006-08-26 15:46:34 +0000463 // Sort the symbols by name, so that when we partition the symbols by scope
464 // of definition, we won't have to sort by name within each partition.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000465 std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp());
466
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000467 // Parition the symbol table entries so that all local symbols come before
Nate Begemand2030e62006-08-26 15:46:34 +0000468 // all symbols with external linkage. { 1 | 2 3 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000469 std::partition(SymbolTable.begin(), SymbolTable.end(),
470 MachOSym::PartitionByLocal);
471
Nate Begemand2030e62006-08-26 15:46:34 +0000472 // Advance iterator to beginning of external symbols and partition so that
473 // all external symbols defined in this module come before all external
474 // symbols defined elsewhere. { 1 | 2 | 3 }
475 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
476 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000477 if (!MachOSym::PartitionByLocal(*I)) {
478 std::partition(I, E, MachOSym::PartitionByDefined);
Nate Begemand2030e62006-08-26 15:46:34 +0000479 break;
480 }
481 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000482
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000483 // Calculate the starting index for each of the local, extern defined, and
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000484 // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB
485 // load command.
486 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
487 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000488 if (MachOSym::PartitionByLocal(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000489 ++DySymTab.nlocalsym;
490 ++DySymTab.iextdefsym;
Nate Begeman6635f352007-01-26 22:39:48 +0000491 ++DySymTab.iundefsym;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000492 } else if (MachOSym::PartitionByDefined(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000493 ++DySymTab.nextdefsym;
494 ++DySymTab.iundefsym;
495 } else {
496 ++DySymTab.nundefsym;
497 }
498 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000499
Nate Begemaneb883af2006-08-23 21:08:52 +0000500 // Write out a leading zero byte when emitting string table, for n_strx == 0
501 // which means an empty string.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000502 OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000503 StrTOut.outbyte(0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000504
Nate Begemand2030e62006-08-26 15:46:34 +0000505 // The order of the string table is:
506 // 1. strings for external symbols
507 // 2. strings for local symbols
508 // Since this is the opposite order from the symbol table, which we have just
509 // sorted, we can walk the symbol table backwards to output the string table.
510 for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(),
511 E = SymbolTable.rend(); I != E; ++I) {
512 if (I->GVName == "") {
513 I->n_strx = 0;
514 } else {
515 I->n_strx = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000516 StrTOut.outstring(I->GVName, I->GVName.length()+1);
Nate Begemand2030e62006-08-26 15:46:34 +0000517 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000518 }
Nate Begemand2030e62006-08-26 15:46:34 +0000519
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000520 OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000521
Nate Begemanfec910c2007-02-28 07:40:50 +0000522 unsigned index = 0;
Nate Begemand2030e62006-08-26 15:46:34 +0000523 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
Nate Begemanfec910c2007-02-28 07:40:50 +0000524 E = SymbolTable.end(); I != E; ++I, ++index) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000525 // Add the section base address to the section offset in the n_value field
526 // to calculate the full address.
527 // FIXME: handle symbols where the n_value field is not the address
528 GlobalValue *GV = const_cast<GlobalValue*>(I->GV);
529 if (GV && GVSection[GV])
530 I->n_value += GVSection[GV]->addr;
Nate Begemanfec910c2007-02-28 07:40:50 +0000531 if (GV && (GVOffset[GV] == -1))
532 GVOffset[GV] = index;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000533
Nate Begemand2030e62006-08-26 15:46:34 +0000534 // Emit nlist to buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000535 SymTOut.outword(I->n_strx);
536 SymTOut.outbyte(I->n_type);
537 SymTOut.outbyte(I->n_sect);
538 SymTOut.outhalf(I->n_desc);
539 SymTOut.outaddr(I->n_value);
Nate Begemand2030e62006-08-26 15:46:34 +0000540 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000541}
Nate Begeman94be2482006-09-08 22:42:09 +0000542
Nate Begeman019f8512006-09-10 23:03:44 +0000543/// CalculateRelocations - For each MachineRelocation in the current section,
544/// calculate the index of the section containing the object to be relocated,
545/// and the offset into that section. From this information, create the
546/// appropriate target-specific MachORelocation type and add buffer it to be
547/// written out after we are finished writing out sections.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000548void MachOWriter::CalculateRelocations(MachOSection &MOS) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000549 std::vector<MachineRelocation> Relocations = MOS.getRelocations();
550 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
551 MachineRelocation &MR = Relocations[i];
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000552 unsigned TargetSection = MR.getConstantVal();
Nate Begemanaf806382007-03-03 06:18:18 +0000553 unsigned TargetAddr = 0;
554 unsigned TargetIndex = 0;
Nate Begeman6635f352007-01-26 22:39:48 +0000555
556 // This is a scattered relocation entry if it points to a global value with
557 // a non-zero offset.
558 bool Scattered = false;
Nate Begemanfec910c2007-02-28 07:40:50 +0000559 bool Extern = false;
Nate Begemanaf806382007-03-03 06:18:18 +0000560
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000561 // Since we may not have seen the GlobalValue we were interested in yet at
562 // the time we emitted the relocation for it, fix it up now so that it
563 // points to the offset into the correct section.
564 if (MR.isGlobalValue()) {
565 GlobalValue *GV = MR.getGlobalValue();
566 MachOSection *MOSPtr = GVSection[GV];
Nate Begeman6635f352007-01-26 22:39:48 +0000567 intptr_t Offset = GVOffset[GV];
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000568
Nate Begemanfec910c2007-02-28 07:40:50 +0000569 // If we have never seen the global before, it must be to a symbol
570 // defined in another module (N_UNDF).
Nate Begeman1257c852007-01-29 21:20:42 +0000571 if (!MOSPtr) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000572 // FIXME: need to append stub suffix
573 Extern = true;
574 TargetAddr = 0;
575 TargetIndex = GVOffset[GV];
576 } else {
577 Scattered = TargetSection != 0;
578 TargetSection = MOSPtr->Index;
Nate Begemanaf806382007-03-03 06:18:18 +0000579 }
580 MR.setResultPointer((void*)Offset);
581 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000582
Nate Begemanaf806382007-03-03 06:18:18 +0000583 // If the symbol is locally defined, pass in the address of the section and
584 // the section index to the code which will generate the target relocation.
585 if (!Extern) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000586 MachOSection &To = *SectionList[TargetSection - 1];
587 TargetAddr = To.addr;
588 TargetIndex = To.Index;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000589 }
Bill Wendling886b4122007-02-03 02:39:40 +0000590
591 OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000592 OutputBuffer SecOut(MOS.getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000593
Nate Begemanfec910c2007-02-28 07:40:50 +0000594 MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex,
595 RelocOut, SecOut, Scattered, Extern);
Nate Begeman019f8512006-09-10 23:03:44 +0000596 }
Nate Begeman019f8512006-09-10 23:03:44 +0000597}
598
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000599// InitMem - Write the value of a Constant to the specified memory location,
600// converting it into bytes and relocations.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000601void MachOWriter::InitMem(const Constant *C, uintptr_t Offset,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000602 const TargetData *TD, MachOSection* mos) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000603 typedef std::pair<const Constant*, intptr_t> CPair;
604 std::vector<CPair> WorkList;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000605 uint8_t *Addr = &mos->getData()[0];
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000606
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000607 WorkList.push_back(CPair(C,(intptr_t)Addr + Offset));
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000608
Nate Begeman6635f352007-01-26 22:39:48 +0000609 intptr_t ScatteredOffset = 0;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000610
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000611 while (!WorkList.empty()) {
612 const Constant *PC = WorkList.back().first;
613 intptr_t PA = WorkList.back().second;
614 WorkList.pop_back();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000615
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000616 if (isa<UndefValue>(PC)) {
617 continue;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000618 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000619 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000620 TD->getTypeAllocSize(CP->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000621 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
622 WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize));
623 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) {
624 //
625 // FIXME: Handle ConstantExpression. See EE::getConstantValue()
626 //
627 switch (CE->getOpcode()) {
Nate Begeman6635f352007-01-26 22:39:48 +0000628 case Instruction::GetElementPtr: {
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000629 SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
Nate Begeman6635f352007-01-26 22:39:48 +0000630 ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(),
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000631 &Indices[0], Indices.size());
Nate Begeman6635f352007-01-26 22:39:48 +0000632 WorkList.push_back(CPair(CE->getOperand(0), PA));
633 break;
634 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000635 case Instruction::Add:
636 default:
637 cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000638 llvm_unreachable(0);
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000639 }
Dan Gohman399101a2008-05-23 00:17:26 +0000640 } else if (PC->getType()->isSingleValueType()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000641 unsigned char *ptr = (unsigned char *)PA;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000642 switch (PC->getType()->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000643 case Type::IntegerTyID: {
644 unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth();
645 uint64_t val = cast<ConstantInt>(PC)->getZExtValue();
646 if (NumBits <= 8)
647 ptr[0] = val;
648 else if (NumBits <= 16) {
649 if (TD->isBigEndian())
650 val = ByteSwap_16(val);
651 ptr[0] = val;
652 ptr[1] = val >> 8;
653 } else if (NumBits <= 32) {
654 if (TD->isBigEndian())
655 val = ByteSwap_32(val);
656 ptr[0] = val;
657 ptr[1] = val >> 8;
658 ptr[2] = val >> 16;
659 ptr[3] = val >> 24;
660 } else if (NumBits <= 64) {
661 if (TD->isBigEndian())
662 val = ByteSwap_64(val);
663 ptr[0] = val;
664 ptr[1] = val >> 8;
665 ptr[2] = val >> 16;
666 ptr[3] = val >> 24;
667 ptr[4] = val >> 32;
668 ptr[5] = val >> 40;
669 ptr[6] = val >> 48;
670 ptr[7] = val >> 56;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000671 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000672 llvm_unreachable("Not implemented: bit widths > 64");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000673 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000674 break;
675 }
676 case Type::FloatTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000677 uint32_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000678 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000679 if (TD->isBigEndian())
680 val = ByteSwap_32(val);
681 ptr[0] = val;
682 ptr[1] = val >> 8;
683 ptr[2] = val >> 16;
684 ptr[3] = val >> 24;
685 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000686 }
687 case Type::DoubleTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000688 uint64_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000689 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000690 if (TD->isBigEndian())
691 val = ByteSwap_64(val);
692 ptr[0] = val;
693 ptr[1] = val >> 8;
694 ptr[2] = val >> 16;
695 ptr[3] = val >> 24;
696 ptr[4] = val >> 32;
697 ptr[5] = val >> 40;
698 ptr[6] = val >> 48;
699 ptr[7] = val >> 56;
700 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000701 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000702 case Type::PointerTyID:
Nate Begeman6635f352007-01-26 22:39:48 +0000703 if (isa<ConstantPointerNull>(PC))
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000704 memset(ptr, 0, TD->getPointerSize());
Nate Begeman6635f352007-01-26 22:39:48 +0000705 else if (const GlobalValue* GV = dyn_cast<GlobalValue>(PC)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000706 // FIXME: what about function stubs?
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000707 mos->addRelocation(MachineRelocation::getGV(PA-(intptr_t)Addr,
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000708 MachineRelocation::VANILLA,
Nate Begeman6635f352007-01-26 22:39:48 +0000709 const_cast<GlobalValue*>(GV),
710 ScatteredOffset));
711 ScatteredOffset = 0;
712 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000713 llvm_unreachable("Unknown constant pointer type!");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000714 break;
715 default:
Torok Edwin7d696d82009-07-11 13:10:19 +0000716 std::string msg;
717 raw_string_ostream Msg(msg);
718 Msg << "ERROR: Constant unimp for type: " << *PC->getType();
719 llvm_report_error(Msg.str());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000720 }
721 } else if (isa<ConstantAggregateZero>(PC)) {
Duncan Sands777d2302009-05-09 07:06:46 +0000722 memset((void*)PA, 0, (size_t)TD->getTypeAllocSize(PC->getType()));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000723 } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000724 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000725 TD->getTypeAllocSize(CPA->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000726 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
727 WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize));
728 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) {
729 const StructLayout *SL =
730 TD->getStructLayout(cast<StructType>(CPS->getType()));
731 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattnerb1919e22007-02-10 19:55:17 +0000732 WorkList.push_back(CPair(CPS->getOperand(i),
733 PA+SL->getElementOffset(i)));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000734 } else {
735 cerr << "Bad Type: " << *PC->getType() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000736 llvm_unreachable("Unknown constant type to initialize memory with!");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000737 }
738 }
739}
740
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000741//===----------------------------------------------------------------------===//
742// MachOSym Implementation
743//===----------------------------------------------------------------------===//
744
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000745MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000746 const TargetAsmInfo *TAI) :
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000747 GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect),
748 n_desc(0), n_value(0) {
749
Nate Begeman94be2482006-09-08 22:42:09 +0000750 switch (GV->getLinkage()) {
751 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000752 llvm_unreachable("Unexpected linkage type!");
Nate Begeman94be2482006-09-08 22:42:09 +0000753 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000754 case GlobalValue::WeakAnyLinkage:
755 case GlobalValue::WeakODRLinkage:
756 case GlobalValue::LinkOnceAnyLinkage:
757 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000758 case GlobalValue::CommonLinkage:
Nate Begeman94be2482006-09-08 22:42:09 +0000759 assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
760 case GlobalValue::ExternalLinkage:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000761 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman6635f352007-01-26 22:39:48 +0000762 n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT;
Nate Begeman94be2482006-09-08 22:42:09 +0000763 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000764 case GlobalValue::PrivateLinkage:
765 GVName = TAI->getPrivateGlobalPrefix() + name;
766 break;
Nate Begeman94be2482006-09-08 22:42:09 +0000767 case GlobalValue::InternalLinkage:
Nate Begeman6635f352007-01-26 22:39:48 +0000768 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman94be2482006-09-08 22:42:09 +0000769 break;
770 }
771}
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000772
773} // end namespace llvm
774