Issue 244886
Summary [lld][WebAssembly] wasm-ld resolves `@TLSREL` to an absolute address for a symbol in `.data`
Labels lld
Assignees
Reporter ngg
    In a single-threaded Wasm link, one object can reference a variable with `@TLSREL` while another defines it in ordinary `.data`. This happens when the compiler lowers thread-local storage to ordinary data in the defining object. Both objects link into the same module. The link succeeds, but code that adds the relocated value to `__tls_base` gets the wrong address.

Reproduced with LLD 23.1.5 and with the WebAssembly linker at LLVM revision `4a11b56ab668`.

The downstream Rust reproducer builds a `HashSet` for `wasm32-wasip1` with `-C target-feature=+atomics`. It runs on Rust 1.99.1. With Rust 1.100.0, Wasmtime traps on an out-of-bounds access while `HashSet::new()` reads its thread-local hash seeds. [rust-lang/rust#166314](https://github.com/rust-lang/rust/issues/166314)

### Reproducer

`data.s`

```asm
.section .data.cell,"",@
.globl cell
cell:
  .int32 42
  .size cell, 4
```

`ref.s`

```asm
.globaltype __tls_base, i32
.globl address
address:
  .functype address () -> (i32)
  global.get __tls_base
 i32.const cell@TLSREL
  i32.add
  end_function
```

```sh
llvm-mc -triple=wasm32 -filetype=obj data.s -o data.o
llvm-mc -triple=wasm32 -filetype=obj ref.s -o ref.o
wasm-ld --no-entry --export=address --global-base=65536 data.o ref.o -o linked.wasm
llvm-objdump --disassemble-symbols=address --no-show-raw-insn linked.wasm
```

### Expected and actual results

Both `cell` and `__tls_base` are at 65536. The immediate for `cell@TLSREL` should therefore be `0`, and `address()` should return `65536`. The disassembly shows that the function adds 65536 to the base.

```asm
global.get 0
i32.const 65536
i32.add
end
```

Running `wasmtime --invoke address linked.wasm` returns `131072`, twice the address of `cell`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to