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