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;
|
|
|
|
|
|
|
|
SymbolTable *lld::wasm::Symtab;
|
|
|
|
|
|
|
|
void SymbolTable::addFile(InputFile *File) {
|
|
|
|
log("Processing: " + toString(File));
|
2019-02-06 10:35:18 +08:00
|
|
|
if (Config->Trace)
|
|
|
|
message(toString(File));
|
2017-11-18 02:14:09 +08:00
|
|
|
File->parse();
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
// LLVM bitcode file
|
|
|
|
if (auto *F = dyn_cast<BitcodeFile>(File))
|
|
|
|
BitcodeFiles.push_back(F);
|
|
|
|
else if (auto *F = dyn_cast<ObjFile>(File))
|
2017-11-18 02:14:09 +08:00
|
|
|
ObjectFiles.push_back(F);
|
2019-03-14 05:29:20 +08:00
|
|
|
else if (auto *F = dyn_cast<SharedFile>(File))
|
|
|
|
SharedFiles.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() {
|
|
|
|
if (BitcodeFiles.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Compile bitcode files and replace bitcode symbols.
|
|
|
|
LTO.reset(new BitcodeCompiler);
|
|
|
|
for (BitcodeFile *F : BitcodeFiles)
|
|
|
|
LTO->add(*F);
|
|
|
|
|
|
|
|
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
|
|
|
void SymbolTable::reportRemainingUndefines() {
|
2017-12-07 09:51:24 +08:00
|
|
|
for (Symbol *Sym : SymVector) {
|
2018-05-31 02:07:52 +08:00
|
|
|
if (!Sym->isUndefined() || Sym->isWeak())
|
|
|
|
continue;
|
|
|
|
if (Config->AllowUndefinedSymbols.count(Sym->getName()) != 0)
|
|
|
|
continue;
|
|
|
|
if (!Sym->IsUsedInRegularObj)
|
|
|
|
continue;
|
2018-08-04 08:04:06 +08:00
|
|
|
error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym));
|
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;
|
|
|
|
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) {
|
2018-06-29 00:53:53 +08:00
|
|
|
if (!NewSig)
|
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
|
|
|
const WasmSignature *OldSig = Existing->Signature;
|
2018-06-29 00:53:53 +08:00
|
|
|
if (!OldSig) {
|
2019-02-21 07:19:31 +08:00
|
|
|
Existing->Signature = NewSig;
|
|
|
|
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-05-23 18:06:03 +08:00
|
|
|
DefinedData *SymbolTable::addOptionalDataSymbol(StringRef Name, uint32_t Value,
|
|
|
|
uint32_t Flags) {
|
|
|
|
Symbol *S = find(Name);
|
|
|
|
if (!S || S->isDefined())
|
|
|
|
return nullptr;
|
|
|
|
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << Name << "\n");
|
|
|
|
auto *rtn = replaceSymbol<DefinedData>(S, Name, Flags);
|
|
|
|
rtn->setVirtualAddress(Value);
|
|
|
|
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-02-21 07:19:31 +08:00
|
|
|
auto Replace = [&](Symbol* Sym) {
|
|
|
|
// 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-02-21 07:19:31 +08:00
|
|
|
Replace(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-02-21 07:19:31 +08:00
|
|
|
if (Function && !signatureMatches(ExistingFunction, &Function->Signature)) {
|
|
|
|
Symbol* Variant;
|
|
|
|
if (getFunctionVariant(S, &Function->Signature, File, &Variant))
|
|
|
|
// New variant, always replace
|
|
|
|
Replace(Variant);
|
|
|
|
else if (shouldReplace(S, File, Flags))
|
|
|
|
// Variant already exists, replace it after checking shouldReplace
|
|
|
|
Replace(Variant);
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
Replace(S);
|
|
|
|
|
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-02-21 07:19:31 +08:00
|
|
|
auto Replace = [&]() {
|
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()) {
|
|
|
|
Replace();
|
2018-02-28 08:09:22 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDataType(S, File);
|
|
|
|
|
|
|
|
if (shouldReplace(S, File, Flags))
|
2019-02-21 07:19:31 +08:00
|
|
|
Replace();
|
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-02-21 07:19:31 +08:00
|
|
|
auto Replace = [&]() {
|
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()) {
|
|
|
|
Replace();
|
2018-02-28 08:09:22 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkGlobalType(S, File, &Global->getType());
|
|
|
|
|
|
|
|
if (shouldReplace(S, File, Flags))
|
2019-02-21 07:19:31 +08:00
|
|
|
Replace();
|
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-02-21 07:19:31 +08:00
|
|
|
auto Replace = [&]() {
|
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()) {
|
|
|
|
Replace();
|
2018-12-08 14:17:43 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkEventType(S, File, &Event->getType(), &Event->Signature);
|
|
|
|
|
|
|
|
if (shouldReplace(S, File, Flags))
|
2019-02-21 07:19:31 +08:00
|
|
|
Replace();
|
2018-12-08 14:17:43 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2019-02-08 06:00:48 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedFunction(StringRef Name, StringRef ImportName,
|
|
|
|
StringRef ImportModule,
|
2019-02-01 10:29:57 +08:00
|
|
|
uint32_t Flags, InputFile *File,
|
2018-02-28 08:09:22 +08:00
|
|
|
const WasmSignature *Sig) {
|
2018-09-29 00:50:14 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << Name <<
|
|
|
|
" [" << (Sig ? toString(*Sig) : "none") << "]\n");
|
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-02-21 07:19:31 +08:00
|
|
|
auto Replace = [&]() {
|
2019-02-08 06:00:48 +08:00
|
|
|
replaceSymbol<UndefinedFunction>(S, Name, ImportName, ImportModule, Flags,
|
|
|
|
File, Sig);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (WasInserted)
|
|
|
|
Replace();
|
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;
|
|
|
|
}
|
|
|
|
if (!signatureMatches(ExistingFunction, Sig))
|
|
|
|
if (getFunctionVariant(S, Sig, File, &S))
|
|
|
|
Replace();
|
|
|
|
}
|
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");
|
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
|
|
|
|
2019-02-08 06:00:48 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedGlobal(StringRef Name, StringRef ImportName,
|
|
|
|
StringRef ImportModule, uint32_t Flags,
|
2018-02-28 08:09:22 +08:00
|
|
|
InputFile *File,
|
|
|
|
const WasmGlobalType *Type) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << Name << "\n");
|
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,
|
|
|
|
// unless the the existing symbol is weak in which case replace the undefined
|
|
|
|
// 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
|
|
|
|
|
|
|
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);
|
2019-02-21 07:19:31 +08:00
|
|
|
replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Sym->getFlags(), nullptr,
|
|
|
|
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
|
|
|
|
// in some circumstantce.
|
|
|
|
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,
|
|
|
|
FunctionSymbol *B, bool Error) {
|
|
|
|
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();
|
|
|
|
if (Error)
|
|
|
|
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);
|
|
|
|
StringRef DebugName = Saver.save("unreachable:" + toString(*F));
|
|
|
|
replaceWithUnreachable(F, *F->Signature, DebugName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|