LLVM 22.0.0git
MCObjectStreamer.cpp
Go to the documentation of this file.
1//===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
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
11#include "llvm/MC/MCAsmInfo.h"
12#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCCodeView.h"
15#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCDwarf.h"
17#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCSFrame.h"
21#include "llvm/MC/MCSection.h"
22#include "llvm/MC/MCSymbol.h"
25using namespace llvm;
26
28 std::unique_ptr<MCAsmBackend> TAB,
29 std::unique_ptr<MCObjectWriter> OW,
30 std::unique_ptr<MCCodeEmitter> Emitter)
32 Assembler(std::make_unique<MCAssembler>(
33 Context, std::move(TAB), std::move(Emitter), std::move(OW))),
34 EmitEHFrame(true), EmitDebugFrame(false), EmitSFrame(false) {
35 assert(Assembler->getBackendPtr() && Assembler->getEmitterPtr());
36 IsObj = true;
37 setAllowAutoPadding(Assembler->getBackend().allowAutoPadding());
38 if (Context.getTargetOptions() && Context.getTargetOptions()->MCRelaxAll)
39 Assembler->setRelaxAll(true);
40}
41
43
46 return Assembler.get();
47 return nullptr;
48}
49
50constexpr size_t FragBlockSize = 16384;
51// Ensure the new fragment can at least store a few bytes.
52constexpr size_t NewFragHeadroom = 8;
53
54static_assert(NewFragHeadroom >= alignof(MCFragment));
55static_assert(FragBlockSize >= sizeof(MCFragment) + NewFragHeadroom);
56
58 auto Size = std::max(FragBlockSize, sizeof(MCFragment) + Headroom);
59 FragSpace = Size - sizeof(MCFragment);
60 auto Block = std::unique_ptr<uint8_t[]>(new uint8_t[Size]);
61 auto *F = reinterpret_cast<MCFragment *>(Block.get());
62 FragStorage.push_back(std::move(Block));
63 return F;
64}
65
68 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
69 auto End = reinterpret_cast<size_t>(getCurFragEnd());
70 F = reinterpret_cast<MCFragment *>(
71 alignToPowerOf2(End, alignof(MCFragment)));
72 FragSpace -= size_t(F) - End + sizeof(MCFragment);
73 } else {
74 F = allocFragSpace(0);
75 }
76 new (F) MCFragment();
78}
79
80void MCObjectStreamer::ensureHeadroom(size_t Headroom) {
81 if (Headroom <= FragSpace)
82 return;
83 auto *F = allocFragSpace(Headroom);
84 new (F) MCFragment();
86}
87
88void MCObjectStreamer::addSpecialFragment(MCFragment *Frag) {
90 "Frag should have a variable-size tail");
91 // Frag is not connected to FragSpace. Before modifying CurFrag with
92 // addFragment(Frag), allocate an empty fragment to maintain FragSpace
93 // connectivity, potentially reusing CurFrag's associated space.
95 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
96 auto End = reinterpret_cast<size_t>(getCurFragEnd());
97 F = reinterpret_cast<MCFragment *>(
98 alignToPowerOf2(End, alignof(MCFragment)));
99 FragSpace -= size_t(F) - End + sizeof(MCFragment);
100 } else {
101 F = allocFragSpace(0);
102 }
103 new (F) MCFragment();
104
105 addFragment(Frag);
106 addFragment(F);
107}
108
110 ensureHeadroom(Contents.size());
111 assert(FragSpace >= Contents.size());
112 llvm::copy(Contents, getCurFragEnd());
113 CurFrag->FixedSize += Contents.size();
114 FragSpace -= Contents.size();
115}
116
118 ensureHeadroom(Num);
120 llvm::fill(Data, Elt);
121 CurFrag->FixedSize += Num;
122 FragSpace -= Num;
123}
124
127}
128
129// As a compile-time optimization, avoid allocating and evaluating an MCExpr
130// tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment's fixed
131// part.
132static std::optional<uint64_t> absoluteSymbolDiff(const MCSymbol *Hi,
133 const MCSymbol *Lo) {
134 assert(Hi && Lo);
135 if (Lo == Hi)
136 return 0;
137 if (Hi->isVariable() || Lo->isVariable())
138 return std::nullopt;
139 auto *LoF = Lo->getFragment();
140 if (!LoF || Hi->getFragment() != LoF || LoF->isLinkerRelaxable())
141 return std::nullopt;
142 // If either symbol resides in the variable part, bail out.
143 auto Fixed = LoF->getFixedSize();
144 if (Lo->getOffset() > Fixed || Hi->getOffset() > Fixed)
145 return std::nullopt;
146
147 return Hi->getOffset() - Lo->getOffset();
148}
149
151 const MCSymbol *Lo,
152 unsigned Size) {
153 if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo))
154 emitIntValue(*Diff, Size);
155 else
157}
158
160 const MCSymbol *Lo) {
161 if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo))
162 emitULEB128IntValue(*Diff);
163 else
165}
166
168 if (Assembler) {
169 Assembler->reset();
170 if (getContext().getTargetOptions())
171 Assembler->setRelaxAll(getContext().getTargetOptions()->MCRelaxAll);
172 }
173 EmitEHFrame = true;
174 EmitDebugFrame = false;
175 FragStorage.clear();
176 FragSpace = 0;
177 SpecialFragAllocator.Reset();
179}
180
182 if (!getNumFrameInfos())
183 return;
184
185 if (EmitEHFrame)
186 MCDwarfFrameEmitter::Emit(*this, MAB, true);
187
188 if (EmitDebugFrame)
189 MCDwarfFrameEmitter::Emit(*this, MAB, false);
190
191 if (EmitSFrame || (getContext().getTargetOptions() &&
192 getContext().getTargetOptions()->EmitSFrameUnwind))
194}
195
197 Assembler->registerSymbol(Sym);
198}
199
200void MCObjectStreamer::emitCFISections(bool EH, bool Debug, bool SFrame) {
202 EmitEHFrame = EH;
203 EmitDebugFrame = Debug;
204 EmitSFrame = SFrame;
205}
206
208 SMLoc Loc) {
210
212
213 // Avoid fixups when possible.
214 int64_t AbsValue;
215 if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
216 if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
218 Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
219 return;
220 }
221 emitIntValue(AbsValue, Size);
222 return;
223 }
227}
228
230 MCSymbol *Label = getContext().createTempSymbol("cfi");
231 emitLabel(Label);
232 return Label;
233}
234
235void MCObjectStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
236 // We need to create a local symbol to avoid relocations.
238 emitLabel(Frame.Begin);
239}
240
241void MCObjectStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
242 Frame.End = getContext().createTempSymbol();
243 emitLabel(Frame.End);
244}
245
247 MCStreamer::emitLabel(Symbol, Loc);
248 // If Symbol is a non-redefiniable variable, emitLabel has reported an error.
249 // Bail out.
250 if (Symbol->isVariable())
251 return;
252
253 getAssembler().registerSymbol(*Symbol);
254
255 // Set the fragment and offset. This function might be called by
256 // changeSection, when the section stack top hasn't been changed to the new
257 // section.
259 Symbol->setFragment(F);
260 Symbol->setOffset(F->getFixedSize());
261
263}
264
266 auto Assignments = pendingAssignments.find(Symbol);
267 if (Assignments != pendingAssignments.end()) {
268 for (const PendingAssignment &A : Assignments->second)
269 emitAssignment(A.Symbol, A.Value);
270
271 pendingAssignments.erase(Assignments);
272 }
273}
274
275// Emit a label at a previously emitted fragment/offset position. This must be
276// within the currently-active section.
279 assert(F.getParent() == getCurrentSectionOnly());
280 MCStreamer::emitLabel(Symbol, Loc);
281 getAssembler().registerSymbol(*Symbol);
282 Symbol->setFragment(&F);
283 Symbol->setOffset(Offset);
284}
285
287 int64_t IntValue;
288 if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
289 emitULEB128IntValue(IntValue);
290 return;
291 }
292 auto *F = getCurrentFragment();
293 F->makeLEB(false, Value);
294 newFragment();
295}
296
298 int64_t IntValue;
299 if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
300 emitSLEB128IntValue(IntValue);
301 return;
302 }
303 auto *F = getCurrentFragment();
304 F->makeLEB(true, Value);
305 newFragment();
306}
307
309 const MCSymbol *Target) {
310 reportFatalUsageError("this file format doesn't support weak aliases");
311}
312
314 assert(Section && "Cannot switch to a null section!");
316
317 // Register the section and create an initial fragment for subsection 0
318 // if `Subsection` is non-zero.
319 bool NewSec = getAssembler().registerSection(*Section);
320 MCFragment *F0 = nullptr;
321 if (NewSec && Subsection) {
322 changeSection(Section, 0);
323 F0 = CurFrag;
324 }
325
326 // To maintain connectivity between CurFrag and FragSpace when CurFrag is
327 // modified, allocate an empty fragment and append it to the fragment list.
328 // (Subsections[I].second.Tail is not connected to FragSpace.)
329 MCFragment *F;
330 if (LLVM_LIKELY(sizeof(MCFragment) + NewFragHeadroom <= FragSpace)) {
331 auto End = reinterpret_cast<size_t>(getCurFragEnd());
332 F = reinterpret_cast<MCFragment *>(
333 alignToPowerOf2(End, alignof(MCFragment)));
334 FragSpace -= size_t(F) - End + sizeof(MCFragment);
335 } else {
336 F = allocFragSpace(0);
337 }
338 new (F) MCFragment();
339 F->setParent(Section);
340
341 auto &Subsections = Section->Subsections;
342 size_t I = 0, E = Subsections.size();
343 while (I != E && Subsections[I].first < Subsection)
344 ++I;
345 // If the subsection number is not in the sorted Subsections list, create a
346 // new fragment list.
347 if (I == E || Subsections[I].first != Subsection) {
348 Subsections.insert(Subsections.begin() + I,
349 {Subsection, MCSection::FragList{F, F}});
350 Section->CurFragList = &Subsections[I].second;
351 CurFrag = F;
352 } else {
353 Section->CurFragList = &Subsections[I].second;
354 CurFrag = Subsections[I].second.Tail;
355 // Ensure CurFrag is associated with FragSpace.
356 addFragment(F);
357 }
358
359 // Define the section symbol at subsection 0's initial fragment if required.
360 if (!NewSec)
361 return;
362 if (auto *Sym = Section->getBeginSymbol()) {
363 Sym->setFragment(Subsection ? F0 : CurFrag);
365 }
366}
367
369 getAssembler().registerSymbol(*Symbol);
372}
373
375 const MCExpr *Value) {
376 const MCSymbol *Target = &cast<MCSymbolRefExpr>(*Value).getSymbol();
377
378 // If the symbol already exists, emit the assignment. Otherwise, emit it
379 // later only if the symbol is also emitted.
380 if (Target->isRegistered())
381 emitAssignment(Symbol, Value);
382 else
383 pendingAssignments[Target].push_back({Symbol, Value});
384}
385
387 return Sec.hasInstructions();
388}
389
391 const MCSubtargetInfo &STI) {
393
395 Sec->setHasInstructions(true);
396
397 // Now that a machine instruction has been assembled into this section, make
398 // a line entry for any .loc directive that has been seen.
400
401 // If this instruction doesn't need relaxation, just emit it as data.
402 MCAssembler &Assembler = getAssembler();
403 MCAsmBackend &Backend = Assembler.getBackend();
404 if (!(Backend.mayNeedRelaxation(Inst.getOpcode(), Inst.getOperands(), STI) ||
405 Backend.allowEnhancedRelaxation())) {
406 emitInstToData(Inst, STI);
407 return;
408 }
409
410 // Otherwise, relax and emit it as data if RelaxAll is specified.
411 if (Assembler.getRelaxAll()) {
412 MCInst Relaxed = Inst;
413 while (Backend.mayNeedRelaxation(Relaxed.getOpcode(), Relaxed.getOperands(),
414 STI))
415 Backend.relaxInstruction(Relaxed, STI);
416 emitInstToData(Relaxed, STI);
417 return;
418 }
419
420 emitInstToFragment(Inst, STI);
421}
422
423void MCObjectStreamer::emitInstToData(const MCInst &Inst,
424 const MCSubtargetInfo &STI) {
426
427 // Append the instruction to the data fragment.
428 size_t CodeOffset = getCurFragSize();
431 getAssembler().getEmitter().encodeInstruction(Inst, Content, Fixups, STI);
433 if (CurFrag != F) {
434 F = CurFrag;
435 CodeOffset = 0;
436 }
437 F->setHasInstructions(STI);
438
439 if (Fixups.empty())
440 return;
441 bool MarkedLinkerRelaxable = false;
442 for (auto &Fixup : Fixups) {
443 Fixup.setOffset(Fixup.getOffset() + CodeOffset);
444 if (!Fixup.isLinkerRelaxable() || MarkedLinkerRelaxable)
445 continue;
446 MarkedLinkerRelaxable = true;
447 // Set the fragment's order within the subsection for use by
448 // MCAssembler::relaxAlign.
449 auto *Sec = F->getParent();
450 if (!Sec->isLinkerRelaxable())
451 Sec->setFirstLinkerRelaxable(F->getLayoutOrder());
452 // Do not add data after a linker-relaxable instruction. The difference
453 // between a new label and a label at or before the linker-relaxable
454 // instruction cannot be resolved at assemble-time.
455 F->setLinkerRelaxable();
456 newFragment();
457 }
458 F->appendFixups(Fixups);
459}
460
462 const MCSubtargetInfo &STI) {
463 auto *F = getCurrentFragment();
466 getAssembler().getEmitter().encodeInstruction(Inst, Data, Fixups, STI);
467
469 F->setHasInstructions(STI);
470
471 F->setVarContents(Data);
472 F->setInst(Inst);
473
474 bool MarkedLinkerRelaxable = false;
475 for (auto &Fixup : Fixups) {
476 if (!Fixup.isLinkerRelaxable() || MarkedLinkerRelaxable)
477 continue;
478 MarkedLinkerRelaxable = true;
479 auto *Sec = F->getParent();
480 if (!Sec->isLinkerRelaxable())
481 Sec->setFirstLinkerRelaxable(F->getLayoutOrder());
482 F->setLinkerRelaxable();
483 }
484 F->setVarFixups(Fixups);
485
486 newFragment();
487}
488
489void MCObjectStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
490 unsigned Column, unsigned Flags,
491 unsigned Isa,
492 unsigned Discriminator,
493 StringRef FileName,
494 StringRef Comment) {
495 // In case we see two .loc directives in a row, make sure the
496 // first one gets a line entry.
498
499 this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
500 Discriminator, FileName, Comment);
501}
502
504 const MCSymbol *B, SMLoc Loc) {
505 MCContext &Context = OS.getContext();
506 const MCExpr *ARef = MCSymbolRefExpr::create(A, Context);
507 const MCExpr *BRef = MCSymbolRefExpr::create(B, Context);
508 const MCExpr *AddrDelta =
510 return AddrDelta;
511}
512
515 int64_t LineDelta, const MCSymbol *Label,
516 int PointerSize) {
517 // emit the sequence to set the address
518 OS.emitIntValue(dwarf::DW_LNS_extended_op, 1);
519 OS.emitULEB128IntValue(PointerSize + 1);
520 OS.emitIntValue(dwarf::DW_LNE_set_address, 1);
521 OS.emitSymbolValue(Label, PointerSize);
522
523 // emit the sequence for the LineDelta (from 1) and a zero address delta.
524 MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
525}
526
528 const MCSymbol *LastLabel,
529 const MCSymbol *Label,
530 unsigned PointerSize) {
531 if (!LastLabel) {
532 emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
533 Label, PointerSize);
534 return;
535 }
536
537 // If the two labels are within the same fragment, then the address-offset is
538 // already a fixed constant and is not relaxable. Emit the advance-line-addr
539 // data immediately to save time and memory.
540 if (auto OptAddrDelta = absoluteSymbolDiff(Label, LastLabel)) {
541 SmallString<16> Tmp;
542 MCDwarfLineAddr::encode(getContext(), Assembler->getDWARFLinetableParams(),
543 LineDelta, *OptAddrDelta, Tmp);
544 emitBytes(Tmp);
545 return;
546 }
547
548 auto *F = getCurrentFragment();
549 F->Kind = MCFragment::FT_Dwarf;
550 F->setDwarfAddrDelta(buildSymbolDiff(*this, Label, LastLabel, SMLoc()));
551 F->setDwarfLineDelta(LineDelta);
552 newFragment();
553}
554
556 MCSymbol *LastLabel,
557 MCSymbol *EndLabel) {
558 // Emit a DW_LNE_end_sequence into the line table. When EndLabel is null, it
559 // means we should emit the entry for the end of the section and therefore we
560 // use the section end label for the reference label. After having the
561 // appropriate reference label, we emit the address delta and use INT64_MAX as
562 // the line delta which is the signal that this is actually a
563 // DW_LNE_end_sequence.
564 if (!EndLabel)
565 EndLabel = endSection(Section);
566
567 // Switch back the dwarf line section, in case endSection had to switch the
568 // section.
569 MCContext &Ctx = getContext();
571
572 const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
573 emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, EndLabel,
574 AsmInfo->getCodePointerSize());
575}
576
578 const MCSymbol *Label,
579 SMLoc Loc) {
580 auto *F = getCurrentFragment();
582 F->setDwarfAddrDelta(buildSymbolDiff(*this, Label, LastLabel, Loc));
583 newFragment();
584}
585
587 unsigned Line, unsigned Column,
588 bool PrologueEnd, bool IsStmt,
589 StringRef FileName, SMLoc Loc) {
590 // Validate the directive.
591 if (!checkCVLocSection(FunctionId, FileNo, Loc))
592 return;
593
594 // Emit a label at the current position and record it in the CodeViewContext.
595 MCSymbol *LineSym = getContext().createTempSymbol();
596 emitLabel(LineSym);
598 FileNo, Line, Column, PrologueEnd,
599 IsStmt);
600}
601
603 const MCSymbol *Begin,
604 const MCSymbol *End) {
606 End);
607 this->MCStreamer::emitCVLinetableDirective(FunctionId, Begin, End);
608}
609
611 unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
612 const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
614 *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
615 FnEndSym);
617 PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
618}
619
621 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
622 StringRef FixedSizePortion) {
623 getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
624 // Attach labels that were pending before we created the defrange fragment to
625 // the beginning of the new fragment.
626 this->MCStreamer::emitCVDefRangeDirective(Ranges, FixedSizePortion);
627}
628
631}
634}
635
638}
639
642 appendContents(ArrayRef(Data.data(), Data.size()));
643}
644
646 uint8_t FillLen,
647 unsigned MaxBytesToEmit) {
648 if (MaxBytesToEmit == 0)
649 MaxBytesToEmit = Alignment.value();
651 F->makeAlign(Alignment, Fill, FillLen, MaxBytesToEmit);
652 newFragment();
653
654 // Update the maximum alignment on the current section if necessary.
655 F->getParent()->ensureMinAlignment(Alignment);
656}
657
659 const MCSubtargetInfo *STI,
660 unsigned MaxBytesToEmit) {
661 auto *F = getCurrentFragment();
662 emitValueToAlignment(Alignment, 0, 1, MaxBytesToEmit);
663 F->u.align.EmitNops = true;
664 F->STI = STI;
665}
666
668 unsigned char Value,
669 SMLoc Loc) {
670 newSpecialFragment<MCOrgFragment>(*Offset, Value, Loc);
671}
672
674 const MCExpr *Expr, SMLoc Loc) {
675 std::optional<MCFixupKind> MaybeKind =
676 Assembler->getBackend().getFixupKind(Name);
677 if (!MaybeKind) {
678 getContext().reportError(Loc, "unknown relocation name");
679 return;
680 }
681
682 MCFixupKind Kind = *MaybeKind;
683 if (Expr)
684 visitUsedExpr(*Expr);
685 else
686 Expr =
687 MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
688
689 auto *O = &Offset;
690 int64_t Val;
691 if (Offset.evaluateAsAbsolute(Val, nullptr)) {
692 auto *SecSym = getCurrentSectionOnly()->getBeginSymbol();
694 O, getContext(), Loc);
695 }
696 getAssembler().addRelocDirective({*O, Expr, Kind});
697}
698
699void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
700 SMLoc Loc) {
701 assert(getCurrentSectionOnly() && "need a section");
702 newSpecialFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc);
703}
704
705void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
706 int64_t Expr, SMLoc Loc) {
707 int64_t IntNumValues;
708 // Do additional checking now if we can resolve the value.
709 if (NumValues.evaluateAsAbsolute(IntNumValues, getAssembler())) {
710 if (IntNumValues < 0) {
713 "'.fill' directive with negative repeat count has no effect");
714 return;
715 }
716 // Emit now if we can for better errors.
717 int64_t NonZeroSize = Size > 4 ? 4 : Size;
718 Expr &= ~0ULL >> (64 - NonZeroSize * 8);
719 for (uint64_t i = 0, e = IntNumValues; i != e; ++i) {
720 emitIntValue(Expr, NonZeroSize);
721 if (NonZeroSize < Size)
722 emitIntValue(0, Size - NonZeroSize);
723 }
724 return;
725 }
726
727 // Otherwise emit as fragment.
728 assert(getCurrentSectionOnly() && "need a section");
729 newSpecialFragment<MCFillFragment>(Expr, Size, NumValues, Loc);
730}
731
732void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,
733 SMLoc Loc, const MCSubtargetInfo &STI) {
734 assert(getCurrentSectionOnly() && "need a section");
735 newSpecialFragment<MCNopsFragment>(NumBytes, ControlledNopLength, Loc, STI);
736}
737
739 MCAssembler &Asm = getAssembler();
740 Asm.getWriter().addFileName(Filename);
741}
742
744 StringRef CompilerVersion,
745 StringRef TimeStamp,
746 StringRef Description) {
748 W.addFileName(Filename);
749 if (CompilerVersion.size())
750 W.setCompilerVersion(CompilerVersion);
751 // TODO: add TimeStamp and Description to .file symbol table entry
752 // with the integrated assembler.
753}
754
757}
758
761}
762
765
766 // If we are generating dwarf for assembly source files dump out the sections.
767 if (getContext().getGenDwarfForAssembly())
769
770 // Dump out the dwarf file & directory tables and line tables.
771 MCDwarfLineTable::emit(this, getAssembler().getDWARFLinetableParams());
772
773 // Emit pseudo probes for the current module.
775
777}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:335
dxil DXContainer Global Emitter
static ManagedStatic< cl::opt< bool, true >, CreateDebug > Debug
Definition: Debug.cpp:147
T Content
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
Symbol * Sym
Definition: ELF_riscv.cpp:479
static const MCExpr * buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A, const MCSymbol *B, SMLoc Loc)
constexpr size_t FragBlockSize
static void emitDwarfSetLineAddr(MCObjectStreamer &OS, MCDwarfLineTableParams Params, int64_t LineDelta, const MCSymbol *Label, int PointerSize)
static std::optional< uint64_t > absoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo)
constexpr size_t NewFragHeadroom
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:124
void emitLineTableForFunction(MCObjectStreamer &OS, unsigned FuncId, const MCSymbol *FuncBegin, const MCSymbol *FuncEnd)
Emits a line table substream.
Definition: MCCodeView.cpp:338
void emitFileChecksums(MCObjectStreamer &OS)
Emits the file checksum substream.
Definition: MCCodeView.cpp:181
void recordCVLoc(MCContext &Ctx, const MCSymbol *Label, unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt)
Saves the information from the currently parsed .cv_loc directive and sets CVLocSeen.
Definition: MCCodeView.cpp:128
void emitDefRange(MCObjectStreamer &OS, ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion)
Definition: MCCodeView.cpp:443
void emitFileChecksumOffset(MCObjectStreamer &OS, unsigned FileNo)
Emits the offset into the checksum table of the given file number.
Definition: MCCodeView.cpp:236
void emitInlineLineTableForFunction(MCObjectStreamer &OS, unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
Definition: MCCodeView.cpp:431
void emitStringTable(MCObjectStreamer &OS)
Emits the string table substream.
Definition: MCCodeView.cpp:158
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:55
bool allowEnhancedRelaxation() const
Return true if this target allows an unrelaxable instruction to be emitted into RelaxableFragment and...
Definition: MCAsmBackend.h:81
virtual void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const
Relax the instruction in the given fragment to the next wider instruction.
Definition: MCAsmBackend.h:157
virtual bool mayNeedRelaxation(unsigned Opcode, ArrayRef< MCOperand > Operands, const MCSubtargetInfo &STI) const
Check whether the given instruction (encoded as Opcode+Operands) may need relaxation.
Definition: MCAsmBackend.h:135
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:64
unsigned getCodePointerSize() const
Get the code pointer size in bytes.
Definition: MCAsmInfo.h:443
LLVM_ABI void Finish()
Finish - Do final processing and write the object to the output stream.
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:179
LLVM_ABI void addRelocDirective(RelocDirective RD)
bool getRelaxAll() const
Definition: MCAssembler.h:193
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:177
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:175
LLVM_ABI bool registerSection(MCSection &Section)
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:343
static LLVM_ABI const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.cpp:201
@ Sub
Subtraction.
Definition: MCExpr.h:324
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.
Context object for machine code objects.
Definition: MCContext.h:83
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:416
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
Definition: MCContext.cpp:386
LLVM_ABI void RemapDebugPaths()
Definition: MCContext.cpp:950
void clearDwarfLocSeen()
Definition: MCContext.h:766
LLVM_ABI CodeViewContext & getCVContext()
Definition: MCContext.cpp:1060
const SourceMgr * getSourceManager() const
Definition: MCContext.h:401
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:412
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1115
static LLVM_ABI void Emit(MCObjectStreamer &streamer, MCAsmBackend *MAB, bool isEH)
Definition: MCDwarf.cpp:1896
static LLVM_ABI void Emit(MCStreamer *MCOS, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta)
Utility function to emit the encoding to a streamer.
Definition: MCDwarf.cpp:720
static LLVM_ABI void encode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, SmallVectorImpl< char > &OS)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Definition: MCDwarf.cpp:735
static LLVM_ABI void make(MCStreamer *MCOS, MCSection *Section)
Definition: MCDwarf.cpp:91
static LLVM_ABI void emit(MCStreamer *MCOS, MCDwarfLineTableParams Params)
Definition: MCDwarf.cpp:298
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Definition: MCFixup.h:110
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, bool PCRel=false)
Consider bit fields if we need more flags.
Definition: MCFixup.h:86
FragmentType getKind() const
Definition: MCSection.h:156
LLVM_ABI void addFixup(MCFixup Fixup)
Definition: MCSection.cpp:71
static LLVM_ABI void Emit(MCStreamer *MCOS)
Definition: MCDwarf.cpp:1185
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
unsigned getOpcode() const
Definition: MCInst.h:202
ArrayRef< MCOperand > getOperands() const
Definition: MCInst.h:214
MCSection * getDwarfLineSection() const
Streaming object file generation interface.
void reset() override
state management
void emitFill(const MCExpr &NumBytes, uint64_t FillValue, SMLoc Loc=SMLoc()) override
Emit Size bytes worth of the value specified by FillValue.
void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol.
void emitCFISections(bool EH, bool Debug, bool SFrame) override
void emitULEB128Value(const MCExpr *Value) override
void emitSLEB128Value(const MCExpr *Value) override
void emitNops(int64_t NumBytes, int64_t ControlledNopLength, SMLoc Loc, const MCSubtargetInfo &STI) override
void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) override
This implements the CodeView '.cv_inline_linetable' assembler directive.
void emitCVStringTableDirective() override
This implements the CodeView '.cv_stringtable' assembler directive.
MCAssembler & getAssembler()
void emitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, const MCSymbol *Label, unsigned PointerSize) override
If targets does not support representing debug line section by .loc/.file directives in assembly outp...
void emitWeakReference(MCSymbol *Alias, const MCSymbol *Target) override
Emit an weak reference from Alias to Symbol.
void appendContents(ArrayRef< char > Contents)
void emitCVLocDirective(unsigned FunctionId, unsigned FileNo, unsigned Line, unsigned Column, bool PrologueEnd, bool IsStmt, StringRef FileName, SMLoc Loc) override
This implements the CodeView '.cv_loc' assembler directive.
void emitRelocDirective(const MCExpr &Offset, StringRef Name, const MCExpr *Expr, SMLoc Loc={}) override
Record a relocation described by the .reloc directive.
void emitBytes(StringRef Data) override
Emit the bytes in Data into the output.
void emitPendingAssignments(MCSymbol *Symbol)
Emits pending conditional assignments that depend on Symbol being emitted.
void addFixup(const MCExpr *Value, MCFixupKind Kind)
void emitFileDirective(StringRef Filename) override
Switch to a new logical file.
void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName, StringRef Comment={}) override
This implements the DWARF2 '.loc fileno lineno ...' assembler directive.
MCSymbol * emitCFILabel() override
When emitting an object file, create and emit a real label.
void emitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion) override
This implements the CodeView '.cv_def_range' assembler directive.
MCAssembler * getAssemblerPtr() override
void emitAddrsigSym(const MCSymbol *Sym) override
void emitDwarfLineEndEntry(MCSection *Section, MCSymbol *LastLabel, MCSymbol *EndLabel=nullptr) override
Emit the debug line end entry.
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void visitUsedSymbol(const MCSymbol &Sym) override
void emitInstToFragment(const MCInst &Inst, const MCSubtargetInfo &)
Emit an instruction to a special fragment, because this instruction can change its size during relaxa...
virtual void emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc, MCFragment &F, uint64_t Offset)
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
MCObjectStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
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.
void changeSection(MCSection *Section, uint32_t Subsection=0) override
This is called by popSection and switchSection, if the current section changes.
void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo) override
Emit the absolute difference between two symbols encoded with ULEB128.
MCFragment * allocFragSpace(size_t Headroom)
void emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel, const MCSymbol *Label, SMLoc Loc)
bool mayHaveInstructions(MCSection &Sec) const override
void emitCodeAlignment(Align ByteAlignment, const MCSubtargetInfo *STI, unsigned MaxBytesToEmit=0) override
Emit nops until the byte alignment ByteAlignment is reached.
void emitFrames(MCAsmBackend *MAB)
void ensureHeadroom(size_t Headroom)
void emitCVFileChecksumOffsetDirective(unsigned FileNo) override
This implements the CodeView '.cv_filechecksumoffset' assembler directive.
void emitConditionalAssignment(MCSymbol *Symbol, const MCExpr *Value) override
Emit an assignment of Value to Symbol, but only if Value is also emitted.
void emitCVFileChecksumsDirective() override
This implements the CodeView '.cv_filechecksums' assembler directive.
void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *Begin, const MCSymbol *End) override
This implements the CodeView '.cv_linetable' assembler directive.
void emitValueToOffset(const MCExpr *Offset, unsigned char Value, SMLoc Loc) override
Emit some number of copies of Value until the byte offset Offset is reached.
uint8_t * getCurFragEnd() const
void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size) override
Emit the absolute difference between two symbols if possible.
Defines the object file and target independent interfaces used by the assembler backend to write nati...
void emitAddrsigSection()
Tell the object writer to emit an address-significance table during writeObject().
void addAddrsigSymbol(const MCSymbol *Sym)
Record the given symbol in the address-significance table to be written diring writeObject().
static LLVM_ABI void emit(MCObjectStreamer *MCOS)
static void emit(MCObjectStreamer &Streamer)
Definition: MCSFrame.cpp:147
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
bool hasInstructions() const
Definition: MCSection.h:591
void setHasInstructions(bool Value)
Definition: MCSection.h:592
MCSymbol * getBeginSymbol()
Definition: MCSection.h:568
Streaming machine code generation interface.
Definition: MCStreamer.h:220
virtual void emitAssignment(MCSymbol *Symbol, const MCExpr *Value)
Emit an assignment of Value to Symbol.
virtual void emitCFISections(bool EH, bool Debug, bool SFrame)
Definition: MCStreamer.cpp:417
virtual void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI)
Emit the given Instruction into the current section.
MCFragment * getCurrentFragment() const
Definition: MCStreamer.h:432
virtual void emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi, const MCSymbol *Lo)
Emit the absolute difference between two symbols encoded with ULEB128.
bool getUseAssemblerInfoForParsing()
Definition: MCStreamer.h:322
MCContext & getContext() const
Definition: MCStreamer.h:314
bool checkCVLocSection(unsigned FuncId, unsigned FileNo, SMLoc Loc)
Returns true if the .cv_loc directive is in the right section.
Definition: MCStreamer.cpp:305
virtual void emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size)
Emit the absolute difference between two symbols.
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:395
void setAllowAutoPadding(bool v)
Definition: MCStreamer.h:328
virtual void reset()
State management.
Definition: MCStreamer.cpp:100
virtual void emitDwarfLocDirective(unsigned FileNo, unsigned Line, unsigned Column, unsigned Flags, unsigned Isa, unsigned Discriminator, StringRef FileName, StringRef Comment={})
This implements the DWARF2 '.loc fileno lineno ...' assembler directive.
Definition: MCStreamer.cpp:237
virtual void emitCVLinetableDirective(unsigned FunctionId, const MCSymbol *FnStart, const MCSymbol *FnEnd)
This implements the CodeView '.cv_linetable' assembler directive.
Definition: MCStreamer.cpp:327
MCFragment * CurFrag
Definition: MCStreamer.h:267
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:132
unsigned getNumFrameInfos()
Definition: MCStreamer.cpp:114
size_t getCurFragSize() const
Definition: MCStreamer.h:441
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:160
MCSymbol * endSection(MCSection *Section)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
virtual void emitCVDefRangeDirective(ArrayRef< std::pair< const MCSymbol *, const MCSymbol * > > Ranges, StringRef FixedSizePortion)
This implements the CodeView '.cv_def_range' assembler directive.
Definition: MCStreamer.cpp:349
MCSection * getCurrentSectionOnly() const
Definition: MCStreamer.h:421
virtual void emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
Emit the expression Value into the output as a native integer of the given Size bytes.
void addFragment(MCFragment *F)
unsigned emitSLEB128IntValue(int64_t Value)
Special case of EmitSLEB128Value that avoids the client having to pass in a MCExpr for constant integ...
Definition: MCStreamer.cpp:170
virtual void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum, const MCSymbol *FnStartSym, const MCSymbol *FnEndSym)
This implements the CodeView '.cv_inline_linetable' assembler directive.
Definition: MCStreamer.cpp:331
void visitUsedExpr(const MCExpr &Expr)
Generic base class for all target subtargets.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition: MCExpr.h:214
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:303
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
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
LLVM_ABI void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Definition: SourceMgr.cpp:352
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:154
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
LLVM Value Representation.
Definition: Value.h:75
This class represents a function that is read from a sample profile.
Definition: FunctionId.h:36
#define INT64_MAX
Definition: DataTypes.h:71
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1764
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:252
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition: MathExtras.h:498
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1854
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:1886
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:257
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition: Error.cpp:180
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
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
MCSymbol * Begin
Definition: MCDwarf.h:772