blob: 337eab18277ae14b23aba829536eaa50a471fa5b [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"
Chris Lattneraf76e592009-08-22 20:48:53 +000032#include "llvm/MC/MCAsmInfo.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"
David Greene7d30d622010-01-04 23:14:46 +000036#include "llvm/Support/Debug.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000037#include "llvm/Support/Mangler.h"
Bill Wendling203d3e42007-01-17 22:22:31 +000038#include "llvm/Support/OutputBuffer.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000039#include "llvm/Support/ErrorHandling.h"
Owen Andersoncb371882008-08-21 00:14:44 +000040#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000041
42namespace llvm {
Nate Begemaneb883af2006-08-23 21:08:52 +000043
Bill Wendling8f84f1f2007-02-08 01:35:27 +000044/// AddMachOWriter - Concrete function to add the Mach-O writer to the function
45/// pass manager.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000046ObjectCodeEmitter *AddMachOWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000047 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000048 TargetMachine &TM) {
49 MachOWriter *MOW = new MachOWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000050 PM.add(MOW);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000051 return MOW->getObjectCodeEmitter();
Bill Wendling8f84f1f2007-02-08 01:35:27 +000052}
53
Nate Begemaneb883af2006-08-23 21:08:52 +000054//===----------------------------------------------------------------------===//
Nate Begemaneb883af2006-08-23 21:08:52 +000055// MachOWriter Implementation
56//===----------------------------------------------------------------------===//
57
Devang Patel19974732007-05-03 01:11:54 +000058char MachOWriter::ID = 0;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000059
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000060MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm)
61 : MachineFunctionPass(&ID), O(o), TM(tm) {
Nate Begemaneb883af2006-08-23 21:08:52 +000062 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
63 isLittleEndian = TM.getTargetData()->isLittleEndian();
64
Chris Lattner33adcfb2009-08-22 21:43:10 +000065 MAI = TM.getMCAsmInfo();
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000066
Nate Begemaneb883af2006-08-23 21:08:52 +000067 // Create the machine code emitter object for this target.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000068 MachOCE = new MachOCodeEmitter(*this, *getTextSection(true));
Nate Begemaneb883af2006-08-23 21:08:52 +000069}
70
71MachOWriter::~MachOWriter() {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000072 delete MachOCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000073}
74
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000075bool MachOWriter::doInitialization(Module &M) {
76 // Set the magic value, now that we know the pointer size and endianness
77 Header.setMagic(isLittleEndian, is64Bit);
78
79 // Set the file type
80 // FIXME: this only works for object files, we do not support the creation
81 // of dynamic libraries or executables at this time.
82 Header.filetype = MachOHeader::MH_OBJECT;
83
84 Mang = new Mangler(M);
85 return false;
86}
87
88bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
89 return false;
90}
91
92/// doFinalization - Now that the module has been completely processed, emit
93/// the Mach-O file to 'O'.
94bool MachOWriter::doFinalization(Module &M) {
95 // FIXME: we don't handle debug info yet, we should probably do that.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000096 // Okay, the.text section has been completed, build the .data, .bss, and
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000097 // "common" sections next.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000098
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000099 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
100 I != E; ++I)
101 EmitGlobal(I);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000102
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000103 // Emit the header and load commands.
104 EmitHeaderAndLoadCommands();
105
106 // Emit the various sections and their relocation info.
107 EmitSections();
108 EmitRelocations();
109
110 // Write the symbol table and the string table to the end of the file.
111 O.write((char*)&SymT[0], SymT.size());
112 O.write((char*)&StrT[0], StrT.size());
113
114 // We are done with the abstract symbols.
115 SectionList.clear();
116 SymbolTable.clear();
117 DynamicSymbolTable.clear();
118
119 // Release the name mangler object.
120 delete Mang; Mang = 0;
121 return false;
122}
123
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000124// getConstSection - Get constant section for Constant 'C'
125MachOSection *MachOWriter::getConstSection(Constant *C) {
126 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
Owen Anderson1ca29d32009-07-13 21:27:19 +0000127 if (CVA && CVA->isCString())
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000128 return getSection("__TEXT", "__cstring",
129 MachOSection::S_CSTRING_LITERALS);
130
131 const Type *Ty = C->getType();
132 if (Ty->isPrimitiveType() || Ty->isInteger()) {
133 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
134 switch(Size) {
135 default: break; // Fall through to __TEXT,__const
136 case 4:
137 return getSection("__TEXT", "__literal4",
138 MachOSection::S_4BYTE_LITERALS);
139 case 8:
140 return getSection("__TEXT", "__literal8",
141 MachOSection::S_8BYTE_LITERALS);
142 case 16:
143 return getSection("__TEXT", "__literal16",
144 MachOSection::S_16BYTE_LITERALS);
145 }
146 }
147 return getSection("__TEXT", "__const");
148}
149
150// getJumpTableSection - Select the Jump Table section
151MachOSection *MachOWriter::getJumpTableSection() {
152 if (TM.getRelocationModel() == Reloc::PIC_)
153 return getTextSection(false);
154 else
155 return getSection("__TEXT", "__const");
156}
157
158// getSection - Return the section with the specified name, creating a new
159// section if one does not already exist.
160MachOSection *MachOWriter::getSection(const std::string &seg,
161 const std::string &sect,
162 unsigned Flags /* = 0 */ ) {
163 MachOSection *MOS = SectionLookup[seg+sect];
164 if (MOS) return MOS;
165
166 MOS = new MachOSection(seg, sect);
167 SectionList.push_back(MOS);
168 MOS->Index = SectionList.size();
169 MOS->flags = MachOSection::S_REGULAR | Flags;
170 SectionLookup[seg+sect] = MOS;
171 return MOS;
172}
173
174// getTextSection - Return text section with different flags for code/data
175MachOSection *MachOWriter::getTextSection(bool isCode /* = true */ ) {
176 if (isCode)
177 return getSection("__TEXT", "__text",
178 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
179 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
180 else
181 return getSection("__TEXT", "__text");
182}
183
184MachOSection *MachOWriter::getBSSSection() {
185 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
186}
187
188// GetJTRelocation - Get a relocation a new BB relocation based
189// on target information.
190MachineRelocation MachOWriter::GetJTRelocation(unsigned Offset,
191 MachineBasicBlock *MBB) const {
192 return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
193}
194
195// GetTargetRelocation - Returns the number of relocations.
196unsigned MachOWriter::GetTargetRelocation(MachineRelocation &MR,
197 unsigned FromIdx, unsigned ToAddr,
198 unsigned ToIndex, OutputBuffer &RelocOut,
199 OutputBuffer &SecOut, bool Scattered,
200 bool Extern) {
201 return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
202 ToIndex, RelocOut,
203 SecOut, Scattered,
204 Extern);
205}
206
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000207void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000208 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000209 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Duncan Sandsd1025932008-01-29 06:23:44 +0000210 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
211
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000212 // Reserve space in the .bss section for this symbol while maintaining the
213 // desired section alignment, which must be at least as much as required by
214 // this symbol.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000215 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000216
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000217 if (Align) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000218 Align = Log2_32(Align);
219 Sec->align = std::max(unsigned(Sec->align), Align);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000220
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000221 Sec->emitAlignment(Sec->align);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000222 }
Nate Begemanfec910c2007-02-28 07:40:50 +0000223 // Globals without external linkage apparently do not go in the symbol table.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000224 if (!GV->hasLocalLinkage()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000225 MachOSym Sym(GV, Mang->getMangledName(GV), Sec->Index, MAI);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000226 Sym.n_value = Sec->size();
Nate Begemanfec910c2007-02-28 07:40:50 +0000227 SymbolTable.push_back(Sym);
228 }
229
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000230 // Record the offset of the symbol, and then allocate space for it.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000231 // FIXME: remove when we have unified size + output buffer
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000232
233 // Now that we know what section the GlovalVariable is going to be emitted
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000234 // into, update our mappings.
235 // FIXME: We may also need to update this when outputting non-GlobalVariable
236 // GlobalValues such as functions.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000237
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000238 GVSection[GV] = Sec;
239 GVOffset[GV] = Sec->size();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000240
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000241 // Allocate space in the section for the global.
242 for (unsigned i = 0; i < Size; ++i)
Bill Wendling203d3e42007-01-17 22:22:31 +0000243 SecDataOut.outbyte(0);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000244}
245
Nate Begemaneb883af2006-08-23 21:08:52 +0000246void MachOWriter::EmitGlobal(GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000247 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000248 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000249 bool NoInit = !GV->hasInitializer();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000250
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000251 // If this global has a zero initializer, it is part of the .bss or common
252 // section.
253 if (NoInit || GV->getInitializer()->isNullValue()) {
254 // If this global is part of the common block, add it now. Variables are
255 // part of the common block if they are zero initialized and allowed to be
256 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000257 if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
258 GV->hasCommonLinkage()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000259 MachOSym ExtOrCommonSym(GV, Mang->getMangledName(GV),
Chris Lattner33adcfb2009-08-22 21:43:10 +0000260 MachOSym::NO_SECT, MAI);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000261 // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
262 // bytes of the symbol.
263 ExtOrCommonSym.n_value = Size;
Nate Begemanfec910c2007-02-28 07:40:50 +0000264 SymbolTable.push_back(ExtOrCommonSym);
265 // Remember that we've seen this symbol
266 GVOffset[GV] = Size;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000267 return;
268 }
269 // Otherwise, this symbol is part of the .bss section.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000270 MachOSection *BSS = getBSSSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000271 AddSymbolToSection(BSS, GV);
272 return;
273 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000274
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000275 // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
276 // 16 bytes, or a cstring. Other read only data goes into a regular const
277 // section. Read-write data goes in the data section.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000278 MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) :
Nate Begeman1257c852007-01-29 21:20:42 +0000279 getDataSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000280 AddSymbolToSection(Sec, GV);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000281 InitMem(GV->getInitializer(), GVOffset[GV], TM.getTargetData(), Sec);
Nate Begemaneb883af2006-08-23 21:08:52 +0000282}
283
284
Nate Begemaneb883af2006-08-23 21:08:52 +0000285
286void MachOWriter::EmitHeaderAndLoadCommands() {
287 // Step #0: Fill in the segment load command size, since we need it to figure
288 // out the rest of the header fields
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000289
Nate Begemaneb883af2006-08-23 21:08:52 +0000290 MachOSegment SEG("", is64Bit);
291 SEG.nsects = SectionList.size();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000292 SEG.cmdsize = SEG.cmdSize(is64Bit) +
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000293 SEG.nsects * SectionList[0]->cmdSize(is64Bit);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000294
Nate Begemaneb883af2006-08-23 21:08:52 +0000295 // Step #1: calculate the number of load commands. We always have at least
296 // one, for the LC_SEGMENT load command, plus two for the normal
297 // and dynamic symbol tables, if there are any symbols.
298 Header.ncmds = SymbolTable.empty() ? 1 : 3;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000299
Nate Begemaneb883af2006-08-23 21:08:52 +0000300 // Step #2: calculate the size of the load commands
301 Header.sizeofcmds = SEG.cmdsize;
302 if (!SymbolTable.empty())
303 Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000304
Nate Begemaneb883af2006-08-23 21:08:52 +0000305 // Step #3: write the header to the file
306 // Local alias to shortenify coming code.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000307 std::vector<unsigned char> &FH = Header.HeaderData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000308 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000309
310 FHOut.outword(Header.magic);
Bill Wendling2b721822007-01-24 07:13:56 +0000311 FHOut.outword(TM.getMachOWriterInfo()->getCPUType());
312 FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType());
Bill Wendling203d3e42007-01-17 22:22:31 +0000313 FHOut.outword(Header.filetype);
314 FHOut.outword(Header.ncmds);
315 FHOut.outword(Header.sizeofcmds);
316 FHOut.outword(Header.flags);
Nate Begemaneb883af2006-08-23 21:08:52 +0000317 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000318 FHOut.outword(Header.reserved);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000319
Nate Begemaneb883af2006-08-23 21:08:52 +0000320 // Step #4: Finish filling in the segment load command and write it out
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000321 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000322 E = SectionList.end(); I != E; ++I)
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000323 SEG.filesize += (*I)->size();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000324
Nate Begemaneb883af2006-08-23 21:08:52 +0000325 SEG.vmsize = SEG.filesize;
326 SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000327
Bill Wendling203d3e42007-01-17 22:22:31 +0000328 FHOut.outword(SEG.cmd);
329 FHOut.outword(SEG.cmdsize);
330 FHOut.outstring(SEG.segname, 16);
331 FHOut.outaddr(SEG.vmaddr);
332 FHOut.outaddr(SEG.vmsize);
333 FHOut.outaddr(SEG.fileoff);
334 FHOut.outaddr(SEG.filesize);
335 FHOut.outword(SEG.maxprot);
336 FHOut.outword(SEG.initprot);
337 FHOut.outword(SEG.nsects);
338 FHOut.outword(SEG.flags);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000339
340 // Step #5: Finish filling in the fields of the MachOSections
Nate Begeman94be2482006-09-08 22:42:09 +0000341 uint64_t currentAddr = 0;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000342 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000343 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000344 MachOSection *MOS = *I;
345 MOS->addr = currentAddr;
346 MOS->offset = currentAddr + SEG.fileoff;
Nate Begeman94be2482006-09-08 22:42:09 +0000347 // FIXME: do we need to do something with alignment here?
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000348 currentAddr += MOS->size();
Nate Begeman94be2482006-09-08 22:42:09 +0000349 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000350
Nate Begemanfec910c2007-02-28 07:40:50 +0000351 // Step #6: Emit the symbol table to temporary buffers, so that we know the
352 // size of the string table when we write the next load command. This also
353 // sorts and assigns indices to each of the symbols, which is necessary for
354 // emitting relocations to externally-defined objects.
355 BufferSymbolAndStringTable();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000356
Nate Begemanfec910c2007-02-28 07:40:50 +0000357 // Step #7: Calculate the number of relocations for each section and write out
Nate Begeman94be2482006-09-08 22:42:09 +0000358 // the section commands for each section
359 currentAddr += SEG.fileoff;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000360 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman94be2482006-09-08 22:42:09 +0000361 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000362 MachOSection *MOS = *I;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000363
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000364 // Convert the relocations to target-specific relocations, and fill in the
365 // relocation offset for this section.
366 CalculateRelocations(*MOS);
367 MOS->reloff = MOS->nreloc ? currentAddr : 0;
368 currentAddr += MOS->nreloc * 8;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000369
Nate Begeman94be2482006-09-08 22:42:09 +0000370 // write the finalized section command to the output buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000371 FHOut.outstring(MOS->sectname, 16);
372 FHOut.outstring(MOS->segname, 16);
373 FHOut.outaddr(MOS->addr);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000374 FHOut.outaddr(MOS->size());
Bill Wendling203d3e42007-01-17 22:22:31 +0000375 FHOut.outword(MOS->offset);
376 FHOut.outword(MOS->align);
377 FHOut.outword(MOS->reloff);
378 FHOut.outword(MOS->nreloc);
379 FHOut.outword(MOS->flags);
380 FHOut.outword(MOS->reserved1);
381 FHOut.outword(MOS->reserved2);
Nate Begemaneb883af2006-08-23 21:08:52 +0000382 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000383 FHOut.outword(MOS->reserved3);
Nate Begemaneb883af2006-08-23 21:08:52 +0000384 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000385
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000386 // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands
Nate Begeman94be2482006-09-08 22:42:09 +0000387 SymTab.symoff = currentAddr;
Nate Begemaneb883af2006-08-23 21:08:52 +0000388 SymTab.nsyms = SymbolTable.size();
Nate Begemand2030e62006-08-26 15:46:34 +0000389 SymTab.stroff = SymTab.symoff + SymT.size();
390 SymTab.strsize = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000391 FHOut.outword(SymTab.cmd);
392 FHOut.outword(SymTab.cmdsize);
393 FHOut.outword(SymTab.symoff);
394 FHOut.outword(SymTab.nsyms);
395 FHOut.outword(SymTab.stroff);
396 FHOut.outword(SymTab.strsize);
Nate Begemaneb883af2006-08-23 21:08:52 +0000397
398 // FIXME: set DySymTab fields appropriately
Nate Begemand2030e62006-08-26 15:46:34 +0000399 // We should probably just update these in BufferSymbolAndStringTable since
400 // thats where we're partitioning up the different kinds of symbols.
Bill Wendling203d3e42007-01-17 22:22:31 +0000401 FHOut.outword(DySymTab.cmd);
402 FHOut.outword(DySymTab.cmdsize);
403 FHOut.outword(DySymTab.ilocalsym);
404 FHOut.outword(DySymTab.nlocalsym);
405 FHOut.outword(DySymTab.iextdefsym);
406 FHOut.outword(DySymTab.nextdefsym);
407 FHOut.outword(DySymTab.iundefsym);
408 FHOut.outword(DySymTab.nundefsym);
409 FHOut.outword(DySymTab.tocoff);
410 FHOut.outword(DySymTab.ntoc);
411 FHOut.outword(DySymTab.modtaboff);
412 FHOut.outword(DySymTab.nmodtab);
413 FHOut.outword(DySymTab.extrefsymoff);
414 FHOut.outword(DySymTab.nextrefsyms);
415 FHOut.outword(DySymTab.indirectsymoff);
416 FHOut.outword(DySymTab.nindirectsyms);
417 FHOut.outword(DySymTab.extreloff);
418 FHOut.outword(DySymTab.nextrel);
419 FHOut.outword(DySymTab.locreloff);
420 FHOut.outword(DySymTab.nlocrel);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000421
Nate Begemaneb883af2006-08-23 21:08:52 +0000422 O.write((char*)&FH[0], FH.size());
423}
424
425/// EmitSections - Now that we have constructed the file header and load
426/// commands, emit the data for each section to the file.
427void MachOWriter::EmitSections() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000428 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000429 E = SectionList.end(); I != E; ++I)
430 // Emit the contents of each section
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000431 if ((*I)->size())
432 O.write((char*)&(*I)->getData()[0], (*I)->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000433}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000434
435/// EmitRelocations - emit relocation data from buffer.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000436void MachOWriter::EmitRelocations() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000437 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000438 E = SectionList.end(); I != E; ++I)
439 // Emit the relocation entry data for each section.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000440 if ((*I)->RelocBuffer.size())
441 O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
Nate Begemaneb883af2006-08-23 21:08:52 +0000442}
443
Nate Begemand2030e62006-08-26 15:46:34 +0000444/// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
445/// each a string table index so that they appear in the correct order in the
446/// output file.
447void MachOWriter::BufferSymbolAndStringTable() {
448 // The order of the symbol table is:
449 // 1. local symbols
450 // 2. defined external symbols (sorted by name)
451 // 3. undefined external symbols (sorted by name)
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000452
Nate Begemanfec910c2007-02-28 07:40:50 +0000453 // Before sorting the symbols, check the PendingGlobals for any undefined
454 // globals that need to be put in the symbol table.
455 for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(),
456 E = PendingGlobals.end(); I != E; ++I) {
457 if (GVOffset[*I] == 0 && GVSection[*I] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000458 MachOSym UndfSym(*I, Mang->getMangledName(*I), MachOSym::NO_SECT, MAI);
Nate Begemanfec910c2007-02-28 07:40:50 +0000459 SymbolTable.push_back(UndfSym);
460 GVOffset[*I] = -1;
461 }
462 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000463
Nate Begemand2030e62006-08-26 15:46:34 +0000464 // Sort the symbols by name, so that when we partition the symbols by scope
465 // of definition, we won't have to sort by name within each partition.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000466 std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp());
467
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000468 // Parition the symbol table entries so that all local symbols come before
Nate Begemand2030e62006-08-26 15:46:34 +0000469 // all symbols with external linkage. { 1 | 2 3 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000470 std::partition(SymbolTable.begin(), SymbolTable.end(),
471 MachOSym::PartitionByLocal);
472
Nate Begemand2030e62006-08-26 15:46:34 +0000473 // Advance iterator to beginning of external symbols and partition so that
474 // all external symbols defined in this module come before all external
475 // symbols defined elsewhere. { 1 | 2 | 3 }
476 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
477 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000478 if (!MachOSym::PartitionByLocal(*I)) {
479 std::partition(I, E, MachOSym::PartitionByDefined);
Nate Begemand2030e62006-08-26 15:46:34 +0000480 break;
481 }
482 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000483
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000484 // Calculate the starting index for each of the local, extern defined, and
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000485 // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB
486 // load command.
487 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
488 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000489 if (MachOSym::PartitionByLocal(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000490 ++DySymTab.nlocalsym;
491 ++DySymTab.iextdefsym;
Nate Begeman6635f352007-01-26 22:39:48 +0000492 ++DySymTab.iundefsym;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000493 } else if (MachOSym::PartitionByDefined(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000494 ++DySymTab.nextdefsym;
495 ++DySymTab.iundefsym;
496 } else {
497 ++DySymTab.nundefsym;
498 }
499 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000500
Nate Begemaneb883af2006-08-23 21:08:52 +0000501 // Write out a leading zero byte when emitting string table, for n_strx == 0
502 // which means an empty string.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000503 OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000504 StrTOut.outbyte(0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000505
Nate Begemand2030e62006-08-26 15:46:34 +0000506 // The order of the string table is:
507 // 1. strings for external symbols
508 // 2. strings for local symbols
509 // Since this is the opposite order from the symbol table, which we have just
510 // sorted, we can walk the symbol table backwards to output the string table.
511 for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(),
512 E = SymbolTable.rend(); I != E; ++I) {
513 if (I->GVName == "") {
514 I->n_strx = 0;
515 } else {
516 I->n_strx = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000517 StrTOut.outstring(I->GVName, I->GVName.length()+1);
Nate Begemand2030e62006-08-26 15:46:34 +0000518 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000519 }
Nate Begemand2030e62006-08-26 15:46:34 +0000520
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000521 OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000522
Nate Begemanfec910c2007-02-28 07:40:50 +0000523 unsigned index = 0;
Nate Begemand2030e62006-08-26 15:46:34 +0000524 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
Nate Begemanfec910c2007-02-28 07:40:50 +0000525 E = SymbolTable.end(); I != E; ++I, ++index) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000526 // Add the section base address to the section offset in the n_value field
527 // to calculate the full address.
528 // FIXME: handle symbols where the n_value field is not the address
529 GlobalValue *GV = const_cast<GlobalValue*>(I->GV);
530 if (GV && GVSection[GV])
531 I->n_value += GVSection[GV]->addr;
Nate Begemanfec910c2007-02-28 07:40:50 +0000532 if (GV && (GVOffset[GV] == -1))
533 GVOffset[GV] = index;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000534
Nate Begemand2030e62006-08-26 15:46:34 +0000535 // Emit nlist to buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000536 SymTOut.outword(I->n_strx);
537 SymTOut.outbyte(I->n_type);
538 SymTOut.outbyte(I->n_sect);
539 SymTOut.outhalf(I->n_desc);
540 SymTOut.outaddr(I->n_value);
Nate Begemand2030e62006-08-26 15:46:34 +0000541 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000542}
Nate Begeman94be2482006-09-08 22:42:09 +0000543
Nate Begeman019f8512006-09-10 23:03:44 +0000544/// CalculateRelocations - For each MachineRelocation in the current section,
545/// calculate the index of the section containing the object to be relocated,
546/// and the offset into that section. From this information, create the
547/// appropriate target-specific MachORelocation type and add buffer it to be
548/// written out after we are finished writing out sections.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000549void MachOWriter::CalculateRelocations(MachOSection &MOS) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000550 std::vector<MachineRelocation> Relocations = MOS.getRelocations();
551 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
552 MachineRelocation &MR = Relocations[i];
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000553 unsigned TargetSection = MR.getConstantVal();
Nate Begemanaf806382007-03-03 06:18:18 +0000554 unsigned TargetAddr = 0;
555 unsigned TargetIndex = 0;
Nate Begeman6635f352007-01-26 22:39:48 +0000556
557 // This is a scattered relocation entry if it points to a global value with
558 // a non-zero offset.
559 bool Scattered = false;
Nate Begemanfec910c2007-02-28 07:40:50 +0000560 bool Extern = false;
Nate Begemanaf806382007-03-03 06:18:18 +0000561
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000562 // Since we may not have seen the GlobalValue we were interested in yet at
563 // the time we emitted the relocation for it, fix it up now so that it
564 // points to the offset into the correct section.
565 if (MR.isGlobalValue()) {
566 GlobalValue *GV = MR.getGlobalValue();
567 MachOSection *MOSPtr = GVSection[GV];
Nate Begeman6635f352007-01-26 22:39:48 +0000568 intptr_t Offset = GVOffset[GV];
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000569
Nate Begemanfec910c2007-02-28 07:40:50 +0000570 // If we have never seen the global before, it must be to a symbol
571 // defined in another module (N_UNDF).
Nate Begeman1257c852007-01-29 21:20:42 +0000572 if (!MOSPtr) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000573 // FIXME: need to append stub suffix
574 Extern = true;
575 TargetAddr = 0;
576 TargetIndex = GVOffset[GV];
577 } else {
578 Scattered = TargetSection != 0;
579 TargetSection = MOSPtr->Index;
Nate Begemanaf806382007-03-03 06:18:18 +0000580 }
581 MR.setResultPointer((void*)Offset);
582 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000583
Nate Begemanaf806382007-03-03 06:18:18 +0000584 // If the symbol is locally defined, pass in the address of the section and
585 // the section index to the code which will generate the target relocation.
586 if (!Extern) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000587 MachOSection &To = *SectionList[TargetSection - 1];
588 TargetAddr = To.addr;
589 TargetIndex = To.Index;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000590 }
Bill Wendling886b4122007-02-03 02:39:40 +0000591
592 OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000593 OutputBuffer SecOut(MOS.getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000594
Nate Begemanfec910c2007-02-28 07:40:50 +0000595 MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex,
596 RelocOut, SecOut, Scattered, Extern);
Nate Begeman019f8512006-09-10 23:03:44 +0000597 }
Nate Begeman019f8512006-09-10 23:03:44 +0000598}
599
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000600// InitMem - Write the value of a Constant to the specified memory location,
601// converting it into bytes and relocations.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000602void MachOWriter::InitMem(const Constant *C, uintptr_t Offset,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000603 const TargetData *TD, MachOSection* mos) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000604 typedef std::pair<const Constant*, intptr_t> CPair;
605 std::vector<CPair> WorkList;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000606 uint8_t *Addr = &mos->getData()[0];
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000607
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000608 WorkList.push_back(CPair(C,(intptr_t)Addr + Offset));
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000609
Nate Begeman6635f352007-01-26 22:39:48 +0000610 intptr_t ScatteredOffset = 0;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000611
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000612 while (!WorkList.empty()) {
613 const Constant *PC = WorkList.back().first;
614 intptr_t PA = WorkList.back().second;
615 WorkList.pop_back();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000616
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000617 if (isa<UndefValue>(PC)) {
618 continue;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000619 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000620 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000621 TD->getTypeAllocSize(CP->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000622 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
623 WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize));
624 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) {
625 //
626 // FIXME: Handle ConstantExpression. See EE::getConstantValue()
627 //
628 switch (CE->getOpcode()) {
Nate Begeman6635f352007-01-26 22:39:48 +0000629 case Instruction::GetElementPtr: {
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000630 SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
Nate Begeman6635f352007-01-26 22:39:48 +0000631 ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(),
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000632 &Indices[0], Indices.size());
Nate Begeman6635f352007-01-26 22:39:48 +0000633 WorkList.push_back(CPair(CE->getOperand(0), PA));
634 break;
635 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000636 case Instruction::Add:
637 default:
David Greene7d30d622010-01-04 23:14:46 +0000638 dbgs() << "ConstantExpr not handled as global var init: " << *CE <<"\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000639 llvm_unreachable(0);
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000640 }
Dan Gohman399101a2008-05-23 00:17:26 +0000641 } else if (PC->getType()->isSingleValueType()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000642 unsigned char *ptr = (unsigned char *)PA;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000643 switch (PC->getType()->getTypeID()) {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000644 case Type::IntegerTyID: {
645 unsigned NumBits = cast<IntegerType>(PC->getType())->getBitWidth();
646 uint64_t val = cast<ConstantInt>(PC)->getZExtValue();
647 if (NumBits <= 8)
648 ptr[0] = val;
649 else if (NumBits <= 16) {
650 if (TD->isBigEndian())
651 val = ByteSwap_16(val);
652 ptr[0] = val;
653 ptr[1] = val >> 8;
654 } else if (NumBits <= 32) {
655 if (TD->isBigEndian())
656 val = ByteSwap_32(val);
657 ptr[0] = val;
658 ptr[1] = val >> 8;
659 ptr[2] = val >> 16;
660 ptr[3] = val >> 24;
661 } else if (NumBits <= 64) {
662 if (TD->isBigEndian())
663 val = ByteSwap_64(val);
664 ptr[0] = val;
665 ptr[1] = val >> 8;
666 ptr[2] = val >> 16;
667 ptr[3] = val >> 24;
668 ptr[4] = val >> 32;
669 ptr[5] = val >> 40;
670 ptr[6] = val >> 48;
671 ptr[7] = val >> 56;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000672 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000673 llvm_unreachable("Not implemented: bit widths > 64");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000674 }
Reid Spencera54b7cb2007-01-12 07:05:14 +0000675 break;
676 }
677 case Type::FloatTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000678 uint32_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000679 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000680 if (TD->isBigEndian())
681 val = ByteSwap_32(val);
682 ptr[0] = val;
683 ptr[1] = val >> 8;
684 ptr[2] = val >> 16;
685 ptr[3] = val >> 24;
686 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000687 }
688 case Type::DoubleTyID: {
Dale Johannesen7111b022008-10-09 18:53:47 +0000689 uint64_t val = cast<ConstantFP>(PC)->getValueAPF().bitcastToAPInt().
Dale Johannesen9d5f4562007-09-12 03:30:33 +0000690 getZExtValue();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000691 if (TD->isBigEndian())
692 val = ByteSwap_64(val);
693 ptr[0] = val;
694 ptr[1] = val >> 8;
695 ptr[2] = val >> 16;
696 ptr[3] = val >> 24;
697 ptr[4] = val >> 32;
698 ptr[5] = val >> 40;
699 ptr[6] = val >> 48;
700 ptr[7] = val >> 56;
701 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +0000702 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000703 case Type::PointerTyID:
Nate Begeman6635f352007-01-26 22:39:48 +0000704 if (isa<ConstantPointerNull>(PC))
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000705 memset(ptr, 0, TD->getPointerSize());
Nate Begeman6635f352007-01-26 22:39:48 +0000706 else if (const GlobalValue* GV = dyn_cast<GlobalValue>(PC)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000707 // FIXME: what about function stubs?
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000708 mos->addRelocation(MachineRelocation::getGV(PA-(intptr_t)Addr,
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000709 MachineRelocation::VANILLA,
Nate Begeman6635f352007-01-26 22:39:48 +0000710 const_cast<GlobalValue*>(GV),
711 ScatteredOffset));
712 ScatteredOffset = 0;
713 } else
Torok Edwinc23197a2009-07-14 16:55:14 +0000714 llvm_unreachable("Unknown constant pointer type!");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000715 break;
716 default:
Torok Edwin7d696d82009-07-11 13:10:19 +0000717 std::string msg;
718 raw_string_ostream Msg(msg);
719 Msg << "ERROR: Constant unimp for type: " << *PC->getType();
720 llvm_report_error(Msg.str());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000721 }
722 } else if (isa<ConstantAggregateZero>(PC)) {
Duncan Sands777d2302009-05-09 07:06:46 +0000723 memset((void*)PA, 0, (size_t)TD->getTypeAllocSize(PC->getType()));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000724 } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000725 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000726 TD->getTypeAllocSize(CPA->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000727 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
728 WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize));
729 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) {
730 const StructLayout *SL =
731 TD->getStructLayout(cast<StructType>(CPS->getType()));
732 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattnerb1919e22007-02-10 19:55:17 +0000733 WorkList.push_back(CPair(CPS->getOperand(i),
734 PA+SL->getElementOffset(i)));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000735 } else {
David Greene7d30d622010-01-04 23:14:46 +0000736 dbgs() << "Bad Type: " << *PC->getType() << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +0000737 llvm_unreachable("Unknown constant type to initialize memory with!");
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000738 }
739 }
740}
741
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000742//===----------------------------------------------------------------------===//
743// MachOSym Implementation
744//===----------------------------------------------------------------------===//
745
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000746MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
Chris Lattner33adcfb2009-08-22 21:43:10 +0000747 const MCAsmInfo *MAI) :
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000748 GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect),
749 n_desc(0), n_value(0) {
750
Chris Lattner90f8b702009-07-21 17:30:51 +0000751 // FIXME: This is completely broken, it should use the mangler interface.
Nate Begeman94be2482006-09-08 22:42:09 +0000752 switch (GV->getLinkage()) {
753 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000754 llvm_unreachable("Unexpected linkage type!");
Nate Begeman94be2482006-09-08 22:42:09 +0000755 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000756 case GlobalValue::WeakAnyLinkage:
757 case GlobalValue::WeakODRLinkage:
758 case GlobalValue::LinkOnceAnyLinkage:
759 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000760 case GlobalValue::CommonLinkage:
Nate Begeman94be2482006-09-08 22:42:09 +0000761 assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
762 case GlobalValue::ExternalLinkage:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000763 GVName = MAI->getGlobalPrefix() + name;
Nate Begeman6635f352007-01-26 22:39:48 +0000764 n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT;
Nate Begeman94be2482006-09-08 22:42:09 +0000765 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000766 case GlobalValue::PrivateLinkage:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000767 GVName = MAI->getPrivateGlobalPrefix() + name;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000768 break;
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000769 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000770 GVName = MAI->getLinkerPrivateGlobalPrefix() + name;
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000771 break;
Nate Begeman94be2482006-09-08 22:42:09 +0000772 case GlobalValue::InternalLinkage:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000773 GVName = MAI->getGlobalPrefix() + name;
Nate Begeman94be2482006-09-08 22:42:09 +0000774 break;
775 }
776}
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000777
778} // end namespace llvm