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"
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "InputSegment.h"
|
|
|
|
#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;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
|
|
|
uint32_t Symbol::getGlobalIndex() const {
|
|
|
|
assert(!Sym->isFunction());
|
|
|
|
return Sym->ElementIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Symbol::getFunctionIndex() const {
|
|
|
|
assert(Sym->isFunction());
|
|
|
|
return Sym->ElementIndex;
|
|
|
|
}
|
|
|
|
|
2017-11-30 09:40:08 +08:00
|
|
|
const WasmSignature &Symbol::getFunctionType() const {
|
|
|
|
assert(FunctionType != nullptr);
|
|
|
|
return *FunctionType;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Symbol::getVirtualAddress() const {
|
|
|
|
assert(isGlobal());
|
|
|
|
DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
|
|
|
|
if (isUndefined())
|
|
|
|
return UINT32_MAX;
|
2017-12-06 03:05:45 +08:00
|
|
|
if (VirtualAddress.hasValue())
|
|
|
|
return VirtualAddress.getValue();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
assert(Sym != nullptr);
|
|
|
|
ObjFile *Obj = cast<ObjFile>(File);
|
|
|
|
const WasmGlobal &Global =
|
|
|
|
Obj->getWasmObj()->globals()[getGlobalIndex() - Obj->NumGlobalImports()];
|
|
|
|
assert(Global.Type == llvm::wasm::WASM_TYPE_I32);
|
|
|
|
assert(Segment);
|
|
|
|
return Segment->translateVA(Global.InitExpr.Value.Int32);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Symbol::getOutputIndex() const {
|
|
|
|
if (isUndefined() && isWeak())
|
|
|
|
return 0;
|
|
|
|
return OutputIndex.getValue();
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:05:45 +08:00
|
|
|
void Symbol::setVirtualAddress(uint32_t Value) {
|
|
|
|
DEBUG(dbgs() << "setVirtualAddress " << Name << " -> " << Value << "\n");
|
2017-12-08 08:13:14 +08:00
|
|
|
assert(!VirtualAddress.hasValue());
|
2017-12-06 03:05:45 +08:00
|
|
|
VirtualAddress = Value;
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void Symbol::setOutputIndex(uint32_t Index) {
|
|
|
|
DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n");
|
|
|
|
assert(!hasOutputIndex());
|
|
|
|
OutputIndex = Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Symbol::update(Kind K, InputFile *F, const WasmSymbol *WasmSym,
|
2017-11-30 09:40:08 +08:00
|
|
|
const InputSegment *Seg, const WasmSignature *Sig) {
|
2017-11-18 02:14:09 +08:00
|
|
|
SymbolKind = K;
|
|
|
|
File = F;
|
|
|
|
Sym = WasmSym;
|
|
|
|
Segment = Seg;
|
2017-11-30 09:40:08 +08:00
|
|
|
FunctionType = Sig;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Symbol::isWeak() const { return Sym && Sym->isWeak(); }
|
|
|
|
|
2017-12-03 10:38:04 +08:00
|
|
|
bool Symbol::isHidden() const { return Sym && Sym->isHidden(); }
|
|
|
|
|
2017-12-06 00:50:46 +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()))
|
|
|
|
return "`" + *S + "'";
|
|
|
|
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";
|
|
|
|
case wasm::Symbol::DefinedGlobalKind:
|
|
|
|
return "DefinedGlobal";
|
|
|
|
case wasm::Symbol::UndefinedFunctionKind:
|
|
|
|
return "UndefinedFunction";
|
|
|
|
case wasm::Symbol::UndefinedGlobalKind:
|
|
|
|
return "UndefinedGlobal";
|
|
|
|
case wasm::Symbol::LazyKind:
|
|
|
|
return "LazyKind";
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid symbol kind!");
|
|
|
|
}
|