[WebAssembly] Initial implementation of PIC code generation
This change implements lowering of references global symbols in PIC
mode.
This change implements lowering of global references in PIC mode using a
new @GOT reference type. @GOT references can be used with function or
data symbol names combined with the get_global instruction. In this case
the linker will insert the wasm global that stores the address of the
symbol (either in memory for data symbols or in the wasm table for
function symbols).
For now I'm continuing to use the R_WASM_GLOBAL_INDEX_LEB relocation
type for this type of reference which means that this relocation type
can refer to either a global or a function or data symbol. We could
choose to introduce specific relocation types for GOT entries in the
future. See the current dynamic linking proposal:
https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
Differential Revision: https://reviews.llvm.org/D54647
llvm-svn: 357022
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 5f12ce7..480257f 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -118,6 +118,7 @@
std::vector<const WasmSignature *> Types;
DenseMap<WasmSignature, int32_t> TypeIndices;
std::vector<const Symbol *> ImportedSymbols;
+ std::vector<const Symbol *> GOTSymbols;
unsigned NumImportedFunctions = 0;
unsigned NumImportedGlobals = 0;
unsigned NumImportedEvents = 0;
@@ -147,7 +148,7 @@
} // anonymous namespace
void Writer::createImportSection() {
- uint32_t NumImports = ImportedSymbols.size();
+ uint32_t NumImports = ImportedSymbols.size() + GOTSymbols.size();
if (Config->ImportMemory)
++NumImports;
if (Config->ImportTable)
@@ -204,9 +205,6 @@
if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) {
Import.Kind = WASM_EXTERNAL_FUNCTION;
Import.SigIndex = lookupType(*FunctionSym->Signature);
- } else if (auto *DataSym = dyn_cast<UndefinedData>(Sym)) {
- Import.Kind = WASM_EXTERNAL_GLOBAL;
- Import.Global = {WASM_TYPE_I32, true};
} else if (auto *GlobalSym = dyn_cast<GlobalSymbol>(Sym)) {
Import.Kind = WASM_EXTERNAL_GLOBAL;
Import.Global = *GlobalSym->getGlobalType();
@@ -218,6 +216,18 @@
}
writeImport(OS, Import);
}
+
+ for (const Symbol *Sym : GOTSymbols) {
+ WasmImport Import;
+ Import.Kind = WASM_EXTERNAL_GLOBAL;
+ Import.Global = {WASM_TYPE_I32, true};
+ if (isa<DataSymbol>(Sym))
+ Import.Module = "GOT.mem";
+ else
+ Import.Module = "GOT.func";
+ Import.Field = Sym->getName();
+ writeImport(OS, Import);
+ }
}
void Writer::createTypeSection() {
@@ -957,9 +967,9 @@
continue;
if (!Sym->IsUsedInRegularObj)
continue;
- // In relocatable output we don't generate imports for data symbols.
- // These live only in the symbol table.
- if (Config->Relocatable && isa<DataSymbol>(Sym))
+ // We don't generate imports for data symbols. They however can be imported
+ // as GOT entries.
+ if (isa<DataSymbol>(Sym))
continue;
LLVM_DEBUG(dbgs() << "import: " << Sym->getName() << "\n");
@@ -968,8 +978,6 @@
F->setFunctionIndex(NumImportedFunctions++);
else if (auto *G = dyn_cast<GlobalSymbol>(Sym))
G->setGlobalIndex(NumImportedGlobals++);
- else if (auto *D = dyn_cast<UndefinedData>(Sym))
- D->setGlobalIndex(NumImportedGlobals++);
else
cast<EventSymbol>(Sym)->setEventIndex(NumImportedEvents++);
}
@@ -1145,6 +1153,13 @@
DataSym->getName());
break;
}
+ case R_WASM_GLOBAL_INDEX_LEB: {
+ auto* Sym = File->getSymbols()[Reloc.Index];
+ if (!isa<GlobalSymbol>(Sym) && !Sym->isInGOT()) {
+ Sym->setGOTIndex(NumImportedGlobals++);
+ GOTSymbols.push_back(Sym);
+ }
+ }
}
}
}