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 "InputElement.h" 13 #include "OutputSegment.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 44 void InputFile::checkArch(Triple::ArchType arch) const { 45 bool is64 = arch == Triple::wasm64; 46 if (is64 && !config->is64.hasValue()) { 47 fatal(toString(this) + 48 ": must specify -mwasm64 to process wasm64 object files"); 49 } else if (config->is64.getValueOr(false) != is64) { 50 fatal(toString(this) + 51 ": wasm32 object file can't be linked in wasm64 mode"); 52 } 53 } 54 55 std::unique_ptr<llvm::TarWriter> tar; 56 57 Optional<MemoryBufferRef> readFile(StringRef path) { 58 log("Loading: " + path); 59 60 auto mbOrErr = MemoryBuffer::getFile(path); 61 if (auto ec = mbOrErr.getError()) { 62 error("cannot open " + path + ": " + ec.message()); 63 return None; 64 } 65 std::unique_ptr<MemoryBuffer> &mb = *mbOrErr; 66 MemoryBufferRef mbref = mb->getMemBufferRef(); 67 make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership 68 69 if (tar) 70 tar->append(relativeToRoot(path), mbref.getBuffer()); 71 return mbref; 72 } 73 74 InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName) { 75 file_magic magic = identify_magic(mb.getBuffer()); 76 if (magic == file_magic::wasm_object) { 77 std::unique_ptr<Binary> bin = 78 CHECK(createBinary(mb), mb.getBufferIdentifier()); 79 auto *obj = cast<WasmObjectFile>(bin.get()); 80 if (obj->isSharedObject()) 81 return make<SharedFile>(mb); 82 return make<ObjFile>(mb, archiveName); 83 } 84 85 if (magic == file_magic::bitcode) 86 return make<BitcodeFile>(mb, archiveName); 87 88 fatal("unknown file type: " + mb.getBufferIdentifier()); 89 } 90 91 void ObjFile::dumpInfo() const { 92 log("info for: " + toString(this) + 93 "\n Symbols : " + Twine(symbols.size()) + 94 "\n Function Imports : " + Twine(wasmObj->getNumImportedFunctions()) + 95 "\n Global Imports : " + Twine(wasmObj->getNumImportedGlobals()) + 96 "\n Event Imports : " + Twine(wasmObj->getNumImportedEvents()) + 97 "\n Table Imports : " + Twine(wasmObj->getNumImportedTables())); 98 } 99 100 // Relocations contain either symbol or type indices. This function takes a 101 // relocation and returns relocated index (i.e. translates from the input 102 // symbol/type space to the output symbol/type space). 103 uint32_t ObjFile::calcNewIndex(const WasmRelocation &reloc) const { 104 if (reloc.Type == R_WASM_TYPE_INDEX_LEB) { 105 assert(typeIsUsed[reloc.Index]); 106 return typeMap[reloc.Index]; 107 } 108 const Symbol *sym = symbols[reloc.Index]; 109 if (auto *ss = dyn_cast<SectionSymbol>(sym)) 110 sym = ss->getOutputSectionSymbol(); 111 return sym->getOutputSymbolIndex(); 112 } 113 114 // Relocations can contain addend for combined sections. This function takes a 115 // relocation and returns updated addend by offset in the output section. 116 uint64_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const { 117 switch (reloc.Type) { 118 case R_WASM_MEMORY_ADDR_LEB: 119 case R_WASM_MEMORY_ADDR_LEB64: 120 case R_WASM_MEMORY_ADDR_SLEB64: 121 case R_WASM_MEMORY_ADDR_SLEB: 122 case R_WASM_MEMORY_ADDR_REL_SLEB: 123 case R_WASM_MEMORY_ADDR_REL_SLEB64: 124 case R_WASM_MEMORY_ADDR_I32: 125 case R_WASM_MEMORY_ADDR_I64: 126 case R_WASM_MEMORY_ADDR_TLS_SLEB: 127 case R_WASM_FUNCTION_OFFSET_I32: 128 case R_WASM_FUNCTION_OFFSET_I64: 129 case R_WASM_MEMORY_ADDR_LOCREL_I32: 130 return reloc.Addend; 131 case R_WASM_SECTION_OFFSET_I32: 132 return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend); 133 default: 134 llvm_unreachable("unexpected relocation type"); 135 } 136 } 137 138 // Translate from the relocation's index into the final linked output value. 139 uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, uint64_t tombstone, 140 const InputChunk *chunk) const { 141 const Symbol* sym = nullptr; 142 if (reloc.Type != R_WASM_TYPE_INDEX_LEB) { 143 sym = symbols[reloc.Index]; 144 145 // We can end up with relocations against non-live symbols. For example 146 // in debug sections. We return a tombstone value in debug symbol sections 147 // so this will not produce a valid range conflicting with ranges of actual 148 // code. In other sections we return reloc.Addend. 149 150 if (!isa<SectionSymbol>(sym) && !sym->isLive()) 151 return tombstone ? tombstone : reloc.Addend; 152 } 153 154 switch (reloc.Type) { 155 case R_WASM_TABLE_INDEX_I32: 156 case R_WASM_TABLE_INDEX_I64: 157 case R_WASM_TABLE_INDEX_SLEB: 158 case R_WASM_TABLE_INDEX_SLEB64: 159 case R_WASM_TABLE_INDEX_REL_SLEB: 160 case R_WASM_TABLE_INDEX_REL_SLEB64: { 161 if (!getFunctionSymbol(reloc.Index)->hasTableIndex()) 162 return 0; 163 uint32_t index = getFunctionSymbol(reloc.Index)->getTableIndex(); 164 if (reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB || 165 reloc.Type == R_WASM_TABLE_INDEX_REL_SLEB64) 166 index -= config->tableBase; 167 return index; 168 } 169 case R_WASM_MEMORY_ADDR_LEB: 170 case R_WASM_MEMORY_ADDR_LEB64: 171 case R_WASM_MEMORY_ADDR_SLEB: 172 case R_WASM_MEMORY_ADDR_SLEB64: 173 case R_WASM_MEMORY_ADDR_REL_SLEB: 174 case R_WASM_MEMORY_ADDR_REL_SLEB64: 175 case R_WASM_MEMORY_ADDR_I32: 176 case R_WASM_MEMORY_ADDR_I64: 177 case R_WASM_MEMORY_ADDR_LOCREL_I32: { 178 if (isa<UndefinedData>(sym) || sym->isUndefWeak()) 179 return 0; 180 auto D = cast<DefinedData>(sym); 181 // Treat non-TLS relocation against symbols that live in the TLS segment 182 // like TLS relocations. This beaviour exists to support older object 183 // files created before we introduced TLS relocations. 184 // TODO(sbc): Remove this legacy behaviour one day. This will break 185 // backward compat with old object files built with `-fPIC`. 186 if (D->segment && D->segment->outputSeg->isTLS()) 187 return D->getOutputSegmentOffset() + reloc.Addend; 188 189 uint64_t value = D->getVA() + reloc.Addend; 190 if (reloc.Type == R_WASM_MEMORY_ADDR_LOCREL_I32) { 191 const auto *segment = cast<InputSegment>(chunk); 192 uint64_t p = segment->outputSeg->startVA + segment->outputSegmentOffset + 193 reloc.Offset - segment->getInputSectionOffset(); 194 value -= p; 195 } 196 return value; 197 } 198 case R_WASM_MEMORY_ADDR_TLS_SLEB: 199 if (isa<UndefinedData>(sym) || sym->isUndefWeak()) 200 return 0; 201 // TLS relocations are relative to the start of the TLS output segment 202 return cast<DefinedData>(sym)->getOutputSegmentOffset() + reloc.Addend; 203 case R_WASM_TYPE_INDEX_LEB: 204 return typeMap[reloc.Index]; 205 case R_WASM_FUNCTION_INDEX_LEB: 206 return getFunctionSymbol(reloc.Index)->getFunctionIndex(); 207 case R_WASM_GLOBAL_INDEX_LEB: 208 case R_WASM_GLOBAL_INDEX_I32: 209 if (auto gs = dyn_cast<GlobalSymbol>(sym)) 210 return gs->getGlobalIndex(); 211 return sym->getGOTIndex(); 212 case R_WASM_EVENT_INDEX_LEB: 213 return getEventSymbol(reloc.Index)->getEventIndex(); 214 case R_WASM_FUNCTION_OFFSET_I32: 215 case R_WASM_FUNCTION_OFFSET_I64: { 216 auto *f = cast<DefinedFunction>(sym); 217 return f->function->getOffset(f->function->getFunctionCodeOffset() + 218 reloc.Addend); 219 } 220 case R_WASM_SECTION_OFFSET_I32: 221 return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend); 222 case R_WASM_TABLE_NUMBER_LEB: 223 return getTableSymbol(reloc.Index)->getTableNumber(); 224 default: 225 llvm_unreachable("unknown relocation type"); 226 } 227 } 228 229 template <class T> 230 static void setRelocs(const std::vector<T *> &chunks, 231 const WasmSection *section) { 232 if (!section) 233 return; 234 235 ArrayRef<WasmRelocation> relocs = section->Relocations; 236 assert(llvm::is_sorted( 237 relocs, [](const WasmRelocation &r1, const WasmRelocation &r2) { 238 return r1.Offset < r2.Offset; 239 })); 240 assert(llvm::is_sorted(chunks, [](InputChunk *c1, InputChunk *c2) { 241 return c1->getInputSectionOffset() < c2->getInputSectionOffset(); 242 })); 243 244 auto relocsNext = relocs.begin(); 245 auto relocsEnd = relocs.end(); 246 auto relocLess = [](const WasmRelocation &r, uint32_t val) { 247 return r.Offset < val; 248 }; 249 for (InputChunk *c : chunks) { 250 auto relocsStart = std::lower_bound(relocsNext, relocsEnd, 251 c->getInputSectionOffset(), relocLess); 252 relocsNext = std::lower_bound( 253 relocsStart, relocsEnd, c->getInputSectionOffset() + c->getInputSize(), 254 relocLess); 255 c->setRelocations(ArrayRef<WasmRelocation>(relocsStart, relocsNext)); 256 } 257 } 258 259 // An object file can have two approaches to tables. With the reference-types 260 // feature enabled, input files that define or use tables declare the tables 261 // using symbols, and record each use with a relocation. This way when the 262 // linker combines inputs, it can collate the tables used by the inputs, 263 // assigning them distinct table numbers, and renumber all the uses as 264 // appropriate. At the same time, the linker has special logic to build the 265 // indirect function table if it is needed. 266 // 267 // However, MVP object files (those that target WebAssembly 1.0, the "minimum 268 // viable product" version of WebAssembly) neither write table symbols nor 269 // record relocations. These files can have at most one table, the indirect 270 // function table used by call_indirect and which is the address space for 271 // function pointers. If this table is present, it is always an import. If we 272 // have a file with a table import but no table symbols, it is an MVP object 273 // file. synthesizeMVPIndirectFunctionTableSymbolIfNeeded serves as a shim when 274 // loading these input files, defining the missing symbol to allow the indirect 275 // function table to be built. 276 // 277 // As indirect function table table usage in MVP objects cannot be relocated, 278 // the linker must ensure that this table gets assigned index zero. 279 void ObjFile::addLegacyIndirectFunctionTableIfNeeded( 280 uint32_t tableSymbolCount) { 281 uint32_t tableCount = wasmObj->getNumImportedTables() + tables.size(); 282 283 // If there are symbols for all tables, then all is good. 284 if (tableCount == tableSymbolCount) 285 return; 286 287 // It's possible for an input to define tables and also use the indirect 288 // function table, but forget to compile with -mattr=+reference-types. 289 // For these newer files, we require symbols for all tables, and 290 // relocations for all of their uses. 291 if (tableSymbolCount != 0) { 292 error(toString(this) + 293 ": expected one symbol table entry for each of the " + 294 Twine(tableCount) + " table(s) present, but got " + 295 Twine(tableSymbolCount) + " symbol(s) instead."); 296 return; 297 } 298 299 // An MVP object file can have up to one table import, for the indirect 300 // function table, but will have no table definitions. 301 if (tables.size()) { 302 error(toString(this) + 303 ": unexpected table definition(s) without corresponding " 304 "symbol-table entries."); 305 return; 306 } 307 308 // An MVP object file can have only one table import. 309 if (tableCount != 1) { 310 error(toString(this) + 311 ": multiple table imports, but no corresponding symbol-table " 312 "entries."); 313 return; 314 } 315 316 const WasmImport *tableImport = nullptr; 317 for (const auto &import : wasmObj->imports()) { 318 if (import.Kind == WASM_EXTERNAL_TABLE) { 319 assert(!tableImport); 320 tableImport = &import; 321 } 322 } 323 assert(tableImport); 324 325 // We can only synthesize a symtab entry for the indirect function table; if 326 // it has an unexpected name or type, assume that it's not actually the 327 // indirect function table. 328 if (tableImport->Field != functionTableName || 329 tableImport->Table.ElemType != uint8_t(ValType::FUNCREF)) { 330 error(toString(this) + ": table import " + Twine(tableImport->Field) + 331 " is missing a symbol table entry."); 332 return; 333 } 334 335 auto *info = make<WasmSymbolInfo>(); 336 info->Name = tableImport->Field; 337 info->Kind = WASM_SYMBOL_TYPE_TABLE; 338 info->ImportModule = tableImport->Module; 339 info->ImportName = tableImport->Field; 340 info->Flags = WASM_SYMBOL_UNDEFINED; 341 info->Flags |= WASM_SYMBOL_NO_STRIP; 342 info->ElementIndex = 0; 343 LLVM_DEBUG(dbgs() << "Synthesizing symbol for table import: " << info->Name 344 << "\n"); 345 const WasmGlobalType *globalType = nullptr; 346 const WasmEventType *eventType = nullptr; 347 const WasmSignature *signature = nullptr; 348 auto *wasmSym = make<WasmSymbol>(*info, globalType, &tableImport->Table, 349 eventType, signature); 350 Symbol *sym = createUndefined(*wasmSym, false); 351 // We're only sure it's a TableSymbol if the createUndefined succeeded. 352 if (errorCount()) 353 return; 354 symbols.push_back(sym); 355 // Because there are no TABLE_NUMBER relocs, we can't compute accurate 356 // liveness info; instead, just mark the symbol as always live. 357 sym->markLive(); 358 359 // We assume that this compilation unit has unrelocatable references to 360 // this table. 361 config->legacyFunctionTable = true; 362 } 363 364 static bool shouldMerge(const WasmSection &sec) { 365 if (config->optimize == 0) 366 return false; 367 // Sadly we don't have section attributes yet for custom sections, so we 368 // currently go by the name alone. 369 // TODO(sbc): Add ability for wasm sections to carry flags so we don't 370 // need to use names here. 371 // For now, keep in sync with uses of wasm::WASM_SEG_FLAG_STRINGS in 372 // MCObjectFileInfo::initWasmMCObjectFileInfo which creates these custom 373 // sections. 374 return sec.Name == ".debug_str" || sec.Name == ".debug_str.dwo" || 375 sec.Name == ".debug_line_str"; 376 } 377 378 static bool shouldMerge(const WasmSegment &seg) { 379 // As of now we only support merging strings, and only with single byte 380 // alignment (2^0). 381 if (!(seg.Data.LinkingFlags & WASM_SEG_FLAG_STRINGS) || 382 (seg.Data.Alignment != 0)) 383 return false; 384 385 // On a regular link we don't merge sections if -O0 (default is -O1). This 386 // sometimes makes the linker significantly faster, although the output will 387 // be bigger. 388 if (config->optimize == 0) 389 return false; 390 391 // A mergeable section with size 0 is useless because they don't have 392 // any data to merge. A mergeable string section with size 0 can be 393 // argued as invalid because it doesn't end with a null character. 394 // We'll avoid a mess by handling them as if they were non-mergeable. 395 if (seg.Data.Content.size() == 0) 396 return false; 397 398 return true; 399 } 400 401 void ObjFile::parse(bool ignoreComdats) { 402 // Parse a memory buffer as a wasm file. 403 LLVM_DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n"); 404 std::unique_ptr<Binary> bin = CHECK(createBinary(mb), toString(this)); 405 406 auto *obj = dyn_cast<WasmObjectFile>(bin.get()); 407 if (!obj) 408 fatal(toString(this) + ": not a wasm file"); 409 if (!obj->isRelocatableObject()) 410 fatal(toString(this) + ": not a relocatable wasm file"); 411 412 bin.release(); 413 wasmObj.reset(obj); 414 415 checkArch(obj->getArch()); 416 417 // Build up a map of function indices to table indices for use when 418 // verifying the existing table index relocations 419 uint32_t totalFunctions = 420 wasmObj->getNumImportedFunctions() + wasmObj->functions().size(); 421 tableEntriesRel.resize(totalFunctions); 422 tableEntries.resize(totalFunctions); 423 for (const WasmElemSegment &seg : wasmObj->elements()) { 424 int64_t offset; 425 if (seg.Offset.Opcode == WASM_OPCODE_I32_CONST) 426 offset = seg.Offset.Value.Int32; 427 else if (seg.Offset.Opcode == WASM_OPCODE_I64_CONST) 428 offset = seg.Offset.Value.Int64; 429 else 430 fatal(toString(this) + ": invalid table elements"); 431 for (size_t index = 0; index < seg.Functions.size(); index++) { 432 auto functionIndex = seg.Functions[index]; 433 tableEntriesRel[functionIndex] = index; 434 tableEntries[functionIndex] = offset + index; 435 } 436 } 437 438 ArrayRef<StringRef> comdats = wasmObj->linkingData().Comdats; 439 for (StringRef comdat : comdats) { 440 bool isNew = ignoreComdats || symtab->addComdat(comdat); 441 keptComdats.push_back(isNew); 442 } 443 444 uint32_t sectionIndex = 0; 445 446 // Bool for each symbol, true if called directly. This allows us to implement 447 // a weaker form of signature checking where undefined functions that are not 448 // called directly (i.e. only address taken) don't have to match the defined 449 // function's signature. We cannot do this for directly called functions 450 // because those signatures are checked at validation times. 451 // See https://bugs.llvm.org/show_bug.cgi?id=40412 452 std::vector<bool> isCalledDirectly(wasmObj->getNumberOfSymbols(), false); 453 for (const SectionRef &sec : wasmObj->sections()) { 454 const WasmSection §ion = wasmObj->getWasmSection(sec); 455 // Wasm objects can have at most one code and one data section. 456 if (section.Type == WASM_SEC_CODE) { 457 assert(!codeSection); 458 codeSection = §ion; 459 } else if (section.Type == WASM_SEC_DATA) { 460 assert(!dataSection); 461 dataSection = §ion; 462 } else if (section.Type == WASM_SEC_CUSTOM) { 463 InputChunk *customSec; 464 if (shouldMerge(section)) 465 customSec = make<MergeInputChunk>(section, this); 466 else 467 customSec = make<InputSection>(section, this); 468 customSec->discarded = isExcludedByComdat(customSec); 469 customSections.emplace_back(customSec); 470 customSections.back()->setRelocations(section.Relocations); 471 customSectionsByIndex[sectionIndex] = customSections.back(); 472 } 473 sectionIndex++; 474 // Scans relocations to determine if a function symbol is called directly. 475 for (const WasmRelocation &reloc : section.Relocations) 476 if (reloc.Type == R_WASM_FUNCTION_INDEX_LEB) 477 isCalledDirectly[reloc.Index] = true; 478 } 479 480 typeMap.resize(getWasmObj()->types().size()); 481 typeIsUsed.resize(getWasmObj()->types().size(), false); 482 483 484 // Populate `Segments`. 485 for (const WasmSegment &s : wasmObj->dataSegments()) { 486 InputChunk *seg; 487 if (shouldMerge(s)) { 488 seg = make<MergeInputChunk>(s, this); 489 } else 490 seg = make<InputSegment>(s, this); 491 seg->discarded = isExcludedByComdat(seg); 492 493 segments.emplace_back(seg); 494 } 495 setRelocs(segments, dataSection); 496 497 // Populate `Functions`. 498 ArrayRef<WasmFunction> funcs = wasmObj->functions(); 499 ArrayRef<uint32_t> funcTypes = wasmObj->functionTypes(); 500 ArrayRef<WasmSignature> types = wasmObj->types(); 501 functions.reserve(funcs.size()); 502 503 for (size_t i = 0, e = funcs.size(); i != e; ++i) { 504 auto* func = make<InputFunction>(types[funcTypes[i]], &funcs[i], this); 505 func->discarded = isExcludedByComdat(func); 506 functions.emplace_back(func); 507 } 508 setRelocs(functions, codeSection); 509 510 // Populate `Tables`. 511 for (const WasmTable &t : wasmObj->tables()) 512 tables.emplace_back(make<InputTable>(t, this)); 513 514 // Populate `Globals`. 515 for (const WasmGlobal &g : wasmObj->globals()) 516 globals.emplace_back(make<InputGlobal>(g, this)); 517 518 // Populate `Events`. 519 for (const WasmEvent &e : wasmObj->events()) 520 events.emplace_back(make<InputEvent>(types[e.Type.SigIndex], e, this)); 521 522 // Populate `Symbols` based on the symbols in the object. 523 symbols.reserve(wasmObj->getNumberOfSymbols()); 524 uint32_t tableSymbolCount = 0; 525 for (const SymbolRef &sym : wasmObj->symbols()) { 526 const WasmSymbol &wasmSym = wasmObj->getWasmSymbol(sym.getRawDataRefImpl()); 527 if (wasmSym.isTypeTable()) 528 tableSymbolCount++; 529 if (wasmSym.isDefined()) { 530 // createDefined may fail if the symbol is comdat excluded in which case 531 // we fall back to creating an undefined symbol 532 if (Symbol *d = createDefined(wasmSym)) { 533 symbols.push_back(d); 534 continue; 535 } 536 } 537 size_t idx = symbols.size(); 538 symbols.push_back(createUndefined(wasmSym, isCalledDirectly[idx])); 539 } 540 541 addLegacyIndirectFunctionTableIfNeeded(tableSymbolCount); 542 } 543 544 bool ObjFile::isExcludedByComdat(InputChunk *chunk) const { 545 uint32_t c = chunk->getComdat(); 546 if (c == UINT32_MAX) 547 return false; 548 return !keptComdats[c]; 549 } 550 551 FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t index) const { 552 return cast<FunctionSymbol>(symbols[index]); 553 } 554 555 GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t index) const { 556 return cast<GlobalSymbol>(symbols[index]); 557 } 558 559 EventSymbol *ObjFile::getEventSymbol(uint32_t index) const { 560 return cast<EventSymbol>(symbols[index]); 561 } 562 563 TableSymbol *ObjFile::getTableSymbol(uint32_t index) const { 564 return cast<TableSymbol>(symbols[index]); 565 } 566 567 SectionSymbol *ObjFile::getSectionSymbol(uint32_t index) const { 568 return cast<SectionSymbol>(symbols[index]); 569 } 570 571 DataSymbol *ObjFile::getDataSymbol(uint32_t index) const { 572 return cast<DataSymbol>(symbols[index]); 573 } 574 575 Symbol *ObjFile::createDefined(const WasmSymbol &sym) { 576 StringRef name = sym.Info.Name; 577 uint32_t flags = sym.Info.Flags; 578 579 switch (sym.Info.Kind) { 580 case WASM_SYMBOL_TYPE_FUNCTION: { 581 InputFunction *func = 582 functions[sym.Info.ElementIndex - wasmObj->getNumImportedFunctions()]; 583 if (sym.isBindingLocal()) 584 return make<DefinedFunction>(name, flags, this, func); 585 if (func->discarded) 586 return nullptr; 587 return symtab->addDefinedFunction(name, flags, this, func); 588 } 589 case WASM_SYMBOL_TYPE_DATA: { 590 InputChunk *seg = segments[sym.Info.DataRef.Segment]; 591 auto offset = sym.Info.DataRef.Offset; 592 auto size = sym.Info.DataRef.Size; 593 if (sym.isBindingLocal()) 594 return make<DefinedData>(name, flags, this, seg, offset, size); 595 if (seg->discarded) 596 return nullptr; 597 return symtab->addDefinedData(name, flags, this, seg, offset, size); 598 } 599 case WASM_SYMBOL_TYPE_GLOBAL: { 600 InputGlobal *global = 601 globals[sym.Info.ElementIndex - wasmObj->getNumImportedGlobals()]; 602 if (sym.isBindingLocal()) 603 return make<DefinedGlobal>(name, flags, this, global); 604 return symtab->addDefinedGlobal(name, flags, this, global); 605 } 606 case WASM_SYMBOL_TYPE_SECTION: { 607 InputChunk *section = customSectionsByIndex[sym.Info.ElementIndex]; 608 assert(sym.isBindingLocal()); 609 // Need to return null if discarded here? data and func only do that when 610 // binding is not local. 611 if (section->discarded) 612 return nullptr; 613 return make<SectionSymbol>(flags, section, this); 614 } 615 case WASM_SYMBOL_TYPE_EVENT: { 616 InputEvent *event = 617 events[sym.Info.ElementIndex - wasmObj->getNumImportedEvents()]; 618 if (sym.isBindingLocal()) 619 return make<DefinedEvent>(name, flags, this, event); 620 return symtab->addDefinedEvent(name, flags, this, event); 621 } 622 case WASM_SYMBOL_TYPE_TABLE: { 623 InputTable *table = 624 tables[sym.Info.ElementIndex - wasmObj->getNumImportedTables()]; 625 if (sym.isBindingLocal()) 626 return make<DefinedTable>(name, flags, this, table); 627 return symtab->addDefinedTable(name, flags, this, table); 628 } 629 } 630 llvm_unreachable("unknown symbol kind"); 631 } 632 633 Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) { 634 StringRef name = sym.Info.Name; 635 uint32_t flags = sym.Info.Flags | WASM_SYMBOL_UNDEFINED; 636 637 switch (sym.Info.Kind) { 638 case WASM_SYMBOL_TYPE_FUNCTION: 639 if (sym.isBindingLocal()) 640 return make<UndefinedFunction>(name, sym.Info.ImportName, 641 sym.Info.ImportModule, flags, this, 642 sym.Signature, isCalledDirectly); 643 return symtab->addUndefinedFunction(name, sym.Info.ImportName, 644 sym.Info.ImportModule, flags, this, 645 sym.Signature, isCalledDirectly); 646 case WASM_SYMBOL_TYPE_DATA: 647 if (sym.isBindingLocal()) 648 return make<UndefinedData>(name, flags, this); 649 return symtab->addUndefinedData(name, flags, this); 650 case WASM_SYMBOL_TYPE_GLOBAL: 651 if (sym.isBindingLocal()) 652 return make<UndefinedGlobal>(name, sym.Info.ImportName, 653 sym.Info.ImportModule, flags, this, 654 sym.GlobalType); 655 return symtab->addUndefinedGlobal(name, sym.Info.ImportName, 656 sym.Info.ImportModule, flags, this, 657 sym.GlobalType); 658 case WASM_SYMBOL_TYPE_TABLE: 659 if (sym.isBindingLocal()) 660 return make<UndefinedTable>(name, sym.Info.ImportName, 661 sym.Info.ImportModule, flags, this, 662 sym.TableType); 663 return symtab->addUndefinedTable(name, sym.Info.ImportName, 664 sym.Info.ImportModule, flags, this, 665 sym.TableType); 666 case WASM_SYMBOL_TYPE_SECTION: 667 llvm_unreachable("section symbols cannot be undefined"); 668 } 669 llvm_unreachable("unknown symbol kind"); 670 } 671 672 void ArchiveFile::parse() { 673 // Parse a MemoryBufferRef as an archive file. 674 LLVM_DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n"); 675 file = CHECK(Archive::create(mb), toString(this)); 676 677 // Read the symbol table to construct Lazy symbols. 678 int count = 0; 679 for (const Archive::Symbol &sym : file->symbols()) { 680 symtab->addLazy(this, &sym); 681 ++count; 682 } 683 LLVM_DEBUG(dbgs() << "Read " << count << " symbols\n"); 684 } 685 686 void ArchiveFile::addMember(const Archive::Symbol *sym) { 687 const Archive::Child &c = 688 CHECK(sym->getMember(), 689 "could not get the member for symbol " + sym->getName()); 690 691 // Don't try to load the same member twice (this can happen when members 692 // mutually reference each other). 693 if (!seen.insert(c.getChildOffset()).second) 694 return; 695 696 LLVM_DEBUG(dbgs() << "loading lazy: " << sym->getName() << "\n"); 697 LLVM_DEBUG(dbgs() << "from archive: " << toString(this) << "\n"); 698 699 MemoryBufferRef mb = 700 CHECK(c.getMemoryBufferRef(), 701 "could not get the buffer for the member defining symbol " + 702 sym->getName()); 703 704 InputFile *obj = createObjectFile(mb, getName()); 705 symtab->addFile(obj); 706 } 707 708 static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) { 709 switch (gvVisibility) { 710 case GlobalValue::DefaultVisibility: 711 return WASM_SYMBOL_VISIBILITY_DEFAULT; 712 case GlobalValue::HiddenVisibility: 713 case GlobalValue::ProtectedVisibility: 714 return WASM_SYMBOL_VISIBILITY_HIDDEN; 715 } 716 llvm_unreachable("unknown visibility"); 717 } 718 719 static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats, 720 const lto::InputFile::Symbol &objSym, 721 BitcodeFile &f) { 722 StringRef name = saver.save(objSym.getName()); 723 724 uint32_t flags = objSym.isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0; 725 flags |= mapVisibility(objSym.getVisibility()); 726 727 int c = objSym.getComdatIndex(); 728 bool excludedByComdat = c != -1 && !keptComdats[c]; 729 730 if (objSym.isUndefined() || excludedByComdat) { 731 flags |= WASM_SYMBOL_UNDEFINED; 732 if (objSym.isExecutable()) 733 return symtab->addUndefinedFunction(name, None, None, flags, &f, nullptr, 734 true); 735 return symtab->addUndefinedData(name, flags, &f); 736 } 737 738 if (objSym.isExecutable()) 739 return symtab->addDefinedFunction(name, flags, &f, nullptr); 740 return symtab->addDefinedData(name, flags, &f, nullptr, 0, 0); 741 } 742 743 bool BitcodeFile::doneLTO = false; 744 745 void BitcodeFile::parse() { 746 if (doneLTO) { 747 error(toString(this) + ": attempt to add bitcode file after LTO."); 748 return; 749 } 750 751 obj = check(lto::InputFile::create(MemoryBufferRef( 752 mb.getBuffer(), saver.save(archiveName + mb.getBufferIdentifier())))); 753 Triple t(obj->getTargetTriple()); 754 if (!t.isWasm()) { 755 error(toString(this) + ": machine type must be wasm32 or wasm64"); 756 return; 757 } 758 checkArch(t.getArch()); 759 std::vector<bool> keptComdats; 760 for (StringRef s : obj->getComdatTable()) 761 keptComdats.push_back(symtab->addComdat(s)); 762 763 for (const lto::InputFile::Symbol &objSym : obj->symbols()) 764 symbols.push_back(createBitcodeSymbol(keptComdats, objSym, *this)); 765 } 766 767 } // namespace wasm 768 } // namespace lld 769