LLVM  8.0.0svn
MCAssembler.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCAssembler.cpp - Assembler Backend Implementation ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/MC/MCAssembler.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCCodeView.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCDwarf.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCFixup.h"
27 #include "llvm/MC/MCFragment.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCObjectWriter.h"
30 #include "llvm/MC/MCSection.h"
31 #include "llvm/MC/MCSectionELF.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCValue.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Debug.h"
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 
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  // If we are padding with nops, force the padding to be larger than the
326  // minimum nop size.
327  if (Size > 0 && AF.hasEmitNops()) {
328  while (Size % getBackend().getMinimumNopSize())
329  Size += AF.getAlignment();
330  }
331  if (Size > AF.getMaxBytesToEmit())
332  return 0;
333  return Size;
334  }
335 
336  case MCFragment::FT_Org: {
337  const MCOrgFragment &OF = cast<MCOrgFragment>(F);
338  MCValue Value;
339  if (!OF.getOffset().evaluateAsValue(Value, Layout)) {
341  "expected assembly-time absolute expression");
342  return 0;
343  }
344 
345  uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
346  int64_t TargetLocation = Value.getConstant();
347  if (const MCSymbolRefExpr *A = Value.getSymA()) {
348  uint64_t Val;
349  if (!Layout.getSymbolOffset(A->getSymbol(), Val)) {
350  getContext().reportError(OF.getLoc(), "expected absolute expression");
351  return 0;
352  }
353  TargetLocation += Val;
354  }
355  int64_t Size = TargetLocation - FragmentOffset;
356  if (Size < 0 || Size >= 0x40000000) {
358  OF.getLoc(), "invalid .org offset '" + Twine(TargetLocation) +
359  "' (at offset '" + Twine(FragmentOffset) + "')");
360  return 0;
361  }
362  return Size;
363  }
364 
366  return cast<MCDwarfLineAddrFragment>(F).getContents().size();
368  return cast<MCDwarfCallFrameFragment>(F).getContents().size();
370  return cast<MCCVInlineLineTableFragment>(F).getContents().size();
372  return cast<MCCVDefRangeFragment>(F).getContents().size();
374  llvm_unreachable("Should not have been added");
375  }
376 
377  llvm_unreachable("invalid fragment kind");
378 }
379 
381  MCFragment *Prev = F->getPrevNode();
382 
383  // We should never try to recompute something which is valid.
384  assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
385  // We should never try to compute the fragment layout if its predecessor
386  // isn't valid.
387  assert((!Prev || isFragmentValid(Prev)) &&
388  "Attempt to compute fragment before its predecessor!");
389 
390  ++stats::FragmentLayouts;
391 
392  // Compute fragment offset and size.
393  if (Prev)
394  F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
395  else
396  F->Offset = 0;
397  LastValidFragment[F->getParent()] = F;
398 
399  // If bundling is enabled and this fragment has instructions in it, it has to
400  // obey the bundling restrictions. With padding, we'll have:
401  //
402  //
403  // BundlePadding
404  // |||
405  // -------------------------------------
406  // Prev |##########| F |
407  // -------------------------------------
408  // ^
409  // |
410  // F->Offset
411  //
412  // The fragment's offset will point to after the padding, and its computed
413  // size won't include the padding.
414  //
415  // When the -mc-relax-all flag is used, we optimize bundling by writting the
416  // padding directly into fragments when the instructions are emitted inside
417  // the streamer. When the fragment is larger than the bundle size, we need to
418  // ensure that it's bundle aligned. This means that if we end up with
419  // multiple fragments, we must emit bundle padding between fragments.
420  //
421  // ".align N" is an example of a directive that introduces multiple
422  // fragments. We could add a special case to handle ".align N" by emitting
423  // within-fragment padding (which would produce less padding when N is less
424  // than the bundle size), but for now we don't.
425  //
426  if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
427  assert(isa<MCEncodedFragment>(F) &&
428  "Only MCEncodedFragment implementations have instructions");
429  MCEncodedFragment *EF = cast<MCEncodedFragment>(F);
430  uint64_t FSize = Assembler.computeFragmentSize(*this, *EF);
431 
432  if (!Assembler.getRelaxAll() && FSize > Assembler.getBundleAlignSize())
433  report_fatal_error("Fragment can't be larger than a bundle size");
434 
435  uint64_t RequiredBundlePadding =
436  computeBundlePadding(Assembler, EF, EF->Offset, FSize);
437  if (RequiredBundlePadding > UINT8_MAX)
438  report_fatal_error("Padding cannot exceed 255 bytes");
439  EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
440  EF->Offset += RequiredBundlePadding;
441  }
442 }
443 
444 void MCAssembler::registerSymbol(const MCSymbol &Symbol, bool *Created) {
445  bool New = !Symbol.isRegistered();
446  if (Created)
447  *Created = New;
448  if (New) {
449  Symbol.setIsRegistered(true);
450  Symbols.push_back(&Symbol);
451  }
452 }
453 
455  const MCEncodedFragment &EF,
456  uint64_t FSize) const {
457  assert(getBackendPtr() && "Expected assembler backend");
458  // Should NOP padding be written out before this fragment?
459  unsigned BundlePadding = EF.getBundlePadding();
460  if (BundlePadding > 0) {
462  "Writing bundle padding with disabled bundling");
463  assert(EF.hasInstructions() &&
464  "Writing bundle padding for a fragment without instructions");
465 
466  unsigned TotalLength = BundlePadding + static_cast<unsigned>(FSize);
467  if (EF.alignToBundleEnd() && TotalLength > getBundleAlignSize()) {
468  // If the padding itself crosses a bundle boundary, it must be emitted
469  // in 2 pieces, since even nop instructions must not cross boundaries.
470  // v--------------v <- BundleAlignSize
471  // v---------v <- BundlePadding
472  // ----------------------------
473  // | Prev |####|####| F |
474  // ----------------------------
475  // ^-------------------^ <- TotalLength
476  unsigned DistanceToBoundary = TotalLength - getBundleAlignSize();
477  if (!getBackend().writeNopData(OS, DistanceToBoundary))
478  report_fatal_error("unable to write NOP sequence of " +
479  Twine(DistanceToBoundary) + " bytes");
480  BundlePadding -= DistanceToBoundary;
481  }
482  if (!getBackend().writeNopData(OS, BundlePadding))
483  report_fatal_error("unable to write NOP sequence of " +
484  Twine(BundlePadding) + " bytes");
485  }
486 }
487 
488 /// Write the fragment \p F to the output file.
489 static void writeFragment(raw_ostream &OS, const MCAssembler &Asm,
490  const MCAsmLayout &Layout, const MCFragment &F) {
491  // FIXME: Embed in fragments instead?
492  uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
493 
494  support::endianness Endian = Asm.getBackend().Endian;
495 
496  if (const MCEncodedFragment *EF = dyn_cast<MCEncodedFragment>(&F))
497  Asm.writeFragmentPadding(OS, *EF, FragmentSize);
498 
499  // This variable (and its dummy usage) is to participate in the assert at
500  // the end of the function.
501  uint64_t Start = OS.tell();
502  (void) Start;
503 
504  ++stats::EmittedFragments;
505 
506  switch (F.getKind()) {
507  case MCFragment::FT_Align: {
508  ++stats::EmittedAlignFragments;
509  const MCAlignFragment &AF = cast<MCAlignFragment>(F);
510  assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
511 
512  uint64_t Count = FragmentSize / AF.getValueSize();
513 
514  // FIXME: This error shouldn't actually occur (the front end should emit
515  // multiple .align directives to enforce the semantics it wants), but is
516  // severe enough that we want to report it. How to handle this?
517  if (Count * AF.getValueSize() != FragmentSize)
518  report_fatal_error("undefined .align directive, value size '" +
519  Twine(AF.getValueSize()) +
520  "' is not a divisor of padding size '" +
521  Twine(FragmentSize) + "'");
522 
523  // See if we are aligning with nops, and if so do that first to try to fill
524  // the Count bytes. Then if that did not fill any bytes or there are any
525  // bytes left to fill use the Value and ValueSize to fill the rest.
526  // If we are aligning with nops, ask that target to emit the right data.
527  if (AF.hasEmitNops()) {
528  if (!Asm.getBackend().writeNopData(OS, Count))
529  report_fatal_error("unable to write nop sequence of " +
530  Twine(Count) + " bytes");
531  break;
532  }
533 
534  // Otherwise, write out in multiples of the value size.
535  for (uint64_t i = 0; i != Count; ++i) {
536  switch (AF.getValueSize()) {
537  default: llvm_unreachable("Invalid size!");
538  case 1: OS << char(AF.getValue()); break;
539  case 2:
540  support::endian::write<uint16_t>(OS, AF.getValue(), Endian);
541  break;
542  case 4:
543  support::endian::write<uint32_t>(OS, AF.getValue(), Endian);
544  break;
545  case 8:
546  support::endian::write<uint64_t>(OS, AF.getValue(), Endian);
547  break;
548  }
549  }
550  break;
551  }
552 
553  case MCFragment::FT_Data:
554  ++stats::EmittedDataFragments;
555  OS << cast<MCDataFragment>(F).getContents();
556  break;
557 
559  ++stats::EmittedRelaxableFragments;
560  OS << cast<MCRelaxableFragment>(F).getContents();
561  break;
562 
564  ++stats::EmittedCompactEncodedInstFragments;
565  OS << cast<MCCompactEncodedInstFragment>(F).getContents();
566  break;
567 
568  case MCFragment::FT_Fill: {
569  ++stats::EmittedFillFragments;
570  const MCFillFragment &FF = cast<MCFillFragment>(F);
571  uint64_t V = FF.getValue();
572  unsigned VSize = FF.getValueSize();
573  const unsigned MaxChunkSize = 16;
574  char Data[MaxChunkSize];
575  // Duplicate V into Data as byte vector to reduce number of
576  // writes done. As such, do endian conversion here.
577  for (unsigned I = 0; I != VSize; ++I) {
578  unsigned index = Endian == support::little ? I : (VSize - I - 1);
579  Data[I] = uint8_t(V >> (index * 8));
580  }
581  for (unsigned I = VSize; I < MaxChunkSize; ++I)
582  Data[I] = Data[I - VSize];
583 
584  // Set to largest multiple of VSize in Data.
585  const unsigned NumPerChunk = MaxChunkSize / VSize;
586  // Set ChunkSize to largest multiple of VSize in Data
587  const unsigned ChunkSize = VSize * NumPerChunk;
588 
589  // Do copies by chunk.
590  StringRef Ref(Data, ChunkSize);
591  for (uint64_t I = 0, E = FragmentSize / ChunkSize; I != E; ++I)
592  OS << Ref;
593 
594  // do remainder if needed.
595  unsigned TrailingCount = FragmentSize % ChunkSize;
596  if (TrailingCount)
597  OS.write(Data, TrailingCount);
598  break;
599  }
600 
601  case MCFragment::FT_LEB: {
602  const MCLEBFragment &LF = cast<MCLEBFragment>(F);
603  OS << LF.getContents();
604  break;
605  }
606 
607  case MCFragment::FT_Padding: {
608  if (!Asm.getBackend().writeNopData(OS, FragmentSize))
609  report_fatal_error("unable to write nop sequence of " +
610  Twine(FragmentSize) + " bytes");
611  break;
612  }
613 
615  const MCSymbolIdFragment &SF = cast<MCSymbolIdFragment>(F);
616  support::endian::write<uint32_t>(OS, SF.getSymbol()->getIndex(), Endian);
617  break;
618  }
619 
620  case MCFragment::FT_Org: {
621  ++stats::EmittedOrgFragments;
622  const MCOrgFragment &OF = cast<MCOrgFragment>(F);
623 
624  for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
625  OS << char(OF.getValue());
626 
627  break;
628  }
629 
630  case MCFragment::FT_Dwarf: {
631  const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
632  OS << OF.getContents();
633  break;
634  }
636  const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
637  OS << CF.getContents();
638  break;
639  }
641  const auto &OF = cast<MCCVInlineLineTableFragment>(F);
642  OS << OF.getContents();
643  break;
644  }
646  const auto &DRF = cast<MCCVDefRangeFragment>(F);
647  OS << DRF.getContents();
648  break;
649  }
651  llvm_unreachable("Should not have been added");
652  }
653 
654  assert(OS.tell() - Start == FragmentSize &&
655  "The stream should advance by fragment size");
656 }
657 
659  const MCAsmLayout &Layout) const {
660  assert(getBackendPtr() && "Expected assembler backend");
661 
662  // Ignore virtual sections.
663  if (Sec->isVirtualSection()) {
664  assert(Layout.getSectionFileSize(Sec) == 0 && "Invalid size for section!");
665 
666  // Check that contents are only things legal inside a virtual section.
667  for (const MCFragment &F : *Sec) {
668  switch (F.getKind()) {
669  default: llvm_unreachable("Invalid fragment in virtual section!");
670  case MCFragment::FT_Data: {
671  // Check that we aren't trying to write a non-zero contents (or fixups)
672  // into a virtual section. This is to support clients which use standard
673  // directives to fill the contents of virtual sections.
674  const MCDataFragment &DF = cast<MCDataFragment>(F);
675  if (DF.fixup_begin() != DF.fixup_end())
676  report_fatal_error("cannot have fixups in virtual section!");
677  for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
678  if (DF.getContents()[i]) {
679  if (auto *ELFSec = dyn_cast<const MCSectionELF>(Sec))
680  report_fatal_error("non-zero initializer found in section '" +
681  ELFSec->getSectionName() + "'");
682  else
683  report_fatal_error("non-zero initializer found in virtual section");
684  }
685  break;
686  }
688  // Check that we aren't trying to write a non-zero value into a virtual
689  // section.
690  assert((cast<MCAlignFragment>(F).getValueSize() == 0 ||
691  cast<MCAlignFragment>(F).getValue() == 0) &&
692  "Invalid align in virtual section!");
693  break;
694  case MCFragment::FT_Fill:
695  assert((cast<MCFillFragment>(F).getValue() == 0) &&
696  "Invalid fill in virtual section!");
697  break;
698  }
699  }
700 
701  return;
702  }
703 
704  uint64_t Start = OS.tell();
705  (void)Start;
706 
707  for (const MCFragment &F : *Sec)
708  writeFragment(OS, *this, Layout, F);
709 
710  assert(OS.tell() - Start == Layout.getSectionAddressSize(Sec));
711 }
712 
713 std::tuple<MCValue, uint64_t, bool>
714 MCAssembler::handleFixup(const MCAsmLayout &Layout, MCFragment &F,
715  const MCFixup &Fixup) {
716  // Evaluate the fixup.
717  MCValue Target;
718  uint64_t FixedValue;
719  bool WasForced;
720  bool IsResolved = evaluateFixup(Layout, Fixup, &F, Target, FixedValue,
721  WasForced);
722  if (!IsResolved) {
723  // The fixup was unresolved, we need a relocation. Inform the object
724  // writer of the relocation, and give it an opportunity to adjust the
725  // fixup value if need be.
726  if (Target.getSymA() && Target.getSymB() &&
728  // The fixup represents the difference between two symbols, which the
729  // backend has indicated must be resolved at link time. Split up the fixup
730  // into two relocations, one for the add, and one for the sub, and emit
731  // both of these. The constant will be associated with the add half of the
732  // expression.
733  MCFixup FixupAdd = MCFixup::createAddFor(Fixup);
734  MCValue TargetAdd =
735  MCValue::get(Target.getSymA(), nullptr, Target.getConstant());
736  getWriter().recordRelocation(*this, Layout, &F, FixupAdd, TargetAdd,
737  FixedValue);
738  MCFixup FixupSub = MCFixup::createSubFor(Fixup);
739  MCValue TargetSub = MCValue::get(Target.getSymB());
740  getWriter().recordRelocation(*this, Layout, &F, FixupSub, TargetSub,
741  FixedValue);
742  } else {
743  getWriter().recordRelocation(*this, Layout, &F, Fixup, Target,
744  FixedValue);
745  }
746  }
747  return std::make_tuple(Target, FixedValue, IsResolved);
748 }
749 
751  assert(getBackendPtr() && "Expected assembler backend");
752  DEBUG_WITH_TYPE("mc-dump", {
753  errs() << "assembler backend - pre-layout\n--\n";
754  dump(); });
755 
756  // Create dummy fragments and assign section ordinals.
757  unsigned SectionIndex = 0;
758  for (MCSection &Sec : *this) {
759  // Create dummy fragments to eliminate any empty sections, this simplifies
760  // layout.
761  if (Sec.getFragmentList().empty())
762  new MCDataFragment(&Sec);
763 
764  Sec.setOrdinal(SectionIndex++);
765  }
766 
767  // Assign layout order indices to sections and fragments.
768  for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
769  MCSection *Sec = Layout.getSectionOrder()[i];
770  Sec->setLayoutOrder(i);
771 
772  unsigned FragmentIndex = 0;
773  for (MCFragment &Frag : *Sec)
774  Frag.setLayoutOrder(FragmentIndex++);
775  }
776 
777  // Layout until everything fits.
778  while (layoutOnce(Layout))
779  if (getContext().hadError())
780  return;
781 
782  DEBUG_WITH_TYPE("mc-dump", {
783  errs() << "assembler backend - post-relaxation\n--\n";
784  dump(); });
785 
786  // Finalize the layout, including fragment lowering.
787  finishLayout(Layout);
788 
789  DEBUG_WITH_TYPE("mc-dump", {
790  errs() << "assembler backend - final-layout\n--\n";
791  dump(); });
792 
793  // Allow the object writer a chance to perform post-layout binding (for
794  // example, to set the index fields in the symbol data).
795  getWriter().executePostLayoutBinding(*this, Layout);
796 
797  // Evaluate and apply the fixups, generating relocation entries as necessary.
798  for (MCSection &Sec : *this) {
799  for (MCFragment &Frag : Sec) {
800  // Data and relaxable fragments both have fixups. So only process
801  // those here.
802  // FIXME: Is there a better way to do this? MCEncodedFragmentWithFixups
803  // being templated makes this tricky.
804  if (isa<MCEncodedFragment>(&Frag) &&
805  isa<MCCompactEncodedInstFragment>(&Frag))
806  continue;
807  if (!isa<MCEncodedFragment>(&Frag) && !isa<MCCVDefRangeFragment>(&Frag))
808  continue;
810  MutableArrayRef<char> Contents;
811  const MCSubtargetInfo *STI = nullptr;
812  if (auto *FragWithFixups = dyn_cast<MCDataFragment>(&Frag)) {
813  Fixups = FragWithFixups->getFixups();
814  Contents = FragWithFixups->getContents();
815  STI = FragWithFixups->getSubtargetInfo();
816  assert(!FragWithFixups->hasInstructions() || STI != nullptr);
817  } else if (auto *FragWithFixups = dyn_cast<MCRelaxableFragment>(&Frag)) {
818  Fixups = FragWithFixups->getFixups();
819  Contents = FragWithFixups->getContents();
820  STI = FragWithFixups->getSubtargetInfo();
821  assert(!FragWithFixups->hasInstructions() || STI != nullptr);
822  } else if (auto *FragWithFixups = dyn_cast<MCCVDefRangeFragment>(&Frag)) {
823  Fixups = FragWithFixups->getFixups();
824  Contents = FragWithFixups->getContents();
825  } else if (auto *FragWithFixups = dyn_cast<MCDwarfLineAddrFragment>(&Frag)) {
826  Fixups = FragWithFixups->getFixups();
827  Contents = FragWithFixups->getContents();
828  } else
829  llvm_unreachable("Unknown fragment with fixups!");
830  for (const MCFixup &Fixup : Fixups) {
831  uint64_t FixedValue;
832  bool IsResolved;
833  MCValue Target;
834  std::tie(Target, FixedValue, IsResolved) =
835  handleFixup(Layout, Frag, Fixup);
836  getBackend().applyFixup(*this, Fixup, Target, Contents, FixedValue,
837  IsResolved, STI);
838  }
839  }
840  }
841 }
842 
844  // Create the layout object.
845  MCAsmLayout Layout(*this);
846  layout(Layout);
847 
848  // Write the object file.
849  stats::ObjectBytes += getWriter().writeObject(*this, Layout);
850 }
851 
852 bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
853  const MCRelaxableFragment *DF,
854  const MCAsmLayout &Layout) const {
855  assert(getBackendPtr() && "Expected assembler backend");
856  MCValue Target;
857  uint64_t Value;
858  bool WasForced;
859  bool Resolved = evaluateFixup(Layout, Fixup, DF, Target, Value, WasForced);
860  if (Target.getSymA() &&
862  Fixup.getKind() == FK_Data_1)
863  return false;
864  return getBackend().fixupNeedsRelaxationAdvanced(Fixup, Resolved, Value, DF,
865  Layout, WasForced);
866 }
867 
868 bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
869  const MCAsmLayout &Layout) const {
870  assert(getBackendPtr() && "Expected assembler backend");
871  // If this inst doesn't ever need relaxation, ignore it. This occurs when we
872  // are intentionally pushing out inst fragments, or because we relaxed a
873  // previous instruction to one that doesn't need relaxation.
874  if (!getBackend().mayNeedRelaxation(F->getInst(), *F->getSubtargetInfo()))
875  return false;
876 
877  for (const MCFixup &Fixup : F->getFixups())
878  if (fixupNeedsRelaxation(Fixup, F, Layout))
879  return true;
880 
881  return false;
882 }
883 
884 bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
885  MCRelaxableFragment &F) {
886  assert(getEmitterPtr() &&
887  "Expected CodeEmitter defined for relaxInstruction");
888  if (!fragmentNeedsRelaxation(&F, Layout))
889  return false;
890 
891  ++stats::RelaxedInstructions;
892 
893  // FIXME-PERF: We could immediately lower out instructions if we can tell
894  // they are fully resolved, to avoid retesting on later passes.
895 
896  // Relax the fragment.
897 
898  MCInst Relaxed;
899  getBackend().relaxInstruction(F.getInst(), *F.getSubtargetInfo(), Relaxed);
900 
901  // Encode the new instruction.
902  //
903  // FIXME-PERF: If it matters, we could let the target do this. It can
904  // probably do so more efficiently in many cases.
906  SmallString<256> Code;
907  raw_svector_ostream VecOS(Code);
908  getEmitter().encodeInstruction(Relaxed, VecOS, Fixups, *F.getSubtargetInfo());
909 
910  // Update the fragment.
911  F.setInst(Relaxed);
912  F.getContents() = Code;
913  F.getFixups() = Fixups;
914 
915  return true;
916 }
917 
918 bool MCAssembler::relaxPaddingFragment(MCAsmLayout &Layout,
919  MCPaddingFragment &PF) {
920  assert(getBackendPtr() && "Expected assembler backend");
921  uint64_t OldSize = PF.getSize();
922  if (!getBackend().relaxFragment(&PF, Layout))
923  return false;
924  uint64_t NewSize = PF.getSize();
925 
926  ++stats::PaddingFragmentsRelaxations;
927  stats::PaddingFragmentsBytes += NewSize;
928  stats::PaddingFragmentsBytes -= OldSize;
929  return true;
930 }
931 
932 bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
933  uint64_t OldSize = LF.getContents().size();
934  int64_t Value;
935  bool Abs = LF.getValue().evaluateKnownAbsolute(Value, Layout);
936  if (!Abs)
937  report_fatal_error("sleb128 and uleb128 expressions must be absolute");
939  Data.clear();
940  raw_svector_ostream OSE(Data);
941  // The compiler can generate EH table assembly that is impossible to assemble
942  // without either adding padding to an LEB fragment or adding extra padding
943  // to a later alignment fragment. To accommodate such tables, relaxation can
944  // only increase an LEB fragment size here, not decrease it. See PR35809.
945  if (LF.isSigned())
946  encodeSLEB128(Value, OSE, OldSize);
947  else
948  encodeULEB128(Value, OSE, OldSize);
949  return OldSize != LF.getContents().size();
950 }
951 
952 bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
954  MCContext &Context = Layout.getAssembler().getContext();
955  uint64_t OldSize = DF.getContents().size();
956  int64_t AddrDelta;
957  bool Abs;
958  if (getBackend().requiresDiffExpressionRelocations())
959  Abs = DF.getAddrDelta().evaluateAsAbsolute(AddrDelta, Layout);
960  else {
961  Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
962  assert(Abs && "We created a line delta with an invalid expression");
963  }
964  int64_t LineDelta;
965  LineDelta = DF.getLineDelta();
967  Data.clear();
968  raw_svector_ostream OSE(Data);
969  DF.getFixups().clear();
970 
971  if (Abs) {
972  MCDwarfLineAddr::Encode(Context, getDWARFLinetableParams(), LineDelta,
973  AddrDelta, OSE);
974  } else {
976  uint32_t Size;
977  bool SetDelta = MCDwarfLineAddr::FixedEncode(Context,
979  LineDelta, AddrDelta,
980  OSE, &Offset, &Size);
981  // Add Fixups for address delta or new address.
982  const MCExpr *FixupExpr;
983  if (SetDelta) {
984  FixupExpr = &DF.getAddrDelta();
985  } else {
986  const MCBinaryExpr *ABE = cast<MCBinaryExpr>(&DF.getAddrDelta());
987  FixupExpr = ABE->getLHS();
988  }
989  DF.getFixups().push_back(
990  MCFixup::create(Offset, FixupExpr,
991  MCFixup::getKindForSize(Size, false /*isPCRel*/)));
992  }
993 
994  return OldSize != Data.size();
995 }
996 
997 bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
999  MCContext &Context = Layout.getAssembler().getContext();
1000  uint64_t OldSize = DF.getContents().size();
1001  int64_t AddrDelta;
1002  bool Abs = DF.getAddrDelta().evaluateKnownAbsolute(AddrDelta, Layout);
1003  assert(Abs && "We created call frame with an invalid expression");
1004  (void) Abs;
1006  Data.clear();
1007  raw_svector_ostream OSE(Data);
1008  MCDwarfFrameEmitter::EncodeAdvanceLoc(Context, AddrDelta, OSE);
1009  return OldSize != Data.size();
1010 }
1011 
1012 bool MCAssembler::relaxCVInlineLineTable(MCAsmLayout &Layout,
1014  unsigned OldSize = F.getContents().size();
1016  return OldSize != F.getContents().size();
1017 }
1018 
1019 bool MCAssembler::relaxCVDefRange(MCAsmLayout &Layout,
1020  MCCVDefRangeFragment &F) {
1021  unsigned OldSize = F.getContents().size();
1022  getContext().getCVContext().encodeDefRange(Layout, F);
1023  return OldSize != F.getContents().size();
1024 }
1025 
1026 bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec) {
1027  // Holds the first fragment which needed relaxing during this layout. It will
1028  // remain NULL if none were relaxed.
1029  // When a fragment is relaxed, all the fragments following it should get
1030  // invalidated because their offset is going to change.
1031  MCFragment *FirstRelaxedFragment = nullptr;
1032 
1033  // Attempt to relax all the fragments in the section.
1034  for (MCSection::iterator I = Sec.begin(), IE = Sec.end(); I != IE; ++I) {
1035  // Check if this is a fragment that needs relaxation.
1036  bool RelaxedFrag = false;
1037  switch(I->getKind()) {
1038  default:
1039  break;
1041  assert(!getRelaxAll() &&
1042  "Did not expect a MCRelaxableFragment in RelaxAll mode");
1043  RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
1044  break;
1045  case MCFragment::FT_Dwarf:
1046  RelaxedFrag = relaxDwarfLineAddr(Layout,
1047  *cast<MCDwarfLineAddrFragment>(I));
1048  break;
1050  RelaxedFrag =
1051  relaxDwarfCallFrameFragment(Layout,
1052  *cast<MCDwarfCallFrameFragment>(I));
1053  break;
1054  case MCFragment::FT_LEB:
1055  RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
1056  break;
1058  RelaxedFrag = relaxPaddingFragment(Layout, *cast<MCPaddingFragment>(I));
1059  break;
1061  RelaxedFrag =
1062  relaxCVInlineLineTable(Layout, *cast<MCCVInlineLineTableFragment>(I));
1063  break;
1065  RelaxedFrag = relaxCVDefRange(Layout, *cast<MCCVDefRangeFragment>(I));
1066  break;
1067  }
1068  if (RelaxedFrag && !FirstRelaxedFragment)
1069  FirstRelaxedFragment = &*I;
1070  }
1071  if (FirstRelaxedFragment) {
1072  Layout.invalidateFragmentsFrom(FirstRelaxedFragment);
1073  return true;
1074  }
1075  return false;
1076 }
1077 
1078 bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
1079  ++stats::RelaxationSteps;
1080 
1081  bool WasRelaxed = false;
1082  for (iterator it = begin(), ie = end(); it != ie; ++it) {
1083  MCSection &Sec = *it;
1084  while (layoutSectionOnce(Layout, Sec))
1085  WasRelaxed = true;
1086  }
1087 
1088  return WasRelaxed;
1089 }
1090 
1091 void MCAssembler::finishLayout(MCAsmLayout &Layout) {
1092  assert(getBackendPtr() && "Expected assembler backend");
1093  // The layout is done. Mark every fragment as valid.
1094  for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
1095  MCSection &Section = *Layout.getSectionOrder()[i];
1096  Layout.getFragmentOffset(&*Section.rbegin());
1097  computeFragmentSize(Layout, *Section.rbegin());
1098  }
1099  getBackend().finishLayout(*this, Layout);
1100 }
1101 
1102 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1104  raw_ostream &OS = errs();
1105 
1106  OS << "<MCAssembler\n";
1107  OS << " Sections:[\n ";
1108  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
1109  if (it != begin()) OS << ",\n ";
1110  it->dump();
1111  }
1112  OS << "],\n";
1113  OS << " Symbols:[";
1114 
1115  for (const_symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
1116  if (it != symbol_begin()) OS << ",\n ";
1117  OS << "(";
1118  it->dump();
1119  OS << ", Index:" << it->getIndex() << ", ";
1120  OS << ")";
1121  }
1122  OS << "]>\n";
1123 }
1124 #endif
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:293
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
Fragment for adding required padding.
Definition: MCFragment.h:341
void encodeDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &F)
Definition: MCCodeView.cpp:609
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:310
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
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
MCObjectWriter * getWriterPtr() const
Definition: MCAssembler.h:284
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:294
This represents an "assembler immediate".
Definition: MCValue.h:40
const support::endianness Endian
Definition: MCAsmBackend.h:53
uint64_t getSectionAddressSize(const MCSection *Sec) const
Get the address space size of the given section, as it effects layout.
Definition: MCFragment.cpp:176
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
VariantKind getKind() const
Definition: MCExpr.h:337
virtual bool requiresDiffExpressionRelocations() const
Check whether the given target requires emitting differences of two symbols as a set of relocations...
Definition: MCAsmBackend.h:104
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:445
iterator begin()
Definition: MCAssembler.h:330
SmallString< 8 > & getContents()
Definition: MCFragment.h:625
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:731
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
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:320
uint64_t getSize() const
Definition: MCFragment.h:419
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:563
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:53
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:510
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
static MCFixupKind getKindForSize(unsigned Size, bool isPCRel)
Return the generic fixup kind for a value with the given size.
Definition: MCFixup.h:132
FragmentType getKind() const
Definition: MCFragment.h:97
STATISTIC(NumFunctions, "Total number of functions")
F(f)
const MCExpr & getOffset() const
Definition: MCFragment.h:473
void dump() const
bool isBundlingEnabled() const
Definition: MCAssembler.h:317
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:649
symbol_iterator symbol_begin()
Definition: MCAssembler.h:341
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
unsigned getBundleAlignSize() const
Definition: MCAssembler.h:319
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:65
MCContext & getContext() const
Definition: MCAssembler.h:278
int64_t getConstant() const
Definition: MCValue.h:47
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:49
Definition: BitVector.h:938
Interface implemented by fragments that contain encoded instructions and/or data. ...
Definition: MCFragment.h:128
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:81
MCDwarfLineTableParams getDWARFLinetableParams() const
Definition: MCAssembler.h:292
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
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:288
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:36
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:252
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:114
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:166
MCObjectWriter & getWriter() const
Definition: MCAssembler.h:290
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
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:477
Context object for machine code objects.
Definition: MCContext.h:63
const MCExpr & getAddrDelta() const
Definition: MCFragment.h:538
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:634
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
const MCExpr & getAddrDelta() const
Definition: MCFragment.h:563
reverse_iterator rbegin()
Definition: MCSection.h:169
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:51
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:333
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:198
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
virtual unsigned getMinimumNopSize() const
Returns the minimum size of a nop in bytes on this target.
Definition: MCAsmBackend.h:147
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:462
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:271
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:467
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:291
virtual void reset()
Lifetime management.
Definition: MCCodeEmitter.h:32
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:52
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:130
virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const
True if the section is atomized using the symbols in it.
Definition: MCAsmInfo.cpp:74
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:220
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:48
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:612
Should this fixup kind force a 4-byte aligned effective PC value?
const MCSymbol * getAtom() const
Definition: MCFragment.h:102
llvm::SmallVectorImpl< MCSection * > & getSectionOrder()
Definition: MCAsmLayout.h:66
void setIsRegistered(bool Value)
Definition: MCSection.h:148
uint32_t getOffset() const
Definition: MCFixup.h:125
void writeSectionData(raw_ostream &OS, const MCSection *Section, const MCAsmLayout &Layout) const
Emit the section contents to OS.
Binary assembler expressions.
Definition: MCExpr.h:416
static MCFixup create(uint32_t Offset, const MCExpr *Value, MCFixupKind Kind, SMLoc Loc=SMLoc())
Definition: MCFixup.h:90
size_t size() const
Definition: SmallVector.h:53
Fragment representing the .cv_def_range directive.
Definition: MCFragment.h:636
#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:504
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:78
PowerPC TLS Dynamic Call Fixup
void setOrdinal(unsigned Value)
Definition: MCSection.h:125
SMLoc getLoc() const
Definition: MCFixup.h:166
Iterator for intrusive lists based on ilist_node.
unsigned getMaxBytesToEmit() const
Definition: MCFragment.h:324
bool hasEmitNops() const
Definition: MCFragment.h:326
MCAsmBackend * getBackendPtr() const
Definition: MCAssembler.h:280
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:286
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:81
const MCSymbol & getSymbol() const
Definition: MCExpr.h:335
bool isUndefined(bool SetUsed=true) const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition: MCSymbol.h:257
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:384
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:24
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
bool isRegistered() const
Definition: MCSymbol.h:210
Represents a symbol table index fragment.
Definition: MCFragment.h:576
An iterator type that allows iterating over the pointees via some other iterator. ...
Definition: iterator.h:289
virtual void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout, const MCFragment *Fragment, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue)=0
Record a relocation entry.
static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta, raw_ostream &OS)
Definition: MCDwarf.cpp:1847
MCLOHContainer & getLOHContainer()
Definition: MCAssembler.h:417
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:191
bool getRelaxAll() const
Definition: MCAssembler.h:314
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Definition: MCSymbol.h:248
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:99
uint64_t getValue() const
Definition: MCFragment.h:444
bool hasInstructions() const
Does this fragment have instructions emitted into it? By default this is false, but specific fragment...
Definition: MCFragment.h:110
const MCSubtargetInfo * getSubtargetInfo() const
Retrieve the MCSubTargetInfo in effect when the instruction was encoded.
Definition: MCFragment.h:174
SmallString< 8 > & getContents()
Definition: MCFragment.h:565
void setIsRegistered(bool Value) const
Definition: MCSymbol.h:211
Fragment representing the binary annotations produced by the .cv_inline_linetable directive...
Definition: MCFragment.h:598
bool isUsedInReloc() const
Definition: MCSymbol.h:214
unsigned getValueSize() const
Definition: MCFragment.h:322
unsigned getAlignment() const
Definition: MCFragment.h:318
int64_t getLineDelta() const
Definition: MCFragment.h:536
const MCInst & getInst() const
Definition: MCFragment.h:282
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:103
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:63
#define I(x, y, z)
Definition: MD5.cpp:58
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:47
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:642
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:508
const MCSymbol * getSymbol()
Definition: MCFragment.h:586
uint32_t getRefKind() const
Definition: MCValue.h:50
CodeViewContext & getCVContext()
Definition: MCContext.cpp:602
void reset()
Reuse an assembler instance.
Definition: MCAssembler.cpp:99
Fragment for data and encoded instructions.
Definition: MCFragment.h:242
uint64_t getSectionFileSize(const MCSection *Sec) const
Get the data size of the given section, as emitted to the object file.
Definition: MCFragment.cpp:182
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:299
bool isSigned() const
Definition: MCFragment.h:506
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:727
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:100
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
const MCExpr * getValue() const
Definition: MCFixup.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
iterator end()
Definition: MCSection.h:166
uint8_t getValue() const
Definition: MCFragment.h:475
virtual void reset()
lifetime management
Definition: MCAsmBackend.h:56
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:156
void setInst(const MCInst &Value)
Definition: MCFragment.h:283
MCCodeEmitter * getEmitterPtr() const
Definition: MCAssembler.h:282
MCFixupKind getKind() const
Definition: MCFixup.h:123
iterator begin()
Definition: MCSection.h:163
symbol_iterator symbol_end()
Definition: MCAssembler.h:344