MCFragment.cpp 16 KB

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