[WebAssembly] Improve support linker synthetic symbols

This is part of larger change to add synthetic symbols
for section start/end points and init/fini_array:
  https://reviews.llvm.org/D40760

Allows synthetic global symbols to have an explicitly set
virtual address.

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

llvm-svn: 319813
This commit is contained in:
Sam Clegg 2017-12-05 19:05:45 +00:00
parent a0d50ce7af
commit 4eedcfc946
5 changed files with 27 additions and 17 deletions

View File

@ -39,7 +39,8 @@ struct Configuration {
llvm::StringSet<> AllowUndefinedSymbols;
std::vector<llvm::StringRef> SearchPaths;
std::vector<std::pair<Symbol *, WasmGlobal>> SyntheticGlobals;
std::vector<Symbol *> SyntheticGlobals;
Symbol *StackPointerSymbol = nullptr;
};
// The only instance of Configuration struct.

View File

@ -138,17 +138,12 @@ static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
// Wasm global are used in relocatable object files to model symbol imports
// and exports. In the final executable the only use of wasm globals is
// for the exlicit stack pointer (__stack_pointer).
static void addSyntheticGlobal(StringRef Name, int32_t Value) {
static Symbol* addSyntheticGlobal(StringRef Name, int32_t Value) {
log("injecting global: " + Name);
Symbol *S = Symtab->addDefinedGlobal(Name);
S->setOutputIndex(Config->SyntheticGlobals.size());
WasmGlobal Global;
Global.Mutable = true;
Global.Type = WASM_TYPE_I32;
Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
Global.InitExpr.Value.Int32 = Value;
Config->SyntheticGlobals.emplace_back(S, Global);
S->setVirtualAddress(Value);
Config->SyntheticGlobals.emplace_back(S);
return S;
}
// Inject a new undefined symbol into the link. This will cause the link to
@ -285,7 +280,7 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
static WasmSignature Signature = {{}, WASM_TYPE_NORESULT};
addSyntheticUndefinedFunction(Config->Entry, &Signature);
addSyntheticGlobal("__stack_pointer", 0);
Config->StackPointerSymbol = addSyntheticGlobal("__stack_pointer", 0);
}
createFiles(Args);

View File

@ -41,6 +41,8 @@ uint32_t Symbol::getVirtualAddress() const {
DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
if (isUndefined())
return UINT32_MAX;
if (VirtualAddress.hasValue())
return VirtualAddress.getValue();
assert(Sym != nullptr);
ObjFile *Obj = cast<ObjFile>(File);
@ -57,6 +59,11 @@ uint32_t Symbol::getOutputIndex() const {
return OutputIndex.getValue();
}
void Symbol::setVirtualAddress(uint32_t Value) {
DEBUG(dbgs() << "setVirtualAddress " << Name << " -> " << Value << "\n");
VirtualAddress = Value;
}
void Symbol::setOutputIndex(uint32_t Index) {
DEBUG(dbgs() << "setOutputIndex " << Name << " -> " << Index << "\n");
assert(!hasOutputIndex());

View File

@ -77,12 +77,14 @@ public:
uint32_t getVirtualAddress() const;
// Returns true if an output index has been set for this symbol
bool hasOutputIndex() { return OutputIndex.hasValue(); }
bool hasOutputIndex() const { return OutputIndex.hasValue(); }
// Set the output index of the symbol (in the function or global index
// space of the output object.
void setOutputIndex(uint32_t Index);
void setVirtualAddress(uint32_t VA);
void update(Kind K, InputFile *F = nullptr, const WasmSymbol *Sym = nullptr,
const InputSegment *Segment = nullptr,
const WasmSignature *Sig = nullptr);
@ -105,6 +107,7 @@ protected:
const WasmSymbol *Sym = nullptr;
const InputSegment *Segment = nullptr;
llvm::Optional<uint32_t> OutputIndex;
llvm::Optional<uint32_t> VirtualAddress;
const WasmSignature *FunctionType;
};

View File

@ -149,7 +149,7 @@ void Writer::createImportSection() {
writeUleb128(OS, NumImports, "import count");
for (Symbol *Sym : FunctionImports) {
for (const Symbol *Sym : FunctionImports) {
WasmImport Import;
Import.Module = "env";
Import.Field = Sym->getName();
@ -169,7 +169,7 @@ void Writer::createImportSection() {
writeImport(OS, Import);
}
for (Symbol *Sym : GlobalImports) {
for (const Symbol *Sym : GlobalImports) {
WasmImport Import;
Import.Module = "env";
Import.Field = Sym->getName();
@ -221,8 +221,12 @@ void Writer::createGlobalSection() {
raw_ostream &OS = Section->getStream();
writeUleb128(OS, NumGlobals, "global count");
for (auto &Pair : Config->SyntheticGlobals) {
WasmGlobal &Global = Pair.second;
for (const Symbol *Sym : Config->SyntheticGlobals) {
WasmGlobal Global;
Global.Type = WASM_TYPE_I32;
Global.Mutable = Sym == Config->StackPointerSymbol;
Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
writeGlobal(OS, Global);
}
@ -507,7 +511,7 @@ void Writer::layoutMemory() {
debugPrint("mem: stack size = %d\n", Config->ZStackSize);
debugPrint("mem: stack base = %d\n", MemoryPtr);
MemoryPtr += Config->ZStackSize;
Config->SyntheticGlobals[0].second.InitExpr.Value.Int32 = MemoryPtr;
Config->StackPointerSymbol->setVirtualAddress(MemoryPtr);
debugPrint("mem: stack top = %d\n", MemoryPtr);
}