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