1 //===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCMachObjectWriter.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCFixupKindInfo.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSectionMachO.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCMachOSymbolFlags.h"
22 #include "llvm/MC/MCValue.h"
23 #include "llvm/Object/MachOFormat.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
29 using namespace llvm::object;
31 bool MachObjectWriter::
32 doesSymbolRequireExternRelocation(const MCSymbolData *SD) {
33 // Undefined symbols are always extern.
34 if (SD->Symbol->isUndefined())
37 // References to weak definitions require external relocation entries; the
38 // definition may not always be the one in the same object file.
39 if (SD->getFlags() & SF_WeakDefinition)
42 // Otherwise, we can use an internal relocation.
46 bool MachObjectWriter::
47 MachSymbolData::operator<(const MachSymbolData &RHS) const {
48 return SymbolData->getSymbol().getName() <
49 RHS.SymbolData->getSymbol().getName();
52 bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
53 const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo(
56 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
59 uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment,
60 const MCAsmLayout &Layout) const {
61 return getSectionAddress(Fragment->getParent()) +
62 Layout.getFragmentOffset(Fragment);
65 uint64_t MachObjectWriter::getSymbolAddress(const MCSymbolData* SD,
66 const MCAsmLayout &Layout) const {
67 const MCSymbol &S = SD->getSymbol();
69 // If this is a variable, then recursively evaluate now.
71 if (const MCConstantExpr *C =
72 dyn_cast<const MCConstantExpr>(S.getVariableValue()))
77 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, Layout))
78 report_fatal_error("unable to evaluate offset for variable '" +
81 // Verify that any used symbols are defined.
82 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
83 report_fatal_error("unable to evaluate offset to undefined symbol '" +
84 Target.getSymA()->getSymbol().getName() + "'");
85 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
86 report_fatal_error("unable to evaluate offset to undefined symbol '" +
87 Target.getSymB()->getSymbol().getName() + "'");
89 uint64_t Address = Target.getConstant();
91 Address += getSymbolAddress(&Layout.getAssembler().getSymbolData(
92 Target.getSymA()->getSymbol()), Layout);
94 Address += getSymbolAddress(&Layout.getAssembler().getSymbolData(
95 Target.getSymB()->getSymbol()), Layout);
99 return getSectionAddress(SD->getFragment()->getParent()) +
100 Layout.getSymbolOffset(SD);
103 uint64_t MachObjectWriter::getPaddingSize(const MCSectionData *SD,
104 const MCAsmLayout &Layout) const {
105 uint64_t EndAddr = getSectionAddress(SD) + Layout.getSectionAddressSize(SD);
106 unsigned Next = SD->getLayoutOrder() + 1;
107 if (Next >= Layout.getSectionOrder().size())
110 const MCSectionData &NextSD = *Layout.getSectionOrder()[Next];
111 if (NextSD.getSection().isVirtualSection())
113 return OffsetToAlignment(EndAddr, NextSD.getAlignment());
116 void MachObjectWriter::WriteHeader(unsigned NumLoadCommands,
117 unsigned LoadCommandsSize,
118 bool SubsectionsViaSymbols) {
121 if (SubsectionsViaSymbols)
122 Flags |= macho::HF_SubsectionsViaSymbols;
124 // struct mach_header (28 bytes) or
125 // struct mach_header_64 (32 bytes)
127 uint64_t Start = OS.tell();
130 Write32(is64Bit() ? macho::HM_Object64 : macho::HM_Object32);
132 Write32(TargetObjectWriter->getCPUType());
133 Write32(TargetObjectWriter->getCPUSubtype());
135 Write32(macho::HFT_Object);
136 Write32(NumLoadCommands);
137 Write32(LoadCommandsSize);
140 Write32(0); // reserved
142 assert(OS.tell() - Start ==
143 (is64Bit() ? macho::Header64Size : macho::Header32Size));
146 /// WriteSegmentLoadCommand - Write a segment load command.
148 /// \param NumSections The number of sections in this segment.
149 /// \param SectionDataSize The total size of the sections.
150 void MachObjectWriter::WriteSegmentLoadCommand(unsigned NumSections,
152 uint64_t SectionDataStartOffset,
153 uint64_t SectionDataSize) {
154 // struct segment_command (56 bytes) or
155 // struct segment_command_64 (72 bytes)
157 uint64_t Start = OS.tell();
160 unsigned SegmentLoadCommandSize =
161 is64Bit() ? macho::SegmentLoadCommand64Size:
162 macho::SegmentLoadCommand32Size;
163 Write32(is64Bit() ? macho::LCT_Segment64 : macho::LCT_Segment);
164 Write32(SegmentLoadCommandSize +
165 NumSections * (is64Bit() ? macho::Section64Size :
166 macho::Section32Size));
170 Write64(0); // vmaddr
171 Write64(VMSize); // vmsize
172 Write64(SectionDataStartOffset); // file offset
173 Write64(SectionDataSize); // file size
175 Write32(0); // vmaddr
176 Write32(VMSize); // vmsize
177 Write32(SectionDataStartOffset); // file offset
178 Write32(SectionDataSize); // file size
180 Write32(0x7); // maxprot
181 Write32(0x7); // initprot
182 Write32(NumSections);
185 assert(OS.tell() - Start == SegmentLoadCommandSize);
188 void MachObjectWriter::WriteSection(const MCAssembler &Asm,
189 const MCAsmLayout &Layout,
190 const MCSectionData &SD,
192 uint64_t RelocationsStart,
193 unsigned NumRelocations) {
194 uint64_t SectionSize = Layout.getSectionAddressSize(&SD);
196 // The offset is unused for virtual sections.
197 if (SD.getSection().isVirtualSection()) {
198 assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!");
202 // struct section (68 bytes) or
203 // struct section_64 (80 bytes)
205 uint64_t Start = OS.tell();
208 const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
209 WriteBytes(Section.getSectionName(), 16);
210 WriteBytes(Section.getSegmentName(), 16);
212 Write64(getSectionAddress(&SD)); // address
213 Write64(SectionSize); // size
215 Write32(getSectionAddress(&SD)); // address
216 Write32(SectionSize); // size
220 unsigned Flags = Section.getTypeAndAttributes();
221 if (SD.hasInstructions())
222 Flags |= MCSectionMachO::S_ATTR_SOME_INSTRUCTIONS;
224 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
225 Write32(Log2_32(SD.getAlignment()));
226 Write32(NumRelocations ? RelocationsStart : 0);
227 Write32(NumRelocations);
229 Write32(IndirectSymBase.lookup(&SD)); // reserved1
230 Write32(Section.getStubSize()); // reserved2
232 Write32(0); // reserved3
234 assert(OS.tell() - Start == (is64Bit() ? macho::Section64Size :
235 macho::Section32Size));
238 void MachObjectWriter::WriteSymtabLoadCommand(uint32_t SymbolOffset,
240 uint32_t StringTableOffset,
241 uint32_t StringTableSize) {
242 // struct symtab_command (24 bytes)
244 uint64_t Start = OS.tell();
247 Write32(macho::LCT_Symtab);
248 Write32(macho::SymtabLoadCommandSize);
249 Write32(SymbolOffset);
251 Write32(StringTableOffset);
252 Write32(StringTableSize);
254 assert(OS.tell() - Start == macho::SymtabLoadCommandSize);
257 void MachObjectWriter::WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
258 uint32_t NumLocalSymbols,
259 uint32_t FirstExternalSymbol,
260 uint32_t NumExternalSymbols,
261 uint32_t FirstUndefinedSymbol,
262 uint32_t NumUndefinedSymbols,
263 uint32_t IndirectSymbolOffset,
264 uint32_t NumIndirectSymbols) {
265 // struct dysymtab_command (80 bytes)
267 uint64_t Start = OS.tell();
270 Write32(macho::LCT_Dysymtab);
271 Write32(macho::DysymtabLoadCommandSize);
272 Write32(FirstLocalSymbol);
273 Write32(NumLocalSymbols);
274 Write32(FirstExternalSymbol);
275 Write32(NumExternalSymbols);
276 Write32(FirstUndefinedSymbol);
277 Write32(NumUndefinedSymbols);
278 Write32(0); // tocoff
280 Write32(0); // modtaboff
281 Write32(0); // nmodtab
282 Write32(0); // extrefsymoff
283 Write32(0); // nextrefsyms
284 Write32(IndirectSymbolOffset);
285 Write32(NumIndirectSymbols);
286 Write32(0); // extreloff
287 Write32(0); // nextrel
288 Write32(0); // locreloff
289 Write32(0); // nlocrel
291 assert(OS.tell() - Start == macho::DysymtabLoadCommandSize);
294 void MachObjectWriter::WriteNlist(MachSymbolData &MSD,
295 const MCAsmLayout &Layout) {
296 MCSymbolData &Data = *MSD.SymbolData;
297 const MCSymbol &Symbol = Data.getSymbol();
299 uint16_t Flags = Data.getFlags();
300 uint64_t Address = 0;
302 // Set the N_TYPE bits. See <mach-o/nlist.h>.
304 // FIXME: Are the prebound or indirect fields possible here?
305 if (Symbol.isUndefined())
306 Type = macho::STT_Undefined;
307 else if (Symbol.isAbsolute())
308 Type = macho::STT_Absolute;
310 Type = macho::STT_Section;
312 // FIXME: Set STAB bits.
314 if (Data.isPrivateExtern())
315 Type |= macho::STF_PrivateExtern;
318 if (Data.isExternal() || Symbol.isUndefined())
319 Type |= macho::STF_External;
321 // Compute the symbol address.
322 if (Symbol.isDefined()) {
323 Address = getSymbolAddress(&Data, Layout);
324 } else if (Data.isCommon()) {
325 // Common symbols are encoded with the size in the address
326 // field, and their alignment in the flags.
327 Address = Data.getCommonSize();
329 // Common alignment is packed into the 'desc' bits.
330 if (unsigned Align = Data.getCommonAlignment()) {
331 unsigned Log2Size = Log2_32(Align);
332 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
334 report_fatal_error("invalid 'common' alignment '" +
336 // FIXME: Keep this mask with the SymbolFlags enumeration.
337 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
341 // struct nlist (12 bytes)
343 Write32(MSD.StringIndex);
345 Write8(MSD.SectionIndex);
347 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
356 void MachObjectWriter::WriteLinkeditLoadCommand(uint32_t Type,
359 uint64_t Start = OS.tell();
363 Write32(macho::LinkeditLoadCommandSize);
367 assert(OS.tell() - Start == macho::LinkeditLoadCommandSize);
371 void MachObjectWriter::RecordRelocation(const MCAssembler &Asm,
372 const MCAsmLayout &Layout,
373 const MCFragment *Fragment,
374 const MCFixup &Fixup,
376 uint64_t &FixedValue) {
377 TargetObjectWriter->RecordRelocation(this, Asm, Layout, Fragment, Fixup,
381 void MachObjectWriter::BindIndirectSymbols(MCAssembler &Asm) {
382 // This is the point where 'as' creates actual symbols for indirect symbols
383 // (in the following two passes). It would be easier for us to do this sooner
384 // when we see the attribute, but that makes getting the order in the symbol
385 // table much more complicated than it is worth.
387 // FIXME: Revisit this when the dust settles.
389 // Bind non lazy symbol pointers first.
390 unsigned IndirectIndex = 0;
391 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
392 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
393 const MCSectionMachO &Section =
394 cast<MCSectionMachO>(it->SectionData->getSection());
396 if (Section.getType() != MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS)
399 // Initialize the section indirect symbol base, if necessary.
400 IndirectSymBase.insert(std::make_pair(it->SectionData, IndirectIndex));
402 Asm.getOrCreateSymbolData(*it->Symbol);
405 // Then lazy symbol pointers and symbol stubs.
407 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
408 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
409 const MCSectionMachO &Section =
410 cast<MCSectionMachO>(it->SectionData->getSection());
412 if (Section.getType() != MCSectionMachO::S_LAZY_SYMBOL_POINTERS &&
413 Section.getType() != MCSectionMachO::S_SYMBOL_STUBS)
416 // Initialize the section indirect symbol base, if necessary.
417 IndirectSymBase.insert(std::make_pair(it->SectionData, IndirectIndex));
419 // Set the symbol type to undefined lazy, but only on construction.
421 // FIXME: Do not hardcode.
423 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
425 Entry.setFlags(Entry.getFlags() | 0x0001);
429 /// ComputeSymbolTable - Compute the symbol table data
431 /// \param StringTable [out] - The string table data.
432 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
434 void MachObjectWriter::
435 ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
436 std::vector<MachSymbolData> &LocalSymbolData,
437 std::vector<MachSymbolData> &ExternalSymbolData,
438 std::vector<MachSymbolData> &UndefinedSymbolData) {
439 // Build section lookup table.
440 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
442 for (MCAssembler::iterator it = Asm.begin(),
443 ie = Asm.end(); it != ie; ++it, ++Index)
444 SectionIndexMap[&it->getSection()] = Index;
445 assert(Index <= 256 && "Too many sections!");
447 // Index 0 is always the empty string.
448 StringMap<uint64_t> StringIndexMap;
449 StringTable += '\x00';
451 // Build the symbol arrays and the string table, but only for non-local
454 // The particular order that we collect the symbols and create the string
455 // table, then sort the symbols is chosen to match 'as'. Even though it
456 // doesn't matter for correctness, this is important for letting us diff .o
458 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
459 ie = Asm.symbol_end(); it != ie; ++it) {
460 const MCSymbol &Symbol = it->getSymbol();
462 // Ignore non-linker visible symbols.
463 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
466 if (!it->isExternal() && !Symbol.isUndefined())
469 uint64_t &Entry = StringIndexMap[Symbol.getName()];
471 Entry = StringTable.size();
472 StringTable += Symbol.getName();
473 StringTable += '\x00';
478 MSD.StringIndex = Entry;
480 if (Symbol.isUndefined()) {
481 MSD.SectionIndex = 0;
482 UndefinedSymbolData.push_back(MSD);
483 } else if (Symbol.isAbsolute()) {
484 MSD.SectionIndex = 0;
485 ExternalSymbolData.push_back(MSD);
487 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
488 assert(MSD.SectionIndex && "Invalid section index!");
489 ExternalSymbolData.push_back(MSD);
493 // Now add the data for local symbols.
494 for (MCAssembler::symbol_iterator it = Asm.symbol_begin(),
495 ie = Asm.symbol_end(); it != ie; ++it) {
496 const MCSymbol &Symbol = it->getSymbol();
498 // Ignore non-linker visible symbols.
499 if (!Asm.isSymbolLinkerVisible(it->getSymbol()))
502 if (it->isExternal() || Symbol.isUndefined())
505 uint64_t &Entry = StringIndexMap[Symbol.getName()];
507 Entry = StringTable.size();
508 StringTable += Symbol.getName();
509 StringTable += '\x00';
514 MSD.StringIndex = Entry;
516 if (Symbol.isAbsolute()) {
517 MSD.SectionIndex = 0;
518 LocalSymbolData.push_back(MSD);
520 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
521 assert(MSD.SectionIndex && "Invalid section index!");
522 LocalSymbolData.push_back(MSD);
526 // External and undefined symbols are required to be in lexicographic order.
527 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
528 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
530 // Set the symbol indices.
532 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
533 LocalSymbolData[i].SymbolData->setIndex(Index++);
534 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
535 ExternalSymbolData[i].SymbolData->setIndex(Index++);
536 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
537 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
539 // The string table is padded to a multiple of 4.
540 while (StringTable.size() % 4)
541 StringTable += '\x00';
544 void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
545 const MCAsmLayout &Layout) {
546 uint64_t StartAddress = 0;
547 const SmallVectorImpl<MCSectionData*> &Order = Layout.getSectionOrder();
548 for (int i = 0, n = Order.size(); i != n ; ++i) {
549 const MCSectionData *SD = Order[i];
550 StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment());
551 SectionAddress[SD] = StartAddress;
552 StartAddress += Layout.getSectionAddressSize(SD);
554 // Explicitly pad the section to match the alignment requirements of the
555 // following one. This is for 'gas' compatibility, it shouldn't
556 /// strictly be necessary.
557 StartAddress += getPaddingSize(SD, Layout);
561 void MachObjectWriter::markAbsoluteVariableSymbols(MCAssembler &Asm,
562 const MCAsmLayout &Layout) {
563 for (MCAssembler::symbol_iterator i = Asm.symbol_begin(),
564 e = Asm.symbol_end();
566 MCSymbolData &SD = *i;
567 if (!SD.getSymbol().isVariable())
570 // Is the variable is a symbol difference (SA - SB + C) expression,
571 // and neither symbol is external, mark the variable as absolute.
572 const MCExpr *Expr = SD.getSymbol().getVariableValue();
574 if (Expr->EvaluateAsRelocatable(Value, Layout)) {
575 if (Value.getSymA() && Value.getSymB())
576 const_cast<MCSymbol*>(&SD.getSymbol())->setAbsolute();
581 void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
582 const MCAsmLayout &Layout) {
583 computeSectionAddresses(Asm, Layout);
585 // Create symbol data for any indirect symbols.
586 BindIndirectSymbols(Asm);
588 // Mark symbol difference expressions in variables (from .set or = directives)
590 markAbsoluteVariableSymbols(Asm, Layout);
592 // Compute symbol table information and bind symbol indices.
593 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
594 UndefinedSymbolData);
597 bool MachObjectWriter::
598 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
599 const MCSymbolData &DataA,
600 const MCFragment &FB,
602 bool IsPCRel) const {
606 // The effective address is
607 // addr(atom(A)) + offset(A)
608 // - addr(atom(B)) - offset(B)
609 // and the offsets are not relocatable, so the fixup is fully resolved when
610 // addr(atom(A)) - addr(atom(B)) == 0.
611 const MCSymbolData *A_Base = 0, *B_Base = 0;
613 const MCSymbol &SA = DataA.getSymbol().AliasedSymbol();
614 const MCSection &SecA = SA.getSection();
615 const MCSection &SecB = FB.getParent()->getSection();
618 // The simple (Darwin, except on x86_64) way of dealing with this was to
619 // assume that any reference to a temporary symbol *must* be a temporary
620 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
621 // relocation to a temporary symbol (in the same section) is fully
622 // resolved. This also works in conjunction with absolutized .set, which
623 // requires the compiler to use .set to absolutize the differences between
624 // symbols which the compiler knows to be assembly time constants, so we
625 // don't need to worry about considering symbol differences fully resolved.
627 // If the file isn't using sub-sections-via-symbols, we can make the
628 // same assumptions about any symbol that we normally make about
631 if (!Asm.getBackend().hasReliableSymbolDifference()) {
632 if (!SA.isInSection() || &SecA != &SecB ||
633 (!SA.isTemporary() &&
634 FB.getAtom() != Asm.getSymbolData(SA).getFragment()->getAtom() &&
635 Asm.getSubsectionsViaSymbols()))
639 // For Darwin x86_64, there is one special case when the reference IsPCRel.
640 // If the fragment with the reference does not have a base symbol but meets
641 // the simple way of dealing with this, in that it is a temporary symbol in
642 // the same atom then it is assumed to be fully resolved. This is needed so
643 // a relocation entry is not created and so the static linker does not
644 // mess up the reference later.
645 else if(!FB.getAtom() &&
646 SA.isTemporary() && SA.isInSection() && &SecA == &SecB){
650 if (!TargetObjectWriter->useAggressiveSymbolFolding())
654 const MCFragment *FA = Asm.getSymbolData(SA).getFragment();
656 // Bail if the symbol has no fragment.
660 A_Base = FA->getAtom();
664 B_Base = FB.getAtom();
668 // If the atoms are the same, they are guaranteed to have the same address.
669 if (A_Base == B_Base)
672 // Otherwise, we can't prove this is fully resolved.
676 void MachObjectWriter::WriteObject(MCAssembler &Asm,
677 const MCAsmLayout &Layout) {
678 unsigned NumSections = Asm.size();
680 // The section data starts after the header, the segment load command (and
681 // section headers) and the symbol table.
682 unsigned NumLoadCommands = 1;
683 uint64_t LoadCommandsSize = is64Bit() ?
684 macho::SegmentLoadCommand64Size + NumSections * macho::Section64Size :
685 macho::SegmentLoadCommand32Size + NumSections * macho::Section32Size;
687 // Add the symbol table load command sizes, if used.
688 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
689 UndefinedSymbolData.size();
691 NumLoadCommands += 2;
692 LoadCommandsSize += (macho::SymtabLoadCommandSize +
693 macho::DysymtabLoadCommandSize);
696 // Add the data-in-code load command size, if used.
697 unsigned NumDataRegions = Asm.getDataRegions().size();
698 if (NumDataRegions) {
700 LoadCommandsSize += macho::LinkeditLoadCommandSize;
703 // Compute the total size of the section data, as well as its file size and vm
705 uint64_t SectionDataStart = (is64Bit() ? macho::Header64Size :
706 macho::Header32Size) + LoadCommandsSize;
707 uint64_t SectionDataSize = 0;
708 uint64_t SectionDataFileSize = 0;
710 for (MCAssembler::const_iterator it = Asm.begin(),
711 ie = Asm.end(); it != ie; ++it) {
712 const MCSectionData &SD = *it;
713 uint64_t Address = getSectionAddress(&SD);
714 uint64_t Size = Layout.getSectionAddressSize(&SD);
715 uint64_t FileSize = Layout.getSectionFileSize(&SD);
716 FileSize += getPaddingSize(&SD, Layout);
718 VMSize = std::max(VMSize, Address + Size);
720 if (SD.getSection().isVirtualSection())
723 SectionDataSize = std::max(SectionDataSize, Address + Size);
724 SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
727 // The section data is padded to 4 bytes.
729 // FIXME: Is this machine dependent?
730 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
731 SectionDataFileSize += SectionDataPadding;
733 // Write the prolog, starting with the header and load command...
734 WriteHeader(NumLoadCommands, LoadCommandsSize,
735 Asm.getSubsectionsViaSymbols());
736 WriteSegmentLoadCommand(NumSections, VMSize,
737 SectionDataStart, SectionDataSize);
739 // ... and then the section headers.
740 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
741 for (MCAssembler::const_iterator it = Asm.begin(),
742 ie = Asm.end(); it != ie; ++it) {
743 std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
744 unsigned NumRelocs = Relocs.size();
745 uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
746 WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
747 RelocTableEnd += NumRelocs * macho::RelocationInfoSize;
750 // Write the data-in-code load command, if used.
751 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
752 if (NumDataRegions) {
753 uint64_t DataRegionsOffset = RelocTableEnd;
754 uint64_t DataRegionsSize = NumDataRegions * 8;
755 WriteLinkeditLoadCommand(macho::LCT_DataInCode, DataRegionsOffset,
759 // Write the symbol table load command, if used.
761 unsigned FirstLocalSymbol = 0;
762 unsigned NumLocalSymbols = LocalSymbolData.size();
763 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
764 unsigned NumExternalSymbols = ExternalSymbolData.size();
765 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
766 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
767 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
768 unsigned NumSymTabSymbols =
769 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
770 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
771 uint64_t IndirectSymbolOffset = 0;
773 // If used, the indirect symbols are written after the section data.
774 if (NumIndirectSymbols)
775 IndirectSymbolOffset = DataInCodeTableEnd;
777 // The symbol table is written after the indirect symbol data.
778 uint64_t SymbolTableOffset = DataInCodeTableEnd + IndirectSymbolSize;
780 // The string table is written after symbol table.
781 uint64_t StringTableOffset =
782 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? macho::Nlist64Size :
784 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
785 StringTableOffset, StringTable.size());
787 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
788 FirstExternalSymbol, NumExternalSymbols,
789 FirstUndefinedSymbol, NumUndefinedSymbols,
790 IndirectSymbolOffset, NumIndirectSymbols);
793 // Write the actual section data.
794 for (MCAssembler::const_iterator it = Asm.begin(),
795 ie = Asm.end(); it != ie; ++it) {
796 Asm.writeSectionData(it, Layout);
798 uint64_t Pad = getPaddingSize(it, Layout);
799 for (unsigned int i = 0; i < Pad; ++i)
803 // Write the extra padding.
804 WriteZeros(SectionDataPadding);
806 // Write the relocation entries.
807 for (MCAssembler::const_iterator it = Asm.begin(),
808 ie = Asm.end(); it != ie; ++it) {
809 // Write the section relocation entries, in reverse order to match 'as'
810 // (approximately, the exact algorithm is more complicated than this).
811 std::vector<macho::RelocationEntry> &Relocs = Relocations[it];
812 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
813 Write32(Relocs[e - i - 1].Word0);
814 Write32(Relocs[e - i - 1].Word1);
818 // Write out the data-in-code region payload, if there is one.
819 for (MCAssembler::const_data_region_iterator
820 it = Asm.data_region_begin(), ie = Asm.data_region_end();
822 const DataRegionData *Data = &(*it);
824 getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->Start),
827 getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->End),
829 DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
830 << " start: " << Start << "(" << Data->Start->getName() << ")"
831 << " end: " << End << "(" << Data->End->getName() << ")"
832 << " size: " << End - Start
835 Write16(End - Start);
839 // Write the symbol table data, if used.
841 // Write the indirect symbol entries.
842 for (MCAssembler::const_indirect_symbol_iterator
843 it = Asm.indirect_symbol_begin(),
844 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
845 // Indirect symbols in the non lazy symbol pointer section have some
847 const MCSectionMachO &Section =
848 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
849 if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) {
850 // If this symbol is defined and internal, mark it as such.
851 if (it->Symbol->isDefined() &&
852 !Asm.getSymbolData(*it->Symbol).isExternal()) {
853 uint32_t Flags = macho::ISF_Local;
854 if (it->Symbol->isAbsolute())
855 Flags |= macho::ISF_Absolute;
861 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
864 // FIXME: Check that offsets match computed ones.
866 // Write the symbol table entries.
867 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
868 WriteNlist(LocalSymbolData[i], Layout);
869 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
870 WriteNlist(ExternalSymbolData[i], Layout);
871 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
872 WriteNlist(UndefinedSymbolData[i], Layout);
874 // Write the string table.
875 OS << StringTable.str();
879 MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
881 bool IsLittleEndian) {
882 return new MachObjectWriter(MOTW, OS, IsLittleEndian);