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
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
SymbolTable *lld::wasm::symtab;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void SymbolTable::addFile(InputFile *file) {
|
|
|
|
log("Processing: " + toString(file));
|
2019-06-06 01:50:45 +08:00
|
|
|
|
|
|
|
// .a file
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<ArchiveFile>(file)) {
|
|
|
|
f->parse();
|
2019-06-06 01:50:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// .so file
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<SharedFile>(file)) {
|
|
|
|
sharedFiles.push_back(f);
|
2019-06-06 01:50:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +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-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<BitcodeFile>(file)) {
|
|
|
|
f->parse();
|
|
|
|
bitcodeFiles.push_back(f);
|
2019-06-06 01:50:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular object file
|
2019-07-11 13:40:30 +08:00
|
|
|
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-07-11 13:40:30 +08:00
|
|
|
if (bitcodeFiles.empty())
|
2018-05-31 02:07:52 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Compile bitcode files and replace bitcode symbols.
|
2019-07-11 13:40:30 +08:00
|
|
|
lto.reset(new BitcodeCompiler);
|
|
|
|
for (BitcodeFile *f : bitcodeFiles)
|
|
|
|
lto->add(*f);
|
|
|
|
|
|
|
|
for (StringRef filename : lto->compile()) {
|
|
|
|
auto *obj = make<ObjFile>(MemoryBufferRef(filename, "lto.tmp"), "");
|
|
|
|
obj->parse(true);
|
|
|
|
objectFiles.push_back(obj);
|
2018-05-31 02:07:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::find(StringRef name) {
|
|
|
|
auto it = symMap.find(CachedHashStringRef(name));
|
|
|
|
if (it == symMap.end() || it->second == -1)
|
2019-02-06 10:35:18 +08:00
|
|
|
return nullptr;
|
2019-07-11 13:40:30 +08:00
|
|
|
return symVector[it->second];
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void SymbolTable::replace(StringRef name, Symbol* sym) {
|
|
|
|
auto it = symMap.find(CachedHashStringRef(name));
|
|
|
|
symVector[it->second] = sym;
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +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
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!isNew)
|
|
|
|
return {symVector[symIndex], false};
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
|
|
|
|
sym->isUsedInRegularObj = false;
|
|
|
|
sym->canInline = true;
|
|
|
|
sym->traced = trace;
|
|
|
|
symVector.emplace_back(sym);
|
|
|
|
return {sym, true};
|
2019-02-06 10:35:18 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::pair<Symbol *, bool> SymbolTable::insert(StringRef name,
|
|
|
|
const InputFile *file) {
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insertName(name);
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!file || file->kind() == InputFile::ObjectKind)
|
|
|
|
s->isUsedInRegularObj = true;
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return {s, wasInserted};
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void reportTypeError(const Symbol *existing, const InputFile *file,
|
|
|
|
llvm::wasm::WasmSymbolType type) {
|
|
|
|
error("symbol type mismatch: " + toString(*existing) + "\n>>> defined as " +
|
|
|
|
toString(existing->getWasmType()) + " in " +
|
|
|
|
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-07-11 13:40:30 +08:00
|
|
|
static bool signatureMatches(FunctionSymbol *existing,
|
|
|
|
const WasmSignature *newSig) {
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!newSig || !oldSig)
|
2019-02-21 07:19:31 +08:00
|
|
|
return true;
|
2018-06-29 00:53:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return *newSig == *oldSig;
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void checkGlobalType(const Symbol *existing, const InputFile *file,
|
|
|
|
const WasmGlobalType *newType) {
|
|
|
|
if (!isa<GlobalSymbol>(existing)) {
|
|
|
|
reportTypeError(existing, file, WASM_SYMBOL_TYPE_GLOBAL);
|
2018-02-28 08:09:22 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +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
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +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);
|
2018-12-08 14:17:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
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-12-08 14:17:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void checkDataType(const Symbol *existing, const InputFile *file) {
|
|
|
|
if (!isa<DataSymbol>(existing))
|
|
|
|
reportTypeError(existing, file, WASM_SYMBOL_TYPE_DATA);
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedFunction *SymbolTable::addSyntheticFunction(StringRef name,
|
|
|
|
uint32_t flags,
|
|
|
|
InputFunction *function) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << name << "\n");
|
|
|
|
assert(!find(name));
|
|
|
|
syntheticFunctions.emplace_back(function);
|
|
|
|
return replaceSymbol<DefinedFunction>(insertName(name).first, name,
|
|
|
|
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
|
|
|
|
// added if there is an undefine reference to it, or if it is explictly 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-07-11 13:40:30 +08:00
|
|
|
Symbol *s = find(name);
|
|
|
|
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;
|
2019-07-11 13:40:30 +08:00
|
|
|
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-07-11 13:40:30 +08:00
|
|
|
rtn->setVirtualAddress(value);
|
|
|
|
rtn->referenced = true;
|
2019-05-23 18:06:03 +08:00
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef name,
|
|
|
|
uint32_t flags) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << name << "\n");
|
|
|
|
assert(!find(name));
|
|
|
|
return replaceSymbol<DefinedData>(insertName(name).first, name, flags);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef name, uint32_t flags,
|
|
|
|
InputGlobal *global) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addSyntheticGlobal: " << name << " -> " << global
|
2018-05-15 21:36:20 +08:00
|
|
|
<< "\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(!find(name));
|
|
|
|
syntheticGlobals.emplace_back(global);
|
|
|
|
return replaceSymbol<DefinedGlobal>(insertName(name).first, name, flags,
|
|
|
|
nullptr, global);
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +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.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!existing->isDefined()) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "resolving existing undefined symbol: "
|
2019-07-11 13:40:30 +08:00
|
|
|
<< 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.
|
2019-07-11 13:40:30 +08:00
|
|
|
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.
|
2019-07-11 13:40:30 +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.
|
2019-07-11 13:40:30 +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
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::addDefinedFunction(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file,
|
|
|
|
InputFunction *function) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedFunction: " << name << " ["
|
|
|
|
<< (function ? toString(function->signature) : "none")
|
2018-09-29 00:50:14 +08:00
|
|
|
<< "]\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-11 13:40:30 +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
|
2019-07-11 13:40:30 +08:00
|
|
|
const WasmSignature *oldSig = s->getSignature();
|
|
|
|
auto* newSym = replaceSymbol<DefinedFunction>(sym, name, flags, file, function);
|
|
|
|
if (!newSym->signature)
|
|
|
|
newSym->signature = oldSig;
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted || s->isLazy()) {
|
|
|
|
replaceSym(s);
|
|
|
|
return s;
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
auto existingFunction = dyn_cast<FunctionSymbol>(s);
|
|
|
|
if (!existingFunction) {
|
|
|
|
reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
|
|
|
|
return s;
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
2018-02-28 08:09:22 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
bool checkSig = true;
|
|
|
|
if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))
|
|
|
|
checkSig = ud->isCalledDirectly;
|
2019-05-25 06:45:08 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) {
|
|
|
|
Symbol* variant;
|
|
|
|
if (getFunctionVariant(s, &function->signature, file, &variant))
|
2019-02-21 07:19:31 +08:00
|
|
|
// New variant, always replace
|
2019-07-11 13:40:30 +08:00
|
|
|
replaceSym(variant);
|
|
|
|
else if (shouldReplace(s, file, flags))
|
2019-02-21 07:19:31 +08:00
|
|
|
// Variant already exists, replace it after checking shouldReplace
|
2019-07-11 13:40:30 +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.
|
2019-07-11 13:40:30 +08:00
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (shouldReplace(s, file, flags))
|
|
|
|
replaceSym(s);
|
2019-02-21 07:19:31 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return s;
|
2018-02-21 02:55:06 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::addDefinedData(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file, InputSegment *segment,
|
|
|
|
uint32_t address, uint32_t size) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedData:" << name << " addr:" << address
|
2018-05-15 21:36:20 +08:00
|
|
|
<< "\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
auto replaceSym = [&]() {
|
|
|
|
replaceSymbol<DefinedData>(s, name, flags, file, segment, address, size);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted || s->isLazy()) {
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
checkDataType(s, file);
|
2018-02-28 08:09:22 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (shouldReplace(s, file, flags))
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::addDefinedGlobal(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file, InputGlobal *global) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << name << "\n");
|
2018-08-03 04:39:19 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
auto replaceSym = [&]() {
|
|
|
|
replaceSymbol<DefinedGlobal>(s, name, flags, file, global);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted || s->isLazy()) {
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
checkGlobalType(s, file, &global->getType());
|
2018-02-28 08:09:22 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (shouldReplace(s, file, flags))
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::addDefinedEvent(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file, InputEvent *event) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedEvent:" << name << "\n");
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
auto replaceSym = [&]() {
|
|
|
|
replaceSymbol<DefinedEvent>(s, name, flags, file, event);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted || s->isLazy()) {
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
2018-12-08 14:17:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
checkEventType(s, file, &event->getType(), &event->signature);
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (shouldReplace(s, file, flags))
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
2018-12-08 14:17:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedFunction(StringRef name, StringRef importName,
|
|
|
|
StringRef importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
|
|
|
const WasmSignature *sig,
|
|
|
|
bool isCalledDirectly) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " ["
|
|
|
|
<< (sig ? toString(*sig) : "none")
|
|
|
|
<< "] IsCalledDirectly:" << isCalledDirectly << "\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
auto replaceSym = [&]() {
|
|
|
|
replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
|
|
|
|
file, sig, isCalledDirectly);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted)
|
|
|
|
replaceSym();
|
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
|
|
|
lazy->fetch();
|
2019-02-21 07:19:31 +08:00
|
|
|
else {
|
2019-07-11 13:40:30 +08:00
|
|
|
auto existingFunction = dyn_cast<FunctionSymbol>(s);
|
|
|
|
if (!existingFunction) {
|
|
|
|
reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
|
|
|
|
return s;
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!existingFunction->signature && sig)
|
|
|
|
existingFunction->signature = sig;
|
2019-08-31 03:50:59 +08:00
|
|
|
if (isCalledDirectly && !signatureMatches(existingFunction, sig)) {
|
|
|
|
auto* existingUndefined = dyn_cast<UndefinedFunction>(existingFunction);
|
|
|
|
// If the existing undefined functions is not called direcltly then let
|
|
|
|
// this one take precedence. Otherwise the existing function is either
|
|
|
|
// direclty called or defined, in which case we need a function variant.
|
|
|
|
if (existingUndefined && !existingUndefined->isCalledDirectly)
|
2019-07-11 13:40:30 +08:00
|
|
|
replaceSym();
|
2019-08-31 03:50:59 +08:00
|
|
|
else if (getFunctionVariant(s, sig, file, &s))
|
|
|
|
replaceSym();
|
|
|
|
}
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
2018-06-29 00:53:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return s;
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
2018-07-18 03:15:02 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<UndefinedData>(s, name, flags, file);
|
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
|
|
|
lazy->fetch();
|
|
|
|
else if (s->isDefined())
|
|
|
|
checkDataType(s, file);
|
|
|
|
return s;
|
2018-02-28 08:09:22 +08:00
|
|
|
}
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedGlobal(StringRef name, StringRef importName,
|
|
|
|
StringRef importModule, uint32_t flags,
|
|
|
|
InputFile *file,
|
|
|
|
const WasmGlobalType *type) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
2018-05-31 02:07:52 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<UndefinedGlobal>(s, name, importName, importModule, flags,
|
|
|
|
file, type);
|
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
|
|
|
lazy->fetch();
|
|
|
|
else if (s->isDefined())
|
|
|
|
checkGlobalType(s, file, type);
|
|
|
|
return s;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void SymbolTable::addLazy(ArchiveFile *file, const Archive::Symbol *sym) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addLazy: " << sym->getName() << "\n");
|
|
|
|
StringRef name = sym->getName();
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insertName(name);
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (wasInserted) {
|
|
|
|
replaceSymbol<LazySymbol>(s, name, 0, file, *sym);
|
2018-02-21 05:08:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!s->isUndefined())
|
2019-01-30 06:26:31 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// The existing symbol is undefined, load a new one from the archive,
|
|
|
|
// unless the the existing symbol is weak in which case replace the undefined
|
|
|
|
// symbols with a LazySymbol.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (s->isWeak()) {
|
|
|
|
const WasmSignature *oldSig = nullptr;
|
2019-01-30 06:26:31 +08:00
|
|
|
// In the case of an UndefinedFunction we need to preserve the expected
|
|
|
|
// signature.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<UndefinedFunction>(s))
|
|
|
|
oldSig = f->signature;
|
2019-01-30 06:26:31 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "replacing existing weak undefined symbol\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
auto newSym = replaceSymbol<LazySymbol>(s, name, WASM_SYMBOL_BINDING_WEAK,
|
|
|
|
file, *sym);
|
|
|
|
newSym->signature = oldSig;
|
2019-01-30 06:26:31 +08:00
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2019-01-30 06:26:31 +08:00
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "replacing existing undefined\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
file->addMember(sym);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2018-01-13 06:25:17 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
bool SymbolTable::addComdat(StringRef name) {
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
bool SymbolTable::getFunctionVariant(Symbol* sym, const WasmSignature *sig,
|
|
|
|
const InputFile *file, Symbol **out) {
|
|
|
|
LLVM_DEBUG(dbgs() << "getFunctionVariant: " << sym->getName() << " -> "
|
|
|
|
<< " " << toString(*sig) << "\n");
|
|
|
|
Symbol *variant = nullptr;
|
2019-02-21 07:19:31 +08:00
|
|
|
|
|
|
|
// Linear search through symbol variants. Should never be more than two
|
|
|
|
// or three entries here.
|
2019-07-11 13:40:30 +08:00
|
|
|
auto &variants = symVariants[CachedHashStringRef(sym->getName())];
|
|
|
|
if (variants.empty())
|
|
|
|
variants.push_back(sym);
|
2019-02-21 07:19:31 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (Symbol* v : variants) {
|
|
|
|
if (*v->getSignature() == *sig) {
|
|
|
|
variant = v;
|
2019-02-21 07:19:31 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
bool wasAdded = !variant;
|
|
|
|
if (wasAdded) {
|
2019-02-21 07:19:31 +08:00
|
|
|
// Create a new variant;
|
|
|
|
LLVM_DEBUG(dbgs() << "added new variant\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
variant = reinterpret_cast<Symbol *>(make<SymbolUnion>());
|
|
|
|
variants.push_back(variant);
|
2019-02-21 07:19:31 +08:00
|
|
|
} else {
|
2019-07-11 13:40:30 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "variant already exists: " << toString(*variant) << "\n");
|
|
|
|
assert(*variant->getSignature() == *sig);
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
*out = variant;
|
|
|
|
return wasAdded;
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
void SymbolTable::trace(StringRef name) {
|
|
|
|
symMap.insert({CachedHashStringRef(name), -1});
|
2019-02-06 10:35:18 +08:00
|
|
|
}
|
2019-02-08 06:42:16 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
|
2019-05-24 22:14:25 +08:00
|
|
|
// Swap symbols as instructed by -wrap.
|
2019-07-11 13:40:30 +08:00
|
|
|
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");
|
2019-05-24 22:14:25 +08:00
|
|
|
|
|
|
|
// Anyone looking up __real symbols should get the original
|
2019-07-11 13:40:30 +08:00
|
|
|
realIdx = origIdx;
|
2019-05-24 22:14:25 +08:00
|
|
|
// Anyone looking up the original should get the __wrap symbol
|
2019-07-11 13:40:30 +08:00
|
|
|
origIdx = wrapIdx;
|
2019-05-24 22:14:25 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static const uint8_t unreachableFn[] = {
|
2019-02-08 06:42:16 +08:00
|
|
|
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-07-11 13:40:30 +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);
|
|
|
|
replaceSymbol<DefinedFunction>(sym, sym->getName(), sym->getFlags(), nullptr,
|
|
|
|
func);
|
|
|
|
return func;
|
2019-02-08 06:42:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2019-07-11 13:40:30 +08:00
|
|
|
for (Symbol *sym : getSymbols()) {
|
|
|
|
if (!sym->isUndefWeak())
|
2019-02-08 06:42:16 +08:00
|
|
|
continue;
|
|
|
|
|
2019-07-11 13:40:30 +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
|
|
|
|
// in some circumstantce.
|
2019-07-11 13:40:30 +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.
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef debugName = saver.save("undefined:" + toString(*sym));
|
|
|
|
InputFunction* func = replaceWithUnreachable(sym, *sig, debugName);
|
2019-02-08 06:42:16 +08:00
|
|
|
// 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).
|
2019-07-11 13:40:30 +08:00
|
|
|
func->setTableIndex(0);
|
2019-02-08 06:42:16 +08:00
|
|
|
// Hide our dummy to prevent export.
|
2019-07-11 13:40:30 +08:00
|
|
|
sym->setHidden(true);
|
2019-02-08 06:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 07:19:31 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
static void reportFunctionSignatureMismatch(StringRef symName,
|
|
|
|
FunctionSymbol *a,
|
|
|
|
FunctionSymbol *b, bool isError) {
|
|
|
|
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()))
|
2019-02-21 07:19:31 +08:00
|
|
|
.str();
|
2019-07-11 13:40:30 +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() {
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto pair : symVariants) {
|
2019-02-21 07:19:31 +08:00
|
|
|
// Push the initial symbol onto the list of variants.
|
2019-07-11 13:40:30 +08:00
|
|
|
StringRef symName = pair.first.val();
|
|
|
|
std::vector<Symbol *> &variants = pair.second;
|
2019-02-21 07:19:31 +08:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2019-07-11 13:40:30 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "symbol with (" << variants.size()
|
|
|
|
<< ") variants: " << symName << "\n");
|
|
|
|
for (auto *s: variants) {
|
|
|
|
auto *f = cast<FunctionSymbol>(s);
|
|
|
|
LLVM_DEBUG(dbgs() << " variant: " + f->getName() << " "
|
|
|
|
<< toString(*f->signature) << "\n");
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Find the one definition.
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedFunction *defined = nullptr;
|
|
|
|
for (auto *symbol : variants) {
|
|
|
|
if (auto f = dyn_cast<DefinedFunction>(symbol)) {
|
|
|
|
defined = f;
|
2019-02-21 07:19:31 +08:00
|
|
|
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.
|
2019-07-11 13:40:30 +08:00
|
|
|
if (!defined) {
|
|
|
|
reportFunctionSignatureMismatch(symName,
|
|
|
|
cast<FunctionSymbol>(variants[0]),
|
|
|
|
cast<FunctionSymbol>(variants[1]), true);
|
2019-02-21 07:19:31 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
for (auto *symbol : variants) {
|
|
|
|
if (symbol != defined) {
|
|
|
|
auto *f = cast<FunctionSymbol>(symbol);
|
|
|
|
reportFunctionSignatureMismatch(symName, f, defined, false);
|
|
|
|
StringRef debugName = saver.save("unreachable:" + toString(*f));
|
|
|
|
replaceWithUnreachable(f, *f->signature, debugName);
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|