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

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 1 15:02:31 PDT 2025


Arshia001 wrote:

> you will still need to call __wasm_init_tls on all threads

Yes, this already happens in both wasix-libc and wasi-libc as part of the thread creation routine.

> including the main thread because this function also calls __wasm_apply_tls_relocs

Not quite sure why this would be the case, considering the statically-allocated TLS section exists and is initialized by `__wasm_init_memory`? Neither of the wasi(x)-libc's make this call today. Note that `__wasm_init_memory` initializes `__tls_base` for the main thread as well, so the call to `__wasm_apply_tls_relocs` is safe in that regard.

> but it cannot be part of the start function and (like __wasm_apply_data_relocs can only be called once all symbols are resolved).

If there are external symbols, then yes. However, I have only ever seen (and you pointed this out as well) that `__wasm_apply_tls_relocs` only initializes DSO-local symbols, which should be safe to call within the start function. Unless I'm missing something here?

> I assume you are calling __wasm_apply_data_relocs somewhere in your dynamic linker?

Yes, at the very last stage when every module is already loaded in and instantiated, since we need everything to be resolved.

I did a bit more digging, and the same problem also exists in DL modules; however, there the problem doesn't show itself (at least in anything we've compiled). Breakdown of the situation:

* Non-DL module: the pre-allocated TLS area for the main thread lives at offset 1024. Hence, the global needs to be relocated to account for a `__tls_base` of 1024. We get an error.
* DL module, running on the Wasmer linker: the dynamic linker puts `__memory_base` for the main thread at offset 0, hence the initial value for the `$GOT.data.internal` TLS symbols are correct, and we don't see any issues.

While this problem also exists in DL modules, I will again stress the fact that `__wasm_apply_tls_relocs` is also generated for non-DL modules, where no dynamic linker comes into play at any point, so there's no "link finalization phase" from which to call `__wasm_apply_tls_relocs`.

As long as `__wasm_apply_tls_relocs` only relocates DSO-local symbols, I don't see why it should be unsafe to be called from the start function.

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


More information about the llvm-commits mailing list