1 //===- IRSymtab.cpp - implementation of IR symbol tables ------------------===// 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 #include "llvm/Object/IRSymtab.h" 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/DenseMap.h" 12 #include "llvm/ADT/SmallPtrSet.h" 13 #include "llvm/ADT/SmallString.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/ADT/Triple.h" 17 #include "llvm/Config/llvm-config.h" 18 #include "llvm/IR/Comdat.h" 19 #include "llvm/IR/DataLayout.h" 20 #include "llvm/IR/GlobalAlias.h" 21 #include "llvm/IR/GlobalObject.h" 22 #include "llvm/IR/Mangler.h" 23 #include "llvm/IR/Metadata.h" 24 #include "llvm/IR/Module.h" 25 #include "llvm/Bitcode/BitcodeReader.h" 26 #include "llvm/MC/StringTableBuilder.h" 27 #include "llvm/Object/IRObjectFile.h" 28 #include "llvm/Object/ModuleSymbolTable.h" 29 #include "llvm/Object/SymbolicFile.h" 30 #include "llvm/Support/Allocator.h" 31 #include "llvm/Support/Casting.h" 32 #include "llvm/Support/Error.h" 33 #include "llvm/Support/StringSaver.h" 34 #include "llvm/Support/VCSRevision.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include <cassert> 37 #include <string> 38 #include <utility> 39 #include <vector> 40 41 using namespace llvm; 42 using namespace irsymtab; 43 44 static const char *LibcallRoutineNames[] = { 45 #define HANDLE_LIBCALL(code, name) name, 46 #include "llvm/IR/RuntimeLibcalls.def" 47 #undef HANDLE_LIBCALL 48 }; 49 50 namespace { 51 52 const char *getExpectedProducerName() { 53 static char DefaultName[] = LLVM_VERSION_STRING 54 #ifdef LLVM_REVISION 55 " " LLVM_REVISION 56 #endif 57 ; 58 // Allows for testing of the irsymtab writer and upgrade mechanism. This 59 // environment variable should not be set by users. 60 if (char *OverrideName = getenv("LLVM_OVERRIDE_PRODUCER")) 61 return OverrideName; 62 return DefaultName; 63 } 64 65 const char *kExpectedProducerName = getExpectedProducerName(); 66 67 /// Stores the temporary state that is required to build an IR symbol table. 68 struct Builder { 69 SmallVector<char, 0> &Symtab; 70 StringTableBuilder &StrtabBuilder; 71 StringSaver Saver; 72 73 // This ctor initializes a StringSaver using the passed in BumpPtrAllocator. 74 // The StringTableBuilder does not create a copy of any strings added to it, 75 // so this provides somewhere to store any strings that we create. 76 Builder(SmallVector<char, 0> &Symtab, StringTableBuilder &StrtabBuilder, 77 BumpPtrAllocator &Alloc) 78 : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc) {} 79 80 DenseMap<const Comdat *, int> ComdatMap; 81 Mangler Mang; 82 Triple TT; 83 84 std::vector<storage::Comdat> Comdats; 85 std::vector<storage::Module> Mods; 86 std::vector<storage::Symbol> Syms; 87 std::vector<storage::Uncommon> Uncommons; 88 89 std::string COFFLinkerOpts; 90 raw_string_ostream COFFLinkerOptsOS{COFFLinkerOpts}; 91 92 std::vector<storage::Str> DependentLibraries; 93 94 void setStr(storage::Str &S, StringRef Value) { 95 S.Offset = StrtabBuilder.add(Value); 96 S.Size = Value.size(); 97 } 98 99 template <typename T> 100 void writeRange(storage::Range<T> &R, const std::vector<T> &Objs) { 101 R.Offset = Symtab.size(); 102 R.Size = Objs.size(); 103 Symtab.insert(Symtab.end(), reinterpret_cast<const char *>(Objs.data()), 104 reinterpret_cast<const char *>(Objs.data() + Objs.size())); 105 } 106 107 Expected<int> getComdatIndex(const Comdat *C, const Module *M); 108 109 Error addModule(Module *M); 110 Error addSymbol(const ModuleSymbolTable &Msymtab, 111 const SmallPtrSet<GlobalValue *, 8> &Used, 112 ModuleSymbolTable::Symbol Sym); 113 114 Error build(ArrayRef<Module *> Mods); 115 }; 116 117 Error Builder::addModule(Module *M) { 118 if (M->getDataLayoutStr().empty()) 119 return make_error<StringError>("input module has no datalayout", 120 inconvertibleErrorCode()); 121 122 SmallPtrSet<GlobalValue *, 8> Used; 123 collectUsedGlobalVariables(*M, Used, /*CompilerUsed*/ false); 124 125 ModuleSymbolTable Msymtab; 126 Msymtab.addModule(M); 127 128 storage::Module Mod; 129 Mod.Begin = Syms.size(); 130 Mod.End = Syms.size() + Msymtab.symbols().size(); 131 Mod.UncBegin = Uncommons.size(); 132 Mods.push_back(Mod); 133 134 if (TT.isOSBinFormatCOFF()) { 135 if (auto E = M->materializeMetadata()) 136 return E; 137 if (NamedMDNode *LinkerOptions = 138 M->getNamedMetadata("llvm.linker.options")) { 139 for (MDNode *MDOptions : LinkerOptions->operands()) 140 for (const MDOperand &MDOption : cast<MDNode>(MDOptions)->operands()) 141 COFFLinkerOptsOS << " " << cast<MDString>(MDOption)->getString(); 142 } 143 } 144 145 if (TT.isOSBinFormatELF()) { 146 if (auto E = M->materializeMetadata()) 147 return E; 148 if (NamedMDNode *N = M->getNamedMetadata("llvm.dependent-libraries")) { 149 for (MDNode *MDOptions : N->operands()) { 150 const auto OperandStr = 151 cast<MDString>(cast<MDNode>(MDOptions)->getOperand(0))->getString(); 152 storage::Str Specifier; 153 setStr(Specifier, OperandStr); 154 DependentLibraries.emplace_back(Specifier); 155 } 156 } 157 } 158 159 for (ModuleSymbolTable::Symbol Msym : Msymtab.symbols()) 160 if (Error Err = addSymbol(Msymtab, Used, Msym)) 161 return Err; 162 163 return Error::success(); 164 } 165 166 Expected<int> Builder::getComdatIndex(const Comdat *C, const Module *M) { 167 auto P = ComdatMap.insert(std::make_pair(C, Comdats.size())); 168 if (P.second) { 169 std::string Name; 170 if (TT.isOSBinFormatCOFF()) { 171 const GlobalValue *GV = M->getNamedValue(C->getName()); 172 if (!GV) 173 return make_error<StringError>("Could not find leader", 174 inconvertibleErrorCode()); 175 // Internal leaders do not affect symbol resolution, therefore they do not 176 // appear in the symbol table. 177 if (GV->hasLocalLinkage()) { 178 P.first->second = -1; 179 return -1; 180 } 181 llvm::raw_string_ostream OS(Name); 182 Mang.getNameWithPrefix(OS, GV, false); 183 } else { 184 Name = std::string(C->getName()); 185 } 186 187 storage::Comdat Comdat; 188 setStr(Comdat.Name, Saver.save(Name)); 189 Comdats.push_back(Comdat); 190 } 191 192 return P.first->second; 193 } 194 195 Error Builder::addSymbol(const ModuleSymbolTable &Msymtab, 196 const SmallPtrSet<GlobalValue *, 8> &Used, 197 ModuleSymbolTable::Symbol Msym) { 198 Syms.emplace_back(); 199 storage::Symbol &Sym = Syms.back(); 200 Sym = {}; 201 202 storage::Uncommon *Unc = nullptr; 203 auto Uncommon = [&]() -> storage::Uncommon & { 204 if (Unc) 205 return *Unc; 206 Sym.Flags |= 1 << storage::Symbol::FB_has_uncommon; 207 Uncommons.emplace_back(); 208 Unc = &Uncommons.back(); 209 *Unc = {}; 210 setStr(Unc->COFFWeakExternFallbackName, ""); 211 setStr(Unc->SectionName, ""); 212 return *Unc; 213 }; 214 215 SmallString<64> Name; 216 { 217 raw_svector_ostream OS(Name); 218 Msymtab.printSymbolName(OS, Msym); 219 } 220 setStr(Sym.Name, Saver.save(StringRef(Name))); 221 222 auto Flags = Msymtab.getSymbolFlags(Msym); 223 if (Flags & object::BasicSymbolRef::SF_Undefined) 224 Sym.Flags |= 1 << storage::Symbol::FB_undefined; 225 if (Flags & object::BasicSymbolRef::SF_Weak) 226 Sym.Flags |= 1 << storage::Symbol::FB_weak; 227 if (Flags & object::BasicSymbolRef::SF_Common) 228 Sym.Flags |= 1 << storage::Symbol::FB_common; 229 if (Flags & object::BasicSymbolRef::SF_Indirect) 230 Sym.Flags |= 1 << storage::Symbol::FB_indirect; 231 if (Flags & object::BasicSymbolRef::SF_Global) 232 Sym.Flags |= 1 << storage::Symbol::FB_global; 233 if (Flags & object::BasicSymbolRef::SF_FormatSpecific) 234 Sym.Flags |= 1 << storage::Symbol::FB_format_specific; 235 if (Flags & object::BasicSymbolRef::SF_Executable) 236 Sym.Flags |= 1 << storage::Symbol::FB_executable; 237 238 Sym.ComdatIndex = -1; 239 auto *GV = Msym.dyn_cast<GlobalValue *>(); 240 if (!GV) { 241 // Undefined module asm symbols act as GC roots and are implicitly used. 242 if (Flags & object::BasicSymbolRef::SF_Undefined) 243 Sym.Flags |= 1 << storage::Symbol::FB_used; 244 setStr(Sym.IRName, ""); 245 return Error::success(); 246 } 247 248 setStr(Sym.IRName, GV->getName()); 249 250 bool IsBuiltinFunc = false; 251 252 for (const char *LibcallName : LibcallRoutineNames) 253 if (GV->getName() == LibcallName) 254 IsBuiltinFunc = true; 255 256 if (Used.count(GV) || IsBuiltinFunc) 257 Sym.Flags |= 1 << storage::Symbol::FB_used; 258 if (GV->isThreadLocal()) 259 Sym.Flags |= 1 << storage::Symbol::FB_tls; 260 if (GV->hasGlobalUnnamedAddr()) 261 Sym.Flags |= 1 << storage::Symbol::FB_unnamed_addr; 262 if (GV->canBeOmittedFromSymbolTable()) 263 Sym.Flags |= 1 << storage::Symbol::FB_may_omit; 264 Sym.Flags |= unsigned(GV->getVisibility()) << storage::Symbol::FB_visibility; 265 266 if (Flags & object::BasicSymbolRef::SF_Common) { 267 auto *GVar = dyn_cast<GlobalVariable>(GV); 268 if (!GVar) 269 return make_error<StringError>("Only variables can have common linkage!", 270 inconvertibleErrorCode()); 271 Uncommon().CommonSize = GV->getParent()->getDataLayout().getTypeAllocSize( 272 GV->getType()->getElementType()); 273 Uncommon().CommonAlign = GVar->getAlignment(); 274 } 275 276 const GlobalObject *Base = GV->getBaseObject(); 277 if (!Base) 278 return make_error<StringError>("Unable to determine comdat of alias!", 279 inconvertibleErrorCode()); 280 if (const Comdat *C = Base->getComdat()) { 281 Expected<int> ComdatIndexOrErr = getComdatIndex(C, GV->getParent()); 282 if (!ComdatIndexOrErr) 283 return ComdatIndexOrErr.takeError(); 284 Sym.ComdatIndex = *ComdatIndexOrErr; 285 } 286 287 if (TT.isOSBinFormatCOFF()) { 288 emitLinkerFlagsForGlobalCOFF(COFFLinkerOptsOS, GV, TT, Mang); 289 290 if ((Flags & object::BasicSymbolRef::SF_Weak) && 291 (Flags & object::BasicSymbolRef::SF_Indirect)) { 292 auto *Fallback = dyn_cast<GlobalValue>( 293 cast<GlobalAlias>(GV)->getAliasee()->stripPointerCasts()); 294 if (!Fallback) 295 return make_error<StringError>("Invalid weak external", 296 inconvertibleErrorCode()); 297 std::string FallbackName; 298 raw_string_ostream OS(FallbackName); 299 Msymtab.printSymbolName(OS, Fallback); 300 OS.flush(); 301 setStr(Uncommon().COFFWeakExternFallbackName, Saver.save(FallbackName)); 302 } 303 } 304 305 if (!Base->getSection().empty()) 306 setStr(Uncommon().SectionName, Saver.save(Base->getSection())); 307 308 return Error::success(); 309 } 310 311 Error Builder::build(ArrayRef<Module *> IRMods) { 312 storage::Header Hdr; 313 314 assert(!IRMods.empty()); 315 Hdr.Version = storage::Header::kCurrentVersion; 316 setStr(Hdr.Producer, kExpectedProducerName); 317 setStr(Hdr.TargetTriple, IRMods[0]->getTargetTriple()); 318 setStr(Hdr.SourceFileName, IRMods[0]->getSourceFileName()); 319 TT = Triple(IRMods[0]->getTargetTriple()); 320 321 for (auto *M : IRMods) 322 if (Error Err = addModule(M)) 323 return Err; 324 325 COFFLinkerOptsOS.flush(); 326 setStr(Hdr.COFFLinkerOpts, Saver.save(COFFLinkerOpts)); 327 328 // We are about to fill in the header's range fields, so reserve space for it 329 // and copy it in afterwards. 330 Symtab.resize(sizeof(storage::Header)); 331 writeRange(Hdr.Modules, Mods); 332 writeRange(Hdr.Comdats, Comdats); 333 writeRange(Hdr.Symbols, Syms); 334 writeRange(Hdr.Uncommons, Uncommons); 335 writeRange(Hdr.DependentLibraries, DependentLibraries); 336 *reinterpret_cast<storage::Header *>(Symtab.data()) = Hdr; 337 return Error::success(); 338 } 339 340 } // end anonymous namespace 341 342 Error irsymtab::build(ArrayRef<Module *> Mods, SmallVector<char, 0> &Symtab, 343 StringTableBuilder &StrtabBuilder, 344 BumpPtrAllocator &Alloc) { 345 return Builder(Symtab, StrtabBuilder, Alloc).build(Mods); 346 } 347 348 // Upgrade a vector of bitcode modules created by an old version of LLVM by 349 // creating an irsymtab for them in the current format. 350 static Expected<FileContents> upgrade(ArrayRef<BitcodeModule> BMs) { 351 FileContents FC; 352 353 LLVMContext Ctx; 354 std::vector<Module *> Mods; 355 std::vector<std::unique_ptr<Module>> OwnedMods; 356 for (auto BM : BMs) { 357 Expected<std::unique_ptr<Module>> MOrErr = 358 BM.getLazyModule(Ctx, /*ShouldLazyLoadMetadata*/ true, 359 /*IsImporting*/ false); 360 if (!MOrErr) 361 return MOrErr.takeError(); 362 363 Mods.push_back(MOrErr->get()); 364 OwnedMods.push_back(std::move(*MOrErr)); 365 } 366 367 StringTableBuilder StrtabBuilder(StringTableBuilder::RAW); 368 BumpPtrAllocator Alloc; 369 if (Error E = build(Mods, FC.Symtab, StrtabBuilder, Alloc)) 370 return std::move(E); 371 372 StrtabBuilder.finalizeInOrder(); 373 FC.Strtab.resize(StrtabBuilder.getSize()); 374 StrtabBuilder.write((uint8_t *)FC.Strtab.data()); 375 376 FC.TheReader = {{FC.Symtab.data(), FC.Symtab.size()}, 377 {FC.Strtab.data(), FC.Strtab.size()}}; 378 return std::move(FC); 379 } 380 381 Expected<FileContents> irsymtab::readBitcode(const BitcodeFileContents &BFC) { 382 if (BFC.Mods.empty()) 383 return make_error<StringError>("Bitcode file does not contain any modules", 384 inconvertibleErrorCode()); 385 386 if (BFC.StrtabForSymtab.empty() || 387 BFC.Symtab.size() < sizeof(storage::Header)) 388 return upgrade(BFC.Mods); 389 390 // We cannot use the regular reader to read the version and producer, because 391 // it will expect the header to be in the current format. The only thing we 392 // can rely on is that the version and producer will be present as the first 393 // struct elements. 394 auto *Hdr = reinterpret_cast<const storage::Header *>(BFC.Symtab.data()); 395 unsigned Version = Hdr->Version; 396 StringRef Producer = Hdr->Producer.get(BFC.StrtabForSymtab); 397 if (Version != storage::Header::kCurrentVersion || 398 Producer != kExpectedProducerName) 399 return upgrade(BFC.Mods); 400 401 FileContents FC; 402 FC.TheReader = {{BFC.Symtab.data(), BFC.Symtab.size()}, 403 {BFC.StrtabForSymtab.data(), BFC.StrtabForSymtab.size()}}; 404 405 // Finally, make sure that the number of modules in the symbol table matches 406 // the number of modules in the bitcode file. If they differ, it may mean that 407 // the bitcode file was created by binary concatenation, so we need to create 408 // a new symbol table from scratch. 409 if (FC.TheReader.getNumModules() != BFC.Mods.size()) 410 return upgrade(std::move(BFC.Mods)); 411 412 return std::move(FC); 413 } 414