Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / Object / IRSymtab.h
blob0601d995fd1832decdf79b9e0d2fe457c0e03898
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.
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.
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.
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_OBJECT_IRSYMTAB_H
24 #define LLVM_OBJECT_IRSYMTAB_H
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/IR/GlobalValue.h"
30 #include "llvm/Object/SymbolicFile.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/Error.h"
33 #include <cassert>
34 #include <cstdint>
35 #include <vector>
37 namespace llvm {
39 struct BitcodeFileContents;
40 class StringTableBuilder;
42 namespace irsymtab {
44 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.
50 using Word = support::ulittle32_t;
52 /// A reference to a string in the string table.
53 struct Str {
54 Word Offset, Size;
56 StringRef get(StringRef Strtab) const {
57 return {Strtab.data() + Offset, Size};
61 /// A reference to a range of objects in the symbol table.
62 template <typename T> struct Range {
63 Word Offset, Size;
65 ArrayRef<T> get(StringRef Symtab) const {
66 return {reinterpret_cast<const T *>(Symtab.data() + Offset), Size};
70 /// Describes the range of a particular module's symbols within the symbol
71 /// table.
72 struct Module {
73 Word Begin, End;
75 /// The index of the first Uncommon for this Module.
76 Word UncBegin;
79 /// This is equivalent to an IR comdat.
80 struct Comdat {
81 Str Name;
84 /// Contains the information needed by linkers for symbol resolution, as well as
85 /// by the LTO implementation itself.
86 struct Symbol {
87 /// The mangled symbol name.
88 Str Name;
90 /// The unmangled symbol name, or the empty string if this is not an IR
91 /// symbol.
92 Str IRName;
94 /// The index into Header::Comdats, or -1 if not a comdat member.
95 Word ComdatIndex;
97 Word Flags;
98 enum FlagBits {
99 FB_visibility, // 2 bits
100 FB_has_uncommon = FB_visibility + 2,
101 FB_undefined,
102 FB_weak,
103 FB_common,
104 FB_indirect,
105 FB_used,
106 FB_tls,
107 FB_may_omit,
108 FB_global,
109 FB_format_specific,
110 FB_unnamed_addr,
111 FB_executable,
115 /// This data structure contains rarely used symbol fields and is optionally
116 /// referenced by a Symbol.
117 struct Uncommon {
118 Word CommonSize, CommonAlign;
120 /// COFF-specific: the name of the symbol that a weak external resolves to
121 /// if not defined.
122 Str COFFWeakExternFallbackName;
124 /// Specified section name, if any.
125 Str SectionName;
128 struct Header {
129 /// Version number of the symtab format. This number should be incremented
130 /// when the format changes, but it does not need to be incremented if a
131 /// change to LLVM would cause it to create a different symbol table.
132 Word Version;
133 enum { kCurrentVersion = 1 };
135 /// The producer's version string (LLVM_VERSION_STRING " " LLVM_REVISION).
136 /// Consumers should rebuild the symbol table from IR if the producer's
137 /// version does not match the consumer's version due to potential differences
138 /// in symbol table format, symbol enumeration order and so on.
139 Str Producer;
141 Range<Module> Modules;
142 Range<Comdat> Comdats;
143 Range<Symbol> Symbols;
144 Range<Uncommon> Uncommons;
146 Str TargetTriple, SourceFileName;
148 /// COFF-specific: linker directives.
149 Str COFFLinkerOpts;
152 } // end namespace storage
154 /// Fills in Symtab and StrtabBuilder with a valid symbol and string table for
155 /// Mods.
156 Error build(ArrayRef<Module *> Mods, SmallVector<char, 0> &Symtab,
157 StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc);
159 /// This represents a symbol that has been read from a storage::Symbol and
160 /// possibly a storage::Uncommon.
161 struct Symbol {
162 // Copied from storage::Symbol.
163 StringRef Name, IRName;
164 int ComdatIndex;
165 uint32_t Flags;
167 // Copied from storage::Uncommon.
168 uint32_t CommonSize, CommonAlign;
169 StringRef COFFWeakExternFallbackName;
170 StringRef SectionName;
172 /// Returns the mangled symbol name.
173 StringRef getName() const { return Name; }
175 /// Returns the unmangled symbol name, or the empty string if this is not an
176 /// IR symbol.
177 StringRef getIRName() const { return IRName; }
179 /// Returns the index into the comdat table (see Reader::getComdatTable()), or
180 /// -1 if not a comdat member.
181 int getComdatIndex() const { return ComdatIndex; }
183 using S = storage::Symbol;
185 GlobalValue::VisibilityTypes getVisibility() const {
186 return GlobalValue::VisibilityTypes((Flags >> S::FB_visibility) & 3);
189 bool isUndefined() const { return (Flags >> S::FB_undefined) & 1; }
190 bool isWeak() const { return (Flags >> S::FB_weak) & 1; }
191 bool isCommon() const { return (Flags >> S::FB_common) & 1; }
192 bool isIndirect() const { return (Flags >> S::FB_indirect) & 1; }
193 bool isUsed() const { return (Flags >> S::FB_used) & 1; }
194 bool isTLS() const { return (Flags >> S::FB_tls) & 1; }
196 bool canBeOmittedFromSymbolTable() const {
197 return (Flags >> S::FB_may_omit) & 1;
200 bool isGlobal() const { return (Flags >> S::FB_global) & 1; }
201 bool isFormatSpecific() const { return (Flags >> S::FB_format_specific) & 1; }
202 bool isUnnamedAddr() const { return (Flags >> S::FB_unnamed_addr) & 1; }
203 bool isExecutable() const { return (Flags >> S::FB_executable) & 1; }
205 uint64_t getCommonSize() const {
206 assert(isCommon());
207 return CommonSize;
210 uint32_t getCommonAlignment() const {
211 assert(isCommon());
212 return CommonAlign;
215 /// COFF-specific: for weak externals, returns the name of the symbol that is
216 /// used as a fallback if the weak external remains undefined.
217 StringRef getCOFFWeakExternalFallback() const {
218 assert(isWeak() && isIndirect());
219 return COFFWeakExternFallbackName;
222 StringRef getSectionName() const { return SectionName; }
225 /// This class can be used to read a Symtab and Strtab produced by
226 /// irsymtab::build.
227 class Reader {
228 StringRef Symtab, Strtab;
230 ArrayRef<storage::Module> Modules;
231 ArrayRef<storage::Comdat> Comdats;
232 ArrayRef<storage::Symbol> Symbols;
233 ArrayRef<storage::Uncommon> Uncommons;
235 StringRef str(storage::Str S) const { return S.get(Strtab); }
237 template <typename T> ArrayRef<T> range(storage::Range<T> R) const {
238 return R.get(Symtab);
241 const storage::Header &header() const {
242 return *reinterpret_cast<const storage::Header *>(Symtab.data());
245 public:
246 class SymbolRef;
248 Reader() = default;
249 Reader(StringRef Symtab, StringRef Strtab) : Symtab(Symtab), Strtab(Strtab) {
250 Modules = range(header().Modules);
251 Comdats = range(header().Comdats);
252 Symbols = range(header().Symbols);
253 Uncommons = range(header().Uncommons);
256 using symbol_range = iterator_range<object::content_iterator<SymbolRef>>;
258 /// Returns the symbol table for the entire bitcode file.
259 /// The symbols enumerated by this method are ephemeral, but they can be
260 /// copied into an irsymtab::Symbol object.
261 symbol_range symbols() const;
263 size_t getNumModules() const { return Modules.size(); }
265 /// Returns a slice of the symbol table for the I'th module in the file.
266 /// The symbols enumerated by this method are ephemeral, but they can be
267 /// copied into an irsymtab::Symbol object.
268 symbol_range module_symbols(unsigned I) const;
270 StringRef getTargetTriple() const { return str(header().TargetTriple); }
272 /// Returns the source file path specified at compile time.
273 StringRef getSourceFileName() const { return str(header().SourceFileName); }
275 /// Returns a table with all the comdats used by this file.
276 std::vector<StringRef> getComdatTable() const {
277 std::vector<StringRef> ComdatTable;
278 ComdatTable.reserve(Comdats.size());
279 for (auto C : Comdats)
280 ComdatTable.push_back(str(C.Name));
281 return ComdatTable;
284 /// COFF-specific: returns linker options specified in the input file.
285 StringRef getCOFFLinkerOpts() const { return str(header().COFFLinkerOpts); }
288 /// Ephemeral symbols produced by Reader::symbols() and
289 /// Reader::module_symbols().
290 class Reader::SymbolRef : public Symbol {
291 const storage::Symbol *SymI, *SymE;
292 const storage::Uncommon *UncI;
293 const Reader *R;
295 void read() {
296 if (SymI == SymE)
297 return;
299 Name = R->str(SymI->Name);
300 IRName = R->str(SymI->IRName);
301 ComdatIndex = SymI->ComdatIndex;
302 Flags = SymI->Flags;
304 if (Flags & (1 << storage::Symbol::FB_has_uncommon)) {
305 CommonSize = UncI->CommonSize;
306 CommonAlign = UncI->CommonAlign;
307 COFFWeakExternFallbackName = R->str(UncI->COFFWeakExternFallbackName);
308 SectionName = R->str(UncI->SectionName);
309 } else
310 // Reset this field so it can be queried unconditionally for all symbols.
311 SectionName = "";
314 public:
315 SymbolRef(const storage::Symbol *SymI, const storage::Symbol *SymE,
316 const storage::Uncommon *UncI, const Reader *R)
317 : SymI(SymI), SymE(SymE), UncI(UncI), R(R) {
318 read();
321 void moveNext() {
322 ++SymI;
323 if (Flags & (1 << storage::Symbol::FB_has_uncommon))
324 ++UncI;
325 read();
328 bool operator==(const SymbolRef &Other) const { return SymI == Other.SymI; }
331 inline Reader::symbol_range Reader::symbols() const {
332 return {SymbolRef(Symbols.begin(), Symbols.end(), Uncommons.begin(), this),
333 SymbolRef(Symbols.end(), Symbols.end(), nullptr, this)};
336 inline Reader::symbol_range Reader::module_symbols(unsigned I) const {
337 const storage::Module &M = Modules[I];
338 const storage::Symbol *MBegin = Symbols.begin() + M.Begin,
339 *MEnd = Symbols.begin() + M.End;
340 return {SymbolRef(MBegin, MEnd, Uncommons.begin() + M.UncBegin, this),
341 SymbolRef(MEnd, MEnd, nullptr, this)};
344 /// The contents of the irsymtab in a bitcode file. Any underlying data for the
345 /// irsymtab are owned by Symtab and Strtab.
346 struct FileContents {
347 SmallVector<char, 0> Symtab, Strtab;
348 Reader TheReader;
351 /// Reads the contents of a bitcode file, creating its irsymtab if necessary.
352 Expected<FileContents> readBitcode(const BitcodeFileContents &BFC);
354 } // end namespace irsymtab
355 } // end namespace llvm
357 #endif // LLVM_OBJECT_IRSYMTAB_H