2017-11-18 02:14:09 +08:00
|
|
|
//===- Symbols.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 "Symbols.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2018-12-08 14:17:43 +08:00
|
|
|
#include "InputEvent.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "InputFiles.h"
|
2018-02-23 13:08:53 +08:00
|
|
|
#include "InputGlobal.h"
|
2019-05-21 17:13:09 +08:00
|
|
|
#include "OutputSections.h"
|
2018-02-23 13:08:53 +08:00
|
|
|
#include "OutputSegment.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-12-06 11:10:39 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2018-01-10 08:52:20 +08:00
|
|
|
using namespace llvm::wasm;
|
2017-11-18 02:14:09 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedFunction *WasmSym::callCtors;
|
|
|
|
DefinedFunction *WasmSym::initMemory;
|
|
|
|
DefinedFunction *WasmSym::applyRelocs;
|
[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-17 06:00:45 +08:00
|
|
|
DefinedFunction *WasmSym::initTLS;
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedData *WasmSym::dsoHandle;
|
|
|
|
DefinedData *WasmSym::dataEnd;
|
|
|
|
DefinedData *WasmSym::globalBase;
|
|
|
|
DefinedData *WasmSym::heapBase;
|
|
|
|
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-17 06:00:45 +08: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-20 07:34:16 +08:00
|
|
|
GlobalSymbol *WasmSym::tlsAlign;
|
2019-07-11 13:40:30 +08:00
|
|
|
UndefinedGlobal *WasmSym::tableBase;
|
2019-08-14 01:02:02 +08:00
|
|
|
DefinedData *WasmSym::definedTableBase;
|
2019-07-11 13:40:30 +08:00
|
|
|
UndefinedGlobal *WasmSym::memoryBase;
|
2019-08-14 01:02:02 +08:00
|
|
|
DefinedData *WasmSym::definedMemoryBase;
|
2018-02-23 13:08:53 +08:00
|
|
|
|
|
|
|
WasmSymbolType Symbol::getWasmType() const {
|
2018-02-28 09:10:50 +08:00
|
|
|
if (isa<FunctionSymbol>(this))
|
2018-05-15 06:42:33 +08:00
|
|
|
return WASM_SYMBOL_TYPE_FUNCTION;
|
2018-02-28 09:10:50 +08:00
|
|
|
if (isa<DataSymbol>(this))
|
2018-05-15 06:42:33 +08:00
|
|
|
return WASM_SYMBOL_TYPE_DATA;
|
2018-02-28 09:10:50 +08:00
|
|
|
if (isa<GlobalSymbol>(this))
|
2018-05-15 06:42:33 +08:00
|
|
|
return WASM_SYMBOL_TYPE_GLOBAL;
|
2018-12-08 14:17:43 +08:00
|
|
|
if (isa<EventSymbol>(this))
|
|
|
|
return WASM_SYMBOL_TYPE_EVENT;
|
2019-05-21 17:13:09 +08:00
|
|
|
if (isa<SectionSymbol>(this) || isa<OutputSectionSymbol>(this))
|
2018-05-15 06:42:33 +08:00
|
|
|
return WASM_SYMBOL_TYPE_SECTION;
|
2018-02-28 09:10:50 +08:00
|
|
|
llvm_unreachable("invalid symbol kind");
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-02-21 07:19:31 +08:00
|
|
|
const WasmSignature *Symbol::getSignature() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto* f = dyn_cast<FunctionSymbol>(this))
|
|
|
|
return f->signature;
|
|
|
|
if (auto *l = dyn_cast<LazySymbol>(this))
|
|
|
|
return l->signature;
|
2019-02-21 07:19:31 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-21 01:45:38 +08:00
|
|
|
InputChunk *Symbol::getChunk() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function;
|
|
|
|
if (auto *d = dyn_cast<DefinedData>(this))
|
|
|
|
return d->segment;
|
2018-02-21 01:45:38 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:00:46 +08:00
|
|
|
bool Symbol::isDiscarded() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (InputChunk *c = getChunk())
|
|
|
|
return c->discarded;
|
2019-06-07 14:00:46 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
bool Symbol::isLive() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *g = dyn_cast<DefinedGlobal>(this))
|
|
|
|
return g->global->live;
|
|
|
|
if (auto *e = dyn_cast<DefinedEvent>(this))
|
|
|
|
return e->event->live;
|
|
|
|
if (InputChunk *c = getChunk())
|
|
|
|
return c->live;
|
|
|
|
return referenced;
|
2018-04-21 01:18:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Symbol::markLive() {
|
2019-06-07 14:00:46 +08:00
|
|
|
assert(!isDiscarded());
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *g = dyn_cast<DefinedGlobal>(this))
|
|
|
|
g->global->live = true;
|
|
|
|
if (auto *e = dyn_cast<DefinedEvent>(this))
|
|
|
|
e->event->live = true;
|
|
|
|
if (InputChunk *c = getChunk())
|
|
|
|
c->live = true;
|
|
|
|
referenced = true;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Symbol::getOutputSymbolIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(outputSymbolIndex != INVALID_INDEX);
|
|
|
|
return outputSymbolIndex;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void Symbol::setOutputSymbolIndex(uint32_t index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setOutputSymbolIndex " << name << " -> " << index
|
2018-05-15 21:36:20 +08:00
|
|
|
<< "\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(outputSymbolIndex == INVALID_INDEX);
|
|
|
|
outputSymbolIndex = index;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void Symbol::setGOTIndex(uint32_t index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setGOTIndex " << name << " -> " << index << "\n");
|
|
|
|
assert(gotIndex == INVALID_INDEX);
|
2019-08-14 01:02:02 +08:00
|
|
|
if (config->isPic) {
|
|
|
|
// Any symbol that is assigned a GOT entry must be exported othewise the
|
|
|
|
// dynamic linker won't be able create the entry that contains it.
|
|
|
|
forceExport = true;
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
gotIndex = index;
|
2019-03-27 03:46:15 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
bool Symbol::isWeak() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbol::isLocal() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
return (flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_LOCAL;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbol::isHidden() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
return (flags & WASM_SYMBOL_VISIBILITY_MASK) == WASM_SYMBOL_VISIBILITY_HIDDEN;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void Symbol::setHidden(bool isHidden) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setHidden: " << name << " -> " << isHidden << "\n");
|
|
|
|
flags &= ~WASM_SYMBOL_VISIBILITY_MASK;
|
|
|
|
if (isHidden)
|
|
|
|
flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
|
2018-02-15 02:27:59 +08:00
|
|
|
else
|
2019-07-11 13:40:30 +08:00
|
|
|
flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
2018-06-29 01:04:58 +08:00
|
|
|
bool Symbol::isExported() const {
|
|
|
|
if (!isDefined() || isLocal())
|
|
|
|
return false;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (forceExport || config->exportAll)
|
2018-06-29 01:04:58 +08:00
|
|
|
return true;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
if (config->exportDynamic && !isHidden())
|
2018-09-28 05:06:25 +08:00
|
|
|
return true;
|
2018-09-26 05:50:15 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
return flags & WASM_SYMBOL_EXPORTED;
|
2018-06-29 01:04:58 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
uint32_t FunctionSymbol::getFunctionIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function->getFunctionIndex();
|
|
|
|
assert(functionIndex != INVALID_INDEX);
|
|
|
|
return functionIndex;
|
2018-03-13 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void FunctionSymbol::setFunctionIndex(uint32_t index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setFunctionIndex " << name << " -> " << index << "\n");
|
|
|
|
assert(functionIndex == INVALID_INDEX);
|
|
|
|
functionIndex = index;
|
2018-03-13 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FunctionSymbol::hasFunctionIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function->hasFunctionIndex();
|
|
|
|
return functionIndex != INVALID_INDEX;
|
2018-03-13 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
uint32_t FunctionSymbol::getTableIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function->getTableIndex();
|
|
|
|
assert(tableIndex != INVALID_INDEX);
|
|
|
|
return tableIndex;
|
2018-01-25 05:45:25 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
bool FunctionSymbol::hasTableIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this))
|
|
|
|
return f->function->hasTableIndex();
|
|
|
|
return tableIndex != INVALID_INDEX;
|
2018-01-25 05:45:25 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void FunctionSymbol::setTableIndex(uint32_t index) {
|
2018-01-25 05:45:25 +08: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).
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(this)) {
|
|
|
|
f->function->setTableIndex(index);
|
2018-01-25 05:45:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setTableIndex " << name << " -> " << index << "\n");
|
|
|
|
assert(tableIndex == INVALID_INDEX);
|
|
|
|
tableIndex = index;
|
2017-12-12 06:00:56 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedFunction::DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
|
|
|
|
InputFunction *function)
|
|
|
|
: FunctionSymbol(name, DefinedFunctionKind, flags, f,
|
|
|
|
function ? &function->signature : nullptr),
|
|
|
|
function(function) {}
|
2018-02-21 01:45:38 +08:00
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
uint32_t DefinedData::getVirtualAddress() const {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
|
[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-17 06:00:45 +08:00
|
|
|
if (segment) {
|
|
|
|
// For thread local data, the symbol location is relative to the start of
|
|
|
|
// the .tdata section, since they are used as offsets from __tls_base.
|
|
|
|
// Hence, we do not add in segment->outputSeg->startVA.
|
|
|
|
if (segment->outputSeg->name == ".tdata")
|
|
|
|
return segment->outputSegmentOffset + offset;
|
2019-07-11 13:40:30 +08:00
|
|
|
return segment->outputSeg->startVA + segment->outputSegmentOffset + offset;
|
[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-17 06:00:45 +08:00
|
|
|
}
|
2019-07-11 13:40:30 +08:00
|
|
|
return offset;
|
2018-01-10 08:52:20 +08:00
|
|
|
}
|
2017-12-03 10:38:04 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void DefinedData::setVirtualAddress(uint32_t value) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setVirtualAddress " << name << " -> " << value << "\n");
|
|
|
|
assert(!segment);
|
|
|
|
offset = value;
|
2018-01-13 06:10:35 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
uint32_t DefinedData::getOutputSegmentOffset() const {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getOutputSegmentOffset: " << getName() << "\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
return segment->outputSegmentOffset + offset;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DefinedData::getOutputSegmentIndex() const {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getOutputSegmentIndex: " << getName() << "\n");
|
2019-07-11 13:40:30 +08:00
|
|
|
return segment->outputSeg->index;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
uint32_t GlobalSymbol::getGlobalIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedGlobal>(this))
|
|
|
|
return f->global->getGlobalIndex();
|
|
|
|
assert(globalIndex != INVALID_INDEX);
|
|
|
|
return globalIndex;
|
2018-03-13 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void GlobalSymbol::setGlobalIndex(uint32_t index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setGlobalIndex " << name << " -> " << index << "\n");
|
|
|
|
assert(globalIndex == INVALID_INDEX);
|
|
|
|
globalIndex = index;
|
2018-03-13 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalSymbol::hasGlobalIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedGlobal>(this))
|
|
|
|
return f->global->hasGlobalIndex();
|
|
|
|
return globalIndex != INVALID_INDEX;
|
2018-03-13 03:56:23 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedGlobal::DefinedGlobal(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputGlobal *global)
|
|
|
|
: GlobalSymbol(name, DefinedGlobalKind, flags, file,
|
|
|
|
global ? &global->getType() : nullptr),
|
|
|
|
global(global) {}
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2018-12-08 14:17:43 +08:00
|
|
|
uint32_t EventSymbol::getEventIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedEvent>(this))
|
|
|
|
return f->event->getEventIndex();
|
|
|
|
assert(eventIndex != INVALID_INDEX);
|
|
|
|
return eventIndex;
|
2018-12-08 14:17:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void EventSymbol::setEventIndex(uint32_t index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setEventIndex " << name << " -> " << index << "\n");
|
|
|
|
assert(eventIndex == INVALID_INDEX);
|
|
|
|
eventIndex = index;
|
2018-12-08 14:17:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EventSymbol::hasEventIndex() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
if (auto *f = dyn_cast<DefinedEvent>(this))
|
|
|
|
return f->event->hasEventIndex();
|
|
|
|
return eventIndex != INVALID_INDEX;
|
2018-12-08 14:17:43 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
DefinedEvent::DefinedEvent(StringRef name, uint32_t flags, InputFile *file,
|
|
|
|
InputEvent *event)
|
|
|
|
: EventSymbol(name, DefinedEventKind, flags, file,
|
|
|
|
event ? &event->getType() : nullptr,
|
|
|
|
event ? &event->signature : nullptr),
|
|
|
|
event(event) {}
|
2018-12-08 14:17:43 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
const OutputSectionSymbol *SectionSymbol::getOutputSectionSymbol() const {
|
2019-07-11 13:40:30 +08:00
|
|
|
assert(section->outputSec && section->outputSec->sectionSym);
|
|
|
|
return section->outputSec->sectionSym;
|
2018-05-05 07:14:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void LazySymbol::fetch() { cast<ArchiveFile>(file)->addMember(&archiveSymbol); }
|
2018-03-01 06:51:51 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::string lld::toString(const wasm::Symbol &sym) {
|
|
|
|
return lld::maybeDemangleSymbol(sym.getName());
|
2018-11-10 00:57:41 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::string lld::maybeDemangleSymbol(StringRef name) {
|
|
|
|
if (config->demangle)
|
|
|
|
if (Optional<std::string> s = demangleItanium(name))
|
|
|
|
return *s;
|
|
|
|
return name;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::string lld::toString(wasm::Symbol::Kind kind) {
|
|
|
|
switch (kind) {
|
2017-11-18 02:14:09 +08:00
|
|
|
case wasm::Symbol::DefinedFunctionKind:
|
|
|
|
return "DefinedFunction";
|
2018-02-21 07:38:27 +08:00
|
|
|
case wasm::Symbol::DefinedDataKind:
|
|
|
|
return "DefinedData";
|
2018-02-23 13:08:53 +08:00
|
|
|
case wasm::Symbol::DefinedGlobalKind:
|
|
|
|
return "DefinedGlobal";
|
2018-12-08 14:17:43 +08:00
|
|
|
case wasm::Symbol::DefinedEventKind:
|
|
|
|
return "DefinedEvent";
|
2017-11-18 02:14:09 +08:00
|
|
|
case wasm::Symbol::UndefinedFunctionKind:
|
|
|
|
return "UndefinedFunction";
|
2018-02-21 07:38:27 +08:00
|
|
|
case wasm::Symbol::UndefinedDataKind:
|
|
|
|
return "UndefinedData";
|
2018-02-23 13:08:53 +08:00
|
|
|
case wasm::Symbol::UndefinedGlobalKind:
|
|
|
|
return "UndefinedGlobal";
|
2017-11-18 02:14:09 +08:00
|
|
|
case wasm::Symbol::LazyKind:
|
|
|
|
return "LazyKind";
|
2018-05-05 07:14:42 +08:00
|
|
|
case wasm::Symbol::SectionKind:
|
|
|
|
return "SectionKind";
|
2019-05-21 17:13:09 +08:00
|
|
|
case wasm::Symbol::OutputSectionKind:
|
|
|
|
return "OutputSectionKind";
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2018-02-23 13:08:53 +08:00
|
|
|
llvm_unreachable("invalid symbol kind");
|
|
|
|
}
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-05-24 21:29:17 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
void lld::wasm::printTraceSymbolUndefined(StringRef name, const InputFile* file) {
|
|
|
|
message(toString(file) + ": reference to " + name);
|
2019-05-24 21:29:17 +08:00
|
|
|
}
|
|
|
|
|
2019-02-06 10:35:18 +08:00
|
|
|
// Print out a log message for --trace-symbol.
|
2019-07-11 13:40:30 +08:00
|
|
|
void lld::wasm::printTraceSymbol(Symbol *sym) {
|
2019-05-24 21:29:17 +08:00
|
|
|
// Undefined symbols are traced via printTraceSymbolUndefined
|
2019-07-11 13:40:30 +08:00
|
|
|
if (sym->isUndefined())
|
2019-05-24 21:29:17 +08:00
|
|
|
return;
|
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
std::string s;
|
|
|
|
if (sym->isLazy())
|
|
|
|
s = ": lazy definition of ";
|
2019-02-06 10:35:18 +08:00
|
|
|
else
|
2019-07-11 13:40:30 +08:00
|
|
|
s = ": definition of ";
|
2019-02-06 10:35:18 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
message(toString(sym->getFile()) + s + sym->getName());
|
2019-02-06 10:35:18 +08:00
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
|
2019-07-11 13:40:30 +08:00
|
|
|
const char *lld::wasm::defaultModule = "env";
|
|
|
|
const char *lld::wasm::functionTableName = "__indirect_function_table";
|