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"
|
|
|
|
#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;
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
DefinedFunction *WasmSym::CallCtors;
|
2018-02-21 07:38:27 +08:00
|
|
|
DefinedData *WasmSym::DsoHandle;
|
|
|
|
DefinedData *WasmSym::DataEnd;
|
|
|
|
DefinedData *WasmSym::HeapBase;
|
2018-11-16 02:15:54 +08:00
|
|
|
GlobalSymbol *WasmSym::StackPointer;
|
|
|
|
UndefinedGlobal *WasmSym::TableBase;
|
|
|
|
UndefinedGlobal *WasmSym::MemoryBase;
|
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;
|
2018-05-05 07:14:42 +08:00
|
|
|
if (isa<SectionSymbol>(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
|
|
|
|
2018-02-21 01:45:38 +08:00
|
|
|
InputChunk *Symbol::getChunk() const {
|
|
|
|
if (auto *F = dyn_cast<DefinedFunction>(this))
|
|
|
|
return F->Function;
|
2018-02-23 13:08:53 +08:00
|
|
|
if (auto *D = dyn_cast<DefinedData>(this))
|
|
|
|
return D->Segment;
|
2018-02-21 01:45:38 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
bool Symbol::isLive() const {
|
|
|
|
if (auto *G = dyn_cast<DefinedGlobal>(this))
|
|
|
|
return G->Global->Live;
|
2018-12-08 14:17:43 +08:00
|
|
|
if (auto *E = dyn_cast<DefinedEvent>(this))
|
|
|
|
return E->Event->Live;
|
2018-02-23 13:08:53 +08:00
|
|
|
if (InputChunk *C = getChunk())
|
|
|
|
return C->Live;
|
2018-04-21 01:18:06 +08:00
|
|
|
return Referenced;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Symbol::markLive() {
|
|
|
|
if (auto *G = dyn_cast<DefinedGlobal>(this))
|
|
|
|
G->Global->Live = true;
|
2018-12-08 14:17:43 +08:00
|
|
|
if (auto *E = dyn_cast<DefinedEvent>(this))
|
|
|
|
E->Event->Live = true;
|
2018-04-21 01:18:06 +08:00
|
|
|
if (InputChunk *C = getChunk())
|
|
|
|
C->Live = true;
|
|
|
|
Referenced = true;
|
2018-02-23 13:08:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Symbol::getOutputSymbolIndex() const {
|
|
|
|
assert(OutputSymbolIndex != INVALID_INDEX);
|
|
|
|
return OutputSymbolIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Symbol::setOutputSymbolIndex(uint32_t Index) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setOutputSymbolIndex " << Name << " -> " << Index
|
|
|
|
<< "\n");
|
2018-02-23 13:08:53 +08:00
|
|
|
assert(OutputSymbolIndex == INVALID_INDEX);
|
|
|
|
OutputSymbolIndex = Index;
|
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Symbol::setHidden(bool IsHidden) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setHidden: " << Name << " -> " << IsHidden << "\n");
|
2018-02-15 02:27:59 +08:00
|
|
|
Flags &= ~WASM_SYMBOL_VISIBILITY_MASK;
|
|
|
|
if (IsHidden)
|
|
|
|
Flags |= WASM_SYMBOL_VISIBILITY_HIDDEN;
|
|
|
|
else
|
|
|
|
Flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2018-06-29 01:04:58 +08:00
|
|
|
bool Symbol::isExported() const {
|
|
|
|
if (!isDefined() || isLocal())
|
|
|
|
return false;
|
|
|
|
|
2018-06-29 01:21:46 +08:00
|
|
|
if (ForceExport || Config->ExportAll)
|
2018-06-29 01:04:58 +08:00
|
|
|
return true;
|
|
|
|
|
2018-09-28 05:06:25 +08:00
|
|
|
if (Config->ExportDynamic && !isHidden())
|
|
|
|
return true;
|
2018-09-26 05:50:15 +08:00
|
|
|
|
2019-02-07 09:53:50 +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 {
|
|
|
|
if (auto *F = dyn_cast<DefinedFunction>(this))
|
|
|
|
return F->Function->getFunctionIndex();
|
|
|
|
assert(FunctionIndex != INVALID_INDEX);
|
|
|
|
return FunctionIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionSymbol::setFunctionIndex(uint32_t Index) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setFunctionIndex " << Name << " -> " << Index << "\n");
|
2018-03-13 03:56:23 +08: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-15 02:27:59 +08:00
|
|
|
uint32_t FunctionSymbol::getTableIndex() const {
|
2018-02-21 01:45:38 +08:00
|
|
|
if (auto *F = dyn_cast<DefinedFunction>(this))
|
|
|
|
return F->Function->getTableIndex();
|
2018-02-15 06:55:38 +08:00
|
|
|
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 {
|
2018-02-21 01:45:38 +08:00
|
|
|
if (auto *F = dyn_cast<DefinedFunction>(this))
|
|
|
|
return F->Function->hasTableIndex();
|
2018-02-15 06:55:38 +08:00
|
|
|
return TableIndex != INVALID_INDEX;
|
2018-01-25 05:45:25 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +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).
|
2018-02-21 01:45:38 +08:00
|
|
|
if (auto *F = dyn_cast<DefinedFunction>(this)) {
|
|
|
|
F->Function->setTableIndex(Index);
|
2018-01-25 05:45:25 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setTableIndex " << Name << " -> " << Index << "\n");
|
2018-02-15 06:55:38 +08:00
|
|
|
assert(TableIndex == INVALID_INDEX);
|
2017-12-12 06:00:56 +08:00
|
|
|
TableIndex = Index;
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-21 01:45:38 +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 07:38:27 +08:00
|
|
|
uint32_t DefinedData::getVirtualAddress() const {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
|
2018-02-28 09:01:34 +08:00
|
|
|
if (Segment)
|
|
|
|
return Segment->OutputSeg->StartVA + Segment->OutputSegmentOffset + Offset;
|
|
|
|
return Offset;
|
2018-01-10 08:52:20 +08:00
|
|
|
}
|
2017-12-03 10:38:04 +08:00
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
void DefinedData::setVirtualAddress(uint32_t Value) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setVirtualAddress " << Name << " -> " << Value << "\n");
|
2018-02-23 13:08:53 +08:00
|
|
|
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");
|
2018-02-23 13:08:53 +08:00
|
|
|
return Segment->OutputSegmentOffset + Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t DefinedData::getOutputSegmentIndex() const {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getOutputSegmentIndex: " << getName() << "\n");
|
2018-02-28 08:20:29 +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 {
|
|
|
|
if (auto *F = dyn_cast<DefinedGlobal>(this))
|
|
|
|
return F->Global->getGlobalIndex();
|
|
|
|
assert(GlobalIndex != INVALID_INDEX);
|
|
|
|
return GlobalIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSymbol::setGlobalIndex(uint32_t Index) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setGlobalIndex " << Name << " -> " << Index << "\n");
|
2018-03-13 03:56:23 +08:00
|
|
|
assert(GlobalIndex == INVALID_INDEX);
|
|
|
|
GlobalIndex = Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalSymbol::hasGlobalIndex() const {
|
|
|
|
if (auto *F = dyn_cast<DefinedGlobal>(this))
|
|
|
|
return F->Global->hasGlobalIndex();
|
|
|
|
return GlobalIndex != INVALID_INDEX;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +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-12-08 14:17:43 +08:00
|
|
|
uint32_t EventSymbol::getEventIndex() const {
|
|
|
|
if (auto *F = dyn_cast<DefinedEvent>(this))
|
|
|
|
return F->Event->getEventIndex();
|
|
|
|
assert(EventIndex != INVALID_INDEX);
|
|
|
|
return EventIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventSymbol::setEventIndex(uint32_t Index) {
|
|
|
|
LLVM_DEBUG(dbgs() << "setEventIndex " << Name << " -> " << Index << "\n");
|
|
|
|
assert(EventIndex == INVALID_INDEX);
|
|
|
|
EventIndex = Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EventSymbol::hasEventIndex() const {
|
|
|
|
if (auto *F = dyn_cast<DefinedEvent>(this))
|
|
|
|
return F->Event->hasEventIndex();
|
|
|
|
return EventIndex != INVALID_INDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-05 07:14:42 +08:00
|
|
|
uint32_t SectionSymbol::getOutputSectionIndex() const {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "getOutputSectionIndex: " << getName() << "\n");
|
2018-05-05 07:14:42 +08:00
|
|
|
assert(OutputSectionIndex != INVALID_INDEX);
|
|
|
|
return OutputSectionIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SectionSymbol::setOutputSectionIndex(uint32_t Index) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "setOutputSectionIndex: " << getName() << " -> " << Index
|
|
|
|
<< "\n");
|
2018-05-05 07:14:42 +08:00
|
|
|
assert(Index != INVALID_INDEX);
|
|
|
|
OutputSectionIndex = Index;
|
|
|
|
}
|
|
|
|
|
2018-03-01 06:51:51 +08:00
|
|
|
void LazySymbol::fetch() { cast<ArchiveFile>(File)->addMember(&ArchiveSymbol); }
|
|
|
|
|
2018-03-10 06:59:34 +08:00
|
|
|
std::string lld::toString(const wasm::Symbol &Sym) {
|
2018-11-10 00:57:41 +08:00
|
|
|
return lld::maybeDemangleSymbol(Sym.getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string lld::maybeDemangleSymbol(StringRef Name) {
|
2017-12-06 11:10:39 +08:00
|
|
|
if (Config->Demangle)
|
2018-11-10 00:57:41 +08:00
|
|
|
if (Optional<std::string> S = demangleItanium(Name))
|
2018-03-10 06:59:34 +08:00
|
|
|
return *S;
|
2018-11-10 00:57:41 +08:00
|
|
|
return Name;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string lld::toString(wasm::Symbol::Kind Kind) {
|
2017-11-18 02:14:09 +08:00
|
|
|
switch (Kind) {
|
|
|
|
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";
|
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
|
|
|
|
|
|
|
// Print out a log message for --trace-symbol.
|
|
|
|
void lld::wasm::printTraceSymbol(Symbol *Sym) {
|
|
|
|
std::string S;
|
|
|
|
if (Sym->isUndefined())
|
|
|
|
S = ": reference to ";
|
|
|
|
else if (Sym->isLazy())
|
|
|
|
S = ": lazy definition of ";
|
|
|
|
else
|
|
|
|
S = ": definition of ";
|
|
|
|
|
|
|
|
message(toString(Sym->getFile()) + S + Sym->getName());
|
|
|
|
}
|