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