IRSymtab.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- IRSymtab.h - data definitions for IR symbol tables -------*- 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. //
  14. // This file contains data definitions and a reader and builder for a symbol
  15. // table for LLVM IR. Its purpose is to allow linkers and other consumers of
  16. // bitcode files to efficiently read the symbol table for symbol resolution
  17. // purposes without needing to construct a module in memory.
  18. //
  19. // As with most object files the symbol table has two parts: the symbol table
  20. // itself and a string table which is referenced by the symbol table.
  21. //
  22. // A symbol table corresponds to a single bitcode file, which may consist of
  23. // multiple modules, so symbol tables may likewise contain symbols for multiple
  24. // modules.
  25. //
  26. //===----------------------------------------------------------------------===//
  27. #ifndef LLVM_OBJECT_IRSYMTAB_H
  28. #define LLVM_OBJECT_IRSYMTAB_H
  29. #include "llvm/ADT/ArrayRef.h"
  30. #include "llvm/ADT/StringRef.h"
  31. #include "llvm/ADT/iterator_range.h"
  32. #include "llvm/IR/Comdat.h"
  33. #include "llvm/IR/GlobalValue.h"
  34. #include "llvm/Object/SymbolicFile.h"
  35. #include "llvm/Support/Allocator.h"
  36. #include "llvm/Support/Endian.h"
  37. #include "llvm/Support/Error.h"
  38. #include <cassert>
  39. #include <cstdint>
  40. #include <vector>
  41. namespace llvm {
  42. struct BitcodeFileContents;
  43. class StringTableBuilder;
  44. namespace irsymtab {
  45. namespace storage {
  46. // The data structures in this namespace define the low-level serialization
  47. // format. Clients that just want to read a symbol table should use the
  48. // irsymtab::Reader class.
  49. using Word = support::ulittle32_t;
  50. /// A reference to a string in the string table.
  51. struct Str {
  52. Word Offset, Size;
  53. StringRef get(StringRef Strtab) const {
  54. return {Strtab.data() + Offset, Size};
  55. }
  56. };
  57. /// A reference to a range of objects in the symbol table.
  58. template <typename T> struct Range {
  59. Word Offset, Size;
  60. ArrayRef<T> get(StringRef Symtab) const {
  61. return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
  62. }
  63. };
  64. /// Describes the range of a particular module's symbols within the symbol
  65. /// table.
  66. struct Module {
  67. Word Begin, End;
  68. /// The index of the first Uncommon for this Module.
  69. Word UncBegin;
  70. };
  71. /// This is equivalent to an IR comdat.
  72. struct Comdat {
  73. Str Name;
  74. // llvm::Comdat::SelectionKind
  75. Word SelectionKind;
  76. };
  77. /// Contains the information needed by linkers for symbol resolution, as well as
  78. /// by the LTO implementation itself.
  79. struct Symbol {
  80. /// The mangled symbol name.
  81. Str Name;
  82. /// The unmangled symbol name, or the empty string if this is not an IR
  83. /// symbol.
  84. Str IRName;
  85. /// The index into Header::Comdats, or -1 if not a comdat member.
  86. Word ComdatIndex;
  87. Word Flags;
  88. enum FlagBits {
  89. FB_visibility, // 2 bits
  90. FB_has_uncommon = FB_visibility + 2,
  91. FB_undefined,
  92. FB_weak,
  93. FB_common,
  94. FB_indirect,
  95. FB_used,
  96. FB_tls,
  97. FB_may_omit,
  98. FB_global,
  99. FB_format_specific,
  100. FB_unnamed_addr,
  101. FB_executable,
  102. };
  103. };
  104. /// This data structure contains rarely used symbol fields and is optionally
  105. /// referenced by a Symbol.
  106. struct Uncommon {
  107. Word CommonSize, CommonAlign;
  108. /// COFF-specific: the name of the symbol that a weak external resolves to
  109. /// if not defined.
  110. Str COFFWeakExternFallbackName;
  111. /// Specified section name, if any.
  112. Str SectionName;
  113. };
  114. struct Header {
  115. /// Version number of the symtab format. This number should be incremented
  116. /// when the format changes, but it does not need to be incremented if a
  117. /// change to LLVM would cause it to create a different symbol table.
  118. Word Version;
  119. enum { kCurrentVersion = 3 };
  120. /// The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
  121. /// Consumers should rebuild the symbol table from IR if the producer's
  122. /// version does not match the consumer's version due to potential differences
  123. /// in symbol table format, symbol enumeration order and so on.
  124. Str Producer;
  125. Range<Module> Modules;
  126. Range<Comdat> Comdats;
  127. Range<Symbol> Symbols;
  128. Range<Uncommon> Uncommons;
  129. Str TargetTriple, SourceFileName;
  130. /// COFF-specific: linker directives.
  131. Str COFFLinkerOpts;
  132. /// Dependent Library Specifiers
  133. Range<Str> DependentLibraries;
  134. };
  135. } // end namespace storage
  136. /// Fills in Symtab and StrtabBuilder with a valid symbol and string table for
  137. /// Mods.
  138. Error build(ArrayRef<Module *> Mods, SmallVector<char, 0> &Symtab,
  139. StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc);
  140. /// This represents a symbol that has been read from a storage::Symbol and
  141. /// possibly a storage::Uncommon.
  142. struct Symbol {
  143. // Copied from storage::Symbol.
  144. StringRef Name, IRName;
  145. int ComdatIndex;
  146. uint32_t Flags;
  147. // Copied from storage::Uncommon.
  148. uint32_t CommonSize, CommonAlign;
  149. StringRef COFFWeakExternFallbackName;
  150. StringRef SectionName;
  151. /// Returns the mangled symbol name.
  152. StringRef getName() const { return Name; }
  153. /// Returns the unmangled symbol name, or the empty string if this is not an
  154. /// IR symbol.
  155. StringRef getIRName() const { return IRName; }
  156. /// Returns the index into the comdat table (see Reader::getComdatTable()), or
  157. /// -1 if not a comdat member.
  158. int getComdatIndex() const { return ComdatIndex; }
  159. using S = storage::Symbol;
  160. GlobalValue::VisibilityTypes getVisibility() const {
  161. return GlobalValue::VisibilityTypes((Flags >> S::FB_visibility) & 3);
  162. }
  163. bool isUndefined() const { return (Flags >> S::FB_undefined) & 1; }
  164. bool isWeak() const { return (Flags >> S::FB_weak) & 1; }
  165. bool isCommon() const { return (Flags >> S::FB_common) & 1; }
  166. bool isIndirect() const { return (Flags >> S::FB_indirect) & 1; }
  167. bool isUsed() const { return (Flags >> S::FB_used) & 1; }
  168. bool isTLS() const { return (Flags >> S::FB_tls) & 1; }
  169. bool canBeOmittedFromSymbolTable() const {
  170. return (Flags >> S::FB_may_omit) & 1;
  171. }
  172. bool isGlobal() const { return (Flags >> S::FB_global) & 1; }
  173. bool isFormatSpecific() const { return (Flags >> S::FB_format_specific) & 1; }
  174. bool isUnnamedAddr() const { return (Flags >> S::FB_unnamed_addr) & 1; }
  175. bool isExecutable() const { return (Flags >> S::FB_executable) & 1; }
  176. uint64_t getCommonSize() const {
  177. assert(isCommon());
  178. return CommonSize;
  179. }
  180. uint32_t getCommonAlignment() const {
  181. assert(isCommon());
  182. return CommonAlign;
  183. }
  184. /// COFF-specific: for weak externals, returns the name of the symbol that is
  185. /// used as a fallback if the weak external remains undefined.
  186. StringRef getCOFFWeakExternalFallback() const {
  187. assert(isWeak() && isIndirect());
  188. return COFFWeakExternFallbackName;
  189. }
  190. StringRef getSectionName() const { return SectionName; }
  191. };
  192. /// This class can be used to read a Symtab and Strtab produced by
  193. /// irsymtab::build.
  194. class Reader {
  195. StringRef Symtab, Strtab;
  196. ArrayRef<storage::Module> Modules;
  197. ArrayRef<storage::Comdat> Comdats;
  198. ArrayRef<storage::Symbol> Symbols;
  199. ArrayRef<storage::Uncommon> Uncommons;
  200. ArrayRef<storage::Str> DependentLibraries;
  201. StringRef str(storage::Str S) const { return S.get(Strtab); }
  202. template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
  203. return R.get(Symtab);
  204. }
  205. const storage::Header &header() const {
  206. return *reinterpret_cast<const storage::Header *>(Symtab.data());
  207. }
  208. public:
  209. class SymbolRef;
  210. Reader() = default;
  211. Reader(StringRef Symtab, StringRef Strtab) : Symtab(Symtab), Strtab(Strtab) {
  212. Modules = range(header().Modules);
  213. Comdats = range(header().Comdats);
  214. Symbols = range(header().Symbols);
  215. Uncommons = range(header().Uncommons);
  216. DependentLibraries = range(header().DependentLibraries);
  217. }
  218. using symbol_range = iterator_range<object::content_iterator<SymbolRef>>;
  219. /// Returns the symbol table for the entire bitcode file.
  220. /// The symbols enumerated by this method are ephemeral, but they can be
  221. /// copied into an irsymtab::Symbol object.
  222. symbol_range symbols() const;
  223. size_t getNumModules() const { return Modules.size(); }
  224. /// Returns a slice of the symbol table for the I'th module in the file.
  225. /// The symbols enumerated by this method are ephemeral, but they can be
  226. /// copied into an irsymtab::Symbol object.
  227. symbol_range module_symbols(unsigned I) const;
  228. StringRef getTargetTriple() const { return str(header().TargetTriple); }
  229. /// Returns the source file path specified at compile time.
  230. StringRef getSourceFileName() const { return str(header().SourceFileName); }
  231. /// Returns a table with all the comdats used by this file.
  232. std::vector<std::pair<StringRef, llvm::Comdat::SelectionKind>>
  233. getComdatTable() const {
  234. std::vector<std::pair<StringRef, llvm::Comdat::SelectionKind>> ComdatTable;
  235. ComdatTable.reserve(Comdats.size());
  236. for (auto C : Comdats)
  237. ComdatTable.push_back({str(C.Name), llvm::Comdat::SelectionKind(
  238. uint32_t(C.SelectionKind))});
  239. return ComdatTable;
  240. }
  241. /// COFF-specific: returns linker options specified in the input file.
  242. StringRef getCOFFLinkerOpts() const { return str(header().COFFLinkerOpts); }
  243. /// Returns dependent library specifiers
  244. std::vector<StringRef> getDependentLibraries() const {
  245. std::vector<StringRef> Specifiers;
  246. Specifiers.reserve(DependentLibraries.size());
  247. for (auto S : DependentLibraries) {
  248. Specifiers.push_back(str(S));
  249. }
  250. return Specifiers;
  251. }
  252. };
  253. /// Ephemeral symbols produced by Reader::symbols() and
  254. /// Reader::module_symbols().
  255. class Reader::SymbolRef : public Symbol {
  256. const storage::Symbol *SymI, *SymE;
  257. const storage::Uncommon *UncI;
  258. const Reader *R;
  259. void read() {
  260. if (SymI == SymE)
  261. return;
  262. Name = R->str(SymI->Name);
  263. IRName = R->str(SymI->IRName);
  264. ComdatIndex = SymI->ComdatIndex;
  265. Flags = SymI->Flags;
  266. if (Flags & (1 << storage::Symbol::FB_has_uncommon)) {
  267. CommonSize = UncI->CommonSize;
  268. CommonAlign = UncI->CommonAlign;
  269. COFFWeakExternFallbackName = R->str(UncI->COFFWeakExternFallbackName);
  270. SectionName = R->str(UncI->SectionName);
  271. } else
  272. // Reset this field so it can be queried unconditionally for all symbols.
  273. SectionName = "";
  274. }
  275. public:
  276. SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE,
  277. const storage::Uncommon *UncI, const Reader *R)
  278. : SymI(SymI), SymE(SymE), UncI(UncI), R(R) {
  279. read();
  280. }
  281. void moveNext() {
  282. ++SymI;
  283. if (Flags & (1 << storage::Symbol::FB_has_uncommon))
  284. ++UncI;
  285. read();
  286. }
  287. bool operator==(const SymbolRef &Other) const { return SymI == Other.SymI; }
  288. };
  289. inline Reader::symbol_range Reader::symbols() const {
  290. return {SymbolRef(Symbols.begin(), Symbols.end(), Uncommons.begin(), this),
  291. SymbolRef(Symbols.end(), Symbols.end(), nullptr, this)};
  292. }
  293. inline Reader::symbol_range Reader::module_symbols(unsigned I) const {
  294. const storage::Module &M = Modules[I];
  295. const storage::Symbol *MBegin = Symbols.begin() + M.Begin,
  296. *MEnd = Symbols.begin() + M.End;
  297. return {SymbolRef(MBegin, MEnd, Uncommons.begin() + M.UncBegin, this),
  298. SymbolRef(MEnd, MEnd, nullptr, this)};
  299. }
  300. /// The contents of the irsymtab in a bitcode file. Any underlying data for the
  301. /// irsymtab are owned by Symtab and Strtab.
  302. struct FileContents {
  303. SmallVector<char, 0> Symtab, Strtab;
  304. Reader TheReader;
  305. };
  306. /// Reads the contents of a bitcode file, creating its irsymtab if necessary.
  307. Expected<FileContents> readBitcode(const BitcodeFileContents &BFC);
  308. } // end namespace irsymtab
  309. } // end namespace llvm
  310. #endif // LLVM_OBJECT_IRSYMTAB_H
  311. #ifdef __GNUC__
  312. #pragma GCC diagnostic pop
  313. #endif