1 //===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/MC/MCObjectStreamer.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCCodeView.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDwarf.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/MC/MCSection.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/TargetRegistry.h"
26 using namespace llvm;
27 
28 MCObjectStreamer::MCObjectStreamer(MCContext &Context,
29                                    std::unique_ptr<MCAsmBackend> TAB,
30                                    raw_pwrite_stream &OS,
31                                    std::unique_ptr<MCCodeEmitter> Emitter)
32     : MCStreamer(Context), ObjectWriter(TAB->createObjectWriter(OS)),
33       TAB(std::move(TAB)), Emitter(std::move(Emitter)),
34       Assembler(llvm::make_unique<MCAssembler>(Context, *this->TAB,
35                                                *this->Emitter, *ObjectWriter)),
36       EmitEHFrame(true), EmitDebugFrame(false) {}
37 
38 MCObjectStreamer::~MCObjectStreamer() {}
39 
40 void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) {
41   if (PendingLabels.empty())
42     return;
43   if (!F) {
44     F = new MCDataFragment();
45     MCSection *CurSection = getCurrentSectionOnly();
46     CurSection->getFragmentList().insert(CurInsertionPoint, F);
47     F->setParent(CurSection);
48   }
49   for (MCSymbol *Sym : PendingLabels) {
50     Sym->setFragment(F);
51     Sym->setOffset(FOffset);
52   }
53   PendingLabels.clear();
54 }
55 
56 void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi,
57                                               const MCSymbol *Lo,
58                                               unsigned Size) {
59   // If not assigned to the same (valid) fragment, fallback.
60   if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() ||
61       Hi->isVariable() || Lo->isVariable()) {
62     MCStreamer::emitAbsoluteSymbolDiff(Hi, Lo, Size);
63     return;
64   }
65 
66   EmitIntValue(Hi->getOffset() - Lo->getOffset(), Size);
67 }
68 
69 void MCObjectStreamer::reset() {
70   if (Assembler)
71     Assembler->reset();
72   CurInsertionPoint = MCSection::iterator();
73   EmitEHFrame = true;
74   EmitDebugFrame = false;
75   PendingLabels.clear();
76   MCStreamer::reset();
77 }
78 
79 void MCObjectStreamer::EmitFrames(MCAsmBackend *MAB) {
80   if (!getNumFrameInfos())
81     return;
82 
83   if (EmitEHFrame)
84     MCDwarfFrameEmitter::Emit(*this, MAB, true);
85 
86   if (EmitDebugFrame)
87     MCDwarfFrameEmitter::Emit(*this, MAB, false);
88 }
89 
90 MCFragment *MCObjectStreamer::getCurrentFragment() const {
91   assert(getCurrentSectionOnly() && "No current section!");
92 
93   if (CurInsertionPoint != getCurrentSectionOnly()->getFragmentList().begin())
94     return &*std::prev(CurInsertionPoint);
95 
96   return nullptr;
97 }
98 
99 MCDataFragment *MCObjectStreamer::getOrCreateDataFragment() {
100   MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
101   // When bundling is enabled, we don't want to add data to a fragment that
102   // already has instructions (see MCELFStreamer::EmitInstToData for details)
103   if (!F || (Assembler->isBundlingEnabled() && !Assembler->getRelaxAll() &&
104              F->hasInstructions())) {
105     F = new MCDataFragment();
106     insert(F);
107   }
108   return F;
109 }
110 
111 MCPaddingFragment *MCObjectStreamer::getOrCreatePaddingFragment() {
112   MCPaddingFragment *F =
113       dyn_cast_or_null<MCPaddingFragment>(getCurrentFragment());
114   if (!F) {
115     F = new MCPaddingFragment();
116     insert(F);
117   }
118   return F;
119 }
120 
121 void MCObjectStreamer::visitUsedSymbol(const MCSymbol &Sym) {
122   Assembler->registerSymbol(Sym);
123 }
124 
125 void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) {
126   MCStreamer::EmitCFISections(EH, Debug);
127   EmitEHFrame = EH;
128   EmitDebugFrame = Debug;
129 }
130 
131 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
132                                      SMLoc Loc) {
133   MCStreamer::EmitValueImpl(Value, Size, Loc);
134   MCDataFragment *DF = getOrCreateDataFragment();
135   flushPendingLabels(DF, DF->getContents().size());
136 
137   MCCVLineEntry::Make(this);
138   MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
139 
140   // Avoid fixups when possible.
141   int64_t AbsValue;
142   if (Value->evaluateAsAbsolute(AbsValue, getAssembler())) {
143     if (!isUIntN(8 * Size, AbsValue) && !isIntN(8 * Size, AbsValue)) {
144       getContext().reportError(
145           Loc, "value evaluated as " + Twine(AbsValue) + " is out of range.");
146       return;
147     }
148     EmitIntValue(AbsValue, Size);
149     return;
150   }
151   DF->getFixups().push_back(
152       MCFixup::create(DF->getContents().size(), Value,
153                       MCFixup::getKindForSize(Size, false), Loc));
154   DF->getContents().resize(DF->getContents().size() + Size, 0);
155 }
156 
157 MCSymbol *MCObjectStreamer::EmitCFILabel() {
158   MCSymbol *Label = getContext().createTempSymbol("cfi", true);
159   EmitLabel(Label);
160   return Label;
161 }
162 
163 void MCObjectStreamer::EmitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
164   // We need to create a local symbol to avoid relocations.
165   Frame.Begin = getContext().createTempSymbol();
166   EmitLabel(Frame.Begin);
167 }
168 
169 void MCObjectStreamer::EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
170   Frame.End = getContext().createTempSymbol();
171   EmitLabel(Frame.End);
172 }
173 
174 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
175   MCStreamer::EmitLabel(Symbol, Loc);
176 
177   getAssembler().registerSymbol(*Symbol);
178 
179   // If there is a current fragment, mark the symbol as pointing into it.
180   // Otherwise queue the label and set its fragment pointer when we emit the
181   // next fragment.
182   auto *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
183   if (F && !(getAssembler().isBundlingEnabled() &&
184              getAssembler().getRelaxAll())) {
185     Symbol->setFragment(F);
186     Symbol->setOffset(F->getContents().size());
187   } else {
188     PendingLabels.push_back(Symbol);
189   }
190 }
191 
192 void MCObjectStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc, MCFragment *F) {
193   MCStreamer::EmitLabel(Symbol, Loc);
194   getAssembler().registerSymbol(*Symbol);
195   auto *DF = dyn_cast_or_null<MCDataFragment>(F);
196   if (DF)
197     Symbol->setFragment(F);
198   else
199     PendingLabels.push_back(Symbol);
200 }
201 
202 void MCObjectStreamer::EmitULEB128Value(const MCExpr *Value) {
203   int64_t IntValue;
204   if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
205     EmitULEB128IntValue(IntValue);
206     return;
207   }
208   insert(new MCLEBFragment(*Value, false));
209 }
210 
211 void MCObjectStreamer::EmitSLEB128Value(const MCExpr *Value) {
212   int64_t IntValue;
213   if (Value->evaluateAsAbsolute(IntValue, getAssembler())) {
214     EmitSLEB128IntValue(IntValue);
215     return;
216   }
217   insert(new MCLEBFragment(*Value, true));
218 }
219 
220 void MCObjectStreamer::EmitWeakReference(MCSymbol *Alias,
221                                          const MCSymbol *Symbol) {
222   report_fatal_error("This file format doesn't support weak aliases.");
223 }
224 
225 void MCObjectStreamer::ChangeSection(MCSection *Section,
226                                      const MCExpr *Subsection) {
227   changeSectionImpl(Section, Subsection);
228 }
229 
230 bool MCObjectStreamer::changeSectionImpl(MCSection *Section,
231                                          const MCExpr *Subsection) {
232   assert(Section && "Cannot switch to a null section!");
233   flushPendingLabels(nullptr);
234   getContext().clearDwarfLocSeen();
235 
236   bool Created = getAssembler().registerSection(*Section);
237 
238   int64_t IntSubsection = 0;
239   if (Subsection &&
240       !Subsection->evaluateAsAbsolute(IntSubsection, getAssembler()))
241     report_fatal_error("Cannot evaluate subsection number");
242   if (IntSubsection < 0 || IntSubsection > 8192)
243     report_fatal_error("Subsection number out of range");
244   CurInsertionPoint =
245       Section->getSubsectionInsertionPoint(unsigned(IntSubsection));
246   return Created;
247 }
248 
249 void MCObjectStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
250   getAssembler().registerSymbol(*Symbol);
251   MCStreamer::EmitAssignment(Symbol, Value);
252 }
253 
254 bool MCObjectStreamer::mayHaveInstructions(MCSection &Sec) const {
255   return Sec.hasInstructions();
256 }
257 
258 void MCObjectStreamer::EmitInstruction(const MCInst &Inst,
259                                        const MCSubtargetInfo &STI, bool) {
260   getAssembler().getBackend().handleCodePaddingInstructionBegin(Inst);
261   EmitInstructionImpl(Inst, STI);
262   getAssembler().getBackend().handleCodePaddingInstructionEnd(Inst);
263 }
264 
265 void MCObjectStreamer::EmitInstructionImpl(const MCInst &Inst,
266                                            const MCSubtargetInfo &STI) {
267   MCStreamer::EmitInstruction(Inst, STI);
268 
269   MCSection *Sec = getCurrentSectionOnly();
270   Sec->setHasInstructions(true);
271 
272   // Now that a machine instruction has been assembled into this section, make
273   // a line entry for any .loc directive that has been seen.
274   MCCVLineEntry::Make(this);
275   MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
276 
277   // If this instruction doesn't need relaxation, just emit it as data.
278   MCAssembler &Assembler = getAssembler();
279   if (!Assembler.getBackend().mayNeedRelaxation(Inst)) {
280     EmitInstToData(Inst, STI);
281     return;
282   }
283 
284   // Otherwise, relax and emit it as data if either:
285   // - The RelaxAll flag was passed
286   // - Bundling is enabled and this instruction is inside a bundle-locked
287   //   group. We want to emit all such instructions into the same data
288   //   fragment.
289   if (Assembler.getRelaxAll() ||
290       (Assembler.isBundlingEnabled() && Sec->isBundleLocked())) {
291     MCInst Relaxed;
292     getAssembler().getBackend().relaxInstruction(Inst, STI, Relaxed);
293     while (getAssembler().getBackend().mayNeedRelaxation(Relaxed))
294       getAssembler().getBackend().relaxInstruction(Relaxed, STI, Relaxed);
295     EmitInstToData(Relaxed, STI);
296     return;
297   }
298 
299   // Otherwise emit to a separate fragment.
300   EmitInstToFragment(Inst, STI);
301 }
302 
303 void MCObjectStreamer::EmitInstToFragment(const MCInst &Inst,
304                                           const MCSubtargetInfo &STI) {
305   if (getAssembler().getRelaxAll() && getAssembler().isBundlingEnabled())
306     llvm_unreachable("All instructions should have already been relaxed");
307 
308   // Always create a new, separate fragment here, because its size can change
309   // during relaxation.
310   MCRelaxableFragment *IF = new MCRelaxableFragment(Inst, STI);
311   insert(IF);
312 
313   SmallString<128> Code;
314   raw_svector_ostream VecOS(Code);
315   getAssembler().getEmitter().encodeInstruction(Inst, VecOS, IF->getFixups(),
316                                                 STI);
317   IF->getContents().append(Code.begin(), Code.end());
318 }
319 
320 #ifndef NDEBUG
321 static const char *const BundlingNotImplementedMsg =
322   "Aligned bundling is not implemented for this object format";
323 #endif
324 
325 void MCObjectStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
326   llvm_unreachable(BundlingNotImplementedMsg);
327 }
328 
329 void MCObjectStreamer::EmitBundleLock(bool AlignToEnd) {
330   llvm_unreachable(BundlingNotImplementedMsg);
331 }
332 
333 void MCObjectStreamer::EmitBundleUnlock() {
334   llvm_unreachable(BundlingNotImplementedMsg);
335 }
336 
337 void MCObjectStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line,
338                                              unsigned Column, unsigned Flags,
339                                              unsigned Isa,
340                                              unsigned Discriminator,
341                                              StringRef FileName) {
342   // In case we see two .loc directives in a row, make sure the
343   // first one gets a line entry.
344   MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
345 
346   this->MCStreamer::EmitDwarfLocDirective(FileNo, Line, Column, Flags,
347                                           Isa, Discriminator, FileName);
348 }
349 
350 static const MCExpr *buildSymbolDiff(MCObjectStreamer &OS, const MCSymbol *A,
351                                      const MCSymbol *B) {
352   MCContext &Context = OS.getContext();
353   MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
354   const MCExpr *ARef = MCSymbolRefExpr::create(A, Variant, Context);
355   const MCExpr *BRef = MCSymbolRefExpr::create(B, Variant, Context);
356   const MCExpr *AddrDelta =
357       MCBinaryExpr::create(MCBinaryExpr::Sub, ARef, BRef, Context);
358   return AddrDelta;
359 }
360 
361 static void emitDwarfSetLineAddr(MCObjectStreamer &OS,
362                                  MCDwarfLineTableParams Params,
363                                  int64_t LineDelta, const MCSymbol *Label,
364                                  int PointerSize) {
365   // emit the sequence to set the address
366   OS.EmitIntValue(dwarf::DW_LNS_extended_op, 1);
367   OS.EmitULEB128IntValue(PointerSize + 1);
368   OS.EmitIntValue(dwarf::DW_LNE_set_address, 1);
369   OS.EmitSymbolValue(Label, PointerSize);
370 
371   // emit the sequence for the LineDelta (from 1) and a zero address delta.
372   MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0);
373 }
374 
375 void MCObjectStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
376                                                 const MCSymbol *LastLabel,
377                                                 const MCSymbol *Label,
378                                                 unsigned PointerSize) {
379   if (!LastLabel) {
380     emitDwarfSetLineAddr(*this, Assembler->getDWARFLinetableParams(), LineDelta,
381                          Label, PointerSize);
382     return;
383   }
384   const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
385   int64_t Res;
386   if (AddrDelta->evaluateAsAbsolute(Res, getAssembler())) {
387     MCDwarfLineAddr::Emit(this, Assembler->getDWARFLinetableParams(), LineDelta,
388                           Res);
389     return;
390   }
391   insert(new MCDwarfLineAddrFragment(LineDelta, *AddrDelta));
392 }
393 
394 void MCObjectStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol *LastLabel,
395                                                  const MCSymbol *Label) {
396   const MCExpr *AddrDelta = buildSymbolDiff(*this, Label, LastLabel);
397   int64_t Res;
398   if (AddrDelta->evaluateAsAbsolute(Res, getAssembler())) {
399     MCDwarfFrameEmitter::EmitAdvanceLoc(*this, Res);
400     return;
401   }
402   insert(new MCDwarfCallFrameFragment(*AddrDelta));
403 }
404 
405 void MCObjectStreamer::EmitCVLocDirective(unsigned FunctionId, unsigned FileNo,
406                                           unsigned Line, unsigned Column,
407                                           bool PrologueEnd, bool IsStmt,
408                                           StringRef FileName, SMLoc Loc) {
409   // In case we see two .cv_loc directives in a row, make sure the
410   // first one gets a line entry.
411   MCCVLineEntry::Make(this);
412 
413   this->MCStreamer::EmitCVLocDirective(FunctionId, FileNo, Line, Column,
414                                        PrologueEnd, IsStmt, FileName, Loc);
415 }
416 
417 void MCObjectStreamer::EmitCVLinetableDirective(unsigned FunctionId,
418                                                 const MCSymbol *Begin,
419                                                 const MCSymbol *End) {
420   getContext().getCVContext().emitLineTableForFunction(*this, FunctionId, Begin,
421                                                        End);
422   this->MCStreamer::EmitCVLinetableDirective(FunctionId, Begin, End);
423 }
424 
425 void MCObjectStreamer::EmitCVInlineLinetableDirective(
426     unsigned PrimaryFunctionId, unsigned SourceFileId, unsigned SourceLineNum,
427     const MCSymbol *FnStartSym, const MCSymbol *FnEndSym) {
428   getContext().getCVContext().emitInlineLineTableForFunction(
429       *this, PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym,
430       FnEndSym);
431   this->MCStreamer::EmitCVInlineLinetableDirective(
432       PrimaryFunctionId, SourceFileId, SourceLineNum, FnStartSym, FnEndSym);
433 }
434 
435 void MCObjectStreamer::EmitCVDefRangeDirective(
436     ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
437     StringRef FixedSizePortion) {
438   getContext().getCVContext().emitDefRange(*this, Ranges, FixedSizePortion);
439   this->MCStreamer::EmitCVDefRangeDirective(Ranges, FixedSizePortion);
440 }
441 
442 void MCObjectStreamer::EmitCVStringTableDirective() {
443   getContext().getCVContext().emitStringTable(*this);
444 }
445 void MCObjectStreamer::EmitCVFileChecksumsDirective() {
446   getContext().getCVContext().emitFileChecksums(*this);
447 }
448 
449 void MCObjectStreamer::EmitCVFileChecksumOffsetDirective(unsigned FileNo) {
450   getContext().getCVContext().emitFileChecksumOffset(*this, FileNo);
451 }
452 
453 void MCObjectStreamer::EmitBytes(StringRef Data) {
454   MCCVLineEntry::Make(this);
455   MCDwarfLineEntry::Make(this, getCurrentSectionOnly());
456   MCDataFragment *DF = getOrCreateDataFragment();
457   flushPendingLabels(DF, DF->getContents().size());
458   DF->getContents().append(Data.begin(), Data.end());
459 }
460 
461 void MCObjectStreamer::EmitValueToAlignment(unsigned ByteAlignment,
462                                             int64_t Value,
463                                             unsigned ValueSize,
464                                             unsigned MaxBytesToEmit) {
465   if (MaxBytesToEmit == 0)
466     MaxBytesToEmit = ByteAlignment;
467   insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
468 
469   // Update the maximum alignment on the current section if necessary.
470   MCSection *CurSec = getCurrentSectionOnly();
471   if (ByteAlignment > CurSec->getAlignment())
472     CurSec->setAlignment(ByteAlignment);
473 }
474 
475 void MCObjectStreamer::EmitCodeAlignment(unsigned ByteAlignment,
476                                          unsigned MaxBytesToEmit) {
477   EmitValueToAlignment(ByteAlignment, 0, 1, MaxBytesToEmit);
478   cast<MCAlignFragment>(getCurrentFragment())->setEmitNops(true);
479 }
480 
481 void MCObjectStreamer::emitValueToOffset(const MCExpr *Offset,
482                                          unsigned char Value,
483                                          SMLoc Loc) {
484   insert(new MCOrgFragment(*Offset, Value, Loc));
485 }
486 
487 void MCObjectStreamer::EmitCodePaddingBasicBlockStart(
488     const MCCodePaddingContext &Context) {
489   getAssembler().getBackend().handleCodePaddingBasicBlockStart(this, Context);
490 }
491 
492 void MCObjectStreamer::EmitCodePaddingBasicBlockEnd(
493     const MCCodePaddingContext &Context) {
494   getAssembler().getBackend().handleCodePaddingBasicBlockEnd(Context);
495 }
496 
497 // Associate DTPRel32 fixup with data and resize data area
498 void MCObjectStreamer::EmitDTPRel32Value(const MCExpr *Value) {
499   MCDataFragment *DF = getOrCreateDataFragment();
500   flushPendingLabels(DF, DF->getContents().size());
501 
502   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
503                                             Value, FK_DTPRel_4));
504   DF->getContents().resize(DF->getContents().size() + 4, 0);
505 }
506 
507 // Associate DTPRel64 fixup with data and resize data area
508 void MCObjectStreamer::EmitDTPRel64Value(const MCExpr *Value) {
509   MCDataFragment *DF = getOrCreateDataFragment();
510   flushPendingLabels(DF, DF->getContents().size());
511 
512   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
513                                             Value, FK_DTPRel_8));
514   DF->getContents().resize(DF->getContents().size() + 8, 0);
515 }
516 
517 // Associate TPRel32 fixup with data and resize data area
518 void MCObjectStreamer::EmitTPRel32Value(const MCExpr *Value) {
519   MCDataFragment *DF = getOrCreateDataFragment();
520   flushPendingLabels(DF, DF->getContents().size());
521 
522   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
523                                             Value, FK_TPRel_4));
524   DF->getContents().resize(DF->getContents().size() + 4, 0);
525 }
526 
527 // Associate TPRel64 fixup with data and resize data area
528 void MCObjectStreamer::EmitTPRel64Value(const MCExpr *Value) {
529   MCDataFragment *DF = getOrCreateDataFragment();
530   flushPendingLabels(DF, DF->getContents().size());
531 
532   DF->getFixups().push_back(MCFixup::create(DF->getContents().size(),
533                                             Value, FK_TPRel_8));
534   DF->getContents().resize(DF->getContents().size() + 8, 0);
535 }
536 
537 // Associate GPRel32 fixup with data and resize data area
538 void MCObjectStreamer::EmitGPRel32Value(const MCExpr *Value) {
539   MCDataFragment *DF = getOrCreateDataFragment();
540   flushPendingLabels(DF, DF->getContents().size());
541 
542   DF->getFixups().push_back(
543       MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
544   DF->getContents().resize(DF->getContents().size() + 4, 0);
545 }
546 
547 // Associate GPRel64 fixup with data and resize data area
548 void MCObjectStreamer::EmitGPRel64Value(const MCExpr *Value) {
549   MCDataFragment *DF = getOrCreateDataFragment();
550   flushPendingLabels(DF, DF->getContents().size());
551 
552   DF->getFixups().push_back(
553       MCFixup::create(DF->getContents().size(), Value, FK_GPRel_4));
554   DF->getContents().resize(DF->getContents().size() + 8, 0);
555 }
556 
557 bool MCObjectStreamer::EmitRelocDirective(const MCExpr &Offset, StringRef Name,
558                                           const MCExpr *Expr, SMLoc Loc) {
559   int64_t OffsetValue;
560   if (!Offset.evaluateAsAbsolute(OffsetValue))
561     llvm_unreachable("Offset is not absolute");
562 
563   if (OffsetValue < 0)
564     llvm_unreachable("Offset is negative");
565 
566   MCDataFragment *DF = getOrCreateDataFragment();
567   flushPendingLabels(DF, DF->getContents().size());
568 
569   Optional<MCFixupKind> MaybeKind = Assembler->getBackend().getFixupKind(Name);
570   if (!MaybeKind.hasValue())
571     return true;
572 
573   MCFixupKind Kind = *MaybeKind;
574 
575   if (Expr == nullptr)
576     Expr =
577         MCSymbolRefExpr::create(getContext().createTempSymbol(), getContext());
578   DF->getFixups().push_back(MCFixup::create(OffsetValue, Expr, Kind, Loc));
579   return false;
580 }
581 
582 void MCObjectStreamer::emitFill(uint64_t NumBytes, uint8_t FillValue) {
583   assert(getCurrentSectionOnly() && "need a section");
584   insert(new MCFillFragment(FillValue, NumBytes));
585 }
586 
587 void MCObjectStreamer::emitFill(const MCExpr &NumBytes, uint64_t FillValue,
588                                 SMLoc Loc) {
589   MCDataFragment *DF = getOrCreateDataFragment();
590   flushPendingLabels(DF, DF->getContents().size());
591 
592   int64_t IntNumBytes;
593   if (!NumBytes.evaluateAsAbsolute(IntNumBytes, getAssembler())) {
594     getContext().reportError(Loc, "expected absolute expression");
595     return;
596   }
597 
598   if (IntNumBytes <= 0) {
599     getContext().reportError(Loc, "invalid number of bytes");
600     return;
601   }
602 
603   emitFill(IntNumBytes, FillValue);
604 }
605 
606 void MCObjectStreamer::emitFill(const MCExpr &NumValues, int64_t Size,
607                                 int64_t Expr, SMLoc Loc) {
608   int64_t IntNumValues;
609   if (!NumValues.evaluateAsAbsolute(IntNumValues, getAssembler())) {
610     getContext().reportError(Loc, "expected absolute expression");
611     return;
612   }
613 
614   if (IntNumValues < 0) {
615     getContext().getSourceManager()->PrintMessage(
616         Loc, SourceMgr::DK_Warning,
617         "'.fill' directive with negative repeat count has no effect");
618     return;
619   }
620 
621   MCStreamer::emitFill(IntNumValues, Size, Expr);
622 }
623 
624 void MCObjectStreamer::EmitFileDirective(StringRef Filename) {
625   getAssembler().addFileName(Filename);
626 }
627 
628 void MCObjectStreamer::FinishImpl() {
629   // If we are generating dwarf for assembly source files dump out the sections.
630   if (getContext().getGenDwarfForAssembly())
631     MCGenDwarfInfo::Emit(this);
632 
633   // Dump out the dwarf file & directory tables and line tables.
634   MCDwarfLineTable::Emit(this, getAssembler().getDWARFLinetableParams());
635 
636   flushPendingLabels(nullptr);
637   getAssembler().Finish();
638 }
639