2017-11-17 18:14:09 +00:00
|
|
|
//===- Symbols.cpp --------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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-17 18:14:09 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 01:13:34 +00:00
|
|
|
#include "InputChunks.h"
|
2021-02-11 11:15:24 +00:00
|
|
|
#include "InputElement.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "InputFiles.h"
|
2019-05-21 09:13:09 +00:00
|
|
|
#include "OutputSections.h"
|
2018-02-23 05:08:53 +00:00
|
|
|
#include "OutputSegment.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2021-01-05 11:08:58 +00:00
|
|
|
#include "lld/Common/Memory.h"
|
2017-12-06 03:10:39 +00:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-11-17 18:14:09 +00:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2019-12-20 01:23:59 +00:00
|
|
|
using namespace llvm::object;
|
2018-01-10 00:52:20 +00:00
|
|
|
using namespace llvm::wasm;
|
2022-01-05 03:05:10 +00:00
|
|
|
using namespace lld::wasm;
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2019-10-10 05:25:39 +00:00
|
|
|
namespace lld {
|
|
|
|
std::string toString(const wasm::Symbol &sym) {
|
|
|
|
return maybeDemangleSymbol(sym.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string maybeDemangleSymbol(StringRef name) {
|
2020-02-27 15:51:37 +00:00
|
|
|
// WebAssembly requires caller and callee signatures to match, so we mangle
|
|
|
|
// `main` in the case where we need to pass it arguments.
|
|
|
|
if (name == "__main_argc_argv")
|
|
|
|
return "main";
|
2022-01-05 03:05:10 +00:00
|
|
|
|
|
|
|
return demangle(name, config->demangle);
|
2019-10-10 05:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string toString(wasm::Symbol::Kind kind) {
|
|
|
|
switch (kind) {
|
|
|
|
case wasm::Symbol::DefinedFunctionKind:
|
|
|
|
return "DefinedFunction";
|
|
|
|
case wasm::Symbol::DefinedDataKind:
|
|
|
|
return "DefinedData";
|
|
|
|
case wasm::Symbol::DefinedGlobalKind:
|
|
|
|
return "DefinedGlobal";
|
2021-01-05 11:08:58 +00:00
|
|
|
case wasm::Symbol::DefinedTableKind:
|
|
|
|
return "DefinedTable";
|
2021-06-15 08:49:43 +00:00
|
|
|
case wasm::Symbol::DefinedTagKind:
|
|
|
|
return "DefinedTag";
|
2019-10-10 05:25:39 +00:00
|
|
|
case wasm::Symbol::UndefinedFunctionKind:
|
|
|
|
return "UndefinedFunction";
|
|
|
|
case wasm::Symbol::UndefinedDataKind:
|
|
|
|
return "UndefinedData";
|
|
|
|
case wasm::Symbol::UndefinedGlobalKind:
|
|
|
|
return "UndefinedGlobal";
|
2021-01-05 11:08:58 +00:00
|
|
|
case wasm::Symbol::UndefinedTableKind:
|
|
|
|
return "UndefinedTable";
|
2021-09-29 21:17:14 +00:00
|
|
|
case wasm::Symbol::UndefinedTagKind:
|
|
|
|
return "UndefinedTag";
|
2019-10-10 05:25:39 +00:00
|
|
|
case wasm::Symbol::LazyKind:
|
|
|
|
return "LazyKind";
|
|
|
|
case wasm::Symbol::SectionKind:
|
|
|
|
return "SectionKind";
|
|
|
|
case wasm::Symbol::OutputSectionKind:
|
|
|
|
return "OutputSectionKind";
|
|
|
|
}
|
|
|
|
llvm_unreachable("invalid symbol kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace wasm {
|
2018-02-14 18:27:59 +00:00
|
|
|
DefinedFunction *WasmSym::callCtors;
|
2020-10-01 00:21:57 +00:00
|
|
|
DefinedFunction *WasmSym::callDtors;
|
2019-07-03 22:04:54 +00:00
|
|
|
DefinedFunction *WasmSym::initMemory;
|
2020-12-10 02:14:31 +00:00
|
|
|
DefinedFunction *WasmSym::applyDataRelocs;
|
|
|
|
DefinedFunction *WasmSym::applyGlobalRelocs;
|
2021-08-26 19:29:32 +00:00
|
|
|
DefinedFunction *WasmSym::applyGlobalTLSRelocs;
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-16 22:00:45 +00:00
|
|
|
DefinedFunction *WasmSym::initTLS;
|
2020-12-10 02:14:31 +00:00
|
|
|
DefinedFunction *WasmSym::startFunction;
|
2018-02-20 23:38:27 +00:00
|
|
|
DefinedData *WasmSym::dsoHandle;
|
|
|
|
DefinedData *WasmSym::dataEnd;
|
2019-06-26 20:12:33 +00:00
|
|
|
DefinedData *WasmSym::globalBase;
|
2018-02-20 23:38:27 +00:00
|
|
|
DefinedData *WasmSym::heapBase;
|
2019-09-04 19:50:39 +00:00
|
|
|
DefinedData *WasmSym::initMemoryFlag;
|
2018-11-15 18:15:54 +00:00
|
|
|
GlobalSymbol *WasmSym::stackPointer;
|
[WebAssembly] Implement thread-local storage (local-exec model)
Summary:
Thread local variables are placed inside a `.tdata` segment. Their symbols are
offsets from the start of the segment. The address of a thread local variable
is computed as `__tls_base` + the offset from the start of the segment.
`.tdata` segment is a passive segment and `memory.init` is used once per thread
to initialize the thread local storage.
`__tls_base` is a wasm global. Since each thread has its own wasm instance,
it is effectively thread local. Currently, `__tls_base` must be initialized
at thread startup, and so cannot be used with dynamic libraries.
`__tls_base` is to be initialized with a new linker-synthesized function,
`__wasm_init_tls`, which takes as an argument a block of memory to use as the
storage for thread locals. It then initializes the block of memory and sets
`__tls_base`. As `__wasm_init_tls` will handle the memory initialization,
the memory does not have to be zeroed.
To help allocating memory for thread-local storage, a new compiler intrinsic
is introduced: `__builtin_wasm_tls_size()`. This instrinsic function returns
the size of the thread-local storage for the current function.
The expected usage is to run something like the following upon thread startup:
__wasm_init_tls(malloc(__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, kripken, sbc100
Subscribers: dschuff, jgravelle-google, hiraditya, sunfish, jfb, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D64537
llvm-svn: 366272
2019-07-16 22:00:45 +00:00
|
|
|
GlobalSymbol *WasmSym::tlsBase;
|
|
|
|
GlobalSymbol *WasmSym::tlsSize;
|
[WebAssembly] Compute and export TLS block alignment
Summary:
Add immutable WASM global `__tls_align` which stores the alignment
requirements of the TLS segment.
Add `__builtin_wasm_tls_align()` intrinsic to get this alignment in Clang.
The expected usage has now changed to:
__wasm_init_tls(memalign(__builtin_wasm_tls_align(),
__builtin_wasm_tls_size()));
Reviewers: tlively, aheejin, sbc100, sunfish, alexcrichton
Reviewed By: tlively
Subscribers: dschuff, jgravelle-google, hiraditya, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D65028
llvm-svn: 366624
2019-07-19 23:34:16 +00:00
|
|
|
GlobalSymbol *WasmSym::tlsAlign;
|
2018-11-15 18:15:54 +00:00
|
|
|
UndefinedGlobal *WasmSym::tableBase;
|
2019-08-13 17:02:02 +00:00
|
|
|
DefinedData *WasmSym::definedTableBase;
|
2021-04-22 23:54:58 +00:00
|
|
|
UndefinedGlobal *WasmSym::tableBase32;
|
|
|
|
DefinedData *WasmSym::definedTableBase32;
|
2018-11-15 18:15:54 +00:00
|
|
|
UndefinedGlobal *WasmSym::memoryBase;
|
2019-08-13 17:02:02 +00:00
|
|
|
DefinedData *WasmSym::definedMemoryBase;
|
2021-01-14 09:15:56 +00:00
|
|
|
TableSymbol *WasmSym::indirectFunctionTable;
|
2018-02-23 05:08:53 +00:00
|
|
|
|
|
|
|
WasmSymbolType Symbol::getWasmType() const {
|
2018-02-28 01:10:50 +00:00
|
|
|
if (isa<FunctionSymbol>(this))
|
2018-05-14 22:42:33 +00:00
|
|
|
return WASM_SYMBOL_TYPE_FUNCTION;
|
2018-02-28 01:10:50 +00:00
|
|
|
if (isa<DataSymbol>(this))
|
2018-05-14 22:42:33 +00:00
|
|
|
return WASM_SYMBOL_TYPE_DATA;
|
2018-02-28 01:10:50 +00:00
|
|
|
if (isa<GlobalSymbol>(this))
|
2018-05-14 22:42:33 +00:00
|
|
|
return WASM_SYMBOL_TYPE_GLOBAL;
|
2021-06-15 08:49:43 +00:00
|
|
|
if (isa<TagSymbol>(this))
|
|
|
|
return WASM_SYMBOL_TYPE_TAG;
|
2021-01-05 11:08:58 +00:00
|
|
|
if (isa<TableSymbol>(this))
|
|
|
|
return WASM_SYMBOL_TYPE_TABLE;
|
2019-05-21 09:13:09 +00:00
|
|
|
if (isa<SectionSymbol>(this) || isa<OutputSectionSymbol>(this))
|
2018-05-14 22:42:33 +00:00
|
|
|
return WASM_SYMBOL_TYPE_SECTION;
|
2018-02-28 01:10:50 +00:00
|
|
|
llvm_unreachable("invalid symbol kind");
|
2018-02-23 05:08:53 +00:00
|
|
|
}
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2019-02-20 23:19:31 +00:00
|
|
|
const WasmSignature *Symbol::getSignature() const {
|
|
|
|
if (auto* f = dyn_cast<FunctionSymbol>(this))
|
|
|
|
return f->signature;
|
2021-09-29 21:17:14 +00:00
|
|
|
if (auto *t = dyn_cast<TagSymbol>(this))
|
|
|
|
return t->signature;
|
2019-02-20 23:19:31 +00:00
|
|
|
if (auto *l = dyn_cast<LazySymbol>(this))
|
|
|
|
return l->signature;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-20 17:45:38 +00:00
|
|
|
InputChunk *Symbol::getChunk() const {
|
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function;
|
2020-05-01 16:14:59 +00:00
|
|
|
if (auto *f = dyn_cast<UndefinedFunction>(this))
|
|
|
|
if (f->stubFunction)
|
|
|
|
return f->stubFunction->function;
|
2018-02-23 05:08:53 +00:00
|
|
|
if (auto *d = dyn_cast<DefinedData>(this))
|
|
|
|
return d->segment;
|
2018-02-20 17:45:38 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-07 06:00:46 +00:00
|
|
|
bool Symbol::isDiscarded() const {
|
|
|
|
if (InputChunk *c = getChunk())
|
|
|
|
return c->discarded;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-23 05:08:53 +00:00
|
|
|
bool Symbol::isLive() const {
|
|
|
|
if (auto *g = dyn_cast<DefinedGlobal>(this))
|
|
|
|
return g->global->live;
|
2021-06-15 08:49:43 +00:00
|
|
|
if (auto *t = dyn_cast<DefinedTag>(this))
|
|
|
|
return t->tag->live;
|
2021-01-05 11:08:58 +00:00
|
|
|
if (auto *t = dyn_cast<DefinedTable>(this))
|
|
|
|
return t->table->live;
|
2018-02-23 05:08:53 +00:00
|
|
|
if (InputChunk *c = getChunk())
|
|
|
|
return c->live;
|
2018-04-20 17:18:06 +00:00
|
|
|
return referenced;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Symbol::markLive() {
|
2019-06-07 06:00:46 +00:00
|
|
|
assert(!isDiscarded());
|
2021-02-27 00:09:32 +00:00
|
|
|
referenced = true;
|
2022-01-02 18:20:21 +00:00
|
|
|
if (file != nullptr && isDefined())
|
2020-10-01 03:00:04 +00:00
|
|
|
file->markLive();
|
2018-04-20 17:18:06 +00:00
|
|
|
if (auto *g = dyn_cast<DefinedGlobal>(this))
|
|
|
|
g->global->live = true;
|
2021-06-15 08:49:43 +00:00
|
|
|
if (auto *t = dyn_cast<DefinedTag>(this))
|
|
|
|
t->tag->live = true;
|
2021-01-05 11:08:58 +00:00
|
|
|
if (auto *t = dyn_cast<DefinedTable>(this))
|
|
|
|
t->table->live = true;
|
2021-02-27 00:09:32 +00:00
|
|
|
if (InputChunk *c = getChunk()) {
|
|
|
|
// Usually, a whole chunk is marked as live or dead, but in mergeable
|
|
|
|
// (splittable) sections, each piece of data has independent liveness bit.
|
|
|
|
// So we explicitly tell it which offset is in use.
|
|
|
|
if (auto *d = dyn_cast<DefinedData>(this)) {
|
2021-05-14 23:25:04 +00:00
|
|
|
if (auto *ms = dyn_cast<MergeInputChunk>(c)) {
|
|
|
|
ms->getSectionPiece(d->value)->live = true;
|
2021-02-27 00:09:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-20 17:18:06 +00:00
|
|
|
c->live = true;
|
2021-02-27 00:09:32 +00:00
|
|
|
}
|
2018-02-23 05:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Symbol::getOutputSymbolIndex() const {
|
|
|
|
assert(outputSymbolIndex != INVALID_INDEX);
|
|
|
|
return outputSymbolIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Symbol::setOutputSymbolIndex(uint32_t index) {
|
2018-05-15 13:36:20 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "setOutputSymbolIndex " << name << " -> " << index
|
|
|
|
<< "\n");
|
2018-02-23 05:08:53 +00:00
|
|
|
assert(outputSymbolIndex == INVALID_INDEX);
|
|
|
|
outputSymbolIndex = index;
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:46:15 +00:00
|
|
|
void Symbol::setGOTIndex(uint32_t index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setGOTIndex " << name << " -> " << index << "\n");
|
|
|
|
assert(gotIndex == INVALID_INDEX);
|
|
|
|
gotIndex = index;
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
bool Symbol::isWeak() const {
|
|
|
|
return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbol::isLocal() const {
|
|
|
|
return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_LOCAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbol::isHidden() const {
|
|
|
|
return (flags & WASM_SYMBOL_VISIBILITY_MASK) == WASM_SYMBOL_VISIBILITY_HIDDEN;
|
|
|
|
}
|
|
|
|
|
2021-09-08 08:53:13 +00:00
|
|
|
bool Symbol::isTLS() const { return flags & WASM_SYMBOL_TLS; }
|
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
void Symbol::setHidden(bool isHidden) {
|
2018-05-15 13:36:20 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "setHidden: " << name << " -> " << isHidden << "\n");
|
2018-02-14 18:27:59 +00:00
|
|
|
flags &= ~WASM_SYMBOL_VISIBILITY_MASK;
|
|
|
|
if (isHidden)
|
|
|
|
flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
|
|
|
|
else
|
|
|
|
flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2018-06-28 17:04:58 +00:00
|
|
|
bool Symbol::isExported() const {
|
2021-08-18 16:57:34 +00:00
|
|
|
// Shared libraries must export all weakly defined symbols
|
2021-10-27 13:52:17 +00:00
|
|
|
// in case they contain the version that will be chosen by
|
2021-08-18 16:57:34 +00:00
|
|
|
// the dynamic linker.
|
|
|
|
if (config->shared && isLive() && isDefined() && isWeak())
|
|
|
|
return true;
|
|
|
|
|
2018-06-28 17:04:58 +00:00
|
|
|
if (!isDefined() || isLocal())
|
|
|
|
return false;
|
|
|
|
|
2021-05-07 03:29:05 +00:00
|
|
|
if (config->exportAll || (config->exportDynamic && !isHidden()))
|
2018-06-28 17:04:58 +00:00
|
|
|
return true;
|
|
|
|
|
2021-05-07 03:29:05 +00:00
|
|
|
return isExportedExplicit();
|
|
|
|
}
|
2018-09-25 21:50:15 +00:00
|
|
|
|
2021-05-07 03:29:05 +00:00
|
|
|
bool Symbol::isExportedExplicit() const {
|
|
|
|
return forceExport || flags & WASM_SYMBOL_EXPORTED;
|
2018-06-28 17:04:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 22:41:05 +00:00
|
|
|
bool Symbol::isNoStrip() const {
|
|
|
|
return flags & WASM_SYMBOL_NO_STRIP;
|
|
|
|
}
|
|
|
|
|
2018-03-12 19:56:23 +00:00
|
|
|
uint32_t FunctionSymbol::getFunctionIndex() const {
|
2021-08-18 16:57:34 +00:00
|
|
|
if (const auto *u = dyn_cast<UndefinedFunction>(this))
|
|
|
|
if (u->stubFunction)
|
2020-05-01 16:14:59 +00:00
|
|
|
return u->stubFunction->getFunctionIndex();
|
2021-08-18 16:57:34 +00:00
|
|
|
if (functionIndex != INVALID_INDEX)
|
|
|
|
return functionIndex;
|
|
|
|
auto *f = cast<DefinedFunction>(this);
|
|
|
|
return f->function->getFunctionIndex();
|
2018-03-12 19:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionSymbol::setFunctionIndex(uint32_t index) {
|
2018-05-15 13:36:20 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "setFunctionIndex " << name << " -> " << index << "\n");
|
2018-03-12 19:56:23 +00:00
|
|
|
assert(functionIndex == INVALID_INDEX);
|
|
|
|
functionIndex = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FunctionSymbol::hasFunctionIndex() const {
|
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function->hasFunctionIndex();
|
|
|
|
return functionIndex != INVALID_INDEX;
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
uint32_t FunctionSymbol::getTableIndex() const {
|
2018-02-20 17:45:38 +00:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function->getTableIndex();
|
2018-02-14 22:55:38 +00:00
|
|
|
assert(tableIndex != INVALID_INDEX);
|
|
|
|
return tableIndex;
|
2018-01-24 21:45:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
bool FunctionSymbol::hasTableIndex() const {
|
2018-02-20 17:45:38 +00:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function->hasTableIndex();
|
2018-02-14 22:55:38 +00:00
|
|
|
return tableIndex != INVALID_INDEX;
|
2018-01-24 21:45:25 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 18:27:59 +00:00
|
|
|
void FunctionSymbol::setTableIndex(uint32_t index) {
|
2018-01-24 21:45:25 +00:00
|
|
|
// For imports, we set the table index here on the Symbol; for defined
|
|
|
|
// functions we set the index on the InputFunction so that we don't export
|
|
|
|
// the same thing twice (keeps the table size down).
|
2018-02-20 17:45:38 +00:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this)) {
|
|
|
|
f->function->setTableIndex(index);
|
2018-01-24 21:45:25 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-15 13:36:20 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "setTableIndex " << name << " -> " << index << "\n");
|
2018-02-14 22:55:38 +00:00
|
|
|
assert(tableIndex == INVALID_INDEX);
|
2017-12-11 22:00:56 +00:00
|
|
|
tableIndex = index;
|
|
|
|
}
|
2017-11-17 18:14:09 +00:00
|
|
|
|
2018-02-20 17:45:38 +00:00
|
|
|
DefinedFunction::DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
|
|
|
|
InputFunction *function)
|
|
|
|
: FunctionSymbol(name, DefinedFunctionKind, flags, f,
|
|
|
|
function ? &function->signature : nullptr),
|
|
|
|
function(function) {}
|
|
|
|
|
2021-08-18 16:57:34 +00:00
|
|
|
uint32_t DefinedFunction::getExportedFunctionIndex() const {
|
|
|
|
return function->getFunctionIndex();
|
|
|
|
}
|
|
|
|
|
2021-05-11 15:58:13 +00:00
|
|
|
uint64_t DefinedData::getVA() const {
|
2021-02-26 23:22:23 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "getVA: " << getName() << "\n");
|
2021-10-29 16:13:40 +00:00
|
|
|
// In the shared memory case, TLS symbols are relative to the start of the TLS
|
|
|
|
// output segment (__tls_base). When building without shared memory, TLS
|
|
|
|
// symbols absolute, just like non-TLS.
|
|
|
|
if (isTLS() && config->sharedMemory)
|
|
|
|
return getOutputSegmentOffset() + value;
|
2020-11-11 01:46:52 +00:00
|
|
|
if (segment)
|
2021-05-11 15:58:13 +00:00
|
|
|
return segment->getVA(value);
|
2021-02-26 23:22:23 +00:00
|
|
|
return value;
|
2018-01-10 00:52:20 +00:00
|
|
|
}
|
2017-12-03 02:38:04 +00:00
|
|
|
|
2021-02-26 23:22:23 +00:00
|
|
|
void DefinedData::setVA(uint64_t value_) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setVA " << name << " -> " << value_ << "\n");
|
2018-02-23 05:08:53 +00:00
|
|
|
assert(!segment);
|
2021-02-26 23:22:23 +00:00
|
|
|
value = value_;
|
2018-01-12 22:10:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:03:12 +00:00
|
|
|
uint64_t DefinedData::getOutputSegmentOffset() const {
|
2018-05-15 13:36:20 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "getOutputSegmentOffset: " << getName() << "\n");
|
2021-05-12 23:48:34 +00:00
|
|
|
return segment->getChunkOffset(value);
|
2018-02-23 05:08:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:03:12 +00:00
|
|
|
uint64_t DefinedData::getOutputSegmentIndex() const {
|
2018-05-15 13:36:20 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "getOutputSegmentIndex: " << getName() << "\n");
|
2018-02-28 00:20:29 +00:00
|
|
|
return segment->outputSeg->index;
|
2018-02-23 05:08:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 19:56:23 +00:00
|
|
|
uint32_t GlobalSymbol::getGlobalIndex() const {
|
|
|
|
if (auto *f = dyn_cast<DefinedGlobal>(this))
|
2021-02-11 11:15:24 +00:00
|
|
|
return f->global->getAssignedIndex();
|
2018-03-12 19:56:23 +00:00
|
|
|
assert(globalIndex != INVALID_INDEX);
|
|
|
|
return globalIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSymbol::setGlobalIndex(uint32_t index) {
|
2018-05-15 13:36:20 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "setGlobalIndex " << name << " -> " << index << "\n");
|
2018-03-12 19:56:23 +00:00
|
|
|
assert(globalIndex == INVALID_INDEX);
|
|
|
|
globalIndex = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalSymbol::hasGlobalIndex() const {
|
|
|
|
if (auto *f = dyn_cast<DefinedGlobal>(this))
|
2021-02-11 11:15:24 +00:00
|
|
|
return f->global->hasAssignedIndex();
|
2018-03-12 19:56:23 +00:00
|
|
|
return globalIndex != INVALID_INDEX;
|
|
|
|
}
|
|
|
|
|
2018-02-23 05:08:53 +00:00
|
|
|
DefinedGlobal::DefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputGlobal *global)
|
|
|
|
: GlobalSymbol(name, DefinedGlobalKind, flags, file,
|
|
|
|
global ? &global->getType() : nullptr),
|
|
|
|
global(global) {}
|
|
|
|
|
2021-06-15 08:49:43 +00:00
|
|
|
uint32_t TagSymbol::getTagIndex() const {
|
|
|
|
if (auto *f = dyn_cast<DefinedTag>(this))
|
|
|
|
return f->tag->getAssignedIndex();
|
|
|
|
assert(tagIndex != INVALID_INDEX);
|
|
|
|
return tagIndex;
|
2018-12-08 06:17:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 08:49:43 +00:00
|
|
|
void TagSymbol::setTagIndex(uint32_t index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setTagIndex " << name << " -> " << index << "\n");
|
|
|
|
assert(tagIndex == INVALID_INDEX);
|
|
|
|
tagIndex = index;
|
2018-12-08 06:17:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 08:49:43 +00:00
|
|
|
bool TagSymbol::hasTagIndex() const {
|
|
|
|
if (auto *f = dyn_cast<DefinedTag>(this))
|
|
|
|
return f->tag->hasAssignedIndex();
|
|
|
|
return tagIndex != INVALID_INDEX;
|
2018-12-08 06:17:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 08:49:43 +00:00
|
|
|
DefinedTag::DefinedTag(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputTag *tag)
|
|
|
|
: TagSymbol(name, DefinedTagKind, flags, file,
|
|
|
|
tag ? &tag->signature : nullptr),
|
|
|
|
tag(tag) {}
|
2018-12-08 06:17:43 +00:00
|
|
|
|
2021-01-05 11:08:58 +00:00
|
|
|
void TableSymbol::setLimits(const WasmLimits &limits) {
|
|
|
|
if (auto *t = dyn_cast<DefinedTable>(this))
|
|
|
|
t->table->setLimits(limits);
|
|
|
|
auto *newType = make<WasmTableType>(*tableType);
|
|
|
|
newType->Limits = limits;
|
|
|
|
tableType = newType;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TableSymbol::getTableNumber() const {
|
|
|
|
if (const auto *t = dyn_cast<DefinedTable>(this))
|
2021-02-11 11:15:24 +00:00
|
|
|
return t->table->getAssignedIndex();
|
2021-01-05 11:08:58 +00:00
|
|
|
assert(tableNumber != INVALID_INDEX);
|
|
|
|
return tableNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableSymbol::setTableNumber(uint32_t number) {
|
2021-02-12 19:01:41 +00:00
|
|
|
if (const auto *t = dyn_cast<DefinedTable>(this))
|
|
|
|
return t->table->assignIndex(number);
|
2021-01-05 11:08:58 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "setTableNumber " << name << " -> " << number << "\n");
|
|
|
|
assert(tableNumber == INVALID_INDEX);
|
|
|
|
tableNumber = number;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TableSymbol::hasTableNumber() const {
|
|
|
|
if (const auto *t = dyn_cast<DefinedTable>(this))
|
2021-02-11 11:15:24 +00:00
|
|
|
return t->table->hasAssignedIndex();
|
2021-01-05 11:08:58 +00:00
|
|
|
return tableNumber != INVALID_INDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefinedTable::DefinedTable(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputTable *table)
|
|
|
|
: TableSymbol(name, DefinedTableKind, flags, file,
|
|
|
|
table ? &table->getType() : nullptr),
|
|
|
|
table(table) {}
|
|
|
|
|
2019-05-21 09:13:09 +00:00
|
|
|
const OutputSectionSymbol *SectionSymbol::getOutputSectionSymbol() const {
|
|
|
|
assert(section->outputSec && section->outputSec->sectionSym);
|
|
|
|
return section->outputSec->sectionSym;
|
2018-05-04 23:14:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:51:51 +00:00
|
|
|
void LazySymbol::fetch() { cast<ArchiveFile>(file)->addMember(&archiveSymbol); }
|
|
|
|
|
2020-08-07 23:12:33 +00:00
|
|
|
void LazySymbol::setWeak() {
|
|
|
|
flags |= (flags & ~WASM_SYMBOL_BINDING_MASK) | WASM_SYMBOL_BINDING_WEAK;
|
|
|
|
}
|
|
|
|
|
2019-12-20 01:23:59 +00:00
|
|
|
MemoryBufferRef LazySymbol::getMemberBuffer() {
|
|
|
|
Archive::Child c =
|
|
|
|
CHECK(archiveSymbol.getMember(),
|
|
|
|
"could not get the member for symbol " + toString(*this));
|
|
|
|
|
|
|
|
return CHECK(c.getMemoryBufferRef(),
|
|
|
|
"could not get the buffer for the member defining symbol " +
|
|
|
|
toString(*this));
|
|
|
|
}
|
|
|
|
|
2019-10-10 05:25:39 +00:00
|
|
|
void printTraceSymbolUndefined(StringRef name, const InputFile* file) {
|
2019-05-24 13:29:17 +00:00
|
|
|
message(toString(file) + ": reference to " + name);
|
|
|
|
}
|
|
|
|
|
2019-02-06 02:35:18 +00:00
|
|
|
// Print out a log message for --trace-symbol.
|
2019-10-10 05:25:39 +00:00
|
|
|
void printTraceSymbol(Symbol *sym) {
|
2019-05-24 13:29:17 +00:00
|
|
|
// Undefined symbols are traced via printTraceSymbolUndefined
|
2019-02-06 02:35:18 +00:00
|
|
|
if (sym->isUndefined())
|
2019-05-24 13:29:17 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
if (sym->isLazy())
|
2019-02-06 02:35:18 +00:00
|
|
|
s = ": lazy definition of ";
|
|
|
|
else
|
|
|
|
s = ": definition of ";
|
|
|
|
|
|
|
|
message(toString(sym->getFile()) + s + sym->getName());
|
|
|
|
}
|
2019-05-21 09:13:09 +00:00
|
|
|
|
2019-10-10 05:25:39 +00:00
|
|
|
const char *defaultModule = "env";
|
|
|
|
const char *functionTableName = "__indirect_function_table";
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|