xref: /llvm-project/llvm/lib/MC/MachObjectWriter.cpp (revision c686f0cc90b5d322347e4e2f29901e018957cc86)
1 //===- lib/MC/MachObjectWriter.cpp - Mach-O File Writer -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/DenseMap.h"
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ADT/iterator_range.h"
12 #include "llvm/BinaryFormat/MachO.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAsmInfoDarwin.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCFixupKindInfo.h"
21 #include "llvm/MC/MCFragment.h"
22 #include "llvm/MC/MCMachObjectWriter.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCObjectWriter.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSectionMachO.h"
27 #include "llvm/MC/MCSymbol.h"
28 #include "llvm/MC/MCSymbolMachO.h"
29 #include "llvm/MC/MCValue.h"
30 #include "llvm/Support/Alignment.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/LEB128.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <algorithm>
38 #include <cassert>
39 #include <cstdint>
40 #include <string>
41 #include <utility>
42 #include <vector>
43 
44 using namespace llvm;
45 
46 #define DEBUG_TYPE "mc"
47 
48 void MachObjectWriter::reset() {
49   Relocations.clear();
50   IndirectSymBase.clear();
51   SectionAddress.clear();
52   SectionOrder.clear();
53   StringTable.clear();
54   LocalSymbolData.clear();
55   ExternalSymbolData.clear();
56   UndefinedSymbolData.clear();
57   MCObjectWriter::reset();
58 }
59 
60 bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) {
61   // Undefined symbols are always extern.
62   if (S.isUndefined())
63     return true;
64 
65   // References to weak definitions require external relocation entries; the
66   // definition may not always be the one in the same object file.
67   if (cast<MCSymbolMachO>(S).isWeakDefinition())
68     return true;
69 
70   // Otherwise, we can use an internal relocation.
71   return false;
72 }
73 
74 bool MachObjectWriter::
75 MachSymbolData::operator<(const MachSymbolData &RHS) const {
76   return Symbol->getName() < RHS.Symbol->getName();
77 }
78 
79 bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) {
80   const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo(
81     (MCFixupKind) Kind);
82 
83   return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel;
84 }
85 
86 uint64_t
87 MachObjectWriter::getFragmentAddress(const MCAssembler &Asm,
88                                      const MCFragment *Fragment) const {
89   return getSectionAddress(Fragment->getParent()) +
90          Asm.getFragmentOffset(*Fragment);
91 }
92 
93 uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S,
94                                             const MCAsmLayout &Layout) const {
95   // If this is a variable, then recursively evaluate now.
96   if (S.isVariable()) {
97     if (const MCConstantExpr *C =
98           dyn_cast<const MCConstantExpr>(S.getVariableValue()))
99       return C->getValue();
100 
101     MCValue Target;
102     if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Layout, nullptr))
103       report_fatal_error("unable to evaluate offset for variable '" +
104                          S.getName() + "'");
105 
106     // Verify that any used symbols are defined.
107     if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
108       report_fatal_error("unable to evaluate offset to undefined symbol '" +
109                          Target.getSymA()->getSymbol().getName() + "'");
110     if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
111       report_fatal_error("unable to evaluate offset to undefined symbol '" +
112                          Target.getSymB()->getSymbol().getName() + "'");
113 
114     uint64_t Address = Target.getConstant();
115     if (Target.getSymA())
116       Address += getSymbolAddress(Target.getSymA()->getSymbol(), Layout);
117     if (Target.getSymB())
118       Address += getSymbolAddress(Target.getSymB()->getSymbol(), Layout);
119     return Address;
120   }
121 
122   return getSectionAddress(S.getFragment()->getParent()) +
123          Layout.getAssembler().getSymbolOffset(S);
124 }
125 
126 uint64_t MachObjectWriter::getPaddingSize(const MCAssembler &Asm,
127                                           const MCSection *Sec) const {
128   uint64_t EndAddr = getSectionAddress(Sec) + Asm.getSectionAddressSize(*Sec);
129   unsigned Next = Sec->getLayoutOrder() + 1;
130   if (Next >= SectionOrder.size())
131     return 0;
132 
133   const MCSection &NextSec = *SectionOrder[Next];
134   if (NextSec.isVirtualSection())
135     return 0;
136   return offsetToAlignment(EndAddr, NextSec.getAlign());
137 }
138 
139 static bool isSymbolLinkerVisible(const MCSymbol &Symbol) {
140   // Non-temporary labels should always be visible to the linker.
141   if (!Symbol.isTemporary())
142     return true;
143 
144   if (Symbol.isUsedInReloc())
145     return true;
146 
147   return false;
148 }
149 
150 const MCSymbol *MachObjectWriter::getAtom(const MCSymbol &S) const {
151   // Linker visible symbols define atoms.
152   if (isSymbolLinkerVisible(S))
153     return &S;
154 
155   // Absolute and undefined symbols have no defining atom.
156   if (!S.isInSection())
157     return nullptr;
158 
159   // Non-linker visible symbols in sections which can't be atomized have no
160   // defining atom.
161   if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(
162           *S.getFragment()->getParent()))
163     return nullptr;
164 
165   // Otherwise, return the atom for the containing fragment.
166   return S.getFragment()->getAtom();
167 }
168 
169 void MachObjectWriter::writeHeader(MachO::HeaderFileType Type,
170                                    unsigned NumLoadCommands,
171                                    unsigned LoadCommandsSize,
172                                    bool SubsectionsViaSymbols) {
173   uint32_t Flags = 0;
174 
175   if (SubsectionsViaSymbols)
176     Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
177 
178   // struct mach_header (28 bytes) or
179   // struct mach_header_64 (32 bytes)
180 
181   uint64_t Start = W.OS.tell();
182   (void) Start;
183 
184   W.write<uint32_t>(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC);
185 
186   W.write<uint32_t>(TargetObjectWriter->getCPUType());
187   W.write<uint32_t>(TargetObjectWriter->getCPUSubtype());
188 
189   W.write<uint32_t>(Type);
190   W.write<uint32_t>(NumLoadCommands);
191   W.write<uint32_t>(LoadCommandsSize);
192   W.write<uint32_t>(Flags);
193   if (is64Bit())
194     W.write<uint32_t>(0); // reserved
195 
196   assert(W.OS.tell() - Start == (is64Bit() ? sizeof(MachO::mach_header_64)
197                                            : sizeof(MachO::mach_header)));
198 }
199 
200 void MachObjectWriter::writeWithPadding(StringRef Str, uint64_t Size) {
201   assert(Size >= Str.size());
202   W.OS << Str;
203   W.OS.write_zeros(Size - Str.size());
204 }
205 
206 /// writeSegmentLoadCommand - Write a segment load command.
207 ///
208 /// \param NumSections The number of sections in this segment.
209 /// \param SectionDataSize The total size of the sections.
210 void MachObjectWriter::writeSegmentLoadCommand(
211     StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize,
212     uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt,
213     uint32_t InitProt) {
214   // struct segment_command (56 bytes) or
215   // struct segment_command_64 (72 bytes)
216 
217   uint64_t Start = W.OS.tell();
218   (void) Start;
219 
220   unsigned SegmentLoadCommandSize =
221     is64Bit() ? sizeof(MachO::segment_command_64):
222     sizeof(MachO::segment_command);
223   W.write<uint32_t>(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT);
224   W.write<uint32_t>(SegmentLoadCommandSize +
225           NumSections * (is64Bit() ? sizeof(MachO::section_64) :
226                          sizeof(MachO::section)));
227 
228   writeWithPadding(Name, 16);
229   if (is64Bit()) {
230     W.write<uint64_t>(VMAddr);                 // vmaddr
231     W.write<uint64_t>(VMSize); // vmsize
232     W.write<uint64_t>(SectionDataStartOffset); // file offset
233     W.write<uint64_t>(SectionDataSize); // file size
234   } else {
235     W.write<uint32_t>(VMAddr);                 // vmaddr
236     W.write<uint32_t>(VMSize); // vmsize
237     W.write<uint32_t>(SectionDataStartOffset); // file offset
238     W.write<uint32_t>(SectionDataSize); // file size
239   }
240   // maxprot
241   W.write<uint32_t>(MaxProt);
242   // initprot
243   W.write<uint32_t>(InitProt);
244   W.write<uint32_t>(NumSections);
245   W.write<uint32_t>(0); // flags
246 
247   assert(W.OS.tell() - Start == SegmentLoadCommandSize);
248 }
249 
250 void MachObjectWriter::writeSection(const MCAssembler &Asm,
251                                     const MCSection &Sec, uint64_t VMAddr,
252                                     uint64_t FileOffset, unsigned Flags,
253                                     uint64_t RelocationsStart,
254                                     unsigned NumRelocations) {
255   uint64_t SectionSize = Asm.getSectionAddressSize(Sec);
256   const MCSectionMachO &Section = cast<MCSectionMachO>(Sec);
257 
258   // The offset is unused for virtual sections.
259   if (Section.isVirtualSection()) {
260     assert(Asm.getSectionFileSize(Sec) == 0 && "Invalid file size!");
261     FileOffset = 0;
262   }
263 
264   // struct section (68 bytes) or
265   // struct section_64 (80 bytes)
266 
267   uint64_t Start = W.OS.tell();
268   (void) Start;
269 
270   writeWithPadding(Section.getName(), 16);
271   writeWithPadding(Section.getSegmentName(), 16);
272   if (is64Bit()) {
273     W.write<uint64_t>(VMAddr);      // address
274     W.write<uint64_t>(SectionSize); // size
275   } else {
276     W.write<uint32_t>(VMAddr);      // address
277     W.write<uint32_t>(SectionSize); // size
278   }
279   W.write<uint32_t>(FileOffset);
280 
281   W.write<uint32_t>(Log2(Section.getAlign()));
282   W.write<uint32_t>(NumRelocations ? RelocationsStart : 0);
283   W.write<uint32_t>(NumRelocations);
284   W.write<uint32_t>(Flags);
285   W.write<uint32_t>(IndirectSymBase.lookup(&Sec)); // reserved1
286   W.write<uint32_t>(Section.getStubSize()); // reserved2
287   if (is64Bit())
288     W.write<uint32_t>(0); // reserved3
289 
290   assert(W.OS.tell() - Start ==
291          (is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section)));
292 }
293 
294 void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset,
295                                               uint32_t NumSymbols,
296                                               uint32_t StringTableOffset,
297                                               uint32_t StringTableSize) {
298   // struct symtab_command (24 bytes)
299 
300   uint64_t Start = W.OS.tell();
301   (void) Start;
302 
303   W.write<uint32_t>(MachO::LC_SYMTAB);
304   W.write<uint32_t>(sizeof(MachO::symtab_command));
305   W.write<uint32_t>(SymbolOffset);
306   W.write<uint32_t>(NumSymbols);
307   W.write<uint32_t>(StringTableOffset);
308   W.write<uint32_t>(StringTableSize);
309 
310   assert(W.OS.tell() - Start == sizeof(MachO::symtab_command));
311 }
312 
313 void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol,
314                                                 uint32_t NumLocalSymbols,
315                                                 uint32_t FirstExternalSymbol,
316                                                 uint32_t NumExternalSymbols,
317                                                 uint32_t FirstUndefinedSymbol,
318                                                 uint32_t NumUndefinedSymbols,
319                                                 uint32_t IndirectSymbolOffset,
320                                                 uint32_t NumIndirectSymbols) {
321   // struct dysymtab_command (80 bytes)
322 
323   uint64_t Start = W.OS.tell();
324   (void) Start;
325 
326   W.write<uint32_t>(MachO::LC_DYSYMTAB);
327   W.write<uint32_t>(sizeof(MachO::dysymtab_command));
328   W.write<uint32_t>(FirstLocalSymbol);
329   W.write<uint32_t>(NumLocalSymbols);
330   W.write<uint32_t>(FirstExternalSymbol);
331   W.write<uint32_t>(NumExternalSymbols);
332   W.write<uint32_t>(FirstUndefinedSymbol);
333   W.write<uint32_t>(NumUndefinedSymbols);
334   W.write<uint32_t>(0); // tocoff
335   W.write<uint32_t>(0); // ntoc
336   W.write<uint32_t>(0); // modtaboff
337   W.write<uint32_t>(0); // nmodtab
338   W.write<uint32_t>(0); // extrefsymoff
339   W.write<uint32_t>(0); // nextrefsyms
340   W.write<uint32_t>(IndirectSymbolOffset);
341   W.write<uint32_t>(NumIndirectSymbols);
342   W.write<uint32_t>(0); // extreloff
343   W.write<uint32_t>(0); // nextrel
344   W.write<uint32_t>(0); // locreloff
345   W.write<uint32_t>(0); // nlocrel
346 
347   assert(W.OS.tell() - Start == sizeof(MachO::dysymtab_command));
348 }
349 
350 MachObjectWriter::MachSymbolData *
351 MachObjectWriter::findSymbolData(const MCSymbol &Sym) {
352   for (auto *SymbolData :
353        {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
354     for (MachSymbolData &Entry : *SymbolData)
355       if (Entry.Symbol == &Sym)
356         return &Entry;
357 
358   return nullptr;
359 }
360 
361 const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const {
362   const MCSymbol *S = &Sym;
363   while (S->isVariable()) {
364     const MCExpr *Value = S->getVariableValue();
365     const auto *Ref = dyn_cast<MCSymbolRefExpr>(Value);
366     if (!Ref)
367       return *S;
368     S = &Ref->getSymbol();
369   }
370   return *S;
371 }
372 
373 void MachObjectWriter::writeNlist(MachSymbolData &MSD,
374                                   const MCAsmLayout &Layout) {
375   const MCSymbol *Symbol = MSD.Symbol;
376   const MCSymbol &Data = *Symbol;
377   const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol);
378   uint8_t SectionIndex = MSD.SectionIndex;
379   uint8_t Type = 0;
380   uint64_t Address = 0;
381   bool IsAlias = Symbol != AliasedSymbol;
382 
383   const MCSymbol &OrigSymbol = *Symbol;
384   MachSymbolData *AliaseeInfo;
385   if (IsAlias) {
386     AliaseeInfo = findSymbolData(*AliasedSymbol);
387     if (AliaseeInfo)
388       SectionIndex = AliaseeInfo->SectionIndex;
389     Symbol = AliasedSymbol;
390     // FIXME: Should this update Data as well?
391   }
392 
393   // Set the N_TYPE bits. See <mach-o/nlist.h>.
394   //
395   // FIXME: Are the prebound or indirect fields possible here?
396   if (IsAlias && Symbol->isUndefined())
397     Type = MachO::N_INDR;
398   else if (Symbol->isUndefined())
399     Type = MachO::N_UNDF;
400   else if (Symbol->isAbsolute())
401     Type = MachO::N_ABS;
402   else
403     Type = MachO::N_SECT;
404 
405   // FIXME: Set STAB bits.
406 
407   if (Data.isPrivateExtern())
408     Type |= MachO::N_PEXT;
409 
410   // Set external bit.
411   if (Data.isExternal() || (!IsAlias && Symbol->isUndefined()))
412     Type |= MachO::N_EXT;
413 
414   // Compute the symbol address.
415   if (IsAlias && Symbol->isUndefined())
416     Address = AliaseeInfo->StringIndex;
417   else if (Symbol->isDefined())
418     Address = getSymbolAddress(OrigSymbol, Layout);
419   else if (Symbol->isCommon()) {
420     // Common symbols are encoded with the size in the address
421     // field, and their alignment in the flags.
422     Address = Symbol->getCommonSize();
423   }
424 
425   // struct nlist (12 bytes)
426 
427   W.write<uint32_t>(MSD.StringIndex);
428   W.OS << char(Type);
429   W.OS << char(SectionIndex);
430 
431   // The Mach-O streamer uses the lowest 16-bits of the flags for the 'desc'
432   // value.
433   bool EncodeAsAltEntry =
434     IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry();
435   W.write<uint16_t>(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry));
436   if (is64Bit())
437     W.write<uint64_t>(Address);
438   else
439     W.write<uint32_t>(Address);
440 }
441 
442 void MachObjectWriter::writeLinkeditLoadCommand(uint32_t Type,
443                                                 uint32_t DataOffset,
444                                                 uint32_t DataSize) {
445   uint64_t Start = W.OS.tell();
446   (void) Start;
447 
448   W.write<uint32_t>(Type);
449   W.write<uint32_t>(sizeof(MachO::linkedit_data_command));
450   W.write<uint32_t>(DataOffset);
451   W.write<uint32_t>(DataSize);
452 
453   assert(W.OS.tell() - Start == sizeof(MachO::linkedit_data_command));
454 }
455 
456 static unsigned ComputeLinkerOptionsLoadCommandSize(
457   const std::vector<std::string> &Options, bool is64Bit)
458 {
459   unsigned Size = sizeof(MachO::linker_option_command);
460   for (const std::string &Option : Options)
461     Size += Option.size() + 1;
462   return alignTo(Size, is64Bit ? 8 : 4);
463 }
464 
465 void MachObjectWriter::writeLinkerOptionsLoadCommand(
466   const std::vector<std::string> &Options)
467 {
468   unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit());
469   uint64_t Start = W.OS.tell();
470   (void) Start;
471 
472   W.write<uint32_t>(MachO::LC_LINKER_OPTION);
473   W.write<uint32_t>(Size);
474   W.write<uint32_t>(Options.size());
475   uint64_t BytesWritten = sizeof(MachO::linker_option_command);
476   for (const std::string &Option : Options) {
477     // Write each string, including the null byte.
478     W.OS << Option << '\0';
479     BytesWritten += Option.size() + 1;
480   }
481 
482   // Pad to a multiple of the pointer size.
483   W.OS.write_zeros(
484       offsetToAlignment(BytesWritten, is64Bit() ? Align(8) : Align(4)));
485 
486   assert(W.OS.tell() - Start == Size);
487 }
488 
489 static bool isFixupTargetValid(const MCValue &Target) {
490   // Target is (LHS - RHS + cst).
491   // We don't support the form where LHS is null: -RHS + cst
492   if (!Target.getSymA() && Target.getSymB())
493     return false;
494   return true;
495 }
496 
497 void MachObjectWriter::recordRelocation(MCAssembler &Asm,
498                                         const MCFragment *Fragment,
499                                         const MCFixup &Fixup, MCValue Target,
500                                         uint64_t &FixedValue) {
501   if (!isFixupTargetValid(Target)) {
502     Asm.getContext().reportError(Fixup.getLoc(),
503                                  "unsupported relocation expression");
504     return;
505   }
506 
507   TargetObjectWriter->recordRelocation(this, Asm, Fragment, Fixup, Target,
508                                        FixedValue);
509 }
510 
511 void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) {
512   // This is the point where 'as' creates actual symbols for indirect symbols
513   // (in the following two passes). It would be easier for us to do this sooner
514   // when we see the attribute, but that makes getting the order in the symbol
515   // table much more complicated than it is worth.
516   //
517   // FIXME: Revisit this when the dust settles.
518 
519   // Report errors for use of .indirect_symbol not in a symbol pointer section
520   // or stub section.
521   for (IndirectSymbolData &ISD : llvm::make_range(Asm.indirect_symbol_begin(),
522                                                   Asm.indirect_symbol_end())) {
523     const MCSectionMachO &Section = cast<MCSectionMachO>(*ISD.Section);
524 
525     if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
526         Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
527         Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS &&
528         Section.getType() != MachO::S_SYMBOL_STUBS) {
529       MCSymbol &Symbol = *ISD.Symbol;
530       report_fatal_error("indirect symbol '" + Symbol.getName() +
531                          "' not in a symbol pointer or stub section");
532     }
533   }
534 
535   // Bind non-lazy symbol pointers first.
536   unsigned IndirectIndex = 0;
537   for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
538          ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
539     const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section);
540 
541     if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS &&
542         Section.getType() !=  MachO::S_THREAD_LOCAL_VARIABLE_POINTERS)
543       continue;
544 
545     // Initialize the section indirect symbol base, if necessary.
546     IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
547 
548     Asm.registerSymbol(*it->Symbol);
549   }
550 
551   // Then lazy symbol pointers and symbol stubs.
552   IndirectIndex = 0;
553   for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(),
554          ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) {
555     const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section);
556 
557     if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS &&
558         Section.getType() != MachO::S_SYMBOL_STUBS)
559       continue;
560 
561     // Initialize the section indirect symbol base, if necessary.
562     IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex));
563 
564     // Set the symbol type to undefined lazy, but only on construction.
565     //
566     // FIXME: Do not hardcode.
567     if (Asm.registerSymbol(*it->Symbol))
568       cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(true);
569   }
570 }
571 
572 /// computeSymbolTable - Compute the symbol table data
573 void MachObjectWriter::computeSymbolTable(
574     MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData,
575     std::vector<MachSymbolData> &ExternalSymbolData,
576     std::vector<MachSymbolData> &UndefinedSymbolData) {
577   // Build section lookup table.
578   DenseMap<const MCSection*, uint8_t> SectionIndexMap;
579   unsigned Index = 1;
580   for (MCAssembler::iterator it = Asm.begin(),
581          ie = Asm.end(); it != ie; ++it, ++Index)
582     SectionIndexMap[&*it] = Index;
583   assert(Index <= 256 && "Too many sections!");
584 
585   // Build the string table.
586   for (const MCSymbol &Symbol : Asm.symbols()) {
587     if (!Asm.isSymbolLinkerVisible(Symbol))
588       continue;
589 
590     StringTable.add(Symbol.getName());
591   }
592   StringTable.finalize();
593 
594   // Build the symbol arrays but only for non-local symbols.
595   //
596   // The particular order that we collect and then sort the symbols is chosen to
597   // match 'as'. Even though it doesn't matter for correctness, this is
598   // important for letting us diff .o files.
599   for (const MCSymbol &Symbol : Asm.symbols()) {
600     // Ignore non-linker visible symbols.
601     if (!Asm.isSymbolLinkerVisible(Symbol))
602       continue;
603 
604     if (!Symbol.isExternal() && !Symbol.isUndefined())
605       continue;
606 
607     MachSymbolData MSD;
608     MSD.Symbol = &Symbol;
609     MSD.StringIndex = StringTable.getOffset(Symbol.getName());
610 
611     if (Symbol.isUndefined()) {
612       MSD.SectionIndex = 0;
613       UndefinedSymbolData.push_back(MSD);
614     } else if (Symbol.isAbsolute()) {
615       MSD.SectionIndex = 0;
616       ExternalSymbolData.push_back(MSD);
617     } else {
618       MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
619       assert(MSD.SectionIndex && "Invalid section index!");
620       ExternalSymbolData.push_back(MSD);
621     }
622   }
623 
624   // Now add the data for local symbols.
625   for (const MCSymbol &Symbol : Asm.symbols()) {
626     // Ignore non-linker visible symbols.
627     if (!Asm.isSymbolLinkerVisible(Symbol))
628       continue;
629 
630     if (Symbol.isExternal() || Symbol.isUndefined())
631       continue;
632 
633     MachSymbolData MSD;
634     MSD.Symbol = &Symbol;
635     MSD.StringIndex = StringTable.getOffset(Symbol.getName());
636 
637     if (Symbol.isAbsolute()) {
638       MSD.SectionIndex = 0;
639       LocalSymbolData.push_back(MSD);
640     } else {
641       MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection());
642       assert(MSD.SectionIndex && "Invalid section index!");
643       LocalSymbolData.push_back(MSD);
644     }
645   }
646 
647   // External and undefined symbols are required to be in lexicographic order.
648   llvm::sort(ExternalSymbolData);
649   llvm::sort(UndefinedSymbolData);
650 
651   // Set the symbol indices.
652   Index = 0;
653   for (auto *SymbolData :
654        {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
655     for (MachSymbolData &Entry : *SymbolData)
656       Entry.Symbol->setIndex(Index++);
657 
658   for (const MCSection &Section : Asm) {
659     for (RelAndSymbol &Rel : Relocations[&Section]) {
660       if (!Rel.Sym)
661         continue;
662 
663       // Set the Index and the IsExtern bit.
664       unsigned Index = Rel.Sym->getIndex();
665       assert(isInt<24>(Index));
666       if (W.Endian == llvm::endianness::little)
667         Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27);
668       else
669         Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4);
670     }
671   }
672 }
673 
674 void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm) {
675   // Assign layout order indices to sections.
676   unsigned i = 0;
677   // Compute the section layout order. Virtual sections must go last.
678   for (MCSection &Sec : Asm) {
679     if (!Sec.isVirtualSection()) {
680       SectionOrder.push_back(&Sec);
681       Sec.setLayoutOrder(i++);
682     }
683   }
684   for (MCSection &Sec : Asm) {
685     if (Sec.isVirtualSection()) {
686       SectionOrder.push_back(&Sec);
687       Sec.setLayoutOrder(i++);
688     }
689   }
690 
691   uint64_t StartAddress = 0;
692   for (const MCSection *Sec : SectionOrder) {
693     StartAddress = alignTo(StartAddress, Sec->getAlign());
694     SectionAddress[Sec] = StartAddress;
695     StartAddress += Asm.getSectionAddressSize(*Sec);
696 
697     // Explicitly pad the section to match the alignment requirements of the
698     // following one. This is for 'gas' compatibility, it shouldn't
699     /// strictly be necessary.
700     StartAddress += getPaddingSize(Asm, Sec);
701   }
702 }
703 
704 void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm) {
705   computeSectionAddresses(Asm);
706 
707   // Create symbol data for any indirect symbols.
708   bindIndirectSymbols(Asm);
709 }
710 
711 bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
712     const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
713     bool InSet, bool IsPCRel) const {
714   if (InSet)
715     return true;
716 
717   // The effective address is
718   //     addr(atom(A)) + offset(A)
719   //   - addr(atom(B)) - offset(B)
720   // and the offsets are not relocatable, so the fixup is fully resolved when
721   //  addr(atom(A)) - addr(atom(B)) == 0.
722   const MCSymbol &SA = findAliasedSymbol(SymA);
723   const MCSection &SecA = SA.getSection();
724   const MCSection &SecB = *FB.getParent();
725 
726   if (IsPCRel) {
727     // The simple (Darwin, except on x86_64) way of dealing with this was to
728     // assume that any reference to a temporary symbol *must* be a temporary
729     // symbol in the same atom, unless the sections differ. Therefore, any PCrel
730     // relocation to a temporary symbol (in the same section) is fully
731     // resolved. This also works in conjunction with absolutized .set, which
732     // requires the compiler to use .set to absolutize the differences between
733     // symbols which the compiler knows to be assembly time constants, so we
734     // don't need to worry about considering symbol differences fully resolved.
735     //
736     // If the file isn't using sub-sections-via-symbols, we can make the
737     // same assumptions about any symbol that we normally make about
738     // assembler locals.
739 
740     bool hasReliableSymbolDifference = isX86_64();
741     if (!hasReliableSymbolDifference) {
742       if (!SA.isInSection() || &SecA != &SecB ||
743           (!SA.isTemporary() && FB.getAtom() != SA.getFragment()->getAtom() &&
744            Asm.getSubsectionsViaSymbols()))
745         return false;
746       return true;
747     }
748   }
749 
750   // If they are not in the same section, we can't compute the diff.
751   if (&SecA != &SecB)
752     return false;
753 
754   // If the atoms are the same, they are guaranteed to have the same address.
755   return SA.getFragment()->getAtom() == FB.getAtom();
756 }
757 
758 static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type) {
759   switch (Type) {
760   case MCVM_OSXVersionMin:     return MachO::LC_VERSION_MIN_MACOSX;
761   case MCVM_IOSVersionMin:     return MachO::LC_VERSION_MIN_IPHONEOS;
762   case MCVM_TvOSVersionMin:    return MachO::LC_VERSION_MIN_TVOS;
763   case MCVM_WatchOSVersionMin: return MachO::LC_VERSION_MIN_WATCHOS;
764   }
765   llvm_unreachable("Invalid mc version min type");
766 }
767 
768 void MachObjectWriter::populateAddrSigSection(MCAssembler &Asm) {
769   MCSection *AddrSigSection =
770       Asm.getContext().getObjectFileInfo()->getAddrSigSection();
771   unsigned Log2Size = is64Bit() ? 3 : 2;
772   for (const MCSymbol *S : getAddrsigSyms()) {
773     if (!S->isRegistered())
774       continue;
775     MachO::any_relocation_info MRE;
776     MRE.r_word0 = 0;
777     MRE.r_word1 = (Log2Size << 25) | (MachO::GENERIC_RELOC_VANILLA << 28);
778     addRelocation(S, AddrSigSection, MRE);
779   }
780 }
781 
782 uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
783   auto &Layout = *Asm.getLayout();
784   uint64_t StartOffset = W.OS.tell();
785 
786   populateAddrSigSection(Asm);
787 
788   // Compute symbol table information and bind symbol indices.
789   computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData,
790                      UndefinedSymbolData);
791 
792   if (!Asm.CGProfile.empty()) {
793     MCSection *CGProfileSection = Asm.getContext().getMachOSection(
794         "__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
795     auto &Frag = cast<MCDataFragment>(*CGProfileSection->begin());
796     Frag.getContents().clear();
797     raw_svector_ostream OS(Frag.getContents());
798     for (const MCAssembler::CGProfileEntry &CGPE : Asm.CGProfile) {
799       uint32_t FromIndex = CGPE.From->getSymbol().getIndex();
800       uint32_t ToIndex = CGPE.To->getSymbol().getIndex();
801       support::endian::write(OS, FromIndex, W.Endian);
802       support::endian::write(OS, ToIndex, W.Endian);
803       support::endian::write(OS, CGPE.Count, W.Endian);
804     }
805   }
806 
807   unsigned NumSections = Asm.size();
808   const MCAssembler::VersionInfoType &VersionInfo = Asm.getVersionInfo();
809 
810   // The section data starts after the header, the segment load command (and
811   // section headers) and the symbol table.
812   unsigned NumLoadCommands = 1;
813   uint64_t LoadCommandsSize = is64Bit() ?
814     sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64):
815     sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section);
816 
817   // Add the deployment target version info load command size, if used.
818   if (VersionInfo.Major != 0) {
819     ++NumLoadCommands;
820     if (VersionInfo.EmitBuildVersion)
821       LoadCommandsSize += sizeof(MachO::build_version_command);
822     else
823       LoadCommandsSize += sizeof(MachO::version_min_command);
824   }
825 
826   const MCAssembler::VersionInfoType &TargetVariantVersionInfo =
827       Asm.getDarwinTargetVariantVersionInfo();
828 
829   // Add the target variant version info load command size, if used.
830   if (TargetVariantVersionInfo.Major != 0) {
831     ++NumLoadCommands;
832     assert(TargetVariantVersionInfo.EmitBuildVersion &&
833            "target variant should use build version");
834     LoadCommandsSize += sizeof(MachO::build_version_command);
835   }
836 
837   // Add the data-in-code load command size, if used.
838   unsigned NumDataRegions = Asm.getDataRegions().size();
839   if (NumDataRegions) {
840     ++NumLoadCommands;
841     LoadCommandsSize += sizeof(MachO::linkedit_data_command);
842   }
843 
844   // Add the loh load command size, if used.
845   uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout);
846   uint64_t LOHSize = alignTo(LOHRawSize, is64Bit() ? 8 : 4);
847   if (LOHSize) {
848     ++NumLoadCommands;
849     LoadCommandsSize += sizeof(MachO::linkedit_data_command);
850   }
851 
852   // Add the symbol table load command sizes, if used.
853   unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() +
854     UndefinedSymbolData.size();
855   if (NumSymbols) {
856     NumLoadCommands += 2;
857     LoadCommandsSize += (sizeof(MachO::symtab_command) +
858                          sizeof(MachO::dysymtab_command));
859   }
860 
861   // Add the linker option load commands sizes.
862   for (const auto &Option : Asm.getLinkerOptions()) {
863     ++NumLoadCommands;
864     LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(Option, is64Bit());
865   }
866 
867   // Compute the total size of the section data, as well as its file size and vm
868   // size.
869   uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) :
870                                sizeof(MachO::mach_header)) + LoadCommandsSize;
871   uint64_t SectionDataSize = 0;
872   uint64_t SectionDataFileSize = 0;
873   uint64_t VMSize = 0;
874   for (const MCSection &Sec : Asm) {
875     uint64_t Address = getSectionAddress(&Sec);
876     uint64_t Size = Asm.getSectionAddressSize(Sec);
877     uint64_t FileSize = Asm.getSectionFileSize(Sec);
878     FileSize += getPaddingSize(Asm, &Sec);
879 
880     VMSize = std::max(VMSize, Address + Size);
881 
882     if (Sec.isVirtualSection())
883       continue;
884 
885     SectionDataSize = std::max(SectionDataSize, Address + Size);
886     SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize);
887   }
888 
889   // The section data is padded to pointer size bytes.
890   //
891   // FIXME: Is this machine dependent?
892   unsigned SectionDataPadding =
893       offsetToAlignment(SectionDataFileSize, is64Bit() ? Align(8) : Align(4));
894   SectionDataFileSize += SectionDataPadding;
895 
896   // Write the prolog, starting with the header and load command...
897   writeHeader(MachO::MH_OBJECT, NumLoadCommands, LoadCommandsSize,
898               Asm.getSubsectionsViaSymbols());
899   uint32_t Prot =
900       MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE;
901   writeSegmentLoadCommand("", NumSections, 0, VMSize, SectionDataStart,
902                           SectionDataSize, Prot, Prot);
903 
904   // ... and then the section headers.
905   uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize;
906   for (const MCSection &Section : Asm) {
907     const auto &Sec = cast<MCSectionMachO>(Section);
908     std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
909     unsigned NumRelocs = Relocs.size();
910     uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec);
911     unsigned Flags = Sec.getTypeAndAttributes();
912     if (Sec.hasInstructions())
913       Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS;
914     writeSection(Asm, Sec, getSectionAddress(&Sec), SectionStart, Flags,
915                  RelocTableEnd, NumRelocs);
916     RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info);
917   }
918 
919   // Write out the deployment target information, if it's available.
920   auto EmitDeploymentTargetVersion =
921       [&](const MCAssembler::VersionInfoType &VersionInfo) {
922         auto EncodeVersion = [](VersionTuple V) -> uint32_t {
923           assert(!V.empty() && "empty version");
924           unsigned Update = V.getSubminor().value_or(0);
925           unsigned Minor = V.getMinor().value_or(0);
926           assert(Update < 256 && "unencodable update target version");
927           assert(Minor < 256 && "unencodable minor target version");
928           assert(V.getMajor() < 65536 && "unencodable major target version");
929           return Update | (Minor << 8) | (V.getMajor() << 16);
930         };
931         uint32_t EncodedVersion = EncodeVersion(VersionTuple(
932             VersionInfo.Major, VersionInfo.Minor, VersionInfo.Update));
933         uint32_t SDKVersion = !VersionInfo.SDKVersion.empty()
934                                   ? EncodeVersion(VersionInfo.SDKVersion)
935                                   : 0;
936         if (VersionInfo.EmitBuildVersion) {
937           // FIXME: Currently empty tools. Add clang version in the future.
938           W.write<uint32_t>(MachO::LC_BUILD_VERSION);
939           W.write<uint32_t>(sizeof(MachO::build_version_command));
940           W.write<uint32_t>(VersionInfo.TypeOrPlatform.Platform);
941           W.write<uint32_t>(EncodedVersion);
942           W.write<uint32_t>(SDKVersion);
943           W.write<uint32_t>(0); // Empty tools list.
944         } else {
945           MachO::LoadCommandType LCType =
946               getLCFromMCVM(VersionInfo.TypeOrPlatform.Type);
947           W.write<uint32_t>(LCType);
948           W.write<uint32_t>(sizeof(MachO::version_min_command));
949           W.write<uint32_t>(EncodedVersion);
950           W.write<uint32_t>(SDKVersion);
951         }
952       };
953   if (VersionInfo.Major != 0)
954     EmitDeploymentTargetVersion(VersionInfo);
955   if (TargetVariantVersionInfo.Major != 0)
956     EmitDeploymentTargetVersion(TargetVariantVersionInfo);
957 
958   // Write the data-in-code load command, if used.
959   uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8;
960   if (NumDataRegions) {
961     uint64_t DataRegionsOffset = RelocTableEnd;
962     uint64_t DataRegionsSize = NumDataRegions * 8;
963     writeLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset,
964                              DataRegionsSize);
965   }
966 
967   // Write the loh load command, if used.
968   uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize;
969   if (LOHSize)
970     writeLinkeditLoadCommand(MachO::LC_LINKER_OPTIMIZATION_HINT,
971                              DataInCodeTableEnd, LOHSize);
972 
973   // Write the symbol table load command, if used.
974   if (NumSymbols) {
975     unsigned FirstLocalSymbol = 0;
976     unsigned NumLocalSymbols = LocalSymbolData.size();
977     unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols;
978     unsigned NumExternalSymbols = ExternalSymbolData.size();
979     unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols;
980     unsigned NumUndefinedSymbols = UndefinedSymbolData.size();
981     unsigned NumIndirectSymbols = Asm.indirect_symbol_size();
982     unsigned NumSymTabSymbols =
983       NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols;
984     uint64_t IndirectSymbolSize = NumIndirectSymbols * 4;
985     uint64_t IndirectSymbolOffset = 0;
986 
987     // If used, the indirect symbols are written after the section data.
988     if (NumIndirectSymbols)
989       IndirectSymbolOffset = LOHTableEnd;
990 
991     // The symbol table is written after the indirect symbol data.
992     uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize;
993 
994     // The string table is written after symbol table.
995     uint64_t StringTableOffset =
996       SymbolTableOffset + NumSymTabSymbols * (is64Bit() ?
997                                               sizeof(MachO::nlist_64) :
998                                               sizeof(MachO::nlist));
999     writeSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols,
1000                            StringTableOffset, StringTable.getSize());
1001 
1002     writeDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols,
1003                              FirstExternalSymbol, NumExternalSymbols,
1004                              FirstUndefinedSymbol, NumUndefinedSymbols,
1005                              IndirectSymbolOffset, NumIndirectSymbols);
1006   }
1007 
1008   // Write the linker options load commands.
1009   for (const auto &Option : Asm.getLinkerOptions())
1010     writeLinkerOptionsLoadCommand(Option);
1011 
1012   // Write the actual section data.
1013   for (const MCSection &Sec : Asm) {
1014     Asm.writeSectionData(W.OS, &Sec);
1015 
1016     uint64_t Pad = getPaddingSize(Asm, &Sec);
1017     W.OS.write_zeros(Pad);
1018   }
1019 
1020   // Write the extra padding.
1021   W.OS.write_zeros(SectionDataPadding);
1022 
1023   // Write the relocation entries.
1024   for (const MCSection &Sec : Asm) {
1025     // Write the section relocation entries, in reverse order to match 'as'
1026     // (approximately, the exact algorithm is more complicated than this).
1027     std::vector<RelAndSymbol> &Relocs = Relocations[&Sec];
1028     for (const RelAndSymbol &Rel : llvm::reverse(Relocs)) {
1029       W.write<uint32_t>(Rel.MRE.r_word0);
1030       W.write<uint32_t>(Rel.MRE.r_word1);
1031     }
1032   }
1033 
1034   // Write out the data-in-code region payload, if there is one.
1035   for (MCAssembler::const_data_region_iterator
1036          it = Asm.data_region_begin(), ie = Asm.data_region_end();
1037          it != ie; ++it) {
1038     const DataRegionData *Data = &(*it);
1039     uint64_t Start = getSymbolAddress(*Data->Start, Layout);
1040     uint64_t End;
1041     if (Data->End)
1042       End = getSymbolAddress(*Data->End, Layout);
1043     else
1044       report_fatal_error("Data region not terminated");
1045 
1046     LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind
1047                       << "  start: " << Start << "(" << Data->Start->getName()
1048                       << ")"
1049                       << "  end: " << End << "(" << Data->End->getName() << ")"
1050                       << "  size: " << End - Start << "\n");
1051     W.write<uint32_t>(Start);
1052     W.write<uint16_t>(End - Start);
1053     W.write<uint16_t>(Data->Kind);
1054   }
1055 
1056   // Write out the loh commands, if there is one.
1057   if (LOHSize) {
1058 #ifndef NDEBUG
1059     unsigned Start = W.OS.tell();
1060 #endif
1061     Asm.getLOHContainer().emit(*this, Layout);
1062     // Pad to a multiple of the pointer size.
1063     W.OS.write_zeros(
1064         offsetToAlignment(LOHRawSize, is64Bit() ? Align(8) : Align(4)));
1065     assert(W.OS.tell() - Start == LOHSize);
1066   }
1067 
1068   // Write the symbol table data, if used.
1069   if (NumSymbols) {
1070     // Write the indirect symbol entries.
1071     for (MCAssembler::const_indirect_symbol_iterator
1072            it = Asm.indirect_symbol_begin(),
1073            ie = Asm.indirect_symbol_end(); it != ie; ++it) {
1074       // Indirect symbols in the non-lazy symbol pointer section have some
1075       // special handling.
1076       const MCSectionMachO &Section =
1077           static_cast<const MCSectionMachO &>(*it->Section);
1078       if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) {
1079         // If this symbol is defined and internal, mark it as such.
1080         if (it->Symbol->isDefined() && !it->Symbol->isExternal()) {
1081           uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL;
1082           if (it->Symbol->isAbsolute())
1083             Flags |= MachO::INDIRECT_SYMBOL_ABS;
1084           W.write<uint32_t>(Flags);
1085           continue;
1086         }
1087       }
1088 
1089       W.write<uint32_t>(it->Symbol->getIndex());
1090     }
1091 
1092     // FIXME: Check that offsets match computed ones.
1093 
1094     // Write the symbol table entries.
1095     for (auto *SymbolData :
1096          {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData})
1097       for (MachSymbolData &Entry : *SymbolData)
1098         writeNlist(Entry, Layout);
1099 
1100     // Write the string table.
1101     StringTable.write(W.OS);
1102   }
1103 
1104   return W.OS.tell() - StartOffset;
1105 }
1106 
1107 std::unique_ptr<MCObjectWriter>
1108 llvm::createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW,
1109                              raw_pwrite_stream &OS, bool IsLittleEndian) {
1110   return std::make_unique<MachObjectWriter>(std::move(MOTW), OS,
1111                                              IsLittleEndian);
1112 }
1113