blob: 8d5aeee3a50f1576a8506abb6eeb9ba396a08ac2 [file] [log] [blame]
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +00001//===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Dunbarae5abd52010-12-16 16:09:19 +000010#include "llvm/MC/MCMachObjectWriter.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000011#include "llvm/ADT/StringMap.h"
12#include "llvm/ADT/Twine.h"
Evan Cheng78c10ee2011-07-25 23:24:55 +000013#include "llvm/MC/MCAsmBackend.h"
Daniel Dunbar207e06e2010-03-24 03:43:40 +000014#include "llvm/MC/MCAsmLayout.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/MC/MCAssembler.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000016#include "llvm/MC/MCExpr.h"
Craig Topperf1d0f772012-03-26 06:58:25 +000017#include "llvm/MC/MCFixupKindInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/MC/MCMachOSymbolFlags.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000019#include "llvm/MC/MCObjectWriter.h"
20#include "llvm/MC/MCSectionMachO.h"
21#include "llvm/MC/MCSymbol.h"
22#include "llvm/MC/MCValue.h"
Jim Grosbach3e965312012-05-18 19:12:01 +000023#include "llvm/Support/Debug.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000024#include "llvm/Support/ErrorHandling.h"
Charles Davis9c3dd1b2013-08-27 05:00:43 +000025#include "llvm/Support/MachO.h"
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000026#include <vector>
27using namespace llvm;
28
Pedro Artigas99cbdde2012-12-14 18:52:11 +000029void MachObjectWriter::reset() {
30 Relocations.clear();
31 IndirectSymBase.clear();
32 StringTable.clear();
33 LocalSymbolData.clear();
34 ExternalSymbolData.clear();
35 UndefinedSymbolData.clear();
36 MCObjectWriter::reset();
37}
38
Jim Grosbachba8297e2011-06-24 23:44:37 +000039bool MachObjectWriter::
40doesSymbolRequireExternRelocation(const MCSymbolData *SD) {
Daniel Dunbare9460ec2010-05-10 23:15:13 +000041 // Undefined symbols are always extern.
42 if (SD->Symbol->isUndefined())
43 return true;
44
45 // References to weak definitions require external relocation entries; the
46 // definition may not always be the one in the same object file.
47 if (SD->getFlags() & SF_WeakDefinition)
48 return true;
49
50 // Otherwise, we can use an internal relocation.
51 return false;
52}
53
Jim Grosbachba8297e2011-06-24 23:44:37 +000054bool MachObjectWriter::
55MachSymbolData::operator<(const MachSymbolData &RHS) const {
56 return SymbolData->getSymbol().getName() <
57 RHS.SymbolData->getSymbol().getName();
58}
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000059
Jim Grosbachba8297e2011-06-24 23:44:37 +000060bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
61 const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo(
62 (MCFixupKind) Kind);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000063
Jim Grosbachba8297e2011-06-24 23:44:37 +000064 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
65}
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +000066
Jim Grosbachba8297e2011-06-24 23:44:37 +000067uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment,
68 const MCAsmLayout &Layout) const {
69 return getSectionAddress(Fragment->getParent()) +
70 Layout.getFragmentOffset(Fragment);
71}
Bill Wendlingbbdffa92011-06-22 21:07:27 +000072
73uint64_t MachObjectWriter::getSymbolAddress(const MCSymbolData* SD,
74 const MCAsmLayout &Layout) const {
75 const MCSymbol &S = SD->getSymbol();
76
77 // If this is a variable, then recursively evaluate now.
78 if (S.isVariable()) {
Jim Grosbach45d81bd2012-09-13 23:11:25 +000079 if (const MCConstantExpr *C =
80 dyn_cast<const MCConstantExpr>(S.getVariableValue()))
81 return C->getValue();
82
83
Bill Wendlingbbdffa92011-06-22 21:07:27 +000084 MCValue Target;
85 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, Layout))
86 report_fatal_error("unable to evaluate offset for variable '" +
87 S.getName() + "'");
88
89 // Verify that any used symbols are defined.
90 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
91 report_fatal_error("unable to evaluate offset to undefined symbol '" +
92 Target.getSymA()->getSymbol().getName() + "'");
93 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
94 report_fatal_error("unable to evaluate offset to undefined symbol '" +
95 Target.getSymB()->getSymbol().getName() + "'");
96
97 uint64_t Address = Target.getConstant();
98 if (Target.getSymA())
99 Address += getSymbolAddress(&Layout.getAssembler().getSymbolData(
100 Target.getSymA()->getSymbol()), Layout);
101 if (Target.getSymB())
102 Address += getSymbolAddress(&Layout.getAssembler().getSymbolData(
103 Target.getSymB()->getSymbol()), Layout);
104 return Address;
105 }
106
107 return getSectionAddress(SD->getFragment()->getParent()) +
108 Layout.getSymbolOffset(SD);
109}
110
111uint64_t MachObjectWriter::getPaddingSize(const MCSectionData *SD,
112 const MCAsmLayout &Layout) const {
113 uint64_t EndAddr = getSectionAddress(SD) + Layout.getSectionAddressSize(SD);
114 unsigned Next = SD->getLayoutOrder() + 1;
115 if (Next >= Layout.getSectionOrder().size())
116 return 0;
117
118 const MCSectionData &NextSD = *Layout.getSectionOrder()[Next];
119 if (NextSD.getSection().isVirtualSection())
120 return 0;
121 return OffsetToAlignment(EndAddr, NextSD.getAlignment());
122}
123
124void MachObjectWriter::WriteHeader(unsigned NumLoadCommands,
125 unsigned LoadCommandsSize,
126 bool SubsectionsViaSymbols) {
127 uint32_t Flags = 0;
128
129 if (SubsectionsViaSymbols)
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000130 Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000131
132 // struct mach_header (28 bytes) or
133 // struct mach_header_64 (32 bytes)
134
135 uint64_t Start = OS.tell();
136 (void) Start;
137
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000138 Write32(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000139
140 Write32(TargetObjectWriter->getCPUType());
141 Write32(TargetObjectWriter->getCPUSubtype());
142
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000143 Write32(MachO::MH_OBJECT);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000144 Write32(NumLoadCommands);
145 Write32(LoadCommandsSize);
146 Write32(Flags);
147 if (is64Bit())
148 Write32(0); // reserved
149
150 assert(OS.tell() - Start ==
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000151 (is64Bit()?sizeof(MachO::mach_header_64): sizeof(MachO::mach_header)));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000152}
153
154/// WriteSegmentLoadCommand - Write a segment load command.
155///
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +0000156/// \param NumSections The number of sections in this segment.
157/// \param SectionDataSize The total size of the sections.
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000158void MachObjectWriter::WriteSegmentLoadCommand(unsigned NumSections,
159 uint64_t VMSize,
160 uint64_t SectionDataStartOffset,
161 uint64_t SectionDataSize) {
162 // struct segment_command (56 bytes) or
163 // struct segment_command_64 (72 bytes)
164
165 uint64_t Start = OS.tell();
166 (void) Start;
167
168 unsigned SegmentLoadCommandSize =
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000169 is64Bit() ? sizeof(MachO::segment_command_64):
170 sizeof(MachO::segment_command);
171 Write32(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000172 Write32(SegmentLoadCommandSize +
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000173 NumSections * (is64Bit() ? sizeof(MachO::section_64) :
174 sizeof(MachO::section)));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000175
176 WriteBytes("", 16);
177 if (is64Bit()) {
178 Write64(0); // vmaddr
179 Write64(VMSize); // vmsize
180 Write64(SectionDataStartOffset); // file offset
181 Write64(SectionDataSize); // file size
182 } else {
183 Write32(0); // vmaddr
184 Write32(VMSize); // vmsize
185 Write32(SectionDataStartOffset); // file offset
186 Write32(SectionDataSize); // file size
187 }
188 Write32(0x7); // maxprot
189 Write32(0x7); // initprot
190 Write32(NumSections);
191 Write32(0); // flags
192
193 assert(OS.tell() - Start == SegmentLoadCommandSize);
194}
195
196void MachObjectWriter::WriteSection(const MCAssembler &Asm,
197 const MCAsmLayout &Layout,
198 const MCSectionData &SD,
199 uint64_t FileOffset,
200 uint64_t RelocationsStart,
201 unsigned NumRelocations) {
202 uint64_t SectionSize = Layout.getSectionAddressSize(&SD);
203
204 // The offset is unused for virtual sections.
205 if (SD.getSection().isVirtualSection()) {
206 assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!");
207 FileOffset = 0;
208 }
209
210 // struct section (68 bytes) or
211 // struct section_64 (80 bytes)
212
213 uint64_t Start = OS.tell();
214 (void) Start;
215
216 const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
217 WriteBytes(Section.getSectionName(), 16);
218 WriteBytes(Section.getSegmentName(), 16);
219 if (is64Bit()) {
220 Write64(getSectionAddress(&SD)); // address
221 Write64(SectionSize); // size
222 } else {
223 Write32(getSectionAddress(&SD)); // address
224 Write32(SectionSize); // size
225 }
226 Write32(FileOffset);
227
228 unsigned Flags = Section.getTypeAndAttributes();
229 if (SD.hasInstructions())
230 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
231
232 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
233 Write32(Log2_32(SD.getAlignment()));
234 Write32(NumRelocations ? RelocationsStart : 0);
235 Write32(NumRelocations);
236 Write32(Flags);
237 Write32(IndirectSymBase.lookup(&SD)); // reserved1
238 Write32(Section.getStubSize()); // reserved2
239 if (is64Bit())
240 Write32(0); // reserved3
241
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000242 assert(OS.tell() - Start == (is64Bit() ? sizeof(MachO::section_64) :
243 sizeof(MachO::section)));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000244}
245
246void MachObjectWriter::WriteSymtabLoadCommand(uint32_t SymbolOffset,
247 uint32_t NumSymbols,
248 uint32_t StringTableOffset,
249 uint32_t StringTableSize) {
250 // struct symtab_command (24 bytes)
251
252 uint64_t Start = OS.tell();
253 (void) Start;
254
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000255 Write32(MachO::LC_SYMTAB);
256 Write32(sizeof(MachO::symtab_command));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000257 Write32(SymbolOffset);
258 Write32(NumSymbols);
259 Write32(StringTableOffset);
260 Write32(StringTableSize);
261
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000262 assert(OS.tell() - Start == sizeof(MachO::symtab_command));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000263}
264
265void MachObjectWriter::WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
266 uint32_t NumLocalSymbols,
267 uint32_t FirstExternalSymbol,
268 uint32_t NumExternalSymbols,
269 uint32_t FirstUndefinedSymbol,
270 uint32_t NumUndefinedSymbols,
271 uint32_t IndirectSymbolOffset,
272 uint32_t NumIndirectSymbols) {
273 // struct dysymtab_command (80 bytes)
274
275 uint64_t Start = OS.tell();
276 (void) Start;
277
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000278 Write32(MachO::LC_DYSYMTAB);
279 Write32(sizeof(MachO::dysymtab_command));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000280 Write32(FirstLocalSymbol);
281 Write32(NumLocalSymbols);
282 Write32(FirstExternalSymbol);
283 Write32(NumExternalSymbols);
284 Write32(FirstUndefinedSymbol);
285 Write32(NumUndefinedSymbols);
286 Write32(0); // tocoff
287 Write32(0); // ntoc
288 Write32(0); // modtaboff
289 Write32(0); // nmodtab
290 Write32(0); // extrefsymoff
291 Write32(0); // nextrefsyms
292 Write32(IndirectSymbolOffset);
293 Write32(NumIndirectSymbols);
294 Write32(0); // extreloff
295 Write32(0); // nextrel
296 Write32(0); // locreloff
297 Write32(0); // nlocrel
298
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000299 assert(OS.tell() - Start == sizeof(MachO::dysymtab_command));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000300}
301
Bill Wendling3f2ea822011-06-23 00:09:43 +0000302void MachObjectWriter::WriteNlist(MachSymbolData &MSD,
303 const MCAsmLayout &Layout) {
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000304 MCSymbolData &Data = *MSD.SymbolData;
305 const MCSymbol &Symbol = Data.getSymbol();
306 uint8_t Type = 0;
307 uint16_t Flags = Data.getFlags();
Jim Grosbach739b5572011-08-09 22:12:37 +0000308 uint64_t Address = 0;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000309
310 // Set the N_TYPE bits. See <mach-o/nlist.h>.
311 //
312 // FIXME: Are the prebound or indirect fields possible here?
313 if (Symbol.isUndefined())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000314 Type = MachO::N_UNDF;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000315 else if (Symbol.isAbsolute())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000316 Type = MachO::N_ABS;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000317 else
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000318 Type = MachO::N_SECT;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000319
320 // FIXME: Set STAB bits.
321
322 if (Data.isPrivateExtern())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000323 Type |= MachO::N_PEXT;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000324
325 // Set external bit.
326 if (Data.isExternal() || Symbol.isUndefined())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000327 Type |= MachO::N_EXT;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000328
329 // Compute the symbol address.
330 if (Symbol.isDefined()) {
Jim Grosbach45d81bd2012-09-13 23:11:25 +0000331 Address = getSymbolAddress(&Data, Layout);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000332 } else if (Data.isCommon()) {
333 // Common symbols are encoded with the size in the address
334 // field, and their alignment in the flags.
335 Address = Data.getCommonSize();
336
337 // Common alignment is packed into the 'desc' bits.
338 if (unsigned Align = Data.getCommonAlignment()) {
339 unsigned Log2Size = Log2_32(Align);
340 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
341 if (Log2Size > 15)
342 report_fatal_error("invalid 'common' alignment '" +
343 Twine(Align) + "'");
344 // FIXME: Keep this mask with the SymbolFlags enumeration.
345 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
346 }
347 }
348
349 // struct nlist (12 bytes)
350
351 Write32(MSD.StringIndex);
352 Write8(Type);
353 Write8(MSD.SectionIndex);
354
355 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
356 // value.
357 Write16(Flags);
358 if (is64Bit())
359 Write64(Address);
360 else
361 Write32(Address);
362}
363
Jim Grosbach3e965312012-05-18 19:12:01 +0000364void MachObjectWriter::WriteLinkeditLoadCommand(uint32_t Type,
365 uint32_t DataOffset,
366 uint32_t DataSize) {
367 uint64_t Start = OS.tell();
368 (void) Start;
369
370 Write32(Type);
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000371 Write32(sizeof(MachO::linkedit_data_command));
Jim Grosbach3e965312012-05-18 19:12:01 +0000372 Write32(DataOffset);
373 Write32(DataSize);
374
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000375 assert(OS.tell() - Start == sizeof(MachO::linkedit_data_command));
Jim Grosbach3e965312012-05-18 19:12:01 +0000376}
377
Daniel Dunbara94c3392013-01-18 01:26:07 +0000378static unsigned ComputeLinkerOptionsLoadCommandSize(
Daniel Dunbar84920962013-01-22 03:42:49 +0000379 const std::vector<std::string> &Options, bool is64Bit)
Daniel Dunbara94c3392013-01-18 01:26:07 +0000380{
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000381 unsigned Size = sizeof(MachO::linker_options_command);
Daniel Dunbara94c3392013-01-18 01:26:07 +0000382 for (unsigned i = 0, e = Options.size(); i != e; ++i)
383 Size += Options[i].size() + 1;
Daniel Dunbar84920962013-01-22 03:42:49 +0000384 return RoundUpToAlignment(Size, is64Bit ? 8 : 4);
Daniel Dunbara94c3392013-01-18 01:26:07 +0000385}
386
387void MachObjectWriter::WriteLinkerOptionsLoadCommand(
388 const std::vector<std::string> &Options)
389{
Daniel Dunbar84920962013-01-22 03:42:49 +0000390 unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit());
Daniel Dunbara94c3392013-01-18 01:26:07 +0000391 uint64_t Start = OS.tell();
392 (void) Start;
393
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000394 Write32(MachO::LC_LINKER_OPTIONS);
Daniel Dunbara94c3392013-01-18 01:26:07 +0000395 Write32(Size);
396 Write32(Options.size());
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000397 uint64_t BytesWritten = sizeof(MachO::linker_options_command);
Daniel Dunbara94c3392013-01-18 01:26:07 +0000398 for (unsigned i = 0, e = Options.size(); i != e; ++i) {
399 // Write each string, including the null byte.
400 const std::string &Option = Options[i];
401 WriteBytes(Option.c_str(), Option.size() + 1);
402 BytesWritten += Option.size() + 1;
403 }
404
Daniel Dunbar84920962013-01-22 03:42:49 +0000405 // Pad to a multiple of the pointer size.
406 WriteBytes("", OffsetToAlignment(BytesWritten, is64Bit() ? 8 : 4));
Daniel Dunbara94c3392013-01-18 01:26:07 +0000407
408 assert(OS.tell() - Start == Size);
409}
410
Jim Grosbach3e965312012-05-18 19:12:01 +0000411
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000412void MachObjectWriter::RecordRelocation(const MCAssembler &Asm,
413 const MCAsmLayout &Layout,
414 const MCFragment *Fragment,
415 const MCFixup &Fixup,
416 MCValue Target,
417 uint64_t &FixedValue) {
Jim Grosbachba8297e2011-06-24 23:44:37 +0000418 TargetObjectWriter->RecordRelocation(this, Asm, Layout, Fragment, Fixup,
419 Target, FixedValue);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000420}
421
422void MachObjectWriter::BindIndirectSymbols(MCAssembler &Asm) {
423 // This is the point where 'as' creates actual symbols for indirect symbols
424 // (in the following two passes). It would be easier for us to do this sooner
425 // when we see the attribute, but that makes getting the order in the symbol
426 // table much more complicated than it is worth.
427 //
428 // FIXME: Revisit this when the dust settles.
429
430 // Bind non lazy symbol pointers first.
431 unsigned IndirectIndex = 0;
432 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
433 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
434 const MCSectionMachO &Section =
435 cast<MCSectionMachO>(it->SectionData->getSection());
436
437 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
438 continue;
439
440 // Initialize the section indirect symbol base, if necessary.
Benjamin Kramer05d96f92012-08-22 15:37:57 +0000441 IndirectSymBase.insert(std::make_pair(it->SectionData, IndirectIndex));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000442
443 Asm.getOrCreateSymbolData(*it->Symbol);
444 }
445
446 // Then lazy symbol pointers and symbol stubs.
447 IndirectIndex = 0;
448 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
449 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
450 const MCSectionMachO &Section =
451 cast<MCSectionMachO>(it->SectionData->getSection());
452
453 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
454 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
455 continue;
456
457 // Initialize the section indirect symbol base, if necessary.
Benjamin Kramer05d96f92012-08-22 15:37:57 +0000458 IndirectSymBase.insert(std::make_pair(it->SectionData, IndirectIndex));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000459
460 // Set the symbol type to undefined lazy, but only on construction.
461 //
462 // FIXME: Do not hardcode.
463 bool Created;
464 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
465 if (Created)
466 Entry.setFlags(Entry.getFlags() | 0x0001);
467 }
468}
469
470/// ComputeSymbolTable - Compute the symbol table data
471///
472/// \param StringTable [out] - The string table data.
473/// \param StringIndexMap [out] - Map from symbol names to offsets in the
474/// string table.
Bill Wendling3f2ea822011-06-23 00:09:43 +0000475void MachObjectWriter::
476ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
477 std::vector<MachSymbolData> &LocalSymbolData,
478 std::vector<MachSymbolData> &ExternalSymbolData,
479 std::vector<MachSymbolData> &UndefinedSymbolData) {
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000480 // Build section lookup table.
481 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
482 unsigned Index = 1;
483 for (MCAssembler::iterator it = Asm.begin(),
484 ie = Asm.end(); it != ie; ++it, ++Index)
485 SectionIndexMap[&it->getSection()] = Index;
486 assert(Index <= 256 && "Too many sections!");
487
488 // Index 0 is always the empty string.
489 StringMap<uint64_t> StringIndexMap;
490 StringTable += '\x00';
491
492 // Build the symbol arrays and the string table, but only for non-local
493 // symbols.
494 //
495 // The particular order that we collect the symbols and create the string
496 // table, then sort the symbols is chosen to match 'as'. Even though it
497 // doesn't matter for correctness, this is important for letting us diff .o
498 // files.
499 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
500 ie = Asm.symbol_end(); it != ie; ++it) {
501 const MCSymbol &Symbol = it->getSymbol();
502
503 // Ignore non-linker visible symbols.
504 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
505 continue;
506
507 if (!it->isExternal() && !Symbol.isUndefined())
508 continue;
509
510 uint64_t &Entry = StringIndexMap[Symbol.getName()];
511 if (!Entry) {
512 Entry = StringTable.size();
513 StringTable += Symbol.getName();
514 StringTable += '\x00';
515 }
516
517 MachSymbolData MSD;
518 MSD.SymbolData = it;
519 MSD.StringIndex = Entry;
520
521 if (Symbol.isUndefined()) {
522 MSD.SectionIndex = 0;
523 UndefinedSymbolData.push_back(MSD);
524 } else if (Symbol.isAbsolute()) {
525 MSD.SectionIndex = 0;
526 ExternalSymbolData.push_back(MSD);
527 } else {
528 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
529 assert(MSD.SectionIndex && "Invalid section index!");
530 ExternalSymbolData.push_back(MSD);
531 }
532 }
533
534 // Now add the data for local symbols.
535 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
536 ie = Asm.symbol_end(); it != ie; ++it) {
537 const MCSymbol &Symbol = it->getSymbol();
538
539 // Ignore non-linker visible symbols.
540 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
541 continue;
542
543 if (it->isExternal() || Symbol.isUndefined())
544 continue;
545
546 uint64_t &Entry = StringIndexMap[Symbol.getName()];
547 if (!Entry) {
548 Entry = StringTable.size();
549 StringTable += Symbol.getName();
550 StringTable += '\x00';
551 }
552
553 MachSymbolData MSD;
554 MSD.SymbolData = it;
555 MSD.StringIndex = Entry;
556
557 if (Symbol.isAbsolute()) {
558 MSD.SectionIndex = 0;
559 LocalSymbolData.push_back(MSD);
560 } else {
561 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
562 assert(MSD.SectionIndex && "Invalid section index!");
563 LocalSymbolData.push_back(MSD);
564 }
565 }
566
567 // External and undefined symbols are required to be in lexicographic order.
568 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
569 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
570
571 // Set the symbol indices.
572 Index = 0;
573 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
574 LocalSymbolData[i].SymbolData->setIndex(Index++);
575 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
576 ExternalSymbolData[i].SymbolData->setIndex(Index++);
577 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
578 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
579
580 // The string table is padded to a multiple of 4.
581 while (StringTable.size() % 4)
582 StringTable += '\x00';
583}
584
585void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
586 const MCAsmLayout &Layout) {
587 uint64_t StartAddress = 0;
588 const SmallVectorImpl<MCSectionData*> &Order = Layout.getSectionOrder();
589 for (int i = 0, n = Order.size(); i != n ; ++i) {
590 const MCSectionData *SD = Order[i];
591 StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment());
592 SectionAddress[SD] = StartAddress;
593 StartAddress += Layout.getSectionAddressSize(SD);
594
595 // Explicitly pad the section to match the alignment requirements of the
596 // following one. This is for 'gas' compatibility, it shouldn't
597 /// strictly be necessary.
598 StartAddress += getPaddingSize(SD, Layout);
599 }
600}
601
Jim Grosbach45d81bd2012-09-13 23:11:25 +0000602void MachObjectWriter::markAbsoluteVariableSymbols(MCAssembler &Asm,
603 const MCAsmLayout &Layout) {
604 for (MCAssembler::symbol_iterator i = Asm.symbol_begin(),
605 e = Asm.symbol_end();
606 i != e; ++i) {
607 MCSymbolData &SD = *i;
608 if (!SD.getSymbol().isVariable())
609 continue;
610
611 // Is the variable is a symbol difference (SA - SB + C) expression,
612 // and neither symbol is external, mark the variable as absolute.
613 const MCExpr *Expr = SD.getSymbol().getVariableValue();
614 MCValue Value;
615 if (Expr->EvaluateAsRelocatable(Value, Layout)) {
616 if (Value.getSymA() && Value.getSymB())
617 const_cast<MCSymbol*>(&SD.getSymbol())->setAbsolute();
618 }
619 }
620}
621
Bill Wendling3f2ea822011-06-23 00:09:43 +0000622void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
623 const MCAsmLayout &Layout) {
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000624 computeSectionAddresses(Asm, Layout);
625
626 // Create symbol data for any indirect symbols.
627 BindIndirectSymbols(Asm);
628
Jim Grosbach45d81bd2012-09-13 23:11:25 +0000629 // Mark symbol difference expressions in variables (from .set or = directives)
630 // as absolute.
631 markAbsoluteVariableSymbols(Asm, Layout);
632
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000633 // Compute symbol table information and bind symbol indices.
634 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
635 UndefinedSymbolData);
636}
637
Bill Wendling3f2ea822011-06-23 00:09:43 +0000638bool MachObjectWriter::
639IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
640 const MCSymbolData &DataA,
641 const MCFragment &FB,
642 bool InSet,
643 bool IsPCRel) const {
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000644 if (InSet)
645 return true;
646
647 // The effective address is
648 // addr(atom(A)) + offset(A)
649 // - addr(atom(B)) - offset(B)
650 // and the offsets are not relocatable, so the fixup is fully resolved when
651 // addr(atom(A)) - addr(atom(B)) == 0.
652 const MCSymbolData *A_Base = 0, *B_Base = 0;
653
654 const MCSymbol &SA = DataA.getSymbol().AliasedSymbol();
655 const MCSection &SecA = SA.getSection();
656 const MCSection &SecB = FB.getParent()->getSection();
657
658 if (IsPCRel) {
659 // The simple (Darwin, except on x86_64) way of dealing with this was to
660 // assume that any reference to a temporary symbol *must* be a temporary
661 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
662 // relocation to a temporary symbol (in the same section) is fully
663 // resolved. This also works in conjunction with absolutized .set, which
664 // requires the compiler to use .set to absolutize the differences between
665 // symbols which the compiler knows to be assembly time constants, so we
666 // don't need to worry about considering symbol differences fully resolved.
Jim Grosbach577b0912011-12-07 19:46:59 +0000667 //
668 // If the file isn't using sub-sections-via-symbols, we can make the
669 // same assumptions about any symbol that we normally make about
670 // assembler locals.
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000671
672 if (!Asm.getBackend().hasReliableSymbolDifference()) {
Jim Grosbach8b9300b2012-01-17 22:14:39 +0000673 if (!SA.isInSection() || &SecA != &SecB ||
Jim Grosbachec4ceb72012-01-18 21:54:12 +0000674 (!SA.isTemporary() &&
Jim Grosbachc389af92012-01-24 21:45:25 +0000675 FB.getAtom() != Asm.getSymbolData(SA).getFragment()->getAtom() &&
676 Asm.getSubsectionsViaSymbols()))
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000677 return false;
678 return true;
679 }
Kevin Enderby5afc1902011-09-08 20:53:44 +0000680 // For Darwin x86_64, there is one special case when the reference IsPCRel.
681 // If the fragment with the reference does not have a base symbol but meets
682 // the simple way of dealing with this, in that it is a temporary symbol in
683 // the same atom then it is assumed to be fully resolved. This is needed so
Eric Christopherd1e002a2011-09-08 22:17:40 +0000684 // a relocation entry is not created and so the static linker does not
Kevin Enderby5afc1902011-09-08 20:53:44 +0000685 // mess up the reference later.
686 else if(!FB.getAtom() &&
687 SA.isTemporary() && SA.isInSection() && &SecA == &SecB){
688 return true;
689 }
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000690 } else {
691 if (!TargetObjectWriter->useAggressiveSymbolFolding())
692 return false;
693 }
694
Benjamin Kramer0d46ccf2011-08-12 01:51:29 +0000695 const MCFragment *FA = Asm.getSymbolData(SA).getFragment();
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000696
Benjamin Kramer0d46ccf2011-08-12 01:51:29 +0000697 // Bail if the symbol has no fragment.
698 if (!FA)
699 return false;
700
701 A_Base = FA->getAtom();
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000702 if (!A_Base)
703 return false;
704
705 B_Base = FB.getAtom();
706 if (!B_Base)
707 return false;
708
709 // If the atoms are the same, they are guaranteed to have the same address.
710 if (A_Base == B_Base)
711 return true;
712
713 // Otherwise, we can't prove this is fully resolved.
714 return false;
715}
716
Eric Christopherd1e002a2011-09-08 22:17:40 +0000717void MachObjectWriter::WriteObject(MCAssembler &Asm,
Jim Grosbachf68a26b2011-12-06 00:13:09 +0000718 const MCAsmLayout &Layout) {
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000719 unsigned NumSections = Asm.size();
720
721 // The section data starts after the header, the segment load command (and
722 // section headers) and the symbol table.
723 unsigned NumLoadCommands = 1;
724 uint64_t LoadCommandsSize = is64Bit() ?
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000725 sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64):
726 sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000727
Daniel Dunbara94c3392013-01-18 01:26:07 +0000728 // Add the data-in-code load command size, if used.
729 unsigned NumDataRegions = Asm.getDataRegions().size();
730 if (NumDataRegions) {
731 ++NumLoadCommands;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000732 LoadCommandsSize += sizeof(MachO::linkedit_data_command);
Daniel Dunbara94c3392013-01-18 01:26:07 +0000733 }
734
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000735 // Add the symbol table load command sizes, if used.
736 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
737 UndefinedSymbolData.size();
738 if (NumSymbols) {
739 NumLoadCommands += 2;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000740 LoadCommandsSize += (sizeof(MachO::symtab_command) +
741 sizeof(MachO::dysymtab_command));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000742 }
743
Daniel Dunbara94c3392013-01-18 01:26:07 +0000744 // Add the linker option load commands sizes.
745 const std::vector<std::vector<std::string> > &LinkerOptions =
746 Asm.getLinkerOptions();
747 for (unsigned i = 0, e = LinkerOptions.size(); i != e; ++i) {
Jim Grosbach3e965312012-05-18 19:12:01 +0000748 ++NumLoadCommands;
Daniel Dunbar84920962013-01-22 03:42:49 +0000749 LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(LinkerOptions[i],
750 is64Bit());
Jim Grosbach3e965312012-05-18 19:12:01 +0000751 }
Daniel Dunbara94c3392013-01-18 01:26:07 +0000752
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000753 // Compute the total size of the section data, as well as its file size and vm
754 // size.
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000755 uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) :
756 sizeof(MachO::mach_header)) + LoadCommandsSize;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000757 uint64_t SectionDataSize = 0;
758 uint64_t SectionDataFileSize = 0;
759 uint64_t VMSize = 0;
760 for (MCAssembler::const_iterator it = Asm.begin(),
761 ie = Asm.end(); it != ie; ++it) {
762 const MCSectionData &SD = *it;
763 uint64_t Address = getSectionAddress(&SD);
764 uint64_t Size = Layout.getSectionAddressSize(&SD);
765 uint64_t FileSize = Layout.getSectionFileSize(&SD);
766 FileSize += getPaddingSize(&SD, Layout);
767
768 VMSize = std::max(VMSize, Address + Size);
769
770 if (SD.getSection().isVirtualSection())
771 continue;
772
773 SectionDataSize = std::max(SectionDataSize, Address + Size);
774 SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
775 }
776
777 // The section data is padded to 4 bytes.
778 //
779 // FIXME: Is this machine dependent?
780 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
781 SectionDataFileSize += SectionDataPadding;
782
783 // Write the prolog, starting with the header and load command...
784 WriteHeader(NumLoadCommands, LoadCommandsSize,
785 Asm.getSubsectionsViaSymbols());
786 WriteSegmentLoadCommand(NumSections, VMSize,
787 SectionDataStart, SectionDataSize);
788
789 // ... and then the section headers.
790 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
791 for (MCAssembler::const_iterator it = Asm.begin(),
792 ie = Asm.end(); it != ie; ++it) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000793 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000794 unsigned NumRelocs = Relocs.size();
795 uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
796 WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000797 RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000798 }
799
Jim Grosbach3e965312012-05-18 19:12:01 +0000800 // Write the data-in-code load command, if used.
801 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
802 if (NumDataRegions) {
803 uint64_t DataRegionsOffset = RelocTableEnd;
804 uint64_t DataRegionsSize = NumDataRegions * 8;
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000805 WriteLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset,
Jim Grosbach3e965312012-05-18 19:12:01 +0000806 DataRegionsSize);
807 }
808
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000809 // Write the symbol table load command, if used.
810 if (NumSymbols) {
811 unsigned FirstLocalSymbol = 0;
812 unsigned NumLocalSymbols = LocalSymbolData.size();
813 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
814 unsigned NumExternalSymbols = ExternalSymbolData.size();
815 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
816 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
817 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
818 unsigned NumSymTabSymbols =
819 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
820 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
821 uint64_t IndirectSymbolOffset = 0;
822
823 // If used, the indirect symbols are written after the section data.
824 if (NumIndirectSymbols)
Jim Grosbach3e965312012-05-18 19:12:01 +0000825 IndirectSymbolOffset = DataInCodeTableEnd;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000826
827 // The symbol table is written after the indirect symbol data.
Jim Grosbach3e965312012-05-18 19:12:01 +0000828 uint64_t SymbolTableOffset = DataInCodeTableEnd + IndirectSymbolSize;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000829
830 // The string table is written after symbol table.
831 uint64_t StringTableOffset =
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000832 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ?
833 sizeof(MachO::nlist_64) :
834 sizeof(MachO::nlist));
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000835 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
836 StringTableOffset, StringTable.size());
837
838 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
839 FirstExternalSymbol, NumExternalSymbols,
840 FirstUndefinedSymbol, NumUndefinedSymbols,
841 IndirectSymbolOffset, NumIndirectSymbols);
842 }
843
Daniel Dunbara94c3392013-01-18 01:26:07 +0000844 // Write the linker options load commands.
845 for (unsigned i = 0, e = LinkerOptions.size(); i != e; ++i) {
846 WriteLinkerOptionsLoadCommand(LinkerOptions[i]);
847 }
848
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000849 // Write the actual section data.
850 for (MCAssembler::const_iterator it = Asm.begin(),
851 ie = Asm.end(); it != ie; ++it) {
Jim Grosbachf77d5b12011-12-06 00:03:48 +0000852 Asm.writeSectionData(it, Layout);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000853
854 uint64_t Pad = getPaddingSize(it, Layout);
855 for (unsigned int i = 0; i < Pad; ++i)
856 Write8(0);
857 }
858
859 // Write the extra padding.
860 WriteZeros(SectionDataPadding);
861
862 // Write the relocation entries.
863 for (MCAssembler::const_iterator it = Asm.begin(),
864 ie = Asm.end(); it != ie; ++it) {
865 // Write the section relocation entries, in reverse order to match 'as'
866 // (approximately, the exact algorithm is more complicated than this).
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000867 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000868 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000869 Write32(Relocs[e - i - 1].r_word0);
870 Write32(Relocs[e - i - 1].r_word1);
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000871 }
872 }
873
Jim Grosbach3e965312012-05-18 19:12:01 +0000874 // Write out the data-in-code region payload, if there is one.
875 for (MCAssembler::const_data_region_iterator
876 it = Asm.data_region_begin(), ie = Asm.data_region_end();
877 it != ie; ++it) {
878 const DataRegionData *Data = &(*it);
Jim Grosbache75a9832012-09-18 23:05:12 +0000879 uint64_t Start =
880 getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->Start),
881 Layout);
882 uint64_t End =
883 getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->End),
884 Layout);
Jim Grosbach3e965312012-05-18 19:12:01 +0000885 DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
886 << " start: " << Start << "(" << Data->Start->getName() << ")"
887 << " end: " << End << "(" << Data->End->getName() << ")"
888 << " size: " << End - Start
889 << "\n");
890 Write32(Start);
891 Write16(End - Start);
892 Write16(Data->Kind);
893 }
894
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000895 // Write the symbol table data, if used.
896 if (NumSymbols) {
897 // Write the indirect symbol entries.
898 for (MCAssembler::const_indirect_symbol_iterator
899 it = Asm.indirect_symbol_begin(),
900 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
901 // Indirect symbols in the non lazy symbol pointer section have some
902 // special handling.
903 const MCSectionMachO &Section =
904 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
905 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
906 // If this symbol is defined and internal, mark it as such.
907 if (it->Symbol->isDefined() &&
908 !Asm.getSymbolData(*it->Symbol).isExternal()) {
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000909 uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000910 if (it->Symbol->isAbsolute())
Charles Davis9c3dd1b2013-08-27 05:00:43 +0000911 Flags |= MachO::INDIRECT_SYMBOL_ABS;
Bill Wendlingbbdffa92011-06-22 21:07:27 +0000912 Write32(Flags);
913 continue;
914 }
915 }
916
917 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
918 }
919
920 // FIXME: Check that offsets match computed ones.
921
922 // Write the symbol table entries.
923 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
924 WriteNlist(LocalSymbolData[i], Layout);
925 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
926 WriteNlist(ExternalSymbolData[i], Layout);
927 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
928 WriteNlist(UndefinedSymbolData[i], Layout);
929
930 // Write the string table.
931 OS << StringTable.str();
932 }
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000933}
934
Daniel Dunbarae5abd52010-12-16 16:09:19 +0000935MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000936 raw_ostream &OS,
Daniel Dunbar115a3dd2010-11-13 07:33:40 +0000937 bool IsLittleEndian) {
Daniel Dunbar5d05d972010-12-16 17:21:02 +0000938 return new MachObjectWriter(MOTW, OS, IsLittleEndian);
Daniel Dunbar2df4ceb2010-03-19 10:43:15 +0000939}