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