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