LLVM  5.0.0
IRSymtab.h
Go to the documentation of this file.
1 //===- IRSymtab.h - data definitions for IR symbol tables -------*- 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 //
10 // This file contains data definitions and a reader and builder for a symbol
11 // table for LLVM IR. Its purpose is to allow linkers and other consumers of
12 // bitcode files to efficiently read the symbol table for symbol resolution
13 // purposes without needing to construct a module in memory.
14 //
15 // As with most object files the symbol table has two parts: the symbol table
16 // itself and a string table which is referenced by the symbol table.
17 //
18 // A symbol table corresponds to a single bitcode file, which may consist of
19 // multiple modules, so symbol tables may likewise contain symbols for multiple
20 // modules.
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #ifndef LLVM_OBJECT_IRSYMTAB_H
25 #define LLVM_OBJECT_IRSYMTAB_H
26 
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/StringRef.h"
30 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/Support/Endian.h"
33 #include "llvm/Support/Error.h"
34 #include <cassert>
35 #include <cstdint>
36 #include <vector>
37 
38 namespace llvm {
39 
40 struct BitcodeFileContents;
41 class StringTableBuilder;
42 
43 namespace irsymtab {
44 
45 namespace storage {
46 
47 // The data structures in this namespace define the low-level serialization
48 // format. Clients that just want to read a symbol table should use the
49 // irsymtab::Reader class.
50 
52 
53 /// A reference to a string in the string table.
54 struct Str {
56 
57  StringRef get(StringRef Strtab) const {
58  return {Strtab.data() + Offset, Size};
59  }
60 };
61 
62 /// A reference to a range of objects in the symbol table.
63 template <typename T> struct Range {
65 
66  ArrayRef<T> get(StringRef Symtab) const {
67  return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
68  }
69 };
70 
71 /// Describes the range of a particular module's symbols within the symbol
72 /// table.
73 struct Module {
74  Word Begin, End;
75 
76  /// The index of the first Uncommon for this Module.
78 };
79 
80 /// This is equivalent to an IR comdat.
81 struct Comdat {
83 };
84 
85 /// Contains the information needed by linkers for symbol resolution, as well as
86 /// by the LTO implementation itself.
87 struct Symbol {
88  /// The mangled symbol name.
90 
91  /// The unmangled symbol name, or the empty string if this is not an IR
92  /// symbol.
94 
95  /// The index into Header::Comdats, or -1 if not a comdat member.
97 
99  enum FlagBits {
100  FB_visibility, // 2 bits
101  FB_has_uncommon = FB_visibility + 2,
113  };
114 };
115 
116 /// This data structure contains rarely used symbol fields and is optionally
117 /// referenced by a Symbol.
118 struct Uncommon {
119  Word CommonSize, CommonAlign;
120 
121  /// COFF-specific: the name of the symbol that a weak external resolves to
122  /// if not defined.
124 };
125 
126 struct Header {
127  /// Version number of the symtab format. This number should be incremented
128  /// when the format changes, but it does not need to be incremented if a
129  /// change to LLVM would cause it to create a different symbol table.
131  enum { kCurrentVersion = 0 };
132 
133  /// The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
134  /// Consumers should rebuild the symbol table from IR if the producer's
135  /// version does not match the consumer's version due to potential differences
136  /// in symbol table format, symbol enumeration order and so on.
138 
143 
144  Str TargetTriple, SourceFileName;
145 
146  /// COFF-specific: linker directives.
148 };
149 
150 } // end namespace storage
151 
152 /// Fills in Symtab and StrtabBuilder with a valid symbol and string table for
153 /// Mods.
155  StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc);
156 
157 /// This represents a symbol that has been read from a storage::Symbol and
158 /// possibly a storage::Uncommon.
159 struct Symbol {
160  // Copied from storage::Symbol.
164 
165  // Copied from storage::Uncommon.
166  uint32_t CommonSize, CommonAlign;
168 
169  /// Returns the mangled symbol name.
170  StringRef getName() const { return Name; }
171 
172  /// Returns the unmangled symbol name, or the empty string if this is not an
173  /// IR symbol.
174  StringRef getIRName() const { return IRName; }
175 
176  /// Returns the index into the comdat table (see Reader::getComdatTable()), or
177  /// -1 if not a comdat member.
178  int getComdatIndex() const { return ComdatIndex; }
179 
181 
183  return GlobalValue::VisibilityTypes((Flags >> S::FB_visibility) & 3);
184  }
185 
186  bool isUndefined() const { return (Flags >> S::FB_undefined) & 1; }
187  bool isWeak() const { return (Flags >> S::FB_weak) & 1; }
188  bool isCommon() const { return (Flags >> S::FB_common) & 1; }
189  bool isIndirect() const { return (Flags >> S::FB_indirect) & 1; }
190  bool isUsed() const { return (Flags >> S::FB_used) & 1; }
191  bool isTLS() const { return (Flags >> S::FB_tls) & 1; }
192 
194  return (Flags >> S::FB_may_omit) & 1;
195  }
196 
197  bool isGlobal() const { return (Flags >> S::FB_global) & 1; }
198  bool isFormatSpecific() const { return (Flags >> S::FB_format_specific) & 1; }
199  bool isUnnamedAddr() const { return (Flags >> S::FB_unnamed_addr) & 1; }
200  bool isExecutable() const { return (Flags >> S::FB_executable) & 1; }
201 
202  uint64_t getCommonSize() const {
203  assert(isCommon());
204  return CommonSize;
205  }
206 
208  assert(isCommon());
209  return CommonAlign;
210  }
211 
212  /// COFF-specific: for weak externals, returns the name of the symbol that is
213  /// used as a fallback if the weak external remains undefined.
215  assert(isWeak() && isIndirect());
216  return COFFWeakExternFallbackName;
217  }
218 };
219 
220 /// This class can be used to read a Symtab and Strtab produced by
221 /// irsymtab::build.
222 class Reader {
223  StringRef Symtab, Strtab;
224 
228  ArrayRef<storage::Uncommon> Uncommons;
229 
230  StringRef str(storage::Str S) const { return S.get(Strtab); }
231 
232  template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
233  return R.get(Symtab);
234  }
235 
236  const storage::Header &header() const {
237  return *reinterpret_cast<const storage::Header *>(Symtab.data());
238  }
239 
240 public:
241  class SymbolRef;
242 
243  Reader() = default;
244  Reader(StringRef Symtab, StringRef Strtab) : Symtab(Symtab), Strtab(Strtab) {
245  Modules = range(header().Modules);
246  Comdats = range(header().Comdats);
247  Symbols = range(header().Symbols);
248  Uncommons = range(header().Uncommons);
249  }
250 
252 
253  /// Returns the symbol table for the entire bitcode file.
254  /// The symbols enumerated by this method are ephemeral, but they can be
255  /// copied into an irsymtab::Symbol object.
256  symbol_range symbols() const;
257 
258  size_t getNumModules() const { return Modules.size(); }
259 
260  /// Returns a slice of the symbol table for the I'th module in the file.
261  /// The symbols enumerated by this method are ephemeral, but they can be
262  /// copied into an irsymtab::Symbol object.
263  symbol_range module_symbols(unsigned I) const;
264 
265  StringRef getTargetTriple() const { return str(header().TargetTriple); }
266 
267  /// Returns the source file path specified at compile time.
268  StringRef getSourceFileName() const { return str(header().SourceFileName); }
269 
270  /// Returns a table with all the comdats used by this file.
271  std::vector<StringRef> getComdatTable() const {
272  std::vector<StringRef> ComdatTable;
273  ComdatTable.reserve(Comdats.size());
274  for (auto C : Comdats)
275  ComdatTable.push_back(str(C.Name));
276  return ComdatTable;
277  }
278 
279  /// COFF-specific: returns linker options specified in the input file.
280  StringRef getCOFFLinkerOpts() const { return str(header().COFFLinkerOpts); }
281 };
282 
283 /// Ephemeral symbols produced by Reader::symbols() and
284 /// Reader::module_symbols().
285 class Reader::SymbolRef : public Symbol {
286  const storage::Symbol *SymI, *SymE;
287  const storage::Uncommon *UncI;
288  const Reader *R;
289 
290  void read() {
291  if (SymI == SymE)
292  return;
293 
294  Name = R->str(SymI->Name);
295  IRName = R->str(SymI->IRName);
296  ComdatIndex = SymI->ComdatIndex;
297  Flags = SymI->Flags;
298 
300  CommonSize = UncI->CommonSize;
301  CommonAlign = UncI->CommonAlign;
302  COFFWeakExternFallbackName = R->str(UncI->COFFWeakExternFallbackName);
303  }
304  }
305 
306 public:
307  SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE,
308  const storage::Uncommon *UncI, const Reader *R)
309  : SymI(SymI), SymE(SymE), UncI(UncI), R(R) {
310  read();
311  }
312 
313  void moveNext() {
314  ++SymI;
316  ++UncI;
317  read();
318  }
319 
320  bool operator==(const SymbolRef &Other) const { return SymI == Other.SymI; }
321 };
322 
324  return {SymbolRef(Symbols.begin(), Symbols.end(), Uncommons.begin(), this),
325  SymbolRef(Symbols.end(), Symbols.end(), nullptr, this)};
326 }
327 
329  const storage::Module &M = Modules[I];
330  const storage::Symbol *MBegin = Symbols.begin() + M.Begin,
331  *MEnd = Symbols.begin() + M.End;
332  return {SymbolRef(MBegin, MEnd, Uncommons.begin() + M.UncBegin, this),
333  SymbolRef(MEnd, MEnd, nullptr, this)};
334 }
335 
336 /// The contents of the irsymtab in a bitcode file. Any underlying data for the
337 /// irsymtab are owned by Symtab and Strtab.
338 struct FileContents {
341 };
342 
343 /// Reads the contents of a bitcode file, creating its irsymtab if necessary.
345 
346 } // end namespace irsymtab
347 } // end namespace llvm
348 
349 #endif // LLVM_OBJECT_IRSYMTAB_H
uint32_t getCommonAlignment() const
Definition: IRSymtab.h:207
StringRef getCOFFLinkerOpts() const
COFF-specific: returns linker options specified in the input file.
Definition: IRSymtab.h:280
bool isUndefined() const
Definition: IRSymtab.h:186
Contains the information needed by linkers for symbol resolution, as well as by the LTO implementatio...
Definition: IRSymtab.h:87
bool operator==(const SymbolRef &Other) const
Definition: IRSymtab.h:320
StringRef getCOFFWeakExternalFallback() const
COFF-specific: for weak externals, returns the name of the symbol that is used as a fallback if the w...
Definition: IRSymtab.h:214
bool isUsed() const
Definition: IRSymtab.h:190
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Reader(StringRef Symtab, StringRef Strtab)
Definition: IRSymtab.h:244
bool isGlobal() const
Definition: IRSymtab.h:197
Word ComdatIndex
The index into Header::Comdats, or -1 if not a comdat member.
Definition: IRSymtab.h:96
StringRef getSourceFileName() const
Returns the source file path specified at compile time.
Definition: IRSymtab.h:268
Str IRName
The unmangled symbol name, or the empty string if this is not an IR symbol.
Definition: IRSymtab.h:93
A reference to a range of objects in the symbol table.
Definition: IRSymtab.h:63
The contents of the irsymtab in a bitcode file.
Definition: IRSymtab.h:338
bool isTLS() const
Definition: IRSymtab.h:191
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
bool isExecutable() const
Definition: IRSymtab.h:200
Error build(ArrayRef< Module * > Mods, SmallVector< char, 0 > &Symtab, StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc)
Fills in Symtab and StrtabBuilder with a valid symbol and string table for Mods.
Definition: IRSymtab.cpp:277
Utility for building string tables with deduplicated suffixes.
SmallVector< char, 0 > Symtab
Definition: IRSymtab.h:339
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:63
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
bool isUnnamedAddr() const
Definition: IRSymtab.h:199
A reference to a string in the string table.
Definition: IRSymtab.h:54
const char * Name
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition: IRSymtab.h:285
Describes the range of a particular module's symbols within the symbol table.
Definition: IRSymtab.h:73
Word Version
Version number of the symtab format.
Definition: IRSymtab.h:130
bool canBeOmittedFromSymbolTable() const
Definition: IRSymtab.h:193
GlobalValue::VisibilityTypes getVisibility() const
Definition: IRSymtab.h:182
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:271
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
Str COFFLinkerOpts
COFF-specific: linker directives.
Definition: IRSymtab.h:147
Str Name
The mangled symbol name.
Definition: IRSymtab.h:89
This data structure contains rarely used symbol fields and is optionally referenced by a Symbol...
Definition: IRSymtab.h:118
This represents a symbol that has been read from a storage::Symbol and possibly a storage::Uncommon...
Definition: IRSymtab.h:159
StringRef getTargetTriple() const
Definition: IRSymtab.h:265
Range< Uncommon > Uncommons
Definition: IRSymtab.h:142
bool isCommon() const
Definition: IRSymtab.h:188
uint64_t getCommonSize() const
Definition: IRSymtab.h:202
StringRef getIRName() const
Returns the unmangled symbol name, or the empty string if this is not an IR symbol.
Definition: IRSymtab.h:174
Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition: IRSymtab.cpp:316
A range adaptor for a pair of iterators.
size_t getNumModules() const
Definition: IRSymtab.h:258
static bool isWeak(const MCSymbolELF &Sym)
symbol_range symbols() const
Returns the symbol table for the entire bitcode file.
Definition: IRSymtab.h:323
int getComdatIndex() const
Returns the index into the comdat table (see Reader::getComdatTable()), or -1 if not a comdat member...
Definition: IRSymtab.h:178
ArrayRef< T > get(StringRef Symtab) const
Definition: IRSymtab.h:66
Str Producer
The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
Definition: IRSymtab.h:137
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
bool isFormatSpecific() const
Definition: IRSymtab.h:198
#define C
Definition: LargeTest.cpp:25
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition: Endian.h:66
SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE, const storage::Uncommon *UncI, const Reader *R)
Definition: IRSymtab.h:307
#define I(x, y, z)
Definition: MD5.cpp:58
bool isWeak() const
Definition: IRSymtab.h:187
std::vector< StringRef > getComdatTable() const
Returns a table with all the comdats used by this file.
Definition: IRSymtab.h:271
StringRef get(StringRef Strtab) const
Definition: IRSymtab.h:57
symbol_range module_symbols(unsigned I) const
Returns a slice of the symbol table for the I'th module in the file.
Definition: IRSymtab.h:328
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is equivalent to an IR comdat.
Definition: IRSymtab.h:81
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
BumpPtrAllocator Alloc
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
StringRef COFFWeakExternFallbackName
Definition: IRSymtab.h:167
bool isIndirect() const
Definition: IRSymtab.h:189
Word UncBegin
The index of the first Uncommon for this Module.
Definition: IRSymtab.h:77
This class can be used to read a Symtab and Strtab produced by irsymtab::build.
Definition: IRSymtab.h:222
StringRef getName() const
Returns the mangled symbol name.
Definition: IRSymtab.h:170
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.
Definition: IRSymtab.h:123