]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/MC/MCAssembler.cpp
MFV r328249:
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / MC / MCAssembler.cpp
1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
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/MCAssembler.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCCodeView.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDwarf.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCFixup.h"
26 #include "llvm/MC/MCFixupKindInfo.h"
27 #include "llvm/MC/MCFragment.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCObjectWriter.h"
30 #include "llvm/MC/MCSection.h"
31 #include "llvm/MC/MCSectionELF.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCValue.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/LEB128.h"
38 #include "llvm/Support/MathExtras.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include <cassert>
41 #include <cstdint>
42 #include <cstring>
43 #include <tuple>
44 #include <utility>
45
46 using namespace llvm;
47
48 #define DEBUG_TYPE "assembler"
49
50 namespace {
51 namespace stats {
52
53 STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
54 STATISTIC(EmittedRelaxableFragments,
55           "Number of emitted assembler fragments - relaxable");
56 STATISTIC(EmittedDataFragments,
57           "Number of emitted assembler fragments - data");
58 STATISTIC(EmittedCompactEncodedInstFragments,
59           "Number of emitted assembler fragments - compact encoded inst");
60 STATISTIC(EmittedAlignFragments,
61           "Number of emitted assembler fragments - align");
62 STATISTIC(EmittedFillFragments,
63           "Number of emitted assembler fragments - fill");
64 STATISTIC(EmittedOrgFragments,
65           "Number of emitted assembler fragments - org");
66 STATISTIC(evaluateFixup, "Number of evaluated fixups");
67 STATISTIC(FragmentLayouts, "Number of fragment layouts");
68 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
69 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
70 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
71 STATISTIC(PaddingFragmentsRelaxations,
72           "Number of Padding Fragments relaxations");
73 STATISTIC(PaddingFragmentsBytes,
74           "Total size of all padding from adding Fragments");
75
76 } // end namespace stats
77 } // end anonymous namespace
78
79 // FIXME FIXME FIXME: There are number of places in this file where we convert
80 // what is a 64-bit assembler value used for computation into a value in the
81 // object file, which may truncate it. We should detect that truncation where
82 // invalid and report errors back.
83
84 /* *** */
85
86 MCAssembler::MCAssembler(MCContext &Context, MCAsmBackend &Backend,
87                          MCCodeEmitter &Emitter, MCObjectWriter &Writer)
88     : Context(Context), Backend(Backend), Emitter(Emitter), Writer(Writer),
89       BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false),
90       IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) {
91   VersionInfo.Major = 0; // Major version == 0 for "none specified"
92 }
93
94 MCAssembler::~MCAssembler() = default;
95
96 void MCAssembler::reset() {
97   Sections.clear();
98   Symbols.clear();
99   IndirectSymbols.clear();
100   DataRegions.clear();
101   LinkerOptions.clear();
102   FileNames.clear();
103   ThumbFuncs.clear();
104   BundleAlignSize = 0;
105   RelaxAll = false;
106   SubsectionsViaSymbols = false;
107   IncrementalLinkerCompatible = false;
108   ELFHeaderEFlags = 0;
109   LOHContainer.reset();
110   VersionInfo.Major = 0;
111
112   // reset objects owned by us
113   getBackend().reset();
114   getEmitter().reset();
115   getWriter().reset();
116   getLOHContainer().reset();
117 }
118
119 bool MCAssembler::registerSection(MCSection &Section) {
120   if (Section.isRegistered())
121     return false;
122   Sections.push_back(&Section);
123   Section.setIsRegistered(true);
124   return true;
125 }
126
127 bool MCAssembler::isThumbFunc(const MCSymbol *Symbol) const {
128   if (ThumbFuncs.count(Symbol))
129     return true;
130
131   if (!Symbol->isVariable())
132     return false;
133
134   const MCExpr *Expr = Symbol->getVariableValue();
135
136   MCValue V;
137   if (!Expr->evaluateAsRelocatable(V, nullptr, nullptr))
138     return false;
139
140   if (V.getSymB() || V.getRefKind() != MCSymbolRefExpr::VK_None)
141     return false;
142
143   const MCSymbolRefExpr *Ref = V.getSymA();
144   if (!Ref)
145     return false;
146
147   if (Ref->getKind() != MCSymbolRefExpr::VK_None)
148     return false;
149
150   const MCSymbol &Sym = Ref->getSymbol();
151   if (!isThumbFunc(&Sym))
152     return false;
153
154   ThumbFuncs.insert(Symbol); // Cache it.
155   return true;
156 }
157
158 bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
159   // Non-temporary labels should always be visible to the linker.
160   if (!Symbol.isTemporary())
161     return true;
162
163   // Absolute temporary labels are never visible.
164   if (!Symbol.isInSection())
165     return false;
166
167   if (Symbol.isUsedInReloc())
168     return true;
169
170   return false;
171 }
172
173 const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const {
174   // Linker visible symbols define atoms.
175   if (isSymbolLinkerVisible(S))
176     return &S;
177
178   // Absolute and undefined symbols have no defining atom.
179   if (!S.isInSection())
180     return nullptr;
181
182   // Non-linker visible symbols in sections which can't be atomized have no
183   // defining atom.
184   if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
185           *S.getFragment()->getParent()))
186     return nullptr;
187
188   // Otherwise, return the atom for the containing fragment.
189   return S.getFragment()->getAtom();
190 }
191
192 bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
193                                 const MCFixup &Fixup, const MCFragment *DF,
194                                 MCValue &Target, uint64_t &Value) const {
195   ++stats::evaluateFixup;
196
197   // FIXME: This code has some duplication with recordRelocation. We should
198   // probably merge the two into a single callback that tries to evaluate a
199   // fixup and records a relocation if one is needed.
200
201   // On error claim to have completely evaluated the fixup, to prevent any
202   // further processing from being done.
203   const MCExpr *Expr = Fixup.getValue();
204   MCContext &Ctx = getContext();
205   Value = 0;
206   if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) {
207     Ctx.reportError(Fixup.getLoc(), "expected relocatable expression");
208     return true;
209   }
210   if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
211     if (RefB->getKind() != MCSymbolRefExpr::VK_None) {
212       Ctx.reportError(Fixup.getLoc(),
213                       "unsupported subtraction of qualified symbol");
214       return true;
215     }
216   }
217
218   bool IsPCRel = Backend.getFixupKindInfo(
219     Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
220
221   bool IsResolved;
222   if (IsPCRel) {
223     if (Target.getSymB()) {
224       IsResolved = false;
225     } else if (!Target.getSymA()) {
226       IsResolved = false;
227     } else {
228       const MCSymbolRefExpr *A = Target.getSymA();
229       const MCSymbol &SA = A->getSymbol();
230       if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
231         IsResolved = false;
232       } else {
233         IsResolved = getWriter().isSymbolRefDifferenceFullyResolvedImpl(
234             *this, SA, *DF, false, true);
235       }
236     }
237   } else {
238     IsResolved = Target.isAbsolute();
239   }
240
241   Value = Target.getConstant();
242
243   if (const MCSymbolRefExpr *A = Target.getSymA()) {
244     const MCSymbol &Sym = A->getSymbol();
245     if (Sym.isDefined())
246       Value += Layout.getSymbolOffset(Sym);
247   }
248   if (const MCSymbolRefExpr *B = Target.getSymB()) {
249     const MCSymbol &Sym = B->getSymbol();
250     if (Sym.isDefined())
251       Value -= Layout.getSymbolOffset(Sym);
252   }
253
254   bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
255                          MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
256   assert((ShouldAlignPC ? IsPCRel : true) &&
257     "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
258
259   if (IsPCRel) {
260     uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
261
262     // A number of ARM fixups in Thumb mode require that the effective PC
263     // address be determined as the 32-bit aligned version of the actual offset.
264     if (ShouldAlignPC) Offset &= ~0x3;
265     Value -= Offset;
266   }
267
268   // Let the backend force a relocation if needed.
269   if (IsResolved && Backend.shouldForceRelocation(*this, Fixup, Target))
270     IsResolved = false;
271
272   return IsResolved;
273 }
274
275 uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
276                                           const MCFragment &F) const {
277   switch (F.getKind()) {
278   case MCFragment::FT_Data:
279     return cast<MCDataFragment>(F).getContents().size();
280   case MCFragment::FT_Relaxable:
281     return cast<MCRelaxableFragment>(F).getContents().size();
282   case MCFragment::FT_CompactEncodedInst:
283     return cast<MCCompactEncodedInstFragment>(F).getContents().size();
284   case MCFragment::FT_Fill:
285     return cast<MCFillFragment>(F).getSize();
286
287   case MCFragment::FT_LEB:
288     return cast<MCLEBFragment>(F).getContents().size();
289
290   case MCFragment::FT_Padding:
291     return cast<MCPaddingFragment>(F).getSize();
292
293   case MCFragment::FT_SymbolId:
294     return 4;
295
296   case MCFragment::FT_Align: {
297     const MCAlignFragment &AF = cast<MCAlignFragment>(F);
298     unsigned Offset = Layout.getFragmentOffset(&AF);
299     unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
300     // If we are padding with nops, force the padding to be larger than the
301     // minimum nop size.
302     if (Size > 0 && AF.hasEmitNops()) {
303       while (Size % getBackend().getMinimumNopSize())
304         Size += AF.getAlignment();
305     }
306     if (Size > AF.getMaxBytesToEmit())
307       return 0;
308     return Size;
309   }
310
311   case MCFragment::FT_Org: {
312     const MCOrgFragment &OF = cast<MCOrgFragment>(F);
313     MCValue Value;
314     if (!OF.getOffset().evaluateAsValue(Value, Layout)) {
315       getContext().reportError(OF.getLoc(),
316                                "expected assembly-time absolute expression");
317         return 0;
318     }
319
320     uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
321     int64_t TargetLocation = Value.getConstant();
322     if (const MCSymbolRefExpr *A = Value.getSymA()) {
323       uint64_t Val;
324       if (!Layout.getSymbolOffset(A->getSymbol(), Val)) {
325         getContext().reportError(OF.getLoc(), "expected absolute expression");
326         return 0;
327       }
328       TargetLocation += Val;
329     }
330     int64_t Size = TargetLocation - FragmentOffset;
331     if (Size < 0 || Size >= 0x40000000) {
332       getContext().reportError(
333           OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
334                            "' (at offset '" + Twine(FragmentOffset) + "')");
335       return 0;
336     }
337     return Size;
338   }
339
340   case MCFragment::FT_Dwarf:
341     return cast<MCDwarfLineAddrFragment>(F).getContents().size();
342   case MCFragment::FT_DwarfFrame:
343     return cast<MCDwarfCallFrameFragment>(F).getContents().size();
344   case MCFragment::FT_CVInlineLines:
345     return cast<MCCVInlineLineTableFragment>(F).getContents().size();
346   case MCFragment::FT_CVDefRange:
347     return cast<MCCVDefRangeFragment>(F).getContents().size();
348   case MCFragment::FT_Dummy:
349     llvm_unreachable("Should not have been added");
350   }
351
352   llvm_unreachable("invalid fragment kind");
353 }
354
355 void MCAsmLayout::layoutFragment(MCFragment *F) {
356   MCFragment *Prev = F->getPrevNode();
357
358   // We should never try to recompute something which is valid.
359   assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
360   // We should never try to compute the fragment layout if its predecessor
361   // isn't valid.
362   assert((!Prev || isFragmentValid(Prev)) &&
363          "Attempt to compute fragment before its predecessor!");
364
365   ++stats::FragmentLayouts;
366
367   // Compute fragment offset and size.
368   if (Prev)
369     F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
370   else
371     F->Offset = 0;
372   LastValidFragment[F->getParent()] = F;
373
374   // If bundling is enabled and this fragment has instructions in it, it has to
375   // obey the bundling restrictions. With padding, we'll have:
376   //
377   //
378   //        BundlePadding
379   //             |||
380   // -------------------------------------
381   //   Prev  |##########|       F        |
382   // -------------------------------------
383   //                    ^
384   //                    |
385   //                    F->Offset
386   //
387   // The fragment's offset will point to after the padding, and its computed
388   // size won't include the padding.
389   //
390   // When the -mc-relax-all flag is used, we optimize bundling by writting the
391   // padding directly into fragments when the instructions are emitted inside
392   // the streamer. When the fragment is larger than the bundle size, we need to
393   // ensure that it's bundle aligned. This means that if we end up with
394   // multiple fragments, we must emit bundle padding between fragments.
395   //
396   // ".align N" is an example of a directive that introduces multiple
397   // fragments. We could add a special case to handle ".align N" by emitting
398   // within-fragment padding (which would produce less padding when N is less
399   // than the bundle size), but for now we don't.
400   //
401   if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
402     assert(isa<MCEncodedFragment>(F) &&
403            "Only MCEncodedFragment implementations have instructions");
404     uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
405
406     if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize())
407       report_fatal_error("Fragment can't be larger than a bundle size");
408
409     uint64_t RequiredBundlePadding = computeBundlePadding(Assembler, F,
410                                                           F->Offset, FSize);
411     if (RequiredBundlePadding > UINT8_MAX)
412       report_fatal_error("Padding cannot exceed 255 bytes");
413     F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
414     F->Offset += RequiredBundlePadding;
415   }
416 }
417
418 void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) {
419   bool New = !Symbol.isRegistered();
420   if (Created)
421     *Created = New;
422   if (New) {
423     Symbol.setIsRegistered(true);
424     Symbols.push_back(&Symbol);
425   }
426 }
427
428 void MCAssembler::writeFragmentPadding(const MCFragment &F, uint64_t FSize,
429                                        MCObjectWriter *OW) const {
430   // Should NOP padding be written out before this fragment?
431   unsigned BundlePadding = F.getBundlePadding();
432   if (BundlePadding > 0) {
433     assert(isBundlingEnabled() &&
434            "Writing bundle padding with disabled bundling");
435     assert(F.hasInstructions() &&
436            "Writing bundle padding for a fragment without instructions");
437
438     unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
439     if (F.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
440       // If the padding itself crosses a bundle boundary, it must be emitted
441       // in 2 pieces, since even nop instructions must not cross boundaries.
442       //             v--------------v   <- BundleAlignSize
443       //        v---------v             <- BundlePadding
444       // ----------------------------
445       // | Prev |####|####|    F    |
446       // ----------------------------
447       //        ^-------------------^   <- TotalLength
448       unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
449       if (!getBackend().writeNopData(DistanceToBoundary, OW))
450           report_fatal_error("unable to write NOP sequence of " +
451                              Twine(DistanceToBoundary) + " bytes");
452       BundlePadding -= DistanceToBoundary;
453     }
454     if (!getBackend().writeNopData(BundlePadding, OW))
455       report_fatal_error("unable to write NOP sequence of " +
456                          Twine(BundlePadding) + " bytes");
457   }
458 }
459
460 /// \brief Write the fragment \p F to the output file.
461 static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
462                           const MCFragment &F) {
463   MCObjectWriter *OW = &Asm.getWriter();
464
465   // FIXME: Embed in fragments instead?
466   uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
467
468   Asm.writeFragmentPadding(F, FragmentSize, OW);
469
470   // This variable (and its dummy usage) is to participate in the assert at
471   // the end of the function.
472   uint64_t Start = OW->getStream().tell();
473   (void) Start;
474
475   ++stats::EmittedFragments;
476
477   switch (F.getKind()) {
478   case MCFragment::FT_Align: {
479     ++stats::EmittedAlignFragments;
480     const MCAlignFragment &AF = cast<MCAlignFragment>(F);
481     assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
482
483     uint64_t Count = FragmentSize / AF.getValueSize();
484
485     // FIXME: This error shouldn't actually occur (the front end should emit
486     // multiple .align directives to enforce the semantics it wants), but is
487     // severe enough that we want to report it. How to handle this?
488     if (Count * AF.getValueSize() != FragmentSize)
489       report_fatal_error("undefined .align directive, value size '" +
490                         Twine(AF.getValueSize()) +
491                         "' is not a divisor of padding size '" +
492                         Twine(FragmentSize) + "'");
493
494     // See if we are aligning with nops, and if so do that first to try to fill
495     // the Count bytes.  Then if that did not fill any bytes or there are any
496     // bytes left to fill use the Value and ValueSize to fill the rest.
497     // If we are aligning with nops, ask that target to emit the right data.
498     if (AF.hasEmitNops()) {
499       if (!Asm.getBackend().writeNopData(Count, OW))
500         report_fatal_error("unable to write nop sequence of " +
501                           Twine(Count) + " bytes");
502       break;
503     }
504
505     // Otherwise, write out in multiples of the value size.
506     for (uint64_t i = 0; i != Count; ++i) {
507       switch (AF.getValueSize()) {
508       default: llvm_unreachable("Invalid size!");
509       case 1: OW->write8 (uint8_t (AF.getValue())); break;
510       case 2: OW->write16(uint16_t(AF.getValue())); break;
511       case 4: OW->write32(uint32_t(AF.getValue())); break;
512       case 8: OW->write64(uint64_t(AF.getValue())); break;
513       }
514     }
515     break;
516   }
517
518   case MCFragment::FT_Data: 
519     ++stats::EmittedDataFragments;
520     OW->writeBytes(cast<MCDataFragment>(F).getContents());
521     break;
522
523   case MCFragment::FT_Relaxable:
524     ++stats::EmittedRelaxableFragments;
525     OW->writeBytes(cast<MCRelaxableFragment>(F).getContents());
526     break;
527
528   case MCFragment::FT_CompactEncodedInst:
529     ++stats::EmittedCompactEncodedInstFragments;
530     OW->writeBytes(cast<MCCompactEncodedInstFragment>(F).getContents());
531     break;
532
533   case MCFragment::FT_Fill: {
534     ++stats::EmittedFillFragments;
535     const MCFillFragment &FF = cast<MCFillFragment>(F);
536     uint8_t V = FF.getValue();
537     const unsigned MaxChunkSize = 16;
538     char Data[MaxChunkSize];
539     memcpy(Data, &V, 1);
540     for (unsigned I = 1; I < MaxChunkSize; ++I)
541       Data[I] = Data[0];
542
543     uint64_t Size = FF.getSize();
544     for (unsigned ChunkSize = MaxChunkSize; ChunkSize; ChunkSize /= 2) {
545       StringRef Ref(Data, ChunkSize);
546       for (uint64_t I = 0, E = Size / ChunkSize; I != E; ++I)
547         OW->writeBytes(Ref);
548       Size = Size % ChunkSize;
549     }
550     break;
551   }
552
553   case MCFragment::FT_LEB: {
554     const MCLEBFragment &LF = cast<MCLEBFragment>(F);
555     OW->writeBytes(LF.getContents());
556     break;
557   }
558
559   case MCFragment::FT_Padding: {
560     if (!Asm.getBackend().writeNopData(FragmentSize, OW))
561       report_fatal_error("unable to write nop sequence of " +
562                          Twine(FragmentSize) + " bytes");
563     break;
564   }
565
566   case MCFragment::FT_SymbolId: {
567     const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F);
568     OW->write32(SF.getSymbol()->getIndex());
569     break;
570   }
571
572   case MCFragment::FT_Org: {
573     ++stats::EmittedOrgFragments;
574     const MCOrgFragment &OF = cast<MCOrgFragment>(F);
575
576     for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
577       OW->write8(uint8_t(OF.getValue()));
578
579     break;
580   }
581
582   case MCFragment::FT_Dwarf: {
583     const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
584     OW->writeBytes(OF.getContents());
585     break;
586   }
587   case MCFragment::FT_DwarfFrame: {
588     const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
589     OW->writeBytes(CF.getContents());
590     break;
591   }
592   case MCFragment::FT_CVInlineLines: {
593     const auto &OF = cast<MCCVInlineLineTableFragment>(F);
594     OW->writeBytes(OF.getContents());
595     break;
596   }
597   case MCFragment::FT_CVDefRange: {
598     const auto &DRF = cast<MCCVDefRangeFragment>(F);
599     OW->writeBytes(DRF.getContents());
600     break;
601   }
602   case MCFragment::FT_Dummy:
603     llvm_unreachable("Should not have been added");
604   }
605
606   assert(OW->getStream().tell() - Start == FragmentSize &&
607          "The stream should advance by fragment size");
608 }
609
610 void MCAssembler::writeSectionData(const MCSection *Sec,
611                                    const MCAsmLayout &Layout) const {
612   // Ignore virtual sections.
613   if (Sec->isVirtualSection()) {
614     assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!");
615
616     // Check that contents are only things legal inside a virtual section.
617     for (const MCFragment &F : *Sec) {
618       switch (F.getKind()) {
619       default: llvm_unreachable("Invalid fragment in virtual section!");
620       case MCFragment::FT_Data: {
621         // Check that we aren't trying to write a non-zero contents (or fixups)
622         // into a virtual section. This is to support clients which use standard
623         // directives to fill the contents of virtual sections.
624         const MCDataFragment &DF = cast<MCDataFragment>(F);
625         if (DF.fixup_begin() != DF.fixup_end())
626           report_fatal_error("cannot have fixups in virtual section!");
627         for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
628           if (DF.getContents()[i]) {
629             if (auto *ELFSec = dyn_cast<const MCSectionELF>(Sec))
630               report_fatal_error("non-zero initializer found in section '" +
631                   ELFSec->getSectionName() + "'");
632             else
633               report_fatal_error("non-zero initializer found in virtual section");
634           }
635         break;
636       }
637       case MCFragment::FT_Align:
638         // Check that we aren't trying to write a non-zero value into a virtual
639         // section.
640         assert((cast<MCAlignFragment>(F).getValueSize() == 0 ||
641                 cast<MCAlignFragment>(F).getValue() == 0) &&
642                "Invalid align in virtual section!");
643         break;
644       case MCFragment::FT_Fill:
645         assert((cast<MCFillFragment>(F).getValue() == 0) &&
646                "Invalid fill in virtual section!");
647         break;
648       }
649     }
650
651     return;
652   }
653
654   uint64_t Start = getWriter().getStream().tell();
655   (void)Start;
656
657   for (const MCFragment &F : *Sec)
658     writeFragment(*this, Layout, F);
659
660   assert(getWriter().getStream().tell() - Start ==
661          Layout.getSectionAddressSize(Sec));
662 }
663
664 std::tuple<MCValue, uint64_t, bool>
665 MCAssembler::handleFixup(const MCAsmLayout &Layout, MCFragment &F,
666                          const MCFixup &Fixup) {
667   // Evaluate the fixup.
668   MCValue Target;
669   uint64_t FixedValue;
670   bool IsResolved = evaluateFixup(Layout, Fixup, &F, Target, FixedValue);
671   if (!IsResolved) {
672     // The fixup was unresolved, we need a relocation. Inform the object
673     // writer of the relocation, and give it an opportunity to adjust the
674     // fixup value if need be.
675     getWriter().recordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
676   }
677   return std::make_tuple(Target, FixedValue, IsResolved);
678 }
679
680 void MCAssembler::layout(MCAsmLayout &Layout) {
681   DEBUG_WITH_TYPE("mc-dump", {
682       errs() << "assembler backend - pre-layout\n--\n";
683       dump(); });
684
685   // Create dummy fragments and assign section ordinals.
686   unsigned SectionIndex = 0;
687   for (MCSection &Sec : *this) {
688     // Create dummy fragments to eliminate any empty sections, this simplifies
689     // layout.
690     if (Sec.getFragmentList().empty())
691       new MCDataFragment(&Sec);
692
693     Sec.setOrdinal(SectionIndex++);
694   }
695
696   // Assign layout order indices to sections and fragments.
697   for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
698     MCSection *Sec = Layout.getSectionOrder()[i];
699     Sec->setLayoutOrder(i);
700
701     unsigned FragmentIndex = 0;
702     for (MCFragment &Frag : *Sec)
703       Frag.setLayoutOrder(FragmentIndex++);
704   }
705
706   // Layout until everything fits.
707   while (layoutOnce(Layout))
708     if (getContext().hadError())
709       return;
710
711   DEBUG_WITH_TYPE("mc-dump", {
712       errs() << "assembler backend - post-relaxation\n--\n";
713       dump(); });
714
715   // Finalize the layout, including fragment lowering.
716   finishLayout(Layout);
717
718   DEBUG_WITH_TYPE("mc-dump", {
719       errs() << "assembler backend - final-layout\n--\n";
720       dump(); });
721
722   // Allow the object writer a chance to perform post-layout binding (for
723   // example, to set the index fields in the symbol data).
724   getWriter().executePostLayoutBinding(*this, Layout);
725
726   // Evaluate and apply the fixups, generating relocation entries as necessary.
727   for (MCSection &Sec : *this) {
728     for (MCFragment &Frag : Sec) {
729       // Data and relaxable fragments both have fixups.  So only process
730       // those here.
731       // FIXME: Is there a better way to do this?  MCEncodedFragmentWithFixups
732       // being templated makes this tricky.
733       if (isa<MCEncodedFragment>(&Frag) &&
734           isa<MCCompactEncodedInstFragment>(&Frag))
735         continue;
736       if (!isa<MCEncodedFragment>(&Frag) && !isa<MCCVDefRangeFragment>(&Frag))
737         continue;
738       ArrayRef<MCFixup> Fixups;
739       MutableArrayRef<char> Contents;
740       if (auto *FragWithFixups = dyn_cast<MCDataFragment>(&Frag)) {
741         Fixups = FragWithFixups->getFixups();
742         Contents = FragWithFixups->getContents();
743       } else if (auto *FragWithFixups = dyn_cast<MCRelaxableFragment>(&Frag)) {
744         Fixups = FragWithFixups->getFixups();
745         Contents = FragWithFixups->getContents();
746       } else if (auto *FragWithFixups = dyn_cast<MCCVDefRangeFragment>(&Frag)) {
747         Fixups = FragWithFixups->getFixups();
748         Contents = FragWithFixups->getContents();
749       } else
750         llvm_unreachable("Unknown fragment with fixups!");
751       for (const MCFixup &Fixup : Fixups) {
752         uint64_t FixedValue;
753         bool IsResolved;
754         MCValue Target;
755         std::tie(Target, FixedValue, IsResolved) =
756             handleFixup(Layout, Frag, Fixup);
757         getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue,
758                                 IsResolved);
759       }
760     }
761   }
762 }
763
764 void MCAssembler::Finish() {
765   // Create the layout object.
766   MCAsmLayout Layout(*this);
767   layout(Layout);
768
769   raw_ostream &OS = getWriter().getStream();
770   uint64_t StartOffset = OS.tell();
771
772   // Write the object file.
773   getWriter().writeObject(*this, Layout);
774
775   stats::ObjectBytes += OS.tell() - StartOffset;
776 }
777
778 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
779                                        const MCRelaxableFragment *DF,
780                                        const MCAsmLayout &Layout) const {
781   MCValue Target;
782   uint64_t Value;
783   bool Resolved = evaluateFixup(Layout, Fixup, DF, Target, Value);
784   if (Target.getSymA() &&
785       Target.getSymA()->getKind() == MCSymbolRefExpr::VK_X86_ABS8 &&
786       Fixup.getKind() == FK_Data_1)
787     return false;
788   return getBackend().fixupNeedsRelaxationAdvanced(Fixup, Resolved, Value, DF,
789                                                    Layout);
790 }
791
792 bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
793                                           const MCAsmLayout &Layout) const {
794   // If this inst doesn't ever need relaxation, ignore it. This occurs when we
795   // are intentionally pushing out inst fragments, or because we relaxed a
796   // previous instruction to one that doesn't need relaxation.
797   if (!getBackend().mayNeedRelaxation(F->getInst()))
798     return false;
799
800   for (const MCFixup &Fixup : F->getFixups())
801     if (fixupNeedsRelaxation(Fixup, F, Layout))
802       return true;
803
804   return false;
805 }
806
807 bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
808                                    MCRelaxableFragment &F) {
809   if (!fragmentNeedsRelaxation(&F, Layout))
810     return false;
811
812   ++stats::RelaxedInstructions;
813
814   // FIXME-PERF: We could immediately lower out instructions if we can tell
815   // they are fully resolved, to avoid retesting on later passes.
816
817   // Relax the fragment.
818
819   MCInst Relaxed;
820   getBackend().relaxInstruction(F.getInst(), F.getSubtargetInfo(), Relaxed);
821
822   // Encode the new instruction.
823   //
824   // FIXME-PERF: If it matters, we could let the target do this. It can
825   // probably do so more efficiently in many cases.
826   SmallVector<MCFixup, 4> Fixups;
827   SmallString<256> Code;
828   raw_svector_ostream VecOS(Code);
829   getEmitter().encodeInstruction(Relaxed, VecOS, Fixups, F.getSubtargetInfo());
830
831   // Update the fragment.
832   F.setInst(Relaxed);
833   F.getContents() = Code;
834   F.getFixups() = Fixups;
835
836   return true;
837 }
838
839 bool MCAssembler::relaxPaddingFragment(MCAsmLayout &Layout,
840                                        MCPaddingFragment &PF) {
841   uint64_t OldSize = PF.getSize();
842   if (!getBackend().relaxFragment(&PF, Layout))
843     return false;
844   uint64_t NewSize = PF.getSize();
845
846   ++stats::PaddingFragmentsRelaxations;
847   stats::PaddingFragmentsBytes += NewSize;
848   stats::PaddingFragmentsBytes -= OldSize;
849   return true;
850 }
851
852 bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
853   uint64_t OldSize = LF.getContents().size();
854   int64_t Value;
855   bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout);
856   if (!Abs)
857     report_fatal_error("sleb128 and uleb128 expressions must be absolute");
858   SmallString<8> &Data = LF.getContents();
859   Data.clear();
860   raw_svector_ostream OSE(Data);
861   if (LF.isSigned())
862     encodeSLEB128(Value, OSE);
863   else
864     encodeULEB128(Value, OSE);
865   return OldSize != LF.getContents().size();
866 }
867
868 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
869                                      MCDwarfLineAddrFragment &DF) {
870   MCContext &Context = Layout.getAssembler().getContext();
871   uint64_t OldSize = DF.getContents().size();
872   int64_t AddrDelta;
873   bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
874   assert(Abs && "We created a line delta with an invalid expression");
875   (void) Abs;
876   int64_t LineDelta;
877   LineDelta = DF.getLineDelta();
878   SmallString<8> &Data = DF.getContents();
879   Data.clear();
880   raw_svector_ostream OSE(Data);
881   MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta,
882                           AddrDelta, OSE);
883   return OldSize != Data.size();
884 }
885
886 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
887                                               MCDwarfCallFrameFragment &DF) {
888   MCContext &Context = Layout.getAssembler().getContext();
889   uint64_t OldSize = DF.getContents().size();
890   int64_t AddrDelta;
891   bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
892   assert(Abs && "We created call frame with an invalid expression");
893   (void) Abs;
894   SmallString<8> &Data = DF.getContents();
895   Data.clear();
896   raw_svector_ostream OSE(Data);
897   MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE);
898   return OldSize != Data.size();
899 }
900
901 bool MCAssembler::relaxCVInlineLineTable(MCAsmLayout &Layout,
902                                          MCCVInlineLineTableFragment &F) {
903   unsigned OldSize = F.getContents().size();
904   getContext().getCVContext().encodeInlineLineTable(Layout, F);
905   return OldSize != F.getContents().size();
906 }
907
908 bool MCAssembler::relaxCVDefRange(MCAsmLayout &Layout,
909                                   MCCVDefRangeFragment &F) {
910   unsigned OldSize = F.getContents().size();
911   getContext().getCVContext().encodeDefRange(Layout, F);
912   return OldSize != F.getContents().size();
913 }
914
915 bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) {
916   // Holds the first fragment which needed relaxing during this layout. It will
917   // remain NULL if none were relaxed.
918   // When a fragment is relaxed, all the fragments following it should get
919   // invalidated because their offset is going to change.
920   MCFragment *FirstRelaxedFragment = nullptr;
921
922   // Attempt to relax all the fragments in the section.
923   for (MCSection::iterator I = Sec.begin(), IE = Sec.end(); I != IE; ++I) {
924     // Check if this is a fragment that needs relaxation.
925     bool RelaxedFrag = false;
926     switch(I->getKind()) {
927     default:
928       break;
929     case MCFragment::FT_Relaxable:
930       assert(!getRelaxAll() &&
931              "Did not expect a MCRelaxableFragment in RelaxAll mode");
932       RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
933       break;
934     case MCFragment::FT_Dwarf:
935       RelaxedFrag = relaxDwarfLineAddr(Layout,
936                                        *cast<MCDwarfLineAddrFragment>(I));
937       break;
938     case MCFragment::FT_DwarfFrame:
939       RelaxedFrag =
940         relaxDwarfCallFrameFragment(Layout,
941                                     *cast<MCDwarfCallFrameFragment>(I));
942       break;
943     case MCFragment::FT_LEB:
944       RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
945       break;
946     case MCFragment::FT_Padding:
947       RelaxedFrag = relaxPaddingFragment(Layout, *cast<MCPaddingFragment>(I));
948       break;
949     case MCFragment::FT_CVInlineLines:
950       RelaxedFrag =
951           relaxCVInlineLineTable(Layout, *cast<MCCVInlineLineTableFragment>(I));
952       break;
953     case MCFragment::FT_CVDefRange:
954       RelaxedFrag = relaxCVDefRange(Layout, *cast<MCCVDefRangeFragment>(I));
955       break;
956     }
957     if (RelaxedFrag && !FirstRelaxedFragment)
958       FirstRelaxedFragment = &*I;
959   }
960   if (FirstRelaxedFragment) {
961     Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
962     return true;
963   }
964   return false;
965 }
966
967 bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
968   ++stats::RelaxationSteps;
969
970   bool WasRelaxed = false;
971   for (iterator it = begin(), ie = end(); it != ie; ++it) {
972     MCSection &Sec = *it;
973     while (layoutSectionOnce(Layout, Sec))
974       WasRelaxed = true;
975   }
976
977   return WasRelaxed;
978 }
979
980 void MCAssembler::finishLayout(MCAsmLayout &Layout) {
981   // The layout is done. Mark every fragment as valid.
982   for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
983     MCSection &Section = *Layout.getSectionOrder()[i];
984     Layout.getFragmentOffset(&*Section.rbegin());
985     computeFragmentSize(Layout, *Section.rbegin());
986   }
987   getBackend().finishLayout(*this, Layout);
988 }