LLVM 19.0.0git
MCELFStreamer.cpp
Go to the documentation of this file.
1//===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
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// This file assembles .s files and emits ELF .o object files.
10//
11//===----------------------------------------------------------------------===//
12
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCAssembler.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCFixup.h"
24#include "llvm/MC/MCFragment.h"
27#include "llvm/MC/MCSection.h"
29#include "llvm/MC/MCStreamer.h"
30#include "llvm/MC/MCSymbol.h"
31#include "llvm/MC/MCSymbolELF.h"
35#include "llvm/Support/LEB128.h"
37#include <cassert>
38#include <cstdint>
39
40using namespace llvm;
41
43 std::unique_ptr<MCAsmBackend> TAB,
44 std::unique_ptr<MCObjectWriter> OW,
45 std::unique_ptr<MCCodeEmitter> Emitter)
46 : MCObjectStreamer(Context, std::move(TAB), std::move(OW),
47 std::move(Emitter)) {}
48
49bool MCELFStreamer::isBundleLocked() const {
51}
52
53void MCELFStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) {
54 MCContext &Ctx = getContext();
57 &STI);
58
59 if (NoExecStack)
61}
62
64 auto *Symbol = cast<MCSymbolELF>(S);
65 MCObjectStreamer::emitLabel(Symbol, Loc);
66
67 const MCSectionELF &Section =
68 static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
69 if (Section.getFlags() & ELF::SHF_TLS)
70 Symbol->setType(ELF::STT_TLS);
71}
72
75 auto *Symbol = cast<MCSymbolELF>(S);
77
78 const MCSectionELF &Section =
79 static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
80 if (Section.getFlags() & ELF::SHF_TLS)
81 Symbol->setType(ELF::STT_TLS);
82}
83
85 // Let the target do whatever target specific stuff it needs to do.
87 // Do any generic stuff we need to do.
88 switch (Flag) {
89 case MCAF_SyntaxUnified: return; // no-op here.
90 case MCAF_Code16: return; // Change parsing mode; no-op here.
91 case MCAF_Code32: return; // Change parsing mode; no-op here.
92 case MCAF_Code64: return; // Change parsing mode; no-op here.
95 return;
96 }
97
98 llvm_unreachable("invalid assembler flag!");
99}
100
101// If bundle alignment is used and there are any instructions in the section, it
102// needs to be aligned to at least the bundle size.
103static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
104 MCSection *Section) {
105 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions())
106 Section->ensureMinAlignment(Align(Assembler.getBundleAlignSize()));
107}
108
110 const MCExpr *Subsection) {
111 MCSection *CurSection = getCurrentSectionOnly();
112 if (CurSection && isBundleLocked())
113 report_fatal_error("Unterminated .bundle_lock when changing a section");
114
115 MCAssembler &Asm = getAssembler();
116 // Ensure the previous section gets aligned if necessary.
117 setSectionAlignmentForBundling(Asm, CurSection);
118 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
119 const MCSymbol *Grp = SectionELF->getGroup();
120 if (Grp)
121 Asm.registerSymbol(*Grp);
122 if (SectionELF->getFlags() & ELF::SHF_GNU_RETAIN)
123 Asm.getWriter().markGnuAbi();
124
125 changeSectionImpl(Section, Subsection);
126 Asm.registerSymbol(*Section->getBeginSymbol());
127}
128
130 getAssembler().registerSymbol(*Symbol);
133 Alias->setVariableValue(Value);
134}
135
136// When GNU as encounters more than one .type declaration for an object it seems
137// to use a mechanism similar to the one below to decide which type is actually
138// used in the object file. The greater of T1 and T2 is selected based on the
139// following ordering:
140// STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
141// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
142// provided type).
143static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
146 if (T1 == Type)
147 return T2;
148 if (T2 == Type)
149 return T1;
150 }
151
152 return T2;
153}
154
156 auto *Symbol = cast<MCSymbolELF>(S);
157
158 // Adding a symbol attribute always introduces the symbol, note that an
159 // important side effect of calling registerSymbol here is to register
160 // the symbol with the assembler.
161 getAssembler().registerSymbol(*Symbol);
162
163 // The implementation of symbol attributes is designed to match 'as', but it
164 // leaves much to desired. It doesn't really make sense to arbitrarily add and
165 // remove flags, but 'as' allows this (in particular, see .desc).
166 //
167 // In the future it might be worth trying to make these operations more well
168 // defined.
169 switch (Attribute) {
170 case MCSA_Cold:
171 case MCSA_Extern:
173 case MCSA_Reference:
178 case MCSA_Invalid:
180 case MCSA_Exported:
181 case MCSA_WeakAntiDep:
182 return false;
183
184 case MCSA_NoDeadStrip:
185 // Ignore for now.
186 break;
187
189 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
190 Symbol->setBinding(ELF::STB_GNU_UNIQUE);
192 break;
193
194 case MCSA_Global:
195 // For `.weak x; .global x`, GNU as sets the binding to STB_WEAK while we
196 // traditionally set the binding to STB_GLOBAL. This is error-prone, so we
197 // error on such cases. Note, we also disallow changed binding from .local.
198 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_GLOBAL)
200 Symbol->getName() +
201 " changed binding to STB_GLOBAL");
202 Symbol->setBinding(ELF::STB_GLOBAL);
203 break;
204
206 case MCSA_Weak:
207 // For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK.
208 // We emit a warning for now but may switch to an error in the future.
209 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK)
211 getStartTokLoc(), Symbol->getName() + " changed binding to STB_WEAK");
212 Symbol->setBinding(ELF::STB_WEAK);
213 break;
214
215 case MCSA_Local:
216 if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_LOCAL)
218 Symbol->getName() +
219 " changed binding to STB_LOCAL");
220 Symbol->setBinding(ELF::STB_LOCAL);
221 break;
222
224 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
225 break;
226
228 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
230 break;
231
233 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
234 break;
235
236 case MCSA_ELF_TypeTLS:
237 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
238 break;
239
241 // TODO: Emit these as a common symbol.
242 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
243 break;
244
246 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
247 break;
248
249 case MCSA_Protected:
250 Symbol->setVisibility(ELF::STV_PROTECTED);
251 break;
252
253 case MCSA_Memtag:
254 Symbol->setMemtag(true);
255 break;
256
257 case MCSA_Hidden:
258 Symbol->setVisibility(ELF::STV_HIDDEN);
259 break;
260
261 case MCSA_Internal:
262 Symbol->setVisibility(ELF::STV_INTERNAL);
263 break;
264
265 case MCSA_AltEntry:
266 llvm_unreachable("ELF doesn't support the .alt_entry attribute");
267
268 case MCSA_LGlobal:
269 llvm_unreachable("ELF doesn't support the .lglobl attribute");
270 }
271
272 return true;
273}
274
276 Align ByteAlignment) {
277 auto *Symbol = cast<MCSymbolELF>(S);
278 getAssembler().registerSymbol(*Symbol);
279
280 if (!Symbol->isBindingSet())
281 Symbol->setBinding(ELF::STB_GLOBAL);
282
283 Symbol->setType(ELF::STT_OBJECT);
284
285 if (Symbol->getBinding() == ELF::STB_LOCAL) {
289 switchSection(&Section);
290
291 emitValueToAlignment(ByteAlignment, 0, 1, 0);
292 emitLabel(Symbol);
294
295 switchSection(P.first, P.second);
296 } else {
297 if (Symbol->declareCommon(Size, ByteAlignment))
298 report_fatal_error(Twine("Symbol: ") + Symbol->getName() +
299 " redeclared as different type");
300 }
301
302 cast<MCSymbolELF>(Symbol)
304}
305
307 cast<MCSymbolELF>(Symbol)->setSize(Value);
308}
309
312 bool KeepOriginalSym) {
314 getStartTokLoc(), OriginalSym, Name, KeepOriginalSym});
315}
316
318 Align ByteAlignment) {
319 auto *Symbol = cast<MCSymbolELF>(S);
320 // FIXME: Should this be caught and done earlier?
321 getAssembler().registerSymbol(*Symbol);
322 Symbol->setBinding(ELF::STB_LOCAL);
323 emitCommonSymbol(Symbol, Size, ByteAlignment);
324}
325
327 SMLoc Loc) {
328 if (isBundleLocked())
329 report_fatal_error("Emitting values inside a locked bundle is forbidden");
330 fixSymbolsInTLSFixups(Value);
332}
333
335 unsigned ValueSize,
336 unsigned MaxBytesToEmit) {
337 if (isBundleLocked())
338 report_fatal_error("Emitting values inside a locked bundle is forbidden");
339 MCObjectStreamer::emitValueToAlignment(Alignment, Value, ValueSize,
340 MaxBytesToEmit);
341}
342
344 const MCSymbolRefExpr *To,
345 uint64_t Count) {
346 getAssembler().CGProfile.push_back({From, To, Count});
347}
348
352 pushSection();
353 switchSection(Comment);
354 if (!SeenIdent) {
355 emitInt8(0);
356 SeenIdent = true;
357 }
358 emitBytes(IdentString);
359 emitInt8(0);
360 popSection();
361}
362
363void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
364 switch (expr->getKind()) {
365 case MCExpr::Target:
366 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
367 break;
368 case MCExpr::Constant:
369 break;
370
371 case MCExpr::Binary: {
372 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
373 fixSymbolsInTLSFixups(be->getLHS());
374 fixSymbolsInTLSFixups(be->getRHS());
375 break;
376 }
377
378 case MCExpr::SymbolRef: {
379 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
380 switch (symRef.getKind()) {
381 default:
382 return;
437 break;
438 }
440 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
441 break;
442 }
443
444 case MCExpr::Unary:
445 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
446 break;
447 }
448}
449
450void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE,
452 const MCSymbol *S = &SRE->getSymbol();
453 if (S->isTemporary()) {
454 if (!S->isInSection()) {
456 SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
457 "`" + S->getName() + "`");
458 return;
459 }
460 S = S->getSection().getBeginSymbol();
461 S->setUsedInReloc();
463 SRE->getLoc());
464 }
466 if (std::optional<std::pair<bool, std::string>> Err =
468 *MCOffset, "BFD_RELOC_NONE", SRE, SRE->getLoc(),
469 *getContext().getSubtargetInfo()))
470 report_fatal_error("Relocation for CG Profile could not be created: " +
471 Twine(Err->second));
472}
473
474void MCELFStreamer::finalizeCGProfile() {
476 if (Asm.CGProfile.empty())
477 return;
479 ".llvm.call-graph-profile", ELF::SHT_LLVM_CALL_GRAPH_PROFILE,
480 ELF::SHF_EXCLUDE, /*sizeof(Elf_CGProfile_Impl<>)=*/8);
481 pushSection();
482 switchSection(CGProfile);
483 uint64_t Offset = 0;
484 for (MCAssembler::CGProfileEntry &E : Asm.CGProfile) {
485 finalizeCGProfileEntry(E.From, Offset);
486 finalizeCGProfileEntry(E.To, Offset);
487 emitIntValue(E.Count, sizeof(uint64_t));
488 Offset += sizeof(uint64_t);
489 }
490 popSection();
491}
492
493void MCELFStreamer::emitInstToFragment(const MCInst &Inst,
494 const MCSubtargetInfo &STI) {
496 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
497
498 for (auto &Fixup : F.getFixups())
499 fixSymbolsInTLSFixups(Fixup.getValue());
500}
501
502// A fragment can only have one Subtarget, and when bundling is enabled we
503// sometimes need to use the same fragment. We give an error if there
504// are conflicting Subtargets.
505static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
506 const MCSubtargetInfo *NewSTI) {
507 if (OldSTI && NewSTI && OldSTI != NewSTI)
508 report_fatal_error("A Bundle can only have one Subtarget.");
509}
510
511void MCELFStreamer::emitInstToData(const MCInst &Inst,
512 const MCSubtargetInfo &STI) {
513 MCAssembler &Assembler = getAssembler();
516 Assembler.getEmitter().encodeInstruction(Inst, Code, Fixups, STI);
517
518 for (auto &Fixup : Fixups)
519 fixSymbolsInTLSFixups(Fixup.getValue());
520
521 // There are several possibilities here:
522 //
523 // If bundling is disabled, append the encoded instruction to the current data
524 // fragment (or create a new such fragment if the current fragment is not a
525 // data fragment, or the Subtarget has changed).
526 //
527 // If bundling is enabled:
528 // - If we're not in a bundle-locked group, emit the instruction into a
529 // fragment of its own. If there are no fixups registered for the
530 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
531 // MCDataFragment.
532 // - If we're in a bundle-locked group, append the instruction to the current
533 // data fragment because we want all the instructions in a group to get into
534 // the same fragment. Be careful not to do that for the first instruction in
535 // the group, though.
537
538 if (Assembler.isBundlingEnabled()) {
540 if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
541 // If we are bundle-locked, we re-use the current fragment.
542 // The bundle-locking directive ensures this is a new data fragment.
543 DF = cast<MCDataFragment>(getCurrentFragment());
544 CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
545 } else if (!isBundleLocked() && Fixups.size() == 0) {
546 // Optimize memory usage by emitting the instruction to a
547 // MCCompactEncodedInstFragment when not in a bundle-locked group and
548 // there are no fixups registered.
551 insert(CEIF);
552 CEIF->getContents().append(Code.begin(), Code.end());
553 CEIF->setHasInstructions(STI);
554 return;
555 } else {
557 insert(DF);
558 }
560 // If this fragment is for a group marked "align_to_end", set a flag
561 // in the fragment. This can happen after the fragment has already been
562 // created if there are nested bundle_align groups and an inner one
563 // is the one marked align_to_end.
564 DF->setAlignToBundleEnd(true);
565 }
566
567 // We're now emitting an instruction in a bundle group, so this flag has
568 // to be turned off.
570 } else {
572 }
573
574 // Add the fixups and data.
575 for (auto &Fixup : Fixups) {
576 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
577 DF->getFixups().push_back(Fixup);
578 }
579
580 DF->setHasInstructions(STI);
581 if (!Fixups.empty() && Fixups.back().getTargetKind() ==
582 getAssembler().getBackend().RelaxFixupKind)
583 DF->setLinkerRelaxable();
584 DF->getContents().append(Code.begin(), Code.end());
585}
586
588 assert(Log2(Alignment) <= 30 && "Invalid bundle alignment");
589 MCAssembler &Assembler = getAssembler();
590 if (Alignment > 1 && (Assembler.getBundleAlignSize() == 0 ||
591 Assembler.getBundleAlignSize() == Alignment.value()))
592 Assembler.setBundleAlignSize(Alignment.value());
593 else
594 report_fatal_error(".bundle_align_mode cannot be changed once set");
595}
596
597void MCELFStreamer::emitBundleLock(bool AlignToEnd) {
599
600 if (!getAssembler().isBundlingEnabled())
601 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
602
603 if (!isBundleLocked())
605
608}
609
612
613 if (!getAssembler().isBundlingEnabled())
614 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
615 else if (!isBundleLocked())
616 report_fatal_error(".bundle_unlock without matching lock");
617 else if (Sec.isBundleGroupBeforeFirstInst())
618 report_fatal_error("Empty bundle-locked group is forbidden");
619
621}
622
624 // Emit the .gnu attributes section if any attributes have been added.
625 if (!GNUAttributes.empty()) {
626 MCSection *DummyAttributeSection = nullptr;
627 createAttributesSection("gnu", ".gnu.attributes", ELF::SHT_GNU_ATTRIBUTES,
628 DummyAttributeSection, GNUAttributes);
629 }
630
631 // Ensure the last section gets aligned if necessary.
632 MCSection *CurSection = getCurrentSectionOnly();
634
635 finalizeCGProfile();
636 emitFrames(nullptr);
637
639}
640
642 llvm_unreachable("Generic ELF doesn't support this directive");
643}
644
645void MCELFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
646 llvm_unreachable("ELF doesn't support this directive");
647}
648
650 uint64_t Size, Align ByteAlignment,
651 SMLoc Loc) {
652 llvm_unreachable("ELF doesn't support this directive");
653}
654
656 uint64_t Size, Align ByteAlignment) {
657 llvm_unreachable("ELF doesn't support this directive");
658}
659
661 bool OverwriteExisting) {
662 // Look for existing attribute item
663 if (AttributeItem *Item = getAttributeItem(Attribute)) {
664 if (!OverwriteExisting)
665 return;
667 Item->IntValue = Value;
668 return;
669 }
670
671 // Create new attribute item
673 std::string(StringRef(""))};
674 Contents.push_back(Item);
675}
676
678 bool OverwriteExisting) {
679 // Look for existing attribute item
680 if (AttributeItem *Item = getAttributeItem(Attribute)) {
681 if (!OverwriteExisting)
682 return;
683 Item->Type = AttributeItem::TextAttribute;
684 Item->StringValue = std::string(Value);
685 return;
686 }
687
688 // Create new attribute item
690 std::string(Value)};
691 Contents.push_back(Item);
692}
693
694void MCELFStreamer::setAttributeItems(unsigned Attribute, unsigned IntValue,
695 StringRef StringValue,
696 bool OverwriteExisting) {
697 // Look for existing attribute item
698 if (AttributeItem *Item = getAttributeItem(Attribute)) {
699 if (!OverwriteExisting)
700 return;
702 Item->IntValue = IntValue;
703 Item->StringValue = std::string(StringValue);
704 return;
705 }
706
707 // Create new attribute item
709 IntValue, std::string(StringValue)};
710 Contents.push_back(Item);
711}
712
714MCELFStreamer::getAttributeItem(unsigned Attribute) {
715 for (size_t I = 0; I < Contents.size(); ++I)
716 if (Contents[I].Tag == Attribute)
717 return &Contents[I];
718 return nullptr;
719}
720
721size_t
722MCELFStreamer::calculateContentSize(SmallVector<AttributeItem, 64> &AttrsVec) {
723 size_t Result = 0;
724 for (size_t I = 0; I < AttrsVec.size(); ++I) {
725 AttributeItem Item = AttrsVec[I];
726 switch (Item.Type) {
728 break;
730 Result += getULEB128Size(Item.Tag);
731 Result += getULEB128Size(Item.IntValue);
732 break;
734 Result += getULEB128Size(Item.Tag);
735 Result += Item.StringValue.size() + 1; // string + '\0'
736 break;
738 Result += getULEB128Size(Item.Tag);
739 Result += getULEB128Size(Item.IntValue);
740 Result += Item.StringValue.size() + 1; // string + '\0';
741 break;
742 }
743 }
744 return Result;
745}
746
747void MCELFStreamer::createAttributesSection(
748 StringRef Vendor, const Twine &Section, unsigned Type,
749 MCSection *&AttributeSection, SmallVector<AttributeItem, 64> &AttrsVec) {
750 // <format-version>
751 // [ <section-length> "vendor-name"
752 // [ <file-tag> <size> <attribute>*
753 // | <section-tag> <size> <section-number>* 0 <attribute>*
754 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
755 // ]+
756 // ]*
757
758 // Switch section to AttributeSection or get/create the section.
759 if (AttributeSection) {
760 switchSection(AttributeSection);
761 } else {
762 AttributeSection = getContext().getELFSection(Section, Type, 0);
763 switchSection(AttributeSection);
764
765 // Format version
766 emitInt8(0x41);
767 }
768
769 // Vendor size + Vendor name + '\0'
770 const size_t VendorHeaderSize = 4 + Vendor.size() + 1;
771
772 // Tag + Tag Size
773 const size_t TagHeaderSize = 1 + 4;
774
775 const size_t ContentsSize = calculateContentSize(AttrsVec);
776
777 emitInt32(VendorHeaderSize + TagHeaderSize + ContentsSize);
778 emitBytes(Vendor);
779 emitInt8(0); // '\0'
780
782 emitInt32(TagHeaderSize + ContentsSize);
783
784 // Size should have been accounted for already, now
785 // emit each field as its type (ULEB or String)
786 for (size_t I = 0; I < AttrsVec.size(); ++I) {
787 AttributeItem Item = AttrsVec[I];
788 emitULEB128IntValue(Item.Tag);
789 switch (Item.Type) {
790 default:
791 llvm_unreachable("Invalid attribute type");
793 emitULEB128IntValue(Item.IntValue);
794 break;
796 emitBytes(Item.StringValue);
797 emitInt8(0); // '\0'
798 break;
800 emitULEB128IntValue(Item.IntValue);
801 emitBytes(Item.StringValue);
802 emitInt8(0); // '\0'
803 break;
804 }
805 }
806
807 AttrsVec.clear();
808}
809
811 std::unique_ptr<MCAsmBackend> &&MAB,
812 std::unique_ptr<MCObjectWriter> &&OW,
813 std::unique_ptr<MCCodeEmitter> &&CE) {
814 MCELFStreamer *S =
815 new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
816 return S;
817}
BlockVerifier::State From
dxil DXContainer Global Emitter
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
std::string Name
uint64_t Size
static unsigned CombineSymbolTypes(unsigned T1, unsigned T2)
static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI, const MCSubtargetInfo *NewSTI)
static void setSectionAlignmentForBundling(const MCAssembler &Assembler, MCSection *Section)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define T1
#define P(N)
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
This file defines the SmallVector class.
virtual void handleAssemblerFlag(MCAssemblerFlag Flag)
Handle any target-specific assembler flags. By default, do nothing.
Definition: MCAsmBackend.h:232
virtual MCSection * getNonexecutableStackSection(MCContext &Ctx) const
Targets can implement this method to specify a section to switch to if the translation unit doesn't h...
Definition: MCAsmInfo.h:579
MCContext & getContext() const
Definition: MCAssembler.h:322
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:363
bool isBundlingEnabled() const
Definition: MCAssembler.h:361
void setBundleAlignSize(unsigned Size)
Definition: MCAssembler.h:365
void setSubsectionsViaSymbols(bool Value)
Definition: MCAssembler.h:349
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:334
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:332
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:330
std::vector< Symver > Symvers
Definition: MCAssembler.h:236
std::vector< CGProfileEntry > CGProfile
Definition: MCAssembler.h:471
bool registerSymbol(const MCSymbol &Symbol)
Binary assembler expressions.
Definition: MCExpr.h:492
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:639
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:642
virtual void encodeInstruction(const MCInst &Inst, SmallVectorImpl< char > &CB, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
Encode the given Inst to bytes and append to CB.
This is a compact (memory-size-wise) fragment for holding an encoded instruction (non-relaxable) that...
Definition: MCFragment.h:253
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:83
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:410
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:540
void reportWarning(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1063
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:406
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1056
F * allocFragment(Args &&...args)
Definition: MCContext.h:434
Fragment for data and encoded instructions.
Definition: MCFragment.h:235
void emitBundleLock(bool AlignToEnd) override
The following instructions are a bundle-locked group.
SmallVector< AttributeItem, 64 > Contents
void emitValueToAlignment(Align, int64_t, unsigned, unsigned) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitIdent(StringRef IdentString) override
Emit the "identifiers" directive.
void setAttributeItems(unsigned Attribute, unsigned IntValue, StringRef StringValue, bool OverwriteExisting)
void emitBundleUnlock() override
Ends a bundle-locked group.
void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a common symbol.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment) override
Emit a local common (.lcomm) symbol.
void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F, uint64_t Offset) override
void emitAssemblerFlag(MCAssemblerFlag Flag) override
Note in the output the specified Flag.
void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override
Set the DescValue for the Symbol.
void emitELFSize(MCSymbol *Symbol, const MCExpr *Value) override
Emit an ELF .size directive.
void emitThumbFunc(MCSymbol *Func) override
Note in the output that the specified Func is a Thumb mode function (ARM target only).
void emitELFSymverDirective(const MCSymbol *OriginalSym, StringRef Name, bool KeepOriginalSym) override
Emit an ELF .symver directive.
void emitCGProfileEntry(const MCSymbolRefExpr *From, const MCSymbolRefExpr *To, uint64_t Count) override
void emitZerofill(MCSection *Section, MCSymbol *Symbol=nullptr, uint64_t Size=0, Align ByteAlignment=Align(1), SMLoc L=SMLoc()) override
Emit the zerofill section and an optional symbol.
void setAttributeItem(unsigned Attribute, unsigned Value, bool OverwriteExisting)
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override
Emit an weak reference from Alias to Symbol.
void changeSection(MCSection *Section, const MCExpr *Subsection) override
Update streamer for a new active section.
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void emitBundleAlignMode(Align Alignment) override
Set the bundle alignment mode from now on in the section.
bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override
Add the given Attribute to Symbol.
void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size, Align ByteAlignment=Align(1)) override
Emit a thread local bss (.tbss) symbol.
void initSections(bool NoExecStack, const MCSubtargetInfo &STI) override
Create the default sections and set the initial one.
void finishImpl() override
Streamer specific finalization.
MCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:190
void setHasInstructions(const MCSubtargetInfo &STI)
Record that the fragment contains instructions with the MCSubtargetInfo in effect when the instructio...
Definition: MCFragment.h:170
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
@ Unary
Unary expressions.
Definition: MCExpr.h:41
@ Constant
Constant expressions.
Definition: MCExpr.h:39
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:40
@ Target
Target specific expression.
Definition: MCExpr.h:42
@ Binary
Binary expressions.
Definition: MCExpr.h:38
ExprKind getKind() const
Definition: MCExpr.h:81
SMLoc getLoc() const
Definition: MCExpr.h:82
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
virtual unsigned getTextSectionAlignment() const
MCSection * getTextSection() const
Streaming object file generation interface.
void emitValueToAlignment(Align Alignment, int64_t Value=0, unsigned ValueSize=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
MCDataFragment * getOrCreateDataFragment(const MCSubtargetInfo *STI=nullptr)
Get a data fragment to write into, creating a new one if the current fragment is not a data fragment.
MCAssembler & getAssembler()
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment *F, uint64_t Offset)
std::optional< std::pair< bool, std::string > > emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc, const MCSubtargetInfo &STI) override
Record a relocation described by the .reloc directive.
void insert(MCFragment *F)
virtual void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc()) override
Emit the expression Value into the output as a native integer of the given Size bytes.
void finishImpl() override
Streamer specific finalization.
bool changeSectionImpl(MCSection *Section, const MCExpr *Subsection)
MCFragment * getCurrentFragment() const
void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
void emitFrames(MCAsmBackend *MAB)
virtual void markGnuAbi()
ELF only. Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN).
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:267
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:35
void setBundleLockState(BundleLockStateType NewState)
Definition: MCSection.cpp:49
bool isBundleGroupBeforeFirstInst() const
Definition: MCSection.h:172
bool isBundleLocked() const
Definition: MCSection.h:170
@ BundleLockedAlignToEnd
Definition: MCSection.h:55
MCSymbol * getBeginSymbol()
Definition: MCSection.h:142
BundleLockStateType getBundleLockState() const
Definition: MCSection.h:168
void setBundleGroupBeforeFirstInst(bool IsFirst)
Definition: MCSection.h:175
Streaming machine code generation interface.
Definition: MCStreamer.h:212
MCContext & getContext() const
Definition: MCStreamer.h:297
SMLoc getStartTokLoc() const
Definition: MCStreamer.h:289
bool popSection()
Restore the current and previous section from the section stack.
Definition: MCStreamer.h:426
virtual void emitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers.
Definition: MCStreamer.cpp:134
void pushSection()
Save the current and previous section on the section stack.
Definition: MCStreamer.h:417
unsigned emitULEB128IntValue(uint64_t Value, unsigned PadTo=0)
Special case of EmitULEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:162
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
Definition: MCStreamer.h:756
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:390
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:395
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:230
void emitInt8(uint64_t Value)
Definition: MCStreamer.h:754
Generic base class for all target subtargets.
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:410
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:397
VariantKind getKind() const
Definition: MCExpr.h:412
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
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
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:47
void setUsedInReloc() const
Definition: MCSymbol.h:215
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
Represents a location in source code.
Definition: SMLoc.h:23
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
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
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ STB_GLOBAL
Definition: ELF.h:1315
@ STB_LOCAL
Definition: ELF.h:1314
@ STB_GNU_UNIQUE
Definition: ELF.h:1317
@ STB_WEAK
Definition: ELF.h:1316
@ SHT_PROGBITS
Definition: ELF.h:1067
@ SHT_LLVM_CALL_GRAPH_PROFILE
Definition: ELF.h:1103
@ SHT_NOBITS
Definition: ELF.h:1074
@ SHT_GNU_ATTRIBUTES
Definition: ELF.h:1110
@ STV_INTERNAL
Definition: ELF.h:1345
@ STV_HIDDEN
Definition: ELF.h:1346
@ STV_PROTECTED
Definition: ELF.h:1347
@ SHF_MERGE
Definition: ELF.h:1167
@ SHF_STRINGS
Definition: ELF.h:1170
@ SHF_EXCLUDE
Definition: ELF.h:1195
@ SHF_ALLOC
Definition: ELF.h:1161
@ SHF_GNU_RETAIN
Definition: ELF.h:1192
@ SHF_WRITE
Definition: ELF.h:1158
@ SHF_TLS
Definition: ELF.h:1186
@ STT_FUNC
Definition: ELF.h:1328
@ STT_NOTYPE
Definition: ELF.h:1326
@ STT_GNU_IFUNC
Definition: ELF.h:1333
@ STT_OBJECT
Definition: ELF.h:1327
@ STT_TLS
Definition: ELF.h:1332
NodeAddr< CodeNode * > Code
Definition: RDFGraph.h:388
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
MCStreamer * createELFStreamer(MCContext &Ctx, std::unique_ptr< MCAsmBackend > &&TAB, std::unique_ptr< MCObjectWriter > &&OW, std::unique_ptr< MCCodeEmitter > &&CE)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:19
MCAssemblerFlag
Definition: MCDirectives.h:53
@ MCAF_SyntaxUnified
.syntax (ARM/ELF)
Definition: MCDirectives.h:54
@ MCAF_Code64
.code64 (X86)
Definition: MCDirectives.h:58
@ MCAF_Code16
.code16 (X86) / .code 16 (ARM)
Definition: MCDirectives.h:56
@ MCAF_Code32
.code32 (X86) / .code 32 (ARM)
Definition: MCDirectives.h:57
@ MCAF_SubsectionsViaSymbols
.subsections_via_symbols (MachO)
Definition: MCDirectives.h:55
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:66
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
MCSymbolAttr
Definition: MCDirectives.h:18
@ MCSA_Local
.local (ELF)
Definition: MCDirectives.h:38
@ MCSA_WeakDefAutoPrivate
.weak_def_can_be_hidden (MachO)
Definition: MCDirectives.h:48
@ MCSA_Memtag
.memtag (ELF)
Definition: MCDirectives.h:50
@ MCSA_Protected
.protected (ELF)
Definition: MCDirectives.h:43
@ MCSA_Exported
.globl _foo, exported (XCOFF)
Definition: MCDirectives.h:34
@ MCSA_PrivateExtern
.private_extern (MachO)
Definition: MCDirectives.h:42
@ MCSA_Internal
.internal (ELF)
Definition: MCDirectives.h:36
@ MCSA_WeakReference
.weak_reference (MachO)
Definition: MCDirectives.h:47
@ MCSA_AltEntry
.alt_entry (MachO)
Definition: MCDirectives.h:41
@ MCSA_ELF_TypeIndFunction
.type _foo, STT_GNU_IFUNC
Definition: MCDirectives.h:24
@ MCSA_LazyReference
.lazy_reference (MachO)
Definition: MCDirectives.h:37
@ MCSA_ELF_TypeNoType
.type _foo, STT_NOTYPE # aka @notype
Definition: MCDirectives.h:28
@ MCSA_Reference
.reference (MachO)
Definition: MCDirectives.h:44
@ MCSA_SymbolResolver
.symbol_resolver (MachO)
Definition: MCDirectives.h:40
@ MCSA_Weak
.weak
Definition: MCDirectives.h:45
@ MCSA_ELF_TypeTLS
.type _foo, STT_TLS # aka @tls_object
Definition: MCDirectives.h:26
@ MCSA_IndirectSymbol
.indirect_symbol (MachO)
Definition: MCDirectives.h:35
@ MCSA_WeakDefinition
.weak_definition (MachO)
Definition: MCDirectives.h:46
@ MCSA_ELF_TypeCommon
.type _foo, STT_COMMON # aka @common
Definition: MCDirectives.h:27
@ MCSA_Global
.type _foo, @gnu_unique_object
Definition: MCDirectives.h:30
@ MCSA_WeakAntiDep
.weak_anti_dep (COFF)
Definition: MCDirectives.h:49
@ MCSA_Extern
.extern (XCOFF)
Definition: MCDirectives.h:32
@ MCSA_Cold
.cold (MachO)
Definition: MCDirectives.h:22
@ MCSA_ELF_TypeObject
.type _foo, STT_OBJECT # aka @object
Definition: MCDirectives.h:25
@ MCSA_ELF_TypeGnuUniqueObject
Definition: MCDirectives.h:29
@ MCSA_ELF_TypeFunction
.type _foo, STT_FUNC # aka @function
Definition: MCDirectives.h:23
@ MCSA_Hidden
.hidden (ELF)
Definition: MCDirectives.h:33
@ MCSA_LGlobal
.lglobl (XCOFF)
Definition: MCDirectives.h:31
@ MCSA_Invalid
Not a valid directive.
Definition: MCDirectives.h:19
@ MCSA_NoDeadStrip
.no_dead_strip (MachO)
Definition: MCDirectives.h:39
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:85
const MCSymbolRefExpr * From
Definition: MCAssembler.h:467
const MCSymbolRefExpr * To
Definition: MCAssembler.h:468
ELF object attributes section emission support.
Definition: MCELFStreamer.h:90