LLVM 22.0.0git
IRSymtab.h
Go to the documentation of this file.
1//===- IRSymtab.h - data definitions for IR symbol tables -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains data definitions and a reader and builder for a symbol
10// table for LLVM IR. Its purpose is to allow linkers and other consumers of
11// bitcode files to efficiently read the symbol table for symbol resolution
12// purposes without needing to construct a module in memory.
13//
14// As with most object files the symbol table has two parts: the symbol table
15// itself and a string table which is referenced by the symbol table.
16//
17// A symbol table corresponds to a single bitcode file, which may consist of
18// multiple modules, so symbol tables may likewise contain symbols for multiple
19// modules.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef LLVM_OBJECT_IRSYMTAB_H
24#define LLVM_OBJECT_IRSYMTAB_H
25
26#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/StringRef.h"
29#include "llvm/IR/Comdat.h"
30#include "llvm/IR/GlobalValue.h"
34#include "llvm/Support/Endian.h"
35#include "llvm/Support/Error.h"
36#include <cassert>
37#include <cstdint>
38#include <vector>
39
40namespace llvm {
41
42struct BitcodeFileContents;
43class StringTableBuilder;
44
45namespace irsymtab {
46
47namespace storage {
48
49// The data structures in this namespace define the low-level serialization
50// format. Clients that just want to read a symbol table should use the
51// irsymtab::Reader class.
52
54
55/// A reference to a string in the string table.
56struct Str {
58
59 StringRef get(StringRef Strtab) const {
60 return {Strtab.data() + Offset, Size};
61 }
62};
63
64/// A reference to a range of objects in the symbol table.
65template <typename T> struct Range {
67
68 ArrayRef<T> get(StringRef Symtab) const {
69 return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
70 }
71};
72
73/// Describes the range of a particular module's symbols within the symbol
74/// table.
75struct Module {
77
78 /// The index of the first Uncommon for this Module.
80};
81
82/// This is equivalent to an IR comdat.
83struct Comdat {
85
86 // llvm::Comdat::SelectionKind
88};
89
90/// Contains the information needed by linkers for symbol resolution, as well as
91/// by the LTO implementation itself.
92struct Symbol {
93 /// The mangled symbol name.
95
96 /// The unmangled symbol name, or the empty string if this is not an IR
97 /// symbol.
99
100 /// The index into Header::Comdats, or -1 if not a comdat member.
102
104 enum FlagBits {
105 FB_visibility, // 2 bits
118 };
119};
120
121/// This data structure contains rarely used symbol fields and is optionally
122/// referenced by a Symbol.
123struct Uncommon {
125
126 /// COFF-specific: the name of the symbol that a weak external resolves to
127 /// if not defined.
129
130 /// Specified section name, if any.
132};
133
134
135struct Header {
136 /// Version number of the symtab format. This number should be incremented
137 /// when the format changes, but it does not need to be incremented if a
138 /// change to LLVM would cause it to create a different symbol table.
140 enum { kCurrentVersion = 3 };
141
142 /// The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
143 /// Consumers should rebuild the symbol table from IR if the producer's
144 /// version does not match the consumer's version due to potential differences
145 /// in symbol table format, symbol enumeration order and so on.
147
152
154
155 /// COFF-specific: linker directives.
157
158 /// Dependent Library Specifiers
160};
161
162} // end namespace storage
163
164/// Fills in Symtab and StrtabBuilder with a valid symbol and string table for
165/// Mods.
167 StringTableBuilder &StrtabBuilder,
169
170/// This represents a symbol that has been read from a storage::Symbol and
171/// possibly a storage::Uncommon.
172struct Symbol {
173 // Copied from storage::Symbol.
178
179 // Copied from storage::Uncommon.
183
184 /// Returns the mangled symbol name.
185 StringRef getName() const { return Name; }
186
187 /// Returns the unmangled symbol name, or the empty string if this is not an
188 /// IR symbol.
189 StringRef getIRName() const { return IRName; }
190
191 /// Returns the index into the comdat table (see Reader::getComdatTable()), or
192 /// -1 if not a comdat member.
193 int getComdatIndex() const { return ComdatIndex; }
194
196
199 }
200
201 bool isUndefined() const { return (Flags >> S::FB_undefined) & 1; }
202 bool isWeak() const { return (Flags >> S::FB_weak) & 1; }
203 bool isCommon() const { return (Flags >> S::FB_common) & 1; }
204 bool isIndirect() const { return (Flags >> S::FB_indirect) & 1; }
205 bool isUsed() const { return (Flags >> S::FB_used) & 1; }
206 bool isTLS() const { return (Flags >> S::FB_tls) & 1; }
207
209 return (Flags >> S::FB_may_omit) & 1;
210 }
211
212 bool isGlobal() const { return (Flags >> S::FB_global) & 1; }
213 bool isFormatSpecific() const { return (Flags >> S::FB_format_specific) & 1; }
214 bool isUnnamedAddr() const { return (Flags >> S::FB_unnamed_addr) & 1; }
215 bool isExecutable() const { return (Flags >> S::FB_executable) & 1; }
216
218 assert(isCommon());
219 return CommonSize;
220 }
221
223 assert(isCommon());
224 return CommonAlign;
225 }
226
227 /// COFF-specific: for weak externals, returns the name of the symbol that is
228 /// used as a fallback if the weak external remains undefined.
230 assert(isWeak() && isIndirect());
232 }
233
235};
236
237/// This class can be used to read a Symtab and Strtab produced by
238/// irsymtab::build.
239class Reader {
240 StringRef Symtab, Strtab;
241
246 ArrayRef<storage::Str> DependentLibraries;
247
248 StringRef str(storage::Str S) const { return S.get(Strtab); }
249
250 template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
251 return R.get(Symtab);
252 }
253
254 const storage::Header &header() const {
255 return *reinterpret_cast<const storage::Header *>(Symtab.data());
256 }
257
258public:
259 class SymbolRef;
260
261 Reader() = default;
262 Reader(StringRef Symtab, StringRef Strtab) : Symtab(Symtab), Strtab(Strtab) {
263 Modules = range(header().Modules);
264 Comdats = range(header().Comdats);
265 Symbols = range(header().Symbols);
266 Uncommons = range(header().Uncommons);
267 DependentLibraries = range(header().DependentLibraries);
268 }
269
271
272 /// Returns the symbol table for the entire bitcode file.
273 /// The symbols enumerated by this method are ephemeral, but they can be
274 /// copied into an irsymtab::Symbol object.
275 symbol_range symbols() const;
276
277 size_t getNumModules() const { return Modules.size(); }
278
279 /// Returns a slice of the symbol table for the I'th module in the file.
280 /// The symbols enumerated by this method are ephemeral, but they can be
281 /// copied into an irsymtab::Symbol object.
282 symbol_range module_symbols(unsigned I) const;
283
284 StringRef getTargetTriple() const { return str(header().TargetTriple); }
285
286 /// Returns the source file path specified at compile time.
287 StringRef getSourceFileName() const { return str(header().SourceFileName); }
288
289 /// Returns a table with all the comdats used by this file.
290 std::vector<std::pair<StringRef, llvm::Comdat::SelectionKind>>
292 std::vector<std::pair<StringRef, llvm::Comdat::SelectionKind>> ComdatTable;
293 ComdatTable.reserve(Comdats.size());
294 for (auto C : Comdats)
295 ComdatTable.push_back({str(C.Name), llvm::Comdat::SelectionKind(
296 uint32_t(C.SelectionKind))});
297 return ComdatTable;
298 }
299
300 /// COFF-specific: returns linker options specified in the input file.
301 StringRef getCOFFLinkerOpts() const { return str(header().COFFLinkerOpts); }
302
303 /// Returns dependent library specifiers
304 std::vector<StringRef> getDependentLibraries() const {
305 std::vector<StringRef> Specifiers;
306 Specifiers.reserve(DependentLibraries.size());
307 for (auto S : DependentLibraries) {
308 Specifiers.push_back(str(S));
309 }
310 return Specifiers;
311 }
312};
313
314/// Ephemeral symbols produced by Reader::symbols() and
315/// Reader::module_symbols().
316class Reader::SymbolRef : public Symbol {
317 const storage::Symbol *SymI, *SymE;
318 const storage::Uncommon *UncI;
319 const Reader *R;
320
321 void read() {
322 if (SymI == SymE)
323 return;
324
325 Name = R->str(SymI->Name);
326 IRName = R->str(SymI->IRName);
327 ComdatIndex = SymI->ComdatIndex;
328 Flags = SymI->Flags;
329
331 CommonSize = UncI->CommonSize;
332 CommonAlign = UncI->CommonAlign;
334 SectionName = R->str(UncI->SectionName);
335 } else
336 // Reset this field so it can be queried unconditionally for all symbols.
337 SectionName = "";
338 }
339
340public:
341 SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE,
342 const storage::Uncommon *UncI, const Reader *R)
343 : SymI(SymI), SymE(SymE), UncI(UncI), R(R) {
344 read();
345 }
346
347 void moveNext() {
348 ++SymI;
350 ++UncI;
351 read();
352 }
353
354 bool operator==(const SymbolRef &Other) const { return SymI == Other.SymI; }
355};
356
358 return {SymbolRef(Symbols.begin(), Symbols.end(), Uncommons.begin(), this),
359 SymbolRef(Symbols.end(), Symbols.end(), nullptr, this)};
360}
361
363 const storage::Module &M = Modules[I];
364 const storage::Symbol *MBegin = Symbols.begin() + M.Begin,
365 *MEnd = Symbols.begin() + M.End;
366 return {SymbolRef(MBegin, MEnd, Uncommons.begin() + M.UncBegin, this),
367 SymbolRef(MEnd, MEnd, nullptr, this)};
368}
369
370/// The contents of the irsymtab in a bitcode file. Any underlying data for the
371/// irsymtab are owned by Symtab and Strtab.
375};
376
377/// Reads the contents of a bitcode file, creating its irsymtab if necessary.
379
380} // end namespace irsymtab
381} // end namespace llvm
382
383#endif // LLVM_OBJECT_IRSYMTAB_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
#define LLVM_ABI
Definition: Compiler.h:213
#define I(x, y, z)
Definition: MD5.cpp:58
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:67
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:67
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:148
Utility for building string tables with deduplicated suffixes.
Ephemeral symbols produced by Reader::symbols() and Reader::module_symbols().
Definition: IRSymtab.h:316
SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE, const storage::Uncommon *UncI, const Reader *R)
Definition: IRSymtab.h:341
bool operator==(const SymbolRef &Other) const
Definition: IRSymtab.h:354
This class can be used to read a Symtab and Strtab produced by irsymtab::build.
Definition: IRSymtab.h:239
Reader(StringRef Symtab, StringRef Strtab)
Definition: IRSymtab.h:262
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:362
symbol_range symbols() const
Returns the symbol table for the entire bitcode file.
Definition: IRSymtab.h:357
StringRef getTargetTriple() const
Definition: IRSymtab.h:284
std::vector< std::pair< StringRef, llvm::Comdat::SelectionKind > > getComdatTable() const
Returns a table with all the comdats used by this file.
Definition: IRSymtab.h:291
StringRef getCOFFLinkerOpts() const
COFF-specific: returns linker options specified in the input file.
Definition: IRSymtab.h:301
iterator_range< object::content_iterator< SymbolRef > > symbol_range
Definition: IRSymtab.h:270
StringRef getSourceFileName() const
Returns the source file path specified at compile time.
Definition: IRSymtab.h:287
std::vector< StringRef > getDependentLibraries() const
Returns dependent library specifiers.
Definition: IRSymtab.h:304
size_t getNumModules() const
Definition: IRSymtab.h:277
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
LLVM_ABI Expected< FileContents > readBitcode(const BitcodeFileContents &BFC)
Reads the contents of a bitcode file, creating its irsymtab if necessary.
Definition: IRSymtab.cpp:401
LLVM_ABI 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:361
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition: Endian.h:289
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
The contents of the irsymtab in a bitcode file.
Definition: IRSymtab.h:372
SmallVector< char, 0 > Strtab
Definition: IRSymtab.h:373
SmallVector< char, 0 > Symtab
Definition: IRSymtab.h:373
This represents a symbol that has been read from a storage::Symbol and possibly a storage::Uncommon.
Definition: IRSymtab.h:172
StringRef getName() const
Returns the mangled symbol name.
Definition: IRSymtab.h:185
StringRef SectionName
Definition: IRSymtab.h:182
bool canBeOmittedFromSymbolTable() const
Definition: IRSymtab.h:208
bool isGlobal() const
Definition: IRSymtab.h:212
bool isUsed() const
Definition: IRSymtab.h:205
StringRef getSectionName() const
Definition: IRSymtab.h:234
bool isTLS() const
Definition: IRSymtab.h:206
bool isUnnamedAddr() const
Definition: IRSymtab.h:214
bool isWeak() const
Definition: IRSymtab.h:202
StringRef COFFWeakExternFallbackName
Definition: IRSymtab.h:181
bool isIndirect() const
Definition: IRSymtab.h:204
bool isCommon() const
Definition: IRSymtab.h:203
uint32_t getCommonAlignment() const
Definition: IRSymtab.h:222
bool isFormatSpecific() const
Definition: IRSymtab.h:213
bool isExecutable() const
Definition: IRSymtab.h:215
uint64_t getCommonSize() const
Definition: IRSymtab.h:217
int getComdatIndex() const
Returns the index into the comdat table (see Reader::getComdatTable()), or -1 if not a comdat member.
Definition: IRSymtab.h:193
GlobalValue::VisibilityTypes getVisibility() const
Definition: IRSymtab.h:197
bool isUndefined() const
Definition: IRSymtab.h:201
StringRef getIRName() const
Returns the unmangled symbol name, or the empty string if this is not an IR symbol.
Definition: IRSymtab.h:189
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:229
This is equivalent to an IR comdat.
Definition: IRSymtab.h:83
Word Version
Version number of the symtab format.
Definition: IRSymtab.h:139
Str COFFLinkerOpts
COFF-specific: linker directives.
Definition: IRSymtab.h:156
Range< Str > DependentLibraries
Dependent Library Specifiers.
Definition: IRSymtab.h:159
Range< Uncommon > Uncommons
Definition: IRSymtab.h:151
Str Producer
The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
Definition: IRSymtab.h:146
Describes the range of a particular module's symbols within the symbol table.
Definition: IRSymtab.h:75
Word UncBegin
The index of the first Uncommon for this Module.
Definition: IRSymtab.h:79
A reference to a range of objects in the symbol table.
Definition: IRSymtab.h:65
ArrayRef< T > get(StringRef Symtab) const
Definition: IRSymtab.h:68
A reference to a string in the string table.
Definition: IRSymtab.h:56
StringRef get(StringRef Strtab) const
Definition: IRSymtab.h:59
Contains the information needed by linkers for symbol resolution, as well as by the LTO implementatio...
Definition: IRSymtab.h:92
Str Name
The mangled symbol name.
Definition: IRSymtab.h:94
Str IRName
The unmangled symbol name, or the empty string if this is not an IR symbol.
Definition: IRSymtab.h:98
Word ComdatIndex
The index into Header::Comdats, or -1 if not a comdat member.
Definition: IRSymtab.h:101
This data structure contains rarely used symbol fields and is optionally referenced by a Symbol.
Definition: IRSymtab.h:123
Str SectionName
Specified section name, if any.
Definition: IRSymtab.h:131
Str COFFWeakExternFallbackName
COFF-specific: the name of the symbol that a weak external resolves to if not defined.
Definition: IRSymtab.h:128