blob: 390676973f1506a4c04d8e3ea7230521aec1cc72 [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//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 "llvm/Module.h"
26#include "llvm/CodeGen/MachineCodeEmitter.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman019f8512006-09-10 23:03:44 +000028#include "llvm/CodeGen/MachineJumpTableInfo.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000029#include "llvm/CodeGen/MachOWriter.h"
Nate Begeman94be2482006-09-08 22:42:09 +000030#include "llvm/ExecutionEngine/ExecutionEngine.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000031#include "llvm/Target/TargetJITInfo.h"
Nate Begemaneb883af2006-08-23 21:08:52 +000032#include "llvm/Support/Mangler.h"
Nate Begemanf8f2c5a2006-08-25 06:36:58 +000033#include "llvm/Support/MathExtras.h"
Nate Begemand2030e62006-08-26 15:46:34 +000034#include <algorithm>
Nate Begemaneb883af2006-08-23 21:08:52 +000035using namespace llvm;
36
37//===----------------------------------------------------------------------===//
38// MachOCodeEmitter Implementation
39//===----------------------------------------------------------------------===//
40
41namespace llvm {
42 /// MachOCodeEmitter - This class is used by the MachOWriter to emit the code
43 /// for functions to the Mach-O file.
44 class MachOCodeEmitter : public MachineCodeEmitter {
45 MachOWriter &MOW;
46
47 /// MOS - The current section we're writing to
48 MachOWriter::MachOSection *MOS;
49
50 /// Relocations - These are the relocations that the function needs, as
51 /// emitted.
52 std::vector<MachineRelocation> Relocations;
Nate Begeman019f8512006-09-10 23:03:44 +000053
54 /// CPLocations - This is a map of constant pool indices to offsets from the
55 /// start of the section for that constant pool index.
56 std::vector<intptr_t> CPLocations;
57
58 /// JTLocations - This is a map of jump table indices to offsets from the
59 /// start of the section for that jump table index.
60 std::vector<intptr_t> JTLocations;
Nate Begemaneb883af2006-08-23 21:08:52 +000061
62 /// MBBLocations - This vector is a mapping from MBB ID's to their address.
63 /// It is filled in by the StartMachineBasicBlock callback and queried by
64 /// the getMachineBasicBlockAddress callback.
65 std::vector<intptr_t> MBBLocations;
66
67 public:
68 MachOCodeEmitter(MachOWriter &mow) : MOW(mow) {}
69
70 void startFunction(MachineFunction &F);
71 bool finishFunction(MachineFunction &F);
72
73 void addRelocation(const MachineRelocation &MR) {
74 Relocations.push_back(MR);
75 }
76
Nate Begeman019f8512006-09-10 23:03:44 +000077 void emitConstantPool(MachineConstantPool *MCP);
78 void emitJumpTables(MachineJumpTableInfo *MJTI);
79
Nate Begemaneb883af2006-08-23 21:08:52 +000080 virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
81 assert(0 && "CP not implementated yet!");
82 return 0;
83 }
84 virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
Nate Begeman019f8512006-09-10 23:03:44 +000085 assert(JTLocations.size() > Index && "JT not emitted!");
86 return JTLocations[Index];
87 }
88
89 virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
90 if (MBBLocations.size() <= (unsigned)MBB->getNumber())
91 MBBLocations.resize((MBB->getNumber()+1)*2);
92 MBBLocations[MBB->getNumber()] = getCurrentPCOffset();
Nate Begemaneb883af2006-08-23 21:08:52 +000093 }
94
95 virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
96 assert(MBBLocations.size() > (unsigned)MBB->getNumber() &&
97 MBBLocations[MBB->getNumber()] && "MBB not emitted!");
98 return MBBLocations[MBB->getNumber()];
99 }
100
101 /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
Evan Chengce9a5762006-11-16 20:04:04 +0000102 void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
Nate Begemaneb883af2006-08-23 21:08:52 +0000103 assert(0 && "JIT specific function called!");
104 abort();
105 }
106 void *finishFunctionStub(const Function *F) {
107 assert(0 && "JIT specific function called!");
108 abort();
109 return 0;
110 }
111 };
112}
113
114/// startFunction - This callback is invoked when a new machine function is
115/// about to be emitted.
116void MachOCodeEmitter::startFunction(MachineFunction &F) {
117 // Align the output buffer to the appropriate alignment, power of 2.
118 // FIXME: GENERICIZE!!
119 unsigned Align = 4;
120
121 // Get the Mach-O Section that this function belongs in.
122 MOS = &MOW.getTextSection();
123
124 // FIXME: better memory management
125 MOS->SectionData.reserve(4096);
126 BufferBegin = &(MOS->SectionData[0]);
127 BufferEnd = BufferBegin + MOS->SectionData.capacity();
128 CurBufferPtr = BufferBegin + MOS->size;
129
130 // Upgrade the section alignment if required.
131 if (MOS->align < Align) MOS->align = Align;
132
Nate Begeman019f8512006-09-10 23:03:44 +0000133 // Clear per-function data structures.
134 CPLocations.clear();
135 JTLocations.clear();
Nate Begemaneb883af2006-08-23 21:08:52 +0000136 MBBLocations.clear();
137}
138
139/// finishFunction - This callback is invoked after the function is completely
140/// finished.
141bool MachOCodeEmitter::finishFunction(MachineFunction &F) {
142 MOS->size += CurBufferPtr - BufferBegin;
143
144 // Get a symbol for the function to add to the symbol table
Nate Begemand2030e62006-08-26 15:46:34 +0000145 const GlobalValue *FuncV = F.getFunction();
Nate Begeman94be2482006-09-08 22:42:09 +0000146 MachOSym FnSym(FuncV, MOW.Mang->getValueName(FuncV), MOS->Index);
147
Nate Begeman019f8512006-09-10 23:03:44 +0000148 // Emit constant pool to appropriate section(s)
149 emitConstantPool(F.getConstantPool());
150
151 // Emit jump tables to appropriate section
152 emitJumpTables(F.getJumpTableInfo());
Nate Begemaneb883af2006-08-23 21:08:52 +0000153
Nate Begeman019f8512006-09-10 23:03:44 +0000154 // If we have emitted any relocations to function-specific objects such as
155 // basic blocks, constant pools entries, or jump tables, record their
156 // addresses now so that we can rewrite them with the correct addresses
157 // later.
Nate Begemaneb883af2006-08-23 21:08:52 +0000158 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
159 MachineRelocation &MR = Relocations[i];
Nate Begeman019f8512006-09-10 23:03:44 +0000160 intptr_t Addr;
Nate Begemaneb883af2006-08-23 21:08:52 +0000161 if (MR.isBasicBlock()) {
Nate Begeman019f8512006-09-10 23:03:44 +0000162 Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
163 MR.setResultPointer((void *)Addr);
164 } else if (MR.isConstantPoolIndex()) {
165 Addr = getConstantPoolEntryAddress(MR.getConstantPoolIndex());
166 MR.setResultPointer((void *)Addr);
167 } else if (MR.isJumpTableIndex()) {
168 // FIXME: handle PIC codegen
169 Addr = getJumpTableEntryAddress(MR.getJumpTableIndex());
170 MR.setResultPointer((void *)Addr);
Nate Begemaneb883af2006-08-23 21:08:52 +0000171 }
Nate Begeman019f8512006-09-10 23:03:44 +0000172 MOS->Relocations.push_back(MR);
Nate Begemaneb883af2006-08-23 21:08:52 +0000173 }
174 Relocations.clear();
175
176 // Finally, add it to the symtab.
177 MOW.SymbolTable.push_back(FnSym);
178 return false;
179}
180
Nate Begeman019f8512006-09-10 23:03:44 +0000181/// emitConstantPool - For each constant pool entry, figure out which section
182/// the constant should live in, allocate space for it, and emit it to the
183/// Section data buffer.
184void MachOCodeEmitter::emitConstantPool(MachineConstantPool *MCP) {
185}
186
187/// emitJumpTables - Emit all the jump tables for a given jump table info
188/// record to the appropriate section.
189void MachOCodeEmitter::emitJumpTables(MachineJumpTableInfo *MJTI) {
190 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
191 if (JT.empty()) return;
192
193 bool isPIC = MOW.TM.getRelocationModel() == Reloc::PIC_;
194 assert(!isPIC && "PIC codegen not yet handled for mach-o jump tables!");
195
196 MachOWriter::MachOSection &Sec = MOW.getJumpTableSection();
197
198 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
199 // For each jump table, record its offset from the start of the section,
200 // reserve space for the relocations to the MBBs, and add the relocations.
201 const std::vector<MachineBasicBlock*> &MBBs = JT[i].MBBs;
202 JTLocations.push_back(Sec.SectionData.size());
203 for (unsigned mi = 0, me = MBBs.size(); mi != me; ++mi) {
204 MachineRelocation MR(MOW.GetJTRelocation(Sec.SectionData.size(),
205 MBBs[mi]));
206 MR.setResultPointer((void *)JTLocations[i]);
207 Sec.Relocations.push_back(MR);
208 MOW.outaddr(Sec.SectionData, 0);
209 }
210 }
211 // FIXME: it really seems like keeping these in sync is redundant, someone
212 // should do something about that (never access section size directly, only
213 // look at buffer size).
214 Sec.size = Sec.SectionData.size();
215}
216
Nate Begemaneb883af2006-08-23 21:08:52 +0000217//===----------------------------------------------------------------------===//
218// MachOWriter Implementation
219//===----------------------------------------------------------------------===//
220
221MachOWriter::MachOWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
Nate Begemaneb883af2006-08-23 21:08:52 +0000222 is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
223 isLittleEndian = TM.getTargetData()->isLittleEndian();
224
225 // Create the machine code emitter object for this target.
226 MCE = new MachOCodeEmitter(*this);
227}
228
229MachOWriter::~MachOWriter() {
230 delete MCE;
231}
232
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000233void MachOWriter::AddSymbolToSection(MachOSection &Sec, GlobalVariable *GV) {
234 const Type *Ty = GV->getType()->getElementType();
235 unsigned Size = TM.getTargetData()->getTypeSize(Ty);
236 unsigned Align = Log2_32(TM.getTargetData()->getTypeAlignment(Ty));
237
Nate Begemand2030e62006-08-26 15:46:34 +0000238 MachOSym Sym(GV, Mang->getValueName(GV), Sec.Index);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000239 // Reserve space in the .bss section for this symbol while maintaining the
240 // desired section alignment, which must be at least as much as required by
241 // this symbol.
242 if (Align) {
Chris Lattner94425992006-09-02 17:37:30 +0000243 Sec.align = std::max(unsigned(Sec.align), Align);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000244 Sec.size = (Sec.size + Align - 1) & ~(Align-1);
245 }
246 // Record the offset of the symbol, and then allocate space for it.
247 Sym.n_value = Sec.size;
248 Sec.size += Size;
249
250 switch (GV->getLinkage()) {
251 default: // weak/linkonce handled above
252 assert(0 && "Unexpected linkage type!");
253 case GlobalValue::ExternalLinkage:
254 Sym.n_type |= MachOSym::N_EXT;
255 break;
256 case GlobalValue::InternalLinkage:
257 break;
258 }
259 SymbolTable.push_back(Sym);
260}
261
Nate Begemaneb883af2006-08-23 21:08:52 +0000262void MachOWriter::EmitGlobal(GlobalVariable *GV) {
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000263 const Type *Ty = GV->getType()->getElementType();
264 unsigned Size = TM.getTargetData()->getTypeSize(Ty);
265 bool NoInit = !GV->hasInitializer();
Nate Begemand2030e62006-08-26 15:46:34 +0000266
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000267 // If this global has a zero initializer, it is part of the .bss or common
268 // section.
269 if (NoInit || GV->getInitializer()->isNullValue()) {
270 // If this global is part of the common block, add it now. Variables are
271 // part of the common block if they are zero initialized and allowed to be
272 // merged with other symbols.
273 if (NoInit || GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
Nate Begeman94be2482006-09-08 22:42:09 +0000274 MachOSym ExtOrCommonSym(GV, Mang->getValueName(GV), MachOSym::NO_SECT);
Nate Begemanf8f2c5a2006-08-25 06:36:58 +0000275 // For undefined (N_UNDF) external (N_EXT) types, n_value is the size in
276 // bytes of the symbol.
277 ExtOrCommonSym.n_value = Size;
278 // If the symbol is external, we'll put it on a list of symbols whose
279 // addition to the symbol table is being pended until we find a reference
280 if (NoInit)
281 PendingSyms.push_back(ExtOrCommonSym);
282 else
283 SymbolTable.push_back(ExtOrCommonSym);
284 return;
285 }
286 // Otherwise, this symbol is part of the .bss section.
287 MachOSection &BSS = getBSSSection();
288 AddSymbolToSection(BSS, GV);
289 return;
290 }
291
292 // Scalar read-only data goes in a literal section if the scalar is 4, 8, or
293 // 16 bytes, or a cstring. Other read only data goes into a regular const
294 // section. Read-write data goes in the data section.
295 MachOSection &Sec = GV->isConstant() ? getConstSection(Ty) : getDataSection();
296 AddSymbolToSection(Sec, GV);
297
Nate Begeman94be2482006-09-08 22:42:09 +0000298 // FIXME: A couple significant changes are required for this to work, even for
299 // trivial cases such as a constant integer:
300 // 0. InitializeMemory needs to be split out of ExecutionEngine. We don't
301 // want to have to create an ExecutionEngine such as JIT just to write
302 // some bytes into a buffer. The only thing necessary for
303 // InitializeMemory to function properly should be TargetData.
304 //
305 // 1. InitializeMemory needs to be enhanced to return MachineRelocations
306 // rather than accessing the address of objects such basic blocks,
307 // constant pools, and jump tables. The client of InitializeMemory such
308 // as an object writer or jit emitter should then handle these relocs
309 // appropriately.
310 //
311 // FIXME: need to allocate memory for the global initializer.
Nate Begemaneb883af2006-08-23 21:08:52 +0000312}
313
314
315bool MachOWriter::runOnMachineFunction(MachineFunction &MF) {
316 // Nothing to do here, this is all done through the MCE object.
317 return false;
318}
319
320bool MachOWriter::doInitialization(Module &M) {
321 // Set the magic value, now that we know the pointer size and endianness
322 Header.setMagic(isLittleEndian, is64Bit);
323
324 // Set the file type
325 // FIXME: this only works for object files, we do not support the creation
326 // of dynamic libraries or executables at this time.
327 Header.filetype = MachOHeader::MH_OBJECT;
328
329 Mang = new Mangler(M);
330 return false;
331}
332
333/// doFinalization - Now that the module has been completely processed, emit
334/// the Mach-O file to 'O'.
335bool MachOWriter::doFinalization(Module &M) {
Nate Begemand2030e62006-08-26 15:46:34 +0000336 // FIXME: we don't handle debug info yet, we should probably do that.
337
Nate Begemaneb883af2006-08-23 21:08:52 +0000338 // Okay, the.text section has been completed, build the .data, .bss, and
339 // "common" sections next.
340 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
341 I != E; ++I)
342 EmitGlobal(I);
343
Nate Begemand2030e62006-08-26 15:46:34 +0000344 // Emit the symbol table to temporary buffers, so that we know the size of
345 // the string table when we write the load commands in the next phase.
346 BufferSymbolAndStringTable();
Nate Begeman94be2482006-09-08 22:42:09 +0000347
Nate Begemaneb883af2006-08-23 21:08:52 +0000348 // Emit the header and load commands.
349 EmitHeaderAndLoadCommands();
350
Nate Begeman019f8512006-09-10 23:03:44 +0000351 // Emit the various sections and their relocation info.
Nate Begemaneb883af2006-08-23 21:08:52 +0000352 EmitSections();
353
Nate Begemand2030e62006-08-26 15:46:34 +0000354 // Write the symbol table and the string table to the end of the file.
355 O.write((char*)&SymT[0], SymT.size());
356 O.write((char*)&StrT[0], StrT.size());
Nate Begemaneb883af2006-08-23 21:08:52 +0000357
358 // We are done with the abstract symbols.
359 SectionList.clear();
360 SymbolTable.clear();
361 DynamicSymbolTable.clear();
362
363 // Release the name mangler object.
364 delete Mang; Mang = 0;
365 return false;
366}
367
368void MachOWriter::EmitHeaderAndLoadCommands() {
369 // Step #0: Fill in the segment load command size, since we need it to figure
370 // out the rest of the header fields
371 MachOSegment SEG("", is64Bit);
372 SEG.nsects = SectionList.size();
373 SEG.cmdsize = SEG.cmdSize(is64Bit) +
374 SEG.nsects * SectionList.begin()->cmdSize(is64Bit);
375
376 // Step #1: calculate the number of load commands. We always have at least
377 // one, for the LC_SEGMENT load command, plus two for the normal
378 // and dynamic symbol tables, if there are any symbols.
379 Header.ncmds = SymbolTable.empty() ? 1 : 3;
380
381 // Step #2: calculate the size of the load commands
382 Header.sizeofcmds = SEG.cmdsize;
383 if (!SymbolTable.empty())
384 Header.sizeofcmds += SymTab.cmdsize + DySymTab.cmdsize;
385
386 // Step #3: write the header to the file
387 // Local alias to shortenify coming code.
388 DataBuffer &FH = Header.HeaderData;
389 outword(FH, Header.magic);
390 outword(FH, Header.cputype);
391 outword(FH, Header.cpusubtype);
392 outword(FH, Header.filetype);
393 outword(FH, Header.ncmds);
394 outword(FH, Header.sizeofcmds);
395 outword(FH, Header.flags);
396 if (is64Bit)
397 outword(FH, Header.reserved);
398
399 // Step #4: Finish filling in the segment load command and write it out
400 for (std::list<MachOSection>::iterator I = SectionList.begin(),
401 E = SectionList.end(); I != E; ++I)
402 SEG.filesize += I->size;
403 SEG.vmsize = SEG.filesize;
404 SEG.fileoff = Header.cmdSize(is64Bit) + Header.sizeofcmds;
405
406 outword(FH, SEG.cmd);
407 outword(FH, SEG.cmdsize);
408 outstring(FH, SEG.segname, 16);
409 outaddr(FH, SEG.vmaddr);
410 outaddr(FH, SEG.vmsize);
411 outaddr(FH, SEG.fileoff);
412 outaddr(FH, SEG.filesize);
413 outword(FH, SEG.maxprot);
414 outword(FH, SEG.initprot);
415 outword(FH, SEG.nsects);
416 outword(FH, SEG.flags);
417
Nate Begeman94be2482006-09-08 22:42:09 +0000418 // Step #5: Finish filling in the fields of the MachOSections
419 uint64_t currentAddr = 0;
Nate Begemaneb883af2006-08-23 21:08:52 +0000420 for (std::list<MachOSection>::iterator I = SectionList.begin(),
421 E = SectionList.end(); I != E; ++I) {
Nate Begeman94be2482006-09-08 22:42:09 +0000422 I->addr = currentAddr;
423 I->offset = currentAddr + SEG.fileoff;
Nate Begeman019f8512006-09-10 23:03:44 +0000424
Nate Begeman94be2482006-09-08 22:42:09 +0000425 // FIXME: do we need to do something with alignment here?
426 currentAddr += I->size;
427 }
428
429 // Step #6: Calculate the number of relocations for each section and write out
430 // the section commands for each section
431 currentAddr += SEG.fileoff;
432 for (std::list<MachOSection>::iterator I = SectionList.begin(),
433 E = SectionList.end(); I != E; ++I) {
434 // calculate the relocation info for this section command
Nate Begeman019f8512006-09-10 23:03:44 +0000435 CalculateRelocations(*I, currentAddr);
436 currentAddr += I->nreloc * 8;
Nate Begeman94be2482006-09-08 22:42:09 +0000437
438 // write the finalized section command to the output buffer
Nate Begemaneb883af2006-08-23 21:08:52 +0000439 outstring(FH, I->sectname, 16);
440 outstring(FH, I->segname, 16);
441 outaddr(FH, I->addr);
442 outaddr(FH, I->size);
443 outword(FH, I->offset);
444 outword(FH, I->align);
445 outword(FH, I->reloff);
446 outword(FH, I->nreloc);
447 outword(FH, I->flags);
448 outword(FH, I->reserved1);
449 outword(FH, I->reserved2);
450 if (is64Bit)
451 outword(FH, I->reserved3);
452 }
453
Nate Begeman94be2482006-09-08 22:42:09 +0000454 // Step #7: Emit LC_SYMTAB/LC_DYSYMTAB load commands
Nate Begemaneb883af2006-08-23 21:08:52 +0000455 // FIXME: add size of relocs
Nate Begeman94be2482006-09-08 22:42:09 +0000456 SymTab.symoff = currentAddr;
Nate Begemaneb883af2006-08-23 21:08:52 +0000457 SymTab.nsyms = SymbolTable.size();
Nate Begemand2030e62006-08-26 15:46:34 +0000458 SymTab.stroff = SymTab.symoff + SymT.size();
459 SymTab.strsize = StrT.size();
Nate Begemaneb883af2006-08-23 21:08:52 +0000460 outword(FH, SymTab.cmd);
461 outword(FH, SymTab.cmdsize);
462 outword(FH, SymTab.symoff);
463 outword(FH, SymTab.nsyms);
464 outword(FH, SymTab.stroff);
465 outword(FH, SymTab.strsize);
466
467 // FIXME: set DySymTab fields appropriately
Nate Begemand2030e62006-08-26 15:46:34 +0000468 // We should probably just update these in BufferSymbolAndStringTable since
469 // thats where we're partitioning up the different kinds of symbols.
Nate Begemaneb883af2006-08-23 21:08:52 +0000470 outword(FH, DySymTab.cmd);
471 outword(FH, DySymTab.cmdsize);
472 outword(FH, DySymTab.ilocalsym);
473 outword(FH, DySymTab.nlocalsym);
474 outword(FH, DySymTab.iextdefsym);
475 outword(FH, DySymTab.nextdefsym);
476 outword(FH, DySymTab.iundefsym);
477 outword(FH, DySymTab.nundefsym);
478 outword(FH, DySymTab.tocoff);
479 outword(FH, DySymTab.ntoc);
480 outword(FH, DySymTab.modtaboff);
481 outword(FH, DySymTab.nmodtab);
482 outword(FH, DySymTab.extrefsymoff);
483 outword(FH, DySymTab.nextrefsyms);
484 outword(FH, DySymTab.indirectsymoff);
485 outword(FH, DySymTab.nindirectsyms);
486 outword(FH, DySymTab.extreloff);
487 outword(FH, DySymTab.nextrel);
488 outword(FH, DySymTab.locreloff);
489 outword(FH, DySymTab.nlocrel);
490
491 O.write((char*)&FH[0], FH.size());
492}
493
494/// EmitSections - Now that we have constructed the file header and load
495/// commands, emit the data for each section to the file.
496void MachOWriter::EmitSections() {
497 for (std::list<MachOSection>::iterator I = SectionList.begin(),
Nate Begeman019f8512006-09-10 23:03:44 +0000498 E = SectionList.end(); I != E; ++I)
499 // Emit the contents of each section
Nate Begemaneb883af2006-08-23 21:08:52 +0000500 O.write((char*)&I->SectionData[0], I->size);
Nate Begeman019f8512006-09-10 23:03:44 +0000501 for (std::list<MachOSection>::iterator I = SectionList.begin(),
502 E = SectionList.end(); I != E; ++I)
503 // Emit the relocation entry data for each section.
504 O.write((char*)&I->RelocBuffer[0], I->RelocBuffer.size());
Nate Begemaneb883af2006-08-23 21:08:52 +0000505}
506
Nate Begemand2030e62006-08-26 15:46:34 +0000507/// PartitionByLocal - Simple boolean predicate that returns true if Sym is
508/// a local symbol rather than an external symbol.
509bool MachOWriter::PartitionByLocal(const MachOSym &Sym) {
510 // FIXME: Not totally sure if private extern counts as external
511 return (Sym.n_type & (MachOSym::N_EXT | MachOSym::N_PEXT)) == 0;
Nate Begemaneb883af2006-08-23 21:08:52 +0000512}
513
Nate Begemand2030e62006-08-26 15:46:34 +0000514/// PartitionByDefined - Simple boolean predicate that returns true if Sym is
515/// defined in this module.
516bool MachOWriter::PartitionByDefined(const MachOSym &Sym) {
517 // FIXME: Do N_ABS or N_INDR count as defined?
518 return (Sym.n_type & MachOSym::N_SECT) == MachOSym::N_SECT;
519}
Nate Begemaneb883af2006-08-23 21:08:52 +0000520
Nate Begemand2030e62006-08-26 15:46:34 +0000521/// BufferSymbolAndStringTable - Sort the symbols we encountered and assign them
522/// each a string table index so that they appear in the correct order in the
523/// output file.
524void MachOWriter::BufferSymbolAndStringTable() {
525 // The order of the symbol table is:
526 // 1. local symbols
527 // 2. defined external symbols (sorted by name)
528 // 3. undefined external symbols (sorted by name)
529
530 // Sort the symbols by name, so that when we partition the symbols by scope
531 // of definition, we won't have to sort by name within each partition.
532 std::sort(SymbolTable.begin(), SymbolTable.end(), MachOSymCmp());
533
534 // Parition the symbol table entries so that all local symbols come before
535 // all symbols with external linkage. { 1 | 2 3 }
536 std::partition(SymbolTable.begin(), SymbolTable.end(), PartitionByLocal);
537
538 // Advance iterator to beginning of external symbols and partition so that
539 // all external symbols defined in this module come before all external
540 // symbols defined elsewhere. { 1 | 2 | 3 }
541 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
542 E = SymbolTable.end(); I != E; ++I) {
543 if (!PartitionByLocal(*I)) {
544 std::partition(I, E, PartitionByDefined);
545 break;
546 }
547 }
548
Nate Begemaneb883af2006-08-23 21:08:52 +0000549 // Write out a leading zero byte when emitting string table, for n_strx == 0
550 // which means an empty string.
Nate Begemand2030e62006-08-26 15:46:34 +0000551 outbyte(StrT, 0);
Nate Begemaneb883af2006-08-23 21:08:52 +0000552
Nate Begemand2030e62006-08-26 15:46:34 +0000553 // The order of the string table is:
554 // 1. strings for external symbols
555 // 2. strings for local symbols
556 // Since this is the opposite order from the symbol table, which we have just
557 // sorted, we can walk the symbol table backwards to output the string table.
558 for (std::vector<MachOSym>::reverse_iterator I = SymbolTable.rbegin(),
559 E = SymbolTable.rend(); I != E; ++I) {
560 if (I->GVName == "") {
561 I->n_strx = 0;
562 } else {
563 I->n_strx = StrT.size();
564 outstring(StrT, I->GVName, I->GVName.length()+1);
565 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000566 }
Nate Begemand2030e62006-08-26 15:46:34 +0000567
568 for (std::vector<MachOSym>::iterator I = SymbolTable.begin(),
569 E = SymbolTable.end(); I != E; ++I) {
570 // Emit nlist to buffer
571 outword(SymT, I->n_strx);
572 outbyte(SymT, I->n_type);
573 outbyte(SymT, I->n_sect);
574 outhalf(SymT, I->n_desc);
575 outaddr(SymT, I->n_value);
576 }
Nate Begemaneb883af2006-08-23 21:08:52 +0000577}
Nate Begeman94be2482006-09-08 22:42:09 +0000578
Nate Begeman019f8512006-09-10 23:03:44 +0000579/// CalculateRelocations - For each MachineRelocation in the current section,
580/// calculate the index of the section containing the object to be relocated,
581/// and the offset into that section. From this information, create the
582/// appropriate target-specific MachORelocation type and add buffer it to be
583/// written out after we are finished writing out sections.
584void MachOWriter::CalculateRelocations(MachOSection &MOS, unsigned RelOffset) {
585 for (unsigned i = 0, e = MOS.Relocations.size(); i != e; ++i) {
586 // FIXME: calculate the correct offset and section index for relocated
587 // object.
588 // FIXME: somehow convey the fact that the relocation might be external
589 // to the relocating code.
590 GetTargetRelocation(MOS.Relocations[i], MOS, MOS.Index);
591 }
592 if (MOS.nreloc != 0)
593 MOS.reloff = RelOffset;
594}
595
Nate Begeman94be2482006-09-08 22:42:09 +0000596MachOSym::MachOSym(const GlobalValue *gv, std::string name, uint8_t sect) :
597 GV(gv), GVName(name), n_strx(0), n_type(sect == NO_SECT ? N_UNDF : N_SECT),
598 n_sect(sect), n_desc(0), n_value(0) {
599 // FIXME: take a target machine, and then add the appropriate prefix for
600 // the linkage type based on the TargetAsmInfo
601 switch (GV->getLinkage()) {
602 default:
603 assert(0 && "Unexpected linkage type!");
604 break;
605 case GlobalValue::WeakLinkage:
606 case GlobalValue::LinkOnceLinkage:
607 assert(!isa<Function>(gv) && "Unexpected linkage type for Function!");
608 case GlobalValue::ExternalLinkage:
609 n_type |= N_EXT;
610 break;
611 case GlobalValue::InternalLinkage:
612 break;
613 }
614}