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"
|
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;
|
|
|
|
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
|
|
|
// Get the signature for a given function symbol, either by looking
|
|
|
|
// it up in function sections (for defined functions), of the imports section
|
|
|
|
// (for imported functions).
|
|
|
|
static const WasmSignature *getFunctionSig(const ObjFile &Obj,
|
|
|
|
const WasmSymbol &Sym) {
|
|
|
|
DEBUG(dbgs() << "getFunctionSig: " << Sym.Name << "\n");
|
|
|
|
const WasmObjectFile *WasmObj = Obj.getWasmObj();
|
2017-12-21 10:43:39 +08:00
|
|
|
return &WasmObj->types()[Sym.FunctionType];
|
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,
|
|
|
|
const WasmSymbol &New,
|
|
|
|
const WasmSignature *NewSig) {
|
|
|
|
if (Existing.isLazy())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool NewIsFunction = New.Type == WasmSymbol::SymbolType::FUNCTION_EXPORT ||
|
|
|
|
New.Type == WasmSymbol::SymbolType::FUNCTION_IMPORT;
|
|
|
|
|
|
|
|
// First check the symbol types match (i.e. either both are function
|
|
|
|
// symbols or both are data symbols).
|
|
|
|
if (Existing.isFunction() != NewIsFunction) {
|
|
|
|
error("symbol type mismatch: " + New.Name + "\n>>> defined as " +
|
|
|
|
(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
|
|
|
|
2017-11-30 09:40:08 +08:00
|
|
|
DEBUG(dbgs() << "checkSymbolTypes: " << New.Name << "\n");
|
|
|
|
assert(NewSig);
|
|
|
|
|
|
|
|
const WasmSignature &OldSig = Existing.getFunctionType();
|
|
|
|
if (*NewSig == OldSig)
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
|
|
|
|
2017-11-30 09:40:08 +08:00
|
|
|
error("function signature mismatch: " + New.Name + "\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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addDefined(InputFile *F, const WasmSymbol *Sym,
|
|
|
|
const InputSegment *Segment) {
|
|
|
|
DEBUG(dbgs() << "addDefined: " << Sym->Name << "\n");
|
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
Symbol::Kind Kind = Symbol::DefinedFunctionKind;
|
2017-11-30 09:40:08 +08:00
|
|
|
const WasmSignature *NewSig = nullptr;
|
2017-11-18 02:14:09 +08:00
|
|
|
if (Sym->Type == WasmSymbol::SymbolType::GLOBAL_EXPORT)
|
|
|
|
Kind = Symbol::DefinedGlobalKind;
|
2017-11-30 09:40:08 +08:00
|
|
|
else
|
|
|
|
NewSig = getFunctionSig(*cast<ObjFile>(F), *Sym);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
std::tie(S, WasInserted) = insert(Sym->Name);
|
|
|
|
if (WasInserted) {
|
2017-11-30 09:40:08 +08:00
|
|
|
S->update(Kind, F, Sym, Segment, NewSig);
|
|
|
|
} else if (S->isLazy()) {
|
|
|
|
// The existing symbol is lazy. Replace it without checking types since
|
|
|
|
// lazy symbols don't have any type information.
|
|
|
|
DEBUG(dbgs() << "replacing existing lazy symbol: " << Sym->Name << "\n");
|
|
|
|
S->update(Kind, F, Sym, Segment, NewSig);
|
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
|
2017-11-18 02:14:09 +08:00
|
|
|
DEBUG(dbgs() << "resolving existing undefined symbol: " << Sym->Name
|
|
|
|
<< "\n");
|
2017-11-30 09:40:08 +08:00
|
|
|
checkSymbolTypes(*S, *F, *Sym, NewSig);
|
|
|
|
S->update(Kind, F, Sym, Segment, NewSig);
|
2017-11-18 02:14:09 +08:00
|
|
|
} else if (Sym->isWeak()) {
|
|
|
|
// 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");
|
2017-11-30 09:40:08 +08:00
|
|
|
checkSymbolTypes(*S, *F, *Sym, NewSig);
|
|
|
|
S->update(Kind, F, Sym, Segment, NewSig);
|
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) {
|
|
|
|
S->update(Symbol::UndefinedFunctionKind, nullptr, nullptr, nullptr, Type);
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *SymbolTable::addUndefined(InputFile *F, const WasmSymbol *Sym) {
|
2017-12-06 11:10:39 +08:00
|
|
|
DEBUG(dbgs() << "addUndefined: " << Sym->Name << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
Symbol *S;
|
|
|
|
bool WasInserted;
|
|
|
|
Symbol::Kind Kind = Symbol::UndefinedFunctionKind;
|
2017-11-30 09:40:08 +08:00
|
|
|
const WasmSignature *NewSig = nullptr;
|
2017-11-18 02:14:09 +08:00
|
|
|
if (Sym->Type == WasmSymbol::SymbolType::GLOBAL_IMPORT)
|
|
|
|
Kind = Symbol::UndefinedGlobalKind;
|
2017-11-30 09:40:08 +08:00
|
|
|
else
|
|
|
|
NewSig = getFunctionSig(*cast<ObjFile>(F), *Sym);
|
2017-11-18 02:14:09 +08:00
|
|
|
std::tie(S, WasInserted) = insert(Sym->Name);
|
|
|
|
if (WasInserted) {
|
2017-11-30 09:40:08 +08:00
|
|
|
S->update(Kind, F, Sym, nullptr, NewSig);
|
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");
|
2017-11-30 09:40:08 +08:00
|
|
|
checkSymbolTypes(*S, *F, *Sym, NewSig);
|
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);
|
|
|
|
}
|
|
|
|
}
|