MCAssembler.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. #define DEBUG_TYPE "assembler"
  10. #include "llvm/MC/MCAssembler.h"
  11. #include "llvm/ADT/Statistic.h"
  12. #include "llvm/ADT/StringExtras.h"
  13. #include "llvm/ADT/Twine.h"
  14. #include "llvm/MC/MCAsmBackend.h"
  15. #include "llvm/MC/MCAsmLayout.h"
  16. #include "llvm/MC/MCCodeEmitter.h"
  17. #include "llvm/MC/MCContext.h"
  18. #include "llvm/MC/MCDwarf.h"
  19. #include "llvm/MC/MCExpr.h"
  20. #include "llvm/MC/MCFixupKindInfo.h"
  21. #include "llvm/MC/MCObjectWriter.h"
  22. #include "llvm/MC/MCSection.h"
  23. #include "llvm/MC/MCSymbol.h"
  24. #include "llvm/MC/MCValue.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/ErrorHandling.h"
  27. #include "llvm/Support/LEB128.h"
  28. #include "llvm/Support/TargetRegistry.h"
  29. #include "llvm/Support/raw_ostream.h"
  30. using namespace llvm;
  31. namespace {
  32. namespace stats {
  33. STATISTIC(EmittedFragments, "Number of emitted assembler fragments - total");
  34. STATISTIC(EmittedRelaxableFragments,
  35. "Number of emitted assembler fragments - relaxable");
  36. STATISTIC(EmittedDataFragments,
  37. "Number of emitted assembler fragments - data");
  38. STATISTIC(EmittedCompactEncodedInstFragments,
  39. "Number of emitted assembler fragments - compact encoded inst");
  40. STATISTIC(EmittedAlignFragments,
  41. "Number of emitted assembler fragments - align");
  42. STATISTIC(EmittedFillFragments,
  43. "Number of emitted assembler fragments - fill");
  44. STATISTIC(EmittedOrgFragments,
  45. "Number of emitted assembler fragments - org");
  46. STATISTIC(evaluateFixup, "Number of evaluated fixups");
  47. STATISTIC(FragmentLayouts, "Number of fragment layouts");
  48. STATISTIC(ObjectBytes, "Number of emitted object file bytes");
  49. STATISTIC(RelaxationSteps, "Number of assembler layout and relaxation steps");
  50. STATISTIC(RelaxedInstructions, "Number of relaxed instructions");
  51. }
  52. }
  53. // FIXME FIXME FIXME: There are number of places in this file where we convert
  54. // what is a 64-bit assembler value used for computation into a value in the
  55. // object file, which may truncate it. We should detect that truncation where
  56. // invalid and report errors back.
  57. /* *** */
  58. MCAsmLayout::MCAsmLayout(MCAssembler &Asm)
  59. : Assembler(Asm), LastValidFragment()
  60. {
  61. // Compute the section layout order. Virtual sections must go last.
  62. for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
  63. if (!it->getSection().isVirtualSection())
  64. SectionOrder.push_back(&*it);
  65. for (MCAssembler::iterator it = Asm.begin(), ie = Asm.end(); it != ie; ++it)
  66. if (it->getSection().isVirtualSection())
  67. SectionOrder.push_back(&*it);
  68. }
  69. bool MCAsmLayout::isFragmentValid(const MCFragment *F) const {
  70. const MCSectionData &SD = *F->getParent();
  71. const MCFragment *LastValid = LastValidFragment.lookup(&SD);
  72. if (!LastValid)
  73. return false;
  74. assert(LastValid->getParent() == F->getParent());
  75. return F->getLayoutOrder() <= LastValid->getLayoutOrder();
  76. }
  77. void MCAsmLayout::invalidateFragmentsAfter(MCFragment *F) {
  78. // If this fragment wasn't already valid, we don't need to do anything.
  79. if (!isFragmentValid(F))
  80. return;
  81. // Otherwise, reset the last valid fragment to this fragment.
  82. const MCSectionData &SD = *F->getParent();
  83. LastValidFragment[&SD] = F;
  84. }
  85. void MCAsmLayout::ensureValid(const MCFragment *F) const {
  86. MCSectionData &SD = *F->getParent();
  87. MCFragment *Cur = LastValidFragment[&SD];
  88. if (!Cur)
  89. Cur = &*SD.begin();
  90. else
  91. Cur = Cur->getNextNode();
  92. // Advance the layout position until the fragment is valid.
  93. while (!isFragmentValid(F)) {
  94. assert(Cur && "Layout bookkeeping error");
  95. const_cast<MCAsmLayout*>(this)->layoutFragment(Cur);
  96. Cur = Cur->getNextNode();
  97. }
  98. }
  99. uint64_t MCAsmLayout::getFragmentOffset(const MCFragment *F) const {
  100. ensureValid(F);
  101. assert(F->Offset != ~UINT64_C(0) && "Address not set!");
  102. return F->Offset;
  103. }
  104. uint64_t MCAsmLayout::getSymbolOffset(const MCSymbolData *SD) const {
  105. const MCSymbol &S = SD->getSymbol();
  106. // If this is a variable, then recursively evaluate now.
  107. if (S.isVariable()) {
  108. MCValue Target;
  109. if (!S.getVariableValue()->EvaluateAsRelocatable(Target, *this))
  110. report_fatal_error("unable to evaluate offset for variable '" +
  111. S.getName() + "'");
  112. // Verify that any used symbols are defined.
  113. if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined())
  114. report_fatal_error("unable to evaluate offset to undefined symbol '" +
  115. Target.getSymA()->getSymbol().getName() + "'");
  116. if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined())
  117. report_fatal_error("unable to evaluate offset to undefined symbol '" +
  118. Target.getSymB()->getSymbol().getName() + "'");
  119. uint64_t Offset = Target.getConstant();
  120. if (Target.getSymA())
  121. Offset += getSymbolOffset(&Assembler.getSymbolData(
  122. Target.getSymA()->getSymbol()));
  123. if (Target.getSymB())
  124. Offset -= getSymbolOffset(&Assembler.getSymbolData(
  125. Target.getSymB()->getSymbol()));
  126. return Offset;
  127. }
  128. assert(SD->getFragment() && "Invalid getOffset() on undefined symbol!");
  129. return getFragmentOffset(SD->getFragment()) + SD->getOffset();
  130. }
  131. uint64_t MCAsmLayout::getSectionAddressSize(const MCSectionData *SD) const {
  132. // The size is the last fragment's end offset.
  133. const MCFragment &F = SD->getFragmentList().back();
  134. return getFragmentOffset(&F) + getAssembler().computeFragmentSize(*this, F);
  135. }
  136. uint64_t MCAsmLayout::getSectionFileSize(const MCSectionData *SD) const {
  137. // Virtual sections have no file size.
  138. if (SD->getSection().isVirtualSection())
  139. return 0;
  140. // Otherwise, the file size is the same as the address space size.
  141. return getSectionAddressSize(SD);
  142. }
  143. uint64_t MCAsmLayout::computeBundlePadding(const MCFragment *F,
  144. uint64_t FOffset, uint64_t FSize) {
  145. uint64_t BundleSize = Assembler.getBundleAlignSize();
  146. assert(BundleSize > 0 &&
  147. "computeBundlePadding should only be called if bundling is enabled");
  148. uint64_t BundleMask = BundleSize - 1;
  149. uint64_t OffsetInBundle = FOffset & BundleMask;
  150. uint64_t EndOfFragment = OffsetInBundle + FSize;
  151. // There are two kinds of bundling restrictions:
  152. //
  153. // 1) For alignToBundleEnd(), add padding to ensure that the fragment will
  154. // *end* on a bundle boundary.
  155. // 2) Otherwise, check if the fragment would cross a bundle boundary. If it
  156. // would, add padding until the end of the bundle so that the fragment
  157. // will start in a new one.
  158. if (F->alignToBundleEnd()) {
  159. // Three possibilities here:
  160. //
  161. // A) The fragment just happens to end at a bundle boundary, so we're good.
  162. // B) The fragment ends before the current bundle boundary: pad it just
  163. // enough to reach the boundary.
  164. // C) The fragment ends after the current bundle boundary: pad it until it
  165. // reaches the end of the next bundle boundary.
  166. //
  167. // Note: this code could be made shorter with some modulo trickery, but it's
  168. // intentionally kept in its more explicit form for simplicity.
  169. if (EndOfFragment == BundleSize)
  170. return 0;
  171. else if (EndOfFragment < BundleSize)
  172. return BundleSize - EndOfFragment;
  173. else { // EndOfFragment > BundleSize
  174. return 2 * BundleSize - EndOfFragment;
  175. }
  176. } else if (EndOfFragment > BundleSize)
  177. return BundleSize - OffsetInBundle;
  178. else
  179. return 0;
  180. }
  181. /* *** */
  182. MCFragment::MCFragment() : Kind(FragmentType(~0)) {
  183. }
  184. MCFragment::~MCFragment() {
  185. }
  186. MCFragment::MCFragment(FragmentType _Kind, MCSectionData *_Parent)
  187. : Kind(_Kind), Parent(_Parent), Atom(0), Offset(~UINT64_C(0))
  188. {
  189. if (Parent)
  190. Parent->getFragmentList().push_back(this);
  191. }
  192. /* *** */
  193. MCEncodedFragment::~MCEncodedFragment() {
  194. }
  195. /* *** */
  196. MCEncodedFragmentWithFixups::~MCEncodedFragmentWithFixups() {
  197. }
  198. /* *** */
  199. MCSectionData::MCSectionData() : Section(0) {}
  200. MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
  201. : Section(&_Section),
  202. Ordinal(~UINT32_C(0)),
  203. Alignment(1),
  204. BundleLockState(NotBundleLocked), BundleGroupBeforeFirstInst(false),
  205. HasInstructions(false)
  206. {
  207. if (A)
  208. A->getSectionList().push_back(this);
  209. }
  210. /* *** */
  211. MCSymbolData::MCSymbolData() : Symbol(0) {}
  212. MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
  213. uint64_t _Offset, MCAssembler *A)
  214. : Symbol(&_Symbol), Fragment(_Fragment), Offset(_Offset),
  215. IsExternal(false), IsPrivateExtern(false),
  216. CommonSize(0), SymbolSize(0), CommonAlign(0),
  217. Flags(0), Index(0)
  218. {
  219. if (A)
  220. A->getSymbolList().push_back(this);
  221. }
  222. /* *** */
  223. MCAssembler::MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
  224. MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
  225. raw_ostream &OS_)
  226. : Context(Context_), Backend(Backend_), Emitter(Emitter_), Writer(Writer_),
  227. OS(OS_), BundleAlignSize(0), RelaxAll(false), NoExecStack(false),
  228. SubsectionsViaSymbols(false), ELFHeaderEFlags(0) {
  229. }
  230. MCAssembler::~MCAssembler() {
  231. }
  232. void MCAssembler::reset() {
  233. Sections.clear();
  234. Symbols.clear();
  235. SectionMap.clear();
  236. SymbolMap.clear();
  237. IndirectSymbols.clear();
  238. DataRegions.clear();
  239. ThumbFuncs.clear();
  240. RelaxAll = false;
  241. NoExecStack = false;
  242. SubsectionsViaSymbols = false;
  243. ELFHeaderEFlags = 0;
  244. // reset objects owned by us
  245. getBackend().reset();
  246. getEmitter().reset();
  247. getWriter().reset();
  248. }
  249. bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
  250. // Non-temporary labels should always be visible to the linker.
  251. if (!Symbol.isTemporary())
  252. return true;
  253. // Absolute temporary labels are never visible.
  254. if (!Symbol.isInSection())
  255. return false;
  256. // Otherwise, check if the section requires symbols even for temporary labels.
  257. return getBackend().doesSectionRequireSymbols(Symbol.getSection());
  258. }
  259. const MCSymbolData *MCAssembler::getAtom(const MCSymbolData *SD) const {
  260. // Linker visible symbols define atoms.
  261. if (isSymbolLinkerVisible(SD->getSymbol()))
  262. return SD;
  263. // Absolute and undefined symbols have no defining atom.
  264. if (!SD->getFragment())
  265. return 0;
  266. // Non-linker visible symbols in sections which can't be atomized have no
  267. // defining atom.
  268. if (!getBackend().isSectionAtomizable(
  269. SD->getFragment()->getParent()->getSection()))
  270. return 0;
  271. // Otherwise, return the atom for the containing fragment.
  272. return SD->getFragment()->getAtom();
  273. }
  274. bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout,
  275. const MCFixup &Fixup, const MCFragment *DF,
  276. MCValue &Target, uint64_t &Value) const {
  277. ++stats::evaluateFixup;
  278. if (!Fixup.getValue()->EvaluateAsRelocatable(Target, Layout))
  279. getContext().FatalError(Fixup.getLoc(), "expected relocatable expression");
  280. bool IsPCRel = Backend.getFixupKindInfo(
  281. Fixup.getKind()).Flags & MCFixupKindInfo::FKF_IsPCRel;
  282. bool IsResolved;
  283. if (IsPCRel) {
  284. if (Target.getSymB()) {
  285. IsResolved = false;
  286. } else if (!Target.getSymA()) {
  287. IsResolved = false;
  288. } else {
  289. const MCSymbolRefExpr *A = Target.getSymA();
  290. const MCSymbol &SA = A->getSymbol();
  291. if (A->getKind() != MCSymbolRefExpr::VK_None ||
  292. SA.AliasedSymbol().isUndefined()) {
  293. IsResolved = false;
  294. } else {
  295. const MCSymbolData &DataA = getSymbolData(SA);
  296. IsResolved =
  297. getWriter().IsSymbolRefDifferenceFullyResolvedImpl(*this, DataA,
  298. *DF, false, true);
  299. }
  300. }
  301. } else {
  302. IsResolved = Target.isAbsolute();
  303. }
  304. Value = Target.getConstant();
  305. if (const MCSymbolRefExpr *A = Target.getSymA()) {
  306. const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
  307. if (Sym.isDefined())
  308. Value += Layout.getSymbolOffset(&getSymbolData(Sym));
  309. }
  310. if (const MCSymbolRefExpr *B = Target.getSymB()) {
  311. const MCSymbol &Sym = B->getSymbol().AliasedSymbol();
  312. if (Sym.isDefined())
  313. Value -= Layout.getSymbolOffset(&getSymbolData(Sym));
  314. }
  315. bool ShouldAlignPC = Backend.getFixupKindInfo(Fixup.getKind()).Flags &
  316. MCFixupKindInfo::FKF_IsAlignedDownTo32Bits;
  317. assert((ShouldAlignPC ? IsPCRel : true) &&
  318. "FKF_IsAlignedDownTo32Bits is only allowed on PC-relative fixups!");
  319. if (IsPCRel) {
  320. uint32_t Offset = Layout.getFragmentOffset(DF) + Fixup.getOffset();
  321. // A number of ARM fixups in Thumb mode require that the effective PC
  322. // address be determined as the 32-bit aligned version of the actual offset.
  323. if (ShouldAlignPC) Offset &= ~0x3;
  324. Value -= Offset;
  325. }
  326. // Let the backend adjust the fixup value if necessary, including whether
  327. // we need a relocation.
  328. Backend.processFixupValue(*this, Layout, Fixup, DF, Target, Value,
  329. IsResolved);
  330. return IsResolved;
  331. }
  332. uint64_t MCAssembler::computeFragmentSize(const MCAsmLayout &Layout,
  333. const MCFragment &F) const {
  334. switch (F.getKind()) {
  335. case MCFragment::FT_Data:
  336. case MCFragment::FT_Relaxable:
  337. case MCFragment::FT_CompactEncodedInst:
  338. return cast<MCEncodedFragment>(F).getContents().size();
  339. case MCFragment::FT_Fill:
  340. return cast<MCFillFragment>(F).getSize();
  341. case MCFragment::FT_LEB:
  342. return cast<MCLEBFragment>(F).getContents().size();
  343. case MCFragment::FT_Align: {
  344. const MCAlignFragment &AF = cast<MCAlignFragment>(F);
  345. unsigned Offset = Layout.getFragmentOffset(&AF);
  346. unsigned Size = OffsetToAlignment(Offset, AF.getAlignment());
  347. // If we are padding with nops, force the padding to be larger than the
  348. // minimum nop size.
  349. if (Size > 0 && AF.hasEmitNops()) {
  350. while (Size % getBackend().getMinimumNopSize())
  351. Size += AF.getAlignment();
  352. }
  353. if (Size > AF.getMaxBytesToEmit())
  354. return 0;
  355. return Size;
  356. }
  357. case MCFragment::FT_Org: {
  358. MCOrgFragment &OF = cast<MCOrgFragment>(F);
  359. int64_t TargetLocation;
  360. if (!OF.getOffset().EvaluateAsAbsolute(TargetLocation, Layout))
  361. report_fatal_error("expected assembly-time absolute expression");
  362. // FIXME: We need a way to communicate this error.
  363. uint64_t FragmentOffset = Layout.getFragmentOffset(&OF);
  364. int64_t Size = TargetLocation - FragmentOffset;
  365. if (Size < 0 || Size >= 0x40000000)
  366. report_fatal_error("invalid .org offset '" + Twine(TargetLocation) +
  367. "' (at offset '" + Twine(FragmentOffset) + "')");
  368. return Size;
  369. }
  370. case MCFragment::FT_Dwarf:
  371. return cast<MCDwarfLineAddrFragment>(F).getContents().size();
  372. case MCFragment::FT_DwarfFrame:
  373. return cast<MCDwarfCallFrameFragment>(F).getContents().size();
  374. }
  375. llvm_unreachable("invalid fragment kind");
  376. }
  377. void MCAsmLayout::layoutFragment(MCFragment *F) {
  378. MCFragment *Prev = F->getPrevNode();
  379. // We should never try to recompute something which is valid.
  380. assert(!isFragmentValid(F) && "Attempt to recompute a valid fragment!");
  381. // We should never try to compute the fragment layout if its predecessor
  382. // isn't valid.
  383. assert((!Prev || isFragmentValid(Prev)) &&
  384. "Attempt to compute fragment before its predecessor!");
  385. ++stats::FragmentLayouts;
  386. // Compute fragment offset and size.
  387. if (Prev)
  388. F->Offset = Prev->Offset + getAssembler().computeFragmentSize(*this, *Prev);
  389. else
  390. F->Offset = 0;
  391. LastValidFragment[F->getParent()] = F;
  392. // If bundling is enabled and this fragment has instructions in it, it has to
  393. // obey the bundling restrictions. With padding, we'll have:
  394. //
  395. //
  396. // BundlePadding
  397. // |||
  398. // -------------------------------------
  399. // Prev |##########| F |
  400. // -------------------------------------
  401. // ^
  402. // |
  403. // F->Offset
  404. //
  405. // The fragment's offset will point to after the padding, and its computed
  406. // size won't include the padding.
  407. //
  408. if (Assembler.isBundlingEnabled() && F->hasInstructions()) {
  409. assert(isa<MCEncodedFragment>(F) &&
  410. "Only MCEncodedFragment implementations have instructions");
  411. uint64_t FSize = Assembler.computeFragmentSize(*this, *F);
  412. if (FSize > Assembler.getBundleAlignSize())
  413. report_fatal_error("Fragment can't be larger than a bundle size");
  414. uint64_t RequiredBundlePadding = computeBundlePadding(F, F->Offset, FSize);
  415. if (RequiredBundlePadding > UINT8_MAX)
  416. report_fatal_error("Padding cannot exceed 255 bytes");
  417. F->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
  418. F->Offset += RequiredBundlePadding;
  419. }
  420. }
  421. /// \brief Write the contents of a fragment to the given object writer. Expects
  422. /// a MCEncodedFragment.
  423. static void writeFragmentContents(const MCFragment &F, MCObjectWriter *OW) {
  424. MCEncodedFragment &EF = cast<MCEncodedFragment>(F);
  425. OW->WriteBytes(EF.getContents());
  426. }
  427. /// \brief Write the fragment \p F to the output file.
  428. static void writeFragment(const MCAssembler &Asm, const MCAsmLayout &Layout,
  429. const MCFragment &F) {
  430. MCObjectWriter *OW = &Asm.getWriter();
  431. // FIXME: Embed in fragments instead?
  432. uint64_t FragmentSize = Asm.computeFragmentSize(Layout, F);
  433. // Should NOP padding be written out before this fragment?
  434. unsigned BundlePadding = F.getBundlePadding();
  435. if (BundlePadding > 0) {
  436. assert(Asm.isBundlingEnabled() &&
  437. "Writing bundle padding with disabled bundling");
  438. assert(F.hasInstructions() &&
  439. "Writing bundle padding for a fragment without instructions");
  440. unsigned TotalLength = BundlePadding + static_cast<unsigned>(FragmentSize);
  441. if (F.alignToBundleEnd() && TotalLength > Asm.getBundleAlignSize()) {
  442. // If the padding itself crosses a bundle boundary, it must be emitted
  443. // in 2 pieces, since even nop instructions must not cross boundaries.
  444. // v--------------v <- BundleAlignSize
  445. // v---------v <- BundlePadding
  446. // ----------------------------
  447. // | Prev |####|####| F |
  448. // ----------------------------
  449. // ^-------------------^ <- TotalLength
  450. unsigned DistanceToBoundary = TotalLength - Asm.getBundleAlignSize();
  451. if (!Asm.getBackend().writeNopData(DistanceToBoundary, OW))
  452. report_fatal_error("unable to write NOP sequence of " +
  453. Twine(DistanceToBoundary) + " bytes");
  454. BundlePadding -= DistanceToBoundary;
  455. }
  456. if (!Asm.getBackend().writeNopData(BundlePadding, OW))
  457. report_fatal_error("unable to write NOP sequence of " +
  458. Twine(BundlePadding) + " bytes");
  459. }
  460. // This variable (and its dummy usage) is to participate in the assert at
  461. // the end of the function.
  462. uint64_t Start = OW->getStream().tell();
  463. (void) Start;
  464. ++stats::EmittedFragments;
  465. switch (F.getKind()) {
  466. case MCFragment::FT_Align: {
  467. ++stats::EmittedAlignFragments;
  468. MCAlignFragment &AF = cast<MCAlignFragment>(F);
  469. uint64_t Count = FragmentSize / AF.getValueSize();
  470. assert(AF.getValueSize() && "Invalid virtual align in concrete fragment!");
  471. // FIXME: This error shouldn't actually occur (the front end should emit
  472. // multiple .align directives to enforce the semantics it wants), but is
  473. // severe enough that we want to report it. How to handle this?
  474. if (Count * AF.getValueSize() != FragmentSize)
  475. report_fatal_error("undefined .align directive, value size '" +
  476. Twine(AF.getValueSize()) +
  477. "' is not a divisor of padding size '" +
  478. Twine(FragmentSize) + "'");
  479. // See if we are aligning with nops, and if so do that first to try to fill
  480. // the Count bytes. Then if that did not fill any bytes or there are any
  481. // bytes left to fill use the Value and ValueSize to fill the rest.
  482. // If we are aligning with nops, ask that target to emit the right data.
  483. if (AF.hasEmitNops()) {
  484. if (!Asm.getBackend().writeNopData(Count, OW))
  485. report_fatal_error("unable to write nop sequence of " +
  486. Twine(Count) + " bytes");
  487. break;
  488. }
  489. // Otherwise, write out in multiples of the value size.
  490. for (uint64_t i = 0; i != Count; ++i) {
  491. switch (AF.getValueSize()) {
  492. default: llvm_unreachable("Invalid size!");
  493. case 1: OW->Write8 (uint8_t (AF.getValue())); break;
  494. case 2: OW->Write16(uint16_t(AF.getValue())); break;
  495. case 4: OW->Write32(uint32_t(AF.getValue())); break;
  496. case 8: OW->Write64(uint64_t(AF.getValue())); break;
  497. }
  498. }
  499. break;
  500. }
  501. case MCFragment::FT_Data:
  502. ++stats::EmittedDataFragments;
  503. writeFragmentContents(F, OW);
  504. break;
  505. case MCFragment::FT_Relaxable:
  506. ++stats::EmittedRelaxableFragments;
  507. writeFragmentContents(F, OW);
  508. break;
  509. case MCFragment::FT_CompactEncodedInst:
  510. ++stats::EmittedCompactEncodedInstFragments;
  511. writeFragmentContents(F, OW);
  512. break;
  513. case MCFragment::FT_Fill: {
  514. ++stats::EmittedFillFragments;
  515. MCFillFragment &FF = cast<MCFillFragment>(F);
  516. assert(FF.getValueSize() && "Invalid virtual align in concrete fragment!");
  517. for (uint64_t i = 0, e = FF.getSize() / FF.getValueSize(); i != e; ++i) {
  518. switch (FF.getValueSize()) {
  519. default: llvm_unreachable("Invalid size!");
  520. case 1: OW->Write8 (uint8_t (FF.getValue())); break;
  521. case 2: OW->Write16(uint16_t(FF.getValue())); break;
  522. case 4: OW->Write32(uint32_t(FF.getValue())); break;
  523. case 8: OW->Write64(uint64_t(FF.getValue())); break;
  524. }
  525. }
  526. break;
  527. }
  528. case MCFragment::FT_LEB: {
  529. MCLEBFragment &LF = cast<MCLEBFragment>(F);
  530. OW->WriteBytes(LF.getContents().str());
  531. break;
  532. }
  533. case MCFragment::FT_Org: {
  534. ++stats::EmittedOrgFragments;
  535. MCOrgFragment &OF = cast<MCOrgFragment>(F);
  536. for (uint64_t i = 0, e = FragmentSize; i != e; ++i)
  537. OW->Write8(uint8_t(OF.getValue()));
  538. break;
  539. }
  540. case MCFragment::FT_Dwarf: {
  541. const MCDwarfLineAddrFragment &OF = cast<MCDwarfLineAddrFragment>(F);
  542. OW->WriteBytes(OF.getContents().str());
  543. break;
  544. }
  545. case MCFragment::FT_DwarfFrame: {
  546. const MCDwarfCallFrameFragment &CF = cast<MCDwarfCallFrameFragment>(F);
  547. OW->WriteBytes(CF.getContents().str());
  548. break;
  549. }
  550. }
  551. assert(OW->getStream().tell() - Start == FragmentSize &&
  552. "The stream should advance by fragment size");
  553. }
  554. void MCAssembler::writeSectionData(const MCSectionData *SD,
  555. const MCAsmLayout &Layout) const {
  556. // Ignore virtual sections.
  557. if (SD->getSection().isVirtualSection()) {
  558. assert(Layout.getSectionFileSize(SD) == 0 && "Invalid size for section!");
  559. // Check that contents are only things legal inside a virtual section.
  560. for (MCSectionData::const_iterator it = SD->begin(),
  561. ie = SD->end(); it != ie; ++it) {
  562. switch (it->getKind()) {
  563. default: llvm_unreachable("Invalid fragment in virtual section!");
  564. case MCFragment::FT_Data: {
  565. // Check that we aren't trying to write a non-zero contents (or fixups)
  566. // into a virtual section. This is to support clients which use standard
  567. // directives to fill the contents of virtual sections.
  568. MCDataFragment &DF = cast<MCDataFragment>(*it);
  569. assert(DF.fixup_begin() == DF.fixup_end() &&
  570. "Cannot have fixups in virtual section!");
  571. for (unsigned i = 0, e = DF.getContents().size(); i != e; ++i)
  572. assert(DF.getContents()[i] == 0 &&
  573. "Invalid data value for virtual section!");
  574. break;
  575. }
  576. case MCFragment::FT_Align:
  577. // Check that we aren't trying to write a non-zero value into a virtual
  578. // section.
  579. assert((!cast<MCAlignFragment>(it)->getValueSize() ||
  580. !cast<MCAlignFragment>(it)->getValue()) &&
  581. "Invalid align in virtual section!");
  582. break;
  583. case MCFragment::FT_Fill:
  584. assert(!cast<MCFillFragment>(it)->getValueSize() &&
  585. "Invalid fill in virtual section!");
  586. break;
  587. }
  588. }
  589. return;
  590. }
  591. uint64_t Start = getWriter().getStream().tell();
  592. (void)Start;
  593. for (MCSectionData::const_iterator it = SD->begin(), ie = SD->end();
  594. it != ie; ++it)
  595. writeFragment(*this, Layout, *it);
  596. assert(getWriter().getStream().tell() - Start ==
  597. Layout.getSectionAddressSize(SD));
  598. }
  599. uint64_t MCAssembler::handleFixup(const MCAsmLayout &Layout,
  600. MCFragment &F,
  601. const MCFixup &Fixup) {
  602. // Evaluate the fixup.
  603. MCValue Target;
  604. uint64_t FixedValue;
  605. if (!evaluateFixup(Layout, Fixup, &F, Target, FixedValue)) {
  606. // The fixup was unresolved, we need a relocation. Inform the object
  607. // writer of the relocation, and give it an opportunity to adjust the
  608. // fixup value if need be.
  609. getWriter().RecordRelocation(*this, Layout, &F, Fixup, Target, FixedValue);
  610. }
  611. return FixedValue;
  612. }
  613. void MCAssembler::Finish() {
  614. DEBUG_WITH_TYPE("mc-dump", {
  615. llvm::errs() << "assembler backend - pre-layout\n--\n";
  616. dump(); });
  617. // Create the layout object.
  618. MCAsmLayout Layout(*this);
  619. // Create dummy fragments and assign section ordinals.
  620. unsigned SectionIndex = 0;
  621. for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
  622. // Create dummy fragments to eliminate any empty sections, this simplifies
  623. // layout.
  624. if (it->getFragmentList().empty())
  625. new MCDataFragment(it);
  626. it->setOrdinal(SectionIndex++);
  627. }
  628. // Assign layout order indices to sections and fragments.
  629. for (unsigned i = 0, e = Layout.getSectionOrder().size(); i != e; ++i) {
  630. MCSectionData *SD = Layout.getSectionOrder()[i];
  631. SD->setLayoutOrder(i);
  632. unsigned FragmentIndex = 0;
  633. for (MCSectionData::iterator iFrag = SD->begin(), iFragEnd = SD->end();
  634. iFrag != iFragEnd; ++iFrag)
  635. iFrag->setLayoutOrder(FragmentIndex++);
  636. }
  637. // Layout until everything fits.
  638. while (layoutOnce(Layout))
  639. continue;
  640. DEBUG_WITH_TYPE("mc-dump", {
  641. llvm::errs() << "assembler backend - post-relaxation\n--\n";
  642. dump(); });
  643. // Finalize the layout, including fragment lowering.
  644. finishLayout(Layout);
  645. DEBUG_WITH_TYPE("mc-dump", {
  646. llvm::errs() << "assembler backend - final-layout\n--\n";
  647. dump(); });
  648. uint64_t StartOffset = OS.tell();
  649. // Allow the object writer a chance to perform post-layout binding (for
  650. // example, to set the index fields in the symbol data).
  651. getWriter().ExecutePostLayoutBinding(*this, Layout);
  652. // Evaluate and apply the fixups, generating relocation entries as necessary.
  653. for (MCAssembler::iterator it = begin(), ie = end(); it != ie; ++it) {
  654. for (MCSectionData::iterator it2 = it->begin(),
  655. ie2 = it->end(); it2 != ie2; ++it2) {
  656. MCEncodedFragmentWithFixups *F =
  657. dyn_cast<MCEncodedFragmentWithFixups>(it2);
  658. if (F) {
  659. for (MCEncodedFragmentWithFixups::fixup_iterator it3 = F->fixup_begin(),
  660. ie3 = F->fixup_end(); it3 != ie3; ++it3) {
  661. MCFixup &Fixup = *it3;
  662. uint64_t FixedValue = handleFixup(Layout, *F, Fixup);
  663. getBackend().applyFixup(Fixup, F->getContents().data(),
  664. F->getContents().size(), FixedValue);
  665. }
  666. }
  667. }
  668. }
  669. // Write the object file.
  670. getWriter().WriteObject(*this, Layout);
  671. stats::ObjectBytes += OS.tell() - StartOffset;
  672. }
  673. bool MCAssembler::fixupNeedsRelaxation(const MCFixup &Fixup,
  674. const MCRelaxableFragment *DF,
  675. const MCAsmLayout &Layout) const {
  676. // If we cannot resolve the fixup value, it requires relaxation.
  677. MCValue Target;
  678. uint64_t Value;
  679. if (!evaluateFixup(Layout, Fixup, DF, Target, Value))
  680. return true;
  681. return getBackend().fixupNeedsRelaxation(Fixup, Value, DF, Layout);
  682. }
  683. bool MCAssembler::fragmentNeedsRelaxation(const MCRelaxableFragment *F,
  684. const MCAsmLayout &Layout) const {
  685. // If this inst doesn't ever need relaxation, ignore it. This occurs when we
  686. // are intentionally pushing out inst fragments, or because we relaxed a
  687. // previous instruction to one that doesn't need relaxation.
  688. if (!getBackend().mayNeedRelaxation(F->getInst()))
  689. return false;
  690. for (MCRelaxableFragment::const_fixup_iterator it = F->fixup_begin(),
  691. ie = F->fixup_end(); it != ie; ++it)
  692. if (fixupNeedsRelaxation(*it, F, Layout))
  693. return true;
  694. return false;
  695. }
  696. bool MCAssembler::relaxInstruction(MCAsmLayout &Layout,
  697. MCRelaxableFragment &F) {
  698. if (!fragmentNeedsRelaxation(&F, Layout))
  699. return false;
  700. ++stats::RelaxedInstructions;
  701. // FIXME-PERF: We could immediately lower out instructions if we can tell
  702. // they are fully resolved, to avoid retesting on later passes.
  703. // Relax the fragment.
  704. MCInst Relaxed;
  705. getBackend().relaxInstruction(F.getInst(), Relaxed);
  706. // Encode the new instruction.
  707. //
  708. // FIXME-PERF: If it matters, we could let the target do this. It can
  709. // probably do so more efficiently in many cases.
  710. SmallVector<MCFixup, 4> Fixups;
  711. SmallString<256> Code;
  712. raw_svector_ostream VecOS(Code);
  713. getEmitter().EncodeInstruction(Relaxed, VecOS, Fixups);
  714. VecOS.flush();
  715. // Update the fragment.
  716. F.setInst(Relaxed);
  717. F.getContents() = Code;
  718. F.getFixups() = Fixups;
  719. return true;
  720. }
  721. bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) {
  722. int64_t Value = 0;
  723. uint64_t OldSize = LF.getContents().size();
  724. bool IsAbs = LF.getValue().EvaluateAsAbsolute(Value, Layout);
  725. (void)IsAbs;
  726. assert(IsAbs);
  727. SmallString<8> &Data = LF.getContents();
  728. Data.clear();
  729. raw_svector_ostream OSE(Data);
  730. if (LF.isSigned())
  731. encodeSLEB128(Value, OSE);
  732. else
  733. encodeULEB128(Value, OSE);
  734. OSE.flush();
  735. return OldSize != LF.getContents().size();
  736. }
  737. bool MCAssembler::relaxDwarfLineAddr(MCAsmLayout &Layout,
  738. MCDwarfLineAddrFragment &DF) {
  739. int64_t AddrDelta = 0;
  740. uint64_t OldSize = DF.getContents().size();
  741. bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
  742. (void)IsAbs;
  743. assert(IsAbs);
  744. int64_t LineDelta;
  745. LineDelta = DF.getLineDelta();
  746. SmallString<8> &Data = DF.getContents();
  747. Data.clear();
  748. raw_svector_ostream OSE(Data);
  749. MCDwarfLineAddr::Encode(LineDelta, AddrDelta, OSE);
  750. OSE.flush();
  751. return OldSize != Data.size();
  752. }
  753. bool MCAssembler::relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
  754. MCDwarfCallFrameFragment &DF) {
  755. int64_t AddrDelta = 0;
  756. uint64_t OldSize = DF.getContents().size();
  757. bool IsAbs = DF.getAddrDelta().EvaluateAsAbsolute(AddrDelta, Layout);
  758. (void)IsAbs;
  759. assert(IsAbs);
  760. SmallString<8> &Data = DF.getContents();
  761. Data.clear();
  762. raw_svector_ostream OSE(Data);
  763. MCDwarfFrameEmitter::EncodeAdvanceLoc(AddrDelta, OSE);
  764. OSE.flush();
  765. return OldSize != Data.size();
  766. }
  767. bool MCAssembler::layoutSectionOnce(MCAsmLayout &Layout, MCSectionData &SD) {
  768. // Holds the first fragment which needed relaxing during this layout. It will
  769. // remain NULL if none were relaxed.
  770. // When a fragment is relaxed, all the fragments following it should get
  771. // invalidated because their offset is going to change.
  772. MCFragment *FirstRelaxedFragment = NULL;
  773. // Attempt to relax all the fragments in the section.
  774. for (MCSectionData::iterator I = SD.begin(), IE = SD.end(); I != IE; ++I) {
  775. // Check if this is a fragment that needs relaxation.
  776. bool RelaxedFrag = false;
  777. switch(I->getKind()) {
  778. default:
  779. break;
  780. case MCFragment::FT_Relaxable:
  781. assert(!getRelaxAll() &&
  782. "Did not expect a MCRelaxableFragment in RelaxAll mode");
  783. RelaxedFrag = relaxInstruction(Layout, *cast<MCRelaxableFragment>(I));
  784. break;
  785. case MCFragment::FT_Dwarf:
  786. RelaxedFrag = relaxDwarfLineAddr(Layout,
  787. *cast<MCDwarfLineAddrFragment>(I));
  788. break;
  789. case MCFragment::FT_DwarfFrame:
  790. RelaxedFrag =
  791. relaxDwarfCallFrameFragment(Layout,
  792. *cast<MCDwarfCallFrameFragment>(I));
  793. break;
  794. case MCFragment::FT_LEB:
  795. RelaxedFrag = relaxLEB(Layout, *cast<MCLEBFragment>(I));
  796. break;
  797. }
  798. if (RelaxedFrag && !FirstRelaxedFragment)
  799. FirstRelaxedFragment = I;
  800. }
  801. if (FirstRelaxedFragment) {
  802. Layout.invalidateFragmentsAfter(FirstRelaxedFragment);
  803. return true;
  804. }
  805. return false;
  806. }
  807. bool MCAssembler::layoutOnce(MCAsmLayout &Layout) {
  808. ++stats::RelaxationSteps;
  809. bool WasRelaxed = false;
  810. for (iterator it = begin(), ie = end(); it != ie; ++it) {
  811. MCSectionData &SD = *it;
  812. while (layoutSectionOnce(Layout, SD))
  813. WasRelaxed = true;
  814. }
  815. return WasRelaxed;
  816. }
  817. void MCAssembler::finishLayout(MCAsmLayout &Layout) {
  818. // The layout is done. Mark every fragment as valid.
  819. for (unsigned int i = 0, n = Layout.getSectionOrder().size(); i != n; ++i) {
  820. Layout.getFragmentOffset(&*Layout.getSectionOrder()[i]->rbegin());
  821. }
  822. }
  823. // Debugging methods
  824. namespace llvm {
  825. raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) {
  826. OS << "<MCFixup" << " Offset:" << AF.getOffset()
  827. << " Value:" << *AF.getValue()
  828. << " Kind:" << AF.getKind() << ">";
  829. return OS;
  830. }
  831. }
  832. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  833. void MCFragment::dump() {
  834. raw_ostream &OS = llvm::errs();
  835. OS << "<";
  836. switch (getKind()) {
  837. case MCFragment::FT_Align: OS << "MCAlignFragment"; break;
  838. case MCFragment::FT_Data: OS << "MCDataFragment"; break;
  839. case MCFragment::FT_CompactEncodedInst:
  840. OS << "MCCompactEncodedInstFragment"; break;
  841. case MCFragment::FT_Fill: OS << "MCFillFragment"; break;
  842. case MCFragment::FT_Relaxable: OS << "MCRelaxableFragment"; break;
  843. case MCFragment::FT_Org: OS << "MCOrgFragment"; break;
  844. case MCFragment::FT_Dwarf: OS << "MCDwarfFragment"; break;
  845. case MCFragment::FT_DwarfFrame: OS << "MCDwarfCallFrameFragment"; break;
  846. case MCFragment::FT_LEB: OS << "MCLEBFragment"; break;
  847. }
  848. OS << "<MCFragment " << (void*) this << " LayoutOrder:" << LayoutOrder
  849. << " Offset:" << Offset
  850. << " HasInstructions:" << hasInstructions()
  851. << " BundlePadding:" << getBundlePadding() << ">";
  852. switch (getKind()) {
  853. case MCFragment::FT_Align: {
  854. const MCAlignFragment *AF = cast<MCAlignFragment>(this);
  855. if (AF->hasEmitNops())
  856. OS << " (emit nops)";
  857. OS << "\n ";
  858. OS << " Alignment:" << AF->getAlignment()
  859. << " Value:" << AF->getValue() << " ValueSize:" << AF->getValueSize()
  860. << " MaxBytesToEmit:" << AF->getMaxBytesToEmit() << ">";
  861. break;
  862. }
  863. case MCFragment::FT_Data: {
  864. const MCDataFragment *DF = cast<MCDataFragment>(this);
  865. OS << "\n ";
  866. OS << " Contents:[";
  867. const SmallVectorImpl<char> &Contents = DF->getContents();
  868. for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
  869. if (i) OS << ",";
  870. OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
  871. }
  872. OS << "] (" << Contents.size() << " bytes)";
  873. if (DF->fixup_begin() != DF->fixup_end()) {
  874. OS << ",\n ";
  875. OS << " Fixups:[";
  876. for (MCDataFragment::const_fixup_iterator it = DF->fixup_begin(),
  877. ie = DF->fixup_end(); it != ie; ++it) {
  878. if (it != DF->fixup_begin()) OS << ",\n ";
  879. OS << *it;
  880. }
  881. OS << "]";
  882. }
  883. break;
  884. }
  885. case MCFragment::FT_CompactEncodedInst: {
  886. const MCCompactEncodedInstFragment *CEIF =
  887. cast<MCCompactEncodedInstFragment>(this);
  888. OS << "\n ";
  889. OS << " Contents:[";
  890. const SmallVectorImpl<char> &Contents = CEIF->getContents();
  891. for (unsigned i = 0, e = Contents.size(); i != e; ++i) {
  892. if (i) OS << ",";
  893. OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF);
  894. }
  895. OS << "] (" << Contents.size() << " bytes)";
  896. break;
  897. }
  898. case MCFragment::FT_Fill: {
  899. const MCFillFragment *FF = cast<MCFillFragment>(this);
  900. OS << " Value:" << FF->getValue() << " ValueSize:" << FF->getValueSize()
  901. << " Size:" << FF->getSize();
  902. break;
  903. }
  904. case MCFragment::FT_Relaxable: {
  905. const MCRelaxableFragment *F = cast<MCRelaxableFragment>(this);
  906. OS << "\n ";
  907. OS << " Inst:";
  908. F->getInst().dump_pretty(OS);
  909. break;
  910. }
  911. case MCFragment::FT_Org: {
  912. const MCOrgFragment *OF = cast<MCOrgFragment>(this);
  913. OS << "\n ";
  914. OS << " Offset:" << OF->getOffset() << " Value:" << OF->getValue();
  915. break;
  916. }
  917. case MCFragment::FT_Dwarf: {
  918. const MCDwarfLineAddrFragment *OF = cast<MCDwarfLineAddrFragment>(this);
  919. OS << "\n ";
  920. OS << " AddrDelta:" << OF->getAddrDelta()
  921. << " LineDelta:" << OF->getLineDelta();
  922. break;
  923. }
  924. case MCFragment::FT_DwarfFrame: {
  925. const MCDwarfCallFrameFragment *CF = cast<MCDwarfCallFrameFragment>(this);
  926. OS << "\n ";
  927. OS << " AddrDelta:" << CF->getAddrDelta();
  928. break;
  929. }
  930. case MCFragment::FT_LEB: {
  931. const MCLEBFragment *LF = cast<MCLEBFragment>(this);
  932. OS << "\n ";
  933. OS << " Value:" << LF->getValue() << " Signed:" << LF->isSigned();
  934. break;
  935. }
  936. }
  937. OS << ">";
  938. }
  939. void MCSectionData::dump() {
  940. raw_ostream &OS = llvm::errs();
  941. OS << "<MCSectionData";
  942. OS << " Alignment:" << getAlignment()
  943. << " Fragments:[\n ";
  944. for (iterator it = begin(), ie = end(); it != ie; ++it) {
  945. if (it != begin()) OS << ",\n ";
  946. it->dump();
  947. }
  948. OS << "]>";
  949. }
  950. void MCSymbolData::dump() {
  951. raw_ostream &OS = llvm::errs();
  952. OS << "<MCSymbolData Symbol:" << getSymbol()
  953. << " Fragment:" << getFragment() << " Offset:" << getOffset()
  954. << " Flags:" << getFlags() << " Index:" << getIndex();
  955. if (isCommon())
  956. OS << " (common, size:" << getCommonSize()
  957. << " align: " << getCommonAlignment() << ")";
  958. if (isExternal())
  959. OS << " (external)";
  960. if (isPrivateExtern())
  961. OS << " (private extern)";
  962. OS << ">";
  963. }
  964. void MCAssembler::dump() {
  965. raw_ostream &OS = llvm::errs();
  966. OS << "<MCAssembler\n";
  967. OS << " Sections:[\n ";
  968. for (iterator it = begin(), ie = end(); it != ie; ++it) {
  969. if (it != begin()) OS << ",\n ";
  970. it->dump();
  971. }
  972. OS << "],\n";
  973. OS << " Symbols:[";
  974. for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) {
  975. if (it != symbol_begin()) OS << ",\n ";
  976. it->dump();
  977. }
  978. OS << "]>\n";
  979. }
  980. #endif
  981. // anchors for MC*Fragment vtables
  982. void MCEncodedFragment::anchor() { }
  983. void MCEncodedFragmentWithFixups::anchor() { }
  984. void MCDataFragment::anchor() { }
  985. void MCCompactEncodedInstFragment::anchor() { }
  986. void MCRelaxableFragment::anchor() { }
  987. void MCAlignFragment::anchor() { }
  988. void MCFillFragment::anchor() { }
  989. void MCOrgFragment::anchor() { }
  990. void MCLEBFragment::anchor() { }
  991. void MCDwarfLineAddrFragment::anchor() { }
  992. void MCDwarfCallFrameFragment::anchor() { }