Skip to content

Commit ef8c913

Browse files
committed
[WebAssembly] Allow import and export of TLS symbols between DSOs
We previously had a limitation that TLS variables could not be exported (and therefore could also not be imported). This change removed that limitation. Differential Revision: https://reviews.llvm.org/D108877
1 parent 5041a48 commit ef8c913

31 files changed

+307
-70
lines changed

lld/test/wasm/pie.ll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ declare void @external_func()
9191
; RUN: obj2yaml %t.shmem.wasm | FileCheck %s --check-prefix=SHMEM
9292

9393
; SHMEM: - Type: CODE
94-
; SHMEM: - Index: 6
94+
; SHMEM: - Index: 7
9595
; SHMEM-NEXT: Locals: []
9696
; SHMEM-NEXT: Body: 100310050B
9797

@@ -109,11 +109,13 @@ declare void @external_func()
109109
; SHMEM-NEXT: - Index: 5
110110
; SHMEM-NEXT: Name: __wasm_apply_global_relocs
111111
; SHMEM-NEXT: - Index: 6
112-
; SHMEM-NEXT: Name: __wasm_start
112+
; SHMEM-NEXT: Name: __wasm_apply_global_tls_relocs
113113
; SHMEM-NEXT: - Index: 7
114-
; SHMEM-NEXT: Name: foo
114+
; SHMEM-NEXT: Name: __wasm_start
115115
; SHMEM-NEXT: - Index: 8
116-
; SHMEM-NEXT: Name: get_data_address
116+
; SHMEM-NEXT: Name: foo
117117
; SHMEM-NEXT: - Index: 9
118+
; SHMEM-NEXT: Name: get_data_address
119+
; SHMEM-NEXT: - Index: 10
118120
; SHMEM-NEXT: Name: _start
119121

lld/test/wasm/shared-needed.s

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ data:
2929
# SO1-NEXT: TableSize: 0
3030
# SO1-NEXT: TableAlignment: 0
3131
# SO1-NEXT: Needed: []
32+
# SO1-NEXT: ExportInfo: []
3233
# SO1-NEXT: - Type: TYPE
3334

3435
# SO2: Sections:
@@ -40,4 +41,5 @@ data:
4041
# SO2-NEXT: TableAlignment: 0
4142
# SO2-NEXT: Needed:
4243
# SO2-NEXT: - shared-needed.s.tmp1.so
44+
# SO2-NEXT: ExportInfo: []
4345
# SO2-NEXT: - Type: TYPE

lld/test/wasm/shared.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ get_local_func_address:
133133
# CHECK-NEXT: TableSize: 2
134134
# CHECK-NEXT: TableAlignment: 0
135135
# CHECK-NEXT: Needed: []
136+
# CHECK-NEXT: ExportInfo: []
136137
# CHECK-NEXT: - Type: TYPE
137138

138139
# check for import of __table_base and __memory_base globals

lld/test/wasm/shared64.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ get_local_func_address:
134134
# CHECK-NEXT: TableSize: 2
135135
# CHECK-NEXT: TableAlignment: 0
136136
# CHECK-NEXT: Needed: []
137+
# CHECK-NEXT: ExportInfo: []
137138
# CHECK-NEXT: - Type: TYPE
138139

139140
# check for import of __table_base and __memory_base globals

lld/test/wasm/tls-export.s

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
2-
# RUN: wasm-ld -no-gc-sections --shared-memory --no-entry -o %t.wasm %t.o
3-
# RUN: not wasm-ld --shared-memory --no-entry --export=tls1 -o %t.wasm %t.o 2>&1 | FileCheck %s
4-
# With --export-all we ignore TLS symbols so we don't expect an error here
5-
# RUN: wasm-ld --shared-memory --no-entry --export-all -o %t.wasm %t.o
6-
7-
# CHECK: error: TLS symbols cannot yet be exported: `tls1`
2+
# RUN: wasm-ld -shared --experimental-pic -o %t.so %t.o
3+
# RUN: obj2yaml %t.so | FileCheck %s
84

95
.section .tdata.tls1,"",@
106
.globl tls1
@@ -24,3 +20,26 @@ tls1:
2420
.int8 43
2521
.int8 15
2622
.ascii "mutable-globals"
23+
24+
# CHECK: ExportInfo:
25+
# CHECK-NEXT: - Name: tls1
26+
# CHECK-NEXT: Flags: [ TLS ]
27+
# CHECK-NEXT: - Type: TYPE
28+
29+
# CHECK: - Type: GLOBAL
30+
# CHECK-NEXT: Globals:
31+
# CHECK-NEXT: - Index: 2
32+
# CHECK-NEXT: Type: I32
33+
# CHECK-NEXT: Mutable: false
34+
# CHECK-NEXT: InitExpr:
35+
# CHECK-NEXT: Opcode: I32_CONST
36+
# CHECK-NEXT: Value: 0
37+
38+
# CHECK: - Type: EXPORT
39+
# CHECK-NEXT: Exports:
40+
# CHECK-NEXT: - Name: __wasm_call_ctors
41+
# CHECK-NEXT: Kind: FUNCTION
42+
# CHECK-NEXT: Index: 0
43+
# CHECK-NEXT: - Name: tls1
44+
# CHECK-NEXT: Kind: GLOBAL
45+
# CHECK-NEXT: Index: 2

lld/test/wasm/tls-import.s

Lines changed: 0 additions & 23 deletions
This file was deleted.

lld/test/wasm/tls-non-shared-memory.s

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Test that linking without shared memory causes __tls_base to be
2-
# internalized
2+
# internalized.
33

44
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
55

@@ -13,14 +13,20 @@ get_tls1:
1313
i32.add
1414
end_function
1515

16+
.globl get_tls1_got
17+
get_tls1_got:
18+
.functype get_tls1_got () -> (i32)
19+
global.get tls1@GOT@TLS
20+
end_function
21+
1622
.section .data.no_tls,"",@
1723
.globl no_tls
1824
.p2align 2
1925
no_tls:
2026
.int32 42
2127
.size no_tls, 4
2228

23-
.section .tdata.tls1,"",@
29+
.section .tdata.tls1,"T",@
2430
.globl tls1
2531
.p2align 2
2632
tls1:
@@ -58,6 +64,13 @@ tls1:
5864
# CHECK-NEXT: InitExpr:
5965
# CHECK-NEXT: Opcode: I32_CONST
6066
# CHECK-NEXT: Value: 1024
67+
# GOT.data.internal.tls1
68+
# CHECK-NEXT: - Index: 2
69+
# CHECK-NEXT: Type: I32
70+
# CHECK-NEXT: Mutable: false
71+
# CHECK-NEXT: InitExpr:
72+
# CHECK-NEXT: Opcode: I32_CONST
73+
# CHECK-NEXT: Value: 1024
6174
# CHECK-NEXT: - Type: EXPORT
6275

6376
# CHECK: - Type: DATA

lld/wasm/Relocations.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ void scanRelocations(InputChunk *chunk) {
139139
}
140140

141141
if (config->isPic) {
142-
if (sym->isTLS() && sym->isUndefined()) {
143-
error(toString(file) +
144-
": TLS symbol is undefined, but TLS symbols cannot yet be "
145-
"imported: `" +
146-
toString(*sym) + "`");
147-
}
148142
switch (reloc.Type) {
149143
case R_WASM_TABLE_INDEX_SLEB:
150144
case R_WASM_TABLE_INDEX_SLEB64:

lld/wasm/Symbols.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ DefinedFunction *WasmSym::callDtors;
7474
DefinedFunction *WasmSym::initMemory;
7575
DefinedFunction *WasmSym::applyDataRelocs;
7676
DefinedFunction *WasmSym::applyGlobalRelocs;
77+
DefinedFunction *WasmSym::applyGlobalTLSRelocs;
7778
DefinedFunction *WasmSym::initTLS;
7879
DefinedFunction *WasmSym::startFunction;
7980
DefinedData *WasmSym::dsoHandle;

lld/wasm/Symbols.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,15 @@ struct WasmSym {
552552
static DefinedFunction *applyDataRelocs;
553553

554554
// __wasm_apply_global_relocs
555-
// Function that applies relocations to data segment post-instantiation.
555+
// Function that applies relocations to wasm globals post-instantiation.
556556
// Unlike __wasm_apply_data_relocs this needs to run on every thread.
557557
static DefinedFunction *applyGlobalRelocs;
558558

559+
// __wasm_apply_global_tls_relocs
560+
// Like applyGlobalRelocs but for globals that hold TLS addresess. These
561+
// must be delayed until __wasm_init_tls.
562+
static DefinedFunction *applyGlobalTLSRelocs;
563+
559564
// __wasm_init_tls
560565
// Function that allocates thread-local storage and initializes it.
561566
static DefinedFunction *initTLS;

0 commit comments

Comments
 (0)