xref: /llvm-project/llvm/lib/MC/MCObjectStreamer.cpp (revision aa3589f0ccc0848fa8d5847afdbc8dd9b9c624e7)
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 
9 #include "llvm/MC/MCObjectStreamer.h"
10 #include "llvm/MC/MCAsmBackend.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCCodeEmitter.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"
18 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/MC/MCValue.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/SourceMgr.h"
25 using namespace llvm;
26 
27 MCObjectStreamer::MCObjectStreamer(MCContext &Context,
28                                    std::unique_ptr<MCAsmBackend> TAB,
29                                    std::unique_ptr<MCObjectWriter> OW,
30                                    std::unique_ptr<MCCodeEmitter> Emitter)
31     : MCStreamer(Context),
32       Assembler(std::make_unique<MCAssembler>(
33           Context, std::move(TAB), std::move(Emitter), std::move(OW))),
34       EmitEHFrame(true), EmitDebugFrame(false) {
35   if (Assembler->getBackendPtr())
36     setAllowAutoPadding(Assembler->getBackend().allowAutoPadding());
37   if (Context.getTargetOptions() && Context.getTargetOptions()->MCRelaxAll)
38     Assembler->setRelaxAll(true);
39 }
40 
41 MCObjectStreamer::~MCObjectStreamer() = default;
42 
43 MCAssembler *MCObjectStreamer::getAssemblerPtr() {
44   if (getUseAssemblerInfoForParsing())
45     return Assembler.get();
46   return nullptr;
47 }
48 
49 // When fixup's offset is a forward declared label, e.g.:
50 //
51 //   .reloc 1f, R_MIPS_JALR, foo
52 // 1: nop
53 //
54 // postpone adding it to Fixups vector until the label is defined and its offset
55 // is known.
56 void MCObjectStreamer::resolvePendingFixups() {
57   for (PendingMCFixup &PendingFixup : PendingFixups) {
58     if (!PendingFixup.Sym || PendingFixup.Sym->isUndefined ()) {
59       getContext().reportError(PendingFixup.Fixup.getLoc(),
60                                "unresolved relocation offset");
61       continue;
62     }
63     PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset() +
64                                  PendingFixup.Fixup.getOffset());
65 
66     // If the location symbol to relocate is in MCEncodedFragmentWithFixups,
67     // put the Fixup into location symbol's fragment. Otherwise
68     // put into PendingFixup.DF
69     MCFragment *SymFragment = PendingFixup.Sym->getFragment();
70     switch (SymFragment->getKind()) {
71     case MCFragment::FT_Relaxable:
72     case MCFragment::FT_Dwarf:
73     case MCFragment::FT_PseudoProbe:
74       cast<MCEncodedFragmentWithFixups<8, 1>>(SymFragment)
75           ->getFixups()
76           .push_back(PendingFixup.Fixup);
77       break;
78     case MCFragment::FT_Data:
79     case MCFragment::FT_CVDefRange:
80       cast<MCEncodedFragmentWithFixups<32, 4>>(SymFragment)
81           ->getFixups()
82           .push_back(PendingFixup.Fixup);
83       break;
84     default:
85       PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup);
86       break;
87     }
88   }
89   PendingFixups.clear();
90 }
91 
92 // As a compile-time optimization, avoid allocating and evaluating an MCExpr
93 // tree for (Hi - Lo) when Hi and Lo are offsets into the same fragment.
94 static std::optional<uint64_t> absoluteSymbolDiff(const MCSymbol *Hi,
95                                                   const MCSymbol *Lo) {
96   assert(Hi && Lo);
97   if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() ||
98       Hi->isVariable() || Lo->isVariable())
99     return std::nullopt;
100 
101   return Hi->getOffset() - Lo->getOffset();
102 }
103 
104 void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi,
105                                               const MCSymbol *Lo,
106                                               unsigned Size) {
107   if (!getAssembler().getContext().getTargetTriple().isRISCV())
108     if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo))
109       return emitIntValue(*Diff, Size);
110   MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size);
111 }
112 
113 void MCObjectStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
114                                                        const MCSymbol *Lo) {
115   if (!getAssembler().getContext().getTargetTriple().isRISCV())
116     if (std::optional<uint64_t> Diff = absoluteSymbolDiff(Hi, Lo)) {
117       emitULEB128IntValue(*Diff);
118       return;
119     }
120   MCStreamer::emitAbsoluteSymbolDiffAsULEB128(Hi, Lo);
121 }
122 
123 void MCObjectStreamer::reset() {
124   if (Assembler) {
125     Assembler->reset();
126     if (getContext().getTargetOptions())
127       Assembler->setRelaxAll(getContext().getTargetOptions()->MCRelaxAll);
128   }
129   EmitEHFrame = true;
130   EmitDebugFrame = false;
131   MCStreamer::reset();
132 }
133 
134 void MCObjectStreamer::emitFrames(MCAsmBackend *MAB) {
135   if (!getNumFrameInfos())
136     return;
137 
138   if (EmitEHFrame)
139     MCDwarfFrameEmitter::Emit(*this, MAB, true);
140 
141   if (EmitDebugFrame)
142     MCDwarfFrameEmitter::Emit(*this, MAB, false);
143 }
144 
145 MCFragment *MCObjectStreamer::getCurrentFragment() const {
146   return getCurrentSectionOnly()->curFragList()->Tail;
147 }
148 
149 static bool canReuseDataFragment(const MCDataFragment &F,
150                                  const MCAssembler &Assembler,
151                                  const MCSubtargetInfo *STI) {
152   if (!F.hasInstructions())
153     return true;
154   // Do not add data after a linker-relaxable instruction. The difference
155   // between a new label and a label at or before the linker-relaxable
156   // instruction cannot be resolved at assemble-time.
157   if (F.isLinkerRelaxable())
158     return false;
159   // When bundling is enabled, we don't want to add data to a fragment that
160   // already has instructions (see MCELFStreamer::emitInstToData for details)
161   if (Assembler.isBundlingEnabled())
162     return false;
163   // If the subtarget is changed mid fragment we start a new fragment to record
164   // the new STI.
165   return !STI || F.getSubtargetInfo() == STI;
166 }
167 
168 MCDataFragment *
169 MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) {
170   MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
171   if (!F || !canReuseDataFragment(*F, *Assembler, STI)) {
172     F = getContext().allocFragment<MCDataFragment>();
173     insert(F);
174   }
175   return F;
176 }
177 
178 void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) {
179   Assembler->registerSymbol(Sym);
180 }
181 
182 void MCObjectStreamer::emitCFISections(bool EH, bool Debug) {
183   MCStreamer::emitCFISections(EH, Debug);
184   EmitEHFrame = EH;
185   EmitDebugFrame = Debug;
186 }
187 
188 void MCObjectStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
189                                      SMLoc Loc) {
190   MCStreamer::emitValueImpl(Value, Size, Loc);
191   MCDataFragment *DF = getOrCreateDataFragment();
192 
193   MCDwarfLineEntry::make(this, getCurrentSectionOnly());
194 
195   // Avoid fixups when possible.
196   int64_t AbsValue;
197   if (Value->evaluateAsAbsolute(AbsValue, getAssemblerPtr())) {
198     if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
199       getContext().reportError(
200           Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
201       return;
202     }
203     emitIntValue(AbsValue, Size);
204     return;
205   }
206   DF->getFixups().push_back(
207       MCFixup::create(DF->getContents().size(), Value,
208                       MCFixup::getKindForSize(Size, false), Loc));
209   DF->getContents().resize(DF->getContents().size() + Size, 0);
210 }
211 
212 MCSymbol *MCObjectStreamer::emitCFILabel() {
213   MCSymbol *Label = getContext().createTempSymbol("cfi");
214   emitLabel(Label);
215   return Label;
216 }
217 
218 void MCObjectStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
219   // We need to create a local symbol to avoid relocations.
220   Frame.Begin = getContext().createTempSymbol();
221   emitLabel(Frame.Begin);
222 }
223 
224 void MCObjectStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
225   Frame.End = getContext().createTempSymbol();
226   emitLabel(Frame.End);
227 }
228 
229 void MCObjectStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
230   MCStreamer::emitLabel(Symbol, Loc);
231 
232   getAssembler().registerSymbol(*Symbol);
233 
234   // If there is a current fragment, mark the symbol as pointing into it.
235   // Otherwise queue the label and set its fragment pointer when we emit the
236   // next fragment.
237   MCDataFragment *F = getOrCreateDataFragment();
238   Symbol->setFragment(F);
239   Symbol->setOffset(F->getContents().size());
240 
241   emitPendingAssignments(Symbol);
242 }
243 
244 void MCObjectStreamer::emitPendingAssignments(MCSymbol *Symbol) {
245   auto Assignments = pendingAssignments.find(Symbol);
246   if (Assignments != pendingAssignments.end()) {
247     for (const PendingAssignment &A : Assignments->second)
248       emitAssignment(A.Symbol, A.Value);
249 
250     pendingAssignments.erase(Assignments);
251   }
252 }
253 
254 // Emit a label at a previously emitted fragment/offset position. This must be
255 // within the currently-active section.
256 void MCObjectStreamer::emitLabelAtPos(MCSymbol *Symbol, SMLoc Loc,
257                                       MCDataFragment &F, uint64_t Offset) {
258   assert(F.getParent() == getCurrentSectionOnly());
259   MCStreamer::emitLabel(Symbol, Loc);
260   getAssembler().registerSymbol(*Symbol);
261   Symbol->setFragment(&F);
262   Symbol->setOffset(Offset);
263 }
264 
265 void MCObjectStreamer::emitULEB128Value(const MCExpr *Value) {
266   int64_t IntValue;
267   if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
268     emitULEB128IntValue(IntValue);
269     return;
270   }
271   insert(getContext().allocFragment<MCLEBFragment>(*Value, false));
272 }
273 
274 void MCObjectStreamer::emitSLEB128Value(const MCExpr *Value) {
275   int64_t IntValue;
276   if (Value->evaluateAsAbsolute(IntValue, getAssemblerPtr())) {
277     emitSLEB128IntValue(IntValue);
278     return;
279   }
280   insert(getContext().allocFragment<MCLEBFragment>(*Value, true));
281 }
282 
283 void MCObjectStreamer::emitWeakReference(MCSymbol *Alias,
284                                          const MCSymbol *Symbol) {
285   report_fatal_error("This file format doesn't support weak aliases.");
286 }
287 
288 void MCObjectStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
289   changeSectionImpl(Section, Subsection);
290 }
291 
292 bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
293                                          uint32_t Subsection) {
294   assert(Section && "Cannot switch to a null section!");
295   getContext().clearDwarfLocSeen();
296 
297   bool Created = getAssembler().registerSection(*Section);
298   Section->switchSubsection(Subsection);
299   return Created;
300 }
301 
302 void MCObjectStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
303   getAssembler().registerSymbol(*Symbol);
304   MCStreamer::emitAssignment(Symbol, Value);
305   emitPendingAssignments(Symbol);
306 }
307 
308 void MCObjectStreamer::emitConditionalAssignment(MCSymbol *Symbol,
309                                                  const MCExpr *Value) {
310   const MCSymbol *Target = &cast<MCSymbolRefExpr>(*Value).getSymbol();
311 
312   // If the symbol already exists, emit the assignment. Otherwise, emit it
313   // later only if the symbol is also emitted.
314   if (Target->isRegistered())
315     emitAssignment(Symbol, Value);
316   else
317     pendingAssignments[Target].push_back({Symbol, Value});
318 }
319 
320 bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const {
321   return Sec.hasInstructions();
322 }
323 
324 void MCObjectStreamer::emitInstruction(const MCInst &Inst,
325                                        const MCSubtargetInfo &STI) {
326   const MCSection &Sec = *getCurrentSectionOnly();
327   if (Sec.isVirtualSection()) {
328     getContext().reportError(Inst.getLoc(), Twine(Sec.getVirtualSectionKind()) +
329                                                 " section '" + Sec.getName() +
330                                                 "' cannot have instructions");
331     return;
332   }
333   emitInstructionImpl(Inst, STI);
334 }
335 
336 void MCObjectStreamer::emitInstructionImpl(const MCInst &Inst,
337                                            const MCSubtargetInfo &STI) {
338   MCStreamer::emitInstruction(Inst, STI);
339 
340   MCSection *Sec = getCurrentSectionOnly();
341   Sec->setHasInstructions(true);
342 
343   // Now that a machine instruction has been assembled into this section, make
344   // a line entry for any .loc directive that has been seen.
345   MCDwarfLineEntry::make(this, getCurrentSectionOnly());
346 
347   // If this instruction doesn't need relaxation, just emit it as data.
348   MCAssembler &Assembler = getAssembler();
349   MCAsmBackend &Backend = Assembler.getBackend();
350   if (!(Backend.mayNeedRelaxation(Inst, STI) ||
351         Backend.allowEnhancedRelaxation())) {
352     emitInstToData(Inst, STI);
353     return;
354   }
355 
356   // Otherwise, relax and emit it as data if either:
357   // - The RelaxAll flag was passed
358   // - Bundling is enabled and this instruction is inside a bundle-locked
359   //   group. We want to emit all such instructions into the same data
360   //   fragment.
361   if (Assembler.getRelaxAll() ||
362       (Assembler.isBundlingEnabled() && Sec->isBundleLocked())) {
363     MCInst Relaxed = Inst;
364     while (Backend.mayNeedRelaxation(Relaxed, STI))
365       Backend.relaxInstruction(Relaxed, STI);
366     emitInstToData(Relaxed, STI);
367     return;
368   }
369 
370   // Otherwise emit to a separate fragment.
371   emitInstToFragment(Inst, STI);
372 }
373 
374 void MCObjectStreamer::emitInstToFragment(const MCInst &Inst,
375                                           const MCSubtargetInfo &STI) {
376   // Always create a new, separate fragment here, because its size can change
377   // during relaxation.
378   MCRelaxableFragment *IF =
379       getContext().allocFragment<MCRelaxableFragment>(Inst, STI);
380   insert(IF);
381 
382   SmallString<128> Code;
383   getAssembler().getEmitter().encodeInstruction(Inst, Code, IF->getFixups(),
384                                                 STI);
385   IF->getContents().append(Code.begin(), Code.end());
386 }
387 
388 #ifndef NDEBUG
389 static const char *const BundlingNotImplementedMsg =
390   "Aligned bundling is not implemented for this object format";
391 #endif
392 
393 void MCObjectStreamer::emitBundleAlignMode(Align Alignment) {
394   llvm_unreachable(BundlingNotImplementedMsg);
395 }
396 
397 void MCObjectStreamer::emitBundleLock(bool AlignToEnd) {
398   llvm_unreachable(BundlingNotImplementedMsg);
399 }
400 
401 void MCObjectStreamer::emitBundleUnlock() {
402   llvm_unreachable(BundlingNotImplementedMsg);
403 }
404 
405 void MCObjectStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
406                                              unsigned Column, unsigned Flags,
407                                              unsigned Isa,
408                                              unsigned Discriminator,
409                                              StringRef FileName) {
410   // In case we see two .loc directives in a row, make sure the
411   // first one gets a line entry.
412   MCDwarfLineEntry::make(this, getCurrentSectionOnly());
413 
414   this->MCStreamer::emitDwarfLocDirective(FileNo, Line, Column, Flags, Isa,
415                                           Discriminator, FileName);
416 }
417 
418 static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A,
419                                      const MCSymbol *B, SMLoc Loc) {
420   MCContext &Context = OS.getContext();
421   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
422   const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context);
423   const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context);
424   const MCExpr *AddrDelta =
425       MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context, Loc);
426   return AddrDelta;
427 }
428 
429 static void emitDwarfSetLineAddr(MCObjectStreamer &OS,
430                                  MCDwarfLineTableParams Params,
431                                  int64_t LineDelta, const MCSymbol *Label,
432                                  int PointerSize) {
433   // emit the sequence to set the address
434   OS.emitIntValue(dwarf::DW_LNS_extended_op, 1);
435   OS.emitULEB128IntValue(PointerSize + 1);
436   OS.emitIntValue(dwarf::DW_LNE_set_address, 1);
437   OS.emitSymbolValue(Label, PointerSize);
438 
439   // emit the sequence for the LineDelta (from 1) and a zero address delta.
440   MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
441 }
442 
443 void MCObjectStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta,
444                                                 const MCSymbol *LastLabel,
445                                                 const MCSymbol *Label,
446                                                 unsigned PointerSize) {
447   if (!LastLabel) {
448     emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
449                          Label, PointerSize);
450     return;
451   }
452   const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel, SMLoc());
453   insert(getContext().allocFragment<MCDwarfLineAddrFragment>(LineDelta,
454                                                              *AddrDelta));
455 }
456 
457 void MCObjectStreamer::emitDwarfLineEndEntry(MCSection *Section,
458                                              MCSymbol *LastLabel) {
459   // Emit a DW_LNE_end_sequence for the end of the section.
460   // Use the section end label to compute the address delta and use INT64_MAX
461   // as the line delta which is the signal that this is actually a
462   // DW_LNE_end_sequence.
463   MCSymbol *SectionEnd = endSection(Section);
464 
465   // Switch back the dwarf line section, in case endSection had to switch the
466   // section.
467   MCContext &Ctx = getContext();
468   switchSection(Ctx.getObjectFileInfo()->getDwarfLineSection());
469 
470   const MCAsmInfo *AsmInfo = Ctx.getAsmInfo();
471   emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, SectionEnd,
472                            AsmInfo->getCodePointerSize());
473 }
474 
475 void MCObjectStreamer::emitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
476                                                  const MCSymbol *Label,
477                                                  SMLoc Loc) {
478   const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel, Loc);
479   insert(getContext().allocFragment<MCDwarfCallFrameFragment>(*AddrDelta));
480 }
481 
482 void MCObjectStreamer::emitCVLocDirective(unsigned FunctionId, unsigned FileNo,
483                                           unsigned Line, unsigned Column,
484                                           bool PrologueEnd, bool IsStmt,
485                                           StringRef FileName, SMLoc Loc) {
486   // Validate the directive.
487   if (!checkCVLocSection(FunctionId, FileNo, Loc))
488     return;
489 
490   // Emit a label at the current position and record it in the CodeViewContext.
491   MCSymbol *LineSym = getContext().createTempSymbol();
492   emitLabel(LineSym);
493   getContext().getCVContext().recordCVLoc(getContext(), LineSym, FunctionId,
494                                           FileNo, Line, Column, PrologueEnd,
495                                           IsStmt);
496 }
497 
498 void MCObjectStreamer::emitCVLinetableDirective(unsigned FunctionId,
499                                                 const MCSymbol *Begin,
500                                                 const MCSymbol *End) {
501   getContext().getCVContext().emitLineTableForFunction(*this, FunctionId, Begin,
502                                                        End);
503   this->MCStreamer::emitCVLinetableDirective(FunctionId, Begin, End);
504 }
505 
506 void MCObjectStreamer::emitCVInlineLinetableDirective(
507     unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
508     const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
509   getContext().getCVContext().emitInlineLineTableForFunction(
510       *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
511       FnEndSym);
512   this->MCStreamer::emitCVInlineLinetableDirective(
513       PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
514 }
515 
516 void MCObjectStreamer::emitCVDefRangeDirective(
517     ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
518     StringRef FixedSizePortion) {
519   getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
520   // Attach labels that were pending before we created the defrange fragment to
521   // the beginning of the new fragment.
522   this->MCStreamer::emitCVDefRangeDirective(Ranges, FixedSizePortion);
523 }
524 
525 void MCObjectStreamer::emitCVStringTableDirective() {
526   getContext().getCVContext().emitStringTable(*this);
527 }
528 void MCObjectStreamer::emitCVFileChecksumsDirective() {
529   getContext().getCVContext().emitFileChecksums(*this);
530 }
531 
532 void MCObjectStreamer::emitCVFileChecksumOffsetDirective(unsigned FileNo) {
533   getContext().getCVContext().emitFileChecksumOffset(*this, FileNo);
534 }
535 
536 void MCObjectStreamer::emitBytes(StringRef Data) {
537   MCDwarfLineEntry::make(this, getCurrentSectionOnly());
538   MCDataFragment *DF = getOrCreateDataFragment();
539   DF->getContents().append(Data.begin(), Data.end());
540 }
541 
542 void MCObjectStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
543                                             unsigned ValueSize,
544                                             unsigned MaxBytesToEmit) {
545   if (MaxBytesToEmit == 0)
546     MaxBytesToEmit = Alignment.value();
547   insert(getContext().allocFragment<MCAlignFragment>(
548       Alignment, Value, ValueSize, MaxBytesToEmit));
549 
550   // Update the maximum alignment on the current section if necessary.
551   MCSection *CurSec = getCurrentSectionOnly();
552   CurSec->ensureMinAlignment(Alignment);
553 }
554 
555 void MCObjectStreamer::emitCodeAlignment(Align Alignment,
556                                          const MCSubtargetInfo *STI,
557                                          unsigned MaxBytesToEmit) {
558   emitValueToAlignment(Alignment, 0, 1, MaxBytesToEmit);
559   cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true, STI);
560 }
561 
562 void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
563                                          unsigned char Value,
564                                          SMLoc Loc) {
565   insert(getContext().allocFragment<MCOrgFragment>(*Offset, Value, Loc));
566 }
567 
568 // Associate DTPRel32 fixup with data and resize data area
569 void MCObjectStreamer::emitDTPRel32Value(const MCExpr *Value) {
570   MCDataFragment *DF = getOrCreateDataFragment();
571   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
572                                             Value, FK_DTPRel_4));
573   DF->getContents().resize(DF->getContents().size() + 4, 0);
574 }
575 
576 // Associate DTPRel64 fixup with data and resize data area
577 void MCObjectStreamer::emitDTPRel64Value(const MCExpr *Value) {
578   MCDataFragment *DF = getOrCreateDataFragment();
579   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
580                                             Value, FK_DTPRel_8));
581   DF->getContents().resize(DF->getContents().size() + 8, 0);
582 }
583 
584 // Associate TPRel32 fixup with data and resize data area
585 void MCObjectStreamer::emitTPRel32Value(const MCExpr *Value) {
586   MCDataFragment *DF = getOrCreateDataFragment();
587   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
588                                             Value, FK_TPRel_4));
589   DF->getContents().resize(DF->getContents().size() + 4, 0);
590 }
591 
592 // Associate TPRel64 fixup with data and resize data area
593 void MCObjectStreamer::emitTPRel64Value(const MCExpr *Value) {
594   MCDataFragment *DF = getOrCreateDataFragment();
595   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
596                                             Value, FK_TPRel_8));
597   DF->getContents().resize(DF->getContents().size() + 8, 0);
598 }
599 
600 // Associate GPRel32 fixup with data and resize data area
601 void MCObjectStreamer::emitGPRel32Value(const MCExpr *Value) {
602   MCDataFragment *DF = getOrCreateDataFragment();
603   DF->getFixups().push_back(
604       MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
605   DF->getContents().resize(DF->getContents().size() + 4, 0);
606 }
607 
608 // Associate GPRel64 fixup with data and resize data area
609 void MCObjectStreamer::emitGPRel64Value(const MCExpr *Value) {
610   MCDataFragment *DF = getOrCreateDataFragment();
611   DF->getFixups().push_back(
612       MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
613   DF->getContents().resize(DF->getContents().size() + 8, 0);
614 }
615 
616 static std::optional<std::pair<bool, std::string>>
617 getOffsetAndDataFragment(const MCSymbol &Symbol, uint32_t &RelocOffset,
618                          MCDataFragment *&DF) {
619   if (Symbol.isVariable()) {
620     const MCExpr *SymbolExpr = Symbol.getVariableValue();
621     MCValue OffsetVal;
622     if(!SymbolExpr->evaluateAsRelocatable(OffsetVal, nullptr, nullptr))
623       return std::make_pair(false,
624                             std::string("symbol in .reloc offset is not "
625                                         "relocatable"));
626     if (OffsetVal.isAbsolute()) {
627       RelocOffset = OffsetVal.getConstant();
628       MCFragment *Fragment = Symbol.getFragment();
629       // FIXME Support symbols with no DF. For example:
630       // .reloc .data, ENUM_VALUE, <some expr>
631       if (!Fragment || Fragment->getKind() != MCFragment::FT_Data)
632         return std::make_pair(false,
633                               std::string("symbol in offset has no data "
634                                           "fragment"));
635       DF = cast<MCDataFragment>(Fragment);
636       return std::nullopt;
637     }
638 
639     if (OffsetVal.getSymB())
640       return std::make_pair(false,
641                             std::string(".reloc symbol offset is not "
642                                         "representable"));
643 
644     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*OffsetVal.getSymA());
645     if (!SRE.getSymbol().isDefined())
646       return std::make_pair(false,
647                             std::string("symbol used in the .reloc offset is "
648                                         "not defined"));
649 
650     if (SRE.getSymbol().isVariable())
651       return std::make_pair(false,
652                             std::string("symbol used in the .reloc offset is "
653                                         "variable"));
654 
655     MCFragment *Fragment = SRE.getSymbol().getFragment();
656     // FIXME Support symbols with no DF. For example:
657     // .reloc .data, ENUM_VALUE, <some expr>
658     if (!Fragment || Fragment->getKind() != MCFragment::FT_Data)
659       return std::make_pair(false,
660                             std::string("symbol in offset has no data "
661                                         "fragment"));
662     RelocOffset = SRE.getSymbol().getOffset() + OffsetVal.getConstant();
663     DF = cast<MCDataFragment>(Fragment);
664   } else {
665     RelocOffset = Symbol.getOffset();
666     MCFragment *Fragment = Symbol.getFragment();
667     // FIXME Support symbols with no DF. For example:
668     // .reloc .data, ENUM_VALUE, <some expr>
669     if (!Fragment || Fragment->getKind() != MCFragment::FT_Data)
670       return std::make_pair(false,
671                             std::string("symbol in offset has no data "
672                                         "fragment"));
673     DF = cast<MCDataFragment>(Fragment);
674   }
675   return std::nullopt;
676 }
677 
678 std::optional<std::pair<bool, std::string>>
679 MCObjectStreamer::emitRelocDirective(const MCExpr &Offset, StringRef Name,
680                                      const MCExpr *Expr, SMLoc Loc,
681                                      const MCSubtargetInfo &STI) {
682   std::optional<MCFixupKind> MaybeKind =
683       Assembler->getBackend().getFixupKind(Name);
684   if (!MaybeKind)
685     return std::make_pair(true, std::string("unknown relocation name"));
686 
687   MCFixupKind Kind = *MaybeKind;
688   if (Expr)
689     visitUsedExpr(*Expr);
690   else
691     Expr =
692         MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
693 
694   MCDataFragment *DF = getOrCreateDataFragment(&STI);
695   MCValue OffsetVal;
696   if (!Offset.evaluateAsRelocatable(OffsetVal, nullptr, nullptr))
697     return std::make_pair(false,
698                           std::string(".reloc offset is not relocatable"));
699   if (OffsetVal.isAbsolute()) {
700     if (OffsetVal.getConstant() < 0)
701       return std::make_pair(false, std::string(".reloc offset is negative"));
702     DF->getFixups().push_back(
703         MCFixup::create(OffsetVal.getConstant(), Expr, Kind, Loc));
704     return std::nullopt;
705   }
706   if (OffsetVal.getSymB())
707     return std::make_pair(false,
708                           std::string(".reloc offset is not representable"));
709 
710   const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*OffsetVal.getSymA());
711   const MCSymbol &Symbol = SRE.getSymbol();
712   if (Symbol.isDefined()) {
713     uint32_t SymbolOffset = 0;
714     std::optional<std::pair<bool, std::string>> Error =
715         getOffsetAndDataFragment(Symbol, SymbolOffset, DF);
716 
717     if (Error != std::nullopt)
718       return Error;
719 
720     DF->getFixups().push_back(
721         MCFixup::create(SymbolOffset + OffsetVal.getConstant(),
722                         Expr, Kind, Loc));
723     return std::nullopt;
724   }
725 
726   PendingFixups.emplace_back(
727       &SRE.getSymbol(), DF,
728       MCFixup::create(OffsetVal.getConstant(), Expr, Kind, Loc));
729   return std::nullopt;
730 }
731 
732 void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
733                                 SMLoc Loc) {
734   assert(getCurrentSectionOnly() && "need a section");
735   insert(
736       getContext().allocFragment<MCFillFragment>(FillValue, 1, NumBytes, Loc));
737 }
738 
739 void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
740                                 int64_t Expr, SMLoc Loc) {
741   int64_t IntNumValues;
742   // Do additional checking now if we can resolve the value.
743   if (NumValues.evaluateAsAbsolute(IntNumValues, getAssemblerPtr())) {
744     if (IntNumValues < 0) {
745       getContext().getSourceManager()->PrintMessage(
746           Loc, SourceMgr::DK_Warning,
747           "'.fill' directive with negative repeat count has no effect");
748       return;
749     }
750     // Emit now if we can for better errors.
751     int64_t NonZeroSize = Size > 4 ? 4 : Size;
752     Expr &= ~0ULL >> (64 - NonZeroSize * 8);
753     for (uint64_t i = 0, e = IntNumValues; i != e; ++i) {
754       emitIntValue(Expr, NonZeroSize);
755       if (NonZeroSize < Size)
756         emitIntValue(0, Size - NonZeroSize);
757     }
758     return;
759   }
760 
761   // Otherwise emit as fragment.
762   assert(getCurrentSectionOnly() && "need a section");
763   insert(
764       getContext().allocFragment<MCFillFragment>(Expr, Size, NumValues, Loc));
765 }
766 
767 void MCObjectStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLength,
768                                 SMLoc Loc, const MCSubtargetInfo &STI) {
769   assert(getCurrentSectionOnly() && "need a section");
770   insert(getContext().allocFragment<MCNopsFragment>(
771       NumBytes, ControlledNopLength, Loc, STI));
772 }
773 
774 void MCObjectStreamer::emitFileDirective(StringRef Filename) {
775   getAssembler().addFileName(Filename);
776 }
777 
778 void MCObjectStreamer::emitFileDirective(StringRef Filename,
779                                          StringRef CompilerVersion,
780                                          StringRef TimeStamp,
781                                          StringRef Description) {
782   getAssembler().addFileName(Filename);
783   getAssembler().setCompilerVersion(CompilerVersion.str());
784   // TODO: add TimeStamp and Description to .file symbol table entry
785   // with the integrated assembler.
786 }
787 
788 void MCObjectStreamer::emitAddrsig() {
789   getAssembler().getWriter().emitAddrsigSection();
790 }
791 
792 void MCObjectStreamer::emitAddrsigSym(const MCSymbol *Sym) {
793   getAssembler().getWriter().addAddrsigSymbol(Sym);
794 }
795 
796 void MCObjectStreamer::finishImpl() {
797   getContext().RemapDebugPaths();
798 
799   // If we are generating dwarf for assembly source files dump out the sections.
800   if (getContext().getGenDwarfForAssembly())
801     MCGenDwarfInfo::Emit(this);
802 
803   // Dump out the dwarf file & directory tables and line tables.
804   MCDwarfLineTable::emit(this, getAssembler().getDWARFLinetableParams());
805 
806   // Emit pseudo probes for the current module.
807   MCPseudoProbeTable::emit(this);
808 
809   resolvePendingFixups();
810   getAssembler().Finish();
811 }
812