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/MCAsmBackend.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCFixupKindInfo.h"
18 #include "llvm/MC/MCMachOSymbolFlags.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/MC/MCValue.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MachO.h"
29 #define DEBUG_TYPE "mc"
31 void MachObjectWriter::reset() {
33 IndirectSymBase.clear();
35 LocalSymbolData.clear();
36 ExternalSymbolData.clear();
37 UndefinedSymbolData.clear();
38 MCObjectWriter::reset();
41 bool MachObjectWriter::
42 doesSymbolRequireExternRelocation(const MCSymbolData *SD) {
43 // Undefined symbols are always extern.
44 if (SD->Symbol->isUndefined())
47 // References to weak definitions require external relocation entries; the
48 // definition may not always be the one in the same object file.
49 if (SD->getFlags() & SF_WeakDefinition)
52 // Otherwise, we can use an internal relocation.
56 bool MachObjectWriter::
57 MachSymbolData::operator<(const MachSymbolData &RHS) const {
58 return SymbolData->getSymbol().getName() <
59 RHS.SymbolData->getSymbol().getName();
62 bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
63 const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo(
66 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
69 uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment,
70 const MCAsmLayout &Layout) const {
71 return getSectionAddress(Fragment->getParent()) +
72 Layout.getFragmentOffset(Fragment);
75 uint64_t MachObjectWriter::getSymbolAddress(const MCSymbolData* SD,
76 const MCAsmLayout &Layout) const {
77 const MCSymbol &S = SD->getSymbol();
79 // If this is a variable, then recursively evaluate now.
81 if (const MCConstantExpr *C =
82 dyn_cast<const MCConstantExpr>(S.getVariableValue()))
87 if (!S.getVariableValue()->EvaluateAsRelocatable(Target, &Layout, nullptr))
88 report_fatal_error("unable to evaluate offset for variable '" +
91 // Verify that any used symbols are defined.
92 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
93 report_fatal_error("unable to evaluate offset to undefined symbol '" +
94 Target.getSymA()->getSymbol().getName() + "'");
95 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
96 report_fatal_error("unable to evaluate offset to undefined symbol '" +
97 Target.getSymB()->getSymbol().getName() + "'");
99 uint64_t Address = Target.getConstant();
100 if (Target.getSymA())
101 Address += getSymbolAddress(&Layout.getAssembler().getSymbolData(
102 Target.getSymA()->getSymbol()), Layout);
103 if (Target.getSymB())
104 Address += getSymbolAddress(&Layout.getAssembler().getSymbolData(
105 Target.getSymB()->getSymbol()), Layout);
109 return getSectionAddress(SD->getFragment()->getParent()) +
110 Layout.getSymbolOffset(SD);
113 uint64_t MachObjectWriter::getPaddingSize(const MCSectionData *SD,
114 const MCAsmLayout &Layout) const {
115 uint64_t EndAddr = getSectionAddress(SD) + Layout.getSectionAddressSize(SD);
116 unsigned Next = SD->getLayoutOrder() + 1;
117 if (Next >= Layout.getSectionOrder().size())
120 const MCSectionData &NextSD = *Layout.getSectionOrder()[Next];
121 if (NextSD.getSection().isVirtualSection())
123 return OffsetToAlignment(EndAddr, NextSD.getAlignment());
126 void MachObjectWriter::WriteHeader(unsigned NumLoadCommands,
127 unsigned LoadCommandsSize,
128 bool SubsectionsViaSymbols) {
131 if (SubsectionsViaSymbols)
132 Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
134 // struct mach_header (28 bytes) or
135 // struct mach_header_64 (32 bytes)
137 uint64_t Start = OS.tell();
140 Write32(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC);
142 Write32(TargetObjectWriter->getCPUType());
143 Write32(TargetObjectWriter->getCPUSubtype());
145 Write32(MachO::MH_OBJECT);
146 Write32(NumLoadCommands);
147 Write32(LoadCommandsSize);
150 Write32(0); // reserved
152 assert(OS.tell() - Start ==
153 (is64Bit()?sizeof(MachO::mach_header_64): sizeof(MachO::mach_header)));
156 /// WriteSegmentLoadCommand - Write a segment load command.
158 /// \param NumSections The number of sections in this segment.
159 /// \param SectionDataSize The total size of the sections.
160 void MachObjectWriter::WriteSegmentLoadCommand(unsigned NumSections,
162 uint64_t SectionDataStartOffset,
163 uint64_t SectionDataSize) {
164 // struct segment_command (56 bytes) or
165 // struct segment_command_64 (72 bytes)
167 uint64_t Start = OS.tell();
170 unsigned SegmentLoadCommandSize =
171 is64Bit() ? sizeof(MachO::segment_command_64):
172 sizeof(MachO::segment_command);
173 Write32(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
174 Write32(SegmentLoadCommandSize +
175 NumSections * (is64Bit() ? sizeof(MachO::section_64) :
176 sizeof(MachO::section)));
180 Write64(0); // vmaddr
181 Write64(VMSize); // vmsize
182 Write64(SectionDataStartOffset); // file offset
183 Write64(SectionDataSize); // file size
185 Write32(0); // vmaddr
186 Write32(VMSize); // vmsize
187 Write32(SectionDataStartOffset); // file offset
188 Write32(SectionDataSize); // file size
191 Write32(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE);
193 Write32(MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE);
194 Write32(NumSections);
197 assert(OS.tell() - Start == SegmentLoadCommandSize);
200 void MachObjectWriter::WriteSection(const MCAssembler &Asm,
201 const MCAsmLayout &Layout,
202 const MCSectionData &SD,
204 uint64_t RelocationsStart,
205 unsigned NumRelocations) {
206 uint64_t SectionSize = Layout.getSectionAddressSize(&SD);
208 // The offset is unused for virtual sections.
209 if (SD.getSection().isVirtualSection()) {
210 assert(Layout.getSectionFileSize(&SD) == 0 && "Invalid file size!");
214 // struct section (68 bytes) or
215 // struct section_64 (80 bytes)
217 uint64_t Start = OS.tell();
220 const MCSectionMachO &Section = cast<MCSectionMachO>(SD.getSection());
221 WriteBytes(Section.getSectionName(), 16);
222 WriteBytes(Section.getSegmentName(), 16);
224 Write64(getSectionAddress(&SD)); // address
225 Write64(SectionSize); // size
227 Write32(getSectionAddress(&SD)); // address
228 Write32(SectionSize); // size
232 unsigned Flags = Section.getTypeAndAttributes();
233 if (SD.hasInstructions())
234 Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS;
236 assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
237 Write32(Log2_32(SD.getAlignment()));
238 Write32(NumRelocations ? RelocationsStart : 0);
239 Write32(NumRelocations);
241 Write32(IndirectSymBase.lookup(&SD)); // reserved1
242 Write32(Section.getStubSize()); // reserved2
244 Write32(0); // reserved3
246 assert(OS.tell() - Start == (is64Bit() ? sizeof(MachO::section_64) :
247 sizeof(MachO::section)));
250 void MachObjectWriter::WriteSymtabLoadCommand(uint32_t SymbolOffset,
252 uint32_t StringTableOffset,
253 uint32_t StringTableSize) {
254 // struct symtab_command (24 bytes)
256 uint64_t Start = OS.tell();
259 Write32(MachO::LC_SYMTAB);
260 Write32(sizeof(MachO::symtab_command));
261 Write32(SymbolOffset);
263 Write32(StringTableOffset);
264 Write32(StringTableSize);
266 assert(OS.tell() - Start == sizeof(MachO::symtab_command));
269 void MachObjectWriter::WriteDysymtabLoadCommand(uint32_t FirstLocalSymbol,
270 uint32_t NumLocalSymbols,
271 uint32_t FirstExternalSymbol,
272 uint32_t NumExternalSymbols,
273 uint32_t FirstUndefinedSymbol,
274 uint32_t NumUndefinedSymbols,
275 uint32_t IndirectSymbolOffset,
276 uint32_t NumIndirectSymbols) {
277 // struct dysymtab_command (80 bytes)
279 uint64_t Start = OS.tell();
282 Write32(MachO::LC_DYSYMTAB);
283 Write32(sizeof(MachO::dysymtab_command));
284 Write32(FirstLocalSymbol);
285 Write32(NumLocalSymbols);
286 Write32(FirstExternalSymbol);
287 Write32(NumExternalSymbols);
288 Write32(FirstUndefinedSymbol);
289 Write32(NumUndefinedSymbols);
290 Write32(0); // tocoff
292 Write32(0); // modtaboff
293 Write32(0); // nmodtab
294 Write32(0); // extrefsymoff
295 Write32(0); // nextrefsyms
296 Write32(IndirectSymbolOffset);
297 Write32(NumIndirectSymbols);
298 Write32(0); // extreloff
299 Write32(0); // nextrel
300 Write32(0); // locreloff
301 Write32(0); // nlocrel
303 assert(OS.tell() - Start == sizeof(MachO::dysymtab_command));
306 MachObjectWriter::MachSymbolData *
307 MachObjectWriter::findSymbolData(const MCSymbol &Sym) {
308 for (auto &Entry : LocalSymbolData)
309 if (&Entry.SymbolData->getSymbol() == &Sym)
312 for (auto &Entry : ExternalSymbolData)
313 if (&Entry.SymbolData->getSymbol() == &Sym)
316 for (auto &Entry : UndefinedSymbolData)
317 if (&Entry.SymbolData->getSymbol() == &Sym)
323 void MachObjectWriter::WriteNlist(MachSymbolData &MSD,
324 const MCAsmLayout &Layout) {
325 MCSymbolData &Data = *MSD.SymbolData;
326 const MCSymbol *Symbol = &Data.getSymbol();
327 const MCSymbol *AliasedSymbol = &Symbol->AliasedSymbol();
328 uint8_t SectionIndex = MSD.SectionIndex;
330 uint16_t Flags = Data.getFlags();
331 uint64_t Address = 0;
332 bool IsAlias = Symbol != AliasedSymbol;
334 MachSymbolData *AliaseeInfo;
336 AliaseeInfo = findSymbolData(*AliasedSymbol);
338 SectionIndex = AliaseeInfo->SectionIndex;
339 Symbol = AliasedSymbol;
342 // Set the N_TYPE bits. See <mach-o/nlist.h>.
344 // FIXME: Are the prebound or indirect fields possible here?
345 if (IsAlias && Symbol->isUndefined())
346 Type = MachO::N_INDR;
347 else if (Symbol->isUndefined())
348 Type = MachO::N_UNDF;
349 else if (Symbol->isAbsolute())
352 Type = MachO::N_SECT;
354 // FIXME: Set STAB bits.
356 if (Data.isPrivateExtern())
357 Type |= MachO::N_PEXT;
360 if (Data.isExternal() || (!IsAlias && Symbol->isUndefined()))
361 Type |= MachO::N_EXT;
363 // Compute the symbol address.
364 if (IsAlias && Symbol->isUndefined())
365 Address = AliaseeInfo->StringIndex;
366 else if (Symbol->isDefined())
367 Address = getSymbolAddress(&Data, Layout);
368 else if (Data.isCommon()) {
369 // Common symbols are encoded with the size in the address
370 // field, and their alignment in the flags.
371 Address = Data.getCommonSize();
373 // Common alignment is packed into the 'desc' bits.
374 if (unsigned Align = Data.getCommonAlignment()) {
375 unsigned Log2Size = Log2_32(Align);
376 assert((1U << Log2Size) == Align && "Invalid 'common' alignment!");
378 report_fatal_error("invalid 'common' alignment '" +
379 Twine(Align) + "' for '" + Symbol->getName() + "'",
381 // FIXME: Keep this mask with the SymbolFlags enumeration.
382 Flags = (Flags & 0xF0FF) | (Log2Size << 8);
386 if (Layout.getAssembler().isThumbFunc(Symbol))
387 Flags |= SF_ThumbFunc;
389 // struct nlist (12 bytes)
391 Write32(MSD.StringIndex);
393 Write8(SectionIndex);
395 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
404 void MachObjectWriter::WriteLinkeditLoadCommand(uint32_t Type,
407 uint64_t Start = OS.tell();
411 Write32(sizeof(MachO::linkedit_data_command));
415 assert(OS.tell() - Start == sizeof(MachO::linkedit_data_command));
418 static unsigned ComputeLinkerOptionsLoadCommandSize(
419 const std::vector<std::string> &Options, bool is64Bit)
421 unsigned Size = sizeof(MachO::linker_options_command);
422 for (unsigned i = 0, e = Options.size(); i != e; ++i)
423 Size += Options[i].size() + 1;
424 return RoundUpToAlignment(Size, is64Bit ? 8 : 4);
427 void MachObjectWriter::WriteLinkerOptionsLoadCommand(
428 const std::vector<std::string> &Options)
430 unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit());
431 uint64_t Start = OS.tell();
434 Write32(MachO::LC_LINKER_OPTIONS);
436 Write32(Options.size());
437 uint64_t BytesWritten = sizeof(MachO::linker_options_command);
438 for (unsigned i = 0, e = Options.size(); i != e; ++i) {
439 // Write each string, including the null byte.
440 const std::string &Option = Options[i];
441 WriteBytes(Option.c_str(), Option.size() + 1);
442 BytesWritten += Option.size() + 1;
445 // Pad to a multiple of the pointer size.
446 WriteBytes("", OffsetToAlignment(BytesWritten, is64Bit() ? 8 : 4));
448 assert(OS.tell() - Start == Size);
452 void MachObjectWriter::RecordRelocation(const MCAssembler &Asm,
453 const MCAsmLayout &Layout,
454 const MCFragment *Fragment,
455 const MCFixup &Fixup,
458 uint64_t &FixedValue) {
459 TargetObjectWriter->RecordRelocation(this, Asm, Layout, Fragment, Fixup,
463 void MachObjectWriter::BindIndirectSymbols(MCAssembler &Asm) {
464 // This is the point where 'as' creates actual symbols for indirect symbols
465 // (in the following two passes). It would be easier for us to do this sooner
466 // when we see the attribute, but that makes getting the order in the symbol
467 // table much more complicated than it is worth.
469 // FIXME: Revisit this when the dust settles.
471 // Report errors for use of .indirect_symbol not in a symbol pointer section
473 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
474 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
475 const MCSectionMachO &Section =
476 cast<MCSectionMachO>(it->SectionData->getSection());
478 if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
479 Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
480 Section.getType() != MachO::S_SYMBOL_STUBS) {
481 MCSymbol &Symbol = *it->Symbol;
482 report_fatal_error("indirect symbol '" + Symbol.getName() +
483 "' not in a symbol pointer or stub section");
487 // Bind non-lazy symbol pointers first.
488 unsigned IndirectIndex = 0;
489 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
490 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
491 const MCSectionMachO &Section =
492 cast<MCSectionMachO>(it->SectionData->getSection());
494 if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS)
497 // Initialize the section indirect symbol base, if necessary.
498 IndirectSymBase.insert(std::make_pair(it->SectionData, IndirectIndex));
500 Asm.getOrCreateSymbolData(*it->Symbol);
503 // Then lazy symbol pointers and symbol stubs.
505 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
506 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
507 const MCSectionMachO &Section =
508 cast<MCSectionMachO>(it->SectionData->getSection());
510 if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
511 Section.getType() != MachO::S_SYMBOL_STUBS)
514 // Initialize the section indirect symbol base, if necessary.
515 IndirectSymBase.insert(std::make_pair(it->SectionData, IndirectIndex));
517 // Set the symbol type to undefined lazy, but only on construction.
519 // FIXME: Do not hardcode.
521 MCSymbolData &Entry = Asm.getOrCreateSymbolData(*it->Symbol, &Created);
523 Entry.setFlags(Entry.getFlags() | 0x0001);
527 /// ComputeSymbolTable - Compute the symbol table data
529 /// \param StringTable [out] - The string table data.
530 /// \param StringIndexMap [out] - Map from symbol names to offsets in the
532 void MachObjectWriter::
533 ComputeSymbolTable(MCAssembler &Asm, SmallString<256> &StringTable,
534 std::vector<MachSymbolData> &LocalSymbolData,
535 std::vector<MachSymbolData> &ExternalSymbolData,
536 std::vector<MachSymbolData> &UndefinedSymbolData) {
537 // Build section lookup table.
538 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
540 for (MCAssembler::iterator it = Asm.begin(),
541 ie = Asm.end(); it != ie; ++it, ++Index)
542 SectionIndexMap[&it->getSection()] = Index;
543 assert(Index <= 256 && "Too many sections!");
545 // Index 0 is always the empty string.
546 StringMap<uint64_t> StringIndexMap;
547 StringTable += '\x00';
549 // Build the symbol arrays and the string table, but only for non-local
552 // The particular order that we collect the symbols and create the string
553 // table, then sort the symbols is chosen to match 'as'. Even though it
554 // doesn't matter for correctness, this is important for letting us diff .o
556 for (MCSymbolData &SD : Asm.symbols()) {
557 const MCSymbol &Symbol = SD.getSymbol();
559 // Ignore non-linker visible symbols.
560 if (!Asm.isSymbolLinkerVisible(SD.getSymbol()))
563 if (!SD.isExternal() && !Symbol.isUndefined())
566 uint64_t &Entry = StringIndexMap[Symbol.getName()];
568 Entry = StringTable.size();
569 StringTable += Symbol.getName();
570 StringTable += '\x00';
574 MSD.SymbolData = &SD;
575 MSD.StringIndex = Entry;
577 if (Symbol.isUndefined()) {
578 MSD.SectionIndex = 0;
579 UndefinedSymbolData.push_back(MSD);
580 } else if (Symbol.isAbsolute()) {
581 MSD.SectionIndex = 0;
582 ExternalSymbolData.push_back(MSD);
584 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
585 assert(MSD.SectionIndex && "Invalid section index!");
586 ExternalSymbolData.push_back(MSD);
590 // Now add the data for local symbols.
591 for (MCSymbolData &SD : Asm.symbols()) {
592 const MCSymbol &Symbol = SD.getSymbol();
594 // Ignore non-linker visible symbols.
595 if (!Asm.isSymbolLinkerVisible(SD.getSymbol()))
598 if (SD.isExternal() || Symbol.isUndefined())
601 uint64_t &Entry = StringIndexMap[Symbol.getName()];
603 Entry = StringTable.size();
604 StringTable += Symbol.getName();
605 StringTable += '\x00';
609 MSD.SymbolData = &SD;
610 MSD.StringIndex = Entry;
612 if (Symbol.isAbsolute()) {
613 MSD.SectionIndex = 0;
614 LocalSymbolData.push_back(MSD);
616 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
617 assert(MSD.SectionIndex && "Invalid section index!");
618 LocalSymbolData.push_back(MSD);
622 // External and undefined symbols are required to be in lexicographic order.
623 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
624 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
626 // Set the symbol indices.
628 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
629 LocalSymbolData[i].SymbolData->setIndex(Index++);
630 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
631 ExternalSymbolData[i].SymbolData->setIndex(Index++);
632 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
633 UndefinedSymbolData[i].SymbolData->setIndex(Index++);
635 // The string table is padded to a multiple of 4.
636 while (StringTable.size() % 4)
637 StringTable += '\x00';
640 void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
641 const MCAsmLayout &Layout) {
642 uint64_t StartAddress = 0;
643 const SmallVectorImpl<MCSectionData*> &Order = Layout.getSectionOrder();
644 for (int i = 0, n = Order.size(); i != n ; ++i) {
645 const MCSectionData *SD = Order[i];
646 StartAddress = RoundUpToAlignment(StartAddress, SD->getAlignment());
647 SectionAddress[SD] = StartAddress;
648 StartAddress += Layout.getSectionAddressSize(SD);
650 // Explicitly pad the section to match the alignment requirements of the
651 // following one. This is for 'gas' compatibility, it shouldn't
652 /// strictly be necessary.
653 StartAddress += getPaddingSize(SD, Layout);
657 void MachObjectWriter::markAbsoluteVariableSymbols(MCAssembler &Asm,
658 const MCAsmLayout &Layout) {
659 for (MCSymbolData &SD : Asm.symbols()) {
660 if (!SD.getSymbol().isVariable())
663 // Is the variable is a symbol difference (SA - SB + C) expression,
664 // and neither symbol is external, mark the variable as absolute.
665 const MCExpr *Expr = SD.getSymbol().getVariableValue();
667 if (Expr->EvaluateAsRelocatable(Value, &Layout, nullptr)) {
668 if (Value.getSymA() && Value.getSymB())
669 const_cast<MCSymbol*>(&SD.getSymbol())->setAbsolute();
674 void MachObjectWriter::ExecutePostLayoutBinding(MCAssembler &Asm,
675 const MCAsmLayout &Layout) {
676 computeSectionAddresses(Asm, Layout);
678 // Create symbol data for any indirect symbols.
679 BindIndirectSymbols(Asm);
681 // Mark symbol difference expressions in variables (from .set or = directives)
683 markAbsoluteVariableSymbols(Asm, Layout);
685 // Compute symbol table information and bind symbol indices.
686 ComputeSymbolTable(Asm, StringTable, LocalSymbolData, ExternalSymbolData,
687 UndefinedSymbolData);
690 bool MachObjectWriter::
691 IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
692 const MCSymbolData &DataA,
693 const MCFragment &FB,
695 bool IsPCRel) const {
699 // The effective address is
700 // addr(atom(A)) + offset(A)
701 // - addr(atom(B)) - offset(B)
702 // and the offsets are not relocatable, so the fixup is fully resolved when
703 // addr(atom(A)) - addr(atom(B)) == 0.
704 const MCSymbolData *A_Base = nullptr, *B_Base = nullptr;
706 const MCSymbol &SA = DataA.getSymbol().AliasedSymbol();
707 const MCSection &SecA = SA.getSection();
708 const MCSection &SecB = FB.getParent()->getSection();
711 // The simple (Darwin, except on x86_64) way of dealing with this was to
712 // assume that any reference to a temporary symbol *must* be a temporary
713 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
714 // relocation to a temporary symbol (in the same section) is fully
715 // resolved. This also works in conjunction with absolutized .set, which
716 // requires the compiler to use .set to absolutize the differences between
717 // symbols which the compiler knows to be assembly time constants, so we
718 // don't need to worry about considering symbol differences fully resolved.
720 // If the file isn't using sub-sections-via-symbols, we can make the
721 // same assumptions about any symbol that we normally make about
724 bool hasReliableSymbolDifference = isX86_64();
725 if (!hasReliableSymbolDifference) {
726 if (!SA.isInSection() || &SecA != &SecB ||
727 (!SA.isTemporary() &&
728 FB.getAtom() != Asm.getSymbolData(SA).getFragment()->getAtom() &&
729 Asm.getSubsectionsViaSymbols()))
733 // For Darwin x86_64, there is one special case when the reference IsPCRel.
734 // If the fragment with the reference does not have a base symbol but meets
735 // the simple way of dealing with this, in that it is a temporary symbol in
736 // the same atom then it is assumed to be fully resolved. This is needed so
737 // a relocation entry is not created and so the static linker does not
738 // mess up the reference later.
739 else if(!FB.getAtom() &&
740 SA.isTemporary() && SA.isInSection() && &SecA == &SecB){
744 if (!TargetObjectWriter->useAggressiveSymbolFolding())
748 const MCFragment *FA = Asm.getSymbolData(SA).getFragment();
750 // Bail if the symbol has no fragment.
754 A_Base = FA->getAtom();
758 B_Base = FB.getAtom();
762 // If the atoms are the same, they are guaranteed to have the same address.
763 if (A_Base == B_Base)
766 // Otherwise, we can't prove this is fully resolved.
770 void MachObjectWriter::WriteObject(MCAssembler &Asm,
771 const MCAsmLayout &Layout) {
772 unsigned NumSections = Asm.size();
773 const MCAssembler::VersionMinInfoType &VersionInfo =
774 Layout.getAssembler().getVersionMinInfo();
776 // The section data starts after the header, the segment load command (and
777 // section headers) and the symbol table.
778 unsigned NumLoadCommands = 1;
779 uint64_t LoadCommandsSize = is64Bit() ?
780 sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64):
781 sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
783 // Add the deployment target version info load command size, if used.
784 if (VersionInfo.Major != 0) {
786 LoadCommandsSize += sizeof(MachO::version_min_command);
789 // Add the data-in-code load command size, if used.
790 unsigned NumDataRegions = Asm.getDataRegions().size();
791 if (NumDataRegions) {
793 LoadCommandsSize += sizeof(MachO::linkedit_data_command);
796 // Add the loh load command size, if used.
797 uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout);
798 uint64_t LOHSize = RoundUpToAlignment(LOHRawSize, is64Bit() ? 8 : 4);
801 LoadCommandsSize += sizeof(MachO::linkedit_data_command);
804 // Add the symbol table load command sizes, if used.
805 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
806 UndefinedSymbolData.size();
808 NumLoadCommands += 2;
809 LoadCommandsSize += (sizeof(MachO::symtab_command) +
810 sizeof(MachO::dysymtab_command));
813 // Add the linker option load commands sizes.
814 const std::vector<std::vector<std::string> > &LinkerOptions =
815 Asm.getLinkerOptions();
816 for (unsigned i = 0, e = LinkerOptions.size(); i != e; ++i) {
818 LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(LinkerOptions[i],
822 // Compute the total size of the section data, as well as its file size and vm
824 uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) :
825 sizeof(MachO::mach_header)) + LoadCommandsSize;
826 uint64_t SectionDataSize = 0;
827 uint64_t SectionDataFileSize = 0;
829 for (MCAssembler::const_iterator it = Asm.begin(),
830 ie = Asm.end(); it != ie; ++it) {
831 const MCSectionData &SD = *it;
832 uint64_t Address = getSectionAddress(&SD);
833 uint64_t Size = Layout.getSectionAddressSize(&SD);
834 uint64_t FileSize = Layout.getSectionFileSize(&SD);
835 FileSize += getPaddingSize(&SD, Layout);
837 VMSize = std::max(VMSize, Address + Size);
839 if (SD.getSection().isVirtualSection())
842 SectionDataSize = std::max(SectionDataSize, Address + Size);
843 SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
846 // The section data is padded to 4 bytes.
848 // FIXME: Is this machine dependent?
849 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
850 SectionDataFileSize += SectionDataPadding;
852 // Write the prolog, starting with the header and load command...
853 WriteHeader(NumLoadCommands, LoadCommandsSize,
854 Asm.getSubsectionsViaSymbols());
855 WriteSegmentLoadCommand(NumSections, VMSize,
856 SectionDataStart, SectionDataSize);
858 // ... and then the section headers.
859 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
860 for (MCAssembler::const_iterator it = Asm.begin(),
861 ie = Asm.end(); it != ie; ++it) {
862 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
863 unsigned NumRelocs = Relocs.size();
864 uint64_t SectionStart = SectionDataStart + getSectionAddress(it);
865 WriteSection(Asm, Layout, *it, SectionStart, RelocTableEnd, NumRelocs);
866 RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
869 // Write out the deployment target information, if it's available.
870 if (VersionInfo.Major != 0) {
871 assert(VersionInfo.Update < 256 && "unencodable update target version");
872 assert(VersionInfo.Minor < 256 && "unencodable minor target version");
873 assert(VersionInfo.Major < 65536 && "unencodable major target version");
874 uint32_t EncodedVersion = VersionInfo.Update | (VersionInfo.Minor << 8) |
875 (VersionInfo.Major << 16);
876 Write32(VersionInfo.Kind == MCVM_OSXVersionMin ? MachO::LC_VERSION_MIN_MACOSX :
877 MachO::LC_VERSION_MIN_IPHONEOS);
878 Write32(sizeof(MachO::version_min_command));
879 Write32(EncodedVersion);
880 Write32(0); // reserved.
883 // Write the data-in-code load command, if used.
884 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
885 if (NumDataRegions) {
886 uint64_t DataRegionsOffset = RelocTableEnd;
887 uint64_t DataRegionsSize = NumDataRegions * 8;
888 WriteLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset,
892 // Write the loh load command, if used.
893 uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
895 WriteLinkeditLoadCommand(MachO::LC_LINKER_OPTIMIZATION_HINT,
896 DataInCodeTableEnd, LOHSize);
898 // Write the symbol table load command, if used.
900 unsigned FirstLocalSymbol = 0;
901 unsigned NumLocalSymbols = LocalSymbolData.size();
902 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
903 unsigned NumExternalSymbols = ExternalSymbolData.size();
904 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
905 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
906 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
907 unsigned NumSymTabSymbols =
908 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
909 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
910 uint64_t IndirectSymbolOffset = 0;
912 // If used, the indirect symbols are written after the section data.
913 if (NumIndirectSymbols)
914 IndirectSymbolOffset = LOHTableEnd;
916 // The symbol table is written after the indirect symbol data.
917 uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
919 // The string table is written after symbol table.
920 uint64_t StringTableOffset =
921 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ?
922 sizeof(MachO::nlist_64) :
923 sizeof(MachO::nlist));
924 WriteSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
925 StringTableOffset, StringTable.size());
927 WriteDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
928 FirstExternalSymbol, NumExternalSymbols,
929 FirstUndefinedSymbol, NumUndefinedSymbols,
930 IndirectSymbolOffset, NumIndirectSymbols);
933 // Write the linker options load commands.
934 for (unsigned i = 0, e = LinkerOptions.size(); i != e; ++i) {
935 WriteLinkerOptionsLoadCommand(LinkerOptions[i]);
938 // Write the actual section data.
939 for (MCAssembler::const_iterator it = Asm.begin(),
940 ie = Asm.end(); it != ie; ++it) {
941 Asm.writeSectionData(it, Layout);
943 uint64_t Pad = getPaddingSize(it, Layout);
944 for (unsigned int i = 0; i < Pad; ++i)
948 // Write the extra padding.
949 WriteZeros(SectionDataPadding);
951 // Write the relocation entries.
952 for (MCAssembler::const_iterator it = Asm.begin(),
953 ie = Asm.end(); it != ie; ++it) {
954 // Write the section relocation entries, in reverse order to match 'as'
955 // (approximately, the exact algorithm is more complicated than this).
956 std::vector<MachO::any_relocation_info> &Relocs = Relocations[it];
957 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
958 Write32(Relocs[e - i - 1].r_word0);
959 Write32(Relocs[e - i - 1].r_word1);
963 // Write out the data-in-code region payload, if there is one.
964 for (MCAssembler::const_data_region_iterator
965 it = Asm.data_region_begin(), ie = Asm.data_region_end();
967 const DataRegionData *Data = &(*it);
969 getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->Start),
972 getSymbolAddress(&Layout.getAssembler().getSymbolData(*Data->End),
974 DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
975 << " start: " << Start << "(" << Data->Start->getName() << ")"
976 << " end: " << End << "(" << Data->End->getName() << ")"
977 << " size: " << End - Start
980 Write16(End - Start);
984 // Write out the loh commands, if there is one.
987 unsigned Start = OS.tell();
989 Asm.getLOHContainer().Emit(*this, Layout);
990 // Pad to a multiple of the pointer size.
991 WriteBytes("", OffsetToAlignment(LOHRawSize, is64Bit() ? 8 : 4));
992 assert(OS.tell() - Start == LOHSize);
995 // Write the symbol table data, if used.
997 // Write the indirect symbol entries.
998 for (MCAssembler::const_indirect_symbol_iterator
999 it = Asm.indirect_symbol_begin(),
1000 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
1001 // Indirect symbols in the non-lazy symbol pointer section have some
1002 // special handling.
1003 const MCSectionMachO &Section =
1004 static_cast<const MCSectionMachO&>(it->SectionData->getSection());
1005 if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) {
1006 // If this symbol is defined and internal, mark it as such.
1007 if (it->Symbol->isDefined() &&
1008 !Asm.getSymbolData(*it->Symbol).isExternal()) {
1009 uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
1010 if (it->Symbol->isAbsolute())
1011 Flags |= MachO::INDIRECT_SYMBOL_ABS;
1017 Write32(Asm.getSymbolData(*it->Symbol).getIndex());
1020 // FIXME: Check that offsets match computed ones.
1022 // Write the symbol table entries.
1023 for (unsigned i = 0, e = LocalSymbolData.size(); i != e; ++i)
1024 WriteNlist(LocalSymbolData[i], Layout);
1025 for (unsigned i = 0, e = ExternalSymbolData.size(); i != e; ++i)
1026 WriteNlist(ExternalSymbolData[i], Layout);
1027 for (unsigned i = 0, e = UndefinedSymbolData.size(); i != e; ++i)
1028 WriteNlist(UndefinedSymbolData[i], Layout);
1030 // Write the string table.
1031 OS << StringTable.str();
1035 MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
1037 bool IsLittleEndian) {
1038 return new MachObjectWriter(MOTW, OS, IsLittleEndian);