2017-11-18 02:14:09 +08:00
|
|
|
//===- Symbols.cpp --------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Symbols.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.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-02-23 13:08:53 +08:00
|
|
|
DefinedGlobal *WasmSym::StackPointer;
|
|
|
|
|
|
|
|
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-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;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
|
|
|
|
return !isHidden();
|
|
|
|
}
|
|
|
|
|
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-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) {
|
2017-12-06 11:10:39 +08:00
|
|
|
if (Config->Demangle)
|
|
|
|
if (Optional<std::string> S = demangleItanium(Sym.getName()))
|
2018-03-10 06:59:34 +08:00
|
|
|
return *S;
|
2017-12-06 11:10:39 +08:00
|
|
|
return Sym.getName();
|
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";
|
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");
|
|
|
|
}
|