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