Skip to content

Commit 20c933e

Browse files
sbc100kateinoigakukun
authored andcommitted
[lld][WebAssembly] Allow symbols with explict import names to be undefined at link time.
Differential Revision: https://reviews.llvm.org/D74110
1 parent 0b0ebf2 commit 20c933e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lld/docs/WebAssembly.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ The default behaviour is to generate these stub function and to produce
112112
a warning. The ``--fatal-warnings`` flag can be used to disable this behaviour
113113
and error out if mismatched are found.
114114

115-
Imports and Exports
116-
~~~~~~~~~~~~~~~~~~~
115+
Exports
116+
~~~~~~~
117117

118118
When building a shared library any symbols marked as ``visibility=default`` will
119119
be exported.
@@ -130,6 +130,17 @@ Finally, just like with native ELF linker the ``--export-dynamic`` flag can be
130130
used to export symbols in the executable which are marked as
131131
``visibility=default``.
132132

133+
Imports
134+
~~~~~~~
135+
136+
By default no undefined symbols are allowed in the final binary. The flag
137+
``--allow-undefined`` results in a WebAssembly import being defined for each
138+
undefined symbol. It is then up to the runtime to provide such symbols.
139+
140+
Alternativly symbols can be marked in the source code as with the
141+
``import_name`` and/or ``import_module`` clang attributes which signals that
142+
they are expected to be undefined at static link time.
143+
133144
Garbage Collection
134145
~~~~~~~~~~~~~~~~~~
135146

lld/test/wasm/import-name.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: llc -filetype=obj %s -o %t.o
2-
; RUN: wasm-ld --allow-undefined -o %t.wasm %t.o
2+
; RUN: wasm-ld -o %t.wasm %t.o
33
; RUN: obj2yaml %t.wasm | FileCheck %s
44

55
target triple = "wasm32-unknown-unknown"

lld/wasm/Relocations.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ static bool allowUndefined(const Symbol* sym) {
2828
// compiling with -fPIC)
2929
if (isa<DataSymbol>(sym))
3030
return false;
31+
// Undefined functions with explicit import name are allowed to be undefined
32+
// at link time.
33+
if (auto *F = dyn_cast<UndefinedFunction>(sym))
34+
if (F->importName)
35+
return true;
3136
return (config->allowUndefined ||
3237
config->allowUndefinedSymbols.count(sym->getName()) != 0);
3338
}

0 commit comments

Comments
 (0)