1 //===- InputFiles.cpp -----------------------------------------------------===// 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 "InputFiles.h" 10 #include "Config.h" 11 #include "InputChunks.h" 12 #include "InputEvent.h" 13 #include "InputGlobal.h" 14 #include "SymbolTable.h" 15 #include "lld/Common/ErrorHandler.h" 16 #include "lld/Common/Memory.h" 17 #include "lld/Common/Reproduce.h" 18 #include "llvm/Object/Binary.h" 19 #include "llvm/Object/Wasm.h" 20 #include "llvm/Support/TarWriter.h" 21 #include "llvm/Support/raw_ostream.h" 22 23 #define DEBUG_TYPE "lld" 24 25 using namespace lld; 26 using namespace lld::wasm; 27 28 using namespace llvm; 29 using namespace llvm::object; 30 using namespace llvm::wasm; 31 32 std::unique_ptr<llvm::TarWriter> lld::wasm::Tar; 33 34 Optional<MemoryBufferRef> lld::wasm::readFile(StringRef Path) { 35 log("Loading: " + Path); 36 37 auto MBOrErr = MemoryBuffer::getFile(Path); 38 if (auto EC = MBOrErr.getError()) { 39 error("cannot open " + Path + ": " + EC.message()); 40 return None; 41 } 42 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 43 MemoryBufferRef MBRef = MB->getMemBufferRef(); 44 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership 45 46 if (Tar) 47 Tar->append(relativeToRoot(Path), MBRef.getBuffer()); 48 return MBRef; 49 } 50 51 InputFile *lld::wasm::createObjectFile(MemoryBufferRef MB, 52 StringRef ArchiveName) { 53 file_magic Magic = identify_magic(MB.getBuffer()); 54 if (Magic == file_magic::wasm_object) { 55 std::unique_ptr<Binary> Bin = check(createBinary(MB)); 56 auto *Obj = cast<WasmObjectFile>(Bin.get()); 57 if (Obj->isSharedObject()) 58 return make<SharedFile>(MB); 59 return make<ObjFile>(MB, ArchiveName); 60 } 61 62 if (Magic == file_magic::bitcode) 63 return make<BitcodeFile>(MB, ArchiveName); 64 65 fatal("unknown file type: " + MB.getBufferIdentifier()); 66 } 67 68 void ObjFile::dumpInfo() const { 69 log("info for: " + toString(this) + 70 "\n Symbols : " + Twine(Symbols.size()) + 71 "\n Function Imports : " + Twine(WasmObj->getNumImportedFunctions()) + 72 "\n Global Imports : " + Twine(WasmObj->getNumImportedGlobals()) + 73 "\n Event Imports : " + Twine(WasmObj->getNumImportedEvents())); 74 } 75 76 // Relocations contain either symbol or type indices. This function takes a 77 // relocation and returns relocated index (i.e. translates from the input 78 // symbol/type space to the output symbol/type space). 79 uint32_t ObjFile::calcNewIndex(const WasmRelocation &Reloc) const { 80 if (Reloc.Type == R_WASM_TYPE_INDEX_LEB) { 81 assert(TypeIsUsed[Reloc.Index]); 82 return TypeMap[Reloc.Index]; 83 } 84 const Symbol *Sym = Symbols[Reloc.Index]; 85 if (auto *SS = dyn_cast<SectionSymbol>(Sym)) 86 Sym = SS->getOutputSectionSymbol(); 87 return Sym->getOutputSymbolIndex(); 88 } 89 90 // Relocations can contain addend for combined sections. This function takes a 91 // relocation and returns updated addend by offset in the output section. 92 uint32_t ObjFile::calcNewAddend(const WasmRelocation &Reloc) const { 93 switch (Reloc.Type) { 94 case R_WASM_MEMORY_ADDR_LEB: 95 case R_WASM_MEMORY_ADDR_SLEB: 96 case R_WASM_MEMORY_ADDR_I32: 97 case R_WASM_FUNCTION_OFFSET_I32: 98 return Reloc.Addend; 99 case R_WASM_SECTION_OFFSET_I32: 100 return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend; 101 default: 102 llvm_unreachable("unexpected relocation type"); 103 } 104 } 105 106 // Calculate the value we expect to find at the relocation location. 107 // This is used as a sanity check before applying a relocation to a given 108 // location. It is useful for catching bugs in the compiler and linker. 109 uint32_t ObjFile::calcExpectedValue(const WasmRelocation &Reloc) const { 110 switch (Reloc.Type) { 111 case R_WASM_TABLE_INDEX_I32: 112 case R_WASM_TABLE_INDEX_SLEB: 113 case R_WASM_TABLE_INDEX_REL_SLEB: { 114 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 115 return TableEntries[Sym.Info.ElementIndex]; 116 } 117 case R_WASM_MEMORY_ADDR_SLEB: 118 case R_WASM_MEMORY_ADDR_I32: 119 case R_WASM_MEMORY_ADDR_LEB: 120 case R_WASM_MEMORY_ADDR_REL_SLEB: { 121 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 122 if (Sym.isUndefined()) 123 return 0; 124 const WasmSegment &Segment = 125 WasmObj->dataSegments()[Sym.Info.DataRef.Segment]; 126 return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset + 127 Reloc.Addend; 128 } 129 case R_WASM_FUNCTION_OFFSET_I32: { 130 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 131 InputFunction *F = 132 Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()]; 133 return F->getFunctionInputOffset() + F->getFunctionCodeOffset() + 134 Reloc.Addend; 135 } 136 case R_WASM_SECTION_OFFSET_I32: 137 return Reloc.Addend; 138 case R_WASM_TYPE_INDEX_LEB: 139 return Reloc.Index; 140 case R_WASM_FUNCTION_INDEX_LEB: 141 case R_WASM_GLOBAL_INDEX_LEB: 142 case R_WASM_EVENT_INDEX_LEB: { 143 const WasmSymbol &Sym = WasmObj->syms()[Reloc.Index]; 144 return Sym.Info.ElementIndex; 145 } 146 default: 147 llvm_unreachable("unknown relocation type"); 148 } 149 } 150 151 // Translate from the relocation's index into the final linked output value. 152 uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const { 153 const Symbol* Sym = nullptr; 154 if (Reloc.Type != R_WASM_TYPE_INDEX_LEB) { 155 Sym = Symbols[Reloc.Index]; 156 157 // We can end up with relocations against non-live symbols. For example 158 // in debug sections. 159 if ((isa<FunctionSymbol>(Sym) || isa<DataSymbol>(Sym)) && !Sym->isLive()) 160 return 0; 161 162 // Special handling for undefined data symbols. Most relocations against 163 // such symbols cannot be resolved. 164 if (isa<DataSymbol>(Sym) && Sym->isUndefined()) { 165 if (Sym->isWeak() || Config->Relocatable) 166 return 0; 167 // R_WASM_MEMORY_ADDR_I32 relocations in PIC code are turned into runtime 168 // fixups in __wasm_apply_relocs 169 if (Config->Pic && Reloc.Type == R_WASM_MEMORY_ADDR_I32) 170 return 0; 171 if (Reloc.Type != R_WASM_GLOBAL_INDEX_LEB) { 172 llvm_unreachable( 173 ("invalid relocation against undefined data symbol: " + toString(*Sym)) 174 .c_str()); 175 } 176 } 177 } 178 179 switch (Reloc.Type) { 180 case R_WASM_TABLE_INDEX_I32: 181 case R_WASM_TABLE_INDEX_SLEB: 182 case R_WASM_TABLE_INDEX_REL_SLEB: 183 if (Config->Pic && !getFunctionSymbol(Reloc.Index)->hasTableIndex()) 184 return 0; 185 return getFunctionSymbol(Reloc.Index)->getTableIndex(); 186 case R_WASM_MEMORY_ADDR_SLEB: 187 case R_WASM_MEMORY_ADDR_I32: 188 case R_WASM_MEMORY_ADDR_LEB: 189 case R_WASM_MEMORY_ADDR_REL_SLEB: 190 return cast<DefinedData>(Sym)->getVirtualAddress() + Reloc.Addend; 191 case R_WASM_TYPE_INDEX_LEB: 192 return TypeMap[Reloc.Index]; 193 case R_WASM_FUNCTION_INDEX_LEB: 194 return getFunctionSymbol(Reloc.Index)->getFunctionIndex(); 195 case R_WASM_GLOBAL_INDEX_LEB: 196 if (auto GS = dyn_cast<GlobalSymbol>(Sym)) 197 return GS->getGlobalIndex(); 198 return Sym->getGOTIndex(); 199 case R_WASM_EVENT_INDEX_LEB: 200 return getEventSymbol(Reloc.Index)->getEventIndex(); 201 case R_WASM_FUNCTION_OFFSET_I32: { 202 auto *F = cast<DefinedFunction>(Sym); 203 return F->Function->OutputOffset + F->Function->getFunctionCodeOffset() + 204 Reloc.Addend; 205 } 206 case R_WASM_SECTION_OFFSET_I32: 207 return getSectionSymbol(Reloc.Index)->Section->OutputOffset + Reloc.Addend; 208 default: 209 llvm_unreachable("unknown relocation type"); 210 } 211 } 212 213 template <class T> 214 static void setRelocs(const std::vector<T *> &Chunks, 215 const WasmSection *Section) { 216 if (!Section) 217 return; 218 219 ArrayRef<WasmRelocation> Relocs = Section->Relocations; 220 assert(std::is_sorted(Relocs.begin(), Relocs.end(), 221 [](const WasmRelocation &R1, const WasmRelocation &R2) { 222 return R1.Offset < R2.Offset; 223 })); 224 assert(std::is_sorted( 225 Chunks.begin(), Chunks.end(), [](InputChunk *C1, InputChunk *C2) { 226 return C1->getInputSectionOffset() < C2->getInputSectionOffset(); 227 })); 228 229 auto RelocsNext = Relocs.begin(); 230 auto RelocsEnd = Relocs.end(); 231 auto RelocLess = [](const WasmRelocation &R, uint32_t Val) { 232 return R.Offset < Val; 233 }; 234 for (InputChunk *C : Chunks) { 235 auto RelocsStart = std::lower_bound(RelocsNext, RelocsEnd, 236 C->getInputSectionOffset(), RelocLess); 237 RelocsNext = std::lower_bound( 238 RelocsStart, RelocsEnd, C->getInputSectionOffset() + C->getInputSize(), 239 RelocLess); 240 C->setRelocations(ArrayRef<WasmRelocation>(RelocsStart, RelocsNext)); 241 } 242 } 243 244 void ObjFile::parse(bool IgnoreComdats) { 245 // Parse a memory buffer as a wasm file. 246 LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n"); 247 std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this)); 248 249 auto *Obj = dyn_cast<WasmObjectFile>(Bin.get()); 250 if (!Obj) 251 fatal(toString(this) + ": not a wasm file"); 252 if (!Obj->isRelocatableObject()) 253 fatal(toString(this) + ": not a relocatable wasm file"); 254 255 Bin.release(); 256 WasmObj.reset(Obj); 257 258 // Build up a map of function indices to table indices for use when 259 // verifying the existing table index relocations 260 uint32_t TotalFunctions = 261 WasmObj->getNumImportedFunctions() + WasmObj->functions().size(); 262 TableEntries.resize(TotalFunctions); 263 for (const WasmElemSegment &Seg : WasmObj->elements()) { 264 if (Seg.Offset.Opcode != WASM_OPCODE_I32_CONST) 265 fatal(toString(this) + ": invalid table elements"); 266 uint32_t Offset = Seg.Offset.Value.Int32; 267 for (uint32_t Index = 0; Index < Seg.Functions.size(); Index++) { 268 269 uint32_t FunctionIndex = Seg.Functions[Index]; 270 TableEntries[FunctionIndex] = Offset + Index; 271 } 272 } 273 274 uint32_t SectionIndex = 0; 275 276 // Bool for each symbol, true if called directly. This allows us to implement 277 // a weaker form of signature checking where undefined functions that are not 278 // called directly (i.e. only address taken) don't have to match the defined 279 // function's signature. We cannot do this for directly called functions 280 // because those signatures are checked at validation times. 281 // See https://bugs.llvm.org/show_bug.cgi?id=40412 282 std::vector<bool> IsCalledDirectly(WasmObj->getNumberOfSymbols(), false); 283 for (const SectionRef &Sec : WasmObj->sections()) { 284 const WasmSection &Section = WasmObj->getWasmSection(Sec); 285 // Wasm objects can have at most one code and one data section. 286 if (Section.Type == WASM_SEC_CODE) { 287 assert(!CodeSection); 288 CodeSection = &Section; 289 } else if (Section.Type == WASM_SEC_DATA) { 290 assert(!DataSection); 291 DataSection = &Section; 292 } else if (Section.Type == WASM_SEC_CUSTOM) { 293 CustomSections.emplace_back(make<InputSection>(Section, this)); 294 CustomSections.back()->setRelocations(Section.Relocations); 295 CustomSectionsByIndex[SectionIndex] = CustomSections.back(); 296 } 297 SectionIndex++; 298 // Scans relocations to dermine determine if a function symbol is called 299 // directly 300 for (const WasmRelocation &Reloc : Section.Relocations) 301 if (Reloc.Type == R_WASM_FUNCTION_INDEX_LEB) 302 IsCalledDirectly[Reloc.Index] = true; 303 } 304 305 TypeMap.resize(getWasmObj()->types().size()); 306 TypeIsUsed.resize(getWasmObj()->types().size(), false); 307 308 ArrayRef<StringRef> Comdats = WasmObj->linkingData().Comdats; 309 for (StringRef Comdat : Comdats) { 310 bool IsNew = IgnoreComdats || Symtab->addComdat(Comdat); 311 KeptComdats.push_back(IsNew); 312 } 313 314 // Populate `Segments`. 315 for (const WasmSegment &S : WasmObj->dataSegments()) { 316 auto* Seg = make<InputSegment>(S, this); 317 Seg->Discarded = isExcludedByComdat(Seg); 318 Segments.emplace_back(Seg); 319 } 320 setRelocs(Segments, DataSection); 321 322 // Populate `Functions`. 323 ArrayRef<WasmFunction> Funcs = WasmObj->functions(); 324 ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes(); 325 ArrayRef<WasmSignature> Types = WasmObj->types(); 326 Functions.reserve(Funcs.size()); 327 328 for (size_t I = 0, E = Funcs.size(); I != E; ++I) { 329 auto* Func = make<InputFunction>(Types[FuncTypes[I]], &Funcs[I], this); 330 Func->Discarded = isExcludedByComdat(Func); 331 Functions.emplace_back(Func); 332 } 333 setRelocs(Functions, CodeSection); 334 335 // Populate `Globals`. 336 for (const WasmGlobal &G : WasmObj->globals()) 337 Globals.emplace_back(make<InputGlobal>(G, this)); 338 339 // Populate `Events`. 340 for (const WasmEvent &E : WasmObj->events()) 341 Events.emplace_back(make<InputEvent>(Types[E.Type.SigIndex], E, this)); 342 343 // Populate `Symbols` based on the WasmSymbols in the object. 344 Symbols.reserve(WasmObj->getNumberOfSymbols()); 345 for (const SymbolRef &Sym : WasmObj->symbols()) { 346 const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl()); 347 if (WasmSym.isDefined()) { 348 // createDefined may fail if the symbol is comdat excluded in which case 349 // we fall back to creating an undefined symbol 350 if (Symbol *D = createDefined(WasmSym)) { 351 Symbols.push_back(D); 352 continue; 353 } 354 } 355 size_t Idx = Symbols.size(); 356 Symbols.push_back(createUndefined(WasmSym, IsCalledDirectly[Idx])); 357 } 358 } 359 360 bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const { 361 uint32_t C = Chunk->getComdat(); 362 if (C == UINT32_MAX) 363 return false; 364 return !KeptComdats[C]; 365 } 366 367 FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const { 368 return cast<FunctionSymbol>(Symbols[Index]); 369 } 370 371 GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const { 372 return cast<GlobalSymbol>(Symbols[Index]); 373 } 374 375 EventSymbol *ObjFile::getEventSymbol(uint32_t Index) const { 376 return cast<EventSymbol>(Symbols[Index]); 377 } 378 379 SectionSymbol *ObjFile::getSectionSymbol(uint32_t Index) const { 380 return cast<SectionSymbol>(Symbols[Index]); 381 } 382 383 DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const { 384 return cast<DataSymbol>(Symbols[Index]); 385 } 386 387 Symbol *ObjFile::createDefined(const WasmSymbol &Sym) { 388 StringRef Name = Sym.Info.Name; 389 uint32_t Flags = Sym.Info.Flags; 390 391 switch (Sym.Info.Kind) { 392 case WASM_SYMBOL_TYPE_FUNCTION: { 393 InputFunction *Func = 394 Functions[Sym.Info.ElementIndex - WasmObj->getNumImportedFunctions()]; 395 if (Func->Discarded) 396 return nullptr; 397 if (Sym.isBindingLocal()) 398 return make<DefinedFunction>(Name, Flags, this, Func); 399 return Symtab->addDefinedFunction(Name, Flags, this, Func); 400 } 401 case WASM_SYMBOL_TYPE_DATA: { 402 InputSegment *Seg = Segments[Sym.Info.DataRef.Segment]; 403 if (Seg->Discarded) 404 return nullptr; 405 406 uint32_t Offset = Sym.Info.DataRef.Offset; 407 uint32_t Size = Sym.Info.DataRef.Size; 408 409 if (Sym.isBindingLocal()) 410 return make<DefinedData>(Name, Flags, this, Seg, Offset, Size); 411 return Symtab->addDefinedData(Name, Flags, this, Seg, Offset, Size); 412 } 413 case WASM_SYMBOL_TYPE_GLOBAL: { 414 InputGlobal *Global = 415 Globals[Sym.Info.ElementIndex - WasmObj->getNumImportedGlobals()]; 416 if (Sym.isBindingLocal()) 417 return make<DefinedGlobal>(Name, Flags, this, Global); 418 return Symtab->addDefinedGlobal(Name, Flags, this, Global); 419 } 420 case WASM_SYMBOL_TYPE_SECTION: { 421 InputSection *Section = CustomSectionsByIndex[Sym.Info.ElementIndex]; 422 assert(Sym.isBindingLocal()); 423 return make<SectionSymbol>(Flags, Section, this); 424 } 425 case WASM_SYMBOL_TYPE_EVENT: { 426 InputEvent *Event = 427 Events[Sym.Info.ElementIndex - WasmObj->getNumImportedEvents()]; 428 if (Sym.isBindingLocal()) 429 return make<DefinedEvent>(Name, Flags, this, Event); 430 return Symtab->addDefinedEvent(Name, Flags, this, Event); 431 } 432 } 433 llvm_unreachable("unknown symbol kind"); 434 } 435 436 Symbol *ObjFile::createUndefined(const WasmSymbol &Sym, bool IsCalledDirectly) { 437 StringRef Name = Sym.Info.Name; 438 uint32_t Flags = Sym.Info.Flags; 439 440 switch (Sym.Info.Kind) { 441 case WASM_SYMBOL_TYPE_FUNCTION: 442 if (Sym.isBindingLocal()) 443 return make<UndefinedFunction>(Name, Sym.Info.ImportName, 444 Sym.Info.ImportModule, Flags, this, 445 Sym.Signature, IsCalledDirectly); 446 return Symtab->addUndefinedFunction(Name, Sym.Info.ImportName, 447 Sym.Info.ImportModule, Flags, this, 448 Sym.Signature, IsCalledDirectly); 449 case WASM_SYMBOL_TYPE_DATA: 450 if (Sym.isBindingLocal()) 451 return make<UndefinedData>(Name, Flags, this); 452 return Symtab->addUndefinedData(Name, Flags, this); 453 case WASM_SYMBOL_TYPE_GLOBAL: 454 if (Sym.isBindingLocal()) 455 return make<UndefinedGlobal>(Name, Sym.Info.ImportName, 456 Sym.Info.ImportModule, Flags, this, 457 Sym.GlobalType); 458 return Symtab->addUndefinedGlobal(Name, Sym.Info.ImportName, 459 Sym.Info.ImportModule, Flags, this, 460 Sym.GlobalType); 461 case WASM_SYMBOL_TYPE_SECTION: 462 llvm_unreachable("section symbols cannot be undefined"); 463 } 464 llvm_unreachable("unknown symbol kind"); 465 } 466 467 void ArchiveFile::parse() { 468 // Parse a MemoryBufferRef as an archive file. 469 LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n"); 470 File = CHECK(Archive::create(MB), toString(this)); 471 472 // Read the symbol table to construct Lazy symbols. 473 int Count = 0; 474 for (const Archive::Symbol &Sym : File->symbols()) { 475 Symtab->addLazy(this, &Sym); 476 ++Count; 477 } 478 LLVM_DEBUG(dbgs() << "Read " << Count << " symbols\n"); 479 } 480 481 void ArchiveFile::addMember(const Archive::Symbol *Sym) { 482 const Archive::Child &C = 483 CHECK(Sym->getMember(), 484 "could not get the member for symbol " + Sym->getName()); 485 486 // Don't try to load the same member twice (this can happen when members 487 // mutually reference each other). 488 if (!Seen.insert(C.getChildOffset()).second) 489 return; 490 491 LLVM_DEBUG(dbgs() << "loading lazy: " << Sym->getName() << "\n"); 492 LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n"); 493 494 MemoryBufferRef MB = 495 CHECK(C.getMemoryBufferRef(), 496 "could not get the buffer for the member defining symbol " + 497 Sym->getName()); 498 499 InputFile *Obj = createObjectFile(MB, getName()); 500 Symtab->addFile(Obj); 501 } 502 503 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { 504 switch (GvVisibility) { 505 case GlobalValue::DefaultVisibility: 506 return WASM_SYMBOL_VISIBILITY_DEFAULT; 507 case GlobalValue::HiddenVisibility: 508 case GlobalValue::ProtectedVisibility: 509 return WASM_SYMBOL_VISIBILITY_HIDDEN; 510 } 511 llvm_unreachable("unknown visibility"); 512 } 513 514 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats, 515 const lto::InputFile::Symbol &ObjSym, 516 BitcodeFile &F) { 517 StringRef Name = Saver.save(ObjSym.getName()); 518 519 uint32_t Flags = ObjSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0; 520 Flags |= mapVisibility(ObjSym.getVisibility()); 521 522 int C = ObjSym.getComdatIndex(); 523 bool ExcludedByComdat = C != -1 && !KeptComdats[C]; 524 525 if (ObjSym.isUndefined() || ExcludedByComdat) { 526 if (ObjSym.isExecutable()) 527 return Symtab->addUndefinedFunction(Name, Name, DefaultModule, Flags, &F, 528 nullptr, true); 529 return Symtab->addUndefinedData(Name, Flags, &F); 530 } 531 532 if (ObjSym.isExecutable()) 533 return Symtab->addDefinedFunction(Name, Flags, &F, nullptr); 534 return Symtab->addDefinedData(Name, Flags, &F, nullptr, 0, 0); 535 } 536 537 void BitcodeFile::parse() { 538 Obj = check(lto::InputFile::create(MemoryBufferRef( 539 MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier())))); 540 Triple T(Obj->getTargetTriple()); 541 if (T.getArch() != Triple::wasm32) { 542 error(toString(MB.getBufferIdentifier()) + ": machine type must be wasm32"); 543 return; 544 } 545 std::vector<bool> KeptComdats; 546 for (StringRef S : Obj->getComdatTable()) 547 KeptComdats.push_back(Symtab->addComdat(S)); 548 549 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) 550 Symbols.push_back(createBitcodeSymbol(KeptComdats, ObjSym, *this)); 551 } 552 553 // Returns a string in the format of "foo.o" or "foo.a(bar.o)". 554 std::string lld::toString(const wasm::InputFile *File) { 555 if (!File) 556 return "<internal>"; 557 558 if (File->ArchiveName.empty()) 559 return File->getName(); 560 561 return (File->ArchiveName + "(" + File->getName() + ")").str(); 562 } 563