1//===- MCMachOStreamer.cpp - MachO Streamer -------------------------------===//
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/SmallString.h"
11#include "llvm/ADT/SmallVector.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/MC/MCAsmBackend.h"
14#include "llvm/MC/MCAssembler.h"
15#include "llvm/MC/MCCodeEmitter.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCDirectives.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCFixup.h"
20#include "llvm/MC/MCFragment.h"
21#include "llvm/MC/MCLinkerOptimizationHint.h"
22#include "llvm/MC/MCObjectFileInfo.h"
23#include "llvm/MC/MCObjectStreamer.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSection.h"
26#include "llvm/MC/MCSectionMachO.h"
27#include "llvm/MC/MCSymbol.h"
28#include "llvm/MC/MCSymbolMachO.h"
29#include "llvm/MC/MCValue.h"
30#include "llvm/MC/SectionKind.h"
31#include "llvm/MC/TargetRegistry.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/ErrorHandling.h"
34#include <cassert>
35#include <vector>
36
37namespace llvm {
38class MCInst;
39class MCStreamer;
40class MCSubtargetInfo;
41class Triple;
42} // namespace llvm
43
44using namespace llvm;
45
46namespace {
47
48class MCMachOStreamer : public MCObjectStreamer {
49private:
50 /// LabelSections - true if each section change should emit a linker local
51 /// label for use in relocations for assembler local references. Obviates the
52 /// need for local relocations. False by default.
53 bool LabelSections;
54
55 bool DWARFMustBeAtTheEnd;
56 bool CreatedADWARFSection;
57
58 /// HasSectionLabel - map of which sections have already had a non-local
59 /// label emitted to them. Used so we don't emit extraneous linker local
60 /// labels in the middle of the section.
61 DenseMap<const MCSection*, bool> HasSectionLabel;
62
63 void emitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
64
65 void emitDataRegion(DataRegionData::KindTy Kind);
66 void emitDataRegionEnd();
67
68public:
69 MCMachOStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
70 std::unique_ptr<MCObjectWriter> OW,
71 std::unique_ptr<MCCodeEmitter> Emitter,
72 bool DWARFMustBeAtTheEnd, bool label)
73 : MCObjectStreamer(Context, std::move(MAB), std::move(OW),
74 std::move(Emitter)),
75 LabelSections(label), DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd),
76 CreatedADWARFSection(false) {}
77
78 /// state management
79 void reset() override {
80 CreatedADWARFSection = false;
81 HasSectionLabel.clear();
82 MCObjectStreamer::reset();
83 }
84
85 /// @name MCStreamer Interface
86 /// @{
87
88 void changeSection(MCSection *Sect, const MCExpr *Subsect) override;
89 void emitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
90 void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
91 void emitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
92 void emitAssemblerFlag(MCAssemblerFlag Flag) override;
93 void emitLinkerOptions(ArrayRef<std::string> Options) override;
94 void emitDataRegion(MCDataRegionType Kind) override;
95 void emitVersionMin(MCVersionMinType Kind, unsigned Major, unsigned Minor,
96 unsigned Update, VersionTuple SDKVersion) override;
97 void emitBuildVersion(unsigned Platform, unsigned Major, unsigned Minor,
98 unsigned Update, VersionTuple SDKVersion) override;
99 void emitDarwinTargetVariantBuildVersion(unsigned Platform, unsigned Major,
100 unsigned Minor, unsigned Update,
101 VersionTuple SDKVersion) override;
102 void emitThumbFunc(MCSymbol *Func) override;
103 bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
104 void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
105 void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
106 Align ByteAlignment) override;
107
108 void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
109 Align ByteAlignment) override;
110 void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
111 uint64_t Size = 0, Align ByteAlignment = Align(1),
112 SMLoc Loc = SMLoc()) override;
113 void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
114 Align ByteAlignment = Align(1)) override;
115
116 void emitIdent(StringRef IdentString) override {
117 llvm_unreachable("macho doesn't support this directive");
118 }
119
120 void emitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
121 getAssembler().getLOHContainer().addDirective(Kind, Args);
122 }
123 void emitCGProfileEntry(const MCSymbolRefExpr *From,
124 const MCSymbolRefExpr *To, uint64_t Count) override {
125 if (!From->getSymbol().isTemporary() && !To->getSymbol().isTemporary())
126 getAssembler().CGProfile.push_back(x: {.From: From, .To: To, .Count: Count});
127 }
128
129 void finishImpl() override;
130
131 void finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE);
132 void finalizeCGProfile();
133 void createAddrSigSection();
134};
135
136} // end anonymous namespace.
137
138static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
139 // These sections are created by the assembler itself after the end of
140 // the .s file.
141 StringRef SegName = MSec.getSegmentName();
142 StringRef SecName = MSec.getName();
143
144 if (SegName == "__LD" && SecName == "__compact_unwind")
145 return true;
146
147 if (SegName == "__IMPORT") {
148 if (SecName == "__jump_table")
149 return true;
150
151 if (SecName == "__pointers")
152 return true;
153 }
154
155 if (SegName == "__TEXT" && SecName == "__eh_frame")
156 return true;
157
158 if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
159 SecName == "__thread_ptr"))
160 return true;
161 if (SegName == "__LLVM" && SecName == "__cg_profile")
162 return true;
163 return false;
164}
165
166void MCMachOStreamer::changeSection(MCSection *Section,
167 const MCExpr *Subsection) {
168 // Change the section normally.
169 bool Created = changeSectionImpl(Section, Subsection);
170 const MCSectionMachO &MSec = *cast<MCSectionMachO>(Val: Section);
171 StringRef SegName = MSec.getSegmentName();
172 if (SegName == "__DWARF")
173 CreatedADWARFSection = true;
174 else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
175 assert(!CreatedADWARFSection && "Creating regular section after DWARF");
176
177 // Output a linker-local symbol so we don't need section-relative local
178 // relocations. The linker hates us when we do that.
179 if (LabelSections && !HasSectionLabel[Section] &&
180 !Section->getBeginSymbol()) {
181 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
182 Section->setBeginSymbol(Label);
183 HasSectionLabel[Section] = true;
184 }
185}
186
187void MCMachOStreamer::emitEHSymAttributes(const MCSymbol *Symbol,
188 MCSymbol *EHSymbol) {
189 getAssembler().registerSymbol(Symbol: *Symbol);
190 if (Symbol->isExternal())
191 emitSymbolAttribute(Symbol: EHSymbol, Attribute: MCSA_Global);
192 if (cast<MCSymbolMachO>(Val: Symbol)->isWeakDefinition())
193 emitSymbolAttribute(Symbol: EHSymbol, Attribute: MCSA_WeakDefinition);
194 if (Symbol->isPrivateExtern())
195 emitSymbolAttribute(Symbol: EHSymbol, Attribute: MCSA_PrivateExtern);
196}
197
198void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
199 // We have to create a new fragment if this is an atom defining symbol,
200 // fragments cannot span atoms.
201 if (getAssembler().isSymbolLinkerVisible(SD: *Symbol))
202 insert(F: new MCDataFragment());
203
204 MCObjectStreamer::emitLabel(Symbol, Loc);
205
206 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
207 // to clear the weak reference and weak definition bits too, but the
208 // implementation was buggy. For now we just try to match 'as', for
209 // diffability.
210 //
211 // FIXME: Cleanup this code, these bits should be emitted based on semantic
212 // properties, not on the order of definition, etc.
213 cast<MCSymbolMachO>(Val: Symbol)->clearReferenceType();
214}
215
216void MCMachOStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
217 MCValue Res;
218
219 if (Value->evaluateAsRelocatable(Res, Layout: nullptr, Fixup: nullptr)) {
220 if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
221 const MCSymbol &SymA = SymAExpr->getSymbol();
222 if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
223 cast<MCSymbolMachO>(Val: Symbol)->setAltEntry();
224 }
225 }
226 MCObjectStreamer::emitAssignment(Symbol, Value);
227}
228
229void MCMachOStreamer::emitDataRegion(DataRegionData::KindTy Kind) {
230 // Create a temporary label to mark the start of the data region.
231 MCSymbol *Start = getContext().createTempSymbol();
232 emitLabel(Symbol: Start);
233 // Record the region for the object writer to use.
234 DataRegionData Data = { .Kind: Kind, .Start: Start, .End: nullptr };
235 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
236 Regions.push_back(x: Data);
237}
238
239void MCMachOStreamer::emitDataRegionEnd() {
240 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
241 assert(!Regions.empty() && "Mismatched .end_data_region!");
242 DataRegionData &Data = Regions.back();
243 assert(!Data.End && "Mismatched .end_data_region!");
244 // Create a temporary label to mark the end of the data region.
245 Data.End = getContext().createTempSymbol();
246 emitLabel(Symbol: Data.End);
247}
248
249void MCMachOStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
250 // Let the target do whatever target specific stuff it needs to do.
251 getAssembler().getBackend().handleAssemblerFlag(Flag);
252 // Do any generic stuff we need to do.
253 switch (Flag) {
254 case MCAF_SyntaxUnified: return; // no-op here.
255 case MCAF_Code16: return; // Change parsing mode; no-op here.
256 case MCAF_Code32: return; // Change parsing mode; no-op here.
257 case MCAF_Code64: return; // Change parsing mode; no-op here.
258 case MCAF_SubsectionsViaSymbols:
259 getAssembler().setSubsectionsViaSymbols(true);
260 return;
261 }
262}
263
264void MCMachOStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
265 getAssembler().getLinkerOptions().push_back(x: Options);
266}
267
268void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
269 switch (Kind) {
270 case MCDR_DataRegion:
271 emitDataRegion(Kind: DataRegionData::Data);
272 return;
273 case MCDR_DataRegionJT8:
274 emitDataRegion(Kind: DataRegionData::JumpTable8);
275 return;
276 case MCDR_DataRegionJT16:
277 emitDataRegion(Kind: DataRegionData::JumpTable16);
278 return;
279 case MCDR_DataRegionJT32:
280 emitDataRegion(Kind: DataRegionData::JumpTable32);
281 return;
282 case MCDR_DataRegionEnd:
283 emitDataRegionEnd();
284 return;
285 }
286}
287
288void MCMachOStreamer::emitVersionMin(MCVersionMinType Kind, unsigned Major,
289 unsigned Minor, unsigned Update,
290 VersionTuple SDKVersion) {
291 getAssembler().setVersionMin(Type: Kind, Major, Minor, Update, SDKVersion);
292}
293
294void MCMachOStreamer::emitBuildVersion(unsigned Platform, unsigned Major,
295 unsigned Minor, unsigned Update,
296 VersionTuple SDKVersion) {
297 getAssembler().setBuildVersion(Platform: (MachO::PlatformType)Platform, Major, Minor,
298 Update, SDKVersion);
299}
300
301void MCMachOStreamer::emitDarwinTargetVariantBuildVersion(
302 unsigned Platform, unsigned Major, unsigned Minor, unsigned Update,
303 VersionTuple SDKVersion) {
304 getAssembler().setDarwinTargetVariantBuildVersion(
305 Platform: (MachO::PlatformType)Platform, Major, Minor, Update, SDKVersion);
306}
307
308void MCMachOStreamer::emitThumbFunc(MCSymbol *Symbol) {
309 // Remember that the function is a thumb function. Fixup and relocation
310 // values will need adjusted.
311 getAssembler().setIsThumbFunc(Symbol);
312 cast<MCSymbolMachO>(Val: Symbol)->setThumbFunc();
313}
314
315bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym,
316 MCSymbolAttr Attribute) {
317 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Val: Sym);
318
319 // Indirect symbols are handled differently, to match how 'as' handles
320 // them. This makes writing matching .o files easier.
321 if (Attribute == MCSA_IndirectSymbol) {
322 // Note that we intentionally cannot use the symbol data here; this is
323 // important for matching the string table that 'as' generates.
324 IndirectSymbolData ISD;
325 ISD.Symbol = Symbol;
326 ISD.Section = getCurrentSectionOnly();
327 getAssembler().getIndirectSymbols().push_back(x: ISD);
328 return true;
329 }
330
331 // Adding a symbol attribute always introduces the symbol, note that an
332 // important side effect of calling registerSymbol here is to register
333 // the symbol with the assembler.
334 getAssembler().registerSymbol(Symbol: *Symbol);
335
336 // The implementation of symbol attributes is designed to match 'as', but it
337 // leaves much to desired. It doesn't really make sense to arbitrarily add and
338 // remove flags, but 'as' allows this (in particular, see .desc).
339 //
340 // In the future it might be worth trying to make these operations more well
341 // defined.
342 switch (Attribute) {
343 case MCSA_Invalid:
344 case MCSA_ELF_TypeFunction:
345 case MCSA_ELF_TypeIndFunction:
346 case MCSA_ELF_TypeObject:
347 case MCSA_ELF_TypeTLS:
348 case MCSA_ELF_TypeCommon:
349 case MCSA_ELF_TypeNoType:
350 case MCSA_ELF_TypeGnuUniqueObject:
351 case MCSA_Extern:
352 case MCSA_Hidden:
353 case MCSA_IndirectSymbol:
354 case MCSA_Internal:
355 case MCSA_Protected:
356 case MCSA_Weak:
357 case MCSA_Local:
358 case MCSA_LGlobal:
359 case MCSA_Exported:
360 case MCSA_Memtag:
361 case MCSA_WeakAntiDep:
362 return false;
363
364 case MCSA_Global:
365 Symbol->setExternal(true);
366 // This effectively clears the undefined lazy bit, in Darwin 'as', although
367 // it isn't very consistent because it implements this as part of symbol
368 // lookup.
369 //
370 // FIXME: Cleanup this code, these bits should be emitted based on semantic
371 // properties, not on the order of definition, etc.
372 Symbol->setReferenceTypeUndefinedLazy(false);
373 break;
374
375 case MCSA_LazyReference:
376 // FIXME: This requires -dynamic.
377 Symbol->setNoDeadStrip();
378 if (Symbol->isUndefined())
379 Symbol->setReferenceTypeUndefinedLazy(true);
380 break;
381
382 // Since .reference sets the no dead strip bit, it is equivalent to
383 // .no_dead_strip in practice.
384 case MCSA_Reference:
385 case MCSA_NoDeadStrip:
386 Symbol->setNoDeadStrip();
387 break;
388
389 case MCSA_SymbolResolver:
390 Symbol->setSymbolResolver();
391 break;
392
393 case MCSA_AltEntry:
394 Symbol->setAltEntry();
395 break;
396
397 case MCSA_PrivateExtern:
398 Symbol->setExternal(true);
399 Symbol->setPrivateExtern(true);
400 break;
401
402 case MCSA_WeakReference:
403 // FIXME: This requires -dynamic.
404 if (Symbol->isUndefined())
405 Symbol->setWeakReference();
406 break;
407
408 case MCSA_WeakDefinition:
409 // FIXME: 'as' enforces that this is defined and global. The manual claims
410 // it has to be in a coalesced section, but this isn't enforced.
411 Symbol->setWeakDefinition();
412 break;
413
414 case MCSA_WeakDefAutoPrivate:
415 Symbol->setWeakDefinition();
416 Symbol->setWeakReference();
417 break;
418
419 case MCSA_Cold:
420 Symbol->setCold();
421 break;
422 }
423
424 return true;
425}
426
427void MCMachOStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
428 // Encode the 'desc' value into the lowest implementation defined bits.
429 getAssembler().registerSymbol(Symbol: *Symbol);
430 cast<MCSymbolMachO>(Val: Symbol)->setDesc(DescValue);
431}
432
433void MCMachOStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
434 Align ByteAlignment) {
435 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
436 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
437
438 getAssembler().registerSymbol(Symbol: *Symbol);
439 Symbol->setExternal(true);
440 Symbol->setCommon(Size, Alignment: ByteAlignment);
441}
442
443void MCMachOStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
444 Align ByteAlignment) {
445 // '.lcomm' is equivalent to '.zerofill'.
446 return emitZerofill(Section: getContext().getObjectFileInfo()->getDataBSSSection(),
447 Symbol, Size, ByteAlignment);
448}
449
450void MCMachOStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
451 uint64_t Size, Align ByteAlignment,
452 SMLoc Loc) {
453 // On darwin all virtual sections have zerofill type. Disallow the usage of
454 // .zerofill in non-virtual functions. If something similar is needed, use
455 // .space or .zero.
456 if (!Section->isVirtualSection()) {
457 getContext().reportError(
458 L: Loc, Msg: "The usage of .zerofill is restricted to sections of "
459 "ZEROFILL type. Use .zero or .space instead.");
460 return; // Early returning here shouldn't harm. EmitZeros should work on any
461 // section.
462 }
463
464 pushSection();
465 switchSection(Section);
466
467 // The symbol may not be present, which only creates the section.
468 if (Symbol) {
469 emitValueToAlignment(Alignment: ByteAlignment, Value: 0, ValueSize: 1, MaxBytesToEmit: 0);
470 emitLabel(Symbol);
471 emitZeros(NumBytes: Size);
472 }
473 popSection();
474}
475
476// This should always be called with the thread local bss section. Like the
477// .zerofill directive this doesn't actually switch sections on us.
478void MCMachOStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
479 uint64_t Size, Align ByteAlignment) {
480 emitZerofill(Section, Symbol, Size, ByteAlignment);
481}
482
483void MCMachOStreamer::emitInstToData(const MCInst &Inst,
484 const MCSubtargetInfo &STI) {
485 MCDataFragment *DF = getOrCreateDataFragment();
486
487 SmallVector<MCFixup, 4> Fixups;
488 SmallString<256> Code;
489 getAssembler().getEmitter().encodeInstruction(Inst, CB&: Code, Fixups, STI);
490
491 // Add the fixups and data.
492 for (MCFixup &Fixup : Fixups) {
493 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
494 DF->getFixups().push_back(Elt: Fixup);
495 }
496 DF->setHasInstructions(STI);
497 DF->getContents().append(in_start: Code.begin(), in_end: Code.end());
498}
499
500void MCMachOStreamer::finishImpl() {
501 emitFrames(MAB: &getAssembler().getBackend());
502
503 // We have to set the fragment atom associations so we can relax properly for
504 // Mach-O.
505
506 // First, scan the symbol table to build a lookup table from fragments to
507 // defining symbols.
508 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
509 for (const MCSymbol &Symbol : getAssembler().symbols()) {
510 if (getAssembler().isSymbolLinkerVisible(SD: Symbol) && Symbol.isInSection() &&
511 !Symbol.isVariable()) {
512 // An atom defining symbol should never be internal to a fragment.
513 assert(Symbol.getOffset() == 0 &&
514 "Invalid offset in atom defining symbol!");
515 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
516 }
517 }
518
519 // Set the fragment atom associations by tracking the last seen atom defining
520 // symbol.
521 for (MCSection &Sec : getAssembler()) {
522 const MCSymbol *CurrentAtom = nullptr;
523 for (MCFragment &Frag : Sec) {
524 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(Val: &Frag))
525 CurrentAtom = Symbol;
526 Frag.setAtom(CurrentAtom);
527 }
528 }
529
530 finalizeCGProfile();
531
532 createAddrSigSection();
533 this->MCObjectStreamer::finishImpl();
534}
535
536void MCMachOStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
537 const MCSymbol *S = &SRE->getSymbol();
538 if (getAssembler().registerSymbol(Symbol: *S))
539 S->setExternal(true);
540}
541
542void MCMachOStreamer::finalizeCGProfile() {
543 MCAssembler &Asm = getAssembler();
544 if (Asm.CGProfile.empty())
545 return;
546 for (MCAssembler::CGProfileEntry &E : Asm.CGProfile) {
547 finalizeCGProfileEntry(SRE&: E.From);
548 finalizeCGProfileEntry(SRE&: E.To);
549 }
550 // We can't write the section out until symbol indices are finalized which
551 // doesn't happen until after section layout. We need to create the section
552 // and set its size now so that it's accounted for in layout.
553 MCSection *CGProfileSection = Asm.getContext().getMachOSection(
554 Segment: "__LLVM", Section: "__cg_profile", TypeAndAttributes: 0, K: SectionKind::getMetadata());
555 Asm.registerSection(Section&: *CGProfileSection);
556 auto *Frag = new MCDataFragment(CGProfileSection);
557 // For each entry, reserve space for 2 32-bit indices and a 64-bit count.
558 size_t SectionBytes =
559 Asm.CGProfile.size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
560 Frag->getContents().resize(N: SectionBytes);
561}
562
563MCStreamer *llvm::createMachOStreamer(MCContext &Context,
564 std::unique_ptr<MCAsmBackend> &&MAB,
565 std::unique_ptr<MCObjectWriter> &&OW,
566 std::unique_ptr<MCCodeEmitter> &&CE,
567 bool RelaxAll, bool DWARFMustBeAtTheEnd,
568 bool LabelSections) {
569 MCMachOStreamer *S =
570 new MCMachOStreamer(Context, std::move(MAB), std::move(OW), std::move(CE),
571 DWARFMustBeAtTheEnd, LabelSections);
572 const Triple &Target = Context.getTargetTriple();
573 S->emitVersionForTarget(
574 Target, SDKVersion: Context.getObjectFileInfo()->getSDKVersion(),
575 DarwinTargetVariantTriple: Context.getObjectFileInfo()->getDarwinTargetVariantTriple(),
576 DarwinTargetVariantSDKVersion: Context.getObjectFileInfo()->getDarwinTargetVariantSDKVersion());
577 if (RelaxAll)
578 S->getAssembler().setRelaxAll(true);
579 return S;
580}
581
582// The AddrSig section uses a series of relocations to refer to the symbols that
583// should be considered address-significant. The only interesting content of
584// these relocations is their symbol; the type, length etc will be ignored by
585// the linker. The reason we are not referring to the symbol indices directly is
586// that those indices will be invalidated by tools that update the symbol table.
587// Symbol relocations OTOH will have their indices updated by e.g. llvm-strip.
588void MCMachOStreamer::createAddrSigSection() {
589 MCAssembler &Asm = getAssembler();
590 MCObjectWriter &writer = Asm.getWriter();
591 if (!writer.getEmitAddrsigSection())
592 return;
593 // Create the AddrSig section and first data fragment here as its layout needs
594 // to be computed immediately after in order for it to be exported correctly.
595 MCSection *AddrSigSection =
596 Asm.getContext().getObjectFileInfo()->getAddrSigSection();
597 Asm.registerSection(Section&: *AddrSigSection);
598 auto *Frag = new MCDataFragment(AddrSigSection);
599 // We will generate a series of pointer-sized symbol relocations at offset
600 // 0x0. Set the section size to be large enough to contain a single pointer
601 // (instead of emitting a zero-sized section) so these relocations are
602 // technically valid, even though we don't expect these relocations to
603 // actually be applied by the linker.
604 Frag->getContents().resize(N: 8);
605}
606

source code of llvm/lib/MC/MCMachOStreamer.cpp