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