2017-11-18 02:14:09 +08:00
|
|
|
//===- SymbolTable.cpp ----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2018-12-08 14:17:43 +08:00
|
|
|
#include "InputEvent.h"
|
2018-02-23 13:08:53 +08:00
|
|
|
#include "InputGlobal.h"
|
2017-11-30 09:40:08 +08:00
|
|
|
#include "WriterUtils.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2018-02-14 06:30:52 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2018-01-10 08:52:20 +08:00
|
|
|
using namespace llvm::wasm;
|
2018-11-27 09:08:16 +08:00
|
|
|
using namespace llvm::object;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
SymbolTable *symtab;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
void SymbolTable::addFile(InputFile *file) {
|
|
|
|
log("Processing: " + toString(file));
|
2019-06-06 01:50:45 +08:00
|
|
|
|
|
|
|
// .a file
|
|
|
|
if (auto *f = dyn_cast<ArchiveFile>(file)) {
|
|
|
|
f->parse();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// .so file
|
|
|
|
if (auto *f = dyn_cast<SharedFile>(file)) {
|
|
|
|
sharedFiles.push_back(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:35:18 +08:00
|
|
|
if (config->trace)
|
|
|
|
message(toString(file));
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
// LLVM bitcode file
|
2019-06-06 01:50:45 +08:00
|
|
|
if (auto *f = dyn_cast<BitcodeFile>(file)) {
|
|
|
|
f->parse();
|
2018-05-31 02:07:52 +08:00
|
|
|
bitcodeFiles.push_back(f);
|
2019-06-06 01:50:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular object file
|
|
|
|
auto *f = cast<ObjFile>(file);
|
|
|
|
f->parse(false);
|
|
|
|
objectFiles.push_back(f);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
// This function is where all the optimizations of link-time
|
|
|
|
// optimization happens. When LTO is in use, some input files are
|
|
|
|
// not in native object file format but in the LLVM bitcode format.
|
|
|
|
// This function compiles bitcode files into a few big native files
|
|
|
|
// using LLVM functions and replaces bitcode symbols with the results.
|
|
|
|
// Because all bitcode files that the program consists of are passed
|
|
|
|
// to the compiler at once, it can do whole-program optimization.
|
|
|
|
void SymbolTable::addCombinedLTOObject() {
|
2019-12-20 09:30:24 +08:00
|
|
|
// Prevent further LTO objects being included
|
|
|
|
BitcodeFile::doneLTO = true;
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
if (bitcodeFiles.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Compile bitcode files and replace bitcode symbols.
|
|
|
|
lto.reset(new BitcodeCompiler);
|
|
|
|
for (BitcodeFile *f : bitcodeFiles)
|
|
|
|
lto->add(*f);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
for (StringRef filename : lto->compile()) {
|
2019-04-09 13:41:52 +08:00
|
|
|
auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), "");
|
2019-05-16 00:03:28 +08:00
|
|
|
obj->parse(true);
|
2018-05-31 02:07:52 +08:00
|
|
|
objectFiles.push_back(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *SymbolTable::find(StringRef name) {
|
2019-02-06 10:35:18 +08:00
|
|
|
auto it = symMap.find(CachedHashStringRef(name));
|
|
|
|
if (it == symMap.end() || it->second == -1)
|
|
|
|
return nullptr;
|
|
|
|
return symVector[it->second];
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
void SymbolTable::replace(StringRef name, Symbol* sym) {
|
|
|
|
auto it = symMap.find(CachedHashStringRef(name));
|
|
|
|
symVector[it->second] = sym;
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:35:18 +08:00
|
|
|
std::pair<Symbol *, bool> SymbolTable::insertName(StringRef name) {
|
|
|
|
bool trace = false;
|
|
|
|
auto p = symMap.insert({CachedHashStringRef(name), (int)symVector.size()});
|
|
|
|
int &symIndex = p.first->second;
|
|
|
|
bool isNew = p.second;
|
|
|
|
if (symIndex == -1) {
|
|
|
|
symIndex = symVector.size();
|
|
|
|
trace = true;
|
|
|
|
isNew = true;
|
2018-08-03 04:39:19 +08:00
|
|
|
}
|
2019-02-06 10:35:18 +08:00
|
|
|
|
|
|
|
if (!isNew)
|
|
|
|
return {symVector[symIndex], false};
|
|
|
|
|
|
|
|
Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
|
|
|
|
sym->isUsedInRegularObj = false;
|
2019-05-24 22:14:25 +08:00
|
|
|
sym->canInline = true;
|
2019-02-06 10:35:18 +08:00
|
|
|
sym->traced = trace;
|
|
|
|
symVector.emplace_back(sym);
|
|
|
|
return {sym, true};
|
|
|
|
}
|
|
|
|
|
2019-02-08 06:42:16 +08:00
|
|
|
std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
|
|
|
|
const InputFile *file) {
|
2019-02-06 10:35:18 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insertName(name);
|
|
|
|
|
2018-08-03 04:39:19 +08:00
|
|
|
if (!file || file->kind() == InputFile::ObjectKind)
|
2019-02-06 10:35:18 +08:00
|
|
|
s->isUsedInRegularObj = true;
|
|
|
|
|
|
|
|
return {s, wasInserted};
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
static void reportTypeError(const Symbol *existing, const InputFile *file,
|
2018-05-15 06:42:33 +08:00
|
|
|
llvm::wasm::WasmSymbolType type) {
|
2018-02-28 08:09:22 +08:00
|
|
|
error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " +
|
|
|
|
toString(existing->getWasmType()) + " in " +
|
2018-05-15 06:42:33 +08:00
|
|
|
toString(existing->getFile()) + "\n>>> defined as " + toString(type) +
|
|
|
|
" in " + toString(file));
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2018-11-20 07:31:28 +08:00
|
|
|
// Check the type of new symbol matches that of the symbol is replacing.
|
2019-02-21 07:19:31 +08:00
|
|
|
// Returns true if the function types match, false is there is a singature
|
|
|
|
// mismatch.
|
2019-03-29 01:18:42 +08:00
|
|
|
static bool signatureMatches(FunctionSymbol *existing,
|
|
|
|
const WasmSignature *newSig) {
|
2019-02-21 07:19:31 +08:00
|
|
|
const WasmSignature *oldSig = existing->signature;
|
2019-05-29 23:36:42 +08:00
|
|
|
|
|
|
|
// If either function is missing a signature (this happend for bitcode
|
|
|
|
// symbols) then assume they match. Any mismatch will be reported later
|
|
|
|
// when the LTO objects are added.
|
|
|
|
if (!newSig || !oldSig)
|
2019-02-21 07:19:31 +08:00
|
|
|
return true;
|
2018-06-29 00:53:53 +08:00
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
return *newSig == *oldSig;
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
static void checkGlobalType(const Symbol *existing, const InputFile *file,
|
|
|
|
const WasmGlobalType *newType) {
|
|
|
|
if (!isa<GlobalSymbol>(existing)) {
|
2018-05-15 06:42:33 +08:00
|
|
|
reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL);
|
2018-02-28 08:09:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
const WasmGlobalType *oldType = cast<GlobalSymbol>(existing)->getGlobalType();
|
|
|
|
if (*newType != *oldType) {
|
|
|
|
error("Global type mismatch: " + existing->getName() + "\n>>> defined as " +
|
|
|
|
toString(*oldType) + " in " + toString(existing->getFile()) +
|
|
|
|
"\n>>> defined as " + toString(*newType) + " in " + toString(file));
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
2018-01-29 03:57:01 +08:00
|
|
|
}
|
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
static void checkEventType(const Symbol *existing, const InputFile *file,
|
|
|
|
const WasmEventType *newType,
|
|
|
|
const WasmSignature *newSig) {
|
|
|
|
auto existingEvent = dyn_cast<EventSymbol>(existing);
|
|
|
|
if (!isa<EventSymbol>(existing)) {
|
|
|
|
reportTypeError(existing, file, WASM_SYMBOL_TYPE_EVENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WasmEventType *oldType = cast<EventSymbol>(existing)->getEventType();
|
|
|
|
const WasmSignature *oldSig = existingEvent->signature;
|
|
|
|
if (newType->Attribute != oldType->Attribute)
|
|
|
|
error("Event type mismatch: " + existing->getName() + "\n>>> defined as " +
|
|
|
|
toString(*oldType) + " in " + toString(existing->getFile()) +
|
|
|
|
"\n>>> defined as " + toString(*newType) + " in " + toString(file));
|
|
|
|
if (*newSig != *oldSig)
|
|
|
|
warn("Event signature mismatch: " + existing->getName() +
|
|
|
|
"\n>>> defined as " + toString(*oldSig) + " in " +
|
|
|
|
toString(existing->getFile()) + "\n>>> defined as " +
|
|
|
|
toString(*newSig) + " in " + toString(file));
|
|
|
|
}
|
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
static void checkDataType(const Symbol *existing, const InputFile *file) {
|
|
|
|
if (!isa<DataSymbol>(existing))
|
2018-05-15 06:42:33 +08:00
|
|
|
reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA);
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
|
2018-03-10 00:43:05 +08:00
|
|
|
uint32_t flags,
|
|
|
|
InputFunction *function) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n");
|
2018-03-01 06:51:51 +08:00
|
|
|
assert(!find(name));
|
2018-03-10 00:43:05 +08:00
|
|
|
syntheticFunctions.emplace_back(function);
|
2019-03-09 05:10:48 +08:00
|
|
|
return replaceSymbol<DefinedFunction>(insertName(name).first, name,
|
2018-08-03 04:39:19 +08:00
|
|
|
flags, nullptr, function);
|
2018-01-13 02:35:13 +08:00
|
|
|
}
|
|
|
|
|
2019-06-05 00:35:23 +08:00
|
|
|
// Adds an optional, linker generated, data symbols. The symbol will only be
|
2020-01-07 02:21:05 +08:00
|
|
|
// added if there is an undefine reference to it, or if it is explicitly
|
|
|
|
// exported via the --export flag. Otherwise we don't add the symbol and return
|
|
|
|
// nullptr.
|
2019-08-09 00:58:36 +08:00
|
|
|
DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,
|
|
|
|
uint32_t value) {
|
2019-05-23 18:06:03 +08:00
|
|
|
Symbol *s = find(name);
|
2019-06-01 06:51:59 +08:00
|
|
|
if (!s && (config->exportAll || config->exportedSymbols.count(name) != 0))
|
|
|
|
s = insertName(name).first;
|
|
|
|
else if (!s || s->isDefined())
|
2019-05-23 18:06:03 +08:00
|
|
|
return nullptr;
|
|
|
|
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
|
2019-08-09 00:58:36 +08:00
|
|
|
auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN);
|
2019-05-23 18:06:03 +08:00
|
|
|
rtn->setVirtualAddress(value);
|
2019-07-08 18:35:08 +08:00
|
|
|
rtn->referenced = true;
|
2019-05-23 18:06:03 +08:00
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name,
|
|
|
|
uint32_t flags) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n");
|
2018-03-01 06:51:51 +08:00
|
|
|
assert(!find(name));
|
2019-03-09 05:10:48 +08:00
|
|
|
return replaceSymbol<DefinedData>(insertName(name).first, name, flags);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags,
|
|
|
|
InputGlobal *global) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global
|
|
|
|
<< "\n");
|
2018-03-01 06:51:51 +08:00
|
|
|
assert(!find(name));
|
2018-03-10 00:43:05 +08:00
|
|
|
syntheticGlobals.emplace_back(global);
|
2019-03-09 05:10:48 +08:00
|
|
|
return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags,
|
2018-08-03 04:39:19 +08:00
|
|
|
nullptr, global);
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
static bool shouldReplace(const Symbol *existing, InputFile *newFile,
|
|
|
|
uint32_t newFlags) {
|
2018-02-21 05:08:47 +08:00
|
|
|
// If existing symbol is undefined, replace it.
|
2018-02-28 08:09:22 +08:00
|
|
|
if (!existing->isDefined()) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
|
|
|
|
<< existing->getName() << "\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have two defined symbols. If the new one is weak, we can ignore it.
|
|
|
|
if ((newFlags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "existing symbol takes precedence\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
return false;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 05:08:47 +08:00
|
|
|
// If the existing symbol is weak, we should replace it.
|
2018-02-28 08:09:22 +08:00
|
|
|
if (existing->isWeak()) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "replacing existing weak symbol\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-21 02:55:06 +08:00
|
|
|
|
2018-02-21 05:08:47 +08:00
|
|
|
// Neither symbol is week. They conflict.
|
2018-02-28 08:09:22 +08:00
|
|
|
error("duplicate symbol: " + toString(*existing) + "\n>>> defined in " +
|
|
|
|
toString(existing->getFile()) + "\n>>> defined in " +
|
|
|
|
toString(newFile));
|
2018-02-21 05:08:47 +08:00
|
|
|
return true;
|
2018-02-21 02:55:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags,
|
2018-02-28 08:09:22 +08:00
|
|
|
InputFile *file,
|
|
|
|
InputFunction *function) {
|
2018-09-29 00:50:14 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " ["
|
|
|
|
<< (function ? toString(function->signature) : "none")
|
|
|
|
<< "]\n");
|
2018-02-21 02:55:06 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2018-08-03 04:39:19 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&](Symbol *sym) {
|
2019-02-21 07:19:31 +08:00
|
|
|
// If the new defined function doesn't have signture (i.e. bitcode
|
|
|
|
// functions) but the old symbol does, then preserve the old signature
|
|
|
|
const WasmSignature *oldSig = s->getSignature();
|
|
|
|
auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function);
|
|
|
|
if (!newSym->signature)
|
|
|
|
newSym->signature = oldSig;
|
|
|
|
};
|
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
if (wasInserted || s->isLazy()) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym(s);
|
2018-02-28 08:09:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
auto existingFunction = dyn_cast<FunctionSymbol>(s);
|
|
|
|
if (!existingFunction) {
|
|
|
|
reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
|
|
|
|
return s;
|
|
|
|
}
|
2018-02-28 08:09:22 +08:00
|
|
|
|
2019-05-25 06:45:08 +08:00
|
|
|
bool checkSig = true;
|
|
|
|
if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))
|
|
|
|
checkSig = ud->isCalledDirectly;
|
|
|
|
|
|
|
|
if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) {
|
2019-02-21 07:19:31 +08:00
|
|
|
Symbol* variant;
|
|
|
|
if (getFunctionVariant(s, &function->signature, file, &variant))
|
|
|
|
// New variant, always replace
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym(variant);
|
2019-02-21 07:19:31 +08:00
|
|
|
else if (shouldReplace(s, file, flags))
|
|
|
|
// Variant already exists, replace it after checking shouldReplace
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym(variant);
|
2019-02-21 07:19:31 +08:00
|
|
|
|
|
|
|
// This variant we found take the place in the symbol table as the primary
|
|
|
|
// variant.
|
|
|
|
replace(name, variant);
|
|
|
|
return variant;
|
2018-09-29 00:50:14 +08:00
|
|
|
}
|
2019-02-21 07:19:31 +08:00
|
|
|
|
|
|
|
// Existing function with matching signature.
|
|
|
|
if (shouldReplace(s, file, flags))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym(s);
|
2019-02-21 07:19:31 +08:00
|
|
|
|
2018-02-21 02:55:06 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags,
|
2018-02-28 08:09:22 +08:00
|
|
|
InputFile *file, InputSegment *segment,
|
2018-02-23 13:08:53 +08:00
|
|
|
uint32_t address, uint32_t size) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address
|
|
|
|
<< "\n");
|
2018-02-21 02:55:06 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2018-08-03 04:39:19 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
2018-02-28 08:09:22 +08:00
|
|
|
replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (wasInserted || s->isLazy()) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-02-28 08:09:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDataType(s, file);
|
|
|
|
|
|
|
|
if (shouldReplace(s, file, flags))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2017-11-18 02:14:09 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags,
|
2018-02-28 08:09:22 +08:00
|
|
|
InputFile *file, InputGlobal *global) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n");
|
2018-08-03 04:39:19 +08:00
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2018-08-03 04:39:19 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
2018-02-28 08:09:22 +08:00
|
|
|
replaceSymbol<DefinedGlobal>(s, name, flags, file, global);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (wasInserted || s->isLazy()) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-02-28 08:09:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkGlobalType(s, file, &global->getType());
|
|
|
|
|
|
|
|
if (shouldReplace(s, file, flags))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-02-23 13:08:53 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
Symbol *SymbolTable::addDefinedEvent(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file, InputEvent *event) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedEvent:" << name << "\n");
|
|
|
|
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
2018-12-08 14:17:43 +08:00
|
|
|
replaceSymbol<DefinedEvent>(s, name, flags, file, event);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (wasInserted || s->isLazy()) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-12-08 14:17:43 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkEventType(s, file, &event->getType(), &event->signature);
|
|
|
|
|
|
|
|
if (shouldReplace(s, file, flags))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-12-08 14:17:43 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-10-17 13:16:54 +08:00
|
|
|
// This function get called when an undefined symbol is added, and there is
|
|
|
|
// already an existing one in the symbols table. In this case we check that
|
|
|
|
// custom 'import-module' and 'import-field' symbol attributes agree.
|
2020-01-07 02:21:05 +08:00
|
|
|
// With LTO these attributes are not available when the bitcode is read and only
|
2019-10-17 13:16:54 +08:00
|
|
|
// become available when the LTO object is read. In this case we silently
|
|
|
|
// replace the empty attributes with the valid ones.
|
|
|
|
template <typename T>
|
2020-02-06 13:18:55 +08:00
|
|
|
static void setImportAttributes(T *existing, Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
2020-02-28 09:32:22 +08:00
|
|
|
uint32_t flags, InputFile *file) {
|
2020-02-06 13:18:55 +08:00
|
|
|
if (importName) {
|
|
|
|
if (!existing->importName)
|
2019-10-17 13:16:54 +08:00
|
|
|
existing->importName = importName;
|
|
|
|
if (existing->importName != importName)
|
|
|
|
error("import name mismatch for symbol: " + toString(*existing) +
|
2020-02-06 13:18:55 +08:00
|
|
|
"\n>>> defined as " + *existing->importName + " in " +
|
|
|
|
toString(existing->getFile()) + "\n>>> defined as " + *importName +
|
2019-10-17 13:16:54 +08:00
|
|
|
" in " + toString(file));
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:18:55 +08:00
|
|
|
if (importModule) {
|
|
|
|
if (!existing->importModule)
|
2019-10-17 13:16:54 +08:00
|
|
|
existing->importModule = importModule;
|
|
|
|
if (existing->importModule != importModule)
|
|
|
|
error("import module mismatch for symbol: " + toString(*existing) +
|
2020-02-06 13:18:55 +08:00
|
|
|
"\n>>> defined as " + *existing->importModule + " in " +
|
|
|
|
toString(existing->getFile()) + "\n>>> defined as " +
|
|
|
|
*importModule + " in " + toString(file));
|
2019-10-17 13:16:54 +08:00
|
|
|
}
|
2020-02-28 09:32:22 +08:00
|
|
|
|
|
|
|
// Update symbol binding, if the existing symbol is weak
|
|
|
|
uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK;
|
|
|
|
if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) {
|
|
|
|
existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding;
|
|
|
|
}
|
2019-10-17 13:16:54 +08:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:18:55 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedFunction(StringRef name,
|
|
|
|
Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
2019-02-01 10:29:57 +08:00
|
|
|
uint32_t flags, InputFile *file,
|
2019-05-25 06:45:08 +08:00
|
|
|
const WasmSignature *sig,
|
|
|
|
bool isCalledDirectly) {
|
2019-05-29 23:36:42 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " ["
|
|
|
|
<< (sig ? toString(*sig) : "none")
|
2020-02-28 09:32:22 +08:00
|
|
|
<< "] IsCalledDirectly:" << isCalledDirectly << " flags=0x"
|
|
|
|
<< utohexstr(flags) << "\n");
|
2019-09-24 05:28:29 +08:00
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2018-08-03 04:39:19 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2019-05-24 21:29:17 +08:00
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
2019-02-08 06:00:48 +08:00
|
|
|
replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
|
2019-05-25 06:45:08 +08:00
|
|
|
file, sig, isCalledDirectly);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (wasInserted)
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-02-28 08:09:22 +08:00
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
2018-03-01 06:51:51 +08:00
|
|
|
lazy->fetch();
|
2019-02-21 07:19:31 +08:00
|
|
|
else {
|
|
|
|
auto existingFunction = dyn_cast<FunctionSymbol>(s);
|
|
|
|
if (!existingFunction) {
|
|
|
|
reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
|
|
|
|
return s;
|
|
|
|
}
|
2019-05-29 23:36:42 +08:00
|
|
|
if (!existingFunction->signature && sig)
|
|
|
|
existingFunction->signature = sig;
|
2020-02-28 09:32:22 +08:00
|
|
|
auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction);
|
2019-08-31 03:50:59 +08:00
|
|
|
if (isCalledDirectly && !signatureMatches(existingFunction, sig)) {
|
2020-02-28 09:32:22 +08:00
|
|
|
// If the existing undefined functions is not called directly then let
|
2019-08-31 03:50:59 +08:00
|
|
|
// this one take precedence. Otherwise the existing function is either
|
2020-02-28 09:32:22 +08:00
|
|
|
// directly called or defined, in which case we need a function variant.
|
2019-08-31 03:50:59 +08:00
|
|
|
if (existingUndefined && !existingUndefined->isCalledDirectly)
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2019-08-31 03:50:59 +08:00
|
|
|
else if (getFunctionVariant(s, sig, file, &s))
|
|
|
|
replaceSym();
|
|
|
|
}
|
2019-10-17 13:16:54 +08:00
|
|
|
if (existingUndefined)
|
2020-02-28 09:32:22 +08:00
|
|
|
setImportAttributes(existingUndefined, importName, importModule, flags,
|
|
|
|
file);
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
2018-06-29 00:53:53 +08:00
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n");
|
2019-09-24 05:28:29 +08:00
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2018-08-03 04:39:19 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2019-05-24 21:29:17 +08:00
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
2018-07-18 03:15:02 +08:00
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<UndefinedData>(s, name, flags, file);
|
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
2018-03-01 06:51:51 +08:00
|
|
|
lazy->fetch();
|
2018-02-28 08:09:22 +08:00
|
|
|
else if (s->isDefined())
|
|
|
|
checkDataType(s, file);
|
|
|
|
return s;
|
|
|
|
}
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2020-02-06 13:18:55 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
|
|
|
|
Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
2018-02-28 08:09:22 +08:00
|
|
|
const WasmGlobalType *type) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");
|
2019-09-24 05:28:29 +08:00
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2018-08-03 04:39:19 +08:00
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2019-05-24 21:29:17 +08:00
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
if (wasInserted)
|
2019-02-08 06:00:48 +08:00
|
|
|
replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,
|
|
|
|
file, type);
|
2018-02-28 08:09:22 +08:00
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
2018-03-01 06:51:51 +08:00
|
|
|
lazy->fetch();
|
2018-02-28 08:09:22 +08:00
|
|
|
else if (s->isDefined())
|
|
|
|
checkGlobalType(s, file, type);
|
2017-11-18 02:14:09 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-02-28 08:09:22 +08:00
|
|
|
void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
StringRef name = sym->getName();
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
2019-03-09 05:10:48 +08:00
|
|
|
std::tie(s, wasInserted) = insertName(name);
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
if (wasInserted) {
|
2019-01-30 06:26:31 +08:00
|
|
|
replaceSymbol<LazySymbol>(s, name, 0, file, *sym);
|
2018-02-21 05:08:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-30 06:26:31 +08:00
|
|
|
if (!s->isUndefined())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The existing symbol is undefined, load a new one from the archive,
|
2020-01-07 02:21:05 +08:00
|
|
|
// unless the existing symbol is weak in which case replace the undefined
|
2019-01-30 06:26:31 +08:00
|
|
|
// symbols with a LazySymbol.
|
|
|
|
if (s->isWeak()) {
|
|
|
|
const WasmSignature *oldSig = nullptr;
|
|
|
|
// In the case of an UndefinedFunction we need to preserve the expected
|
|
|
|
// signature.
|
|
|
|
if (auto *f = dyn_cast<UndefinedFunction>(s))
|
|
|
|
oldSig = f->signature;
|
|
|
|
LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
|
|
|
|
auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK,
|
|
|
|
file, *sym);
|
|
|
|
newSym->signature = oldSig;
|
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2019-01-30 06:26:31 +08:00
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
|
|
|
|
file->addMember(sym);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2018-01-13 06:25:17 +08:00
|
|
|
|
2018-03-14 23:45:11 +08:00
|
|
|
bool SymbolTable::addComdat(StringRef name) {
|
2019-05-16 00:03:28 +08:00
|
|
|
return comdatGroups.insert(CachedHashStringRef(name)).second;
|
2018-01-13 06:25:17 +08:00
|
|
|
}
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
// The new signature doesn't match. Create a variant to the symbol with the
|
|
|
|
// signature encoded in the name and return that instead. These symbols are
|
|
|
|
// then unified later in handleSymbolVariants.
|
|
|
|
bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,
|
|
|
|
const InputFile *file, Symbol **out) {
|
2019-02-21 08:36:14 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> "
|
2019-02-21 07:19:31 +08:00
|
|
|
<< " " << toString(*sig) << "\n");
|
|
|
|
Symbol *variant = nullptr;
|
|
|
|
|
|
|
|
// Linear search through symbol variants. Should never be more than two
|
|
|
|
// or three entries here.
|
|
|
|
auto &variants = symVariants[CachedHashStringRef(sym->getName())];
|
2019-04-18 21:33:29 +08:00
|
|
|
if (variants.empty())
|
2019-02-21 07:19:31 +08:00
|
|
|
variants.push_back(sym);
|
|
|
|
|
|
|
|
for (Symbol* v : variants) {
|
|
|
|
if (*v->getSignature() == *sig) {
|
|
|
|
variant = v;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wasAdded = !variant;
|
|
|
|
if (wasAdded) {
|
|
|
|
// Create a new variant;
|
|
|
|
LLVM_DEBUG(dbgs() << "added new variant\n");
|
|
|
|
variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
|
|
|
|
variants.push_back(variant);
|
|
|
|
} else {
|
|
|
|
LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n");
|
|
|
|
assert(*variant->getSignature() == *sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
*out = variant;
|
|
|
|
return wasAdded;
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:35:18 +08:00
|
|
|
// Set a flag for --trace-symbol so that we can print out a log message
|
|
|
|
// if a new symbol with the same name is inserted into the symbol table.
|
|
|
|
void SymbolTable::trace(StringRef name) {
|
|
|
|
symMap.insert({CachedHashStringRef(name), -1});
|
|
|
|
}
|
2019-02-08 06:42:16 +08:00
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
|
|
|
|
// Swap symbols as instructed by -wrap.
|
|
|
|
int &origIdx = symMap[CachedHashStringRef(sym->getName())];
|
|
|
|
int &realIdx= symMap[CachedHashStringRef(real->getName())];
|
|
|
|
int &wrapIdx = symMap[CachedHashStringRef(wrap->getName())];
|
|
|
|
LLVM_DEBUG(dbgs() << "wrap: " << sym->getName() << "\n");
|
|
|
|
|
|
|
|
// Anyone looking up __real symbols should get the original
|
|
|
|
realIdx = origIdx;
|
|
|
|
// Anyone looking up the original should get the __wrap symbol
|
|
|
|
origIdx = wrapIdx;
|
|
|
|
}
|
|
|
|
|
2019-02-08 06:42:16 +08:00
|
|
|
static const uint8_t unreachableFn[] = {
|
|
|
|
0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
|
|
|
|
0x00 /* opcode unreachable */, 0x0b /* opcode end */
|
|
|
|
};
|
|
|
|
|
|
|
|
// Replace the given symbol body with an unreachable function.
|
|
|
|
// This is used by handleWeakUndefines in order to generate a callable
|
2019-02-21 07:19:31 +08:00
|
|
|
// equivalent of an undefined function and also handleSymbolVariants for
|
|
|
|
// undefined functions that don't match the signature of the definition.
|
2019-02-08 06:42:16 +08:00
|
|
|
InputFunction *SymbolTable::replaceWithUnreachable(Symbol *sym,
|
|
|
|
const WasmSignature &sig,
|
|
|
|
StringRef debugName) {
|
|
|
|
auto *func = make<SyntheticFunction>(sig, sym->getName(), debugName);
|
|
|
|
func->setBody(unreachableFn);
|
|
|
|
syntheticFunctions.emplace_back(func);
|
2020-02-28 09:32:22 +08:00
|
|
|
replaceSymbol<DefinedFunction>(sym, sym->getName(), sym->flags, nullptr,
|
2019-02-21 07:19:31 +08:00
|
|
|
func);
|
2019-02-08 06:42:16 +08:00
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For weak undefined functions, there may be "call" instructions that reference
|
|
|
|
// the symbol. In this case, we need to synthesise a dummy/stub function that
|
|
|
|
// will abort at runtime, so that relocations can still provided an operand to
|
|
|
|
// the call instruction that passes Wasm validation.
|
|
|
|
void SymbolTable::handleWeakUndefines() {
|
|
|
|
for (Symbol *sym : getSymbols()) {
|
|
|
|
if (!sym->isUndefWeak())
|
|
|
|
continue;
|
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
const WasmSignature *sig = sym->getSignature();
|
|
|
|
if (!sig) {
|
2019-02-08 06:42:16 +08:00
|
|
|
// It is possible for undefined functions not to have a signature (eg. if
|
|
|
|
// added via "--undefined"), but weak undefined ones do have a signature.
|
2019-02-21 07:19:31 +08:00
|
|
|
// Lazy symbols may not be functions and therefore Sig can still be null
|
2020-01-07 02:21:05 +08:00
|
|
|
// in some circumstance.
|
2019-02-21 07:19:31 +08:00
|
|
|
assert(!isa<FunctionSymbol>(sym));
|
2019-02-08 06:42:16 +08:00
|
|
|
continue;
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
2019-02-08 06:42:16 +08:00
|
|
|
|
|
|
|
// Add a synthetic dummy for weak undefined functions. These dummies will
|
|
|
|
// be GC'd if not used as the target of any "call" instructions.
|
|
|
|
StringRef debugName = saver.save("undefined:" + toString(*sym));
|
|
|
|
InputFunction* func = replaceWithUnreachable(sym, *sig, debugName);
|
|
|
|
// Ensure it compares equal to the null pointer, and so that table relocs
|
|
|
|
// don't pull in the stub body (only call-operand relocs should do that).
|
|
|
|
func->setTableIndex(0);
|
|
|
|
// Hide our dummy to prevent export.
|
|
|
|
sym->setHidden(true);
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 07:19:31 +08:00
|
|
|
|
|
|
|
static void reportFunctionSignatureMismatch(StringRef symName,
|
|
|
|
FunctionSymbol *a,
|
2019-07-10 17:10:01 +08:00
|
|
|
FunctionSymbol *b, bool isError) {
|
2019-02-21 07:19:31 +08:00
|
|
|
std::string msg = ("function signature mismatch: " + symName +
|
|
|
|
"\n>>> defined as " + toString(*a->signature) + " in " +
|
|
|
|
toString(a->getFile()) + "\n>>> defined as " +
|
|
|
|
toString(*b->signature) + " in " + toString(b->getFile()))
|
|
|
|
.str();
|
2019-07-10 17:10:01 +08:00
|
|
|
if (isError)
|
2019-02-21 07:19:31 +08:00
|
|
|
error(msg);
|
|
|
|
else
|
|
|
|
warn(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any variant symbols that were created due to function signature
|
|
|
|
// mismatches.
|
|
|
|
void SymbolTable::handleSymbolVariants() {
|
|
|
|
for (auto pair : symVariants) {
|
|
|
|
// Push the initial symbol onto the list of variants.
|
|
|
|
StringRef symName = pair.first.val();
|
|
|
|
std::vector<Symbol *> &variants = pair.second;
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2019-02-21 09:33:26 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "symbol with (" << variants.size()
|
|
|
|
<< ") variants: " << symName << "\n");
|
2019-02-21 07:19:31 +08:00
|
|
|
for (auto *s: variants) {
|
|
|
|
auto *f = cast<FunctionSymbol>(s);
|
2019-02-21 09:33:26 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " "
|
|
|
|
<< toString(*f->signature) << "\n");
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Find the one definition.
|
|
|
|
DefinedFunction *defined = nullptr;
|
|
|
|
for (auto *symbol : variants) {
|
|
|
|
if (auto f = dyn_cast<DefinedFunction>(symbol)) {
|
|
|
|
defined = f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no definitions, and the undefined symbols disagree on
|
|
|
|
// the signature, there is not we can do since we don't know which one
|
|
|
|
// to use as the signature on the import.
|
|
|
|
if (!defined) {
|
|
|
|
reportFunctionSignatureMismatch(symName,
|
|
|
|
cast<FunctionSymbol>(variants[0]),
|
|
|
|
cast<FunctionSymbol>(variants[1]), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *symbol : variants) {
|
|
|
|
if (symbol != defined) {
|
|
|
|
auto *f = cast<FunctionSymbol>(symbol);
|
|
|
|
reportFunctionSignatureMismatch(symName, f, defined, false);
|
2020-01-17 06:36:44 +08:00
|
|
|
StringRef debugName = saver.save("signature_mismatch:" + toString(*f));
|
2019-02-21 07:19:31 +08:00
|
|
|
replaceWithUnreachable(f, *f->signature, debugName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 13:25:39 +08:00
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|