MCAssembler.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCAssembler.h - Object File Generation -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCASSEMBLER_H
  14. #define LLVM_MC_MCASSEMBLER_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/SmallPtrSet.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/iterator.h"
  19. #include "llvm/ADT/iterator_range.h"
  20. #include "llvm/BinaryFormat/MachO.h"
  21. #include "llvm/MC/MCDirectives.h"
  22. #include "llvm/MC/MCDwarf.h"
  23. #include "llvm/MC/MCLinkerOptimizationHint.h"
  24. #include "llvm/MC/MCSymbol.h"
  25. #include "llvm/Support/SMLoc.h"
  26. #include "llvm/Support/VersionTuple.h"
  27. #include <algorithm>
  28. #include <cassert>
  29. #include <cstddef>
  30. #include <cstdint>
  31. #include <memory>
  32. #include <string>
  33. #include <tuple>
  34. #include <utility>
  35. #include <vector>
  36. namespace llvm {
  37. class MCBoundaryAlignFragment;
  38. class MCCVDefRangeFragment;
  39. class MCCVInlineLineTableFragment;
  40. class MCDwarfCallFrameFragment;
  41. class MCDwarfLineAddrFragment;
  42. class MCEncodedFragment;
  43. class MCFixup;
  44. class MCLEBFragment;
  45. class MCPseudoProbeAddrFragment;
  46. class MCRelaxableFragment;
  47. class MCSymbolRefExpr;
  48. class raw_ostream;
  49. class MCAsmBackend;
  50. class MCAsmLayout;
  51. class MCContext;
  52. class MCCodeEmitter;
  53. class MCFragment;
  54. class MCObjectWriter;
  55. class MCSection;
  56. class MCValue;
  57. // FIXME: This really doesn't belong here. See comments below.
  58. struct IndirectSymbolData {
  59. MCSymbol *Symbol;
  60. MCSection *Section;
  61. };
  62. // FIXME: Ditto this. Purely so the Streamer and the ObjectWriter can talk
  63. // to one another.
  64. struct DataRegionData {
  65. // This enum should be kept in sync w/ the mach-o definition in
  66. // llvm/Object/MachOFormat.h.
  67. enum KindTy { Data = 1, JumpTable8, JumpTable16, JumpTable32 } Kind;
  68. MCSymbol *Start;
  69. MCSymbol *End;
  70. };
  71. class MCAssembler {
  72. friend class MCAsmLayout;
  73. public:
  74. using SectionListType = std::vector<MCSection *>;
  75. using SymbolDataListType = std::vector<const MCSymbol *>;
  76. using const_iterator = pointee_iterator<SectionListType::const_iterator>;
  77. using iterator = pointee_iterator<SectionListType::iterator>;
  78. using const_symbol_iterator =
  79. pointee_iterator<SymbolDataListType::const_iterator>;
  80. using symbol_iterator = pointee_iterator<SymbolDataListType::iterator>;
  81. using symbol_range = iterator_range<symbol_iterator>;
  82. using const_symbol_range = iterator_range<const_symbol_iterator>;
  83. using const_indirect_symbol_iterator =
  84. std::vector<IndirectSymbolData>::const_iterator;
  85. using indirect_symbol_iterator = std::vector<IndirectSymbolData>::iterator;
  86. using const_data_region_iterator =
  87. std::vector<DataRegionData>::const_iterator;
  88. using data_region_iterator = std::vector<DataRegionData>::iterator;
  89. /// MachO specific deployment target version info.
  90. // A Major version of 0 indicates that no version information was supplied
  91. // and so the corresponding load command should not be emitted.
  92. using VersionInfoType = struct {
  93. bool EmitBuildVersion;
  94. union {
  95. MCVersionMinType Type; ///< Used when EmitBuildVersion==false.
  96. MachO::PlatformType Platform; ///< Used when EmitBuildVersion==true.
  97. } TypeOrPlatform;
  98. unsigned Major;
  99. unsigned Minor;
  100. unsigned Update;
  101. /// An optional version of the SDK that was used to build the source.
  102. VersionTuple SDKVersion;
  103. };
  104. private:
  105. MCContext &Context;
  106. std::unique_ptr<MCAsmBackend> Backend;
  107. std::unique_ptr<MCCodeEmitter> Emitter;
  108. std::unique_ptr<MCObjectWriter> Writer;
  109. SectionListType Sections;
  110. SymbolDataListType Symbols;
  111. std::vector<IndirectSymbolData> IndirectSymbols;
  112. std::vector<DataRegionData> DataRegions;
  113. /// The list of linker options to propagate into the object file.
  114. std::vector<std::vector<std::string>> LinkerOptions;
  115. /// List of declared file names
  116. std::vector<std::pair<std::string, size_t>> FileNames;
  117. MCDwarfLineTableParams LTParams;
  118. /// The set of function symbols for which a .thumb_func directive has
  119. /// been seen.
  120. //
  121. // FIXME: We really would like this in target specific code rather than
  122. // here. Maybe when the relocation stuff moves to target specific,
  123. // this can go with it? The streamer would need some target specific
  124. // refactoring too.
  125. mutable SmallPtrSet<const MCSymbol *, 32> ThumbFuncs;
  126. /// The bundle alignment size currently set in the assembler.
  127. ///
  128. /// By default it's 0, which means bundling is disabled.
  129. unsigned BundleAlignSize;
  130. bool RelaxAll : 1;
  131. bool SubsectionsViaSymbols : 1;
  132. bool IncrementalLinkerCompatible : 1;
  133. /// ELF specific e_header flags
  134. // It would be good if there were an MCELFAssembler class to hold this.
  135. // ELF header flags are used both by the integrated and standalone assemblers.
  136. // Access to the flags is necessary in cases where assembler directives affect
  137. // which flags to be set.
  138. unsigned ELFHeaderEFlags;
  139. /// Used to communicate Linker Optimization Hint information between
  140. /// the Streamer and the .o writer
  141. MCLOHContainer LOHContainer;
  142. VersionInfoType VersionInfo;
  143. VersionInfoType DarwinTargetVariantVersionInfo;
  144. /// Evaluate a fixup to a relocatable expression and the value which should be
  145. /// placed into the fixup.
  146. ///
  147. /// \param Layout The layout to use for evaluation.
  148. /// \param Fixup The fixup to evaluate.
  149. /// \param DF The fragment the fixup is inside.
  150. /// \param Target [out] On return, the relocatable expression the fixup
  151. /// evaluates to.
  152. /// \param Value [out] On return, the value of the fixup as currently laid
  153. /// out.
  154. /// \param WasForced [out] On return, the value in the fixup is set to the
  155. /// correct value if WasForced is true, even if evaluateFixup returns false.
  156. /// \return Whether the fixup value was fully resolved. This is true if the
  157. /// \p Value result is fixed, otherwise the value may change due to
  158. /// relocation.
  159. bool evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
  160. const MCFragment *DF, MCValue &Target,
  161. uint64_t &Value, bool &WasForced) const;
  162. /// Check whether a fixup can be satisfied, or whether it needs to be relaxed
  163. /// (increased in size, in order to hold its value correctly).
  164. bool fixupNeedsRelaxation(const MCFixup &Fixup, const MCRelaxableFragment *DF,
  165. const MCAsmLayout &Layout) const;
  166. /// Check whether the given fragment needs relaxation.
  167. bool fragmentNeedsRelaxation(const MCRelaxableFragment *IF,
  168. const MCAsmLayout &Layout) const;
  169. /// Perform one layout iteration and return true if any offsets
  170. /// were adjusted.
  171. bool layoutOnce(MCAsmLayout &Layout);
  172. /// Perform one layout iteration of the given section and return true
  173. /// if any offsets were adjusted.
  174. bool layoutSectionOnce(MCAsmLayout &Layout, MCSection &Sec);
  175. /// Perform relaxation on a single fragment - returns true if the fragment
  176. /// changes as a result of relaxation.
  177. bool relaxFragment(MCAsmLayout &Layout, MCFragment &F);
  178. bool relaxInstruction(MCAsmLayout &Layout, MCRelaxableFragment &IF);
  179. bool relaxLEB(MCAsmLayout &Layout, MCLEBFragment &IF);
  180. bool relaxBoundaryAlign(MCAsmLayout &Layout, MCBoundaryAlignFragment &BF);
  181. bool relaxDwarfLineAddr(MCAsmLayout &Layout, MCDwarfLineAddrFragment &DF);
  182. bool relaxDwarfCallFrameFragment(MCAsmLayout &Layout,
  183. MCDwarfCallFrameFragment &DF);
  184. bool relaxCVInlineLineTable(MCAsmLayout &Layout,
  185. MCCVInlineLineTableFragment &DF);
  186. bool relaxCVDefRange(MCAsmLayout &Layout, MCCVDefRangeFragment &DF);
  187. bool relaxPseudoProbeAddr(MCAsmLayout &Layout, MCPseudoProbeAddrFragment &DF);
  188. /// finishLayout - Finalize a layout, including fragment lowering.
  189. void finishLayout(MCAsmLayout &Layout);
  190. std::tuple<MCValue, uint64_t, bool>
  191. handleFixup(const MCAsmLayout &Layout, MCFragment &F, const MCFixup &Fixup);
  192. public:
  193. struct Symver {
  194. SMLoc Loc;
  195. const MCSymbol *Sym;
  196. StringRef Name;
  197. // True if .symver *, *@@@* or .symver *, *, remove.
  198. bool KeepOriginalSym;
  199. };
  200. std::vector<Symver> Symvers;
  201. /// Construct a new assembler instance.
  202. //
  203. // FIXME: How are we going to parameterize this? Two obvious options are stay
  204. // concrete and require clients to pass in a target like object. The other
  205. // option is to make this abstract, and have targets provide concrete
  206. // implementations as we do with AsmParser.
  207. MCAssembler(MCContext &Context, std::unique_ptr<MCAsmBackend> Backend,
  208. std::unique_ptr<MCCodeEmitter> Emitter,
  209. std::unique_ptr<MCObjectWriter> Writer);
  210. MCAssembler(const MCAssembler &) = delete;
  211. MCAssembler &operator=(const MCAssembler &) = delete;
  212. ~MCAssembler();
  213. /// Compute the effective fragment size assuming it is laid out at the given
  214. /// \p SectionAddress and \p FragmentOffset.
  215. uint64_t computeFragmentSize(const MCAsmLayout &Layout,
  216. const MCFragment &F) const;
  217. /// Find the symbol which defines the atom containing the given symbol, or
  218. /// null if there is no such symbol.
  219. const MCSymbol *getAtom(const MCSymbol &S) const;
  220. /// Check whether a particular symbol is visible to the linker and is required
  221. /// in the symbol table, or whether it can be discarded by the assembler. This
  222. /// also effects whether the assembler treats the label as potentially
  223. /// defining a separate atom.
  224. bool isSymbolLinkerVisible(const MCSymbol &SD) const;
  225. /// Emit the section contents to \p OS.
  226. void writeSectionData(raw_ostream &OS, const MCSection *Section,
  227. const MCAsmLayout &Layout) const;
  228. /// Check whether a given symbol has been flagged with .thumb_func.
  229. bool isThumbFunc(const MCSymbol *Func) const;
  230. /// Flag a function symbol as the target of a .thumb_func directive.
  231. void setIsThumbFunc(const MCSymbol *Func) { ThumbFuncs.insert(Func); }
  232. /// ELF e_header flags
  233. unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
  234. void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
  235. /// MachO deployment target version information.
  236. const VersionInfoType &getVersionInfo() const { return VersionInfo; }
  237. void setVersionMin(MCVersionMinType Type, unsigned Major, unsigned Minor,
  238. unsigned Update,
  239. VersionTuple SDKVersion = VersionTuple()) {
  240. VersionInfo.EmitBuildVersion = false;
  241. VersionInfo.TypeOrPlatform.Type = Type;
  242. VersionInfo.Major = Major;
  243. VersionInfo.Minor = Minor;
  244. VersionInfo.Update = Update;
  245. VersionInfo.SDKVersion = SDKVersion;
  246. }
  247. void setBuildVersion(MachO::PlatformType Platform, unsigned Major,
  248. unsigned Minor, unsigned Update,
  249. VersionTuple SDKVersion = VersionTuple()) {
  250. VersionInfo.EmitBuildVersion = true;
  251. VersionInfo.TypeOrPlatform.Platform = Platform;
  252. VersionInfo.Major = Major;
  253. VersionInfo.Minor = Minor;
  254. VersionInfo.Update = Update;
  255. VersionInfo.SDKVersion = SDKVersion;
  256. }
  257. const VersionInfoType &getDarwinTargetVariantVersionInfo() const {
  258. return DarwinTargetVariantVersionInfo;
  259. }
  260. void setDarwinTargetVariantBuildVersion(MachO::PlatformType Platform,
  261. unsigned Major, unsigned Minor,
  262. unsigned Update,
  263. VersionTuple SDKVersion) {
  264. DarwinTargetVariantVersionInfo.EmitBuildVersion = true;
  265. DarwinTargetVariantVersionInfo.TypeOrPlatform.Platform = Platform;
  266. DarwinTargetVariantVersionInfo.Major = Major;
  267. DarwinTargetVariantVersionInfo.Minor = Minor;
  268. DarwinTargetVariantVersionInfo.Update = Update;
  269. DarwinTargetVariantVersionInfo.SDKVersion = SDKVersion;
  270. }
  271. /// Reuse an assembler instance
  272. ///
  273. void reset();
  274. MCContext &getContext() const { return Context; }
  275. MCAsmBackend *getBackendPtr() const { return Backend.get(); }
  276. MCCodeEmitter *getEmitterPtr() const { return Emitter.get(); }
  277. MCObjectWriter *getWriterPtr() const { return Writer.get(); }
  278. MCAsmBackend &getBackend() const { return *Backend; }
  279. MCCodeEmitter &getEmitter() const { return *Emitter; }
  280. MCObjectWriter &getWriter() const { return *Writer; }
  281. MCDwarfLineTableParams getDWARFLinetableParams() const { return LTParams; }
  282. void setDWARFLinetableParams(MCDwarfLineTableParams P) { LTParams = P; }
  283. /// Finish - Do final processing and write the object to the output stream.
  284. /// \p Writer is used for custom object writer (as the MCJIT does),
  285. /// if not specified it is automatically created from backend.
  286. void Finish();
  287. // Layout all section and prepare them for emission.
  288. void layout(MCAsmLayout &Layout);
  289. // FIXME: This does not belong here.
  290. bool getSubsectionsViaSymbols() const { return SubsectionsViaSymbols; }
  291. void setSubsectionsViaSymbols(bool Value) { SubsectionsViaSymbols = Value; }
  292. bool isIncrementalLinkerCompatible() const {
  293. return IncrementalLinkerCompatible;
  294. }
  295. void setIncrementalLinkerCompatible(bool Value) {
  296. IncrementalLinkerCompatible = Value;
  297. }
  298. bool getRelaxAll() const { return RelaxAll; }
  299. void setRelaxAll(bool Value) { RelaxAll = Value; }
  300. bool isBundlingEnabled() const { return BundleAlignSize != 0; }
  301. unsigned getBundleAlignSize() const { return BundleAlignSize; }
  302. void setBundleAlignSize(unsigned Size) {
  303. assert((Size == 0 || !(Size & (Size - 1))) &&
  304. "Expect a power-of-two bundle align size");
  305. BundleAlignSize = Size;
  306. }
  307. /// \name Section List Access
  308. /// @{
  309. iterator begin() { return Sections.begin(); }
  310. const_iterator begin() const { return Sections.begin(); }
  311. iterator end() { return Sections.end(); }
  312. const_iterator end() const { return Sections.end(); }
  313. size_t size() const { return Sections.size(); }
  314. /// @}
  315. /// \name Symbol List Access
  316. /// @{
  317. symbol_iterator symbol_begin() { return Symbols.begin(); }
  318. const_symbol_iterator symbol_begin() const { return Symbols.begin(); }
  319. symbol_iterator symbol_end() { return Symbols.end(); }
  320. const_symbol_iterator symbol_end() const { return Symbols.end(); }
  321. symbol_range symbols() { return make_range(symbol_begin(), symbol_end()); }
  322. const_symbol_range symbols() const {
  323. return make_range(symbol_begin(), symbol_end());
  324. }
  325. size_t symbol_size() const { return Symbols.size(); }
  326. /// @}
  327. /// \name Indirect Symbol List Access
  328. /// @{
  329. // FIXME: This is a total hack, this should not be here. Once things are
  330. // factored so that the streamer has direct access to the .o writer, it can
  331. // disappear.
  332. std::vector<IndirectSymbolData> &getIndirectSymbols() {
  333. return IndirectSymbols;
  334. }
  335. indirect_symbol_iterator indirect_symbol_begin() {
  336. return IndirectSymbols.begin();
  337. }
  338. const_indirect_symbol_iterator indirect_symbol_begin() const {
  339. return IndirectSymbols.begin();
  340. }
  341. indirect_symbol_iterator indirect_symbol_end() {
  342. return IndirectSymbols.end();
  343. }
  344. const_indirect_symbol_iterator indirect_symbol_end() const {
  345. return IndirectSymbols.end();
  346. }
  347. size_t indirect_symbol_size() const { return IndirectSymbols.size(); }
  348. /// @}
  349. /// \name Linker Option List Access
  350. /// @{
  351. std::vector<std::vector<std::string>> &getLinkerOptions() {
  352. return LinkerOptions;
  353. }
  354. /// @}
  355. /// \name Data Region List Access
  356. /// @{
  357. // FIXME: This is a total hack, this should not be here. Once things are
  358. // factored so that the streamer has direct access to the .o writer, it can
  359. // disappear.
  360. std::vector<DataRegionData> &getDataRegions() { return DataRegions; }
  361. data_region_iterator data_region_begin() { return DataRegions.begin(); }
  362. const_data_region_iterator data_region_begin() const {
  363. return DataRegions.begin();
  364. }
  365. data_region_iterator data_region_end() { return DataRegions.end(); }
  366. const_data_region_iterator data_region_end() const {
  367. return DataRegions.end();
  368. }
  369. size_t data_region_size() const { return DataRegions.size(); }
  370. /// @}
  371. /// \name Data Region List Access
  372. /// @{
  373. // FIXME: This is a total hack, this should not be here. Once things are
  374. // factored so that the streamer has direct access to the .o writer, it can
  375. // disappear.
  376. MCLOHContainer &getLOHContainer() { return LOHContainer; }
  377. const MCLOHContainer &getLOHContainer() const {
  378. return const_cast<MCAssembler *>(this)->getLOHContainer();
  379. }
  380. struct CGProfileEntry {
  381. const MCSymbolRefExpr *From;
  382. const MCSymbolRefExpr *To;
  383. uint64_t Count;
  384. };
  385. std::vector<CGProfileEntry> CGProfile;
  386. /// @}
  387. /// \name Backend Data Access
  388. /// @{
  389. bool registerSection(MCSection &Section);
  390. void registerSymbol(const MCSymbol &Symbol, bool *Created = nullptr);
  391. MutableArrayRef<std::pair<std::string, size_t>> getFileNames() {
  392. return FileNames;
  393. }
  394. void addFileName(StringRef FileName) {
  395. FileNames.emplace_back(std::string(FileName), Symbols.size());
  396. }
  397. /// Write the necessary bundle padding to \p OS.
  398. /// Expects a fragment \p F containing instructions and its size \p FSize.
  399. void writeFragmentPadding(raw_ostream &OS, const MCEncodedFragment &F,
  400. uint64_t FSize) const;
  401. /// @}
  402. void dump() const;
  403. };
  404. /// Compute the amount of padding required before the fragment \p F to
  405. /// obey bundling restrictions, where \p FOffset is the fragment's offset in
  406. /// its section and \p FSize is the fragment's size.
  407. uint64_t computeBundlePadding(const MCAssembler &Assembler,
  408. const MCEncodedFragment *F, uint64_t FOffset,
  409. uint64_t FSize);
  410. } // end namespace llvm
  411. #endif // LLVM_MC_MCASSEMBLER_H
  412. #ifdef __GNUC__
  413. #pragma GCC diagnostic pop
  414. #endif