LCOV - code coverage report
Current view: top level - lib/MC - MCAssembler.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 421 444 94.8 %
Date: 2017-09-14 15:23:50 Functions: 27 27 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13