2017-11-18 02:14:09 +08:00
|
|
|
//===- SymbolTable.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.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;
|
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));
|
|
|
|
File->parse();
|
|
|
|
|
|
|
|
if (auto *F = dyn_cast<ObjFile>(File))
|
|
|
|
ObjectFiles.push_back(F);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolTable::reportRemainingUndefines() {
|
2018-02-14 06:30:52 +08:00
|
|
|
SetVector<Symbol *> Undefs;
|
2017-12-07 09:51:24 +08:00
|
|
|
for (Symbol *Sym : SymVector) {
|
2017-11-18 02:14:09 +08:00
|
|
|
if (Sym->isUndefined() && !Sym->isWeak() &&
|
|
|
|
Config->AllowUndefinedSymbols.count(Sym->getName()) == 0) {
|
|
|
|
Undefs.insert(Sym);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Undefs.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (ObjFile *File : ObjectFiles)
|
|
|
|
for (Symbol *Sym : File->getSymbols())
|
|
|
|
if (Undefs.count(Sym))
|
|
|
|
error(toString(File) + ": undefined symbol: " + toString(*Sym));
|
|
|
|
|
|
|
|
for (Symbol *Sym : Undefs)
|
|
|
|
if (!Sym->getFile())
|
|
|
|
error("undefined symbol: " + toString(*Sym));
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::find(StringRef Name) {
|
2017-11-28 07:16:06 +08:00
|
|
|
auto It = SymMap.find(CachedHashStringRef(Name));
|
|
|
|
if (It == SymMap.end())
|
2017-11-18 02:14:09 +08:00
|
|
|
return nullptr;
|
|
|
|
return It->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
|
2017-11-28 07:16:06 +08:00
|
|
|
Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
|
2017-11-18 02:14:09 +08:00
|
|
|
if (Sym)
|
|
|
|
return {Sym, false};
|
2018-02-15 02:27:59 +08:00
|
|
|
Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
|
2017-12-07 09:51:24 +08:00
|
|
|
SymVector.emplace_back(Sym);
|
2017-11-18 02:14:09 +08:00
|
|
|
return {Sym, true};
|
|
|
|
}
|
|
|
|
|
2017-11-30 09:40:08 +08:00
|
|
|
// Check the type of new symbol matches that of the symbol is replacing.
|
|
|
|
// For functions this can also involve verifying that the signatures match.
|
|
|
|
static void checkSymbolTypes(const Symbol &Existing, const InputFile &F,
|
2018-02-15 02:27:59 +08:00
|
|
|
bool NewIsFunction, const WasmSignature *NewSig) {
|
2017-11-30 09:40:08 +08:00
|
|
|
if (Existing.isLazy())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// First check the symbol types match (i.e. either both are function
|
|
|
|
// symbols or both are data symbols).
|
2018-02-17 08:44:21 +08:00
|
|
|
if (isa<FunctionSymbol>(Existing) != NewIsFunction) {
|
2018-01-10 08:52:20 +08:00
|
|
|
error("symbol type mismatch: " + Existing.getName() + "\n>>> defined as " +
|
2018-02-17 08:44:21 +08:00
|
|
|
(isa<FunctionSymbol>(Existing) ? "Function" : "Global") + " in " +
|
2017-11-30 09:40:08 +08:00
|
|
|
toString(Existing.getFile()) + "\n>>> defined as " +
|
|
|
|
(NewIsFunction ? "Function" : "Global") + " in " + F.getName());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For function symbols, optionally check the function signature matches too.
|
2018-02-15 02:27:59 +08:00
|
|
|
auto *ExistingFunc = dyn_cast<FunctionSymbol>(&Existing);
|
|
|
|
if (!ExistingFunc || !Config->CheckSignatures)
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
2018-02-15 02:27:59 +08:00
|
|
|
|
2018-02-17 07:50:23 +08:00
|
|
|
const WasmSignature *OldSig = ExistingFunc->getFunctionType();
|
|
|
|
|
2017-12-07 11:19:53 +08:00
|
|
|
// Skip the signature check if the existing function has no signature (e.g.
|
|
|
|
// if it is an undefined symbol generated by --undefined command line flag).
|
2018-02-17 07:50:23 +08:00
|
|
|
if (OldSig == nullptr)
|
2017-12-07 11:19:53 +08:00
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
DEBUG(dbgs() << "checkSymbolTypes: " << ExistingFunc->getName() << "\n");
|
2017-11-30 09:40:08 +08:00
|
|
|
assert(NewSig);
|
|
|
|
|
2018-02-17 07:50:23 +08:00
|
|
|
if (*NewSig == *OldSig)
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
error("function signature mismatch: " + ExistingFunc->getName() +
|
2018-02-17 07:50:23 +08:00
|
|
|
"\n>>> defined as " + toString(*OldSig) + " in " +
|
2018-02-15 02:27:59 +08:00
|
|
|
toString(ExistingFunc->getFile()) + "\n>>> defined as " +
|
|
|
|
toString(*NewSig) + " in " + F.getName());
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 03:57:01 +08:00
|
|
|
static void checkSymbolTypes(const Symbol &Existing, const InputFile &F,
|
2018-02-15 02:27:59 +08:00
|
|
|
bool IsFunction, const InputChunk *Chunk) {
|
2018-01-29 03:57:02 +08:00
|
|
|
const WasmSignature *Sig = nullptr;
|
|
|
|
if (auto *F = dyn_cast_or_null<InputFunction>(Chunk))
|
2018-01-29 03:57:01 +08:00
|
|
|
Sig = &F->Signature;
|
2018-02-15 02:27:59 +08:00
|
|
|
return checkSymbolTypes(Existing, F, IsFunction, Sig);
|
2018-01-29 03:57:01 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
DefinedFunction *SymbolTable::addSyntheticFunction(StringRef Name,
|
|
|
|
const WasmSignature *Type,
|
|
|
|
uint32_t Flags) {
|
2018-02-14 04:14:26 +08:00
|
|
|
DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
|
2018-01-13 02:35:13 +08:00
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
2018-02-14 04:14:26 +08:00
|
|
|
assert(WasInserted);
|
2018-02-15 02:27:59 +08:00
|
|
|
return replaceSymbol<DefinedFunction>(S, Name, Flags, Type);
|
2018-01-13 02:35:13 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags) {
|
2018-02-14 04:14:26 +08:00
|
|
|
DEBUG(dbgs() << "addSyntheticGlobal: " << Name << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
2018-02-14 04:14:26 +08:00
|
|
|
assert(WasInserted);
|
2018-02-15 02:27:59 +08:00
|
|
|
return replaceSymbol<DefinedGlobal>(S, Name, Flags);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 02:55:07 +08:00
|
|
|
static bool shouldReplace(const Symbol &Existing, InputFile *NewFile,
|
|
|
|
uint32_t NewFlags, InputChunk *NewChunk,
|
|
|
|
bool NewIsFunction) {
|
2018-02-21 05:08:47 +08:00
|
|
|
// If existing symbol is lazy, replace it without checking types since
|
|
|
|
// lazy symbols don't have any type information.
|
2018-02-21 02:55:06 +08:00
|
|
|
if (Existing.isLazy()) {
|
|
|
|
DEBUG(dbgs() << "replacing existing lazy symbol: " << Existing.getName()
|
|
|
|
<< "\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we have two wasm symbols, and all wasm symbols that have the same
|
|
|
|
// symbol name must have the same type, even if they are undefined. This
|
|
|
|
// is different from ELF because symbol types are not that significant
|
|
|
|
// in ELF, and undefined symbols in ELF don't have type in the first place.
|
|
|
|
checkSymbolTypes(Existing, *NewFile, NewIsFunction, NewChunk);
|
|
|
|
|
|
|
|
// If existing symbol is undefined, replace it.
|
|
|
|
if (!Existing.isDefined()) {
|
2018-02-21 02:55:06 +08:00
|
|
|
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) {
|
2017-12-01 08:53:21 +08:00
|
|
|
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.
|
|
|
|
if (Existing.isWeak()) {
|
|
|
|
DEBUG(dbgs() << "replacing existing weak symbol\n");
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-21 02:55:06 +08:00
|
|
|
|
2018-02-21 05:08:47 +08:00
|
|
|
// Neither symbol is week. They conflict.
|
|
|
|
error("duplicate symbol: " + toString(Existing) + "\n>>> defined in " +
|
|
|
|
toString(Existing.getFile()) + "\n>>> defined in " + toString(NewFile));
|
|
|
|
return true;
|
2018-02-21 02:55:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addDefinedFunction(StringRef Name, uint32_t Flags,
|
|
|
|
InputFile *F, InputFunction *Function) {
|
|
|
|
DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n");
|
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
2018-02-21 02:55:07 +08:00
|
|
|
if (WasInserted || shouldReplace(*S, F, Flags, Function, true))
|
2018-02-21 02:55:06 +08:00
|
|
|
replaceSymbol<DefinedFunction>(S, Name, Flags, F, Function);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
|
|
|
|
InputFile *F, InputSegment *Segment,
|
|
|
|
uint32_t Address) {
|
|
|
|
DEBUG(dbgs() << "addDefinedGlobal:" << Name << " addr:" << Address << "\n");
|
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
2018-02-21 02:55:07 +08:00
|
|
|
if (WasInserted || shouldReplace(*S, F, Flags, Segment, false))
|
2018-02-21 02:55:06 +08:00
|
|
|
replaceSymbol<DefinedGlobal>(S, Name, Flags, F, Segment, Address);
|
2017-11-18 02:14:09 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
Symbol *SymbolTable::addUndefined(StringRef Name, Symbol::Kind Kind,
|
|
|
|
uint32_t Flags, InputFile *F,
|
2018-01-10 07:56:44 +08:00
|
|
|
const WasmSignature *Type) {
|
2018-01-10 08:52:20 +08:00
|
|
|
DEBUG(dbgs() << "addUndefined: " << Name << "\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
2018-01-10 08:52:20 +08:00
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
bool IsFunction = Kind == Symbol::UndefinedFunctionKind;
|
2017-11-18 02:14:09 +08:00
|
|
|
if (WasInserted) {
|
2018-02-15 02:27:59 +08:00
|
|
|
if (IsFunction)
|
|
|
|
replaceSymbol<UndefinedFunction>(S, Name, Flags, F, Type);
|
|
|
|
else
|
|
|
|
replaceSymbol<UndefinedGlobal>(S, Name, Flags, F);
|
2018-02-21 05:08:47 +08:00
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto *Lazy = dyn_cast<LazySymbol>(S)) {
|
2017-11-18 02:14:09 +08:00
|
|
|
DEBUG(dbgs() << "resolved by existing lazy\n");
|
2018-02-21 05:08:47 +08:00
|
|
|
cast<ArchiveFile>(Lazy->getFile())->addMember(&Lazy->getArchiveSymbol());
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (S->isDefined()) {
|
2017-11-18 02:14:09 +08:00
|
|
|
DEBUG(dbgs() << "resolved by existing\n");
|
2018-02-15 02:27:59 +08:00
|
|
|
checkSymbolTypes(*S, *F, IsFunction, Type);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolTable::addLazy(ArchiveFile *F, const Archive::Symbol *Sym) {
|
2017-12-06 11:10:39 +08:00
|
|
|
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;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
if (WasInserted) {
|
2018-02-15 02:27:59 +08:00
|
|
|
replaceSymbol<LazySymbol>(S, Name, F, *Sym);
|
2018-02-21 05:08:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is an existing undefined symbol, load a new one from the archive.
|
|
|
|
if (S->isUndefined()) {
|
2017-11-18 02:14:09 +08:00
|
|
|
DEBUG(dbgs() << "replacing existing undefined\n");
|
|
|
|
F->addMember(Sym);
|
|
|
|
}
|
|
|
|
}
|
2018-01-13 06:25:17 +08:00
|
|
|
|
|
|
|
bool SymbolTable::addComdat(StringRef Name, ObjFile *F) {
|
|
|
|
DEBUG(dbgs() << "addComdat: " << Name << "\n");
|
|
|
|
ObjFile *&File = ComdatMap[CachedHashStringRef(Name)];
|
|
|
|
if (File) {
|
|
|
|
DEBUG(dbgs() << "COMDAT already defined\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
File = F;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjFile *SymbolTable::findComdat(StringRef Name) const {
|
|
|
|
auto It = ComdatMap.find(CachedHashStringRef(Name));
|
|
|
|
return It == ComdatMap.end() ? nullptr : It->second;
|
|
|
|
}
|