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