IRSymtab.h 11 KB

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