MCELFStreamer.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
  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. // This file assembles .s files and emits ELF .o object files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/MC/MCELFStreamer.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/MC/MCAsmBackend.h"
  17. #include "llvm/MC/MCAsmLayout.h"
  18. #include "llvm/MC/MCAsmInfo.h"
  19. #include "llvm/MC/MCAssembler.h"
  20. #include "llvm/MC/MCCodeEmitter.h"
  21. #include "llvm/MC/MCContext.h"
  22. #include "llvm/MC/MCExpr.h"
  23. #include "llvm/MC/MCInst.h"
  24. #include "llvm/MC/MCObjectFileInfo.h"
  25. #include "llvm/MC/MCObjectStreamer.h"
  26. #include "llvm/MC/MCSection.h"
  27. #include "llvm/MC/MCSectionELF.h"
  28. #include "llvm/MC/MCSymbolELF.h"
  29. #include "llvm/MC/MCSymbol.h"
  30. #include "llvm/MC/MCValue.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/ELF.h"
  33. #include "llvm/Support/ErrorHandling.h"
  34. #include "llvm/Support/TargetRegistry.h"
  35. #include "llvm/Support/raw_ostream.h"
  36. using namespace llvm;
  37. bool MCELFStreamer::isBundleLocked() const {
  38. return getCurrentSectionOnly()->isBundleLocked();
  39. }
  40. MCELFStreamer::~MCELFStreamer() {
  41. }
  42. void MCELFStreamer::mergeFragment(MCDataFragment *DF,
  43. MCDataFragment *EF) {
  44. MCAssembler &Assembler = getAssembler();
  45. if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
  46. uint64_t FSize = EF->getContents().size();
  47. if (FSize > Assembler.getBundleAlignSize())
  48. report_fatal_error("Fragment can't be larger than a bundle size");
  49. uint64_t RequiredBundlePadding = computeBundlePadding(
  50. Assembler, EF, DF->getContents().size(), FSize);
  51. if (RequiredBundlePadding > UINT8_MAX)
  52. report_fatal_error("Padding cannot exceed 255 bytes");
  53. if (RequiredBundlePadding > 0) {
  54. SmallString<256> Code;
  55. raw_svector_ostream VecOS(Code);
  56. MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
  57. EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
  58. Assembler.writeFragmentPadding(*EF, FSize, OW);
  59. VecOS.flush();
  60. delete OW;
  61. DF->getContents().append(Code.begin(), Code.end());
  62. }
  63. }
  64. flushPendingLabels(DF, DF->getContents().size());
  65. for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
  66. EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
  67. DF->getContents().size());
  68. DF->getFixups().push_back(EF->getFixups()[i]);
  69. }
  70. DF->setHasInstructions(true);
  71. DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
  72. }
  73. void MCELFStreamer::InitSections(bool NoExecStack) {
  74. // This emulates the same behavior of GNU as. This makes it easier
  75. // to compare the output as the major sections are in the same order.
  76. MCContext &Ctx = getContext();
  77. SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
  78. EmitCodeAlignment(4);
  79. SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
  80. EmitCodeAlignment(4);
  81. SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
  82. EmitCodeAlignment(4);
  83. SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
  84. if (NoExecStack)
  85. SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
  86. }
  87. void MCELFStreamer::EmitLabel(MCSymbol *S) {
  88. auto *Symbol = cast<MCSymbolELF>(S);
  89. assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
  90. MCObjectStreamer::EmitLabel(Symbol);
  91. const MCSectionELF &Section =
  92. static_cast<const MCSectionELF&>(Symbol->getSection());
  93. if (Section.getFlags() & ELF::SHF_TLS)
  94. Symbol->setType(ELF::STT_TLS);
  95. }
  96. void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
  97. // Let the target do whatever target specific stuff it needs to do.
  98. getAssembler().getBackend().handleAssemblerFlag(Flag);
  99. // Do any generic stuff we need to do.
  100. switch (Flag) {
  101. case MCAF_SyntaxUnified: return; // no-op here.
  102. case MCAF_Code16: return; // Change parsing mode; no-op here.
  103. case MCAF_Code32: return; // Change parsing mode; no-op here.
  104. case MCAF_Code64: return; // Change parsing mode; no-op here.
  105. case MCAF_SubsectionsViaSymbols:
  106. getAssembler().setSubsectionsViaSymbols(true);
  107. return;
  108. }
  109. llvm_unreachable("invalid assembler flag!");
  110. }
  111. // If bundle aligment is used and there are any instructions in the section, it
  112. // needs to be aligned to at least the bundle size.
  113. static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
  114. MCSection *Section) {
  115. if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
  116. Section->getAlignment() < Assembler.getBundleAlignSize())
  117. Section->setAlignment(Assembler.getBundleAlignSize());
  118. }
  119. void MCELFStreamer::ChangeSection(MCSection *Section,
  120. const MCExpr *Subsection) {
  121. MCSection *CurSection = getCurrentSectionOnly();
  122. if (CurSection && isBundleLocked())
  123. report_fatal_error("Unterminated .bundle_lock when changing a section");
  124. MCAssembler &Asm = getAssembler();
  125. // Ensure the previous section gets aligned if necessary.
  126. setSectionAlignmentForBundling(Asm, CurSection);
  127. auto *SectionELF = static_cast<const MCSectionELF *>(Section);
  128. const MCSymbol *Grp = SectionELF->getGroup();
  129. if (Grp)
  130. Asm.registerSymbol(*Grp);
  131. this->MCObjectStreamer::ChangeSection(Section, Subsection);
  132. MCContext &Ctx = getContext();
  133. auto *Begin = cast_or_null<MCSymbolELF>(Section->getBeginSymbol());
  134. if (!Begin) {
  135. Begin = Ctx.getOrCreateSectionSymbol(*SectionELF);
  136. Section->setBeginSymbol(Begin);
  137. }
  138. if (Begin->isUndefined()) {
  139. Asm.registerSymbol(*Begin);
  140. Begin->setType(ELF::STT_SECTION);
  141. }
  142. }
  143. void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
  144. getAssembler().registerSymbol(*Symbol);
  145. const MCExpr *Value = MCSymbolRefExpr::create(
  146. Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
  147. Alias->setVariableValue(Value);
  148. }
  149. // When GNU as encounters more than one .type declaration for an object it seems
  150. // to use a mechanism similar to the one below to decide which type is actually
  151. // used in the object file. The greater of T1 and T2 is selected based on the
  152. // following ordering:
  153. // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
  154. // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
  155. // provided type).
  156. static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
  157. for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
  158. ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
  159. if (T1 == Type)
  160. return T2;
  161. if (T2 == Type)
  162. return T1;
  163. }
  164. return T2;
  165. }
  166. bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
  167. auto *Symbol = cast<MCSymbolELF>(S);
  168. // Indirect symbols are handled differently, to match how 'as' handles
  169. // them. This makes writing matching .o files easier.
  170. if (Attribute == MCSA_IndirectSymbol) {
  171. // Note that we intentionally cannot use the symbol data here; this is
  172. // important for matching the string table that 'as' generates.
  173. IndirectSymbolData ISD;
  174. ISD.Symbol = Symbol;
  175. ISD.Section = getCurrentSectionOnly();
  176. getAssembler().getIndirectSymbols().push_back(ISD);
  177. return true;
  178. }
  179. // Adding a symbol attribute always introduces the symbol, note that an
  180. // important side effect of calling registerSymbol here is to register
  181. // the symbol with the assembler.
  182. getAssembler().registerSymbol(*Symbol);
  183. // The implementation of symbol attributes is designed to match 'as', but it
  184. // leaves much to desired. It doesn't really make sense to arbitrarily add and
  185. // remove flags, but 'as' allows this (in particular, see .desc).
  186. //
  187. // In the future it might be worth trying to make these operations more well
  188. // defined.
  189. switch (Attribute) {
  190. case MCSA_LazyReference:
  191. case MCSA_Reference:
  192. case MCSA_SymbolResolver:
  193. case MCSA_PrivateExtern:
  194. case MCSA_WeakDefinition:
  195. case MCSA_WeakDefAutoPrivate:
  196. case MCSA_Invalid:
  197. case MCSA_IndirectSymbol:
  198. return false;
  199. case MCSA_NoDeadStrip:
  200. // Ignore for now.
  201. break;
  202. case MCSA_ELF_TypeGnuUniqueObject:
  203. Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
  204. Symbol->setBinding(ELF::STB_GNU_UNIQUE);
  205. Symbol->setExternal(true);
  206. break;
  207. case MCSA_Global:
  208. Symbol->setBinding(ELF::STB_GLOBAL);
  209. Symbol->setExternal(true);
  210. break;
  211. case MCSA_WeakReference:
  212. case MCSA_Weak:
  213. Symbol->setBinding(ELF::STB_WEAK);
  214. Symbol->setExternal(true);
  215. break;
  216. case MCSA_Local:
  217. Symbol->setBinding(ELF::STB_LOCAL);
  218. Symbol->setExternal(false);
  219. break;
  220. case MCSA_ELF_TypeFunction:
  221. Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
  222. break;
  223. case MCSA_ELF_TypeIndFunction:
  224. Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
  225. break;
  226. case MCSA_ELF_TypeObject:
  227. Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
  228. break;
  229. case MCSA_ELF_TypeTLS:
  230. Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
  231. break;
  232. case MCSA_ELF_TypeCommon:
  233. // TODO: Emit these as a common symbol.
  234. Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
  235. break;
  236. case MCSA_ELF_TypeNoType:
  237. Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
  238. break;
  239. case MCSA_Protected:
  240. Symbol->setVisibility(ELF::STV_PROTECTED);
  241. break;
  242. case MCSA_Hidden:
  243. Symbol->setVisibility(ELF::STV_HIDDEN);
  244. break;
  245. case MCSA_Internal:
  246. Symbol->setVisibility(ELF::STV_INTERNAL);
  247. break;
  248. }
  249. return true;
  250. }
  251. void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
  252. unsigned ByteAlignment) {
  253. auto *Symbol = cast<MCSymbolELF>(S);
  254. getAssembler().registerSymbol(*Symbol);
  255. if (!Symbol->isBindingSet()) {
  256. Symbol->setBinding(ELF::STB_GLOBAL);
  257. Symbol->setExternal(true);
  258. }
  259. Symbol->setType(ELF::STT_OBJECT);
  260. if (Symbol->getBinding() == ELF::STB_LOCAL) {
  261. MCSection *Section = getAssembler().getContext().getELFSection(
  262. ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
  263. AssignSection(Symbol, Section);
  264. struct LocalCommon L = {Symbol, Size, ByteAlignment};
  265. LocalCommons.push_back(L);
  266. } else {
  267. if(Symbol->declareCommon(Size, ByteAlignment))
  268. report_fatal_error("Symbol: " + Symbol->getName() +
  269. " redeclared as different type");
  270. }
  271. cast<MCSymbolELF>(Symbol)
  272. ->setSize(MCConstantExpr::create(Size, getContext()));
  273. }
  274. void MCELFStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {
  275. Symbol->setSize(Value);
  276. }
  277. void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
  278. unsigned ByteAlignment) {
  279. auto *Symbol = cast<MCSymbolELF>(S);
  280. // FIXME: Should this be caught and done earlier?
  281. getAssembler().registerSymbol(*Symbol);
  282. Symbol->setBinding(ELF::STB_LOCAL);
  283. Symbol->setExternal(false);
  284. EmitCommonSymbol(Symbol, Size, ByteAlignment);
  285. }
  286. void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
  287. const SMLoc &Loc) {
  288. if (isBundleLocked())
  289. report_fatal_error("Emitting values inside a locked bundle is forbidden");
  290. fixSymbolsInTLSFixups(Value);
  291. MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
  292. }
  293. void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
  294. int64_t Value,
  295. unsigned ValueSize,
  296. unsigned MaxBytesToEmit) {
  297. if (isBundleLocked())
  298. report_fatal_error("Emitting values inside a locked bundle is forbidden");
  299. MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
  300. ValueSize, MaxBytesToEmit);
  301. }
  302. // Add a symbol for the file name of this module. They start after the
  303. // null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
  304. // with the same name may appear.
  305. void MCELFStreamer::EmitFileDirective(StringRef Filename) {
  306. getAssembler().addFileName(Filename);
  307. }
  308. void MCELFStreamer::EmitIdent(StringRef IdentString) {
  309. MCSection *Comment = getAssembler().getContext().getELFSection(
  310. ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
  311. PushSection();
  312. SwitchSection(Comment);
  313. if (!SeenIdent) {
  314. EmitIntValue(0, 1);
  315. SeenIdent = true;
  316. }
  317. EmitBytes(IdentString);
  318. EmitIntValue(0, 1);
  319. PopSection();
  320. }
  321. void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
  322. switch (expr->getKind()) {
  323. case MCExpr::Target:
  324. cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
  325. break;
  326. case MCExpr::Constant:
  327. break;
  328. case MCExpr::Binary: {
  329. const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
  330. fixSymbolsInTLSFixups(be->getLHS());
  331. fixSymbolsInTLSFixups(be->getRHS());
  332. break;
  333. }
  334. case MCExpr::SymbolRef: {
  335. const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
  336. switch (symRef.getKind()) {
  337. default:
  338. return;
  339. case MCSymbolRefExpr::VK_GOTTPOFF:
  340. case MCSymbolRefExpr::VK_INDNTPOFF:
  341. case MCSymbolRefExpr::VK_NTPOFF:
  342. case MCSymbolRefExpr::VK_GOTNTPOFF:
  343. case MCSymbolRefExpr::VK_TLSGD:
  344. case MCSymbolRefExpr::VK_TLSLD:
  345. case MCSymbolRefExpr::VK_TLSLDM:
  346. case MCSymbolRefExpr::VK_TPOFF:
  347. case MCSymbolRefExpr::VK_DTPOFF:
  348. case MCSymbolRefExpr::VK_Mips_TLSGD:
  349. case MCSymbolRefExpr::VK_Mips_GOTTPREL:
  350. case MCSymbolRefExpr::VK_Mips_TPREL_HI:
  351. case MCSymbolRefExpr::VK_Mips_TPREL_LO:
  352. case MCSymbolRefExpr::VK_PPC_DTPMOD:
  353. case MCSymbolRefExpr::VK_PPC_TPREL:
  354. case MCSymbolRefExpr::VK_PPC_TPREL_LO:
  355. case MCSymbolRefExpr::VK_PPC_TPREL_HI:
  356. case MCSymbolRefExpr::VK_PPC_TPREL_HA:
  357. case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
  358. case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
  359. case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
  360. case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
  361. case MCSymbolRefExpr::VK_PPC_DTPREL:
  362. case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
  363. case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
  364. case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
  365. case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
  366. case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
  367. case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
  368. case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
  369. case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
  370. case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
  371. case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
  372. case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
  373. case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
  374. case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
  375. case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
  376. case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
  377. case MCSymbolRefExpr::VK_PPC_TLS:
  378. case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
  379. case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
  380. case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
  381. case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
  382. case MCSymbolRefExpr::VK_PPC_TLSGD:
  383. case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
  384. case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
  385. case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
  386. case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
  387. case MCSymbolRefExpr::VK_PPC_TLSLD:
  388. break;
  389. }
  390. getAssembler().registerSymbol(symRef.getSymbol());
  391. cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
  392. break;
  393. }
  394. case MCExpr::Unary:
  395. fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
  396. break;
  397. }
  398. }
  399. void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
  400. const MCSubtargetInfo &STI) {
  401. this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
  402. MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
  403. for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
  404. fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
  405. }
  406. void MCELFStreamer::EmitInstToData(const MCInst &Inst,
  407. const MCSubtargetInfo &STI) {
  408. MCAssembler &Assembler = getAssembler();
  409. SmallVector<MCFixup, 4> Fixups;
  410. SmallString<256> Code;
  411. raw_svector_ostream VecOS(Code);
  412. Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
  413. VecOS.flush();
  414. for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
  415. fixSymbolsInTLSFixups(Fixups[i].getValue());
  416. // There are several possibilities here:
  417. //
  418. // If bundling is disabled, append the encoded instruction to the current data
  419. // fragment (or create a new such fragment if the current fragment is not a
  420. // data fragment).
  421. //
  422. // If bundling is enabled:
  423. // - If we're not in a bundle-locked group, emit the instruction into a
  424. // fragment of its own. If there are no fixups registered for the
  425. // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
  426. // MCDataFragment.
  427. // - If we're in a bundle-locked group, append the instruction to the current
  428. // data fragment because we want all the instructions in a group to get into
  429. // the same fragment. Be careful not to do that for the first instruction in
  430. // the group, though.
  431. MCDataFragment *DF;
  432. if (Assembler.isBundlingEnabled()) {
  433. MCSection &Sec = *getCurrentSectionOnly();
  434. if (Assembler.getRelaxAll() && isBundleLocked())
  435. // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
  436. // the current bundle group.
  437. DF = BundleGroups.back();
  438. else if (Assembler.getRelaxAll() && !isBundleLocked())
  439. // When not in a bundle-locked group and the -mc-relax-all flag is used,
  440. // we create a new temporary fragment which will be later merged into
  441. // the current fragment.
  442. DF = new MCDataFragment();
  443. else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst())
  444. // If we are bundle-locked, we re-use the current fragment.
  445. // The bundle-locking directive ensures this is a new data fragment.
  446. DF = cast<MCDataFragment>(getCurrentFragment());
  447. else if (!isBundleLocked() && Fixups.size() == 0) {
  448. // Optimize memory usage by emitting the instruction to a
  449. // MCCompactEncodedInstFragment when not in a bundle-locked group and
  450. // there are no fixups registered.
  451. MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
  452. insert(CEIF);
  453. CEIF->getContents().append(Code.begin(), Code.end());
  454. return;
  455. } else {
  456. DF = new MCDataFragment();
  457. insert(DF);
  458. }
  459. if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
  460. // If this fragment is for a group marked "align_to_end", set a flag
  461. // in the fragment. This can happen after the fragment has already been
  462. // created if there are nested bundle_align groups and an inner one
  463. // is the one marked align_to_end.
  464. DF->setAlignToBundleEnd(true);
  465. }
  466. // We're now emitting an instruction in a bundle group, so this flag has
  467. // to be turned off.
  468. Sec.setBundleGroupBeforeFirstInst(false);
  469. } else {
  470. DF = getOrCreateDataFragment();
  471. }
  472. // Add the fixups and data.
  473. for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
  474. Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
  475. DF->getFixups().push_back(Fixups[i]);
  476. }
  477. DF->setHasInstructions(true);
  478. DF->getContents().append(Code.begin(), Code.end());
  479. if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
  480. if (!isBundleLocked()) {
  481. mergeFragment(getOrCreateDataFragment(), DF);
  482. delete DF;
  483. }
  484. }
  485. }
  486. void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
  487. assert(AlignPow2 <= 30 && "Invalid bundle alignment");
  488. MCAssembler &Assembler = getAssembler();
  489. if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
  490. Assembler.getBundleAlignSize() == 1U << AlignPow2))
  491. Assembler.setBundleAlignSize(1U << AlignPow2);
  492. else
  493. report_fatal_error(".bundle_align_mode cannot be changed once set");
  494. }
  495. void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
  496. MCSection &Sec = *getCurrentSectionOnly();
  497. // Sanity checks
  498. //
  499. if (!getAssembler().isBundlingEnabled())
  500. report_fatal_error(".bundle_lock forbidden when bundling is disabled");
  501. if (!isBundleLocked())
  502. Sec.setBundleGroupBeforeFirstInst(true);
  503. if (getAssembler().getRelaxAll() && !isBundleLocked()) {
  504. // TODO: drop the lock state and set directly in the fragment
  505. MCDataFragment *DF = new MCDataFragment();
  506. BundleGroups.push_back(DF);
  507. }
  508. Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
  509. : MCSection::BundleLocked);
  510. }
  511. void MCELFStreamer::EmitBundleUnlock() {
  512. MCSection &Sec = *getCurrentSectionOnly();
  513. // Sanity checks
  514. if (!getAssembler().isBundlingEnabled())
  515. report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
  516. else if (!isBundleLocked())
  517. report_fatal_error(".bundle_unlock without matching lock");
  518. else if (Sec.isBundleGroupBeforeFirstInst())
  519. report_fatal_error("Empty bundle-locked group is forbidden");
  520. // When the -mc-relax-all flag is used, we emit instructions to fragments
  521. // stored on a stack. When the bundle unlock is emited, we pop a fragment
  522. // from the stack a merge it to the one below.
  523. if (getAssembler().getRelaxAll()) {
  524. assert(!BundleGroups.empty() && "There are no bundle groups");
  525. MCDataFragment *DF = BundleGroups.back();
  526. // FIXME: Use BundleGroups to track the lock state instead.
  527. Sec.setBundleLockState(MCSection::NotBundleLocked);
  528. // FIXME: Use more separate fragments for nested groups.
  529. if (!isBundleLocked()) {
  530. mergeFragment(getOrCreateDataFragment(), DF);
  531. BundleGroups.pop_back();
  532. delete DF;
  533. }
  534. if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
  535. getOrCreateDataFragment()->setAlignToBundleEnd(false);
  536. } else
  537. Sec.setBundleLockState(MCSection::NotBundleLocked);
  538. }
  539. void MCELFStreamer::Flush() {
  540. for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
  541. e = LocalCommons.end();
  542. i != e; ++i) {
  543. const MCSymbol &Symbol = *i->Symbol;
  544. uint64_t Size = i->Size;
  545. unsigned ByteAlignment = i->ByteAlignment;
  546. MCSection &Section = Symbol.getSection();
  547. getAssembler().registerSection(Section);
  548. new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &Section);
  549. MCFragment *F = new MCFillFragment(0, 0, Size, &Section);
  550. Symbol.setFragment(F);
  551. // Update the maximum alignment of the section if necessary.
  552. if (ByteAlignment > Section.getAlignment())
  553. Section.setAlignment(ByteAlignment);
  554. }
  555. LocalCommons.clear();
  556. }
  557. void MCELFStreamer::FinishImpl() {
  558. // Ensure the last section gets aligned if necessary.
  559. MCSection *CurSection = getCurrentSectionOnly();
  560. setSectionAlignmentForBundling(getAssembler(), CurSection);
  561. EmitFrames(nullptr);
  562. Flush();
  563. this->MCObjectStreamer::FinishImpl();
  564. }
  565. MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
  566. raw_pwrite_stream &OS, MCCodeEmitter *CE,
  567. bool RelaxAll) {
  568. MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
  569. if (RelaxAll)
  570. S->getAssembler().setRelaxAll(true);
  571. return S;
  572. }
  573. void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
  574. llvm_unreachable("Generic ELF doesn't support this directive");
  575. }
  576. void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
  577. llvm_unreachable("ELF doesn't support this directive");
  578. }
  579. void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
  580. llvm_unreachable("ELF doesn't support this directive");
  581. }
  582. void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
  583. llvm_unreachable("ELF doesn't support this directive");
  584. }
  585. void MCELFStreamer::EmitCOFFSymbolType(int Type) {
  586. llvm_unreachable("ELF doesn't support this directive");
  587. }
  588. void MCELFStreamer::EndCOFFSymbolDef() {
  589. llvm_unreachable("ELF doesn't support this directive");
  590. }
  591. void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
  592. uint64_t Size, unsigned ByteAlignment) {
  593. llvm_unreachable("ELF doesn't support this directive");
  594. }
  595. void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
  596. uint64_t Size, unsigned ByteAlignment) {
  597. llvm_unreachable("ELF doesn't support this directive");
  598. }