1 //===- MCMachOStreamer.cpp - MachO Streamer -------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
40 class MCSubtargetInfo
;
48 class MCMachOStreamer
: public MCObjectStreamer
{
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.
55 bool DWARFMustBeAtTheEnd
;
56 bool CreatedADWARFSection
;
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
;
63 void emitInstToData(const MCInst
&Inst
, const MCSubtargetInfo
&STI
) override
;
65 void emitDataRegion(DataRegionData::KindTy Kind
);
66 void emitDataRegionEnd();
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
),
75 LabelSections(label
), DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd
),
76 CreatedADWARFSection(false) {}
79 void reset() override
{
80 CreatedADWARFSection
= false;
81 HasSectionLabel
.clear();
82 MCObjectStreamer::reset();
85 /// @name MCStreamer Interface
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
;
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
;
116 void emitIdent(StringRef IdentString
) override
{
117 llvm_unreachable("macho doesn't support this directive");
120 void emitLOHDirective(MCLOHType Kind
, const MCLOHArgs
&Args
) override
{
121 getAssembler().getLOHContainer().addDirective(Kind
, Args
);
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({From
, To
, Count
});
129 void finishImpl() override
;
131 void finalizeCGProfileEntry(const MCSymbolRefExpr
*&SRE
);
132 void finalizeCGProfile();
133 void createAddrSigSection();
136 } // end anonymous namespace.
138 static bool canGoAfterDWARF(const MCSectionMachO
&MSec
) {
139 // These sections are created by the assembler itself after the end of
141 StringRef SegName
= MSec
.getSegmentName();
142 StringRef SecName
= MSec
.getName();
144 if (SegName
== "__LD" && SecName
== "__compact_unwind")
147 if (SegName
== "__IMPORT") {
148 if (SecName
== "__jump_table")
151 if (SecName
== "__pointers")
155 if (SegName
== "__TEXT" && SecName
== "__eh_frame")
158 if (SegName
== "__DATA" && (SecName
== "__nl_symbol_ptr" ||
159 SecName
== "__thread_ptr"))
161 if (SegName
== "__LLVM" && SecName
== "__cg_profile")
166 void 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
>(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");
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;
187 void MCMachOStreamer::emitEHSymAttributes(const MCSymbol
*Symbol
,
188 MCSymbol
*EHSymbol
) {
189 getAssembler().registerSymbol(*Symbol
);
190 if (Symbol
->isExternal())
191 emitSymbolAttribute(EHSymbol
, MCSA_Global
);
192 if (cast
<MCSymbolMachO
>(Symbol
)->isWeakDefinition())
193 emitSymbolAttribute(EHSymbol
, MCSA_WeakDefinition
);
194 if (Symbol
->isPrivateExtern())
195 emitSymbolAttribute(EHSymbol
, MCSA_PrivateExtern
);
198 void 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(*Symbol
))
202 insert(new MCDataFragment());
204 MCObjectStreamer::emitLabel(Symbol
, Loc
);
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
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
>(Symbol
)->clearReferenceType();
216 void MCMachOStreamer::emitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
219 if (Value
->evaluateAsRelocatable(Res
, nullptr, 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
>(Symbol
)->setAltEntry();
226 MCObjectStreamer::emitAssignment(Symbol
, Value
);
229 void MCMachOStreamer::emitDataRegion(DataRegionData::KindTy Kind
) {
230 // Create a temporary label to mark the start of the data region.
231 MCSymbol
*Start
= getContext().createTempSymbol();
233 // Record the region for the object writer to use.
234 DataRegionData Data
= { Kind
, Start
, nullptr };
235 std::vector
<DataRegionData
> &Regions
= getAssembler().getDataRegions();
236 Regions
.push_back(Data
);
239 void 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();
249 void 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.
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);
264 void MCMachOStreamer::emitLinkerOptions(ArrayRef
<std::string
> Options
) {
265 getAssembler().getLinkerOptions().push_back(Options
);
268 void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind
) {
270 case MCDR_DataRegion
:
271 emitDataRegion(DataRegionData::Data
);
273 case MCDR_DataRegionJT8
:
274 emitDataRegion(DataRegionData::JumpTable8
);
276 case MCDR_DataRegionJT16
:
277 emitDataRegion(DataRegionData::JumpTable16
);
279 case MCDR_DataRegionJT32
:
280 emitDataRegion(DataRegionData::JumpTable32
);
282 case MCDR_DataRegionEnd
:
288 void MCMachOStreamer::emitVersionMin(MCVersionMinType Kind
, unsigned Major
,
289 unsigned Minor
, unsigned Update
,
290 VersionTuple SDKVersion
) {
291 getAssembler().setVersionMin(Kind
, Major
, Minor
, Update
, SDKVersion
);
294 void MCMachOStreamer::emitBuildVersion(unsigned Platform
, unsigned Major
,
295 unsigned Minor
, unsigned Update
,
296 VersionTuple SDKVersion
) {
297 getAssembler().setBuildVersion((MachO::PlatformType
)Platform
, Major
, Minor
,
301 void MCMachOStreamer::emitDarwinTargetVariantBuildVersion(
302 unsigned Platform
, unsigned Major
, unsigned Minor
, unsigned Update
,
303 VersionTuple SDKVersion
) {
304 getAssembler().setDarwinTargetVariantBuildVersion(
305 (MachO::PlatformType
)Platform
, Major
, Minor
, Update
, SDKVersion
);
308 void 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
>(Symbol
)->setThumbFunc();
315 bool MCMachOStreamer::emitSymbolAttribute(MCSymbol
*Sym
,
316 MCSymbolAttr Attribute
) {
317 MCSymbolMachO
*Symbol
= cast
<MCSymbolMachO
>(Sym
);
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
;
326 ISD
.Section
= getCurrentSectionOnly();
327 getAssembler().getIndirectSymbols().push_back(ISD
);
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
);
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).
340 // In the future it might be worth trying to make these operations more well
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
:
353 case MCSA_IndirectSymbol
:
364 Symbol
->setExternal(true);
365 // This effectively clears the undefined lazy bit, in Darwin 'as', although
366 // it isn't very consistent because it implements this as part of symbol
369 // FIXME: Cleanup this code, these bits should be emitted based on semantic
370 // properties, not on the order of definition, etc.
371 Symbol
->setReferenceTypeUndefinedLazy(false);
374 case MCSA_LazyReference
:
375 // FIXME: This requires -dynamic.
376 Symbol
->setNoDeadStrip();
377 if (Symbol
->isUndefined())
378 Symbol
->setReferenceTypeUndefinedLazy(true);
381 // Since .reference sets the no dead strip bit, it is equivalent to
382 // .no_dead_strip in practice.
384 case MCSA_NoDeadStrip
:
385 Symbol
->setNoDeadStrip();
388 case MCSA_SymbolResolver
:
389 Symbol
->setSymbolResolver();
393 Symbol
->setAltEntry();
396 case MCSA_PrivateExtern
:
397 Symbol
->setExternal(true);
398 Symbol
->setPrivateExtern(true);
401 case MCSA_WeakReference
:
402 // FIXME: This requires -dynamic.
403 if (Symbol
->isUndefined())
404 Symbol
->setWeakReference();
407 case MCSA_WeakDefinition
:
408 // FIXME: 'as' enforces that this is defined and global. The manual claims
409 // it has to be in a coalesced section, but this isn't enforced.
410 Symbol
->setWeakDefinition();
413 case MCSA_WeakDefAutoPrivate
:
414 Symbol
->setWeakDefinition();
415 Symbol
->setWeakReference();
426 void MCMachOStreamer::emitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
427 // Encode the 'desc' value into the lowest implementation defined bits.
428 getAssembler().registerSymbol(*Symbol
);
429 cast
<MCSymbolMachO
>(Symbol
)->setDesc(DescValue
);
432 void MCMachOStreamer::emitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
433 Align ByteAlignment
) {
434 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
435 assert(Symbol
->isUndefined() && "Cannot define a symbol twice!");
437 getAssembler().registerSymbol(*Symbol
);
438 Symbol
->setExternal(true);
439 Symbol
->setCommon(Size
, ByteAlignment
);
442 void MCMachOStreamer::emitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
443 Align ByteAlignment
) {
444 // '.lcomm' is equivalent to '.zerofill'.
445 return emitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
446 Symbol
, Size
, ByteAlignment
);
449 void MCMachOStreamer::emitZerofill(MCSection
*Section
, MCSymbol
*Symbol
,
450 uint64_t Size
, Align ByteAlignment
,
452 // On darwin all virtual sections have zerofill type. Disallow the usage of
453 // .zerofill in non-virtual functions. If something similar is needed, use
455 if (!Section
->isVirtualSection()) {
456 getContext().reportError(
457 Loc
, "The usage of .zerofill is restricted to sections of "
458 "ZEROFILL type. Use .zero or .space instead.");
459 return; // Early returning here shouldn't harm. EmitZeros should work on any
464 switchSection(Section
);
466 // The symbol may not be present, which only creates the section.
468 emitValueToAlignment(ByteAlignment
, 0, 1, 0);
475 // This should always be called with the thread local bss section. Like the
476 // .zerofill directive this doesn't actually switch sections on us.
477 void MCMachOStreamer::emitTBSSSymbol(MCSection
*Section
, MCSymbol
*Symbol
,
478 uint64_t Size
, Align ByteAlignment
) {
479 emitZerofill(Section
, Symbol
, Size
, ByteAlignment
);
482 void MCMachOStreamer::emitInstToData(const MCInst
&Inst
,
483 const MCSubtargetInfo
&STI
) {
484 MCDataFragment
*DF
= getOrCreateDataFragment();
486 SmallVector
<MCFixup
, 4> Fixups
;
487 SmallString
<256> Code
;
488 getAssembler().getEmitter().encodeInstruction(Inst
, Code
, Fixups
, STI
);
490 // Add the fixups and data.
491 for (MCFixup
&Fixup
: Fixups
) {
492 Fixup
.setOffset(Fixup
.getOffset() + DF
->getContents().size());
493 DF
->getFixups().push_back(Fixup
);
495 DF
->setHasInstructions(STI
);
496 DF
->getContents().append(Code
.begin(), Code
.end());
499 void MCMachOStreamer::finishImpl() {
500 emitFrames(&getAssembler().getBackend());
502 // We have to set the fragment atom associations so we can relax properly for
505 // First, scan the symbol table to build a lookup table from fragments to
507 DenseMap
<const MCFragment
*, const MCSymbol
*> DefiningSymbolMap
;
508 for (const MCSymbol
&Symbol
: getAssembler().symbols()) {
509 if (getAssembler().isSymbolLinkerVisible(Symbol
) && Symbol
.isInSection() &&
510 !Symbol
.isVariable()) {
511 // An atom defining symbol should never be internal to a fragment.
512 assert(Symbol
.getOffset() == 0 &&
513 "Invalid offset in atom defining symbol!");
514 DefiningSymbolMap
[Symbol
.getFragment()] = &Symbol
;
518 // Set the fragment atom associations by tracking the last seen atom defining
520 for (MCSection
&Sec
: getAssembler()) {
521 const MCSymbol
*CurrentAtom
= nullptr;
522 for (MCFragment
&Frag
: Sec
) {
523 if (const MCSymbol
*Symbol
= DefiningSymbolMap
.lookup(&Frag
))
524 CurrentAtom
= Symbol
;
525 Frag
.setAtom(CurrentAtom
);
531 createAddrSigSection();
532 this->MCObjectStreamer::finishImpl();
535 void MCMachOStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr
*&SRE
) {
536 const MCSymbol
*S
= &SRE
->getSymbol();
537 if (getAssembler().registerSymbol(*S
))
538 S
->setExternal(true);
541 void MCMachOStreamer::finalizeCGProfile() {
542 MCAssembler
&Asm
= getAssembler();
543 if (Asm
.CGProfile
.empty())
545 for (MCAssembler::CGProfileEntry
&E
: Asm
.CGProfile
) {
546 finalizeCGProfileEntry(E
.From
);
547 finalizeCGProfileEntry(E
.To
);
549 // We can't write the section out until symbol indices are finalized which
550 // doesn't happen until after section layout. We need to create the section
551 // and set its size now so that it's accounted for in layout.
552 MCSection
*CGProfileSection
= Asm
.getContext().getMachOSection(
553 "__LLVM", "__cg_profile", 0, SectionKind::getMetadata());
554 Asm
.registerSection(*CGProfileSection
);
555 auto *Frag
= new MCDataFragment(CGProfileSection
);
556 // For each entry, reserve space for 2 32-bit indices and a 64-bit count.
557 size_t SectionBytes
=
558 Asm
.CGProfile
.size() * (2 * sizeof(uint32_t) + sizeof(uint64_t));
559 Frag
->getContents().resize(SectionBytes
);
562 MCStreamer
*llvm::createMachOStreamer(MCContext
&Context
,
563 std::unique_ptr
<MCAsmBackend
> &&MAB
,
564 std::unique_ptr
<MCObjectWriter
> &&OW
,
565 std::unique_ptr
<MCCodeEmitter
> &&CE
,
566 bool RelaxAll
, bool DWARFMustBeAtTheEnd
,
567 bool LabelSections
) {
569 new MCMachOStreamer(Context
, std::move(MAB
), std::move(OW
), std::move(CE
),
570 DWARFMustBeAtTheEnd
, LabelSections
);
571 const Triple
&Target
= Context
.getTargetTriple();
572 S
->emitVersionForTarget(
573 Target
, Context
.getObjectFileInfo()->getSDKVersion(),
574 Context
.getObjectFileInfo()->getDarwinTargetVariantTriple(),
575 Context
.getObjectFileInfo()->getDarwinTargetVariantSDKVersion());
577 S
->getAssembler().setRelaxAll(true);
581 // The AddrSig section uses a series of relocations to refer to the symbols that
582 // should be considered address-significant. The only interesting content of
583 // these relocations is their symbol; the type, length etc will be ignored by
584 // the linker. The reason we are not referring to the symbol indices directly is
585 // that those indices will be invalidated by tools that update the symbol table.
586 // Symbol relocations OTOH will have their indices updated by e.g. llvm-strip.
587 void MCMachOStreamer::createAddrSigSection() {
588 MCAssembler
&Asm
= getAssembler();
589 MCObjectWriter
&writer
= Asm
.getWriter();
590 if (!writer
.getEmitAddrsigSection())
592 // Create the AddrSig section and first data fragment here as its layout needs
593 // to be computed immediately after in order for it to be exported correctly.
594 MCSection
*AddrSigSection
=
595 Asm
.getContext().getObjectFileInfo()->getAddrSigSection();
596 Asm
.registerSection(*AddrSigSection
);
597 auto *Frag
= new MCDataFragment(AddrSigSection
);
598 // We will generate a series of pointer-sized symbol relocations at offset
599 // 0x0. Set the section size to be large enough to contain a single pointer
600 // (instead of emitting a zero-sized section) so these relocations are
601 // technically valid, even though we don't expect these relocations to
602 // actually be applied by the linker.
603 Frag
->getContents().resize(8);