2017-11-18 02:14:09 +08:00
|
|
|
//===- InputFiles.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2018-02-23 13:08:53 +08:00
|
|
|
#include "InputGlobal.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
|
|
|
Optional<MemoryBufferRef> lld::wasm::readFile(StringRef Path) {
|
|
|
|
log("Loading: " + Path);
|
|
|
|
|
|
|
|
auto MBOrErr = MemoryBuffer::getFile(Path);
|
|
|
|
if (auto EC = MBOrErr.getError()) {
|
|
|
|
error("cannot open " + Path + ": " + EC.message());
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
|
|
|
|
MemoryBufferRef MBRef = MB->getMemBufferRef();
|
|
|
|
make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership
|
|
|
|
|
|
|
|
return MBRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::dumpInfo() const {
|
2018-01-10 07:56:44 +08:00
|
|
|
log("info for: " + getName() + "\n" +
|
2018-02-23 13:08:53 +08:00
|
|
|
" Symbols : " + Twine(Symbols.size()) + "\n" +
|
2018-01-10 07:56:44 +08:00
|
|
|
" Function Imports : " + Twine(NumFunctionImports) + "\n" +
|
2018-01-23 09:25:56 +08:00
|
|
|
" Global Imports : " + Twine(NumGlobalImports) + "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
uint32_t ObjFile::relocateVirtualAddress(uint32_t GlobalIndex) const {
|
2018-02-21 07:38:27 +08:00
|
|
|
if (auto *DG = dyn_cast<DefinedData>(getDataSymbol(GlobalIndex)))
|
2018-02-15 02:27:59 +08:00
|
|
|
return DG->getVirtualAddress();
|
|
|
|
else
|
|
|
|
return 0;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ObjFile::relocateFunctionIndex(uint32_t Original) const {
|
2018-02-15 02:27:59 +08:00
|
|
|
const FunctionSymbol *Sym = getFunctionSymbol(Original);
|
2017-12-07 09:51:24 +08:00
|
|
|
uint32_t Index = Sym->getOutputIndex();
|
|
|
|
DEBUG(dbgs() << "relocateFunctionIndex: " << toString(*Sym) << ": "
|
|
|
|
<< Original << " -> " << Index << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ObjFile::relocateTypeIndex(uint32_t Original) const {
|
2018-02-01 07:48:14 +08:00
|
|
|
assert(TypeIsUsed[Original]);
|
2017-11-18 02:14:09 +08:00
|
|
|
return TypeMap[Original];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ObjFile::relocateTableIndex(uint32_t Original) const {
|
2018-02-15 02:27:59 +08:00
|
|
|
const FunctionSymbol *Sym = getFunctionSymbol(Original);
|
2018-02-23 13:08:53 +08:00
|
|
|
// The null case is possible, if you take the address of a weak function
|
|
|
|
// that's simply not supplied.
|
2018-01-09 07:39:11 +08:00
|
|
|
uint32_t Index = Sym->hasTableIndex() ? Sym->getTableIndex() : 0;
|
2017-12-12 06:00:56 +08:00
|
|
|
DEBUG(dbgs() << "relocateTableIndex: " << toString(*Sym) << ": " << Original
|
|
|
|
<< " -> " << Index << "\n");
|
|
|
|
return Index;
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ObjFile::relocateGlobalIndex(uint32_t Original) const {
|
2018-02-23 13:08:53 +08:00
|
|
|
const Symbol *Sym = getGlobalSymbol(Original);
|
2018-02-09 15:09:12 +08:00
|
|
|
uint32_t Index = Sym->getOutputIndex();
|
2017-12-07 09:51:24 +08:00
|
|
|
DEBUG(dbgs() << "relocateGlobalIndex: " << toString(*Sym) << ": " << Original
|
|
|
|
<< " -> " << Index << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
uint32_t ObjFile::relocateSymbolIndex(uint32_t Original) const {
|
|
|
|
Symbol *Sym = getSymbol(Original);
|
|
|
|
uint32_t Index = Sym->getOutputSymbolIndex();
|
|
|
|
DEBUG(dbgs() << "relocateSymbolIndex: " << toString(*Sym) << ": " << Original
|
|
|
|
<< " -> " << Index << "\n");
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2018-01-11 03:22:42 +08:00
|
|
|
// Relocations contain an index into the function, global or table index
|
|
|
|
// space of the input file. This function takes a relocation and returns the
|
|
|
|
// relocated index (i.e. translates from the input index space to the output
|
|
|
|
// index space).
|
|
|
|
uint32_t ObjFile::calcNewIndex(const WasmRelocation &Reloc) const {
|
2018-02-23 13:08:53 +08:00
|
|
|
if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB)
|
2018-01-11 03:22:42 +08:00
|
|
|
return relocateTypeIndex(Reloc.Index);
|
2018-02-23 13:08:53 +08:00
|
|
|
|
|
|
|
return relocateSymbolIndex(Reloc.Index);
|
2018-01-11 03:22:42 +08:00
|
|
|
}
|
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
// Translate from the relocation's index into the final linked output value.
|
|
|
|
uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const {
|
|
|
|
switch (Reloc.Type) {
|
|
|
|
case R_WEBASSEMBLY_TABLE_INDEX_I32:
|
|
|
|
case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
|
|
|
return relocateTableIndex(Reloc.Index);
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
|
|
|
|
return relocateVirtualAddress(Reloc.Index) + Reloc.Addend;
|
|
|
|
case R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
|
|
|
return relocateTypeIndex(Reloc.Index);
|
|
|
|
case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
|
|
|
return relocateFunctionIndex(Reloc.Index);
|
|
|
|
case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
|
|
|
|
return relocateGlobalIndex(Reloc.Index);
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void ObjFile::parse() {
|
|
|
|
// Parse a memory buffer as a wasm file.
|
|
|
|
DEBUG(dbgs() << "Parsing object: " << toString(this) << "\n");
|
2017-12-07 06:08:17 +08:00
|
|
|
std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), toString(this));
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
auto *Obj = dyn_cast<WasmObjectFile>(Bin.get());
|
|
|
|
if (!Obj)
|
|
|
|
fatal(toString(this) + ": not a wasm file");
|
|
|
|
if (!Obj->isRelocatableObject())
|
|
|
|
fatal(toString(this) + ": not a relocatable wasm file");
|
|
|
|
|
|
|
|
Bin.release();
|
|
|
|
WasmObj.reset(Obj);
|
|
|
|
|
|
|
|
// Find the code and data sections. Wasm objects can have at most one code
|
|
|
|
// and one data section.
|
|
|
|
for (const SectionRef &Sec : WasmObj->sections()) {
|
|
|
|
const WasmSection &Section = WasmObj->getWasmSection(Sec);
|
|
|
|
if (Section.Type == WASM_SEC_CODE)
|
|
|
|
CodeSection = &Section;
|
|
|
|
else if (Section.Type == WASM_SEC_DATA)
|
|
|
|
DataSection = &Section;
|
|
|
|
}
|
|
|
|
|
2018-02-01 07:48:14 +08:00
|
|
|
TypeMap.resize(getWasmObj()->types().size());
|
|
|
|
TypeIsUsed.resize(getWasmObj()->types().size(), false);
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
initializeSymbols();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the InputSegment in which a given symbol is defined.
|
2018-01-10 07:56:44 +08:00
|
|
|
InputSegment *ObjFile::getSegment(const WasmSymbol &WasmSym) const {
|
2018-02-23 13:08:53 +08:00
|
|
|
return Segments[WasmSym.Info.DataRef.Segment];
|
2018-01-10 07:56:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
InputFunction *ObjFile::getFunction(const WasmSymbol &Sym) const {
|
2018-02-23 13:08:53 +08:00
|
|
|
assert(Sym.Info.ElementIndex >= NumFunctionImports);
|
|
|
|
uint32_t FunctionIndex = Sym.Info.ElementIndex - NumFunctionImports;
|
2018-01-10 07:56:44 +08:00
|
|
|
return Functions[FunctionIndex];
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
InputGlobal *ObjFile::getGlobal(const WasmSymbol &Sym) const {
|
|
|
|
assert(Sym.Info.ElementIndex >= NumGlobalImports);
|
|
|
|
uint32_t GlobalIndex = Sym.Info.ElementIndex - NumGlobalImports;
|
|
|
|
return Globals[GlobalIndex];
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:25:17 +08:00
|
|
|
bool ObjFile::isExcludedByComdat(InputChunk *Chunk) const {
|
|
|
|
StringRef Comdat = Chunk->getComdat();
|
|
|
|
return !Comdat.empty() && Symtab->findComdat(Comdat) != this;
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
FunctionSymbol *ObjFile::getFunctionSymbol(uint32_t Index) const {
|
|
|
|
return cast<FunctionSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalSymbol *ObjFile::getGlobalSymbol(uint32_t Index) const {
|
|
|
|
return cast<GlobalSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSymbol *ObjFile::getDataSymbol(uint32_t Index) const {
|
|
|
|
return cast<DataSymbol>(Symbols[Index]);
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void ObjFile::initializeSymbols() {
|
|
|
|
Symbols.reserve(WasmObj->getNumberOfSymbols());
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
NumFunctionImports = WasmObj->getNumImportedFunctions();
|
|
|
|
NumGlobalImports = WasmObj->getNumImportedGlobals();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-13 06:25:17 +08:00
|
|
|
ArrayRef<WasmFunction> Funcs = WasmObj->functions();
|
|
|
|
ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes();
|
|
|
|
ArrayRef<WasmSignature> Types = WasmObj->types();
|
|
|
|
|
2018-02-24 09:39:25 +08:00
|
|
|
for (StringRef Name : WasmObj->comdats())
|
|
|
|
Symtab->addComdat(Name, this);
|
2018-01-13 06:25:17 +08:00
|
|
|
|
2017-12-18 01:52:01 +08:00
|
|
|
for (const WasmSegment &S : WasmObj->dataSegments()) {
|
2018-01-13 02:35:13 +08:00
|
|
|
InputSegment *Seg = make<InputSegment>(S, this);
|
2018-01-10 09:13:34 +08:00
|
|
|
Seg->copyRelocations(*DataSection);
|
2017-12-18 01:52:01 +08:00
|
|
|
Segments.emplace_back(Seg);
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
for (const WasmGlobal &G : WasmObj->globals())
|
|
|
|
Globals.emplace_back(make<InputGlobal>(G));
|
|
|
|
|
2018-01-10 07:56:44 +08:00
|
|
|
for (size_t I = 0; I < Funcs.size(); ++I) {
|
|
|
|
const WasmFunction &Func = Funcs[I];
|
|
|
|
const WasmSignature &Sig = Types[FuncTypes[I]];
|
2018-01-13 02:35:13 +08:00
|
|
|
InputFunction *F = make<InputFunction>(Sig, &Func, this);
|
2018-01-10 09:13:34 +08:00
|
|
|
F->copyRelocations(*CodeSection);
|
|
|
|
Functions.emplace_back(F);
|
2018-01-10 07:56:44 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
// Populate `Symbols` based on the WasmSymbols in the object
|
2017-11-18 02:14:09 +08:00
|
|
|
for (const SymbolRef &Sym : WasmObj->symbols()) {
|
|
|
|
const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
|
2018-02-23 13:08:53 +08:00
|
|
|
bool IsDefined = WasmSym.isDefined();
|
|
|
|
|
|
|
|
if (IsDefined) {
|
|
|
|
switch (WasmSym.Info.Kind) {
|
|
|
|
case WASM_SYMBOL_TYPE_FUNCTION: {
|
|
|
|
InputFunction *Function = getFunction(WasmSym);
|
|
|
|
if (isExcludedByComdat(Function)) {
|
|
|
|
Function->Live = false;
|
|
|
|
IsDefined = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Symbols.push_back(createDefinedFunction(WasmSym, Function));
|
2018-01-13 06:25:17 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-02-23 13:08:53 +08:00
|
|
|
case WASM_SYMBOL_TYPE_DATA: {
|
|
|
|
InputSegment *Segment = getSegment(WasmSym);
|
|
|
|
if (isExcludedByComdat(Segment)) {
|
|
|
|
Segment->Live = false;
|
|
|
|
IsDefined = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Symbols.push_back(createDefinedData(WasmSym, Segment,
|
|
|
|
WasmSym.Info.DataRef.Offset,
|
|
|
|
WasmSym.Info.DataRef.Size));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WASM_SYMBOL_TYPE_GLOBAL:
|
|
|
|
Symbols.push_back(createDefinedGlobal(WasmSym, getGlobal(WasmSym)));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unkown symbol kind");
|
2018-01-13 06:25:17 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
// Either the the symbol itself was undefined, or was excluded via comdat
|
|
|
|
// in which case this simply insertes the existing symbol into the correct
|
|
|
|
// slot in the Symbols array.
|
|
|
|
if (!IsDefined)
|
|
|
|
Symbols.push_back(createUndefined(WasmSym));
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
Symbol *ObjFile::createUndefined(const WasmSymbol &Sym) {
|
2018-02-28 08:09:22 +08:00
|
|
|
StringRef Name = Sym.Info.Name;
|
|
|
|
uint32_t Flags = Sym.Info.Flags;
|
|
|
|
|
|
|
|
switch (Sym.Info.Kind) {
|
|
|
|
case WASM_SYMBOL_TYPE_FUNCTION:
|
|
|
|
return Symtab->addUndefinedFunction(Name, Flags, this, Sym.FunctionType);
|
|
|
|
case WASM_SYMBOL_TYPE_DATA:
|
|
|
|
return Symtab->addUndefinedData(Name, Flags, this);
|
|
|
|
case WASM_SYMBOL_TYPE_GLOBAL:
|
|
|
|
return Symtab->addUndefinedGlobal(Name, Flags, this, Sym.GlobalType);
|
|
|
|
}
|
|
|
|
llvm_unreachable("unkown symbol kind");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
Symbol *ObjFile::createDefinedFunction(const WasmSymbol &Sym,
|
2018-02-17 07:50:23 +08:00
|
|
|
InputFunction *Function) {
|
2018-02-15 02:27:59 +08:00
|
|
|
if (Sym.isBindingLocal())
|
2018-02-23 13:08:53 +08:00
|
|
|
return make<DefinedFunction>(Sym.Info.Name, Sym.Info.Flags, this, Function);
|
|
|
|
return Symtab->addDefinedFunction(Sym.Info.Name, Sym.Info.Flags, this,
|
|
|
|
Function);
|
2018-02-15 02:27:59 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
Symbol *ObjFile::createDefinedData(const WasmSymbol &Sym, InputSegment *Segment,
|
2018-02-23 13:08:53 +08:00
|
|
|
uint32_t Offset, uint32_t Size) {
|
|
|
|
if (Sym.isBindingLocal())
|
|
|
|
return make<DefinedData>(Sym.Info.Name, Sym.Info.Flags, this, Segment,
|
|
|
|
Offset, Size);
|
|
|
|
return Symtab->addDefinedData(Sym.Info.Name, Sym.Info.Flags, this, Segment,
|
|
|
|
Offset, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *ObjFile::createDefinedGlobal(const WasmSymbol &Sym,
|
|
|
|
InputGlobal *Global) {
|
2018-02-15 02:27:59 +08:00
|
|
|
if (Sym.isBindingLocal())
|
2018-02-23 13:08:53 +08:00
|
|
|
return make<DefinedGlobal>(Sym.Info.Name, Sym.Info.Flags, this, Global);
|
|
|
|
return Symtab->addDefinedGlobal(Sym.Info.Name, Sym.Info.Flags, this, Global);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveFile::parse() {
|
|
|
|
// Parse a MemoryBufferRef as an archive file.
|
|
|
|
DEBUG(dbgs() << "Parsing library: " << toString(this) << "\n");
|
2017-12-07 06:08:17 +08:00
|
|
|
File = CHECK(Archive::create(MB), toString(this));
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Read the symbol table to construct Lazy symbols.
|
|
|
|
int Count = 0;
|
|
|
|
for (const Archive::Symbol &Sym : File->symbols()) {
|
|
|
|
Symtab->addLazy(this, &Sym);
|
|
|
|
++Count;
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << "Read " << Count << " symbols\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveFile::addMember(const Archive::Symbol *Sym) {
|
|
|
|
const Archive::Child &C =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(Sym->getMember(),
|
2017-11-18 02:14:09 +08:00
|
|
|
"could not get the member for symbol " + Sym->getName());
|
|
|
|
|
|
|
|
// Don't try to load the same member twice (this can happen when members
|
|
|
|
// mutually reference each other).
|
|
|
|
if (!Seen.insert(C.getChildOffset()).second)
|
|
|
|
return;
|
|
|
|
|
2017-12-06 11:10:39 +08:00
|
|
|
DEBUG(dbgs() << "loading lazy: " << Sym->getName() << "\n");
|
2017-11-18 02:14:09 +08:00
|
|
|
DEBUG(dbgs() << "from archive: " << toString(this) << "\n");
|
|
|
|
|
|
|
|
MemoryBufferRef MB =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(C.getMemoryBufferRef(),
|
2017-11-18 02:14:09 +08:00
|
|
|
"could not get the buffer for the member defining symbol " +
|
|
|
|
Sym->getName());
|
|
|
|
|
|
|
|
if (identify_magic(MB.getBuffer()) != file_magic::wasm_object) {
|
|
|
|
error("unknown file type: " + MB.getBufferIdentifier());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
InputFile *Obj = make<ObjFile>(MB);
|
|
|
|
Obj->ParentName = ParentName;
|
|
|
|
Symtab->addFile(Obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a string in the format of "foo.o" or "foo.a(bar.o)".
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string lld::toString(const wasm::InputFile *File) {
|
2017-11-18 02:14:09 +08:00
|
|
|
if (!File)
|
|
|
|
return "<internal>";
|
|
|
|
|
|
|
|
if (File->ParentName.empty())
|
|
|
|
return File->getName();
|
|
|
|
|
|
|
|
return (File->ParentName + "(" + File->getName() + ")").str();
|
|
|
|
}
|