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