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"
|
2021-02-11 19:15:24 +08:00
|
|
|
#include "InputElement.h"
|
2017-11-30 09:40:08 +08:00
|
|
|
#include "WriterUtils.h"
|
2022-01-21 03:53:18 +08:00
|
|
|
#include "lld/Common/CommonLinkerContext.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
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
SymbolTable *symtab;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
void SymbolTable::addFile(InputFile *file) {
|
|
|
|
log("Processing: " + toString(file));
|
2019-06-06 01:50:45 +08:00
|
|
|
|
|
|
|
// .a file
|
|
|
|
if (auto *f = dyn_cast<ArchiveFile>(file)) {
|
|
|
|
f->parse();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// .so file
|
|
|
|
if (auto *f = dyn_cast<SharedFile>(file)) {
|
|
|
|
sharedFiles.push_back(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-06 10:35:18 +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-06-06 01:50:45 +08:00
|
|
|
if (auto *f = dyn_cast<BitcodeFile>(file)) {
|
|
|
|
f->parse();
|
2018-05-31 02:07:52 +08:00
|
|
|
bitcodeFiles.push_back(f);
|
2019-06-06 01:50:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular object file
|
|
|
|
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.
|
2021-10-28 22:33:30 +08:00
|
|
|
void SymbolTable::compileBitcodeFiles() {
|
2019-12-20 09:30:24 +08:00
|
|
|
// Prevent further LTO objects being included
|
|
|
|
BitcodeFile::doneLTO = true;
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
if (bitcodeFiles.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Compile bitcode files and replace bitcode symbols.
|
|
|
|
lto.reset(new BitcodeCompiler);
|
|
|
|
for (BitcodeFile *f : bitcodeFiles)
|
|
|
|
lto->add(*f);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
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
|
|
|
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;
|
2019-05-24 22:14:25 +08:00
|
|
|
sym->canInline = true;
|
2019-02-06 10:35:18 +08:00
|
|
|
sym->traced = trace;
|
2020-05-12 08:39:04 +08:00
|
|
|
sym->forceExport = false;
|
2019-02-06 10:35:18 +08:00
|
|
|
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.
|
2020-04-02 00:21:08 +08:00
|
|
|
// Returns true if the function types match, false is there is a signature
|
2019-02-21 07:19:31 +08:00
|
|
|
// mismatch.
|
2019-03-29 01:18:42 +08:00
|
|
|
static bool signatureMatches(FunctionSymbol *existing,
|
|
|
|
const WasmSignature *newSig) {
|
2019-02-21 07:19:31 +08:00
|
|
|
const WasmSignature *oldSig = existing->signature;
|
2019-05-29 23:36:42 +08:00
|
|
|
|
2021-10-27 21:52:17 +08:00
|
|
|
// If either function is missing a signature (this happens for bitcode
|
2019-05-29 23:36:42 +08:00
|
|
|
// symbols) then assume they match. Any mismatch will be reported later
|
|
|
|
// when the LTO objects are added.
|
|
|
|
if (!newSig || !oldSig)
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
static void checkTagType(const Symbol *existing, const InputFile *file,
|
|
|
|
const WasmSignature *newSig) {
|
|
|
|
const auto *existingTag = dyn_cast<TagSymbol>(existing);
|
|
|
|
if (!isa<TagSymbol>(existing)) {
|
|
|
|
reportTypeError(existing, file, WASM_SYMBOL_TYPE_TAG);
|
2018-12-08 14:17:43 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
const WasmSignature *oldSig = existingTag->signature;
|
2018-12-08 14:17:43 +08:00
|
|
|
if (*newSig != *oldSig)
|
2021-06-15 16:49:43 +08:00
|
|
|
warn("Tag signature mismatch: " + existing->getName() +
|
2018-12-08 14:17:43 +08:00
|
|
|
"\n>>> defined as " + toString(*oldSig) + " in " +
|
|
|
|
toString(existing->getFile()) + "\n>>> defined as " +
|
|
|
|
toString(*newSig) + " in " + toString(file));
|
|
|
|
}
|
|
|
|
|
2021-01-05 19:08:58 +08:00
|
|
|
static void checkTableType(const Symbol *existing, const InputFile *file,
|
|
|
|
const WasmTableType *newType) {
|
|
|
|
if (!isa<TableSymbol>(existing)) {
|
|
|
|
reportTypeError(existing, file, WASM_SYMBOL_TYPE_TABLE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WasmTableType *oldType = cast<TableSymbol>(existing)->getTableType();
|
|
|
|
if (newType->ElemType != oldType->ElemType) {
|
|
|
|
error("Table type mismatch: " + existing->getName() + "\n>>> defined as " +
|
|
|
|
toString(*oldType) + " in " + toString(existing->getFile()) +
|
|
|
|
"\n>>> defined as " + toString(*newType) + " in " + toString(file));
|
|
|
|
}
|
|
|
|
// FIXME: No assertions currently on the limits.
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-10 09:52:39 +08:00
|
|
|
// Adds an optional, linker generated, data symbol. The symbol will only be
|
2020-01-07 02:21:05 +08:00
|
|
|
// added if there is an undefine reference to it, or if it is explicitly
|
|
|
|
// 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,
|
2020-08-08 04:24:43 +08:00
|
|
|
uint64_t value) {
|
2019-05-23 18:06:03 +08:00
|
|
|
Symbol *s = find(name);
|
2019-06-01 06:51:59 +08:00
|
|
|
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;
|
|
|
|
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
|
2019-08-09 00:58:36 +08:00
|
|
|
auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN);
|
2021-02-27 07:22:23 +08:00
|
|
|
rtn->setVA(value);
|
2019-07-08 18:35:08 +08:00
|
|
|
rtn->referenced = true;
|
2019-05-23 18:06:03 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-02-09 09:12:23 +08:00
|
|
|
DefinedGlobal *SymbolTable::addOptionalGlobalSymbol(StringRef name,
|
|
|
|
InputGlobal *global) {
|
2020-11-10 09:52:39 +08:00
|
|
|
Symbol *s = find(name);
|
|
|
|
if (!s || s->isDefined())
|
|
|
|
return nullptr;
|
2021-09-29 02:43:47 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addOptionalGlobalSymbol: " << name << " -> " << global
|
|
|
|
<< "\n");
|
2020-11-10 09:52:39 +08:00
|
|
|
syntheticGlobals.emplace_back(global);
|
2021-02-09 09:12:23 +08:00
|
|
|
return replaceSymbol<DefinedGlobal>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN,
|
|
|
|
nullptr, global);
|
2020-11-10 09:52:39 +08:00
|
|
|
}
|
|
|
|
|
2021-01-14 17:15:56 +08:00
|
|
|
DefinedTable *SymbolTable::addSyntheticTable(StringRef name, uint32_t flags,
|
|
|
|
InputTable *table) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addSyntheticTable: " << name << " -> " << table
|
|
|
|
<< "\n");
|
|
|
|
Symbol *s = find(name);
|
|
|
|
assert(!s || s->isUndefined());
|
|
|
|
if (!s)
|
|
|
|
s = insertName(name).first;
|
|
|
|
syntheticTables.emplace_back(table);
|
|
|
|
return replaceSymbol<DefinedTable>(s, name, flags, nullptr, table);
|
|
|
|
}
|
|
|
|
|
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-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&](Symbol *sym) {
|
2020-04-02 00:21:08 +08:00
|
|
|
// If the new defined function doesn't have signature (i.e. bitcode
|
2019-02-21 07:19:31 +08:00
|
|
|
// 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-07-10 17:10:01 +08:00
|
|
|
replaceSym(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-05-25 06:45:08 +08:00
|
|
|
bool checkSig = true;
|
|
|
|
if (auto ud = dyn_cast<UndefinedFunction>(existingFunction))
|
|
|
|
checkSig = ud->isCalledDirectly;
|
|
|
|
|
|
|
|
if (checkSig && function && !signatureMatches(existingFunction, &function->signature)) {
|
2019-02-21 07:19:31 +08:00
|
|
|
Symbol* variant;
|
|
|
|
if (getFunctionVariant(s, &function->signature, file, &variant))
|
|
|
|
// New variant, always replace
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym(variant);
|
2019-02-21 07:19:31 +08:00
|
|
|
else if (shouldReplace(s, file, flags))
|
|
|
|
// Variant already exists, replace it after checking shouldReplace
|
2019-07-10 17:10:01 +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.
|
|
|
|
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))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym(s);
|
2019-02-21 07:19:31 +08:00
|
|
|
|
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,
|
2021-05-15 07:25:04 +08:00
|
|
|
InputFile *file, InputChunk *segment,
|
2020-06-06 00:03:12 +08:00
|
|
|
uint64_t address, uint64_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-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
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()) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-02-28 08:09:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDataType(s, file);
|
|
|
|
|
|
|
|
if (shouldReplace(s, file, flags))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
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-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
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()) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-02-28 08:09:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkGlobalType(s, file, &global->getType());
|
|
|
|
|
|
|
|
if (shouldReplace(s, file, flags))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-02-23 13:08:53 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
Symbol *SymbolTable::addDefinedTag(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file, InputTag *tag) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedTag:" << name << "\n");
|
2018-12-08 14:17:43 +08:00
|
|
|
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
2021-06-15 16:49:43 +08:00
|
|
|
replaceSymbol<DefinedTag>(s, name, flags, file, tag);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (wasInserted || s->isLazy()) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-12-08 14:17:43 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
[WebAssembly] Remove WasmTagType
This removes `WasmTagType`. `WasmTagType` contained an attribute and a
signature index:
```
struct WasmTagType {
uint8_t Attribute;
uint32_t SigIndex;
};
```
Currently the attribute field is not used and reserved for future use,
and always 0. And that this class contains `SigIndex` as its property is
a little weird in the place, because the tag type's signature index is
not an inherent property of a tag but rather a reference to another
section that changes after linking. This makes tag handling in the
linker also weird that tag-related methods are taking both `WasmTagType`
and `WasmSignature` even though `WasmTagType` contains a signature
index. This is because the signature index changes in linking so it
doesn't have any info at this point. This instead moves `SigIndex` to
`struct WasmTag` itself, as we did for `struct WasmFunction` in D111104.
In this CL, in lib/MC and lib/Object, this now treats tag types in the
same way as function types. Also in YAML, this removes `struct Tag`,
because now it only contains the tag index. Also tags set `SigIndex` in
`WasmImport` union, as functions do.
I think this makes things simpler and makes tag handling more in line
with function handling. These two shares similar properties in that both
of them have signatures, but they are kind of nominal so having the same
signature doesn't mean they are the same element.
Also a drive-by fix: the reserved 'attirubute' part's encoding changed
from uleb32 to uint8 a while ago. This was fixed in lib/MC and
lib/Object but not in YAML. This doesn't change object files because the
field's value is always 0 and its encoding is the same for the both
encoding.
This is effectively NFC; I didn't mark it as such just because it
changed YAML test results.
Reviewed By: sbc100, tlively
Differential Revision: https://reviews.llvm.org/D111086
2021-10-02 10:07:41 +08:00
|
|
|
checkTagType(s, file, &tag->signature);
|
2018-12-08 14:17:43 +08:00
|
|
|
|
|
|
|
if (shouldReplace(s, file, flags))
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2018-12-08 14:17:43 +08:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-01-05 19:08:58 +08:00
|
|
|
Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags,
|
|
|
|
InputFile *file, InputTable *table) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addDefinedTable:" << name << "\n");
|
|
|
|
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
|
|
|
|
auto replaceSym = [&]() {
|
|
|
|
replaceSymbol<DefinedTable>(s, name, flags, file, table);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (wasInserted || s->isLazy()) {
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkTableType(s, file, &table->getType());
|
|
|
|
|
|
|
|
if (shouldReplace(s, file, flags))
|
|
|
|
replaceSym();
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-10-17 13:16:54 +08:00
|
|
|
// This function get called when an undefined symbol is added, and there is
|
|
|
|
// already an existing one in the symbols table. In this case we check that
|
|
|
|
// custom 'import-module' and 'import-field' symbol attributes agree.
|
2020-01-07 02:21:05 +08:00
|
|
|
// With LTO these attributes are not available when the bitcode is read and only
|
2019-10-17 13:16:54 +08:00
|
|
|
// become available when the LTO object is read. In this case we silently
|
|
|
|
// replace the empty attributes with the valid ones.
|
|
|
|
template <typename T>
|
2020-02-06 13:18:55 +08:00
|
|
|
static void setImportAttributes(T *existing, Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
2020-02-28 09:32:22 +08:00
|
|
|
uint32_t flags, InputFile *file) {
|
2020-02-06 13:18:55 +08:00
|
|
|
if (importName) {
|
|
|
|
if (!existing->importName)
|
2019-10-17 13:16:54 +08:00
|
|
|
existing->importName = importName;
|
|
|
|
if (existing->importName != importName)
|
|
|
|
error("import name mismatch for symbol: " + toString(*existing) +
|
2020-02-06 13:18:55 +08:00
|
|
|
"\n>>> defined as " + *existing->importName + " in " +
|
|
|
|
toString(existing->getFile()) + "\n>>> defined as " + *importName +
|
2019-10-17 13:16:54 +08:00
|
|
|
" in " + toString(file));
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:18:55 +08:00
|
|
|
if (importModule) {
|
|
|
|
if (!existing->importModule)
|
2019-10-17 13:16:54 +08:00
|
|
|
existing->importModule = importModule;
|
|
|
|
if (existing->importModule != importModule)
|
|
|
|
error("import module mismatch for symbol: " + toString(*existing) +
|
2020-02-06 13:18:55 +08:00
|
|
|
"\n>>> defined as " + *existing->importModule + " in " +
|
|
|
|
toString(existing->getFile()) + "\n>>> defined as " +
|
|
|
|
*importModule + " in " + toString(file));
|
2019-10-17 13:16:54 +08:00
|
|
|
}
|
2020-02-28 09:32:22 +08:00
|
|
|
|
|
|
|
// Update symbol binding, if the existing symbol is weak
|
|
|
|
uint32_t binding = flags & WASM_SYMBOL_BINDING_MASK;
|
|
|
|
if (existing->isWeak() && binding != WASM_SYMBOL_BINDING_WEAK) {
|
|
|
|
existing->flags = (existing->flags & ~WASM_SYMBOL_BINDING_MASK) | binding;
|
|
|
|
}
|
2019-10-17 13:16:54 +08:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:18:55 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedFunction(StringRef name,
|
|
|
|
Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
2019-02-01 10:29:57 +08:00
|
|
|
uint32_t flags, InputFile *file,
|
2019-05-25 06:45:08 +08:00
|
|
|
const WasmSignature *sig,
|
|
|
|
bool isCalledDirectly) {
|
2019-05-29 23:36:42 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " ["
|
|
|
|
<< (sig ? toString(*sig) : "none")
|
2020-02-28 09:32:22 +08:00
|
|
|
<< "] IsCalledDirectly:" << isCalledDirectly << " flags=0x"
|
|
|
|
<< utohexstr(flags) << "\n");
|
2019-09-24 05:28:29 +08:00
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
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-07-10 17:10:01 +08:00
|
|
|
auto replaceSym = [&]() {
|
2019-02-08 06:00:48 +08:00
|
|
|
replaceSymbol<UndefinedFunction>(s, name, importName, importModule, flags,
|
2019-05-25 06:45:08 +08:00
|
|
|
file, sig, isCalledDirectly);
|
2019-02-21 07:19:31 +08:00
|
|
|
};
|
|
|
|
|
2020-08-08 07:12:33 +08:00
|
|
|
if (wasInserted) {
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2020-08-08 07:12:33 +08:00
|
|
|
} else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
|
|
|
|
if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
|
|
|
|
lazy->setWeak();
|
|
|
|
lazy->signature = sig;
|
|
|
|
} else {
|
|
|
|
lazy->fetch();
|
|
|
|
}
|
|
|
|
} else {
|
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;
|
|
|
|
}
|
2019-05-29 23:36:42 +08:00
|
|
|
if (!existingFunction->signature && sig)
|
|
|
|
existingFunction->signature = sig;
|
2020-02-28 09:32:22 +08:00
|
|
|
auto *existingUndefined = dyn_cast<UndefinedFunction>(existingFunction);
|
2019-08-31 03:50:59 +08:00
|
|
|
if (isCalledDirectly && !signatureMatches(existingFunction, sig)) {
|
2020-02-28 09:32:22 +08:00
|
|
|
// If the existing undefined functions is not called directly then let
|
2019-08-31 03:50:59 +08:00
|
|
|
// this one take precedence. Otherwise the existing function is either
|
2020-02-28 09:32:22 +08:00
|
|
|
// directly called or defined, in which case we need a function variant.
|
2019-08-31 03:50:59 +08:00
|
|
|
if (existingUndefined && !existingUndefined->isCalledDirectly)
|
2019-07-10 17:10:01 +08:00
|
|
|
replaceSym();
|
2019-08-31 03:50:59 +08:00
|
|
|
else if (getFunctionVariant(s, sig, file, &s))
|
|
|
|
replaceSym();
|
|
|
|
}
|
2021-08-19 10:20:55 +08:00
|
|
|
if (existingUndefined) {
|
2020-02-28 09:32:22 +08:00
|
|
|
setImportAttributes(existingUndefined, importName, importModule, flags,
|
|
|
|
file);
|
2021-08-19 10:20:55 +08:00
|
|
|
if (isCalledDirectly)
|
|
|
|
existingUndefined->isCalledDirectly = true;
|
|
|
|
}
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
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");
|
2019-09-24 05:28:29 +08:00
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
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
|
|
|
|
2020-08-08 07:12:33 +08:00
|
|
|
if (wasInserted) {
|
2018-02-28 08:09:22 +08:00
|
|
|
replaceSymbol<UndefinedData>(s, name, flags, file);
|
2020-08-08 07:12:33 +08:00
|
|
|
} else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
|
|
|
|
if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK)
|
|
|
|
lazy->setWeak();
|
|
|
|
else
|
|
|
|
lazy->fetch();
|
|
|
|
} else if (s->isDefined()) {
|
2018-02-28 08:09:22 +08:00
|
|
|
checkDataType(s, file);
|
2020-08-08 07:12:33 +08:00
|
|
|
}
|
2018-02-28 08:09:22 +08:00
|
|
|
return s;
|
|
|
|
}
|
2018-02-21 05:08:47 +08:00
|
|
|
|
2020-02-06 13:18:55 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
|
|
|
|
Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
2018-02-28 08:09:22 +08:00
|
|
|
const WasmGlobalType *type) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");
|
2019-09-24 05:28:29 +08:00
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-05 19:08:58 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedTable(StringRef name,
|
|
|
|
Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
|
|
|
const WasmTableType *type) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n");
|
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
|
|
|
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
|
|
|
|
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<UndefinedTable>(s, name, importName, importModule, flags,
|
|
|
|
file, type);
|
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
|
|
|
lazy->fetch();
|
|
|
|
else if (s->isDefined())
|
|
|
|
checkTableType(s, file, type);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-09-30 05:17:14 +08:00
|
|
|
Symbol *SymbolTable::addUndefinedTag(StringRef name,
|
|
|
|
Optional<StringRef> importName,
|
|
|
|
Optional<StringRef> importModule,
|
|
|
|
uint32_t flags, InputFile *file,
|
|
|
|
const WasmSignature *sig) {
|
|
|
|
LLVM_DEBUG(dbgs() << "addUndefinedTag: " << name << "\n");
|
|
|
|
assert(flags & WASM_SYMBOL_UNDEFINED);
|
|
|
|
|
|
|
|
Symbol *s;
|
|
|
|
bool wasInserted;
|
|
|
|
std::tie(s, wasInserted) = insert(name, file);
|
|
|
|
if (s->traced)
|
|
|
|
printTraceSymbolUndefined(name, file);
|
|
|
|
|
|
|
|
if (wasInserted)
|
|
|
|
replaceSymbol<UndefinedTag>(s, name, importName, importModule, flags, file,
|
|
|
|
sig);
|
|
|
|
else if (auto *lazy = dyn_cast<LazySymbol>(s))
|
|
|
|
lazy->fetch();
|
|
|
|
else if (s->isDefined())
|
|
|
|
checkTagType(s, file, sig);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-03-03 18:13:25 +08:00
|
|
|
TableSymbol *SymbolTable::createUndefinedIndirectFunctionTable(StringRef name) {
|
|
|
|
WasmLimits limits{0, 0, 0}; // Set by the writer.
|
|
|
|
WasmTableType *type = make<WasmTableType>();
|
|
|
|
type->ElemType = uint8_t(ValType::FUNCREF);
|
|
|
|
type->Limits = limits;
|
|
|
|
StringRef module(defaultModule);
|
|
|
|
uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
|
|
|
|
flags |= WASM_SYMBOL_UNDEFINED;
|
|
|
|
Symbol *sym = addUndefinedTable(name, name, module, flags, nullptr, type);
|
|
|
|
sym->markLive();
|
|
|
|
sym->forceExport = config->exportTable;
|
|
|
|
return cast<TableSymbol>(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
TableSymbol *SymbolTable::createDefinedIndirectFunctionTable(StringRef name) {
|
|
|
|
const uint32_t invalidIndex = -1;
|
|
|
|
WasmLimits limits{0, 0, 0}; // Set by the writer.
|
|
|
|
WasmTableType type{uint8_t(ValType::FUNCREF), limits};
|
|
|
|
WasmTable desc{invalidIndex, type, name};
|
|
|
|
InputTable *table = make<InputTable>(desc, nullptr);
|
|
|
|
uint32_t flags = config->exportTable ? 0 : WASM_SYMBOL_VISIBILITY_HIDDEN;
|
|
|
|
TableSymbol *sym = addSyntheticTable(name, flags, table);
|
|
|
|
sym->markLive();
|
|
|
|
sym->forceExport = config->exportTable;
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whether or not we need an indirect function table is usually a function of
|
|
|
|
// whether an input declares a need for it. However sometimes it's possible for
|
|
|
|
// no input to need the indirect function table, but then a late
|
|
|
|
// addInternalGOTEntry causes a function to be allocated an address. In that
|
|
|
|
// case address we synthesize a definition at the last minute.
|
|
|
|
TableSymbol *SymbolTable::resolveIndirectFunctionTable(bool required) {
|
|
|
|
Symbol *existing = find(functionTableName);
|
|
|
|
if (existing) {
|
|
|
|
if (!isa<TableSymbol>(existing)) {
|
|
|
|
error(Twine("reserved symbol must be of type table: `") +
|
|
|
|
functionTableName + "`");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (existing->isDefined()) {
|
|
|
|
error(Twine("reserved symbol must not be defined in input files: `") +
|
|
|
|
functionTableName + "`");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config->importTable) {
|
|
|
|
if (existing)
|
|
|
|
return cast<TableSymbol>(existing);
|
|
|
|
if (required)
|
|
|
|
return createUndefinedIndirectFunctionTable(functionTableName);
|
|
|
|
} else if ((existing && existing->isLive()) || config->exportTable ||
|
|
|
|
required) {
|
|
|
|
// A defined table is required. Either because the user request an exported
|
|
|
|
// table or because the table symbol is already live. The existing table is
|
|
|
|
// guaranteed to be undefined due to the check above.
|
|
|
|
return createDefinedIndirectFunctionTable(functionTableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// An indirect function table will only be present in the symbol table if
|
|
|
|
// needed by a reloc; if we get here, we don't need one.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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,
|
2020-01-07 02:21:05 +08:00
|
|
|
// unless the existing symbol is weak in which case replace the undefined
|
2019-01-30 06:26:31 +08:00
|
|
|
// 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>());
|
2020-05-12 08:39:04 +08:00
|
|
|
variant->isUsedInRegularObj =
|
|
|
|
!file || file->kind() == InputFile::ObjectKind;
|
|
|
|
variant->canInline = true;
|
|
|
|
variant->traced = false;
|
|
|
|
variant->forceExport = false;
|
2019-02-21 07:19:31 +08:00
|
|
|
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
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
void SymbolTable::wrap(Symbol *sym, Symbol *real, Symbol *wrap) {
|
|
|
|
// Swap symbols as instructed by -wrap.
|
|
|
|
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");
|
|
|
|
|
|
|
|
// Anyone looking up __real symbols should get the original
|
|
|
|
realIdx = origIdx;
|
|
|
|
// Anyone looking up the original should get the __wrap symbol
|
|
|
|
origIdx = wrapIdx;
|
|
|
|
}
|
|
|
|
|
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);
|
2020-04-24 08:57:00 +08:00
|
|
|
// Mark new symbols as local. For relocatable output we don't want them
|
|
|
|
// to be exported outside the object file.
|
|
|
|
replaceSymbol<DefinedFunction>(sym, debugName, WASM_SYMBOL_BINDING_LOCAL,
|
|
|
|
nullptr, func);
|
2020-11-24 07:41:07 +08:00
|
|
|
// Ensure the stub function doesn't get a table entry. Its address
|
|
|
|
// should always compare equal to the null pointer.
|
|
|
|
sym->isStub = true;
|
2019-02-08 06:42:16 +08:00
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2020-05-02 00:14:59 +08:00
|
|
|
void SymbolTable::replaceWithUndefined(Symbol *sym) {
|
|
|
|
// Add a synthetic dummy for weak undefined functions. These dummies will
|
|
|
|
// be GC'd if not used as the target of any "call" instructions.
|
2022-01-21 03:53:18 +08:00
|
|
|
StringRef debugName = saver().save("undefined_weak:" + toString(*sym));
|
2020-05-02 00:14:59 +08:00
|
|
|
replaceWithUnreachable(sym, *sym->getSignature(), debugName);
|
|
|
|
// Hide our dummy to prevent export.
|
|
|
|
sym->setHidden(true);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
for (Symbol *sym : getSymbols()) {
|
2022-05-27 23:34:31 +08:00
|
|
|
if (sym->isUndefWeak() && sym->isUsedInRegularObj) {
|
2020-05-02 00:14:59 +08:00
|
|
|
if (sym->getSignature()) {
|
|
|
|
replaceWithUndefined(sym);
|
|
|
|
} else {
|
|
|
|
// It is possible for undefined functions not to have a signature (eg.
|
|
|
|
// if added via "--undefined"), but weak undefined ones do have a
|
|
|
|
// signature. Lazy symbols may not be functions and therefore Sig can
|
|
|
|
// still be null in some circumstance.
|
|
|
|
assert(!isa<FunctionSymbol>(sym));
|
|
|
|
}
|
2019-02-21 07:19:31 +08:00
|
|
|
}
|
2019-02-08 06:42:16 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-21 07:19:31 +08:00
|
|
|
|
2020-05-02 00:14:59 +08:00
|
|
|
DefinedFunction *SymbolTable::createUndefinedStub(const WasmSignature &sig) {
|
|
|
|
if (stubFunctions.count(sig))
|
|
|
|
return stubFunctions[sig];
|
|
|
|
LLVM_DEBUG(dbgs() << "createUndefinedStub: " << toString(sig) << "\n");
|
|
|
|
auto *sym = reinterpret_cast<DefinedFunction *>(make<SymbolUnion>());
|
|
|
|
sym->isUsedInRegularObj = true;
|
|
|
|
sym->canInline = true;
|
|
|
|
sym->traced = false;
|
|
|
|
sym->forceExport = false;
|
|
|
|
sym->signature = &sig;
|
|
|
|
replaceSymbol<DefinedFunction>(
|
|
|
|
sym, "undefined_stub", WASM_SYMBOL_VISIBILITY_HIDDEN, nullptr, nullptr);
|
|
|
|
replaceWithUnreachable(sym, sig, "undefined_stub");
|
|
|
|
stubFunctions[sig] = sym;
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
static void reportFunctionSignatureMismatch(StringRef symName,
|
|
|
|
FunctionSymbol *a,
|
2019-07-10 17:10:01 +08:00
|
|
|
FunctionSymbol *b, bool isError) {
|
2019-02-21 07:19:31 +08:00
|
|
|
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();
|
2019-07-10 17:10:01 +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() {
|
|
|
|
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);
|
2022-01-21 03:53:18 +08:00
|
|
|
StringRef debugName =
|
|
|
|
saver().save("signature_mismatch:" + toString(*f));
|
2019-02-21 07:19:31 +08:00
|
|
|
replaceWithUnreachable(f, *f->signature, debugName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 13:25:39 +08:00
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|