LLVM  10.0.0svn
MCAssembler.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCAssembler.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCCodeView.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCFixup.h"
26 #include "llvm/MC/MCFragment.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSectionELF.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/LEB128.h"
39 #include <cassert>
40 #include <cstdint>
41 #include <cstring>
42 #include <tuple>
43 #include <utility>
44 
45 using namespace llvm;
46 
47 #define DEBUG_TYPE "assembler"
48 
49 namespace {
50 namespace stats {
51 
52 STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
53 STATISTIC(EmittedRelaxableFragments,
54  "Number of emitted assembler fragments - relaxable");
55 STATISTIC(EmittedDataFragments,
56  "Number of emitted assembler fragments - data");
57 STATISTIC(EmittedCompactEncodedInstFragments,
58  "Number of emitted assembler fragments - compact encoded inst");
59 STATISTIC(EmittedAlignFragments,
60  "Number of emitted assembler fragments - align");
61 STATISTIC(EmittedFillFragments,
62  "Number of emitted assembler fragments - fill");
63 STATISTIC(EmittedOrgFragments,
64  "Number of emitted assembler fragments - org");
65 STATISTIC(evaluateFixup, "Number of evaluated fixups");
66 STATISTIC(FragmentLayouts, "Number of fragment layouts");
67 STATISTIC(ObjectBytes, "Number of emitted object file bytes");
68 STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
69 STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
70 STATISTIC(PaddingFragmentsRelaxations,
71  "Number of Padding Fragments relaxations");
72 STATISTIC(PaddingFragmentsBytes,
73  "Total size of all padding from adding Fragments");
74 
75 } // end namespace stats
76 } // end anonymous namespace
77 
78 // FIXME FIXME FIXME: There are number of places in this file where we convert
79 // what is a 64-bit assembler value used for computation into a value in the
80 // object file, which may truncate it. We should detect that truncation where
81 // invalid and report errors back.
82 
83 /* *** */
84 
86  std::unique_ptr<MCAsmBackend> Backend,
87  std::unique_ptr<MCCodeEmitter> Emitter,
88  std::unique_ptr<MCObjectWriter> Writer)
89  : Context(Context), Backend(std::move(Backend)),
90  Emitter(std::move(Emitter)), Writer(std::move(Writer)),
91  BundleAlignSize(0), RelaxAll(false), SubsectionsViaSymbols(false),
92  IncrementalLinkerCompatible(false), ELFHeaderEFlags(0) {
93  VersionInfo.Major = 0; // Major version == 0 for "none specified"
94 }
95 
96 MCAssembler::~MCAssembler() = default;
97 
99  Sections.clear();
100  Symbols.clear();
101  IndirectSymbols.clear();
102  DataRegions.clear();
103  LinkerOptions.clear();
104  FileNames.clear();
105  ThumbFuncs.clear();
106  BundleAlignSize = 0;
107  RelaxAll = false;
108  SubsectionsViaSymbols = false;
109  IncrementalLinkerCompatible = false;
110  ELFHeaderEFlags = 0;
111  LOHContainer.reset();
112  VersionInfo.Major = 0;
113  VersionInfo.SDKVersion = VersionTuple();
114 
115  // reset objects owned by us
116  if (getBackendPtr())
117  getBackendPtr()->reset();
118  if (getEmitterPtr())
119  getEmitterPtr()->reset();
120  if (getWriterPtr())
121  getWriterPtr()->reset();
123 }
124 
126  if (Section.isRegistered())
127  return false;
128  Sections.push_back(&Section);
129  Section.setIsRegistered(true);
130  return true;
131 }
132 
134  if (ThumbFuncs.count(Symbol))
135  return true;
136 
137  if (!Symbol->isVariable())
138  return false;
139 
140  const MCExpr *Expr = Symbol->getVariableValue();
141 
142  MCValue V;
143  if (!Expr->evaluateAsRelocatable(V, nullptr, nullptr))
144  return false;
145 
146  if (V.getSymB() || V.getRefKind() != MCSymbolRefExpr::VK_None)
147  return false;
148 
149  const MCSymbolRefExpr *Ref = V.getSymA();
150  if (!Ref)
151  return false;
152 
153  if (Ref->getKind() != MCSymbolRefExpr::VK_None)
154  return false;
155 
156  const MCSymbol &Sym = Ref->getSymbol();
157  if (!isThumbFunc(&Sym))
158  return false;
159 
160  ThumbFuncs.insert(Symbol); // Cache it.
161  return true;
162 }
163 
165  // Non-temporary labels should always be visible to the linker.
166  if (!Symbol.isTemporary())
167  return true;
168 
169  // Absolute temporary labels are never visible.
170  if (!Symbol.isInSection())
171  return false;
172 
173  if (Symbol.isUsedInReloc())
174  return true;
175 
176  return false;
177 }
178 
179 const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const {
180  // Linker visible symbols define atoms.
181  if (isSymbolLinkerVisible(S))
182  return &S;
183 
184  // Absolute and undefined symbols have no defining atom.
185  if (!S.isInSection())
186  return nullptr;
187 
188  // Non-linker visible symbols in sections which can't be atomized have no
189  // defining atom.
191  *S.getFragment()->getParent()))
192  return nullptr;
193 
194  // Otherwise, return the atom for the containing fragment.
195  return S.getFragment()->getAtom();
196 }
197 
198 bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
199  const MCFixup &Fixup, const MCFragment *DF,
200  MCValue &Target, uint64_t &Value,
201  bool &WasForced) const {
202  ++stats::evaluateFixup;
203 
204  // FIXME: This code has some duplication with recordRelocation. We should
205  // probably merge the two into a single callback that tries to evaluate a
206  // fixup and records a relocation if one is needed.
207 
208  // On error claim to have completely evaluated the fixup, to prevent any
209  // further processing from being done.
210  const MCExpr *Expr = Fixup.getValue();
211  MCContext &Ctx = getContext();
212  Value = 0;
213  WasForced = false;
214  if (!Expr->evaluateAsRelocatable(Target, &Layout, &Fixup)) {
215  Ctx.reportError(Fixup.getLoc(), "expected relocatable expression");
216  return true;
217  }
218  if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
219  if (RefB->getKind() != MCSymbolRefExpr::VK_None) {
220  Ctx.reportError(Fixup.getLoc(),
221  "unsupported subtraction of qualified symbol");
222  return true;
223  }
224  }
225 
226  assert(getBackendPtr() && "Expected assembler backend");
227  bool IsPCRel = getBackendPtr()->getFixupKindInfo(Fixup.getKind()).Flags &
229 
230  bool IsResolved = false;
231  if (IsPCRel) {
232  if (Target.getSymB()) {
233  IsResolved = false;
234  } else if (!Target.getSymA()) {
235  IsResolved = false;
236  } else {
237  const MCSymbolRefExpr *A = Target.getSymA();
238  const MCSymbol &SA = A->getSymbol();
239  if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {
240  IsResolved = false;
241  } else if (auto *Writer = getWriterPtr()) {
242  IsResolved = Writer->isSymbolRefDifferenceFullyResolvedImpl(
243  *this, SA, *DF, false, true);
244  }
245  }
246  } else {
247  IsResolved = Target.isAbsolute();
248  }
249 
250  Value = Target.getConstant();
251 
252  if (const MCSymbolRefExpr *A = Target.getSymA()) {
253  const MCSymbol &Sym = A->getSymbol();
254  if (Sym.isDefined())
255  Value += Layout.getSymbolOffset(Sym);
256  }
257  if (const MCSymbolRefExpr *B = Target.getSymB()) {
258  const MCSymbol &Sym = B->getSymbol();
259  if (Sym.isDefined())
260  Value -= Layout.getSymbolOffset(Sym);
261  }
262 
263  bool ShouldAlignPC = getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
265  assert((ShouldAlignPC ? IsPCRel : true) &&
266  "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
267 
268  if (IsPCRel) {
269  uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
270 
271  // A number of ARM fixups in Thumb mode require that the effective PC
272  // address be determined as the 32-bit aligned version of the actual offset.
273  if (ShouldAlignPC) Offset &= ~0x3;
274  Value -= Offset;
275  }
276 
277  // Let the backend force a relocation if needed.
278  if (IsResolved && getBackend().shouldForceRelocation(*this, Fixup, Target)) {
279  IsResolved = false;
280  WasForced = true;
281  }
282 
283  return IsResolved;
284 }
285 
287  const MCFragment &F) const {
288  assert(getBackendPtr() && "Requires assembler backend");
289  switch (F.getKind()) {
290  case MCFragment::FT_Data:
291  return cast<MCDataFragment>(F).getContents().size();
293  return cast<MCRelaxableFragment>(F).getContents().size();
295  return cast<MCCompactEncodedInstFragment>(F).getContents().size();
296  case MCFragment::FT_Fill: {
297  auto &FF = cast<MCFillFragment>(F);
298  int64_t NumValues = 0;
299  if (!FF.getNumValues().evaluateAsAbsolute(NumValues, Layout)) {
300  getContext().reportError(FF.getLoc(),
301  "expected assembly-time absolute expression");
302  return 0;
303  }
304  int64_t Size = NumValues * FF.getValueSize();
305  if (Size < 0) {
306  getContext().reportError(FF.getLoc(), "invalid number of bytes");
307  return 0;
308  }
309  return Size;
310  }
311 
312  case MCFragment::FT_LEB:
313  return cast<MCLEBFragment>(F).getContents().size();
314 
316  return cast<MCPaddingFragment>(F).getSize();
317 
319  return 4;
320 
321  case MCFragment::FT_Align: {
322  const MCAlignFragment &AF = cast<MCAlignFragment>(F);
323  unsigned Offset = Layout.getFragmentOffset(&AF);
324  unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
325 
326  // Insert extra Nops for code alignment if the target define
327  // shouldInsertExtraNopBytesForCodeAlign target hook.
328  if (AF.getParent()->UseCodeAlign() && AF.hasEmitNops() &&
330  return Size;
331 
332  // If we are padding with nops, force the padding to be larger than the
333  // minimum nop size.
334  if (Size > 0 && AF.hasEmitNops()) {
335  while (Size % getBackend().getMinimumNopSize())
336  Size += AF.getAlignment();
337  }
338  if (Size > AF.getMaxBytesToEmit())
339  return 0;
340  return Size;
341  }
342 
343  case MCFragment::FT_Org: {
344  const MCOrgFragment &OF = cast<MCOrgFragment>(F);
345  MCValue Value;
346  if (!OF.getOffset().evaluateAsValue(Value, Layout)) {
348  "expected assembly-time absolute expression");
349  return 0;
350  }
351 
352  uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
353  int64_t TargetLocation = Value.getConstant();
354  if (const MCSymbolRefExpr *A = Value.getSymA()) {
355  uint64_t Val;
356  if (!Layout.getSymbolOffset(A->getSymbol(), Val)) {
357  getContext().reportError(OF.getLoc(), "expected absolute expression");
358  return 0;
359  }
360  TargetLocation += Val;
361  }
362  int64_t Size = TargetLocation - FragmentOffset;
363  if (Size < 0 || Size >= 0x40000000) {
365  OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
366  "' (at offset '" + Twine(FragmentOffset) + "')");
367  return 0;
368  }
369  return Size;
370  }
371 
373  return cast<MCDwarfLineAddrFragment>(F).getContents().size();
375  return cast<MCDwarfCallFrameFragment>(F).getContents().size();
377  return cast<MCCVInlineLineTableFragment>(F).getContents().size();
379  return cast<MCCVDefRangeFragment>(F).getContents().size();
381  llvm_unreachable("Should not have been added");
382  }
383 
384  llvm_unreachable("invalid fragment kind");
385 }
386 
388  MCFragment *Prev = F->getPrevNode();
389 
390  // We should never try to recompute something which is valid.
391  assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
392  // We should never try to compute the fragment layout if its predecessor
393  // isn't valid.
394  assert((!Prev || isFragmentValid(Prev)) &&
395  "Attempt to compute fragment before its predecessor!");
396 
397  ++stats::FragmentLayouts;
398 
399  // Compute fragment offset and size.
400  if (Prev)
401  F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
402  else
403  F->Offset = 0;
404  LastValidFragment[F->getParent()] = F;
405 
406  // If bundling is enabled and this fragment has instructions in it, it has to
407  // obey the bundling restrictions. With padding, we'll have:
408  //
409  //
410  // BundlePadding
411  // |||
412  // -------------------------------------
413  // Prev |##########| F |
414  // -------------------------------------
415  // ^
416  // |
417  // F->Offset
418  //
419  // The fragment's offset will point to after the padding, and its computed
420  // size won't include the padding.
421  //
422  // When the -mc-relax-all flag is used, we optimize bundling by writting the
423  // padding directly into fragments when the instructions are emitted inside
424  // the streamer. When the fragment is larger than the bundle size, we need to
425  // ensure that it's bundle aligned. This means that if we end up with
426  // multiple fragments, we must emit bundle padding between fragments.
427  //
428  // ".align N" is an example of a directive that introduces multiple
429  // fragments. We could add a special case to handle ".align N" by emitting
430  // within-fragment padding (which would produce less padding when N is less
431  // than the bundle size), but for now we don't.
432  //
433  if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
434  assert(isa<MCEncodedFragment>(F) &&
435  "Only MCEncodedFragment implementations have instructions");
436  MCEncodedFragment *EF = cast<MCEncodedFragment>(F);
437  uint64_t FSize = Assembler.computeFragmentSize(*this, *EF);
438 
439  if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize())
440  report_fatal_error("Fragment can't be larger than a bundle size");
441 
442  uint64_t RequiredBundlePadding =
443  computeBundlePadding(Assembler, EF, EF->Offset, FSize);
444  if (RequiredBundlePadding > UINT8_MAX)
445  report_fatal_error("Padding cannot exceed 255 bytes");
446  EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
447  EF->Offset += RequiredBundlePadding;
448  }
449 }
450 
451 void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) {
452  bool New = !Symbol.isRegistered();
453  if (Created)
454  *Created = New;
455  if (New) {
456  Symbol.setIsRegistered(true);
457  Symbols.push_back(&Symbol);
458  }
459 }
460 
462  const MCEncodedFragment &EF,
463  uint64_t FSize) const {
464  assert(getBackendPtr() && "Expected assembler backend");
465  // Should NOP padding be written out before this fragment?
466  unsigned BundlePadding = EF.getBundlePadding();
467  if (BundlePadding > 0) {
469  "Writing bundle padding with disabled bundling");
470  assert(EF.hasInstructions() &&
471  "Writing bundle padding for a fragment without instructions");
472 
473  unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
474  if (EF.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
475  // If the padding itself crosses a bundle boundary, it must be emitted
476  // in 2 pieces, since even nop instructions must not cross boundaries.
477  // v--------------v <- BundleAlignSize
478  // v---------v <- BundlePadding
479  // ----------------------------
480  // | Prev |####|####| F |
481  // ----------------------------
482  // ^-------------------^ <- TotalLength
483  unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
484  if (!getBackend().writeNopData(OS, DistanceToBoundary))
485  report_fatal_error("unable to write NOP sequence of " +
486  Twine(DistanceToBoundary) + " bytes");
487  BundlePadding -= DistanceToBoundary;
488  }
489  if (!getBackend().writeNopData(OS, BundlePadding))
490  report_fatal_error("unable to write NOP sequence of " +
491  Twine(BundlePadding) + " bytes");
492  }
493 }
494 
495 /// Write the fragment \p F to the output file.
496 static void writeFragment(raw_ostream &OS, const MCAssembler &Asm,
497  const MCAsmLayout &Layout, const MCFragment &F) {
498  // FIXME: Embed in fragments instead?
499  uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
500 
501  support::endianness Endian = Asm.getBackend().Endian;
502 
503  if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(&F))
504  Asm.writeFragmentPadding(OS, *EF, FragmentSize);
505 
506  // This variable (and its dummy usage) is to participate in the assert at
507  // the end of the function.
508  uint64_t Start = OS.tell();
509  (void) Start;
510 
511  ++stats::EmittedFragments;
512 
513  switch (F.getKind()) {
514  case MCFragment::FT_Align: {
515  ++stats::EmittedAlignFragments;
516  const MCAlignFragment &AF = cast<MCAlignFragment>(F);
517  assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
518 
519  uint64_t Count = FragmentSize / AF.getValueSize();
520 
521  // FIXME: This error shouldn't actually occur (the front end should emit
522  // multiple .align directives to enforce the semantics it wants), but is
523  // severe enough that we want to report it. How to handle this?
524  if (Count * AF.getValueSize() != FragmentSize)
525  report_fatal_error("undefined .align directive, value size '" +
526  Twine(AF.getValueSize()) +
527  "' is not a divisor of padding size '" +
528  Twine(FragmentSize) + "'");
529 
530  // See if we are aligning with nops, and if so do that first to try to fill
531  // the Count bytes. Then if that did not fill any bytes or there are any
532  // bytes left to fill use the Value and ValueSize to fill the rest.
533  // If we are aligning with nops, ask that target to emit the right data.
534  if (AF.hasEmitNops()) {
535  if (!Asm.getBackend().writeNopData(OS, Count))
536  report_fatal_error("unable to write nop sequence of " +
537  Twine(Count) + " bytes");
538  break;
539  }
540 
541  // Otherwise, write out in multiples of the value size.
542  for (uint64_t i = 0; i != Count; ++i) {
543  switch (AF.getValueSize()) {
544  default: llvm_unreachable("Invalid size!");
545  case 1: OS << char(AF.getValue()); break;
546  case 2:
547  support::endian::write<uint16_t>(OS, AF.getValue(), Endian);
548  break;
549  case 4:
550  support::endian::write<uint32_t>(OS, AF.getValue(), Endian);
551  break;
552  case 8:
553  support::endian::write<uint64_t>(OS, AF.getValue(), Endian);
554  break;
555  }
556  }
557  break;
558  }
559 
560  case MCFragment::FT_Data:
561  ++stats::EmittedDataFragments;
562  OS << cast<MCDataFragment>(F).getContents();
563  break;
564 
566  ++stats::EmittedRelaxableFragments;
567  OS << cast<MCRelaxableFragment>(F).getContents();
568  break;
569 
571  ++stats::EmittedCompactEncodedInstFragments;
572  OS << cast<MCCompactEncodedInstFragment>(F).getContents();
573  break;
574 
575  case MCFragment::FT_Fill: {
576  ++stats::EmittedFillFragments;
577  const MCFillFragment &FF = cast<MCFillFragment>(F);
578  uint64_t V = FF.getValue();
579  unsigned VSize = FF.getValueSize();
580  const unsigned MaxChunkSize = 16;
581  char Data[MaxChunkSize];
582  // Duplicate V into Data as byte vector to reduce number of
583  // writes done. As such, do endian conversion here.
584  for (unsigned I = 0; I != VSize; ++I) {
585  unsigned index = Endian == support::little ? I : (VSize - I - 1);
586  Data[I] = uint8_t(V >> (index * 8));
587  }
588  for (unsigned I = VSize; I < MaxChunkSize; ++I)
589  Data[I] = Data[I - VSize];
590 
591  // Set to largest multiple of VSize in Data.
592  const unsigned NumPerChunk = MaxChunkSize / VSize;
593  // Set ChunkSize to largest multiple of VSize in Data
594  const unsigned ChunkSize = VSize * NumPerChunk;
595 
596  // Do copies by chunk.
597  StringRef Ref(Data, ChunkSize);
598  for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I)
599  OS << Ref;
600 
601  // do remainder if needed.
602  unsigned TrailingCount = FragmentSize % ChunkSize;
603  if (TrailingCount)
604  OS.write(Data, TrailingCount);
605  break;
606  }
607 
608  case MCFragment::FT_LEB: {
609  const MCLEBFragment &LF = cast<MCLEBFragment>(F);
610  OS << LF.getContents();
611  break;
612  }
613 
614  case MCFragment::FT_Padding: {
615  if (!Asm.getBackend().writeNopData(OS, FragmentSize))
616  report_fatal_error("unable to write nop sequence of " +
617  Twine(FragmentSize) + " bytes");
618  break;
619  }
620 
622  const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F);
623  support::endian::write<uint32_t>(OS, SF.getSymbol()->getIndex(), Endian);
624  break;
625  }
626 
627  case MCFragment::FT_Org: {
628  ++stats::EmittedOrgFragments;
629  const MCOrgFragment &OF = cast<MCOrgFragment>(F);
630 
631  for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
632  OS << char(OF.getValue());
633 
634  break;
635  }
636 
637  case MCFragment::FT_Dwarf: {
638  const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
639  OS << OF.getContents();
640  break;
641  }
643  const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
644  OS << CF.getContents();
645  break;
646  }
648  const auto &OF = cast<MCCVInlineLineTableFragment>(F);
649  OS << OF.getContents();
650  break;
651  }
653  const auto &DRF = cast<MCCVDefRangeFragment>(F);
654  OS << DRF.getContents();
655  break;
656  }
658  llvm_unreachable("Should not have been added");
659  }
660 
661  assert(OS.tell() - Start == FragmentSize &&
662  "The stream should advance by fragment size");
663 }
664 
666  const MCAsmLayout &Layout) const {
667  assert(getBackendPtr() && "Expected assembler backend");
668 
669  // Ignore virtual sections.
670  if (Sec->isVirtualSection()) {
671  assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!");
672 
673  // Check that contents are only things legal inside a virtual section.
674  for (const MCFragment &F : *Sec) {
675  switch (F.getKind()) {
676  default: llvm_unreachable("Invalid fragment in virtual section!");
677  case MCFragment::FT_Data: {
678  // Check that we aren't trying to write a non-zero contents (or fixups)
679  // into a virtual section. This is to support clients which use standard
680  // directives to fill the contents of virtual sections.
681  const MCDataFragment &DF = cast<MCDataFragment>(F);
682  if (DF.fixup_begin() != DF.fixup_end())
683  report_fatal_error("cannot have fixups in virtual section!");
684  for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
685  if (DF.getContents()[i]) {
686  if (auto *ELFSec = dyn_cast<const MCSectionELF>(Sec))
687  report_fatal_error("non-zero initializer found in section '" +
688  ELFSec->getSectionName() + "'");
689  else
690  report_fatal_error("non-zero initializer found in virtual section");
691  }
692  break;
693  }
695  // Check that we aren't trying to write a non-zero value into a virtual
696  // section.
697  assert((cast<MCAlignFragment>(F).getValueSize() == 0 ||
698  cast<MCAlignFragment>(F).getValue() == 0) &&
699  "Invalid align in virtual section!");
700  break;
701  case MCFragment::FT_Fill:
702  assert((cast<MCFillFragment>(F).getValue() == 0) &&
703  "Invalid fill in virtual section!");
704  break;
705  }
706  }
707 
708  return;
709  }
710 
711  uint64_t Start = OS.tell();
712  (void)Start;
713 
714  for (const MCFragment &F : *Sec)
715  writeFragment(OS, *this, Layout, F);
716 
717  assert(OS.tell() - Start == Layout.getSectionAddressSize(Sec));
718 }
719 
720 std::tuple<MCValue, uint64_t, bool>
721 MCAssembler::handleFixup(const MCAsmLayout &Layout, MCFragment &F,
722  const MCFixup &Fixup) {
723  // Evaluate the fixup.
724  MCValue Target;
725  uint64_t FixedValue;
726  bool WasForced;
727  bool IsResolved = evaluateFixup(Layout, Fixup, &F, Target, FixedValue,
728  WasForced);
729  if (!IsResolved) {
730  // The fixup was unresolved, we need a relocation. Inform the object
731  // writer of the relocation, and give it an opportunity to adjust the
732  // fixup value if need be.
733  if (Target.getSymA() && Target.getSymB() &&
735  // The fixup represents the difference between two symbols, which the
736  // backend has indicated must be resolved at link time. Split up the fixup
737  // into two relocations, one for the add, and one for the sub, and emit
738  // both of these. The constant will be associated with the add half of the
739  // expression.
740  MCFixup FixupAdd = MCFixup::createAddFor(Fixup);
741  MCValue TargetAdd =
742  MCValue::get(Target.getSymA(), nullptr, Target.getConstant());
743  getWriter().recordRelocation(*this, Layout, &F, FixupAdd, TargetAdd,
744  FixedValue);
745  MCFixup FixupSub = MCFixup::createSubFor(Fixup);
746  MCValue TargetSub = MCValue::get(Target.getSymB());
747  getWriter().recordRelocation(*this, Layout, &F, FixupSub, TargetSub,
748  FixedValue);
749  } else {
750  getWriter().recordRelocation(*this, Layout, &F, Fixup, Target,
751  FixedValue);
752  }
753  }
754  return std::make_tuple(Target, FixedValue, IsResolved);
755 }
756 
758  assert(getBackendPtr() && "Expected assembler backend");
759  DEBUG_WITH_TYPE("mc-dump", {
760  errs() << "assembler backend - pre-layout\n--\n";
761  dump(); });
762 
763  // Create dummy fragments and assign section ordinals.
764  unsigned SectionIndex = 0;
765  for (MCSection &Sec : *this) {
766  // Create dummy fragments to eliminate any empty sections, this simplifies
767  // layout.
768  if (Sec.getFragmentList().empty())
769  new MCDataFragment(&Sec);
770 
771  Sec.setOrdinal(SectionIndex++);
772  }
773 
774  // Assign layout order indices to sections and fragments.
775  for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
776  MCSection *Sec = Layout.getSectionOrder()[i];
777  Sec->setLayoutOrder(i);
778 
779  unsigned FragmentIndex = 0;
780  for (MCFragment &Frag : *Sec)
781  Frag.setLayoutOrder(FragmentIndex++);
782  }
783 
784  // Layout until everything fits.
785  while (layoutOnce(Layout))
786  if (getContext().hadError())
787  return;
788 
789  DEBUG_WITH_TYPE("mc-dump", {
790  errs() << "assembler backend - post-relaxation\n--\n";
791  dump(); });
792 
793  // Finalize the layout, including fragment lowering.
794  finishLayout(Layout);
795 
796  DEBUG_WITH_TYPE("mc-dump", {
797  errs() << "assembler backend - final-layout\n--\n";
798  dump(); });
799 
800  // Allow the object writer a chance to perform post-layout binding (for
801  // example, to set the index fields in the symbol data).
802  getWriter().executePostLayoutBinding(*this, Layout);
803 
804  // Evaluate and apply the fixups, generating relocation entries as necessary.
805  for (MCSection &Sec : *this) {
806  for (MCFragment &Frag : Sec) {
807  // Data and relaxable fragments both have fixups. So only process
808  // those here.
809  // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups
810  // being templated makes this tricky.
811  if (isa<MCEncodedFragment>(&Frag) &&
812  isa<MCCompactEncodedInstFragment>(&Frag))
813  continue;
814  if (!isa<MCEncodedFragment>(&Frag) && !isa<MCCVDefRangeFragment>(&Frag) &&
815  !isa<MCAlignFragment>(&Frag))
816  continue;
818  MutableArrayRef<char> Contents;
819  const MCSubtargetInfo *STI = nullptr;
820  if (auto *FragWithFixups = dyn_cast<MCDataFragment>(&Frag)) {
821  Fixups = FragWithFixups->getFixups();
822  Contents = FragWithFixups->getContents();
823  STI = FragWithFixups->getSubtargetInfo();
824  assert(!FragWithFixups->hasInstructions() || STI != nullptr);
825  } else if (auto *FragWithFixups = dyn_cast<MCRelaxableFragment>(&Frag)) {
826  Fixups = FragWithFixups->getFixups();
827  Contents = FragWithFixups->getContents();
828  STI = FragWithFixups->getSubtargetInfo();
829  assert(!FragWithFixups->hasInstructions() || STI != nullptr);
830  } else if (auto *FragWithFixups = dyn_cast<MCCVDefRangeFragment>(&Frag)) {
831  Fixups = FragWithFixups->getFixups();
832  Contents = FragWithFixups->getContents();
833  } else if (auto *FragWithFixups = dyn_cast<MCDwarfLineAddrFragment>(&Frag)) {
834  Fixups = FragWithFixups->getFixups();
835  Contents = FragWithFixups->getContents();
836  } else if (auto *AF = dyn_cast<MCAlignFragment>(&Frag)) {
837  // Insert fixup type for code alignment if the target define
838  // shouldInsertFixupForCodeAlign target hook.
839  if (Sec.UseCodeAlign() && AF->hasEmitNops()) {
840  getBackend().shouldInsertFixupForCodeAlign(*this, Layout, *AF);
841  }
842  continue;
843  } else if (auto *FragWithFixups =
844  dyn_cast<MCDwarfCallFrameFragment>(&Frag)) {
845  Fixups = FragWithFixups->getFixups();
846  Contents = FragWithFixups->getContents();
847  } else
848  llvm_unreachable("Unknown fragment with fixups!");
849  for (const MCFixup &Fixup : Fixups) {
850  uint64_t FixedValue;
851  bool IsResolved;
852  MCValue Target;
853  std::tie(Target, FixedValue, IsResolved) =
854  handleFixup(Layout, Frag, Fixup);
855  getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue,
856  IsResolved, STI);
857  }
858  }
859  }
860 }
861 
863  // Create the layout object.
864  MCAsmLayout Layout(*this);
865  layout(Layout);
866 
867  // Write the object file.
868  stats::ObjectBytes += getWriter().writeObject(*this, Layout);
869 }
870 
871 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
872  const MCRelaxableFragment *DF,
873  const MCAsmLayout &Layout) const {
874  assert(getBackendPtr() && "Expected assembler backend");
875  MCValue Target;
876  uint64_t Value;
877  bool WasForced;
878  bool Resolved = evaluateFixup(Layout, Fixup, DF, Target, Value, WasForced);
879  if (Target.getSymA() &&
881  Fixup.getKind() == FK_Data_1)
882  return false;
883  return getBackend().fixupNeedsRelaxationAdvanced(Fixup, Resolved, Value, DF,
884  Layout, WasForced);
885 }
886 
887 bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
888  const MCAsmLayout &Layout) const {
889  assert(getBackendPtr() && "Expected assembler backend");
890  // If this inst doesn't ever need relaxation, ignore it. This occurs when we
891  // are intentionally pushing out inst fragments, or because we relaxed a
892  // previous instruction to one that doesn't need relaxation.
893  if (!getBackend().mayNeedRelaxation(F->getInst(), *F->getSubtargetInfo()))
894  return false;
895 
896  for (const MCFixup &Fixup : F->getFixups())
897  if (fixupNeedsRelaxation(Fixup, F, Layout))
898  return true;
899 
900  return false;
901 }
902 
903 bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
904  MCRelaxableFragment &F) {
905  assert(getEmitterPtr() &&
906  "Expected CodeEmitter defined for relaxInstruction");
907  if (!fragmentNeedsRelaxation(&F, Layout))
908  return false;
909 
910  ++stats::RelaxedInstructions;
911 
912  // FIXME-PERF: We could immediately lower out instructions if we can tell
913  // they are fully resolved, to avoid retesting on later passes.
914 
915  // Relax the fragment.
916 
917  MCInst Relaxed;
918  getBackend().relaxInstruction(F.getInst(), *F.getSubtargetInfo(), Relaxed);
919 
920  // Encode the new instruction.
921  //
922  // FIXME-PERF: If it matters, we could let the target do this. It can
923  // probably do so more efficiently in many cases.
925  SmallString<256> Code;
926  raw_svector_ostream VecOS(Code);
927  getEmitter().encodeInstruction(Relaxed, VecOS, Fixups, *F.getSubtargetInfo());
928 
929  // Update the fragment.
930  F.setInst(Relaxed);
931  F.getContents() = Code;
932  F.getFixups() = Fixups;
933 
934  return true;
935 }
936 
937 bool MCAssembler::relaxPaddingFragment(MCAsmLayout &Layout,
938  MCPaddingFragment &PF) {
939  assert(getBackendPtr() && "Expected assembler backend");
940  uint64_t OldSize = PF.getSize();
941  if (!getBackend().relaxFragment(&PF, Layout))
942  return false;
943  uint64_t NewSize = PF.getSize();
944 
945  ++stats::PaddingFragmentsRelaxations;
946  stats::PaddingFragmentsBytes += NewSize;
947  stats::PaddingFragmentsBytes -= OldSize;
948  return true;
949 }
950 
951 bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
952  uint64_t OldSize = LF.getContents().size();
953  int64_t Value;
954  bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout);
955  if (!Abs)
956  report_fatal_error("sleb128 and uleb128 expressions must be absolute");
958  Data.clear();
959  raw_svector_ostream OSE(Data);
960  // The compiler can generate EH table assembly that is impossible to assemble
961  // without either adding padding to an LEB fragment or adding extra padding
962  // to a later alignment fragment. To accommodate such tables, relaxation can
963  // only increase an LEB fragment size here, not decrease it. See PR35809.
964  if (LF.isSigned())
965  encodeSLEB128(Value, OSE, OldSize);
966  else
967  encodeULEB128(Value, OSE, OldSize);
968  return OldSize != LF.getContents().size();
969 }
970 
971 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
973  MCContext &Context = Layout.getAssembler().getContext();
974  uint64_t OldSize = DF.getContents().size();
975  int64_t AddrDelta;
976  bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
977  assert(Abs && "We created a line delta with an invalid expression");
978  (void)Abs;
979  int64_t LineDelta;
980  LineDelta = DF.getLineDelta();
982  Data.clear();
983  raw_svector_ostream OSE(Data);
984  DF.getFixups().clear();
985 
986  if (!getBackend().requiresDiffExpressionRelocations()) {
987  MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta,
988  AddrDelta, OSE);
989  } else {
991  uint32_t Size;
992  bool SetDelta = MCDwarfLineAddr::FixedEncode(Context,
994  LineDelta, AddrDelta,
995  OSE, &Offset, &Size);
996  // Add Fixups for address delta or new address.
997  const MCExpr *FixupExpr;
998  if (SetDelta) {
999  FixupExpr = &DF.getAddrDelta();
1000  } else {
1001  const MCBinaryExpr *ABE = cast<MCBinaryExpr>(&DF.getAddrDelta());
1002  FixupExpr = ABE->getLHS();
1003  }
1004  DF.getFixups().push_back(
1005  MCFixup::create(Offset, FixupExpr,
1006  MCFixup::getKindForSize(Size, false /*isPCRel*/)));
1007  }
1008 
1009  return OldSize != Data.size();
1010 }
1011 
1012 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
1014  MCContext &Context = Layout.getAssembler().getContext();
1015  uint64_t OldSize = DF.getContents().size();
1016  int64_t AddrDelta;
1017  bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
1018  assert(Abs && "We created call frame with an invalid expression");
1019  (void) Abs;
1021  Data.clear();
1022  raw_svector_ostream OSE(Data);
1023  DF.getFixups().clear();
1024 
1025  if (getBackend().requiresDiffExpressionRelocations()) {
1026  uint32_t Offset;
1027  uint32_t Size;
1028  MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE, &Offset,
1029  &Size);
1030  if (Size) {
1031  DF.getFixups().push_back(MCFixup::create(
1032  Offset, &DF.getAddrDelta(),
1033  MCFixup::getKindForSizeInBits(Size /*In bits.*/, false /*isPCRel*/)));
1034  }
1035  } else {
1036  MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE);
1037  }
1038 
1039  return OldSize != Data.size();
1040 }
1041 
1042 bool MCAssembler::relaxCVInlineLineTable(MCAsmLayout &Layout,
1044  unsigned OldSize = F.getContents().size();
1046  return OldSize != F.getContents().size();
1047 }
1048 
1049 bool MCAssembler::relaxCVDefRange(MCAsmLayout &Layout,
1050  MCCVDefRangeFragment &F) {
1051  unsigned OldSize = F.getContents().size();
1052  getContext().getCVContext().encodeDefRange(Layout, F);
1053  return OldSize != F.getContents().size();
1054 }
1055 
1056 bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) {
1057  // Holds the first fragment which needed relaxing during this layout. It will
1058  // remain NULL if none were relaxed.
1059  // When a fragment is relaxed, all the fragments following it should get
1060  // invalidated because their offset is going to change.
1061  MCFragment *FirstRelaxedFragment = nullptr;
1062 
1063  // Attempt to relax all the fragments in the section.
1064  for (MCSection::iterator I = Sec.begin(), IE = Sec.end(); I != IE; ++I) {
1065  // Check if this is a fragment that needs relaxation.
1066  bool RelaxedFrag = false;
1067  switch(I->getKind()) {
1068  default:
1069  break;
1071  assert(!getRelaxAll() &&
1072  "Did not expect a MCRelaxableFragment in RelaxAll mode");
1073  RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
1074  break;
1075  case MCFragment::FT_Dwarf:
1076  RelaxedFrag = relaxDwarfLineAddr(Layout,
1077  *cast<MCDwarfLineAddrFragment>(I));
1078  break;
1080  RelaxedFrag =
1081  relaxDwarfCallFrameFragment(Layout,
1082  *cast<MCDwarfCallFrameFragment>(I));
1083  break;
1084  case MCFragment::FT_LEB:
1085  RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
1086  break;
1088  RelaxedFrag = relaxPaddingFragment(Layout, *cast<MCPaddingFragment>(I));
1089  break;
1091  RelaxedFrag =
1092  relaxCVInlineLineTable(Layout, *cast<MCCVInlineLineTableFragment>(I));
1093  break;
1095  RelaxedFrag = relaxCVDefRange(Layout, *cast<MCCVDefRangeFragment>(I));
1096  break;
1097  }
1098  if (RelaxedFrag && !FirstRelaxedFragment)
1099  FirstRelaxedFragment = &*I;
1100  }
1101  if (FirstRelaxedFragment) {
1102  Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
1103  return true;
1104  }
1105  return false;
1106 }
1107 
1108 bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
1109  ++stats::RelaxationSteps;
1110 
1111  bool WasRelaxed = false;
1112  for (iterator it = begin(), ie = end(); it != ie; ++it) {
1113  MCSection &Sec = *it;
1114  while (layoutSectionOnce(Layout, Sec))
1115  WasRelaxed = true;
1116  }
1117 
1118  return WasRelaxed;
1119 }
1120 
1121 void MCAssembler::finishLayout(MCAsmLayout &Layout) {
1122  assert(getBackendPtr() && "Expected assembler backend");
1123  // The layout is done. Mark every fragment as valid.
1124  for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
1125  MCSection &Section = *Layout.getSectionOrder()[i];
1126  Layout.getFragmentOffset(&*Section.rbegin());
1127  computeFragmentSize(Layout, *Section.rbegin());
1128  }
1129  getBackend().finishLayout(*this, Layout);
1130 }
1131 
1132 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1134  raw_ostream &OS = errs();
1135 
1136  OS << "<MCAssembler\n";
1137  OS << " Sections:[\n ";
1138  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
1139  if (it != begin()) OS << ",\n ";
1140  it->dump();
1141  }
1142  OS << "],\n";
1143  OS << " Symbols:[";
1144 
1145  for (const_symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1146  if (it != symbol_begin()) OS << ",\n ";
1147  OS << "(";
1148  it->dump();
1149  OS << ", Index:" << it->getIndex() << ", ";
1150  OS << ")";
1151  }
1152  OS << "]>\n";
1153 }
1154 #endif
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:318
virtual bool shouldInsertExtraNopBytesForCodeAlign(const MCAlignFragment &AF, unsigned &Size)
Hook to check if extra nop bytes must be inserted for alignment directive.
Definition: MCAsmBackend.h:93
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
bool alignToBundleEnd() const
Should this fragment be placed at the end of an aligned bundle?
Definition: MCFragment.h:158
Fragment for adding required padding.
Definition: MCFragment.h:342
void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F)
Definition: MCCodeView.cpp:608
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
uint32_t getIndex() const
Get the (implementation defined) index.
Definition: MCSymbol.h:313
LLVMContext & Context
static void writeFragment(raw_ostream &OS, const MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment &F)
Write the fragment F to the output file.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCObjectWriter * getWriterPtr() const
Definition: MCAssembler.h:290
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:297
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:473
This represents an "assembler immediate".
Definition: MCValue.h:39
const support::endianness Endian
Definition: MCAsmBackend.h:52
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:175
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
VariantKind getKind() const
Definition: MCExpr.h:346
virtual bool requiresDiffExpressionRelocations() const
Check whether the given target requires emitting differences of two symbols as a set of relocations...
Definition: MCAsmBackend.h:119
virtual void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, MCInst &Res) const =0
Relax the instruction in the given fragment to the next wider instruction.
uint8_t getValueSize() const
Definition: MCFragment.h:446
iterator begin()
Definition: MCAssembler.h:336
SmallString< 8 > & getContents()
Definition: MCFragment.h:620
void setLayoutOrder(unsigned Value)
Definition: MCSection.h:127
static bool FixedEncode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS, uint32_t *Offset, uint32_t *Size)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas using fixed length operands...
Definition: MCDwarf.cpp:742
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual bool shouldInsertFixupForCodeAlign(MCAssembler &Asm, const MCAsmLayout &Layout, MCAlignFragment &AF)
Hook which indicates if the target requires a fixup to be generated when handling an align directive ...
Definition: MCAsmBackend.h:100
virtual void executePostLayoutBinding(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
int64_t getValue() const
Definition: MCFragment.h:321
uint64_t getSize() const
Definition: MCFragment.h:420
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:572
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:52
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
FragmentType getKind() const
Definition: MCFragment.h:96
STATISTIC(NumFunctions, "Total number of functions")
F(f)
const MCExpr & getOffset() const
Definition: MCFragment.h:474
void dump() const
bool isBundlingEnabled() const
Definition: MCAssembler.h:323
static void Encode(MCContext &Context, MCDwarfLineTableParams Params, int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS)
Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
Definition: MCDwarf.cpp:660
symbol_iterator symbol_begin()
Definition: MCAssembler.h:347
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:325
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
MCContext & getContext() const
Definition: MCAssembler.h:284
int64_t getConstant() const
Definition: MCValue.h:46
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:48
Definition: BitVector.h:937
Interface implemented by fragments that contain encoded instructions and/or data. ...
Definition: MCFragment.h:127
virtual uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout)=0
Write the object file and returns the number of bytes written.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:298
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
bool isSymbolLinkerVisible(const MCSymbol &SD) const
Check whether a particular symbol is visible to the linker and is required in the symbol table...
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:294
virtual void encodeInstruction(const MCInst &Inst, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
EncodeInstruction - Encode the given Inst to bytes on the output stream OS.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:253
bool registerSection(MCSection &Section)
The access may reference the value stored in memory.
static MCFixup createSubFor(const MCFixup &Fixup)
Return a fixup corresponding to the sub half of a add/sub fixup pair for the given Fixup...
Definition: MCFixup.h:117
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:173
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:296
virtual bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
SMLoc getLoc() const
Definition: MCFragment.h:478
Context object for machine code objects.
Definition: MCContext.h:65
const MCExpr & getAddrDelta() const
Definition: MCFragment.h:539
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:672
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
const MCExpr & getAddrDelta() const
Definition: MCFragment.h:561
reverse_iterator rbegin()
Definition: MCSection.h:168
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
void layoutFragment(MCFragment *Fragment)
Perform layout for a single fragment, assuming that the previous fragment has already been laid out c...
iterator end()
Definition: MCAssembler.h:339
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:198
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:162
uint8_t getBundlePadding() const
Get the padding size that must be inserted before this fragment.
Definition: MCFragment.h:166
void encodeInlineLineTable(MCAsmLayout &Layout, MCCVInlineLineTableFragment &F)
Encodes the binary annotations once we have a layout.
Definition: MCCodeView.cpp:461
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:272
virtual bool writeNopData(raw_ostream &OS, uint64_t Count) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const
Definition: MCExpr.cpp:478
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:290
virtual void reset()
Lifetime management.
Definition: MCCodeEmitter.h:31
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void invalidateFragmentsFrom(MCFragment *F)
Invalidate the fragments starting with F because it has been resized.
Definition: MCFragment.cpp:51
virtual void reset()
lifetime management
bool isRegistered() const
Definition: MCSection.h:146
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:224
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
Definition: MCFragment.cpp:129
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const
True if the section is atomized using the symbols in it.
Definition: MCAsmInfo.cpp:77
const MCSymbol * getAtom(const MCSymbol &S) const
Find the symbol which defines the atom containing the given symbol, or null if there is no such symbo...
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition: MCSymbol.h:221
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:686
Should this fixup kind force a 4-byte aligned effective PC value?
const MCSymbol * getAtom() const
Definition: MCFragment.h:101
llvm::SmallVectorImpl< MCSection * > & getSectionOrder()
Definition: MCAsmLayout.h:65
void setIsRegistered(bool Value)
Definition: MCSection.h:147
uint32_t getOffset() const
Definition: MCFixup.h:128
void writeSectionData(raw_ostream &OS, const MCSection *Section, const MCAsmLayout &Layout) const
Emit the section contents to OS.
virtual bool UseCodeAlign() const =0
Return true if a .align directive should use "optimized nops" to fill instead of 0s.
Binary assembler expressions.
Definition: MCExpr.h:425
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:93
size_t size() const
Definition: SmallVector.h:52
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:631
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A one-byte fixup.
Definition: MCFixup.h:24
raw_ostream & write(unsigned char C)
void layout(MCAsmLayout &Layout)
const MCExpr & getValue() const
Definition: MCFragment.h:505
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:77
PowerPC TLS Dynamic Call Fixup
void setOrdinal(unsigned Value)
Definition: MCSection.h:124
SMLoc getLoc() const
Definition: MCFixup.h:195
Iterator for intrusive lists based on ilist_node.
unsigned getMaxBytesToEmit() const
Definition: MCFragment.h:325
bool hasEmitNops() const
Definition: MCFragment.h:327
MCAsmBackend * getBackendPtr() const
Definition: MCAssembler.h:286
void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F, uint64_t FSize) const
Write the necessary bundle padding to OS.
MCAssembler(MCContext &Context, std::unique_ptr< MCAsmBackend > Backend, std::unique_ptr< MCCodeEmitter > Emitter, std::unique_ptr< MCObjectWriter > Writer)
Construct a new assembler instance.
Definition: MCAssembler.cpp:85
virtual bool isVirtualSection() const =0
Check whether this section is "virtual", that is has no actual object file contents.
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:292
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
const MCSymbol & getSymbol() const
Definition: MCExpr.h:344
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:258
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:396
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
bool isRegistered() const
Definition: MCSymbol.h:211
Represents a symbol table index fragment.
Definition: MCFragment.h:571
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:286
virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
Record a relocation entry.
MCLOHContainer & getLOHContainer()
Definition: MCAssembler.h:423
void setBundlePadding(uint8_t N)
Set the padding size for this fragment.
Definition: MCFragment.h:170
uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCEncodedFragment *F, uint64_t FOffset, uint64_t FSize)
Compute the amount of padding required before the fragment F to obey bundling restrictions, where FOffset is the fragment&#39;s offset in its section and FSize is the fragment&#39;s size.
Definition: MCFragment.cpp:190
bool getRelaxAll() const
Definition: MCAssembler.h:320
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:249
Target - Wrapper for Target specific information.
bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
MCSection * getParent() const
Definition: MCFragment.h:98
uint64_t getValue() const
Definition: MCFragment.h:445
bool hasInstructions() const
Does this fragment have instructions emitted into it? By default this is false, but specific fragment...
Definition: MCFragment.h:109
const MCSubtargetInfo * getSubtargetInfo() const
Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
Definition: MCFragment.h:174
void setIsRegistered(bool Value) const
Definition: MCSymbol.h:212
Fragment representing the binary annotations produced by the .cv_inline_linetable directive...
Definition: MCFragment.h:593
bool isUsedInReloc() const
Definition: MCSymbol.h:215
unsigned getValueSize() const
Definition: MCFragment.h:323
unsigned getAlignment() const
Definition: MCFragment.h:319
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
int64_t getLineDelta() const
Definition: MCFragment.h:537
const MCInst & getInst() const
Definition: MCFragment.h:283
static MCFixup createAddFor(const MCFixup &Fixup)
Return a fixup corresponding to the add half of a add/sub fixup pair for the given Fixup...
Definition: MCFixup.h:106
static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, raw_ostream &OS, uint32_t *Offset=nullptr, uint32_t *Size=nullptr)
Definition: MCDwarf.cpp:1899
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:62
#define I(x, y, z)
Definition: MD5.cpp:58
static MCFixupKind getKindForSizeInBits(unsigned Size, bool IsPCRel)
Return the generic fixup kind for a value with the given size in bits.
Definition: MCFixup.h:151
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:46
bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const
Try to evaluate the expression to the form (a - b + constant) where neither a nor b are variables...
Definition: MCExpr.cpp:680
uint64_t computeFragmentSize(const MCAsmLayout &Layout, const MCFragment &F) const
Compute the effective fragment size assuming it is laid out at the given SectionAddress and FragmentO...
SmallString< 8 > & getContents()
Definition: MCFragment.h:509
const MCSymbol * getSymbol()
Definition: MCFragment.h:581
uint32_t getRefKind() const
Definition: MCValue.h:49
CodeViewContext & getCVContext()
Definition: MCContext.cpp:676
static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel)
Return the generic fixup kind for a value with the given size.
Definition: MCFixup.h:135
void reset()
Reuse an assembler instance.
Definition: MCAssembler.cpp:98
Fragment for data and encoded instructions.
Definition: MCFragment.h:243
uint64_t getSectionFileSize(const MCSection *Sec) const
Get the data size of the given section, as emitted to the object file.
Definition: MCFragment.cpp:181
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:302
bool isSigned() const
Definition: MCFragment.h:507
LLVM Value Representation.
Definition: Value.h:73
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: MathExtras.h:731
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:111
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
const MCExpr * getValue() const
Definition: MCFixup.h:131
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
iterator end()
Definition: MCSection.h:165
uint8_t getValue() const
Definition: MCFragment.h:476
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:55
void Finish()
Finish - Do final processing and write the object to the output stream.
MCSection::FragmentListType & getFragmentList()
Definition: MCSection.h:149
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
virtual void finishLayout(MCAssembler const &Asm, MCAsmLayout &Layout) const
Give backend an opportunity to finish layout after relaxation.
Definition: MCAsmBackend.h:171
void setInst(const MCInst &Value)
Definition: MCFragment.h:284
MCCodeEmitter * getEmitterPtr() const
Definition: MCAssembler.h:288
MCFixupKind getKind() const
Definition: MCFixup.h:126
iterator begin()
Definition: MCSection.h:162
symbol_iterator symbol_end()
Definition: MCAssembler.h:350