[lld][WebAssembly] Add --unresolved-symbols=import-dynamic
This is a new mode for handling unresolved symbols that allows all
symbols to be imported in the same that they would be in the case of
`-fpie` or `-shared`, but generting an otherwise fixed/non-relocatable
binary.
Code linked in this way should still be compiled with `-fPIC` so that
data symbols can be resolved via imports.
This essentially allows the building of static binaries that have
dynamic imports. See:
https://github.com/emscripten-core/emscripten/issues/12682
As with other uses of the experimental dynamic linking ABI, this
behaviour will produce a warning unless run with `--experimental-pic`.
Differential Revision: https://reviews.llvm.org/D91577
diff --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 0109b67..c7710a9 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -20,7 +20,8 @@
namespace wasm {
static bool requiresGOTAccess(const Symbol *sym) {
- if (!config->isPic)
+ if (!config->isPic &&
+ config->unresolvedSymbols != UnresolvedPolicy::ImportDynamic)
return false;
if (sym->isHidden() || sym->isLocal())
return false;
@@ -66,6 +67,8 @@
}
}
break;
+ case UnresolvedPolicy::ImportDynamic:
+ break;
}
}
}
@@ -139,7 +142,9 @@
break;
}
- if (config->isPic) {
+ if (config->isPic ||
+ (sym->isUndefined() &&
+ config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic)) {
switch (reloc.Type) {
case R_WASM_TABLE_INDEX_SLEB:
case R_WASM_TABLE_INDEX_SLEB64:
@@ -150,8 +155,8 @@
// Certain relocation types can't be used when building PIC output,
// since they would require absolute symbol addresses at link time.
error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
- " cannot be used against symbol " + toString(*sym) +
- "; recompile with -fPIC");
+ " cannot be used against symbol `" + toString(*sym) +
+ "`; recompile with -fPIC");
break;
case R_WASM_TABLE_INDEX_I32:
case R_WASM_TABLE_INDEX_I64: