MCAssembler.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
  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. #ifndef LLVM_MC_MCASSEMBLER_H
  10. #define LLVM_MC_MCASSEMBLER_H
  11. #include "llvm/ADT/DenseMap.h"
  12. #include "llvm/ADT/DenseSet.h"
  13. #include "llvm/ADT/SmallPtrSet.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/ilist.h"
  16. #include "llvm/ADT/ilist_node.h"
  17. #include "llvm/ADT/iterator.h"
  18. #include "llvm/MC/MCDirectives.h"
  19. #include "llvm/MC/MCFixup.h"
  20. #include "llvm/MC/MCInst.h"
  21. #include "llvm/MC/MCLinkerOptimizationHint.h"
  22. #include "llvm/MC/MCSection.h"
  23. #include "llvm/MC/MCSubtargetInfo.h"
  24. #include "llvm/Support/Casting.h"
  25. #include "llvm/Support/DataTypes.h"
  26. #include <algorithm>
  27. #include <vector> // FIXME: Shouldn't be needed.
  28. namespace llvm {
  29. class raw_ostream;
  30. class MCAsmLayout;
  31. class MCAssembler;
  32. class MCContext;
  33. class MCCodeEmitter;
  34. class MCExpr;
  35. class MCFragment;
  36. class MCObjectWriter;
  37. class MCSection;
  38. class MCSubtargetInfo;
  39. class MCValue;
  40. class MCAsmBackend;
  41. class MCFragment : public ilist_node<MCFragment> {
  42. friend class MCAsmLayout;
  43. MCFragment(const MCFragment &) = delete;
  44. void operator=(const MCFragment &) = delete;
  45. public:
  46. enum FragmentType : uint8_t {
  47. FT_Align,
  48. FT_Data,
  49. FT_CompactEncodedInst,
  50. FT_Fill,
  51. FT_Relaxable,
  52. FT_Org,
  53. FT_Dwarf,
  54. FT_DwarfFrame,
  55. FT_LEB,
  56. FT_SafeSEH
  57. };
  58. private:
  59. FragmentType Kind;
  60. protected:
  61. bool HasInstructions;
  62. private:
  63. /// \brief Should this fragment be aligned to the end of a bundle?
  64. bool AlignToBundleEnd;
  65. uint8_t BundlePadding;
  66. /// LayoutOrder - The layout order of this fragment.
  67. unsigned LayoutOrder;
  68. /// The data for the section this fragment is in.
  69. MCSection *Parent;
  70. /// Atom - The atom this fragment is in, as represented by it's defining
  71. /// symbol.
  72. const MCSymbol *Atom;
  73. /// \name Assembler Backend Data
  74. /// @{
  75. //
  76. // FIXME: This could all be kept private to the assembler implementation.
  77. /// Offset - The offset of this fragment in its section. This is ~0 until
  78. /// initialized.
  79. uint64_t Offset;
  80. /// @}
  81. protected:
  82. MCFragment(FragmentType Kind, bool HasInstructions,
  83. uint8_t BundlePadding, MCSection *Parent = nullptr);
  84. ~MCFragment();
  85. private:
  86. // This is a friend so that the sentinal can be created.
  87. friend struct ilist_sentinel_traits<MCFragment>;
  88. MCFragment();
  89. public:
  90. /// Destroys the current fragment.
  91. ///
  92. /// This must be used instead of delete as MCFragment is non-virtual.
  93. /// This method will dispatch to the appropriate subclass.
  94. void destroy();
  95. FragmentType getKind() const { return Kind; }
  96. MCSection *getParent() const { return Parent; }
  97. void setParent(MCSection *Value) { Parent = Value; }
  98. const MCSymbol *getAtom() const { return Atom; }
  99. void setAtom(const MCSymbol *Value) { Atom = Value; }
  100. unsigned getLayoutOrder() const { return LayoutOrder; }
  101. void setLayoutOrder(unsigned Value) { LayoutOrder = Value; }
  102. /// \brief Does this fragment have instructions emitted into it? By default
  103. /// this is false, but specific fragment types may set it to true.
  104. bool hasInstructions() const { return HasInstructions; }
  105. /// \brief Should this fragment be placed at the end of an aligned bundle?
  106. bool alignToBundleEnd() const { return AlignToBundleEnd; }
  107. void setAlignToBundleEnd(bool V) { AlignToBundleEnd = V; }
  108. /// \brief Get the padding size that must be inserted before this fragment.
  109. /// Used for bundling. By default, no padding is inserted.
  110. /// Note that padding size is restricted to 8 bits. This is an optimization
  111. /// to reduce the amount of space used for each fragment. In practice, larger
  112. /// padding should never be required.
  113. uint8_t getBundlePadding() const { return BundlePadding; }
  114. /// \brief Set the padding size for this fragment. By default it's a no-op,
  115. /// and only some fragments have a meaningful implementation.
  116. void setBundlePadding(uint8_t N) { BundlePadding = N; }
  117. void dump();
  118. };
  119. /// Interface implemented by fragments that contain encoded instructions and/or
  120. /// data.
  121. ///
  122. class MCEncodedFragment : public MCFragment {
  123. protected:
  124. MCEncodedFragment(MCFragment::FragmentType FType, bool HasInstructions,
  125. MCSection *Sec)
  126. : MCFragment(FType, HasInstructions, 0, Sec) {}
  127. public:
  128. static bool classof(const MCFragment *F) {
  129. MCFragment::FragmentType Kind = F->getKind();
  130. switch (Kind) {
  131. default:
  132. return false;
  133. case MCFragment::FT_Relaxable:
  134. case MCFragment::FT_CompactEncodedInst:
  135. case MCFragment::FT_Data:
  136. return true;
  137. }
  138. }
  139. };
  140. /// Interface implemented by fragments that contain encoded instructions and/or
  141. /// data.
  142. ///
  143. template<unsigned ContentsSize>
  144. class MCEncodedFragmentWithContents : public MCEncodedFragment {
  145. SmallVector<char, ContentsSize> Contents;
  146. protected:
  147. MCEncodedFragmentWithContents(MCFragment::FragmentType FType,
  148. bool HasInstructions,
  149. MCSection *Sec)
  150. : MCEncodedFragment(FType, HasInstructions, Sec) {}
  151. public:
  152. SmallVectorImpl<char> &getContents() { return Contents; }
  153. const SmallVectorImpl<char> &getContents() const { return Contents; }
  154. };
  155. /// Interface implemented by fragments that contain encoded instructions and/or
  156. /// data and also have fixups registered.
  157. ///
  158. template<unsigned ContentsSize, unsigned FixupsSize>
  159. class MCEncodedFragmentWithFixups :
  160. public MCEncodedFragmentWithContents<ContentsSize> {
  161. /// Fixups - The list of fixups in this fragment.
  162. SmallVector<MCFixup, FixupsSize> Fixups;
  163. protected:
  164. MCEncodedFragmentWithFixups(MCFragment::FragmentType FType,
  165. bool HasInstructions,
  166. MCSection *Sec)
  167. : MCEncodedFragmentWithContents<ContentsSize>(FType, HasInstructions,
  168. Sec) {}
  169. public:
  170. typedef SmallVectorImpl<MCFixup>::const_iterator const_fixup_iterator;
  171. typedef SmallVectorImpl<MCFixup>::iterator fixup_iterator;
  172. SmallVectorImpl<MCFixup> &getFixups() { return Fixups; }
  173. const SmallVectorImpl<MCFixup> &getFixups() const { return Fixups; }
  174. fixup_iterator fixup_begin() { return Fixups.begin(); }
  175. const_fixup_iterator fixup_begin() const { return Fixups.begin(); }
  176. fixup_iterator fixup_end() { return Fixups.end(); }
  177. const_fixup_iterator fixup_end() const { return Fixups.end(); }
  178. static bool classof(const MCFragment *F) {
  179. MCFragment::FragmentType Kind = F->getKind();
  180. return Kind == MCFragment::FT_Relaxable || Kind == MCFragment::FT_Data;
  181. }
  182. };
  183. /// Fragment for data and encoded instructions.
  184. ///
  185. class MCDataFragment : public MCEncodedFragmentWithFixups<32, 4> {
  186. public:
  187. MCDataFragment(MCSection *Sec = nullptr)
  188. : MCEncodedFragmentWithFixups<32, 4>(FT_Data, false, Sec) {}
  189. void setHasInstructions(bool V) { HasInstructions = V; }
  190. static bool classof(const MCFragment *F) {
  191. return F->getKind() == MCFragment::FT_Data;
  192. }
  193. };
  194. /// This is a compact (memory-size-wise) fragment for holding an encoded
  195. /// instruction (non-relaxable) that has no fixups registered. When applicable,
  196. /// it can be used instead of MCDataFragment and lead to lower memory
  197. /// consumption.
  198. ///
  199. class MCCompactEncodedInstFragment : public MCEncodedFragmentWithContents<4> {
  200. public:
  201. MCCompactEncodedInstFragment(MCSection *Sec = nullptr)
  202. : MCEncodedFragmentWithContents(FT_CompactEncodedInst, true, Sec) {
  203. }
  204. static bool classof(const MCFragment *F) {
  205. return F->getKind() == MCFragment::FT_CompactEncodedInst;
  206. }
  207. };
  208. /// A relaxable fragment holds on to its MCInst, since it may need to be
  209. /// relaxed during the assembler layout and relaxation stage.
  210. ///
  211. class MCRelaxableFragment : public MCEncodedFragmentWithFixups<8, 1> {
  212. /// Inst - The instruction this is a fragment for.
  213. MCInst Inst;
  214. /// STI - The MCSubtargetInfo in effect when the instruction was encoded.
  215. /// Keep a copy instead of a reference to make sure that updates to STI
  216. /// in the assembler are not seen here.
  217. const MCSubtargetInfo STI;
  218. public:
  219. MCRelaxableFragment(const MCInst &Inst, const MCSubtargetInfo &STI,
  220. MCSection *Sec = nullptr)
  221. : MCEncodedFragmentWithFixups(FT_Relaxable, true, Sec),
  222. Inst(Inst), STI(STI) {}
  223. const MCInst &getInst() const { return Inst; }
  224. void setInst(const MCInst &Value) { Inst = Value; }
  225. const MCSubtargetInfo &getSubtargetInfo() { return STI; }
  226. static bool classof(const MCFragment *F) {
  227. return F->getKind() == MCFragment::FT_Relaxable;
  228. }
  229. };
  230. class MCAlignFragment : public MCFragment {
  231. /// Alignment - The alignment to ensure, in bytes.
  232. unsigned Alignment;
  233. /// EmitNops - Flag to indicate that (optimal) NOPs should be emitted instead
  234. /// of using the provided value. The exact interpretation of this flag is
  235. /// target dependent.
  236. bool EmitNops : 1;
  237. /// Value - Value to use for filling padding bytes.
  238. int64_t Value;
  239. /// ValueSize - The size of the integer (in bytes) of \p Value.
  240. unsigned ValueSize;
  241. /// MaxBytesToEmit - The maximum number of bytes to emit; if the alignment
  242. /// cannot be satisfied in this width then this fragment is ignored.
  243. unsigned MaxBytesToEmit;
  244. public:
  245. MCAlignFragment(unsigned Alignment, int64_t Value, unsigned ValueSize,
  246. unsigned MaxBytesToEmit, MCSection *Sec = nullptr)
  247. : MCFragment(FT_Align, false, 0, Sec), Alignment(Alignment),
  248. EmitNops(false), Value(Value),
  249. ValueSize(ValueSize), MaxBytesToEmit(MaxBytesToEmit) {}
  250. /// \name Accessors
  251. /// @{
  252. unsigned getAlignment() const { return Alignment; }
  253. int64_t getValue() const { return Value; }
  254. unsigned getValueSize() const { return ValueSize; }
  255. unsigned getMaxBytesToEmit() const { return MaxBytesToEmit; }
  256. bool hasEmitNops() const { return EmitNops; }
  257. void setEmitNops(bool Value) { EmitNops = Value; }
  258. /// @}
  259. static bool classof(const MCFragment *F) {
  260. return F->getKind() == MCFragment::FT_Align;
  261. }
  262. };
  263. class MCFillFragment : public MCFragment {
  264. /// Value - Value to use for filling bytes.
  265. int64_t Value;
  266. /// ValueSize - The size (in bytes) of \p Value to use when filling, or 0 if
  267. /// this is a virtual fill fragment.
  268. unsigned ValueSize;
  269. /// Size - The number of bytes to insert.
  270. uint64_t Size;
  271. public:
  272. MCFillFragment(int64_t Value, unsigned ValueSize, uint64_t Size,
  273. MCSection *Sec = nullptr)
  274. : MCFragment(FT_Fill, false, 0, Sec), Value(Value), ValueSize(ValueSize),
  275. Size(Size) {
  276. assert((!ValueSize || (Size % ValueSize) == 0) &&
  277. "Fill size must be a multiple of the value size!");
  278. }
  279. /// \name Accessors
  280. /// @{
  281. int64_t getValue() const { return Value; }
  282. unsigned getValueSize() const { return ValueSize; }
  283. uint64_t getSize() const { return Size; }
  284. /// @}
  285. static bool classof(const MCFragment *F) {
  286. return F->getKind() == MCFragment::FT_Fill;
  287. }
  288. };
  289. class MCOrgFragment : public MCFragment {
  290. /// Offset - The offset this fragment should start at.
  291. const MCExpr *Offset;
  292. /// Value - Value to use for filling bytes.
  293. int8_t Value;
  294. public:
  295. MCOrgFragment(const MCExpr &Offset, int8_t Value, MCSection *Sec = nullptr)
  296. : MCFragment(FT_Org, false, 0, Sec), Offset(&Offset), Value(Value) {}
  297. /// \name Accessors
  298. /// @{
  299. const MCExpr &getOffset() const { return *Offset; }
  300. uint8_t getValue() const { return Value; }
  301. /// @}
  302. static bool classof(const MCFragment *F) {
  303. return F->getKind() == MCFragment::FT_Org;
  304. }
  305. };
  306. class MCLEBFragment : public MCFragment {
  307. /// Value - The value this fragment should contain.
  308. const MCExpr *Value;
  309. /// IsSigned - True if this is a sleb128, false if uleb128.
  310. bool IsSigned;
  311. SmallString<8> Contents;
  312. public:
  313. MCLEBFragment(const MCExpr &Value_, bool IsSigned_, MCSection *Sec = nullptr)
  314. : MCFragment(FT_LEB, false, 0, Sec), Value(&Value_), IsSigned(IsSigned_) {
  315. Contents.push_back(0);
  316. }
  317. /// \name Accessors
  318. /// @{
  319. const MCExpr &getValue() const { return *Value; }
  320. bool isSigned() const { return IsSigned; }
  321. SmallString<8> &getContents() { return Contents; }
  322. const SmallString<8> &getContents() const { return Contents; }
  323. /// @}
  324. static bool classof(const MCFragment *F) {
  325. return F->getKind() == MCFragment::FT_LEB;
  326. }
  327. };
  328. class MCDwarfLineAddrFragment : public MCFragment {
  329. /// LineDelta - the value of the difference between the two line numbers
  330. /// between two .loc dwarf directives.
  331. int64_t LineDelta;
  332. /// AddrDelta - The expression for the difference of the two symbols that
  333. /// make up the address delta between two .loc dwarf directives.
  334. const MCExpr *AddrDelta;
  335. SmallString<8> Contents;
  336. public:
  337. MCDwarfLineAddrFragment(int64_t LineDelta, const MCExpr &AddrDelta,
  338. MCSection *Sec = nullptr)
  339. : MCFragment(FT_Dwarf, false, 0, Sec), LineDelta(LineDelta),
  340. AddrDelta(&AddrDelta) {
  341. Contents.push_back(0);
  342. }
  343. /// \name Accessors
  344. /// @{
  345. int64_t getLineDelta() const { return LineDelta; }
  346. const MCExpr &getAddrDelta() const { return *AddrDelta; }
  347. SmallString<8> &getContents() { return Contents; }
  348. const SmallString<8> &getContents() const { return Contents; }
  349. /// @}
  350. static bool classof(const MCFragment *F) {
  351. return F->getKind() == MCFragment::FT_Dwarf;
  352. }
  353. };
  354. class MCDwarfCallFrameFragment : public MCFragment {
  355. /// AddrDelta - The expression for the difference of the two symbols that
  356. /// make up the address delta between two .cfi_* dwarf directives.
  357. const MCExpr *AddrDelta;
  358. SmallString<8> Contents;
  359. public:
  360. MCDwarfCallFrameFragment(const MCExpr &AddrDelta, MCSection *Sec = nullptr)
  361. : MCFragment(FT_DwarfFrame, false, 0, Sec), AddrDelta(&AddrDelta) {
  362. Contents.push_back(0);
  363. }
  364. /// \name Accessors
  365. /// @{
  366. const MCExpr &getAddrDelta() const { return *AddrDelta; }
  367. SmallString<8> &getContents() { return Contents; }
  368. const SmallString<8> &getContents() const { return Contents; }
  369. /// @}
  370. static bool classof(const MCFragment *F) {
  371. return F->getKind() == MCFragment::FT_DwarfFrame;
  372. }
  373. };
  374. class MCSafeSEHFragment : public MCFragment {
  375. const MCSymbol *Sym;
  376. public:
  377. MCSafeSEHFragment(const MCSymbol *Sym, MCSection *Sec = nullptr)
  378. : MCFragment(FT_SafeSEH, false, 0, Sec), Sym(Sym) {}
  379. /// \name Accessors
  380. /// @{
  381. const MCSymbol *getSymbol() { return Sym; }
  382. const MCSymbol *getSymbol() const { return Sym; }
  383. /// @}
  384. static bool classof(const MCFragment *F) {
  385. return F->getKind() == MCFragment::FT_SafeSEH;
  386. }
  387. };
  388. // FIXME: This really doesn't belong here. See comments below.
  389. struct IndirectSymbolData {
  390. MCSymbol *Symbol;
  391. MCSection *Section;
  392. };
  393. // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
  394. // to one another.
  395. struct DataRegionData {
  396. // This enum should be kept in sync w/ the mach-o definition in
  397. // llvm/Object/MachOFormat.h.
  398. enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
  399. MCSymbol *Start;
  400. MCSymbol *End;
  401. };
  402. class MCAssembler {
  403. friend class MCAsmLayout;
  404. public:
  405. typedef std::vector<MCSection *> SectionListType;
  406. typedef std::vector<const MCSymbol *> SymbolDataListType;
  407. typedef pointee_iterator<SectionListType::const_iterator> const_iterator;
  408. typedef pointee_iterator<SectionListType::iterator> iterator;
  409. typedef pointee_iterator<SymbolDataListType::const_iterator>
  410. const_symbol_iterator;
  411. typedef pointee_iterator<SymbolDataListType::iterator> symbol_iterator;
  412. typedef iterator_range<symbol_iterator> symbol_range;
  413. typedef iterator_range<const_symbol_iterator> const_symbol_range;
  414. typedef std::vector<IndirectSymbolData>::const_iterator
  415. const_indirect_symbol_iterator;
  416. typedef std::vector<IndirectSymbolData>::iterator indirect_symbol_iterator;
  417. typedef std::vector<DataRegionData>::const_iterator
  418. const_data_region_iterator;
  419. typedef std::vector<DataRegionData>::iterator data_region_iterator;
  420. /// MachO specific deployment target version info.
  421. // A Major version of 0 indicates that no version information was supplied
  422. // and so the corresponding load command should not be emitted.
  423. typedef struct {
  424. MCVersionMinType Kind;
  425. unsigned Major;
  426. unsigned Minor;
  427. unsigned Update;
  428. } VersionMinInfoType;
  429. private:
  430. MCAssembler(const MCAssembler &) = delete;
  431. void operator=(const MCAssembler &) = delete;
  432. MCContext &Context;
  433. MCAsmBackend &Backend;
  434. MCCodeEmitter &Emitter;
  435. MCObjectWriter &Writer;
  436. raw_ostream &OS;
  437. SectionListType Sections;
  438. SymbolDataListType Symbols;
  439. std::vector<IndirectSymbolData> IndirectSymbols;
  440. std::vector<DataRegionData> DataRegions;
  441. /// The list of linker options to propagate into the object file.
  442. std::vector<std::vector<std::string>> LinkerOptions;
  443. /// List of declared file names
  444. std::vector<std::string> FileNames;
  445. /// The set of function symbols for which a .thumb_func directive has
  446. /// been seen.
  447. //
  448. // FIXME: We really would like this in target specific code rather than
  449. // here. Maybe when the relocation stuff moves to target specific,
  450. // this can go with it? The streamer would need some target specific
  451. // refactoring too.
  452. mutable SmallPtrSet<const MCSymbol *, 64> ThumbFuncs;
  453. /// \brief The bundle alignment size currently set in the assembler.
  454. ///
  455. /// By default it's 0, which means bundling is disabled.
  456. unsigned BundleAlignSize;
  457. unsigned RelaxAll : 1;
  458. unsigned SubsectionsViaSymbols : 1;
  459. /// ELF specific e_header flags
  460. // It would be good if there were an MCELFAssembler class to hold this.
  461. // ELF header flags are used both by the integrated and standalone assemblers.
  462. // Access to the flags is necessary in cases where assembler directives affect
  463. // which flags to be set.
  464. unsigned ELFHeaderEFlags;
  465. /// Used to communicate Linker Optimization Hint information between
  466. /// the Streamer and the .o writer
  467. MCLOHContainer LOHContainer;
  468. VersionMinInfoType VersionMinInfo;
  469. private:
  470. /// Evaluate a fixup to a relocatable expression and the value which should be
  471. /// placed into the fixup.
  472. ///
  473. /// \param Layout The layout to use for evaluation.
  474. /// \param Fixup The fixup to evaluate.
  475. /// \param DF The fragment the fixup is inside.
  476. /// \param Target [out] On return, the relocatable expression the fixup
  477. /// evaluates to.
  478. /// \param Value [out] On return, the value of the fixup as currently laid
  479. /// out.
  480. /// \return Whether the fixup value was fully resolved. This is true if the
  481. /// \p Value result is fixed, otherwise the value may change due to
  482. /// relocation.
  483. bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
  484. const MCFragment *DF, MCValue &Target,
  485. uint64_t &Value) const;
  486. /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
  487. /// (increased in size, in order to hold its value correctly).
  488. bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
  489. const MCAsmLayout &Layout) const;
  490. /// Check whether the given fragment needs relaxation.
  491. bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
  492. const MCAsmLayout &Layout) const;
  493. /// \brief Perform one layout iteration and return true if any offsets
  494. /// were adjusted.
  495. bool layoutOnce(MCAsmLayout &Layout);
  496. /// \brief Perform one layout iteration of the given section and return true
  497. /// if any offsets were adjusted.
  498. bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
  499. bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
  500. bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
  501. bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
  502. bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
  503. MCDwarfCallFrameFragment &DF);
  504. /// finishLayout - Finalize a layout, including fragment lowering.
  505. void finishLayout(MCAsmLayout &Layout);
  506. std::pair<uint64_t, bool> handleFixup(const MCAsmLayout &Layout,
  507. MCFragment &F, const MCFixup &Fixup);
  508. public:
  509. /// Compute the effective fragment size assuming it is laid out at the given
  510. /// \p SectionAddress and \p FragmentOffset.
  511. uint64_t computeFragmentSize(const MCAsmLayout &Layout,
  512. const MCFragment &F) const;
  513. /// Find the symbol which defines the atom containing the given symbol, or
  514. /// null if there is no such symbol.
  515. const MCSymbol *getAtom(const MCSymbol &S) const;
  516. /// Check whether a particular symbol is visible to the linker and is required
  517. /// in the symbol table, or whether it can be discarded by the assembler. This
  518. /// also effects whether the assembler treats the label as potentially
  519. /// defining a separate atom.
  520. bool isSymbolLinkerVisible(const MCSymbol &SD) const;
  521. /// Emit the section contents using the given object writer.
  522. void writeSectionData(const MCSection *Section,
  523. const MCAsmLayout &Layout) const;
  524. /// Check whether a given symbol has been flagged with .thumb_func.
  525. bool isThumbFunc(const MCSymbol *Func) const;
  526. /// Flag a function symbol as the target of a .thumb_func directive.
  527. void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
  528. /// ELF e_header flags
  529. unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
  530. void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
  531. /// MachO deployment target version information.
  532. const VersionMinInfoType &getVersionMinInfo() const { return VersionMinInfo; }
  533. void setVersionMinInfo(MCVersionMinType Kind, unsigned Major, unsigned Minor,
  534. unsigned Update) {
  535. VersionMinInfo.Kind = Kind;
  536. VersionMinInfo.Major = Major;
  537. VersionMinInfo.Minor = Minor;
  538. VersionMinInfo.Update = Update;
  539. }
  540. public:
  541. /// Construct a new assembler instance.
  542. ///
  543. /// \param OS The stream to output to.
  544. //
  545. // FIXME: How are we going to parameterize this? Two obvious options are stay
  546. // concrete and require clients to pass in a target like object. The other
  547. // option is to make this abstract, and have targets provide concrete
  548. // implementations as we do with AsmParser.
  549. MCAssembler(MCContext &Context_, MCAsmBackend &Backend_,
  550. MCCodeEmitter &Emitter_, MCObjectWriter &Writer_,
  551. raw_ostream &OS);
  552. ~MCAssembler();
  553. /// Reuse an assembler instance
  554. ///
  555. void reset();
  556. MCContext &getContext() const { return Context; }
  557. MCAsmBackend &getBackend() const { return Backend; }
  558. MCCodeEmitter &getEmitter() const { return Emitter; }
  559. MCObjectWriter &getWriter() const { return Writer; }
  560. /// Finish - Do final processing and write the object to the output stream.
  561. /// \p Writer is used for custom object writer (as the MCJIT does),
  562. /// if not specified it is automatically created from backend.
  563. void Finish();
  564. // FIXME: This does not belong here.
  565. bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
  566. void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
  567. bool getRelaxAll() const { return RelaxAll; }
  568. void setRelaxAll(bool Value) { RelaxAll = Value; }
  569. bool isBundlingEnabled() const { return BundleAlignSize != 0; }
  570. unsigned getBundleAlignSize() const { return BundleAlignSize; }
  571. void setBundleAlignSize(unsigned Size) {
  572. assert((Size == 0 || !(Size & (Size - 1))) &&
  573. "Expect a power-of-two bundle align size");
  574. BundleAlignSize = Size;
  575. }
  576. /// \name Section List Access
  577. /// @{
  578. iterator begin() { return Sections.begin(); }
  579. const_iterator begin() const { return Sections.begin(); }
  580. iterator end() { return Sections.end(); }
  581. const_iterator end() const { return Sections.end(); }
  582. size_t size() const { return Sections.size(); }
  583. /// @}
  584. /// \name Symbol List Access
  585. /// @{
  586. symbol_iterator symbol_begin() { return Symbols.begin(); }
  587. const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
  588. symbol_iterator symbol_end() { return Symbols.end(); }
  589. const_symbol_iterator symbol_end() const { return Symbols.end(); }
  590. symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
  591. const_symbol_range symbols() const {
  592. return make_range(symbol_begin(), symbol_end());
  593. }
  594. size_t symbol_size() const { return Symbols.size(); }
  595. /// @}
  596. /// \name Indirect Symbol List Access
  597. /// @{
  598. // FIXME: This is a total hack, this should not be here. Once things are
  599. // factored so that the streamer has direct access to the .o writer, it can
  600. // disappear.
  601. std::vector<IndirectSymbolData> &getIndirectSymbols() {
  602. return IndirectSymbols;
  603. }
  604. indirect_symbol_iterator indirect_symbol_begin() {
  605. return IndirectSymbols.begin();
  606. }
  607. const_indirect_symbol_iterator indirect_symbol_begin() const {
  608. return IndirectSymbols.begin();
  609. }
  610. indirect_symbol_iterator indirect_symbol_end() {
  611. return IndirectSymbols.end();
  612. }
  613. const_indirect_symbol_iterator indirect_symbol_end() const {
  614. return IndirectSymbols.end();
  615. }
  616. size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
  617. /// @}
  618. /// \name Linker Option List Access
  619. /// @{
  620. std::vector<std::vector<std::string>> &getLinkerOptions() {
  621. return LinkerOptions;
  622. }
  623. /// @}
  624. /// \name Data Region List Access
  625. /// @{
  626. // FIXME: This is a total hack, this should not be here. Once things are
  627. // factored so that the streamer has direct access to the .o writer, it can
  628. // disappear.
  629. std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
  630. data_region_iterator data_region_begin() { return DataRegions.begin(); }
  631. const_data_region_iterator data_region_begin() const {
  632. return DataRegions.begin();
  633. }
  634. data_region_iterator data_region_end() { return DataRegions.end(); }
  635. const_data_region_iterator data_region_end() const {
  636. return DataRegions.end();
  637. }
  638. size_t data_region_size() const { return DataRegions.size(); }
  639. /// @}
  640. /// \name Data Region List Access
  641. /// @{
  642. // FIXME: This is a total hack, this should not be here. Once things are
  643. // factored so that the streamer has direct access to the .o writer, it can
  644. // disappear.
  645. MCLOHContainer &getLOHContainer() { return LOHContainer; }
  646. const MCLOHContainer &getLOHContainer() const {
  647. return const_cast<MCAssembler *>(this)->getLOHContainer();
  648. }
  649. /// @}
  650. /// \name Backend Data Access
  651. /// @{
  652. bool registerSection(MCSection &Section) {
  653. if (Section.isRegistered())
  654. return false;
  655. Sections.push_back(&Section);
  656. Section.setIsRegistered(true);
  657. return true;
  658. }
  659. void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
  660. ArrayRef<std::string> getFileNames() { return FileNames; }
  661. void addFileName(StringRef FileName) {
  662. if (std::find(FileNames.begin(), FileNames.end(), FileName) ==
  663. FileNames.end())
  664. FileNames.push_back(FileName);
  665. }
  666. /// \brief Write the necessary bundle padding to the given object writer.
  667. /// Expects a fragment \p F containing instructions and its size \p FSize.
  668. void writeFragmentPadding(const MCFragment &F, uint64_t FSize,
  669. MCObjectWriter *OW) const;
  670. /// @}
  671. void dump();
  672. };
  673. /// \brief Compute the amount of padding required before the fragment \p F to
  674. /// obey bundling restrictions, where \p FOffset is the fragment's offset in
  675. /// its section and \p FSize is the fragment's size.
  676. uint64_t computeBundlePadding(const MCAssembler &Assembler, const MCFragment *F,
  677. uint64_t FOffset, uint64_t FSize);
  678. } // end namespace llvm
  679. #endif