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/ADT/DenseMap.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/ADT/iterator_range.h"
13 #include "llvm/BinaryFormat/MachO.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCDirectives.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCFixupKindInfo.h"
20 #include "llvm/MC/MCFragment.h"
21 #include "llvm/MC/MCMachObjectWriter.h"
22 #include "llvm/MC/MCObjectWriter.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCSectionMachO.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCSymbolMachO.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/raw_ostream.h"
42 #define DEBUG_TYPE "mc"
44 void MachObjectWriter::reset() {
46 IndirectSymBase.clear();
48 LocalSymbolData.clear();
49 ExternalSymbolData.clear();
50 UndefinedSymbolData.clear();
51 MCObjectWriter::reset();
54 bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) {
55 // Undefined symbols are always extern.
59 // References to weak definitions require external relocation entries; the
60 // definition may not always be the one in the same object file.
61 if (cast<MCSymbolMachO>(S).isWeakDefinition())
64 // Otherwise, we can use an internal relocation.
68 bool MachObjectWriter::
69 MachSymbolData::operator<(const MachSymbolData &RHS) const {
70 return Symbol->getName() < RHS.Symbol->getName();
73 bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
74 const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo(
77 return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
80 uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment,
81 const MCAsmLayout &Layout) const {
82 return getSectionAddress(Fragment->getParent()) +
83 Layout.getFragmentOffset(Fragment);
86 uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
87 const MCAsmLayout &Layout) const {
88 // If this is a variable, then recursively evaluate now.
90 if (const MCConstantExpr *C =
91 dyn_cast<const MCConstantExpr>(S.getVariableValue()))
95 if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Layout, nullptr))
96 report_fatal_error("unable to evaluate offset for variable '" +
99 // Verify that any used symbols are defined.
100 if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
101 report_fatal_error("unable to evaluate offset to undefined symbol '" +
102 Target.getSymA()->getSymbol().getName() + "'");
103 if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
104 report_fatal_error("unable to evaluate offset to undefined symbol '" +
105 Target.getSymB()->getSymbol().getName() + "'");
107 uint64_t Address = Target.getConstant();
108 if (Target.getSymA())
109 Address += getSymbolAddress(Target.getSymA()->getSymbol(), Layout);
110 if (Target.getSymB())
111 Address += getSymbolAddress(Target.getSymB()->getSymbol(), Layout);
115 return getSectionAddress(S.getFragment()->getParent()) +
116 Layout.getSymbolOffset(S);
119 uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec,
120 const MCAsmLayout &Layout) const {
121 uint64_t EndAddr = getSectionAddress(Sec) + Layout.getSectionAddressSize(Sec);
122 unsigned Next = Sec->getLayoutOrder() + 1;
123 if (Next >= Layout.getSectionOrder().size())
126 const MCSection &NextSec = *Layout.getSectionOrder()[Next];
127 if (NextSec.isVirtualSection())
129 return OffsetToAlignment(EndAddr, NextSec.getAlignment());
132 void MachObjectWriter::writeHeader(MachO::HeaderFileType Type,
133 unsigned NumLoadCommands,
134 unsigned LoadCommandsSize,
135 bool SubsectionsViaSymbols) {
138 if (SubsectionsViaSymbols)
139 Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
141 // struct mach_header (28 bytes) or
142 // struct mach_header_64 (32 bytes)
144 uint64_t Start = getStream().tell();
147 write32(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC);
149 write32(TargetObjectWriter->getCPUType());
150 write32(TargetObjectWriter->getCPUSubtype());
153 write32(NumLoadCommands);
154 write32(LoadCommandsSize);
157 write32(0); // reserved
160 getStream().tell() - Start ==
161 (is64Bit() ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header)));
164 /// writeSegmentLoadCommand - Write a segment load command.
166 /// \param NumSections The number of sections in this segment.
167 /// \param SectionDataSize The total size of the sections.
168 void MachObjectWriter::writeSegmentLoadCommand(
169 StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize,
170 uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt,
172 // struct segment_command (56 bytes) or
173 // struct segment_command_64 (72 bytes)
175 uint64_t Start = getStream().tell();
178 unsigned SegmentLoadCommandSize =
179 is64Bit() ? sizeof(MachO::segment_command_64):
180 sizeof(MachO::segment_command);
181 write32(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
182 write32(SegmentLoadCommandSize +
183 NumSections * (is64Bit() ? sizeof(MachO::section_64) :
184 sizeof(MachO::section)));
186 assert(Name.size() <= 16);
187 writeBytes(Name, 16);
189 write64(VMAddr); // vmaddr
190 write64(VMSize); // vmsize
191 write64(SectionDataStartOffset); // file offset
192 write64(SectionDataSize); // file size
194 write32(VMAddr); // vmaddr
195 write32(VMSize); // vmsize
196 write32(SectionDataStartOffset); // file offset
197 write32(SectionDataSize); // file size
203 write32(NumSections);
206 assert(getStream().tell() - Start == SegmentLoadCommandSize);
209 void MachObjectWriter::writeSection(const MCAsmLayout &Layout,
210 const MCSection &Sec, uint64_t VMAddr,
211 uint64_t FileOffset, unsigned Flags,
212 uint64_t RelocationsStart,
213 unsigned NumRelocations) {
214 uint64_t SectionSize = Layout.getSectionAddressSize(&Sec);
215 const MCSectionMachO &Section = cast<MCSectionMachO>(Sec);
217 // The offset is unused for virtual sections.
218 if (Section.isVirtualSection()) {
219 assert(Layout.getSectionFileSize(&Sec) == 0 && "Invalid file size!");
223 // struct section (68 bytes) or
224 // struct section_64 (80 bytes)
226 uint64_t Start = getStream().tell();
229 writeBytes(Section.getSectionName(), 16);
230 writeBytes(Section.getSegmentName(), 16);
232 write64(VMAddr); // address
233 write64(SectionSize); // size
235 write32(VMAddr); // address
236 write32(SectionSize); // size
240 assert(isPowerOf2_32(Section.getAlignment()) && "Invalid alignment!");
241 write32(Log2_32(Section.getAlignment()));
242 write32(NumRelocations ? RelocationsStart : 0);
243 write32(NumRelocations);
245 write32(IndirectSymBase.lookup(&Sec)); // reserved1
246 write32(Section.getStubSize()); // reserved2
248 write32(0); // reserved3
250 assert(getStream().tell() - Start ==
251 (is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section)));
254 void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset,
256 uint32_t StringTableOffset,
257 uint32_t StringTableSize) {
258 // struct symtab_command (24 bytes)
260 uint64_t Start = getStream().tell();
263 write32(MachO::LC_SYMTAB);
264 write32(sizeof(MachO::symtab_command));
265 write32(SymbolOffset);
267 write32(StringTableOffset);
268 write32(StringTableSize);
270 assert(getStream().tell() - Start == sizeof(MachO::symtab_command));
273 void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol,
274 uint32_t NumLocalSymbols,
275 uint32_t FirstExternalSymbol,
276 uint32_t NumExternalSymbols,
277 uint32_t FirstUndefinedSymbol,
278 uint32_t NumUndefinedSymbols,
279 uint32_t IndirectSymbolOffset,
280 uint32_t NumIndirectSymbols) {
281 // struct dysymtab_command (80 bytes)
283 uint64_t Start = getStream().tell();
286 write32(MachO::LC_DYSYMTAB);
287 write32(sizeof(MachO::dysymtab_command));
288 write32(FirstLocalSymbol);
289 write32(NumLocalSymbols);
290 write32(FirstExternalSymbol);
291 write32(NumExternalSymbols);
292 write32(FirstUndefinedSymbol);
293 write32(NumUndefinedSymbols);
294 write32(0); // tocoff
296 write32(0); // modtaboff
297 write32(0); // nmodtab
298 write32(0); // extrefsymoff
299 write32(0); // nextrefsyms
300 write32(IndirectSymbolOffset);
301 write32(NumIndirectSymbols);
302 write32(0); // extreloff
303 write32(0); // nextrel
304 write32(0); // locreloff
305 write32(0); // nlocrel
307 assert(getStream().tell() - Start == sizeof(MachO::dysymtab_command));
310 MachObjectWriter::MachSymbolData *
311 MachObjectWriter::findSymbolData(const MCSymbol &Sym) {
312 for (auto *SymbolData :
313 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
314 for (MachSymbolData &Entry : *SymbolData)
315 if (Entry.Symbol == &Sym)
321 const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const {
322 const MCSymbol *S = &Sym;
323 while (S->isVariable()) {
324 const MCExpr *Value = S->getVariableValue();
325 const auto *Ref = dyn_cast<MCSymbolRefExpr>(Value);
328 S = &Ref->getSymbol();
333 void MachObjectWriter::writeNlist(MachSymbolData &MSD,
334 const MCAsmLayout &Layout) {
335 const MCSymbol *Symbol = MSD.Symbol;
336 const MCSymbol &Data = *Symbol;
337 const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol);
338 uint8_t SectionIndex = MSD.SectionIndex;
340 uint64_t Address = 0;
341 bool IsAlias = Symbol != AliasedSymbol;
343 const MCSymbol &OrigSymbol = *Symbol;
344 MachSymbolData *AliaseeInfo;
346 AliaseeInfo = findSymbolData(*AliasedSymbol);
348 SectionIndex = AliaseeInfo->SectionIndex;
349 Symbol = AliasedSymbol;
350 // FIXME: Should this update Data as well?
353 // Set the N_TYPE bits. See <mach-o/nlist.h>.
355 // FIXME: Are the prebound or indirect fields possible here?
356 if (IsAlias && Symbol->isUndefined())
357 Type = MachO::N_INDR;
358 else if (Symbol->isUndefined())
359 Type = MachO::N_UNDF;
360 else if (Symbol->isAbsolute())
363 Type = MachO::N_SECT;
365 // FIXME: Set STAB bits.
367 if (Data.isPrivateExtern())
368 Type |= MachO::N_PEXT;
371 if (Data.isExternal() || (!IsAlias && Symbol->isUndefined()))
372 Type |= MachO::N_EXT;
374 // Compute the symbol address.
375 if (IsAlias && Symbol->isUndefined())
376 Address = AliaseeInfo->StringIndex;
377 else if (Symbol->isDefined())
378 Address = getSymbolAddress(OrigSymbol, Layout);
379 else if (Symbol->isCommon()) {
380 // Common symbols are encoded with the size in the address
381 // field, and their alignment in the flags.
382 Address = Symbol->getCommonSize();
385 // struct nlist (12 bytes)
387 write32(MSD.StringIndex);
389 write8(SectionIndex);
391 // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
393 bool EncodeAsAltEntry =
394 IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry();
395 write16(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry));
402 void MachObjectWriter::writeLinkeditLoadCommand(uint32_t Type,
405 uint64_t Start = getStream().tell();
409 write32(sizeof(MachO::linkedit_data_command));
413 assert(getStream().tell() - Start == sizeof(MachO::linkedit_data_command));
416 static unsigned ComputeLinkerOptionsLoadCommandSize(
417 const std::vector<std::string> &Options, bool is64Bit)
419 unsigned Size = sizeof(MachO::linker_option_command);
420 for (const std::string &Option : Options)
421 Size += Option.size() + 1;
422 return alignTo(Size, is64Bit ? 8 : 4);
425 void MachObjectWriter::writeLinkerOptionsLoadCommand(
426 const std::vector<std::string> &Options)
428 unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit());
429 uint64_t Start = getStream().tell();
432 write32(MachO::LC_LINKER_OPTION);
434 write32(Options.size());
435 uint64_t BytesWritten = sizeof(MachO::linker_option_command);
436 for (const std::string &Option : Options) {
437 // Write each string, including the null byte.
438 writeBytes(Option, Option.size() + 1);
439 BytesWritten += Option.size() + 1;
442 // Pad to a multiple of the pointer size.
443 writeBytes("", OffsetToAlignment(BytesWritten, is64Bit() ? 8 : 4));
445 assert(getStream().tell() - Start == Size);
448 void MachObjectWriter::recordRelocation(MCAssembler &Asm,
449 const MCAsmLayout &Layout,
450 const MCFragment *Fragment,
451 const MCFixup &Fixup, MCValue Target,
452 uint64_t &FixedValue) {
453 TargetObjectWriter->recordRelocation(this, Asm, Layout, Fragment, Fixup,
457 void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {
458 // This is the point where 'as' creates actual symbols for indirect symbols
459 // (in the following two passes). It would be easier for us to do this sooner
460 // when we see the attribute, but that makes getting the order in the symbol
461 // table much more complicated than it is worth.
463 // FIXME: Revisit this when the dust settles.
465 // Report errors for use of .indirect_symbol not in a symbol pointer section
467 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
468 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
469 const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section);
471 if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
472 Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
473 Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
474 Section.getType() != MachO::S_SYMBOL_STUBS) {
475 MCSymbol &Symbol = *it->Symbol;
476 report_fatal_error("indirect symbol '" + Symbol.getName() +
477 "' not in a symbol pointer or stub section");
481 // Bind non-lazy symbol pointers first.
482 unsigned IndirectIndex = 0;
483 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
484 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
485 const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section);
487 if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
488 Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
491 // Initialize the section indirect symbol base, if necessary.
492 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
494 Asm.registerSymbol(*it->Symbol);
497 // Then lazy symbol pointers and symbol stubs.
499 for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
500 ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
501 const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section);
503 if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
504 Section.getType() != MachO::S_SYMBOL_STUBS)
507 // Initialize the section indirect symbol base, if necessary.
508 IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
510 // Set the symbol type to undefined lazy, but only on construction.
512 // FIXME: Do not hardcode.
514 Asm.registerSymbol(*it->Symbol, &Created);
516 cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(true);
520 /// computeSymbolTable - Compute the symbol table data
521 void MachObjectWriter::computeSymbolTable(
522 MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
523 std::vector<MachSymbolData> &ExternalSymbolData,
524 std::vector<MachSymbolData> &UndefinedSymbolData) {
525 // Build section lookup table.
526 DenseMap<const MCSection*, uint8_t> SectionIndexMap;
528 for (MCAssembler::iterator it = Asm.begin(),
529 ie = Asm.end(); it != ie; ++it, ++Index)
530 SectionIndexMap[&*it] = Index;
531 assert(Index <= 256 && "Too many sections!");
533 // Build the string table.
534 for (const MCSymbol &Symbol : Asm.symbols()) {
535 if (!Asm.isSymbolLinkerVisible(Symbol))
538 StringTable.add(Symbol.getName());
540 StringTable.finalize();
542 // Build the symbol arrays but only for non-local symbols.
544 // The particular order that we collect and then sort the symbols is chosen to
545 // match 'as'. Even though it doesn't matter for correctness, this is
546 // important for letting us diff .o files.
547 for (const MCSymbol &Symbol : Asm.symbols()) {
548 // Ignore non-linker visible symbols.
549 if (!Asm.isSymbolLinkerVisible(Symbol))
552 if (!Symbol.isExternal() && !Symbol.isUndefined())
556 MSD.Symbol = &Symbol;
557 MSD.StringIndex = StringTable.getOffset(Symbol.getName());
559 if (Symbol.isUndefined()) {
560 MSD.SectionIndex = 0;
561 UndefinedSymbolData.push_back(MSD);
562 } else if (Symbol.isAbsolute()) {
563 MSD.SectionIndex = 0;
564 ExternalSymbolData.push_back(MSD);
566 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
567 assert(MSD.SectionIndex && "Invalid section index!");
568 ExternalSymbolData.push_back(MSD);
572 // Now add the data for local symbols.
573 for (const MCSymbol &Symbol : Asm.symbols()) {
574 // Ignore non-linker visible symbols.
575 if (!Asm.isSymbolLinkerVisible(Symbol))
578 if (Symbol.isExternal() || Symbol.isUndefined())
582 MSD.Symbol = &Symbol;
583 MSD.StringIndex = StringTable.getOffset(Symbol.getName());
585 if (Symbol.isAbsolute()) {
586 MSD.SectionIndex = 0;
587 LocalSymbolData.push_back(MSD);
589 MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
590 assert(MSD.SectionIndex && "Invalid section index!");
591 LocalSymbolData.push_back(MSD);
595 // External and undefined symbols are required to be in lexicographic order.
596 std::sort(ExternalSymbolData.begin(), ExternalSymbolData.end());
597 std::sort(UndefinedSymbolData.begin(), UndefinedSymbolData.end());
599 // Set the symbol indices.
601 for (auto *SymbolData :
602 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
603 for (MachSymbolData &Entry : *SymbolData)
604 Entry.Symbol->setIndex(Index++);
606 for (const MCSection &Section : Asm) {
607 for (RelAndSymbol &Rel : Relocations[&Section]) {
611 // Set the Index and the IsExtern bit.
612 unsigned Index = Rel.Sym->getIndex();
613 assert(isInt<24>(Index));
615 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27);
617 Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4);
622 void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm,
623 const MCAsmLayout &Layout) {
624 uint64_t StartAddress = 0;
625 for (const MCSection *Sec : Layout.getSectionOrder()) {
626 StartAddress = alignTo(StartAddress, Sec->getAlignment());
627 SectionAddress[Sec] = StartAddress;
628 StartAddress += Layout.getSectionAddressSize(Sec);
630 // Explicitly pad the section to match the alignment requirements of the
631 // following one. This is for 'gas' compatibility, it shouldn't
632 /// strictly be necessary.
633 StartAddress += getPaddingSize(Sec, Layout);
637 void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
638 const MCAsmLayout &Layout) {
639 computeSectionAddresses(Asm, Layout);
641 // Create symbol data for any indirect symbols.
642 bindIndirectSymbols(Asm);
645 bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
646 const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B,
648 // FIXME: We don't handle things like
651 if (A.isVariable() || B.isVariable())
653 return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, A, B,
657 bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
658 const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
659 bool InSet, bool IsPCRel) const {
663 // The effective address is
664 // addr(atom(A)) + offset(A)
665 // - addr(atom(B)) - offset(B)
666 // and the offsets are not relocatable, so the fixup is fully resolved when
667 // addr(atom(A)) - addr(atom(B)) == 0.
668 const MCSymbol &SA = findAliasedSymbol(SymA);
669 const MCSection &SecA = SA.getSection();
670 const MCSection &SecB = *FB.getParent();
673 // The simple (Darwin, except on x86_64) way of dealing with this was to
674 // assume that any reference to a temporary symbol *must* be a temporary
675 // symbol in the same atom, unless the sections differ. Therefore, any PCrel
676 // relocation to a temporary symbol (in the same section) is fully
677 // resolved. This also works in conjunction with absolutized .set, which
678 // requires the compiler to use .set to absolutize the differences between
679 // symbols which the compiler knows to be assembly time constants, so we
680 // don't need to worry about considering symbol differences fully resolved.
682 // If the file isn't using sub-sections-via-symbols, we can make the
683 // same assumptions about any symbol that we normally make about
686 bool hasReliableSymbolDifference = isX86_64();
687 if (!hasReliableSymbolDifference) {
688 if (!SA.isInSection() || &SecA != &SecB ||
689 (!SA.isTemporary() && FB.getAtom() != SA.getFragment()->getAtom() &&
690 Asm.getSubsectionsViaSymbols()))
694 // For Darwin x86_64, there is one special case when the reference IsPCRel.
695 // If the fragment with the reference does not have a base symbol but meets
696 // the simple way of dealing with this, in that it is a temporary symbol in
697 // the same atom then it is assumed to be fully resolved. This is needed so
698 // a relocation entry is not created and so the static linker does not
699 // mess up the reference later.
700 else if(!FB.getAtom() &&
701 SA.isTemporary() && SA.isInSection() && &SecA == &SecB){
706 // If they are not in the same section, we can't compute the diff.
710 const MCFragment *FA = SA.getFragment();
712 // Bail if the symbol has no fragment.
716 // If the atoms are the same, they are guaranteed to have the same address.
717 if (FA->getAtom() == FB.getAtom())
720 // Otherwise, we can't prove this is fully resolved.
724 void MachObjectWriter::writeObject(MCAssembler &Asm,
725 const MCAsmLayout &Layout) {
726 // Compute symbol table information and bind symbol indices.
727 computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData,
728 UndefinedSymbolData);
730 unsigned NumSections = Asm.size();
731 const MCAssembler::VersionMinInfoType &VersionInfo =
732 Layout.getAssembler().getVersionMinInfo();
734 // The section data starts after the header, the segment load command (and
735 // section headers) and the symbol table.
736 unsigned NumLoadCommands = 1;
737 uint64_t LoadCommandsSize = is64Bit() ?
738 sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64):
739 sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
741 // Add the deployment target version info load command size, if used.
742 if (VersionInfo.Major != 0) {
744 LoadCommandsSize += sizeof(MachO::version_min_command);
747 // Add the data-in-code load command size, if used.
748 unsigned NumDataRegions = Asm.getDataRegions().size();
749 if (NumDataRegions) {
751 LoadCommandsSize += sizeof(MachO::linkedit_data_command);
754 // Add the loh load command size, if used.
755 uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout);
756 uint64_t LOHSize = alignTo(LOHRawSize, is64Bit() ? 8 : 4);
759 LoadCommandsSize += sizeof(MachO::linkedit_data_command);
762 // Add the symbol table load command sizes, if used.
763 unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
764 UndefinedSymbolData.size();
766 NumLoadCommands += 2;
767 LoadCommandsSize += (sizeof(MachO::symtab_command) +
768 sizeof(MachO::dysymtab_command));
771 // Add the linker option load commands sizes.
772 for (const auto &Option : Asm.getLinkerOptions()) {
774 LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(Option, is64Bit());
777 // Compute the total size of the section data, as well as its file size and vm
779 uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) :
780 sizeof(MachO::mach_header)) + LoadCommandsSize;
781 uint64_t SectionDataSize = 0;
782 uint64_t SectionDataFileSize = 0;
784 for (const MCSection &Sec : Asm) {
785 uint64_t Address = getSectionAddress(&Sec);
786 uint64_t Size = Layout.getSectionAddressSize(&Sec);
787 uint64_t FileSize = Layout.getSectionFileSize(&Sec);
788 FileSize += getPaddingSize(&Sec, Layout);
790 VMSize = std::max(VMSize, Address + Size);
792 if (Sec.isVirtualSection())
795 SectionDataSize = std::max(SectionDataSize, Address + Size);
796 SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
799 // The section data is padded to 4 bytes.
801 // FIXME: Is this machine dependent?
802 unsigned SectionDataPadding = OffsetToAlignment(SectionDataFileSize, 4);
803 SectionDataFileSize += SectionDataPadding;
805 // Write the prolog, starting with the header and load command...
806 writeHeader(MachO::MH_OBJECT, NumLoadCommands, LoadCommandsSize,
807 Asm.getSubsectionsViaSymbols());
809 MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE;
810 writeSegmentLoadCommand("", NumSections, 0, VMSize, SectionDataStart,
811 SectionDataSize, Prot, Prot);
813 // ... and then the section headers.
814 uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
815 for (const MCSection &Section : Asm) {
816 const auto &Sec = cast<MCSectionMachO>(Section);
817 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
818 unsigned NumRelocs = Relocs.size();
819 uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec);
820 unsigned Flags = Sec.getTypeAndAttributes();
821 if (Sec.hasInstructions())
822 Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS;
823 writeSection(Layout, Sec, getSectionAddress(&Sec), SectionStart, Flags,
824 RelocTableEnd, NumRelocs);
825 RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
828 // Write out the deployment target information, if it's available.
829 if (VersionInfo.Major != 0) {
830 assert(VersionInfo.Update < 256 && "unencodable update target version");
831 assert(VersionInfo.Minor < 256 && "unencodable minor target version");
832 assert(VersionInfo.Major < 65536 && "unencodable major target version");
833 uint32_t EncodedVersion = VersionInfo.Update | (VersionInfo.Minor << 8) |
834 (VersionInfo.Major << 16);
835 MachO::LoadCommandType LCType;
836 switch (VersionInfo.Kind) {
837 case MCVM_OSXVersionMin:
838 LCType = MachO::LC_VERSION_MIN_MACOSX;
840 case MCVM_IOSVersionMin:
841 LCType = MachO::LC_VERSION_MIN_IPHONEOS;
843 case MCVM_TvOSVersionMin:
844 LCType = MachO::LC_VERSION_MIN_TVOS;
846 case MCVM_WatchOSVersionMin:
847 LCType = MachO::LC_VERSION_MIN_WATCHOS;
851 write32(sizeof(MachO::version_min_command));
852 write32(EncodedVersion);
853 write32(0); // reserved.
856 // Write the data-in-code load command, if used.
857 uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
858 if (NumDataRegions) {
859 uint64_t DataRegionsOffset = RelocTableEnd;
860 uint64_t DataRegionsSize = NumDataRegions * 8;
861 writeLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset,
865 // Write the loh load command, if used.
866 uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
868 writeLinkeditLoadCommand(MachO::LC_LINKER_OPTIMIZATION_HINT,
869 DataInCodeTableEnd, LOHSize);
871 // Write the symbol table load command, if used.
873 unsigned FirstLocalSymbol = 0;
874 unsigned NumLocalSymbols = LocalSymbolData.size();
875 unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
876 unsigned NumExternalSymbols = ExternalSymbolData.size();
877 unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
878 unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
879 unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
880 unsigned NumSymTabSymbols =
881 NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
882 uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
883 uint64_t IndirectSymbolOffset = 0;
885 // If used, the indirect symbols are written after the section data.
886 if (NumIndirectSymbols)
887 IndirectSymbolOffset = LOHTableEnd;
889 // The symbol table is written after the indirect symbol data.
890 uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
892 // The string table is written after symbol table.
893 uint64_t StringTableOffset =
894 SymbolTableOffset + NumSymTabSymbols * (is64Bit() ?
895 sizeof(MachO::nlist_64) :
896 sizeof(MachO::nlist));
897 writeSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
898 StringTableOffset, StringTable.getSize());
900 writeDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
901 FirstExternalSymbol, NumExternalSymbols,
902 FirstUndefinedSymbol, NumUndefinedSymbols,
903 IndirectSymbolOffset, NumIndirectSymbols);
906 // Write the linker options load commands.
907 for (const auto &Option : Asm.getLinkerOptions())
908 writeLinkerOptionsLoadCommand(Option);
910 // Write the actual section data.
911 for (const MCSection &Sec : Asm) {
912 Asm.writeSectionData(&Sec, Layout);
914 uint64_t Pad = getPaddingSize(&Sec, Layout);
918 // Write the extra padding.
919 WriteZeros(SectionDataPadding);
921 // Write the relocation entries.
922 for (const MCSection &Sec : Asm) {
923 // Write the section relocation entries, in reverse order to match 'as'
924 // (approximately, the exact algorithm is more complicated than this).
925 std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
926 for (const RelAndSymbol &Rel : make_range(Relocs.rbegin(), Relocs.rend())) {
927 write32(Rel.MRE.r_word0);
928 write32(Rel.MRE.r_word1);
932 // Write out the data-in-code region payload, if there is one.
933 for (MCAssembler::const_data_region_iterator
934 it = Asm.data_region_begin(), ie = Asm.data_region_end();
936 const DataRegionData *Data = &(*it);
937 uint64_t Start = getSymbolAddress(*Data->Start, Layout);
938 uint64_t End = getSymbolAddress(*Data->End, Layout);
939 DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
940 << " start: " << Start << "(" << Data->Start->getName() << ")"
941 << " end: " << End << "(" << Data->End->getName() << ")"
942 << " size: " << End - Start
945 write16(End - Start);
949 // Write out the loh commands, if there is one.
952 unsigned Start = getStream().tell();
954 Asm.getLOHContainer().emit(*this, Layout);
955 // Pad to a multiple of the pointer size.
956 writeBytes("", OffsetToAlignment(LOHRawSize, is64Bit() ? 8 : 4));
957 assert(getStream().tell() - Start == LOHSize);
960 // Write the symbol table data, if used.
962 // Write the indirect symbol entries.
963 for (MCAssembler::const_indirect_symbol_iterator
964 it = Asm.indirect_symbol_begin(),
965 ie = Asm.indirect_symbol_end(); it != ie; ++it) {
966 // Indirect symbols in the non-lazy symbol pointer section have some
968 const MCSectionMachO &Section =
969 static_cast<const MCSectionMachO &>(*it->Section);
970 if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) {
971 // If this symbol is defined and internal, mark it as such.
972 if (it->Symbol->isDefined() && !it->Symbol->isExternal()) {
973 uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
974 if (it->Symbol->isAbsolute())
975 Flags |= MachO::INDIRECT_SYMBOL_ABS;
981 write32(it->Symbol->getIndex());
984 // FIXME: Check that offsets match computed ones.
986 // Write the symbol table entries.
987 for (auto *SymbolData :
988 {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
989 for (MachSymbolData &Entry : *SymbolData)
990 writeNlist(Entry, Layout);
992 // Write the string table.
993 StringTable.write(getStream());
997 MCObjectWriter *llvm::createMachObjectWriter(MCMachObjectTargetWriter *MOTW,
998 raw_pwrite_stream &OS,
999 bool IsLittleEndian) {
1000 return new MachObjectWriter(MOTW, OS, IsLittleEndian);