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 llvm; 26 using namespace llvm::object; 27 using namespace llvm::wasm; 28 29 namespace lld { 30 31 // Returns a string in the format of "foo.o" or "foo.a(bar.o)". 32 std::string toString(const wasm::InputFile *file) { 33 if (!file) 34 return "<internal>"; 35 36 if (file->archiveName.empty()) 37 return std::string(file->getName()); 38 39 return (file->archiveName + "(" + file->getName() + ")").str(); 40 } 41 42 namespace wasm { 43 std::unique_ptr<llvm::TarWriter> tar; 44 45 Optional<MemoryBufferRef> readFile(StringRef path) { 46 log("Loading: " + path); 47 48 auto mbOrErr = MemoryBuffer::getFile(path); 49 if (auto ec = mbOrErr.getError()) { 50 error("cannot open " + path + ": " + ec.message()); 51 return None; 52 } 53 std::unique_ptr<MemoryBuffer> &mb = *mbOrErr; 54 MemoryBufferRef mbref = mb->getMemBufferRef(); 55 make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership 56 57 if (tar) 58 tar->append(relativeToRoot(path), mbref.getBuffer()); 59 return mbref; 60 } 61 62 InputFile *createObjectFile(MemoryBufferRef mb, 63 StringRef archiveName) { 64 file_magic magic = identify_magic(mb.getBuffer()); 65 if (magic == file_magic::wasm_object) { 66 std::unique_ptr<Binary> bin = 67 CHECK(createBinary(mb), mb.getBufferIdentifier()); 68 auto *obj = cast<WasmObjectFile>(bin.get()); 69 if (obj->isSharedObject()) 70 return make<SharedFile>(mb); 71 return make<ObjFile>(mb, archiveName); 72 } 73 74 if (magic == file_magic::bitcode) 75 return make<BitcodeFile>(mb, archiveName); 76 77 fatal("unknown file type: " + mb.getBufferIdentifier()); 78 } 79 80 void ObjFile::dumpInfo() const { 81 log("info for: " + toString(this) + 82 "\n Symbols : " + Twine(symbols.size()) + 83 "\n Function Imports : " + Twine(wasmObj->getNumImportedFunctions()) + 84 "\n Global Imports : " + Twine(wasmObj->getNumImportedGlobals()) + 85 "\n Event Imports : " + Twine(wasmObj->getNumImportedEvents())); 86 } 87 88 // Relocations contain either symbol or type indices. This function takes a 89 // relocation and returns relocated index (i.e. translates from the input 90 // symbol/type space to the output symbol/type space). 91 uint32_t ObjFile::calcNewIndex(const WasmRelocation &reloc) const { 92 if (reloc.Type == R_WASM_TYPE_INDEX_LEB) { 93 assert(typeIsUsed[reloc.Index]); 94 return typeMap[reloc.Index]; 95 } 96 const Symbol *sym = symbols[reloc.Index]; 97 if (auto *ss = dyn_cast<SectionSymbol>(sym)) 98 sym = ss->getOutputSectionSymbol(); 99 return sym->getOutputSymbolIndex(); 100 } 101 102 // Relocations can contain addend for combined sections. This function takes a 103 // relocation and returns updated addend by offset in the output section. 104 uint64_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const { 105 switch (reloc.Type) { 106 case R_WASM_MEMORY_ADDR_LEB: 107 case R_WASM_MEMORY_ADDR_LEB64: 108 case R_WASM_MEMORY_ADDR_SLEB64: 109 case R_WASM_MEMORY_ADDR_SLEB: 110 case R_WASM_MEMORY_ADDR_REL_SLEB: 111 case R_WASM_MEMORY_ADDR_REL_SLEB64: 112 case R_WASM_MEMORY_ADDR_I32: 113 case R_WASM_MEMORY_ADDR_I64: 114 case R_WASM_FUNCTION_OFFSET_I32: 115 return reloc.Addend; 116 case R_WASM_SECTION_OFFSET_I32: 117 return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend; 118 default: 119 llvm_unreachable("unexpected relocation type"); 120 } 121 } 122 123 // Calculate the value we expect to find at the relocation location. 124 // This is used as a sanity check before applying a relocation to a given 125 // location. It is useful for catching bugs in the compiler and linker. 126 uint64_t ObjFile::calcExpectedValue(const WasmRelocation &reloc) const { 127 switch (reloc.Type) { 128 case R_WASM_TABLE_INDEX_I32: 129 case R_WASM_TABLE_INDEX_I64: 130 case R_WASM_TABLE_INDEX_SLEB: 131 case R_WASM_TABLE_INDEX_SLEB64: { 132 const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; 133 return tableEntries[sym.Info.ElementIndex]; 134 } 135 case R_WASM_TABLE_INDEX_REL_SLEB: { 136 const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; 137 return tableEntriesRel[sym.Info.ElementIndex]; 138 } 139 case R_WASM_MEMORY_ADDR_LEB: 140 case R_WASM_MEMORY_ADDR_LEB64: 141 case R_WASM_MEMORY_ADDR_SLEB: 142 case R_WASM_MEMORY_ADDR_SLEB64: 143 case R_WASM_MEMORY_ADDR_REL_SLEB: 144 case R_WASM_MEMORY_ADDR_REL_SLEB64: 145 case R_WASM_MEMORY_ADDR_I32: 146 case R_WASM_MEMORY_ADDR_I64: { 147 const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; 148 if (sym.isUndefined()) 149 return 0; 150 const WasmSegment &segment = 151 wasmObj->dataSegments()[sym.Info.DataRef.Segment]; 152 if (segment.Data.Offset.Opcode == WASM_OPCODE_I32_CONST) 153 return segment.Data.Offset.Value.Int32 + sym.Info.DataRef.Offset + 154 reloc.Addend; 155 else if (segment.Data.Offset.Opcode == WASM_OPCODE_I64_CONST) 156 return segment.Data.Offset.Value.Int64 + sym.Info.DataRef.Offset + 157 reloc.Addend; 158 else 159 llvm_unreachable("unknown init expr opcode"); 160 } 161 case R_WASM_FUNCTION_OFFSET_I32: { 162 const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; 163 InputFunction *f = 164 functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()]; 165 return f->getFunctionInputOffset() + f->getFunctionCodeOffset() + 166 reloc.Addend; 167 } 168 case R_WASM_SECTION_OFFSET_I32: 169 return reloc.Addend; 170 case R_WASM_TYPE_INDEX_LEB: 171 return reloc.Index; 172 case R_WASM_FUNCTION_INDEX_LEB: 173 case R_WASM_GLOBAL_INDEX_LEB: 174 case R_WASM_GLOBAL_INDEX_I32: 175 case R_WASM_EVENT_INDEX_LEB: { 176 const WasmSymbol &sym = wasmObj->syms()[reloc.Index]; 177 return sym.Info.ElementIndex; 178 } 179 default: 180 llvm_unreachable("unknown relocation type"); 181 } 182 } 183 184 // Translate from the relocation's index into the final linked output value. 185 uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc) const { 186 const Symbol* sym = nullptr; 187 if (reloc.Type != R_WASM_TYPE_INDEX_LEB) { 188 sym = symbols[reloc.Index]; 189 190 // We can end up with relocations against non-live symbols. For example 191 // in debug sections. We return reloc.Addend because always returning zero 192 // causes the generation of spurious range-list terminators in the 193 // .debug_ranges section. 194 if ((isa<FunctionSymbol>(sym) || isa<DataSymbol>(sym)) && !sym->isLive()) 195 return reloc.Addend; 196 } 197 198 switch (reloc.Type) { 199 case R_WASM_TABLE_INDEX_I32: 200 case R_WASM_TABLE_INDEX_I64: 201 case R_WASM_TABLE_INDEX_SLEB: 202 case R_WASM_TABLE_INDEX_SLEB64: 203 case R_WASM_TABLE_INDEX_REL_SLEB: { 204 if (!getFunctionSymbol(reloc.Index)->hasTableIndex()) 205 return 0; 206 uint32_t index = getFunctionSymbol(reloc.Index)->getTableIndex(); 207 if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB) 208 index -= config->tableBase; 209 return index; 210 211 } 212 case R_WASM_MEMORY_ADDR_LEB: 213 case R_WASM_MEMORY_ADDR_LEB64: 214 case R_WASM_MEMORY_ADDR_SLEB: 215 case R_WASM_MEMORY_ADDR_SLEB64: 216 case R_WASM_MEMORY_ADDR_REL_SLEB: 217 case R_WASM_MEMORY_ADDR_REL_SLEB64: 218 case R_WASM_MEMORY_ADDR_I32: 219 case R_WASM_MEMORY_ADDR_I64: 220 if (isa<UndefinedData>(sym) || sym->isUndefWeak()) 221 return 0; 222 return cast<DefinedData>(sym)->getVirtualAddress() + reloc.Addend; 223 case R_WASM_TYPE_INDEX_LEB: 224 return typeMap[reloc.Index]; 225 case R_WASM_FUNCTION_INDEX_LEB: 226 return getFunctionSymbol(reloc.Index)->getFunctionIndex(); 227 case R_WASM_GLOBAL_INDEX_LEB: 228 case R_WASM_GLOBAL_INDEX_I32: 229 if (auto gs = dyn_cast<GlobalSymbol>(sym)) 230 return gs->getGlobalIndex(); 231 return sym->getGOTIndex(); 232 case R_WASM_EVENT_INDEX_LEB: 233 return getEventSymbol(reloc.Index)->getEventIndex(); 234 case R_WASM_FUNCTION_OFFSET_I32: { 235 auto *f = cast<DefinedFunction>(sym); 236 return f->function->outputOffset + 237 (f->function->getFunctionCodeOffset() + reloc.Addend); 238 } 239 case R_WASM_SECTION_OFFSET_I32: 240 return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend; 241 default: 242 llvm_unreachable("unknown relocation type"); 243 } 244 } 245 246 template <class T> 247 static void setRelocs(const std::vector<T *> &chunks, 248 const WasmSection *section) { 249 if (!section) 250 return; 251 252 ArrayRef<WasmRelocation> relocs = section->Relocations; 253 assert(llvm::is_sorted( 254 relocs, [](const WasmRelocation &r1, const WasmRelocation &r2) { 255 return r1.Offset < r2.Offset; 256 })); 257 assert(llvm::is_sorted(chunks, [](InputChunk *c1, InputChunk *c2) { 258 return c1->getInputSectionOffset() < c2->getInputSectionOffset(); 259 })); 260 261 auto relocsNext = relocs.begin(); 262 auto relocsEnd = relocs.end(); 263 auto relocLess = [](const WasmRelocation &r, uint32_t val) { 264 return r.Offset < val; 265 }; 266 for (InputChunk *c : chunks) { 267 auto relocsStart = std::lower_bound(relocsNext, relocsEnd, 268 c->getInputSectionOffset(), relocLess); 269 relocsNext = std::lower_bound( 270 relocsStart, relocsEnd, c->getInputSectionOffset() + c->getInputSize(), 271 relocLess); 272 c->setRelocations(ArrayRef<WasmRelocation>(relocsStart, relocsNext)); 273 } 274 } 275 276 void ObjFile::parse(bool ignoreComdats) { 277 // Parse a memory buffer as a wasm file. 278 LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n"); 279 std::unique_ptr<Binary> bin = CHECK(createBinary(mb), toString(this)); 280 281 auto *obj = dyn_cast<WasmObjectFile>(bin.get()); 282 if (!obj) 283 fatal(toString(this) + ": not a wasm file"); 284 if (!obj->isRelocatableObject()) 285 fatal(toString(this) + ": not a relocatable wasm file"); 286 287 bin.release(); 288 wasmObj.reset(obj); 289 290 // Build up a map of function indices to table indices for use when 291 // verifying the existing table index relocations 292 uint32_t totalFunctions = 293 wasmObj->getNumImportedFunctions() + wasmObj->functions().size(); 294 tableEntriesRel.resize(totalFunctions); 295 tableEntries.resize(totalFunctions); 296 for (const WasmElemSegment &seg : wasmObj->elements()) { 297 int64_t offset; 298 if (seg.Offset.Opcode == WASM_OPCODE_I32_CONST) 299 offset = seg.Offset.Value.Int32; 300 else if (seg.Offset.Opcode == WASM_OPCODE_I64_CONST) 301 offset = seg.Offset.Value.Int64; 302 else 303 fatal(toString(this) + ": invalid table elements"); 304 for (size_t index = 0; index < seg.Functions.size(); index++) { 305 auto functionIndex = seg.Functions[index]; 306 tableEntriesRel[functionIndex] = index; 307 tableEntries[functionIndex] = offset + index; 308 } 309 } 310 311 uint32_t sectionIndex = 0; 312 313 // Bool for each symbol, true if called directly. This allows us to implement 314 // a weaker form of signature checking where undefined functions that are not 315 // called directly (i.e. only address taken) don't have to match the defined 316 // function's signature. We cannot do this for directly called functions 317 // because those signatures are checked at validation times. 318 // See https://bugs.llvm.org/show_bug.cgi?id=40412 319 std::vector<bool> isCalledDirectly(wasmObj->getNumberOfSymbols(), false); 320 for (const SectionRef &sec : wasmObj->sections()) { 321 const WasmSection §ion = wasmObj->getWasmSection(sec); 322 // Wasm objects can have at most one code and one data section. 323 if (section.Type == WASM_SEC_CODE) { 324 assert(!codeSection); 325 codeSection = §ion; 326 } else if (section.Type == WASM_SEC_DATA) { 327 assert(!dataSection); 328 dataSection = §ion; 329 } else if (section.Type == WASM_SEC_CUSTOM) { 330 customSections.emplace_back(make<InputSection>(section, this)); 331 customSections.back()->setRelocations(section.Relocations); 332 customSectionsByIndex[sectionIndex] = customSections.back(); 333 } 334 sectionIndex++; 335 // Scans relocations to determine if a function symbol is called directly. 336 for (const WasmRelocation &reloc : section.Relocations) 337 if (reloc.Type == R_WASM_FUNCTION_INDEX_LEB) 338 isCalledDirectly[reloc.Index] = true; 339 } 340 341 typeMap.resize(getWasmObj()->types().size()); 342 typeIsUsed.resize(getWasmObj()->types().size(), false); 343 344 ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats; 345 for (StringRef comdat : comdats) { 346 bool isNew = ignoreComdats || symtab->addComdat(comdat); 347 keptComdats.push_back(isNew); 348 } 349 350 // Populate `Segments`. 351 for (const WasmSegment &s : wasmObj->dataSegments()) { 352 auto* seg = make<InputSegment>(s, this); 353 seg->discarded = isExcludedByComdat(seg); 354 segments.emplace_back(seg); 355 } 356 setRelocs(segments, dataSection); 357 358 // Populate `Functions`. 359 ArrayRef<WasmFunction> funcs = wasmObj->functions(); 360 ArrayRef<uint32_t> funcTypes = wasmObj->functionTypes(); 361 ArrayRef<WasmSignature> types = wasmObj->types(); 362 functions.reserve(funcs.size()); 363 364 for (size_t i = 0, e = funcs.size(); i != e; ++i) { 365 auto* func = make<InputFunction>(types[funcTypes[i]], &funcs[i], this); 366 func->discarded = isExcludedByComdat(func); 367 functions.emplace_back(func); 368 } 369 setRelocs(functions, codeSection); 370 371 // Populate `Globals`. 372 for (const WasmGlobal &g : wasmObj->globals()) 373 globals.emplace_back(make<InputGlobal>(g, this)); 374 375 // Populate `Events`. 376 for (const WasmEvent &e : wasmObj->events()) 377 events.emplace_back(make<InputEvent>(types[e.Type.SigIndex], e, this)); 378 379 // Populate `Symbols` based on the symbols in the object. 380 symbols.reserve(wasmObj->getNumberOfSymbols()); 381 for (const SymbolRef &sym : wasmObj->symbols()) { 382 const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl()); 383 if (wasmSym.isDefined()) { 384 // createDefined may fail if the symbol is comdat excluded in which case 385 // we fall back to creating an undefined symbol 386 if (Symbol *d = createDefined(wasmSym)) { 387 symbols.push_back(d); 388 continue; 389 } 390 } 391 size_t idx = symbols.size(); 392 symbols.push_back(createUndefined(wasmSym, isCalledDirectly[idx])); 393 } 394 } 395 396 bool ObjFile::isExcludedByComdat(InputChunk *chunk) const { 397 uint32_t c = chunk->getComdat(); 398 if (c == UINT32_MAX) 399 return false; 400 return !keptComdats[c]; 401 } 402 403 FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t index) const { 404 return cast<FunctionSymbol>(symbols[index]); 405 } 406 407 GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t index) const { 408 return cast<GlobalSymbol>(symbols[index]); 409 } 410 411 EventSymbol *ObjFile::getEventSymbol(uint32_t index) const { 412 return cast<EventSymbol>(symbols[index]); 413 } 414 415 SectionSymbol *ObjFile::getSectionSymbol(uint32_t index) const { 416 return cast<SectionSymbol>(symbols[index]); 417 } 418 419 DataSymbol *ObjFile::getDataSymbol(uint32_t index) const { 420 return cast<DataSymbol>(symbols[index]); 421 } 422 423 Symbol *ObjFile::createDefined(const WasmSymbol &sym) { 424 StringRef name = sym.Info.Name; 425 uint32_t flags = sym.Info.Flags; 426 427 switch (sym.Info.Kind) { 428 case WASM_SYMBOL_TYPE_FUNCTION: { 429 InputFunction *func = 430 functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()]; 431 if (sym.isBindingLocal()) 432 return make<DefinedFunction>(name, flags, this, func); 433 if (func->discarded) 434 return nullptr; 435 return symtab->addDefinedFunction(name, flags, this, func); 436 } 437 case WASM_SYMBOL_TYPE_DATA: { 438 InputSegment *seg = segments[sym.Info.DataRef.Segment]; 439 auto offset = sym.Info.DataRef.Offset; 440 auto size = sym.Info.DataRef.Size; 441 if (sym.isBindingLocal()) 442 return make<DefinedData>(name, flags, this, seg, offset, size); 443 if (seg->discarded) 444 return nullptr; 445 return symtab->addDefinedData(name, flags, this, seg, offset, size); 446 } 447 case WASM_SYMBOL_TYPE_GLOBAL: { 448 InputGlobal *global = 449 globals[sym.Info.ElementIndex - wasmObj->getNumImportedGlobals()]; 450 if (sym.isBindingLocal()) 451 return make<DefinedGlobal>(name, flags, this, global); 452 return symtab->addDefinedGlobal(name, flags, this, global); 453 } 454 case WASM_SYMBOL_TYPE_SECTION: { 455 InputSection *section = customSectionsByIndex[sym.Info.ElementIndex]; 456 assert(sym.isBindingLocal()); 457 return make<SectionSymbol>(flags, section, this); 458 } 459 case WASM_SYMBOL_TYPE_EVENT: { 460 InputEvent *event = 461 events[sym.Info.ElementIndex - wasmObj->getNumImportedEvents()]; 462 if (sym.isBindingLocal()) 463 return make<DefinedEvent>(name, flags, this, event); 464 return symtab->addDefinedEvent(name, flags, this, event); 465 } 466 } 467 llvm_unreachable("unknown symbol kind"); 468 } 469 470 Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) { 471 StringRef name = sym.Info.Name; 472 uint32_t flags = sym.Info.Flags | WASM_SYMBOL_UNDEFINED; 473 474 switch (sym.Info.Kind) { 475 case WASM_SYMBOL_TYPE_FUNCTION: 476 if (sym.isBindingLocal()) 477 return make<UndefinedFunction>(name, sym.Info.ImportName, 478 sym.Info.ImportModule, flags, this, 479 sym.Signature, isCalledDirectly); 480 return symtab->addUndefinedFunction(name, sym.Info.ImportName, 481 sym.Info.ImportModule, flags, this, 482 sym.Signature, isCalledDirectly); 483 case WASM_SYMBOL_TYPE_DATA: 484 if (sym.isBindingLocal()) 485 return make<UndefinedData>(name, flags, this); 486 return symtab->addUndefinedData(name, flags, this); 487 case WASM_SYMBOL_TYPE_GLOBAL: 488 if (sym.isBindingLocal()) 489 return make<UndefinedGlobal>(name, sym.Info.ImportName, 490 sym.Info.ImportModule, flags, this, 491 sym.GlobalType); 492 return symtab->addUndefinedGlobal(name, sym.Info.ImportName, 493 sym.Info.ImportModule, flags, this, 494 sym.GlobalType); 495 case WASM_SYMBOL_TYPE_SECTION: 496 llvm_unreachable("section symbols cannot be undefined"); 497 } 498 llvm_unreachable("unknown symbol kind"); 499 } 500 501 void ArchiveFile::parse() { 502 // Parse a MemoryBufferRef as an archive file. 503 LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n"); 504 file = CHECK(Archive::create(mb), toString(this)); 505 506 // Read the symbol table to construct Lazy symbols. 507 int count = 0; 508 for (const Archive::Symbol &sym : file->symbols()) { 509 symtab->addLazy(this, &sym); 510 ++count; 511 } 512 LLVM_DEBUG(dbgs() << "Read " << count << " symbols\n"); 513 } 514 515 void ArchiveFile::addMember(const Archive::Symbol *sym) { 516 const Archive::Child &c = 517 CHECK(sym->getMember(), 518 "could not get the member for symbol " + sym->getName()); 519 520 // Don't try to load the same member twice (this can happen when members 521 // mutually reference each other). 522 if (!seen.insert(c.getChildOffset()).second) 523 return; 524 525 LLVM_DEBUG(dbgs() << "loading lazy: " << sym->getName() << "\n"); 526 LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n"); 527 528 MemoryBufferRef mb = 529 CHECK(c.getMemoryBufferRef(), 530 "could not get the buffer for the member defining symbol " + 531 sym->getName()); 532 533 InputFile *obj = createObjectFile(mb, getName()); 534 symtab->addFile(obj); 535 } 536 537 static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) { 538 switch (gvVisibility) { 539 case GlobalValue::DefaultVisibility: 540 return WASM_SYMBOL_VISIBILITY_DEFAULT; 541 case GlobalValue::HiddenVisibility: 542 case GlobalValue::ProtectedVisibility: 543 return WASM_SYMBOL_VISIBILITY_HIDDEN; 544 } 545 llvm_unreachable("unknown visibility"); 546 } 547 548 static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats, 549 const lto::InputFile::Symbol &objSym, 550 BitcodeFile &f) { 551 StringRef name = saver.save(objSym.getName()); 552 553 uint32_t flags = objSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0; 554 flags |= mapVisibility(objSym.getVisibility()); 555 556 int c = objSym.getComdatIndex(); 557 bool excludedByComdat = c != -1 && !keptComdats[c]; 558 559 if (objSym.isUndefined() || excludedByComdat) { 560 flags |= WASM_SYMBOL_UNDEFINED; 561 if (objSym.isExecutable()) 562 return symtab->addUndefinedFunction(name, None, None, flags, &f, nullptr, 563 true); 564 return symtab->addUndefinedData(name, flags, &f); 565 } 566 567 if (objSym.isExecutable()) 568 return symtab->addDefinedFunction(name, flags, &f, nullptr); 569 return symtab->addDefinedData(name, flags, &f, nullptr, 0, 0); 570 } 571 572 bool BitcodeFile::doneLTO = false; 573 574 void BitcodeFile::parse() { 575 if (doneLTO) { 576 error(toString(this) + ": attempt to add bitcode file after LTO."); 577 return; 578 } 579 580 obj = check(lto::InputFile::create(MemoryBufferRef( 581 mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier())))); 582 Triple t(obj->getTargetTriple()); 583 if (!t.isWasm()) { 584 error(toString(this) + ": machine type must be wasm32 or wasm64"); 585 return; 586 } 587 bool is64 = t.getArch() == Triple::wasm64; 588 if (config->is64.hasValue() && *config->is64 != is64) { 589 error(toString(this) + ": machine type for all bitcode files must match"); 590 return; 591 } 592 config->is64 = is64; 593 std::vector<bool> keptComdats; 594 for (StringRef s : obj->getComdatTable()) 595 keptComdats.push_back(symtab->addComdat(s)); 596 597 for (const lto::InputFile::Symbol &objSym : obj->symbols()) 598 symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this)); 599 } 600 601 } // namespace wasm 602 } // namespace lld 603