LLVM  10.0.0
MCFragment.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCFragment.cpp - Assembler Fragment 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/MCFragment.h"
10 #include "llvm/ADT/SmallVector.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCFixup.h"
19 #include "llvm/MC/MCSection.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCValue.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Compiler.h"
26 #include <cassert>
27 #include <cstdint>
28 #include <utility>
29 
30 using namespace llvm;
31 
33  // Compute the section layout order. Virtual sections must go last.
34  for (MCSection &Sec : Asm)
35  if (!Sec.isVirtualSection())
36  SectionOrder.push_back(&Sec);
37  for (MCSection &Sec : Asm)
38  if (Sec.isVirtualSection())
39  SectionOrder.push_back(&Sec);
40 }
41 
42 bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
43  const MCSection *Sec = F->getParent();
44  const MCFragment *LastValid = LastValidFragment.lookup(Sec);
45  if (!LastValid)
46  return false;
47  assert(LastValid->getParent() == Sec);
48  return F->getLayoutOrder() <= LastValid->getLayoutOrder();
49 }
50 
52  // If this fragment wasn't already valid, we don't need to do anything.
53  if (!isFragmentValid(F))
54  return;
55 
56  // Otherwise, reset the last valid fragment to the previous fragment
57  // (if this is the first fragment, it will be NULL).
58  LastValidFragment[F->getParent()] = F->getPrevNode();
59 }
60 
61 void MCAsmLayout::ensureValid(const MCFragment *F) const {
62  MCSection *Sec = F->getParent();
64  if (MCFragment *Cur = LastValidFragment[Sec])
65  I = ++MCSection::iterator(Cur);
66  else
67  I = Sec->begin();
68 
69  // Advance the layout position until the fragment is valid.
70  while (!isFragmentValid(F)) {
71  assert(I != Sec->end() && "Layout bookkeeping error");
72  const_cast<MCAsmLayout *>(this)->layoutFragment(&*I);
73  ++I;
74  }
75 }
76 
77 uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
78  ensureValid(F);
79  assert(F->Offset != ~UINT64_C(0) && "Address not set!");
80  return F->Offset;
81 }
82 
83 // Simple getSymbolOffset helper for the non-variable case.
84 static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S,
85  bool ReportError, uint64_t &Val) {
86  if (!S.getFragment()) {
87  if (ReportError)
88  report_fatal_error("unable to evaluate offset to undefined symbol '" +
89  S.getName() + "'");
90  return false;
91  }
92  Val = Layout.getFragmentOffset(S.getFragment()) + S.getOffset();
93  return true;
94 }
95 
96 static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S,
97  bool ReportError, uint64_t &Val) {
98  if (!S.isVariable())
99  return getLabelOffset(Layout, S, ReportError, Val);
100 
101  // If SD is a variable, evaluate it.
102  MCValue Target;
103  if (!S.getVariableValue()->evaluateAsValue(Target, Layout))
104  report_fatal_error("unable to evaluate offset for variable '" +
105  S.getName() + "'");
106 
107  uint64_t Offset = Target.getConstant();
108 
109  const MCSymbolRefExpr *A = Target.getSymA();
110  if (A) {
111  uint64_t ValA;
112  if (!getLabelOffset(Layout, A->getSymbol(), ReportError, ValA))
113  return false;
114  Offset += ValA;
115  }
116 
117  const MCSymbolRefExpr *B = Target.getSymB();
118  if (B) {
119  uint64_t ValB;
120  if (!getLabelOffset(Layout, B->getSymbol(), ReportError, ValB))
121  return false;
122  Offset -= ValB;
123  }
124 
125  Val = Offset;
126  return true;
127 }
128 
129 bool MCAsmLayout::getSymbolOffset(const MCSymbol &S, uint64_t &Val) const {
130  return getSymbolOffsetImpl(*this, S, false, Val);
131 }
132 
133 uint64_t MCAsmLayout::getSymbolOffset(const MCSymbol &S) const {
134  uint64_t Val;
135  getSymbolOffsetImpl(*this, S, true, Val);
136  return Val;
137 }
138 
140  if (!Symbol.isVariable())
141  return &Symbol;
142 
143  const MCExpr *Expr = Symbol.getVariableValue();
144  MCValue Value;
145  if (!Expr->evaluateAsValue(Value, *this)) {
146  Assembler.getContext().reportError(
147  Expr->getLoc(), "expression could not be evaluated");
148  return nullptr;
149  }
150 
151  const MCSymbolRefExpr *RefB = Value.getSymB();
152  if (RefB) {
153  Assembler.getContext().reportError(
154  Expr->getLoc(), Twine("symbol '") + RefB->getSymbol().getName() +
155  "' could not be evaluated in a subtraction expression");
156  return nullptr;
157  }
158 
159  const MCSymbolRefExpr *A = Value.getSymA();
160  if (!A)
161  return nullptr;
162 
163  const MCSymbol &ASym = A->getSymbol();
164  const MCAssembler &Asm = getAssembler();
165  if (ASym.isCommon()) {
166  Asm.getContext().reportError(Expr->getLoc(),
167  "Common symbol '" + ASym.getName() +
168  "' cannot be used in assignment expr");
169  return nullptr;
170  }
171 
172  return &ASym;
173 }
174 
175 uint64_t MCAsmLayout::getSectionAddressSize(const MCSection *Sec) const {
176  // The size is the last fragment's end offset.
177  const MCFragment &F = Sec->getFragmentList().back();
178  return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
179 }
180 
181 uint64_t MCAsmLayout::getSectionFileSize(const MCSection *Sec) const {
182  // Virtual sections have no file size.
183  if (Sec->isVirtualSection())
184  return 0;
185 
186  // Otherwise, the file size is the same as the address space size.
187  return getSectionAddressSize(Sec);
188 }
189 
190 uint64_t llvm::computeBundlePadding(const MCAssembler &Assembler,
191  const MCEncodedFragment *F,
192  uint64_t FOffset, uint64_t FSize) {
193  uint64_t BundleSize = Assembler.getBundleAlignSize();
194  assert(BundleSize > 0 &&
195  "computeBundlePadding should only be called if bundling is enabled");
196  uint64_t BundleMask = BundleSize - 1;
197  uint64_t OffsetInBundle = FOffset & BundleMask;
198  uint64_t EndOfFragment = OffsetInBundle + FSize;
199 
200  // There are two kinds of bundling restrictions:
201  //
202  // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
203  // *end* on a bundle boundary.
204  // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
205  // would, add padding until the end of the bundle so that the fragment
206  // will start in a new one.
207  if (F->alignToBundleEnd()) {
208  // Three possibilities here:
209  //
210  // A) The fragment just happens to end at a bundle boundary, so we're good.
211  // B) The fragment ends before the current bundle boundary: pad it just
212  // enough to reach the boundary.
213  // C) The fragment ends after the current bundle boundary: pad it until it
214  // reaches the end of the next bundle boundary.
215  //
216  // Note: this code could be made shorter with some modulo trickery, but it's
217  // intentionally kept in its more explicit form for simplicity.
218  if (EndOfFragment == BundleSize)
219  return 0;
220  else if (EndOfFragment < BundleSize)
221  return BundleSize - EndOfFragment;
222  else { // EndOfFragment > BundleSize
223  return 2 * BundleSize - EndOfFragment;
224  }
225  } else if (OffsetInBundle > 0 && EndOfFragment > BundleSize)
226  return BundleSize - OffsetInBundle;
227  else
228  return 0;
229 }
230 
231 /* *** */
232 
234 
236  MCSection *Parent)
237  : Parent(Parent), Atom(nullptr), Offset(~UINT64_C(0)), LayoutOrder(0),
238  Kind(Kind), HasInstructions(HasInstructions) {
239  if (Parent && !isa<MCDummyFragment>(*this))
240  Parent->getFragmentList().push_back(this);
241 }
242 
244  // First check if we are the sentinal.
245  if (Kind == FragmentType(~0)) {
246  delete this;
247  return;
248  }
249 
250  switch (Kind) {
251  case FT_Align:
252  delete cast<MCAlignFragment>(this);
253  return;
254  case FT_Data:
255  delete cast<MCDataFragment>(this);
256  return;
258  delete cast<MCCompactEncodedInstFragment>(this);
259  return;
260  case FT_Fill:
261  delete cast<MCFillFragment>(this);
262  return;
263  case FT_Relaxable:
264  delete cast<MCRelaxableFragment>(this);
265  return;
266  case FT_Org:
267  delete cast<MCOrgFragment>(this);
268  return;
269  case FT_Dwarf:
270  delete cast<MCDwarfLineAddrFragment>(this);
271  return;
272  case FT_DwarfFrame:
273  delete cast<MCDwarfCallFrameFragment>(this);
274  return;
275  case FT_LEB:
276  delete cast<MCLEBFragment>(this);
277  return;
278  case FT_BoundaryAlign:
279  delete cast<MCBoundaryAlignFragment>(this);
280  return;
281  case FT_SymbolId:
282  delete cast<MCSymbolIdFragment>(this);
283  return;
284  case FT_CVInlineLines:
285  delete cast<MCCVInlineLineTableFragment>(this);
286  return;
287  case FT_CVDefRange:
288  delete cast<MCCVDefRangeFragment>(this);
289  return;
290  case FT_Dummy:
291  delete cast<MCDummyFragment>(this);
292  return;
293  }
294 }
295 
296 // Debugging methods
297 
298 namespace llvm {
299 
301  OS << "<MCFixup" << " Offset:" << AF.getOffset()
302  << " Value:" << *AF.getValue()
303  << " Kind:" << AF.getKind() << ">";
304  return OS;
305 }
306 
307 } // end namespace llvm
308 
309 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
311  raw_ostream &OS = errs();
312 
313  OS << "<";
314  switch (getKind()) {
315  case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
316  case MCFragment::FT_Data: OS << "MCDataFragment"; break;
318  OS << "MCCompactEncodedInstFragment"; break;
319  case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
320  case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
321  case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
322  case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
323  case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
324  case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
325  case MCFragment::FT_BoundaryAlign: OS<<"MCBoundaryAlignFragment"; break;
326  case MCFragment::FT_SymbolId: OS << "MCSymbolIdFragment"; break;
327  case MCFragment::FT_CVInlineLines: OS << "MCCVInlineLineTableFragment"; break;
328  case MCFragment::FT_CVDefRange: OS << "MCCVDefRangeTableFragment"; break;
329  case MCFragment::FT_Dummy: OS << "MCDummyFragment"; break;
330  }
331 
332  OS << "<MCFragment " << (const void *)this << " LayoutOrder:" << LayoutOrder
333  << " Offset:" << Offset << " HasInstructions:" << hasInstructions();
334  if (const auto *EF = dyn_cast<MCEncodedFragment>(this))
335  OS << " BundlePadding:" << static_cast<unsigned>(EF->getBundlePadding());
336  OS << ">";
337 
338  switch (getKind()) {
339  case MCFragment::FT_Align: {
340  const auto *AF = cast<MCAlignFragment>(this);
341  if (AF->hasEmitNops())
342  OS << " (emit nops)";
343  OS << "\n ";
344  OS << " Alignment:" << AF->getAlignment()
345  << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
346  << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
347  break;
348  }
349  case MCFragment::FT_Data: {
350  const auto *DF = cast<MCDataFragment>(this);
351  OS << "\n ";
352  OS << " Contents:[";
353  const SmallVectorImpl<char> &Contents = DF->getContents();
354  for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
355  if (i) OS << ",";
356  OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
357  }
358  OS << "] (" << Contents.size() << " bytes)";
359 
360  if (DF->fixup_begin() != DF->fixup_end()) {
361  OS << ",\n ";
362  OS << " Fixups:[";
363  for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
364  ie = DF->fixup_end(); it != ie; ++it) {
365  if (it != DF->fixup_begin()) OS << ",\n ";
366  OS << *it;
367  }
368  OS << "]";
369  }
370  break;
371  }
373  const auto *CEIF =
374  cast<MCCompactEncodedInstFragment>(this);
375  OS << "\n ";
376  OS << " Contents:[";
377  const SmallVectorImpl<char> &Contents = CEIF->getContents();
378  for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
379  if (i) OS << ",";
380  OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
381  }
382  OS << "] (" << Contents.size() << " bytes)";
383  break;
384  }
385  case MCFragment::FT_Fill: {
386  const auto *FF = cast<MCFillFragment>(this);
387  OS << " Value:" << static_cast<unsigned>(FF->getValue())
388  << " ValueSize:" << static_cast<unsigned>(FF->getValueSize())
389  << " NumValues:" << FF->getNumValues();
390  break;
391  }
393  const auto *F = cast<MCRelaxableFragment>(this);
394  OS << "\n ";
395  OS << " Inst:";
396  F->getInst().dump_pretty(OS);
397  break;
398  }
399  case MCFragment::FT_Org: {
400  const auto *OF = cast<MCOrgFragment>(this);
401  OS << "\n ";
402  OS << " Offset:" << OF->getOffset()
403  << " Value:" << static_cast<unsigned>(OF->getValue());
404  break;
405  }
406  case MCFragment::FT_Dwarf: {
407  const auto *OF = cast<MCDwarfLineAddrFragment>(this);
408  OS << "\n ";
409  OS << " AddrDelta:" << OF->getAddrDelta()
410  << " LineDelta:" << OF->getLineDelta();
411  break;
412  }
414  const auto *CF = cast<MCDwarfCallFrameFragment>(this);
415  OS << "\n ";
416  OS << " AddrDelta:" << CF->getAddrDelta();
417  break;
418  }
419  case MCFragment::FT_LEB: {
420  const auto *LF = cast<MCLEBFragment>(this);
421  OS << "\n ";
422  OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
423  break;
424  }
426  const auto *BF = cast<MCBoundaryAlignFragment>(this);
427  if (BF->canEmitNops())
428  OS << " (can emit nops to align";
429  if (BF->isFused())
430  OS << " fused branch)";
431  else
432  OS << " unfused branch)";
433  OS << "\n ";
434  OS << " BoundarySize:" << BF->getAlignment().value()
435  << " Size:" << BF->getSize();
436  break;
437  }
439  const auto *F = cast<MCSymbolIdFragment>(this);
440  OS << "\n ";
441  OS << " Sym:" << F->getSymbol();
442  break;
443  }
445  const auto *F = cast<MCCVInlineLineTableFragment>(this);
446  OS << "\n ";
447  OS << " Sym:" << *F->getFnStartSym();
448  break;
449  }
451  const auto *F = cast<MCCVDefRangeFragment>(this);
452  OS << "\n ";
453  for (std::pair<const MCSymbol *, const MCSymbol *> RangeStartEnd :
454  F->getRanges()) {
455  OS << " RangeStart:" << RangeStartEnd.first;
456  OS << " RangeEnd:" << RangeStartEnd.second;
457  }
458  break;
459  }
461  break;
462  }
463  OS << ">";
464 }
465 #endif
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:144
static bool getSymbolOffsetImpl(const MCAsmLayout &Layout, const MCSymbol &S, bool ReportError, uint64_t &Val)
Definition: MCFragment.cpp:96
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:289
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:492
This represents an "assembler immediate".
Definition: MCValue.h:39
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
FragmentType getKind() const
Definition: MCFragment.h:85
F(f)
bool isCommon() const
Is this a &#39;common&#39; symbol.
Definition: MCSymbol.h:378
static bool getLabelOffset(const MCAsmLayout &Layout, const MCSymbol &S, bool ReportError, uint64_t &Val)
Definition: MCFragment.cpp:84
SMLoc getLoc() const
Definition: MCExpr.h:69
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:322
MCContext & getContext() const
Definition: MCAssembler.h:281
int64_t getConstant() const
Definition: MCValue.h:46
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:48
Interface implemented by fragments that contain encoded instructions and/or data. ...
Definition: MCFragment.h:113
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
SmallVectorImpl< MCFixup >::const_iterator const_fixup_iterator
Definition: MCFragment.h:207
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
void destroy()
Destroys the current fragment.
Definition: MCFragment.cpp:243
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16)...
Definition: StringExtras.h:36
void layoutFragment(MCFragment *Fragment)
Perform layout for a single fragment, assuming that the previous fragment has already been laid out c...
void dump() const
Definition: MCFragment.cpp:310
MCAsmLayout(MCAssembler &Assembler)
Definition: MCFragment.cpp:32
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void LLVM_ATTRIBUTE_NORETURN ReportError(uint64_t StartOffset, const char *ErrorMsg)
void invalidateFragmentsFrom(MCFragment *F)
Invalidate the fragments starting with F because it has been resized.
Definition: MCFragment.cpp:51
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
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:690
uint64_t getOffset() const
Definition: MCSymbol.h:316
uint32_t getOffset() const
Definition: MCFixup.h:130
size_t size() const
Definition: SmallVector.h:52
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
Definition: MCFragment.cpp:77
Iterator for intrusive lists based on ilist_node.
virtual bool isVirtualSection() const =0
Check whether this section is "virtual", that is has no actual object file contents.
const MCSymbol & getSymbol() const
Definition: MCExpr.h:342
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:388
unsigned getLayoutOrder() const
Definition: MCFragment.h:93
static void deleteNode(NodeTy *V)
Definition: ilist.h:41
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
Target - Wrapper for Target specific information.
MCSection * getParent() const
Definition: MCFragment.h:87
void push_back(pointer val)
Definition: ilist.h:311
bool hasInstructions() const
Does this fragment have instructions emitted into it? By default this is false, but specific fragment...
Definition: MCFragment.h:98
#define I(x, y, z)
Definition: MD5.cpp:58
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...
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2096
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:196
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
MCFragment()=delete
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
Definition: MCSymbol.h:294
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:133
FragmentListType::iterator iterator
Definition: MCSection.h:53
MCSection::FragmentListType & getFragmentList()
Definition: MCSection.h:152
const MCSymbol * getBaseSymbol(const MCSymbol &Symbol) const
If this symbol is equivalent to A + Constant, return A.
Definition: MCFragment.cpp:139
MCFixupKind getKind() const
Definition: MCFixup.h:126