Skip to content

Commit

Permalink
[WebAssembly] Output functions individually
Browse files Browse the repository at this point in the history
The code section is now written out one function
at a time rather than all the functions in a given
objects being serialized at once.

This change lays the groundwork for supporting
--gc-sections.

Differential Revision: https://reviews.llvm.org/D41315

llvm-svn: 322138
  • Loading branch information
sbc100 committed Jan 9, 2018
1 parent 1f56217 commit 8d146bb
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 214 deletions.
101 changes: 58 additions & 43 deletions lld/wasm/InputFiles.cpp
Expand Up @@ -10,6 +10,7 @@
#include "InputFiles.h"

#include "Config.h"
#include "InputFunction.h"
#include "InputSegment.h"
#include "SymbolTable.h"
#include "lld/Common/ErrorHandler.h"
Expand Down Expand Up @@ -43,34 +44,20 @@ Optional<MemoryBufferRef> lld::wasm::readFile(StringRef Path) {
}

void ObjFile::dumpInfo() const {
log("reloc info for: " + getName() + "\n" +
" FunctionIndexOffset : " + Twine(FunctionIndexOffset) + "\n" +
" NumFunctionImports : " + Twine(NumFunctionImports()) + "\n" +
" NumGlobalImports : " + Twine(NumGlobalImports()) + "\n");
log("info for: " + getName() + "\n" +
" Total Functions : " + Twine(FunctionSymbols.size()) + "\n" +
" Total Globals : " + Twine(GlobalSymbols.size()) + "\n" +
" Function Imports : " + Twine(NumFunctionImports) + "\n" +
" Global Imports : " + Twine(NumGlobalImports) + "\n" +
" Table Entries : " + Twine(TableSymbols.size()) + "\n");
}

bool ObjFile::isImportedFunction(uint32_t Index) const {
return Index < NumFunctionImports();
}

Symbol *ObjFile::getFunctionSymbol(uint32_t Index) const {
return FunctionSymbols[Index];
}

Symbol *ObjFile::getTableSymbol(uint32_t Index) const {
return TableSymbols[Index];
}

Symbol *ObjFile::getGlobalSymbol(uint32_t Index) const {
return GlobalSymbols[Index];
}

uint32_t ObjFile::getRelocatedAddress(uint32_t Index) const {
return getGlobalSymbol(Index)->getVirtualAddress();
uint32_t ObjFile::getRelocatedAddress(uint32_t GlobalIndex) const {
return GlobalSymbols[GlobalIndex]->getVirtualAddress();
}

uint32_t ObjFile::relocateFunctionIndex(uint32_t Original) const {
Symbol *Sym = getFunctionSymbol(Original);
Symbol *Sym = FunctionSymbols[Original];
uint32_t Index = Sym->getOutputIndex();
DEBUG(dbgs() << "relocateFunctionIndex: " << toString(*Sym) << ": "
<< Original << " -> " << Index << "\n");
Expand All @@ -82,15 +69,15 @@ uint32_t ObjFile::relocateTypeIndex(uint32_t Original) const {
}

uint32_t ObjFile::relocateTableIndex(uint32_t Original) const {
Symbol *Sym = getTableSymbol(Original);
Symbol *Sym = TableSymbols[Original];
uint32_t Index = Sym->hasTableIndex() ? Sym->getTableIndex() : 0;
DEBUG(dbgs() << "relocateTableIndex: " << toString(*Sym) << ": " << Original
<< " -> " << Index << "\n");
return Index;
}

uint32_t ObjFile::relocateGlobalIndex(uint32_t Original) const {
Symbol *Sym = getGlobalSymbol(Original);
Symbol *Sym = GlobalSymbols[Original];
uint32_t Index = Sym->hasOutputIndex() ? Sym->getOutputIndex() : 0;
DEBUG(dbgs() << "relocateGlobalIndex: " << toString(*Sym) << ": " << Original
<< " -> " << Index << "\n");
Expand Down Expand Up @@ -125,7 +112,7 @@ void ObjFile::parse() {
}

// Return the InputSegment in which a given symbol is defined.
InputSegment *ObjFile::getSegment(const WasmSymbol &WasmSym) {
InputSegment *ObjFile::getSegment(const WasmSymbol &WasmSym) const {
uint32_t Address = WasmObj->getWasmSymbolValue(WasmSym);
for (InputSegment *Segment : Segments) {
if (Address >= Segment->startVA() && Address < Segment->endVA()) {
Expand All @@ -141,52 +128,78 @@ InputSegment *ObjFile::getSegment(const WasmSymbol &WasmSym) {

static void copyRelocationsRange(std::vector<WasmRelocation> &To,
ArrayRef<WasmRelocation> From, size_t Start,
size_t End) {
size_t Size) {
for (const WasmRelocation &R : From)
if (R.Offset >= Start && R.Offset < End)
if (R.Offset >= Start && R.Offset < Start + Size)
To.push_back(R);
}

// Get the signature for a given function symbol, either by looking
// it up in function sections (for defined functions), of the imports section
// (for imported functions).
const WasmSignature *ObjFile::getFunctionSig(const WasmSymbol &Sym) const {
DEBUG(dbgs() << "getFunctionSig: " << Sym.Name << "\n");
return &WasmObj->types()[Sym.FunctionType];
}

InputFunction *ObjFile::getFunction(const WasmSymbol &Sym) const {
uint32_t FunctionIndex = Sym.ElementIndex - NumFunctionImports;
return Functions[FunctionIndex];
}

void ObjFile::initializeSymbols() {
Symbols.reserve(WasmObj->getNumberOfSymbols());

for (const WasmImport &Import : WasmObj->imports()) {
switch (Import.Kind) {
case WASM_EXTERNAL_FUNCTION:
++FunctionImports;
++NumFunctionImports;
break;
case WASM_EXTERNAL_GLOBAL:
++GlobalImports;
++NumGlobalImports;
break;
}
}

FunctionSymbols.resize(FunctionImports + WasmObj->functions().size());
GlobalSymbols.resize(GlobalImports + WasmObj->globals().size());
FunctionSymbols.resize(NumFunctionImports + WasmObj->functions().size());
GlobalSymbols.resize(NumGlobalImports + WasmObj->globals().size());

for (const WasmSegment &S : WasmObj->dataSegments()) {
InputSegment *Seg = make<InputSegment>(&S, this);
InputSegment *Seg = make<InputSegment>(S, *this);
copyRelocationsRange(Seg->Relocations, DataSection->Relocations,
Seg->getInputSectionOffset(),
Seg->getInputSectionOffset() + Seg->getSize());
Seg->getInputSectionOffset(), Seg->getSize());
Segments.emplace_back(Seg);
}

ArrayRef<WasmFunction> Funcs = WasmObj->functions();
ArrayRef<uint32_t> FuncTypes = WasmObj->functionTypes();
ArrayRef<WasmSignature> Types = WasmObj->types();
for (size_t I = 0; I < Funcs.size(); ++I) {
const WasmFunction &Func = Funcs[I];
const WasmSignature &Sig = Types[FuncTypes[I]];
InputFunction *Function = make<InputFunction>(Sig, Func, *this);
copyRelocationsRange(Function->Relocations, CodeSection->Relocations,
Func.CodeSectionOffset, Func.Size);
Functions.emplace_back(Function);
}

// Populate `FunctionSymbols` and `GlobalSymbols` based on the WasmSymbols
// in the object
for (const SymbolRef &Sym : WasmObj->symbols()) {
const WasmSymbol &WasmSym = WasmObj->getWasmSymbol(Sym.getRawDataRefImpl());
Symbol *S;
switch (WasmSym.Type) {
case WasmSymbol::SymbolType::FUNCTION_IMPORT:
S = createUndefined(WasmSym, getFunctionSig(WasmSym));
break;
case WasmSymbol::SymbolType::GLOBAL_IMPORT:
S = createUndefined(WasmSym);
break;
case WasmSymbol::SymbolType::GLOBAL_EXPORT:
S = createDefined(WasmSym, getSegment(WasmSym));
S = createDefined(WasmSym, getSegment(WasmSym), nullptr);
break;
case WasmSymbol::SymbolType::FUNCTION_EXPORT:
S = createDefined(WasmSym);
S = createDefined(WasmSym, nullptr, getFunction(WasmSym));
break;
case WasmSymbol::SymbolType::DEBUG_FUNCTION_NAME:
// These are for debugging only, no need to create linker symbols for them
Expand Down Expand Up @@ -228,20 +241,22 @@ void ObjFile::initializeSymbols() {
fatal(getName() + ": unsupported element segment offset");
TableSymbols.reserve(Segment.Functions.size());
for (uint64_t FunctionIndex : Segment.Functions)
TableSymbols.push_back(getFunctionSymbol(FunctionIndex));
TableSymbols.push_back(FunctionSymbols[FunctionIndex]);
}

DEBUG(dbgs() << "TableSymbols: " << TableSymbols.size() << "\n");
DEBUG(dbgs() << "Functions : " << FunctionSymbols.size() << "\n");
DEBUG(dbgs() << "Globals : " << GlobalSymbols.size() << "\n");
}

Symbol *ObjFile::createUndefined(const WasmSymbol &Sym) {
return Symtab->addUndefined(this, &Sym);
Symbol *ObjFile::createUndefined(const WasmSymbol &Sym,
const WasmSignature *Signature) {
return Symtab->addUndefined(this, &Sym, Signature);
}

Symbol *ObjFile::createDefined(const WasmSymbol &Sym,
const InputSegment *Segment) {
const InputSegment *Segment,
InputFunction *Function) {
Symbol *S;
if (Sym.isLocal()) {
S = make<Symbol>(Sym.Name, true);
Expand All @@ -252,10 +267,10 @@ Symbol *ObjFile::createDefined(const WasmSymbol &Sym,
Kind = Symbol::Kind::DefinedGlobalKind;
else
llvm_unreachable("invalid local symbol type");
S->update(Kind, this, &Sym, Segment);
S->update(Kind, this, &Sym, Segment, Function);
return S;
}
return Symtab->addDefined(this, &Sym, Segment);
return Symtab->addDefined(this, &Sym, Segment, Function);
}

void ArchiveFile::parse() {
Expand Down
32 changes: 14 additions & 18 deletions lld/wasm/InputFiles.h
Expand Up @@ -26,11 +26,13 @@ using llvm::object::WasmObjectFile;
using llvm::object::WasmSection;
using llvm::object::WasmSymbol;
using llvm::wasm::WasmImport;
using llvm::wasm::WasmSignature;

namespace lld {
namespace wasm {

class Symbol;
class InputFunction;
class InputSegment;

class InputFile {
Expand Down Expand Up @@ -95,34 +97,27 @@ class ObjFile : public InputFile {
uint32_t relocateTableIndex(uint32_t Original) const;
uint32_t getRelocatedAddress(uint32_t Index) const;

// Returns true if the given function index is an imported function,
// as opposed to the locally defined function.
bool isImportedFunction(uint32_t Index) const;
size_t getNumGlobalImports() const { return NumGlobalImports; }

size_t NumFunctionImports() const { return FunctionImports; }
size_t NumGlobalImports() const { return GlobalImports; }

int32_t FunctionIndexOffset = 0;
const WasmSection *CodeSection = nullptr;
std::vector<OutputRelocation> CodeRelocations;
int32_t CodeOffset = 0;
const WasmSection *DataSection = nullptr;

std::vector<uint32_t> TypeMap;
std::vector<InputSegment *> Segments;
std::vector<InputFunction *> Functions;

ArrayRef<Symbol *> getSymbols() { return Symbols; }
ArrayRef<Symbol *> getTableSymbols() { return TableSymbols; }

private:
Symbol *createDefined(const WasmSymbol &Sym,
const InputSegment *Segment = nullptr);
Symbol *createUndefined(const WasmSymbol &Sym);
const InputSegment *Segment = nullptr,
InputFunction *Function = nullptr);
Symbol *createUndefined(const WasmSymbol &Sym,
const WasmSignature *Signature = nullptr);
void initializeSymbols();
InputSegment *getSegment(const WasmSymbol &WasmSym);
Symbol *getFunctionSymbol(uint32_t FunctionIndex) const;
Symbol *getTableSymbol(uint32_t TableIndex) const;
Symbol *getGlobalSymbol(uint32_t GlobalIndex) const;
InputSegment *getSegment(const WasmSymbol &WasmSym) const;
const WasmSignature *getFunctionSig(const WasmSymbol &Sym) const;
InputFunction *getFunction(const WasmSymbol &Sym) const;

// List of all symbols referenced or defined by this file.
std::vector<Symbol *> Symbols;
Expand All @@ -136,8 +131,9 @@ class ObjFile : public InputFile {
// List of all indirect symbols indexed by table index space.
std::vector<Symbol *> TableSymbols;

uint32_t GlobalImports = 0;
uint32_t FunctionImports = 0;
const WasmSection *DataSection = nullptr;
uint32_t NumGlobalImports = 0;
uint32_t NumFunctionImports = 0;
std::unique_ptr<WasmObjectFile> WasmObj;
};

Expand Down
57 changes: 57 additions & 0 deletions lld/wasm/InputFunction.h
@@ -0,0 +1,57 @@
//===- InpuFunction.h -------------------------------------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Represents a WebAssembly function in an input file which could also be
// assigned a function index in the output.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_WASM_INPUT_FUNCTION_H
#define LLD_WASM_INPUT_FUNCTION_H

#include "WriterUtils.h"
#include "llvm/Object/Wasm.h"

using llvm::wasm::WasmRelocation;
using llvm::wasm::WasmFunction;

namespace lld {
namespace wasm {

class ObjFile;

class InputFunction {
public:
InputFunction(const WasmSignature &S, const WasmFunction &Func,
const ObjFile &F)
: Signature(S), Function(Func), File(F) {}

uint32_t getOutputIndex() const { return OutputIndex.getValue(); };
bool hasOutputIndex() const { return OutputIndex.hasValue(); };

void setOutputIndex(uint32_t Index) {
assert(!hasOutputIndex());
OutputIndex = Index;
};

const WasmSignature &Signature;
const WasmFunction &Function;
int32_t OutputOffset = 0;
std::vector<WasmRelocation> Relocations;
std::vector<OutputRelocation> OutRelocations;
const ObjFile &File;

protected:
llvm::Optional<uint32_t> OutputIndex;
};

} // namespace wasm
} // namespace lld

#endif // LLD_WASM_INPUT_FUNCTION_H
16 changes: 8 additions & 8 deletions lld/wasm/InputSegment.h
Expand Up @@ -36,7 +36,7 @@ class OutputSegment;

class InputSegment {
public:
InputSegment(const WasmSegment *Seg, const ObjFile *F)
InputSegment(const WasmSegment &Seg, const ObjFile &F)
: Segment(Seg), File(F) {}

// Translate an offset in the input segment to an offset in the output
Expand All @@ -47,21 +47,21 @@ class InputSegment {

uint32_t getOutputSegmentOffset() const { return OutputSegmentOffset; }

uint32_t getInputSectionOffset() const { return Segment->SectionOffset; }
uint32_t getInputSectionOffset() const { return Segment.SectionOffset; }

void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
OutputSeg = Segment;
OutputSegmentOffset = Offset;
}

uint32_t getSize() const { return Segment->Data.Content.size(); }
uint32_t getAlignment() const { return Segment->Data.Alignment; }
uint32_t startVA() const { return Segment->Data.Offset.Value.Int32; }
uint32_t getSize() const { return Segment.Data.Content.size(); }
uint32_t getAlignment() const { return Segment.Data.Alignment; }
uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
uint32_t endVA() const { return startVA() + getSize(); }
StringRef getName() const { return Segment->Data.Name; }
StringRef getName() const { return Segment.Data.Name; }

const WasmSegment *Segment;
const ObjFile *File;
const WasmSegment &Segment;
const ObjFile &File;
std::vector<WasmRelocation> Relocations;
std::vector<OutputRelocation> OutRelocations;

Expand Down

0 comments on commit 8d146bb

Please sign in to comment.