<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60806>60806</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [wasm-ld] `--unresolved-symbols=ignore-all` does not actually resolve undefined functions to null
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          SingleAccretion
      </td>
    </tr>
</table>

<pre>
    Reproduction steps:
1) `test.c`:
```c
extern void undefined_func(void);

int main(void) {
    void* a = (void*)&undefined_func;
    asm (
        ""
 : "=r"(a)
        : "0"(a)
    );

    return (a == 0) ? 1 : 2;
}
```
2) `$env:EMCC_DEBUG=1` / `export EMCC_DEBUG=1`.
3) `emcc  emcc test.c -nostdlib -O2`
4) `emcc` will fail with a few `wasm-ld` errors like below:
```
wasm-ld: error: symbol exported via --export not found: stackSave
wasm-ld: error: symbol exported via --export not found: stackRestore
wasm-ld: error: symbol exported via --export not found: stackAlloc
wasm-ld: error: symbol exported via --export not found: __errno_location
wasm-ld: error: symbol exported via --export not found: __get_temp_ret
wasm-ld: error: symbol exported via --export not found: __set_temp_ret
emcc: error: 'C:/Users/Accretion/source/dotnet/emsdk/upstream/bin\wasm-ld.exe -o a.out.wasm C:\Users\ACCRET~1\AppData\Local\Temp\emscripten_temp\test_0.o -LC:\Users\Accretion\source\dotnet\emsdk\upstream\emscripten\cache\sysroot\lib\wasm32-emscripten -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --strip-debug --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=__start_em_js --export-if-defined=__stop_em_js --export-if-defined=__main_argc_argv --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=__get_temp_ret --export=__set_temp_ret --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=16777216 --global-base=1024' failed (returned 1)
```
5) Copy the linker command line, delete the problematic `export=<...>` arguments, delete `--import-undefined`, add `--unresolved-symbols=ignore-all`. Run it again with the modified command line.
6) `wasm-objdump -j Elem -x a.out.wasm`:
```
a.out.wasm:     file format wasm 0x1 
Section Details:

Elem[1]:
 - segment[0] flags=0 table=0 count=1 - init i32=1
  - elem[1] = func[1] <undefined_stub>
```
7) `wasm-objdump -d a.out.wasm`:
```
Code Disassembly:

000089 func[0] <__wasm_call_ctors>:
 00008a: 0b                         | end
00008c func[1] <undefined_stub>:
 00008d: 00                         | unreachable
 00008e: 0b                         | end
000090 func[2] <__original_main>:
 000091: 01 7f                      | local[2] type=i32
 000093: 41 81 80 80 80 00          | i32.const 1
 000099: 21 00                      | local.set 0
 00009b: 41 02 | i32.const 2
 00009d: 41 01                      | i32.const 1
 00009f: 20 00                      | local.get 0
 0000a1: 1b | select
 0000a2: 0b                         | end
0000a4 func[3] <main>:
 0000a5: 10 82 80 80 80 00          | call 2 <__original_main>
 0000ab: 0b                         | end
```
Expected behavior ([documentation](https://lld.llvm.org/WebAssembly.html#cmdoption-unresolved-symbols)): `undefined_stub` is not in the indirect function table, `&undefined_func` is resolved to zero.
Actual behavior: `&undefined_func` is resolved to `&undefined_stub` / `1`.

Analysis: [scanRelocations](https://github.com/llvm/llvm-project/blob/f8d5f0e53b04040cebbba639530133b261a4432f/lld/wasm/Relocations.cpp#L83) will first process the function index relocation, invoking `out.elemSec->addEntry(cast<FunctionSymbol>(sym));`, adding it to the indirect function table, and only then replace the function with a stub (set `isStub = true`), hence, the check in `ElemSection::addEntry` is not effective.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysWF9v4roS_zTuyyjIcUiAhz7QUO7LXl1pe47uI3KcAdw6dmQ7tOzD_exXdkJIaXt2V9oK0cSe-c1fz4zhzsmDRrwn-QPJN3e880dj75-kPihcC2HRS6PvKlOf779ja03dibACzmPrSL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