blob: 3ad03a4eee1923e4ccf309e516e483203bc6520a [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"
Owen Andersoncb371882008-08-21 00:14:44 +000038#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000039
40namespace llvm {
Nate Begemaneb883af2006-08-23 21:08:52 +000041
Bill Wendling8f84f1f2007-02-08 01:35:27 +000042/// AddMachOWriter - Concrete function to add the Mach-O writer to the function
43/// pass manager.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000044ObjectCodeEmitter *AddMachOWriter(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000045 raw_ostream &O,
Bill Wendling8f84f1f2007-02-08 01:35:27 +000046 TargetMachine &TM) {
47 MachOWriter *MOW = new MachOWriter(O, TM);
Dan Gohmanbfae8312008-03-11 22:29:46 +000048 PM.add(MOW);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000049 return MOW->getObjectCodeEmitter();
Bill Wendling8f84f1f2007-02-08 01:35:27 +000050}
51
Nate Begemaneb883af2006-08-23 21:08:52 +000052//===----------------------------------------------------------------------===//
Nate Begemaneb883af2006-08-23 21:08:52 +000053// MachOWriter Implementation
54//===----------------------------------------------------------------------===//
55
Devang Patel19974732007-05-03 01:11:54 +000056char MachOWriter::ID = 0;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000057
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000058MachOWriter::MachOWriter(raw_ostream &o, TargetMachine &tm)
59 : MachineFunctionPass(&ID), O(o), TM(tm) {
Nate Begemaneb883af2006-08-23 21:08:52 +000060 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
61 isLittleEndian = TM.getTargetData()->isLittleEndian();
62
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000063 TAI = TM.getTargetAsmInfo();
64
Nate Begemaneb883af2006-08-23 21:08:52 +000065 // Create the machine code emitter object for this target.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000066 MachOCE = new MachOCodeEmitter(*this, *getTextSection(true));
Nate Begemaneb883af2006-08-23 21:08:52 +000067}
68
69MachOWriter::~MachOWriter() {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000070 delete MachOCE;
Nate Begemaneb883af2006-08-23 21:08:52 +000071}
72
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000073bool MachOWriter::doInitialization(Module &M) {
74 // Set the magic value, now that we know the pointer size and endianness
75 Header.setMagic(isLittleEndian, is64Bit);
76
77 // Set the file type
78 // FIXME: this only works for object files, we do not support the creation
79 // of dynamic libraries or executables at this time.
80 Header.filetype = MachOHeader::MH_OBJECT;
81
82 Mang = new Mangler(M);
83 return false;
84}
85
86bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
87 return false;
88}
89
90/// doFinalization - Now that the module has been completely processed, emit
91/// the Mach-O file to 'O'.
92bool MachOWriter::doFinalization(Module &M) {
93 // FIXME: we don't handle debug info yet, we should probably do that.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +000094 // Okay, the.text section has been completed, build the .data, .bss, and
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000095 // "common" sections next.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000096
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +000097 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
98 I != E; ++I)
99 EmitGlobal(I);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000100
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000101 // Emit the header and load commands.
102 EmitHeaderAndLoadCommands();
103
104 // Emit the various sections and their relocation info.
105 EmitSections();
106 EmitRelocations();
107
108 // Write the symbol table and the string table to the end of the file.
109 O.write((char*)&SymT[0], SymT.size());
110 O.write((char*)&StrT[0], StrT.size());
111
112 // We are done with the abstract symbols.
113 SectionList.clear();
114 SymbolTable.clear();
115 DynamicSymbolTable.clear();
116
117 // Release the name mangler object.
118 delete Mang; Mang = 0;
119 return false;
120}
121
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000122// getConstSection - Get constant section for Constant 'C'
123MachOSection *MachOWriter::getConstSection(Constant *C) {
124 const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
125 if (CVA && CVA->isCString())
126 return getSection("__TEXT", "__cstring",
127 MachOSection::S_CSTRING_LITERALS);
128
129 const Type *Ty = C->getType();
130 if (Ty->isPrimitiveType() || Ty->isInteger()) {
131 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
132 switch(Size) {
133 default: break; // Fall through to __TEXT,__const
134 case 4:
135 return getSection("__TEXT", "__literal4",
136 MachOSection::S_4BYTE_LITERALS);
137 case 8:
138 return getSection("__TEXT", "__literal8",
139 MachOSection::S_8BYTE_LITERALS);
140 case 16:
141 return getSection("__TEXT", "__literal16",
142 MachOSection::S_16BYTE_LITERALS);
143 }
144 }
145 return getSection("__TEXT", "__const");
146}
147
148// getJumpTableSection - Select the Jump Table section
149MachOSection *MachOWriter::getJumpTableSection() {
150 if (TM.getRelocationModel() == Reloc::PIC_)
151 return getTextSection(false);
152 else
153 return getSection("__TEXT", "__const");
154}
155
156// getSection - Return the section with the specified name, creating a new
157// section if one does not already exist.
158MachOSection *MachOWriter::getSection(const std::string &seg,
159 const std::string &sect,
160 unsigned Flags /* = 0 */ ) {
161 MachOSection *MOS = SectionLookup[seg+sect];
162 if (MOS) return MOS;
163
164 MOS = new MachOSection(seg, sect);
165 SectionList.push_back(MOS);
166 MOS->Index = SectionList.size();
167 MOS->flags = MachOSection::S_REGULAR | Flags;
168 SectionLookup[seg+sect] = MOS;
169 return MOS;
170}
171
172// getTextSection - Return text section with different flags for code/data
173MachOSection *MachOWriter::getTextSection(bool isCode /* = true */ ) {
174 if (isCode)
175 return getSection("__TEXT", "__text",
176 MachOSection::S_ATTR_PURE_INSTRUCTIONS |
177 MachOSection::S_ATTR_SOME_INSTRUCTIONS);
178 else
179 return getSection("__TEXT", "__text");
180}
181
182MachOSection *MachOWriter::getBSSSection() {
183 return getSection("__DATA", "__bss", MachOSection::S_ZEROFILL);
184}
185
186// GetJTRelocation - Get a relocation a new BB relocation based
187// on target information.
188MachineRelocation MachOWriter::GetJTRelocation(unsigned Offset,
189 MachineBasicBlock *MBB) const {
190 return TM.getMachOWriterInfo()->GetJTRelocation(Offset, MBB);
191}
192
193// GetTargetRelocation - Returns the number of relocations.
194unsigned MachOWriter::GetTargetRelocation(MachineRelocation &MR,
195 unsigned FromIdx, unsigned ToAddr,
196 unsigned ToIndex, OutputBuffer &RelocOut,
197 OutputBuffer &SecOut, bool Scattered,
198 bool Extern) {
199 return TM.getMachOWriterInfo()->GetTargetRelocation(MR, FromIdx, ToAddr,
200 ToIndex, RelocOut,
201 SecOut, Scattered,
202 Extern);
203}
204
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000205void MachOWriter::AddSymbolToSection(MachOSection *Sec, GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000206 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000207 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Duncan Sandsd1025932008-01-29 06:23:44 +0000208 unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
209
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000210 // Reserve space in the .bss section for this symbol while maintaining the
211 // desired section alignment, which must be at least as much as required by
212 // this symbol.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000213 OutputBuffer SecDataOut(Sec->getData(), is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000214
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000215 if (Align) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000216 Align = Log2_32(Align);
217 Sec->align = std::max(unsigned(Sec->align), Align);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000218
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000219 Sec->emitAlignment(Sec->align);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000220 }
Nate Begemanfec910c2007-02-28 07:40:50 +0000221 // Globals without external linkage apparently do not go in the symbol table.
Rafael Espindolabb46f522009-01-15 20:18:42 +0000222 if (!GV->hasLocalLinkage()) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000223 MachOSym Sym(GV, Mang->getValueName(GV), Sec->Index, TAI);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000224 Sym.n_value = Sec->size();
Nate Begemanfec910c2007-02-28 07:40:50 +0000225 SymbolTable.push_back(Sym);
226 }
227
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000228 // Record the offset of the symbol, and then allocate space for it.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000229 // FIXME: remove when we have unified size + output buffer
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000230
231 // Now that we know what section the GlovalVariable is going to be emitted
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000232 // into, update our mappings.
233 // FIXME: We may also need to update this when outputting non-GlobalVariable
234 // GlobalValues such as functions.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000235
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000236 GVSection[GV] = Sec;
237 GVOffset[GV] = Sec->size();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000238
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000239 // Allocate space in the section for the global.
240 for (unsigned i = 0; i < Size; ++i)
Bill Wendling203d3e42007-01-17 22:22:31 +0000241 SecDataOut.outbyte(0);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000242}
243
Nate Begemaneb883af2006-08-23 21:08:52 +0000244void MachOWriter::EmitGlobal(GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000245 const Type *Ty = GV->getType()->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000246 unsigned Size = TM.getTargetData()->getTypeAllocSize(Ty);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000247 bool NoInit = !GV->hasInitializer();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000248
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000249 // If this global has a zero initializer, it is part of the .bss or common
250 // section.
251 if (NoInit || GV->getInitializer()->isNullValue()) {
252 // If this global is part of the common block, add it now. Variables are
253 // part of the common block if they are zero initialized and allowed to be
254 // merged with other symbols.
Dale Johannesenaafce772008-05-14 20:12:51 +0000255 if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
256 GV->hasCommonLinkage()) {
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000257 MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV),
258 MachOSym::NO_SECT, TAI);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000259 // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
260 // bytes of the symbol.
261 ExtOrCommonSym.n_value = Size;
Nate Begemanfec910c2007-02-28 07:40:50 +0000262 SymbolTable.push_back(ExtOrCommonSym);
263 // Remember that we've seen this symbol
264 GVOffset[GV] = Size;
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000265 return;
266 }
267 // Otherwise, this symbol is part of the .bss section.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000268 MachOSection *BSS = getBSSSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000269 AddSymbolToSection(BSS, GV);
270 return;
271 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000272
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000273 // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
274 // 16 bytes, or a cstring. Other read only data goes into a regular const
275 // section. Read-write data goes in the data section.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000276 MachOSection *Sec = GV->isConstant() ? getConstSection(GV->getInitializer()) :
Nate Begeman1257c852007-01-29 21:20:42 +0000277 getDataSection();
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000278 AddSymbolToSection(Sec, GV);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000279 InitMem(GV->getInitializer(), GVOffset[GV], TM.getTargetData(), Sec);
Nate Begemaneb883af2006-08-23 21:08:52 +0000280}
281
282
Nate Begemaneb883af2006-08-23 21:08:52 +0000283
284void MachOWriter::EmitHeaderAndLoadCommands() {
285 // Step #0: Fill in the segment load command size, since we need it to figure
286 // out the rest of the header fields
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000287
Nate Begemaneb883af2006-08-23 21:08:52 +0000288 MachOSegment SEG("", is64Bit);
289 SEG.nsects = SectionList.size();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000290 SEG.cmdsize = SEG.cmdSize(is64Bit) +
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000291 SEG.nsects * SectionList[0]->cmdSize(is64Bit);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000292
Nate Begemaneb883af2006-08-23 21:08:52 +0000293 // Step #1: calculate the number of load commands. We always have at least
294 // one, for the LC_SEGMENT load command, plus two for the normal
295 // and dynamic symbol tables, if there are any symbols.
296 Header.ncmds = SymbolTable.empty() ? 1 : 3;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000297
Nate Begemaneb883af2006-08-23 21:08:52 +0000298 // Step #2: calculate the size of the load commands
299 Header.sizeofcmds = SEG.cmdsize;
300 if (!SymbolTable.empty())
301 Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000302
Nate Begemaneb883af2006-08-23 21:08:52 +0000303 // Step #3: write the header to the file
304 // Local alias to shortenify coming code.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000305 std::vector<unsigned char> &FH = Header.HeaderData;
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000306 OutputBuffer FHOut(FH, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000307
308 FHOut.outword(Header.magic);
Bill Wendling2b721822007-01-24 07:13:56 +0000309 FHOut.outword(TM.getMachOWriterInfo()->getCPUType());
310 FHOut.outword(TM.getMachOWriterInfo()->getCPUSubType());
Bill Wendling203d3e42007-01-17 22:22:31 +0000311 FHOut.outword(Header.filetype);
312 FHOut.outword(Header.ncmds);
313 FHOut.outword(Header.sizeofcmds);
314 FHOut.outword(Header.flags);
Nate Begemaneb883af2006-08-23 21:08:52 +0000315 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000316 FHOut.outword(Header.reserved);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000317
Nate Begemaneb883af2006-08-23 21:08:52 +0000318 // Step #4: Finish filling in the segment load command and write it out
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000319 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000320 E = SectionList.end(); I != E; ++I)
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000321 SEG.filesize += (*I)->size();
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000322
Nate Begemaneb883af2006-08-23 21:08:52 +0000323 SEG.vmsize = SEG.filesize;
324 SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000325
Bill Wendling203d3e42007-01-17 22:22:31 +0000326 FHOut.outword(SEG.cmd);
327 FHOut.outword(SEG.cmdsize);
328 FHOut.outstring(SEG.segname, 16);
329 FHOut.outaddr(SEG.vmaddr);
330 FHOut.outaddr(SEG.vmsize);
331 FHOut.outaddr(SEG.fileoff);
332 FHOut.outaddr(SEG.filesize);
333 FHOut.outword(SEG.maxprot);
334 FHOut.outword(SEG.initprot);
335 FHOut.outword(SEG.nsects);
336 FHOut.outword(SEG.flags);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000337
338 // Step #5: Finish filling in the fields of the MachOSections
Nate Begeman94be2482006-09-08 22:42:09 +0000339 uint64_t currentAddr = 0;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000340 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begemaneb883af2006-08-23 21:08:52 +0000341 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000342 MachOSection *MOS = *I;
343 MOS->addr = currentAddr;
344 MOS->offset = currentAddr + SEG.fileoff;
Nate Begeman94be2482006-09-08 22:42:09 +0000345 // FIXME: do we need to do something with alignment here?
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000346 currentAddr += MOS->size();
Nate Begeman94be2482006-09-08 22:42:09 +0000347 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000348
Nate Begemanfec910c2007-02-28 07:40:50 +0000349 // Step #6: Emit the symbol table to temporary buffers, so that we know the
350 // size of the string table when we write the next load command. This also
351 // sorts and assigns indices to each of the symbols, which is necessary for
352 // emitting relocations to externally-defined objects.
353 BufferSymbolAndStringTable();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000354
Nate Begemanfec910c2007-02-28 07:40:50 +0000355 // Step #7: Calculate the number of relocations for each section and write out
Nate Begeman94be2482006-09-08 22:42:09 +0000356 // the section commands for each section
357 currentAddr += SEG.fileoff;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000358 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman94be2482006-09-08 22:42:09 +0000359 E = SectionList.end(); I != E; ++I) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000360 MachOSection *MOS = *I;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000361
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000362 // Convert the relocations to target-specific relocations, and fill in the
363 // relocation offset for this section.
364 CalculateRelocations(*MOS);
365 MOS->reloff = MOS->nreloc ? currentAddr : 0;
366 currentAddr += MOS->nreloc * 8;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000367
Nate Begeman94be2482006-09-08 22:42:09 +0000368 // write the finalized section command to the output buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000369 FHOut.outstring(MOS->sectname, 16);
370 FHOut.outstring(MOS->segname, 16);
371 FHOut.outaddr(MOS->addr);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000372 FHOut.outaddr(MOS->size());
Bill Wendling203d3e42007-01-17 22:22:31 +0000373 FHOut.outword(MOS->offset);
374 FHOut.outword(MOS->align);
375 FHOut.outword(MOS->reloff);
376 FHOut.outword(MOS->nreloc);
377 FHOut.outword(MOS->flags);
378 FHOut.outword(MOS->reserved1);
379 FHOut.outword(MOS->reserved2);
Nate Begemaneb883af2006-08-23 21:08:52 +0000380 if (is64Bit)
Bill Wendling203d3e42007-01-17 22:22:31 +0000381 FHOut.outword(MOS->reserved3);
Nate Begemaneb883af2006-08-23 21:08:52 +0000382 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000383
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000384 // Step #8: Emit LC_SYMTAB/LC_DYSYMTAB load commands
Nate Begeman94be2482006-09-08 22:42:09 +0000385 SymTab.symoff = currentAddr;
Nate Begemaneb883af2006-08-23 21:08:52 +0000386 SymTab.nsyms = SymbolTable.size();
Nate Begemand2030e62006-08-26 15:46:34 +0000387 SymTab.stroff = SymTab.symoff + SymT.size();
388 SymTab.strsize = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000389 FHOut.outword(SymTab.cmd);
390 FHOut.outword(SymTab.cmdsize);
391 FHOut.outword(SymTab.symoff);
392 FHOut.outword(SymTab.nsyms);
393 FHOut.outword(SymTab.stroff);
394 FHOut.outword(SymTab.strsize);
Nate Begemaneb883af2006-08-23 21:08:52 +0000395
396 // FIXME: set DySymTab fields appropriately
Nate Begemand2030e62006-08-26 15:46:34 +0000397 // We should probably just update these in BufferSymbolAndStringTable since
398 // thats where we're partitioning up the different kinds of symbols.
Bill Wendling203d3e42007-01-17 22:22:31 +0000399 FHOut.outword(DySymTab.cmd);
400 FHOut.outword(DySymTab.cmdsize);
401 FHOut.outword(DySymTab.ilocalsym);
402 FHOut.outword(DySymTab.nlocalsym);
403 FHOut.outword(DySymTab.iextdefsym);
404 FHOut.outword(DySymTab.nextdefsym);
405 FHOut.outword(DySymTab.iundefsym);
406 FHOut.outword(DySymTab.nundefsym);
407 FHOut.outword(DySymTab.tocoff);
408 FHOut.outword(DySymTab.ntoc);
409 FHOut.outword(DySymTab.modtaboff);
410 FHOut.outword(DySymTab.nmodtab);
411 FHOut.outword(DySymTab.extrefsymoff);
412 FHOut.outword(DySymTab.nextrefsyms);
413 FHOut.outword(DySymTab.indirectsymoff);
414 FHOut.outword(DySymTab.nindirectsyms);
415 FHOut.outword(DySymTab.extreloff);
416 FHOut.outword(DySymTab.nextrel);
417 FHOut.outword(DySymTab.locreloff);
418 FHOut.outword(DySymTab.nlocrel);
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000419
Nate Begemaneb883af2006-08-23 21:08:52 +0000420 O.write((char*)&FH[0], FH.size());
421}
422
423/// EmitSections - Now that we have constructed the file header and load
424/// commands, emit the data for each section to the file.
425void MachOWriter::EmitSections() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000426 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000427 E = SectionList.end(); I != E; ++I)
428 // Emit the contents of each section
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000429 if ((*I)->size())
430 O.write((char*)&(*I)->getData()[0], (*I)->size());
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000431}
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000432
433/// EmitRelocations - emit relocation data from buffer.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000434void MachOWriter::EmitRelocations() {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000435 for (std::vector<MachOSection*>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000436 E = SectionList.end(); I != E; ++I)
437 // Emit the relocation entry data for each section.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000438 if ((*I)->RelocBuffer.size())
439 O.write((char*)&(*I)->RelocBuffer[0], (*I)->RelocBuffer.size());
Nate Begemaneb883af2006-08-23 21:08:52 +0000440}
441
Nate Begemand2030e62006-08-26 15:46:34 +0000442/// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
443/// each a string table index so that they appear in the correct order in the
444/// output file.
445void MachOWriter::BufferSymbolAndStringTable() {
446 // The order of the symbol table is:
447 // 1. local symbols
448 // 2. defined external symbols (sorted by name)
449 // 3. undefined external symbols (sorted by name)
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000450
Nate Begemanfec910c2007-02-28 07:40:50 +0000451 // Before sorting the symbols, check the PendingGlobals for any undefined
452 // globals that need to be put in the symbol table.
453 for (std::vector<GlobalValue*>::iterator I = PendingGlobals.begin(),
454 E = PendingGlobals.end(); I != E; ++I) {
455 if (GVOffset[*I] == 0 && GVSection[*I] == 0) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000456 MachOSym UndfSym(*I, Mang->getValueName(*I), MachOSym::NO_SECT, TAI);
Nate Begemanfec910c2007-02-28 07:40:50 +0000457 SymbolTable.push_back(UndfSym);
458 GVOffset[*I] = -1;
459 }
460 }
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000461
Nate Begemand2030e62006-08-26 15:46:34 +0000462 // Sort the symbols by name, so that when we partition the symbols by scope
463 // of definition, we won't have to sort by name within each partition.
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000464 std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSym::SymCmp());
465
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000466 // Parition the symbol table entries so that all local symbols come before
Nate Begemand2030e62006-08-26 15:46:34 +0000467 // all symbols with external linkage. { 1 | 2 3 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000468 std::partition(SymbolTable.begin(), SymbolTable.end(),
469 MachOSym::PartitionByLocal);
470
Nate Begemand2030e62006-08-26 15:46:34 +0000471 // Advance iterator to beginning of external symbols and partition so that
472 // all external symbols defined in this module come before all external
473 // symbols defined elsewhere. { 1 | 2 | 3 }
474 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
475 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000476 if (!MachOSym::PartitionByLocal(*I)) {
477 std::partition(I, E, MachOSym::PartitionByDefined);
Nate Begemand2030e62006-08-26 15:46:34 +0000478 break;
479 }
480 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000481
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000482 // Calculate the starting index for each of the local, extern defined, and
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000483 // undefined symbols, as well as the number of each to put in the LC_DYSYMTAB
484 // load command.
485 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
486 E = SymbolTable.end(); I != E; ++I) {
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000487 if (MachOSym::PartitionByLocal(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000488 ++DySymTab.nlocalsym;
489 ++DySymTab.iextdefsym;
Nate Begeman6635f352007-01-26 22:39:48 +0000490 ++DySymTab.iundefsym;
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000491 } else if (MachOSym::PartitionByDefined(*I)) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000492 ++DySymTab.nextdefsym;
493 ++DySymTab.iundefsym;
494 } else {
495 ++DySymTab.nundefsym;
496 }
497 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000498
Nate Begemaneb883af2006-08-23 21:08:52 +0000499 // Write out a leading zero byte when emitting string table, for n_strx == 0
500 // which means an empty string.
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000501 OutputBuffer StrTOut(StrT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000502 StrTOut.outbyte(0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000503
Nate Begemand2030e62006-08-26 15:46:34 +0000504 // The order of the string table is:
505 // 1. strings for external symbols
506 // 2. strings for local symbols
507 // Since this is the opposite order from the symbol table, which we have just
508 // sorted, we can walk the symbol table backwards to output the string table.
509 for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(),
510 E = SymbolTable.rend(); I != E; ++I) {
511 if (I->GVName == "") {
512 I->n_strx = 0;
513 } else {
514 I->n_strx = StrT.size();
Bill Wendling203d3e42007-01-17 22:22:31 +0000515 StrTOut.outstring(I->GVName, I->GVName.length()+1);
Nate Begemand2030e62006-08-26 15:46:34 +0000516 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000517 }
Nate Begemand2030e62006-08-26 15:46:34 +0000518
Bill Wendlingc904a5b2007-01-18 01:23:11 +0000519 OutputBuffer SymTOut(SymT, is64Bit, isLittleEndian);
Bill Wendling203d3e42007-01-17 22:22:31 +0000520
Nate Begemanfec910c2007-02-28 07:40:50 +0000521 unsigned index = 0;
Nate Begemand2030e62006-08-26 15:46:34 +0000522 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
Nate Begemanfec910c2007-02-28 07:40:50 +0000523 E = SymbolTable.end(); I != E; ++I, ++index) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000524 // Add the section base address to the section offset in the n_value field
525 // to calculate the full address.
526 // FIXME: handle symbols where the n_value field is not the address
527 GlobalValue *GV = const_cast<GlobalValue*>(I->GV);
528 if (GV && GVSection[GV])
529 I->n_value += GVSection[GV]->addr;
Nate Begemanfec910c2007-02-28 07:40:50 +0000530 if (GV && (GVOffset[GV] == -1))
531 GVOffset[GV] = index;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000532
Nate Begemand2030e62006-08-26 15:46:34 +0000533 // Emit nlist to buffer
Bill Wendling203d3e42007-01-17 22:22:31 +0000534 SymTOut.outword(I->n_strx);
535 SymTOut.outbyte(I->n_type);
536 SymTOut.outbyte(I->n_sect);
537 SymTOut.outhalf(I->n_desc);
538 SymTOut.outaddr(I->n_value);
Nate Begemand2030e62006-08-26 15:46:34 +0000539 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000540}
Nate Begeman94be2482006-09-08 22:42:09 +0000541
Nate Begeman019f8512006-09-10 23:03:44 +0000542/// CalculateRelocations - For each MachineRelocation in the current section,
543/// calculate the index of the section containing the object to be relocated,
544/// and the offset into that section. From this information, create the
545/// appropriate target-specific MachORelocation type and add buffer it to be
546/// written out after we are finished writing out sections.
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000547void MachOWriter::CalculateRelocations(MachOSection &MOS) {
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000548 std::vector<MachineRelocation> Relocations = MOS.getRelocations();
549 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
550 MachineRelocation &MR = Relocations[i];
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000551 unsigned TargetSection = MR.getConstantVal();
Nate Begemanaf806382007-03-03 06:18:18 +0000552 unsigned TargetAddr = 0;
553 unsigned TargetIndex = 0;
Nate Begeman6635f352007-01-26 22:39:48 +0000554
555 // This is a scattered relocation entry if it points to a global value with
556 // a non-zero offset.
557 bool Scattered = false;
Nate Begemanfec910c2007-02-28 07:40:50 +0000558 bool Extern = false;
Nate Begemanaf806382007-03-03 06:18:18 +0000559
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000560 // Since we may not have seen the GlobalValue we were interested in yet at
561 // the time we emitted the relocation for it, fix it up now so that it
562 // points to the offset into the correct section.
563 if (MR.isGlobalValue()) {
564 GlobalValue *GV = MR.getGlobalValue();
565 MachOSection *MOSPtr = GVSection[GV];
Nate Begeman6635f352007-01-26 22:39:48 +0000566 intptr_t Offset = GVOffset[GV];
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000567
Nate Begemanfec910c2007-02-28 07:40:50 +0000568 // If we have never seen the global before, it must be to a symbol
569 // defined in another module (N_UNDF).
Nate Begeman1257c852007-01-29 21:20:42 +0000570 if (!MOSPtr) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000571 // FIXME: need to append stub suffix
572 Extern = true;
573 TargetAddr = 0;
574 TargetIndex = GVOffset[GV];
575 } else {
576 Scattered = TargetSection != 0;
577 TargetSection = MOSPtr->Index;
Nate Begemanaf806382007-03-03 06:18:18 +0000578 }
579 MR.setResultPointer((void*)Offset);
580 }
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000581
Nate Begemanaf806382007-03-03 06:18:18 +0000582 // If the symbol is locally defined, pass in the address of the section and
583 // the section index to the code which will generate the target relocation.
584 if (!Extern) {
Nate Begemanfec910c2007-02-28 07:40:50 +0000585 MachOSection &To = *SectionList[TargetSection - 1];
586 TargetAddr = To.addr;
587 TargetIndex = To.Index;
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000588 }
Bill Wendling886b4122007-02-03 02:39:40 +0000589
590 OutputBuffer RelocOut(MOS.RelocBuffer, is64Bit, isLittleEndian);
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000591 OutputBuffer SecOut(MOS.getData(), is64Bit, isLittleEndian);
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000592
Nate Begemanfec910c2007-02-28 07:40:50 +0000593 MOS.nreloc += GetTargetRelocation(MR, MOS.Index, TargetAddr, TargetIndex,
594 RelocOut, SecOut, Scattered, Extern);
Nate Begeman019f8512006-09-10 23:03:44 +0000595 }
Nate Begeman019f8512006-09-10 23:03:44 +0000596}
597
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000598// InitMem - Write the value of a Constant to the specified memory location,
599// converting it into bytes and relocations.
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000600void MachOWriter::InitMem(const Constant *C, uintptr_t Offset,
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000601 const TargetData *TD, MachOSection* mos) {
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000602 typedef std::pair<const Constant*, intptr_t> CPair;
603 std::vector<CPair> WorkList;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000604 uint8_t *Addr = &mos->getData()[0];
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000605
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000606 WorkList.push_back(CPair(C,(intptr_t)Addr + Offset));
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000607
Nate Begeman6635f352007-01-26 22:39:48 +0000608 intptr_t ScatteredOffset = 0;
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000609
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000610 while (!WorkList.empty()) {
611 const Constant *PC = WorkList.back().first;
612 intptr_t PA = WorkList.back().second;
613 WorkList.pop_back();
Bruno Cardoso Lopes752e9282009-07-06 06:40:51 +0000614
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000615 if (isa<UndefValue>(PC)) {
616 continue;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000617 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000618 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000619 TD->getTypeAllocSize(CP->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000620 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
621 WorkList.push_back(CPair(CP->getOperand(i), PA+i*ElementSize));
622 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(PC)) {
623 //
624 // FIXME: Handle ConstantExpression. See EE::getConstantValue()
625 //
626 switch (CE->getOpcode()) {
Nate Begeman6635f352007-01-26 22:39:48 +0000627 case Instruction::GetElementPtr: {
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000628 SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
Nate Begeman6635f352007-01-26 22:39:48 +0000629 ScatteredOffset = TD->getIndexedOffset(CE->getOperand(0)->getType(),
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000630 &Indices[0], Indices.size());
Nate Begeman6635f352007-01-26 22:39:48 +0000631 WorkList.push_back(CPair(CE->getOperand(0), PA));
632 break;
633 }
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000634 case Instruction::Add:
635 default:
636 cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
637 abort();
638 break;
639 }
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 {
Reid Spencera54b7cb2007-01-12 07:05:14 +0000672 assert(0 && "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
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000713 assert(0 && "Unknown constant pointer type!");
714 break;
715 default:
716 cerr << "ERROR: Constant unimp for type: " << *PC->getType() << "\n";
717 abort();
718 }
719 } else if (isa<ConstantAggregateZero>(PC)) {
Duncan Sands777d2302009-05-09 07:06:46 +0000720 memset((void*)PA, 0, (size_t)TD->getTypeAllocSize(PC->getType()));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000721 } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(PC)) {
Duncan Sandsca0ed742007-11-05 00:04:43 +0000722 unsigned ElementSize =
Duncan Sands777d2302009-05-09 07:06:46 +0000723 TD->getTypeAllocSize(CPA->getType()->getElementType());
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000724 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
725 WorkList.push_back(CPair(CPA->getOperand(i), PA+i*ElementSize));
726 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(PC)) {
727 const StructLayout *SL =
728 TD->getStructLayout(cast<StructType>(CPS->getType()));
729 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
Chris Lattnerb1919e22007-02-10 19:55:17 +0000730 WorkList.push_back(CPair(CPS->getOperand(i),
731 PA+SL->getElementOffset(i)));
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000732 } else {
733 cerr << "Bad Type: " << *PC->getType() << "\n";
734 assert(0 && "Unknown constant type to initialize memory with!");
735 }
736 }
737}
738
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000739//===----------------------------------------------------------------------===//
740// MachOSym Implementation
741//===----------------------------------------------------------------------===//
742
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000743MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect,
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000744 const TargetAsmInfo *TAI) :
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000745 GV(gv), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT), n_sect(sect),
746 n_desc(0), n_value(0) {
747
Nate Begeman94be2482006-09-08 22:42:09 +0000748 switch (GV->getLinkage()) {
749 default:
750 assert(0 && "Unexpected linkage type!");
751 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000752 case GlobalValue::WeakAnyLinkage:
753 case GlobalValue::WeakODRLinkage:
754 case GlobalValue::LinkOnceAnyLinkage:
755 case GlobalValue::LinkOnceODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000756 case GlobalValue::CommonLinkage:
Nate Begeman94be2482006-09-08 22:42:09 +0000757 assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
758 case GlobalValue::ExternalLinkage:
Nate Begemanbfaaaa62006-12-11 02:20:45 +0000759 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman6635f352007-01-26 22:39:48 +0000760 n_type |= GV->hasHiddenVisibility() ? N_PEXT : N_EXT;
Nate Begeman94be2482006-09-08 22:42:09 +0000761 break;
Rafael Espindolabb46f522009-01-15 20:18:42 +0000762 case GlobalValue::PrivateLinkage:
763 GVName = TAI->getPrivateGlobalPrefix() + name;
764 break;
Nate Begeman94be2482006-09-08 22:42:09 +0000765 case GlobalValue::InternalLinkage:
Nate Begeman6635f352007-01-26 22:39:48 +0000766 GVName = TAI->getGlobalPrefix() + name;
Nate Begeman94be2482006-09-08 22:42:09 +0000767 break;
768 }
769}
Bruno Cardoso Lopesa321dcd2009-06-03 03:43:31 +0000770
771} // end namespace llvm
772