Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm-ld] --unresolved-symbols=ignore-all does not actually resolve undefined functions to null #60806

Open
SingleAccretion opened this issue Feb 16, 2023 · 1 comment
Assignees
Labels

Comments

@SingleAccretion
Copy link
Contributor

SingleAccretion commented Feb 16, 2023

Reproduction steps:

  1. test.c:
extern void undefined_func(void);

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

    return (a == 0) ? 1 : 2;
}
  1. $env:EMCC_DEBUG=1 / export EMCC_DEBUG=1.
  2. emcc test.c -nostdlib -O2
  3. 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)
  1. 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.
  2. wasm-objdump -j Elem -x a.out.wasm:
Elem[1]:
 - segment[0] flags=0 table=0 count=1 - init i32=1
  - elem[1] = func[1] <undefined_stub>
  1. 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): 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 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.

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 16, 2023

@llvm/issue-subscribers-lld-wasm

@sbc100 sbc100 self-assigned this Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants