brintos

brintos / llvm-project-archived public Read only

0
0
Text · 46.2 KiB · 705ef88 Raw
1246 lines · cpp
1//===- SymbolTable.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "SymbolTable.h"10#include "Config.h"11#include "InputChunks.h"12#include "InputElement.h"13#include "WriterUtils.h"14#include "lld/Common/CommonLinkerContext.h"15#include <optional>16 17#define DEBUG_TYPE "lld"18 19using namespace llvm;20using namespace llvm::wasm;21using namespace llvm::object;22 23namespace lld::wasm {24SymbolTable *symtab;25 26void SymbolTable::addFile(InputFile *file, StringRef symName) {27  log("Processing: " + toString(file));28 29  // Lazy object file30  if (file->lazy) {31    if (auto *f = dyn_cast<BitcodeFile>(file)) {32      ctx.lazyBitcodeFiles.push_back(f);33      f->parseLazy();34    } else {35      cast<ObjFile>(file)->parseLazy();36    }37    return;38  }39 40  // .so file41  if (auto *f = dyn_cast<SharedFile>(file)) {42    // If we are not reporting undefined symbols that we don't actualy43    // parse the shared library symbol table.44    f->parse();45    ctx.sharedFiles.push_back(f);46    return;47  }48 49  // stub file50  if (auto *f = dyn_cast<StubFile>(file)) {51    f->parse();52    ctx.stubFiles.push_back(f);53    return;54  }55 56  if (ctx.arg.trace)57    message(toString(file));58 59  // LLVM bitcode file60  if (auto *f = dyn_cast<BitcodeFile>(file)) {61    // This order, first adding to `bitcodeFiles` and then parsing is necessary.62    // See https://github.com/llvm/llvm-project/pull/7309563    ctx.bitcodeFiles.push_back(f);64    f->parse(symName);65    return;66  }67 68  // Regular object file69  auto *f = cast<ObjFile>(file);70  f->parse(false);71  ctx.objectFiles.push_back(f);72}73 74// This function is where all the optimizations of link-time75// optimization happens. When LTO is in use, some input files are76// not in native object file format but in the LLVM bitcode format.77// This function compiles bitcode files into a few big native files78// using LLVM functions and replaces bitcode symbols with the results.79// Because all bitcode files that the program consists of are passed80// to the compiler at once, it can do whole-program optimization.81void SymbolTable::compileBitcodeFiles() {82  // Prevent further LTO objects being included83  BitcodeFile::doneLTO = true;84 85  // Compile bitcode files and replace bitcode symbols.86  lto.reset(new BitcodeCompiler);87  for (BitcodeFile *f : ctx.bitcodeFiles)88    lto->add(*f);89 90  for (auto &file : lto->compile()) {91    auto *obj = cast<ObjFile>(file);92    obj->parse(true);93    ctx.objectFiles.push_back(obj);94  }95}96 97Symbol *SymbolTable::find(StringRef name) {98  auto it = symMap.find(CachedHashStringRef(name));99  if (it == symMap.end() || it->second == -1)100    return nullptr;101  return symVector[it->second];102}103 104void SymbolTable::replace(StringRef name, Symbol* sym) {105  auto it = symMap.find(CachedHashStringRef(name));106  symVector[it->second] = sym;107}108 109std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) {110  bool trace = false;111  auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});112  int &symIndex = p.first->second;113  bool isNew = p.second;114  if (symIndex == -1) {115    symIndex = symVector.size();116    trace = true;117    isNew = true;118  }119 120  if (!isNew)121    return {symVector[symIndex], false};122 123  Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());124  sym->isUsedInRegularObj = false;125  sym->canInline = true;126  sym->traced = trace;127  sym->forceExport = false;128  sym->referenced = !ctx.arg.gcSections;129  symVector.emplace_back(sym);130  return {sym, true};131}132 133std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,134                                              const InputFile *file) {135  Symbol *s;136  bool wasInserted;137  std::tie(s, wasInserted) = insertName(name);138 139  if (!file || file->kind() == InputFile::ObjectKind)140    s->isUsedInRegularObj = true;141 142  return {s, wasInserted};143}144 145static void reportTypeError(const Symbol *existing, const InputFile *file,146                            llvm::wasm::WasmSymbolType type) {147  error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " +148        toString(existing->getWasmType()) + " in " +149        toString(existing->getFile()) + "\n>>> defined as " + toString(type) +150        " in " + toString(file));151}152 153// Check the type of new symbol matches that of the symbol is replacing.154// Returns true if the function types match, false is there is a signature155// mismatch.156static bool signatureMatches(FunctionSymbol *existing,157                             const WasmSignature *newSig) {158  const WasmSignature *oldSig = existing->signature;159 160  // If either function is missing a signature (this happens for bitcode161  // symbols) then assume they match.  Any mismatch will be reported later162  // when the LTO objects are added.163  if (!newSig || !oldSig)164    return true;165 166  return *newSig == *oldSig;167}168 169static void checkGlobalType(const Symbol *existing, const InputFile *file,170                            const WasmGlobalType *newType) {171  if (!isa<GlobalSymbol>(existing)) {172    reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL);173    return;174  }175 176  const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType();177  if (*newType != *oldType) {178    error("Global type mismatch: " + existing->getName() + "\n>>> defined as " +179          toString(*oldType) + " in " + toString(existing->getFile()) +180          "\n>>> defined as " + toString(*newType) + " in " + toString(file));181  }182}183 184static void checkTagType(const Symbol *existing, const InputFile *file,185                         const WasmSignature *newSig) {186  const auto *existingTag = dyn_cast<TagSymbol>(existing);187  if (!isa<TagSymbol>(existing)) {188    reportTypeError(existing, file, WASM_SYMBOL_TYPE_TAG);189    return;190  }191 192  const WasmSignature *oldSig = existingTag->signature;193  if (*newSig != *oldSig)194    warn("Tag signature mismatch: " + existing->getName() +195         "\n>>> defined as " + toString(*oldSig) + " in " +196         toString(existing->getFile()) + "\n>>> defined as " +197         toString(*newSig) + " in " + toString(file));198}199 200static void checkTableType(const Symbol *existing, const InputFile *file,201                           const WasmTableType *newType) {202  if (!isa<TableSymbol>(existing)) {203    reportTypeError(existing, file, WASM_SYMBOL_TYPE_TABLE);204    return;205  }206 207  const WasmTableType *oldType = cast<TableSymbol>(existing)->getTableType();208  if (newType->ElemType != oldType->ElemType) {209    error("Table type mismatch: " + existing->getName() + "\n>>> defined as " +210          toString(*oldType) + " in " + toString(existing->getFile()) +211          "\n>>> defined as " + toString(*newType) + " in " + toString(file));212  }213  // FIXME: No assertions currently on the limits.214}215 216static void checkDataType(const Symbol *existing, const InputFile *file) {217  if (!isa<DataSymbol>(existing))218    reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA);219}220 221// glibc/wasm32 (HWJS-B-1): a *strong, undefined* symbol classified222// WASM_SYMBOL_TYPE_DATA carries no authoritative type. On wasm, a symbol223// referenced only by a GNU-as style assembler alias ("alias = target", as224// emitted by glibc's libc_hidden_builtin redirect, e.g.225// `memset = __GI_memset`) reaches the object file with no `.functype`226// directive, so MC necessarily classifies the undefined target as DATA --227// wasm has no "untyped" symbol kind, so an undefined reference with no type228// information must default to something. Mirroring ELF/GNU-ld, such a229// typeless *undefined* data reference must be allowed to resolve against a230// real FUNCTION of the same name instead of being pinned to DATA and231// rejected with a spurious "symbol type mismatch".232//233// This is the link-time consequence of the MC-side sibling fix in234// toolchains/llvm-project commit 4f7e79cc9 (which leaves an alias-to-an-235// undefined-target undefined for link-time resolution).236//237// It is tightly guarded to preserve the never-silent contract:238//   * It only fires when the data side is *undefined* (a mere reference).239//     Two *defined* symbols of conflicting type, a *defined* data object vs a240//     function, and a directly *called* (undefined function) symbol vs a241//     *defined* data object all still error loudly. A function symbol --242//     defined, or undefined-because-called -- is authoritative about243//     function-ness and is never demoted to data here.244//   * It excludes *weak* undefined data. A strong undefined reference MUST be245//     satisfied by a definition, so when the sole definition is a function,246//     binding to it is the unique correct resolution. A weak undefined data247//     symbol, by contrast, has a legitimate unresolved meaning (it resolves248//     to address 0 as data), so its DATA typing is load-bearing; silently249//     rebinding it to a function could change program meaning, so a250//     weak-data-vs-function clash still errors loudly.251static bool isUntypedUndefinedDataRef(const Symbol *s) {252  return isa<UndefinedData>(s) && !s->isWeak();253}254 255// True for an incoming undefined-data symbol (described by `flags`) that may256// bind to an existing function of the same name -- i.e. it is undefined and257// not weak. See isUntypedUndefinedDataRef for the rationale and guards.258static bool isUntypedUndefinedDataFlags(uint32_t flags) {259  return (flags & WASM_SYMBOL_UNDEFINED) &&260         (flags & WASM_SYMBOL_BINDING_MASK) != WASM_SYMBOL_BINDING_WEAK;261}262 263// glibc/wasm32 (HWJS-B-1): the link-stage sequel to the MC-side fix 95803ba61.264// That commit made a bare label (`.globl X` / `X:`, with no `.functype`/`.type`265// /`.size` and nothing following) *assemble* as a size-0 DATA *definition* --266// it synthesizes an empty data segment so the symbol is addressable -- rather267// than crashing. wasm has no NOTYPE/untyped symbol kind (the symbol-type enum268// is only FUNCTION/DATA/GLOBAL/SECTION/TAG/TABLE), so an untyped bare label269// must be classified as *something*, and a size-0 DATA def is the addressable,270// never-dropped representation 95803ba61 chose.271//272// glibc's stock elf/Makefile emits exactly such bare labels for the rtld symbol273// map (rtld-stubbed-symbols: malloc/free/calloc/__syscall_cancel/274// __libc_assert_fail/...), into a throwaway object (librtld.mapT.o) that exists275// only to produce a linker -Map; its symbol *definitions* are placeholders. At276// the rtld-map link those size-0 DATA placeholders collide with the real277// FUNCTION definitions of the same names in libc_pic.a.278//279// On ELF/GNU-as a bare label with no `.type` is NOTYPE: it asserts no type and280// resolves against whatever real definition exists. Mirror that here -- a281// size-0 DATA *definition* (a placeholder that occupies no storage) defers to a282// real FUNCTION definition of the same name; the function wins, in either283// object order. This refines, and does not regress, 95803ba61: a bare label284// with no competing function still assembles and stays an addressable size-0285// DATA symbol; only when a typed FUNCTION definition of the same name also286// exists does the typed one win.287//288// Tightly guarded to preserve never-silent: only a *zero-size* DATA definition289// qualifies. A real DATA object (non-zero size) vs a FUNCTION definition of the290// same name is a genuine conflict and still errors loudly via checkDataType --291// two real definitions are never silently merged. A function symbol is292// authoritative about function-ness and is never demoted to data.293static bool isPlaceholderDataDef(const Symbol *s) {294  auto *d = dyn_cast<DefinedData>(s);295  return d && d->getSize() == 0;296}297 298DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,299                                                   uint32_t flags,300                                                   InputFunction *function) {301  LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n");302  assert(!find(name));303  ctx.syntheticFunctions.emplace_back(function);304  return replaceSymbol<DefinedFunction>(insertName(name).first, name,305                                        flags, nullptr, function);306}307 308// Adds an optional, linker generated, data symbol.  The symbol will only be309// added if there is an undefine reference to it, or if it is explicitly310// exported via the --export flag.  Otherwise we don't add the symbol and return311// nullptr.312DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,313                                                uint64_t value) {314  Symbol *s = find(name);315  if (!s && (ctx.arg.exportAll || ctx.arg.exportedSymbols.count(name) != 0))316    s = insertName(name).first;317  else if (!s || s->isDefined())318    return nullptr;319  LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");320  auto *rtn = replaceSymbol<DefinedData>(321      s, name, WASM_SYMBOL_VISIBILITY_HIDDEN | WASM_SYMBOL_ABSOLUTE);322  rtn->setVA(value);323  rtn->referenced = true;324  return rtn;325}326 327DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name,328                                                 uint32_t flags) {329  LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n");330  assert(!find(name));331  return replaceSymbol<DefinedData>(insertName(name).first, name,332                                    flags | WASM_SYMBOL_ABSOLUTE);333}334 335DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags,336                                               InputGlobal *global) {337  LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global338                    << "\n");339  assert(!find(name));340  ctx.syntheticGlobals.emplace_back(global);341  return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags,342                                      nullptr, global);343}344 345DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name,346                                                    InputGlobal *global) {347  Symbol *s = find(name);348  if (!s || s->isDefined())349    return nullptr;350  LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global351                    << "\n");352  ctx.syntheticGlobals.emplace_back(global);353  return replaceSymbol<DefinedGlobal>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN,354                                      nullptr, global);355}356 357DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags,358                                             InputTable *table) {359  LLVM_DEBUG(dbgs() << "addSyntheticTable: " << name << " -> " << table360                    << "\n");361  Symbol *s = find(name);362  assert(!s || s->isUndefined());363  if (!s)364    s = insertName(name).first;365  ctx.syntheticTables.emplace_back(table);366  return replaceSymbol<DefinedTable>(s, name, flags, nullptr, table);367}368 369static bool shouldReplace(const Symbol *existing, InputFile *newFile,370                          uint32_t newFlags) {371  // If existing symbol is undefined, replace it.372  if (!existing->isDefined()) {373    LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "374                      << existing->getName() << "\n");375    return true;376  }377 378  // Now we have two defined symbols. If the new one is weak, we can ignore it.379  if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {380    LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");381    return false;382  }383 384  // If the existing symbol is weak, we should replace it.385  if (existing->isWeak()) {386    LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");387    return true;388  }389 390  // Similarly with shared symbols391  if (existing->isShared()) {392    LLVM_DEBUG(dbgs() << "replacing existing shared symbol\n");393    return true;394  }395 396  // Neither symbol is week. They conflict.397  if (ctx.arg.allowMultipleDefinition)398    return false;399 400  errorOrWarn("duplicate symbol: " + toString(*existing) + "\n>>> defined in " +401              toString(existing->getFile()) + "\n>>> defined in " +402              toString(newFile));403  return true;404}405 406static void reportFunctionSignatureMismatch(StringRef symName,407                                            FunctionSymbol *sym,408                                            const WasmSignature *signature,409                                            InputFile *file,410                                            bool isError = true) {411  std::string msg =412      ("function signature mismatch: " + symName + "\n>>> defined as " +413       toString(*sym->signature) + " in " + toString(sym->getFile()) +414       "\n>>> defined as " + toString(*signature) + " in " + toString(file))415          .str();416  if (isError)417    error(msg);418  else419    warn(msg);420}421 422static void reportFunctionSignatureMismatch(StringRef symName,423                                            FunctionSymbol *a,424                                            FunctionSymbol *b,425                                            bool isError = true) {426  reportFunctionSignatureMismatch(symName, a, b->signature, b->getFile(),427                                  isError);428}429 430Symbol *SymbolTable::addSharedFunction(StringRef name, uint32_t flags,431                                       InputFile *file,432                                       const WasmSignature *sig) {433  LLVM_DEBUG(dbgs() << "addSharedFunction: " << name << " [" << toString(*sig)434                    << "]\n");435  Symbol *s;436  bool wasInserted;437  std::tie(s, wasInserted) = insert(name, file);438 439  auto replaceSym = [&](Symbol *sym) {440    replaceSymbol<SharedFunctionSymbol>(sym, name, flags, file, sig);441  };442 443  if (wasInserted || s->isLazy()) {444    replaceSym(s);445    return s;446  }447 448  auto existingFunction = dyn_cast<FunctionSymbol>(s);449  if (!existingFunction) {450    reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);451    return s;452  }453 454  // Shared symbols should never replace locally-defined ones455  if (s->isDefined()) {456    return s;457  }458 459  LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: " << s->getName()460                    << "\n");461 462  bool checkSig = true;463  if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))464    checkSig = ud->isCalledDirectly;465 466  if (checkSig && !signatureMatches(existingFunction, sig)) {467    if (ctx.arg.shlibSigCheck) {468      reportFunctionSignatureMismatch(name, existingFunction, sig, file);469    } else {470      // With --no-shlib-sigcheck we ignore the signature of the function as471      // defined by the shared library and instead use the signature as472      // expected by the program being linked.473      sig = existingFunction->signature;474    }475  }476 477  replaceSym(s);478  return s;479}480 481Symbol *SymbolTable::addSharedData(StringRef name, uint32_t flags,482                                   InputFile *file) {483  LLVM_DEBUG(dbgs() << "addSharedData: " << name << "\n");484  Symbol *s;485  bool wasInserted;486  std::tie(s, wasInserted) = insert(name, file);487 488  if (wasInserted || s->isLazy()) {489    replaceSymbol<SharedData>(s, name, flags, file);490    return s;491  }492 493  // Shared symbols should never replace locally-defined ones494  if (s->isDefined()) {495    return s;496  }497 498  checkDataType(s, file);499  replaceSymbol<SharedData>(s, name, flags, file);500  return s;501}502 503Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags,504                                        InputFile *file,505                                        InputFunction *function) {506  LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " ["507                    << (function ? toString(function->signature) : "none")508                    << "]\n");509  Symbol *s;510  bool wasInserted;511  std::tie(s, wasInserted) = insert(name, file);512 513  auto replaceSym = [&](Symbol *sym) {514    // If the new defined function doesn't have signature (i.e. bitcode515    // functions) but the old symbol does, then preserve the old signature516    const WasmSignature *oldSig = s->getSignature();517    auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function);518    if (!newSym->signature)519      newSym->signature = oldSig;520  };521 522  if (wasInserted || s->isLazy()) {523    replaceSym(s);524    return s;525  }526 527  auto existingFunction = dyn_cast<FunctionSymbol>(s);528  if (!existingFunction) {529    // A typeless undefined data reference (e.g. the target of a GNU-as alias)530    // binds to this real function definition. See isUntypedUndefinedDataRef.531    if (isUntypedUndefinedDataRef(s)) {532      LLVM_DEBUG(dbgs() << "resolving undefined data ref to function def: "533                        << s->getName() << "\n");534      replaceSym(s);535      return s;536    }537    // A size-0 DATA *definition* placeholder (a bare label emitted for the rtld538    // symbol map, commit 95803ba61) defers to this real function definition --539    // the typed definition wins. See isPlaceholderDataDef.540    if (isPlaceholderDataDef(s)) {541      LLVM_DEBUG(dbgs() << "resolving placeholder data def to function def: "542                        << s->getName() << "\n");543      replaceSym(s);544      return s;545    }546    reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);547    return s;548  }549 550  bool checkSig = true;551  if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))552    checkSig = ud->isCalledDirectly;553 554  if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) {555    Symbol* variant;556    if (getFunctionVariant(s, &function->signature, file, &variant))557      // New variant, always replace558      replaceSym(variant);559    else if (shouldReplace(s, file, flags))560      // Variant already exists, replace it after checking shouldReplace561      replaceSym(variant);562 563    // This variant we found take the place in the symbol table as the primary564    // variant.565    replace(name, variant);566    return variant;567  }568 569  // Existing function with matching signature.570  if (shouldReplace(s, file, flags))571    replaceSym(s);572 573  return s;574}575 576Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags,577                                    InputFile *file, InputChunk *segment,578                                    uint64_t address, uint64_t size) {579  LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address580                    << "\n");581  Symbol *s;582  bool wasInserted;583  std::tie(s, wasInserted) = insert(name, file);584 585  auto replaceSym = [&]() {586    replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size);587  };588 589  if (wasInserted || s->isLazy()) {590    replaceSym();591    return s;592  }593 594  // A size-0 DATA *definition* placeholder (a bare label emitted for the rtld595  // symbol map, commit 95803ba61) is typeless, like a NOTYPE symbol on ELF.596  // This is the reverse-order companion of the paths in addDefinedFunction /597  // addUndefinedFunction. Only a *zero-size* placeholder qualifies; a real598  // (non-zero) data object vs a function still errors via checkDataType below599  // (never-silent). See isPlaceholderDataDef.600  if (size == 0 && isa<FunctionSymbol>(s)) {601    // An existing real FUNCTION *definition* (defined here or in a shared lib)602    // is authoritative and wins -- the placeholder defers and is never603    // promoted over it.604    if (!isa<UndefinedFunction>(s)) {605      LLVM_DEBUG(dbgs() << "placeholder data def defers to existing function: "606                        << s->getName() << "\n");607      return s;608    }609    // An existing *undefined* FUNCTION reference (a mere call) is satisfied by610    // this placeholder definition (stub semantics): the placeholder becomes the611    // definition so the reference resolves to it, exactly as a NOTYPE612    // definition would on ELF. (Forward order -- placeholder first, then the613    // call -- is handled in addUndefinedFunction.)614    LLVM_DEBUG(dbgs() << "placeholder data def satisfies undefined function "615                         "ref: "616                      << s->getName() << "\n");617    replaceSym();618    return s;619  }620 621  checkDataType(s, file);622 623  if (shouldReplace(s, file, flags))624    replaceSym();625  return s;626}627 628Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags,629                                      InputFile *file, InputGlobal *global) {630  LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n");631 632  Symbol *s;633  bool wasInserted;634  std::tie(s, wasInserted) = insert(name, file);635 636  auto replaceSym = [&]() {637    replaceSymbol<DefinedGlobal>(s, name, flags, file, global);638  };639 640  if (wasInserted || s->isLazy()) {641    replaceSym();642    return s;643  }644 645  checkGlobalType(s, file, &global->getType());646 647  if (shouldReplace(s, file, flags))648    replaceSym();649  return s;650}651 652Symbol *SymbolTable::addDefinedTag(StringRef name, uint32_t flags,653                                   InputFile *file, InputTag *tag) {654  LLVM_DEBUG(dbgs() << "addDefinedTag:" << name << "\n");655 656  Symbol *s;657  bool wasInserted;658  std::tie(s, wasInserted) = insert(name, file);659 660  auto replaceSym = [&]() {661    replaceSymbol<DefinedTag>(s, name, flags, file, tag);662  };663 664  if (wasInserted || s->isLazy()) {665    replaceSym();666    return s;667  }668 669  checkTagType(s, file, &tag->signature);670 671  if (shouldReplace(s, file, flags))672    replaceSym();673  return s;674}675 676Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags,677                                     InputFile *file, InputTable *table) {678  LLVM_DEBUG(dbgs() << "addDefinedTable:" << name << "\n");679 680  Symbol *s;681  bool wasInserted;682  std::tie(s, wasInserted) = insert(name, file);683 684  auto replaceSym = [&]() {685    replaceSymbol<DefinedTable>(s, name, flags, file, table);686  };687 688  if (wasInserted || s->isLazy()) {689    replaceSym();690    return s;691  }692 693  checkTableType(s, file, &table->getType());694 695  if (shouldReplace(s, file, flags))696    replaceSym();697  return s;698}699 700// This function get called when an undefined symbol is added, and there is701// already an existing one in the symbols table.  In this case we check that702// custom 'import-module' and 'import-field' symbol attributes agree.703// With LTO these attributes are not available when the bitcode is read and only704// become available when the LTO object is read.  In this case we silently705// replace the empty attributes with the valid ones.706template <typename T>707static void setImportAttributes(T *existing,708                                std::optional<StringRef> importName,709                                std::optional<StringRef> importModule,710                                uint32_t flags, InputFile *file) {711  if (importName) {712    if (!existing->importName)713      existing->importName = importName;714    if (existing->importName != importName)715      error("import name mismatch for symbol: " + toString(*existing) +716            "\n>>> defined as " + *existing->importName + " in " +717            toString(existing->getFile()) + "\n>>> defined as " + *importName +718            " in " + toString(file));719  }720 721  if (importModule) {722    if (!existing->importModule)723      existing->importModule = importModule;724    if (existing->importModule != importModule)725      error("import module mismatch for symbol: " + toString(*existing) +726            "\n>>> defined as " + *existing->importModule + " in " +727            toString(existing->getFile()) + "\n>>> defined as " +728            *importModule + " in " + toString(file));729  }730 731  // Update symbol binding, if the existing symbol is weak732  uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK;733  if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) {734    existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding;735  }736}737 738Symbol *SymbolTable::addUndefinedFunction(StringRef name,739                                          std::optional<StringRef> importName,740                                          std::optional<StringRef> importModule,741                                          uint32_t flags, InputFile *file,742                                          const WasmSignature *sig,743                                          bool isCalledDirectly) {744  LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " ["745                    << (sig ? toString(*sig) : "none")746                    << "] IsCalledDirectly:" << isCalledDirectly << " flags=0x"747                    << utohexstr(flags) << "\n");748  assert(flags & WASM_SYMBOL_UNDEFINED);749 750  Symbol *s;751  bool wasInserted;752  std::tie(s, wasInserted) = insert(name, file);753  if (s->traced)754    printTraceSymbolUndefined(name, file);755 756  auto replaceSym = [&]() {757    replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,758                                     file, sig, isCalledDirectly);759  };760 761  if (wasInserted) {762    replaceSym();763  } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {764    if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {765      lazy->setWeak();766      lazy->signature = sig;767    } else {768      lazy->extract();769      if (!ctx.arg.whyExtract.empty())770        ctx.whyExtractRecords.emplace_back(toString(file), s->getFile(), *s);771    }772  } else {773    auto existingFunction = dyn_cast<FunctionSymbol>(s);774    if (!existingFunction) {775      // A typeless undefined data reference (e.g. the target of a GNU-as776      // alias) is upgraded to an undefined function reference, so it can777      // later bind to a real FUNCTION definition of the same name. See778      // isUntypedUndefinedDataRef.779      if (isUntypedUndefinedDataRef(s)) {780        LLVM_DEBUG(dbgs() << "upgrading undefined data ref to function ref: "781                          << s->getName() << "\n");782        replaceSym();783        return s;784      }785      // A size-0 DATA *definition* placeholder (a bare label emitted for the786      // rtld symbol map, commit 95803ba61) is typeless: like a NOTYPE787      // definition on ELF it satisfies this function reference (a call)788      // directly, without being demoted to a function or pulling a real789      // implementation. glibc's rtld map relies on exactly this -- its stubbed790      // symbols (malloc/free/calloc/__syscall_cancel/...) are deliberately791      // *defined* as such placeholders so the relocatable link does NOT pull792      // their real definition out of libc_pic.a (elf/Makefile: "stubbed out793      // during symbol discovery ... provided differently in rtld"). The794      // placeholder definition stays put and the reference resolves to it. A795      // genuine FUNCTION *definition* of the same name still wins via796      // addDefinedFunction (the typed definition is authoritative). Only a797      // *zero-size* placeholder qualifies; a real (non-zero) data object called798      // as a function still errors below (never-silent).799      if (isPlaceholderDataDef(s)) {800        LLVM_DEBUG(dbgs() << "function ref satisfied by placeholder data def: "801                          << s->getName() << "\n");802        return s;803      }804      reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);805      return s;806    }807    if (!existingFunction->signature && sig)808      existingFunction->signature = sig;809    auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction);810    if (isCalledDirectly && !signatureMatches(existingFunction, sig)) {811      if (existingFunction->isShared()) {812        // Special handling for when the existing function is a shared symbol813        if (ctx.arg.shlibSigCheck) {814          reportFunctionSignatureMismatch(name, existingFunction, sig, file);815        } else {816          existingFunction->signature = sig;817        }818      }819      // If the existing undefined functions is not called directly then let820      // this one take precedence.  Otherwise the existing function is either821      // directly called or defined, in which case we need a function variant.822      else if (existingUndefined && !existingUndefined->isCalledDirectly)823        replaceSym();824      else if (getFunctionVariant(s, sig, file, &s))825        replaceSym();826    }827    if (existingUndefined) {828      setImportAttributes(existingUndefined, importName, importModule, flags,829                          file);830      if (isCalledDirectly)831        existingUndefined->isCalledDirectly = true;832      if (s->isWeak())833        s->flags = flags;834    }835  }836 837  return s;838}839 840Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,841                                      InputFile *file) {842  LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n");843  assert(flags & WASM_SYMBOL_UNDEFINED);844 845  Symbol *s;846  bool wasInserted;847  std::tie(s, wasInserted) = insert(name, file);848  if (s->traced)849    printTraceSymbolUndefined(name, file);850 851  if (wasInserted) {852    replaceSymbol<UndefinedData>(s, name, flags, file);853  } else if (auto *lazy = dyn_cast<LazySymbol>(s)) {854    if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK)855      lazy->setWeak();856    else857      lazy->extract();858  } else if (s->isDefined()) {859    // A strong (non-weak) typeless undefined data reference (this symbol) may860    // bind to an existing real function of the same name -- keep the861    // function. Only a genuine defined-data type clash, or a *weak* undefined862    // data reference (whose DATA typing is load-bearing), errors. See863    // isUntypedUndefinedDataRef. (The reverse order, where the function864    // arrives second, is handled in addDefinedFunction; an existing865    // *undefined* function is likewise left in place by the fall-through866    // below, letting a strong undefined data reference bind to a definition867    // later.)868    if (!(isa<FunctionSymbol>(s) && isUntypedUndefinedDataFlags(flags)))869      checkDataType(s, file);870  } else if (s->isWeak()) {871    s->flags = flags;872  }873  return s;874}875 876Symbol *SymbolTable::addUndefinedGlobal(StringRef name,877                                        std::optional<StringRef> importName,878                                        std::optional<StringRef> importModule,879                                        uint32_t flags, InputFile *file,880                                        const WasmGlobalType *type) {881  LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");882  assert(flags & WASM_SYMBOL_UNDEFINED);883 884  Symbol *s;885  bool wasInserted;886  std::tie(s, wasInserted) = insert(name, file);887  if (s->traced)888    printTraceSymbolUndefined(name, file);889 890  if (wasInserted)891    replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,892                                   file, type);893  else if (auto *lazy = dyn_cast<LazySymbol>(s))894    lazy->extract();895  else if (s->isDefined())896    checkGlobalType(s, file, type);897  else if (s->isWeak())898    s->flags = flags;899  return s;900}901 902Symbol *SymbolTable::addUndefinedTable(StringRef name,903                                       std::optional<StringRef> importName,904                                       std::optional<StringRef> importModule,905                                       uint32_t flags, InputFile *file,906                                       const WasmTableType *type) {907  LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n");908  assert(flags & WASM_SYMBOL_UNDEFINED);909 910  Symbol *s;911  bool wasInserted;912  std::tie(s, wasInserted) = insert(name, file);913  if (s->traced)914    printTraceSymbolUndefined(name, file);915 916  if (wasInserted)917    replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags,918                                  file, type);919  else if (auto *lazy = dyn_cast<LazySymbol>(s))920    lazy->extract();921  else if (s->isDefined())922    checkTableType(s, file, type);923  else if (s->isWeak())924    s->flags = flags;925  return s;926}927 928Symbol *SymbolTable::addUndefinedTag(StringRef name,929                                     std::optional<StringRef> importName,930                                     std::optional<StringRef> importModule,931                                     uint32_t flags, InputFile *file,932                                     const WasmSignature *sig) {933  LLVM_DEBUG(dbgs() << "addUndefinedTag: " << name << "\n");934  assert(flags & WASM_SYMBOL_UNDEFINED);935 936  Symbol *s;937  bool wasInserted;938  std::tie(s, wasInserted) = insert(name, file);939  if (s->traced)940    printTraceSymbolUndefined(name, file);941 942  if (wasInserted)943    replaceSymbol<UndefinedTag>(s, name, importName, importModule, flags, file,944                                sig);945  else if (auto *lazy = dyn_cast<LazySymbol>(s))946    lazy->extract();947  else if (s->isDefined())948    checkTagType(s, file, sig);949  else if (s->isWeak())950    s->flags = flags;951  return s;952}953 954TableSymbol *SymbolTable::createUndefinedIndirectFunctionTable(StringRef name) {955  WasmLimits limits{0, 0, 0, 0}; // Set by the writer.956  WasmTableType *type = make<WasmTableType>();957  type->ElemType = ValType::FUNCREF;958  type->Limits = limits;959  uint32_t flags = ctx.arg.exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;960  flags |= WASM_SYMBOL_UNDEFINED;961  Symbol *sym =962      addUndefinedTable(name, name, defaultModule, flags, nullptr, type);963  sym->markLive();964  sym->forceExport = ctx.arg.exportTable;965  return cast<TableSymbol>(sym);966}967 968TableSymbol *SymbolTable::createDefinedIndirectFunctionTable(StringRef name) {969  const uint32_t invalidIndex = -1;970  WasmLimits limits{0, 0, 0, 0}; // Set by the writer.971  WasmTableType type{ValType::FUNCREF, limits};972  WasmTable desc{invalidIndex, type, name};973  InputTable *table = make<InputTable>(desc, nullptr);974  uint32_t flags = ctx.arg.exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;975  TableSymbol *sym = addSyntheticTable(name, flags, table);976  sym->markLive();977  sym->forceExport = ctx.arg.exportTable;978  return sym;979}980 981// Whether or not we need an indirect function table is usually a function of982// whether an input declares a need for it.  However sometimes it's possible for983// no input to need the indirect function table, but then a late984// addInternalGOTEntry causes a function to be allocated an address.  In that985// case address we synthesize a definition at the last minute.986TableSymbol *SymbolTable::resolveIndirectFunctionTable(bool required) {987  Symbol *existing = find(functionTableName);988  if (existing) {989    if (!isa<TableSymbol>(existing)) {990      error(Twine("reserved symbol must be of type table: `") +991            functionTableName + "`");992      return nullptr;993    }994    if (existing->isDefined()) {995      error(Twine("reserved symbol must not be defined in input files: `") +996            functionTableName + "`");997      return nullptr;998    }999  }1000 1001  if (ctx.arg.importTable) {1002    if (existing) {1003      existing->importModule = defaultModule;1004      existing->importName = functionTableName;1005      return cast<TableSymbol>(existing);1006    }1007    if (required)1008      return createUndefinedIndirectFunctionTable(functionTableName);1009  } else if ((existing && existing->isLive()) || ctx.arg.exportTable ||1010             required) {1011    // A defined table is required.  Either because the user request an exported1012    // table or because the table symbol is already live.  The existing table is1013    // guaranteed to be undefined due to the check above.1014    return createDefinedIndirectFunctionTable(functionTableName);1015  }1016 1017  // An indirect function table will only be present in the symbol table if1018  // needed by a reloc; if we get here, we don't need one.1019  return nullptr;1020}1021 1022void SymbolTable::addLazy(StringRef name, InputFile *file) {1023  LLVM_DEBUG(dbgs() << "addLazy: " << name << "\n");1024 1025  Symbol *s;1026  bool wasInserted;1027  std::tie(s, wasInserted) = insertName(name);1028 1029  if (wasInserted) {1030    replaceSymbol<LazySymbol>(s, name, 0, file);1031    return;1032  }1033 1034  if (!s->isUndefined())1035    return;1036 1037  // The existing symbol is undefined, load a new one from the archive,1038  // unless the existing symbol is weak in which case replace the undefined1039  // symbols with a LazySymbol.1040  if (s->isWeak()) {1041    const WasmSignature *oldSig = nullptr;1042    // In the case of an UndefinedFunction we need to preserve the expected1043    // signature.1044    if (auto *f = dyn_cast<UndefinedFunction>(s))1045      oldSig = f->signature;1046    LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");1047    auto newSym =1048        replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK, file);1049    newSym->signature = oldSig;1050    return;1051  }1052 1053  LLVM_DEBUG(dbgs() << "replacing existing undefined\n");1054  const InputFile *oldFile = s->getFile();1055  LazySymbol(name, 0, file).extract();1056  if (!ctx.arg.whyExtract.empty())1057    ctx.whyExtractRecords.emplace_back(toString(oldFile), s->getFile(), *s);1058}1059 1060bool SymbolTable::addComdat(StringRef name) {1061  return comdatGroups.insert(CachedHashStringRef(name)).second;1062}1063 1064// The new signature doesn't match.  Create a variant to the symbol with the1065// signature encoded in the name and return that instead.  These symbols are1066// then unified later in handleSymbolVariants.1067bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,1068                                     const InputFile *file, Symbol **out) {1069  LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> "1070                    << " " << toString(*sig) << "\n");1071  Symbol *variant = nullptr;1072 1073  // Linear search through symbol variants.  Should never be more than two1074  // or three entries here.1075  auto &variants = symVariants[CachedHashStringRef(sym->getName())];1076  if (variants.empty())1077    variants.push_back(sym);1078 1079  for (Symbol* v : variants) {1080    if (*v->getSignature() == *sig) {1081      variant = v;1082      break;1083    }1084  }1085 1086  bool wasAdded = !variant;1087  if (wasAdded) {1088    // Create a new variant;1089    LLVM_DEBUG(dbgs() << "added new variant\n");1090    variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());1091    variant->isUsedInRegularObj =1092        !file || file->kind() == InputFile::ObjectKind;1093    variant->canInline = true;1094    variant->traced = false;1095    variant->forceExport = false;1096    variants.push_back(variant);1097  } else {1098    LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n");1099    assert(*variant->getSignature() == *sig);1100  }1101 1102  *out = variant;1103  return wasAdded;1104}1105 1106// Set a flag for --trace-symbol so that we can print out a log message1107// if a new symbol with the same name is inserted into the symbol table.1108void SymbolTable::trace(StringRef name) {1109  symMap.insert({CachedHashStringRef(name), -1});1110}1111 1112void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {1113  // Swap symbols as instructed by -wrap.1114  int &origIdx = symMap[CachedHashStringRef(sym->getName())];1115  int &realIdx= symMap[CachedHashStringRef(real->getName())];1116  int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())];1117  LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n");1118 1119  // Anyone looking up __real symbols should get the original1120  realIdx = origIdx;1121  // Anyone looking up the original should get the __wrap symbol1122  origIdx = wrapIdx;1123}1124 1125static const uint8_t unreachableFn[] = {1126    0x03 /* ULEB length */, 0x00 /* ULEB num locals */,1127    0x00 /* opcode unreachable */, 0x0b /* opcode end */1128};1129 1130// Replace the given symbol body with an unreachable function.1131// This is used by handleWeakUndefines in order to generate a callable1132// equivalent of an undefined function and also handleSymbolVariants for1133// undefined functions that don't match the signature of the definition.1134InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym,1135                                                   const WasmSignature &sig,1136                                                   StringRef debugName) {1137  auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName);1138  func->setBody(unreachableFn);1139  ctx.syntheticFunctions.emplace_back(func);1140  // Mark new symbols as local. For relocatable output we don't want them1141  // to be exported outside the object file.1142  replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL,1143                                 nullptr, func);1144  // Ensure the stub function doesn't get a table entry.  Its address1145  // should always compare equal to the null pointer.1146  sym->isStub = true;1147  return func;1148}1149 1150void SymbolTable::replaceWithUndefined(Symbol *sym) {1151  // Add a synthetic dummy for weak undefined functions.  These dummies will1152  // be GC'd if not used as the target of any "call" instructions.1153  StringRef debugName = saver().save("undefined_weak:" + toString(*sym));1154  replaceWithUnreachable(sym, *sym->getSignature(), debugName);1155  // Hide our dummy to prevent export.1156  sym->setHidden(true);1157}1158 1159// For weak undefined functions, there may be "call" instructions that reference1160// the symbol. In this case, we need to synthesise a dummy/stub function that1161// will abort at runtime, so that relocations can still provided an operand to1162// the call instruction that passes Wasm validation.1163void SymbolTable::handleWeakUndefines() {1164  for (Symbol *sym : symbols()) {1165    if (sym->isUndefWeak() && sym->isUsedInRegularObj) {1166      if (sym->getSignature()) {1167        replaceWithUndefined(sym);1168      } else {1169        // It is possible for undefined functions not to have a signature (eg.1170        // if added via "--undefined"), but weak undefined ones do have a1171        // signature.  Lazy symbols may not be functions and therefore Sig can1172        // still be null in some circumstance.1173        assert(!isa<FunctionSymbol>(sym));1174      }1175    }1176  }1177}1178 1179DefinedFunction *SymbolTable::createUndefinedStub(const WasmSignature &sig) {1180  if (auto it = stubFunctions.find(sig); it != stubFunctions.end())1181    return it->second;1182  LLVM_DEBUG(dbgs() << "createUndefinedStub: " << toString(sig) << "\n");1183  auto *sym = reinterpret_cast<DefinedFunction *>(make<SymbolUnion>());1184  sym->isUsedInRegularObj = true;1185  sym->canInline = true;1186  sym->traced = false;1187  sym->forceExport = false;1188  sym->signature = &sig;1189  replaceSymbol<DefinedFunction>(1190      sym, "undefined_stub", WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, nullptr);1191  replaceWithUnreachable(sym, sig, "undefined_stub");1192  stubFunctions[sig] = sym;1193  return sym;1194}1195 1196// Remove any variant symbols that were created due to function signature1197// mismatches.1198void SymbolTable::handleSymbolVariants() {1199  for (auto pair : symVariants) {1200    // Push the initial symbol onto the list of variants.1201    StringRef symName = pair.first.val();1202    std::vector<Symbol *> &variants = pair.second;1203 1204#ifndef NDEBUG1205    LLVM_DEBUG(dbgs() << "symbol with (" << variants.size()1206                      << ") variants: " << symName << "\n");1207    for (auto *s: variants) {1208      auto *f = cast<FunctionSymbol>(s);1209      LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " "1210                        << toString(*f->signature) << "\n");1211    }1212#endif1213 1214    // Find the one definition.1215    DefinedFunction *defined = nullptr;1216    for (auto *symbol : variants) {1217      if (auto f = dyn_cast<DefinedFunction>(symbol)) {1218        defined = f;1219        break;1220      }1221    }1222 1223    // If there are no definitions, and the undefined symbols disagree on1224    // the signature, there is not we can do since we don't know which one1225    // to use as the signature on the import.1226    if (!defined) {1227      reportFunctionSignatureMismatch(symName,1228                                      cast<FunctionSymbol>(variants[0]),1229                                      cast<FunctionSymbol>(variants[1]));1230      return;1231    }1232 1233    for (auto *symbol : variants) {1234      if (symbol != defined) {1235        auto *f = cast<FunctionSymbol>(symbol);1236        reportFunctionSignatureMismatch(symName, f, defined, false);1237        StringRef debugName =1238            saver().save("signature_mismatch:" + toString(*f));1239        replaceWithUnreachable(f, *f->signature, debugName);1240      }1241    }1242  }1243}1244 1245} // namespace wasm::lld1246