[lld] [WebAssembly] Generate a call to __wasm_apply_global_tls_relocs in __wasm_init_memory (PR #149832)

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 15 20:02:42 PDT 2025


sbc100 wrote:

> > but we should probably have only __wasm_init_tls do it.
> 
> How would `__tls_base` for the main thread be initialized? I understand `__wasm_init_memory` initializes it to the statically-allocated TLS area, but `__wasm_init_tls` expects it to be initialized already before it's called.
> But, more importantly, changing the existing behavior in `__wasm_init_memory` would be a breaking change for `wasix-libc` and `wasi-libc`. One can do lots of tricks in `wasix-libc` to support all compiler versions, but if you have a (outdated) version of wasix-libc installed already and decide to update your clang installation, you'll suddenly start getting broken modules. I'd rather avoid that scenario if possible, since it's quite difficult to debug for users.

Yes makes sense.

However it seems like there are two actual bugs here, neither of which are solved by this PR, but both of which probably deserve fixing:

1. Bug in lld: `memory.init` is called twice for the TLS region of the main thread.
2. Bug in wasi-libc:  __wasm_init_tls is not called on the main thread, even though it should be.   This PR doesn't fix this in the general case because __wasm_init_tls does more then just just call __wasm_apply_global_tls_relocs, and in particular applies relocations which must happen *after* the start function.

I think there are several ways we can solve (1) here in wasm-ld, but we will also need to address (2) downstream.



https://github.com/llvm/llvm-project/pull/149832


More information about the llvm-commits mailing list