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"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#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() {
|
|
|
|
std::unordered_set<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};
|
|
|
|
Sym = make<Symbol>(Name, false);
|
2017-12-07 09:51:24 +08:00
|
|
|
SymVector.emplace_back(Sym);
|
2017-11-18 02:14:09 +08:00
|
|
|
return {Sym, true};
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolTable::reportDuplicate(Symbol *Existing, InputFile *NewFile) {
|
|
|
|
error("duplicate symbol: " + toString(*Existing) + "\n>>> defined in " +
|
|
|
|
toString(Existing->getFile()) + "\n>>> defined in " +
|
2017-11-28 06:49:16 +08:00
|
|
|
toString(NewFile));
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
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-01-10 08:52:20 +08:00
|
|
|
Symbol::Kind Kind, const WasmSignature *NewSig) {
|
2017-11-30 09:40:08 +08:00
|
|
|
if (Existing.isLazy())
|
|
|
|
return;
|
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
bool NewIsFunction = Kind == Symbol::Kind::UndefinedFunctionKind ||
|
|
|
|
Kind == Symbol::Kind::DefinedFunctionKind;
|
2017-11-30 09:40:08 +08:00
|
|
|
|
|
|
|
// First check the symbol types match (i.e. either both are function
|
|
|
|
// symbols or both are data symbols).
|
|
|
|
if (Existing.isFunction() != NewIsFunction) {
|
2018-01-10 08:52:20 +08:00
|
|
|
error("symbol type mismatch: " + Existing.getName() + "\n>>> defined as " +
|
2017-11-30 09:40:08 +08:00
|
|
|
(Existing.isFunction() ? "Function" : "Global") + " in " +
|
|
|
|
toString(Existing.getFile()) + "\n>>> defined as " +
|
|
|
|
(NewIsFunction ? "Function" : "Global") + " in " + F.getName());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For function symbols, optionally check the function signature matches too.
|
|
|
|
if (!NewIsFunction || !Config->CheckSignatures)
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
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).
|
|
|
|
if (!Existing.hasFunctionType())
|
|
|
|
return;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
DEBUG(dbgs() << "checkSymbolTypes: " << Existing.getName() << "\n");
|
2017-11-30 09:40:08 +08:00
|
|
|
assert(NewSig);
|
|
|
|
|
|
|
|
const WasmSignature &OldSig = Existing.getFunctionType();
|
|
|
|
if (*NewSig == OldSig)
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
error("function signature mismatch: " + Existing.getName() +
|
|
|
|
"\n>>> defined as " + toString(OldSig) + " in " +
|
|
|
|
toString(Existing.getFile()) + "\n>>> defined as " + toString(*NewSig) +
|
|
|
|
" in " + F.getName());
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 02:35:13 +08:00
|
|
|
Symbol *SymbolTable::addDefinedFunction(StringRef Name,
|
|
|
|
const WasmSignature *Type,
|
|
|
|
uint32_t Flags) {
|
|
|
|
DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n");
|
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
if (WasInserted) {
|
|
|
|
S->update(Symbol::DefinedFunctionKind, nullptr, Flags);
|
|
|
|
S->setFunctionType(Type);
|
|
|
|
} else if (!S->isFunction()) {
|
|
|
|
error("symbol type mismatch: " + Name);
|
|
|
|
}
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *SymbolTable::addDefinedGlobal(StringRef Name) {
|
|
|
|
DEBUG(dbgs() << "addDefinedGlobal: " << Name << "\n");
|
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
if (WasInserted)
|
|
|
|
S->update(Symbol::DefinedGlobalKind);
|
|
|
|
else if (!S->isGlobal())
|
|
|
|
error("symbol type mismatch: " + Name);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
Symbol *SymbolTable::addDefined(StringRef Name, Symbol::Kind Kind,
|
|
|
|
uint32_t Flags, InputFile *F,
|
2018-01-10 07:56:44 +08:00
|
|
|
const InputSegment *Segment,
|
2018-01-10 08:52:20 +08:00
|
|
|
InputFunction *Function, uint32_t Address) {
|
|
|
|
DEBUG(dbgs() << "addDefined: " << Name << " addr:" << Address << "\n");
|
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);
|
2017-11-18 02:14:09 +08:00
|
|
|
if (WasInserted) {
|
2018-01-10 08:52:20 +08:00
|
|
|
S->update(Kind, F, Flags, Segment, Function, Address);
|
2017-11-30 09:40:08 +08:00
|
|
|
} else if (S->isLazy()) {
|
|
|
|
// The existing symbol is lazy. Replace it without checking types since
|
|
|
|
// lazy symbols don't have any type information.
|
2018-01-10 08:52:20 +08:00
|
|
|
DEBUG(dbgs() << "replacing existing lazy symbol: " << Name << "\n");
|
|
|
|
S->update(Kind, F, Flags, Segment, Function, Address);
|
2017-11-18 02:14:09 +08:00
|
|
|
} else if (!S->isDefined()) {
|
|
|
|
// The existing symbol table entry is undefined. The new symbol replaces
|
2017-12-01 08:53:21 +08:00
|
|
|
// it, after checking the type matches
|
2018-01-10 08:52:20 +08:00
|
|
|
DEBUG(dbgs() << "resolving existing undefined symbol: " << Name << "\n");
|
|
|
|
checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr);
|
|
|
|
S->update(Kind, F, Flags, Segment, Function, Address);
|
|
|
|
} else if ((Flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
|
2017-11-18 02:14:09 +08:00
|
|
|
// the new symbol is weak we can ignore it
|
2017-12-01 08:53:21 +08:00
|
|
|
DEBUG(dbgs() << "existing symbol takes precedence\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
} else if (S->isWeak()) {
|
|
|
|
// the new symbol is not weak and the existing symbol is, so we replace
|
|
|
|
// it
|
|
|
|
DEBUG(dbgs() << "replacing existing weak symbol\n");
|
2018-01-10 08:52:20 +08:00
|
|
|
checkSymbolTypes(*S, *F, Kind, Function ? &Function->Signature : nullptr);
|
|
|
|
S->update(Kind, F, Flags, Segment, Function, Address);
|
2017-11-18 02:14:09 +08:00
|
|
|
} else {
|
2017-12-01 08:53:21 +08:00
|
|
|
// neither symbol is week. They conflict.
|
2017-11-18 02:14:09 +08:00
|
|
|
reportDuplicate(S, F);
|
|
|
|
}
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2017-11-30 09:40:08 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedFunction(StringRef Name,
|
|
|
|
const WasmSignature *Type) {
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
2017-11-30 09:40:08 +08:00
|
|
|
if (WasInserted) {
|
2018-01-10 07:56:44 +08:00
|
|
|
S->update(Symbol::UndefinedFunctionKind);
|
|
|
|
S->setFunctionType(Type);
|
2017-11-30 09:40:08 +08:00
|
|
|
} else if (!S->isFunction()) {
|
2017-11-18 02:14:09 +08:00
|
|
|
error("symbol type mismatch: " + Name);
|
2017-11-30 09:40:08 +08:00
|
|
|
}
|
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");
|
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);
|
2017-11-18 02:14:09 +08:00
|
|
|
if (WasInserted) {
|
2018-01-10 08:52:20 +08:00
|
|
|
S->update(Kind, F, Flags);
|
2018-01-10 07:56:44 +08:00
|
|
|
if (Type)
|
|
|
|
S->setFunctionType(Type);
|
2017-11-18 02:14:09 +08:00
|
|
|
} else if (S->isLazy()) {
|
|
|
|
DEBUG(dbgs() << "resolved by existing lazy\n");
|
|
|
|
auto *AF = cast<ArchiveFile>(S->getFile());
|
|
|
|
AF->addMember(&S->getArchiveSymbol());
|
|
|
|
} else if (S->isDefined()) {
|
|
|
|
DEBUG(dbgs() << "resolved by existing\n");
|
2018-01-10 08:52:20 +08:00
|
|
|
checkSymbolTypes(*S, *F, Kind, 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();
|
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
std::tie(S, WasInserted) = insert(Name);
|
|
|
|
if (WasInserted) {
|
|
|
|
S->update(Symbol::LazyKind, F);
|
|
|
|
S->setArchiveSymbol(*Sym);
|
|
|
|
} else if (S->isUndefined()) {
|
|
|
|
// There is an existing undefined symbol. The can load from the
|
|
|
|
// archive.
|
|
|
|
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;
|
|
|
|
}
|