Skip to content

Commit 53217ec

Browse files
committed
[lld][WebAssembly] Don't apply data relocations at static constructor time
Instead, export `__wasm_apply_data_relocs` and `__wasm_call_ctors` separately. This is required since user code in a shared library (such as static constructors) should not be run until relocations have been applied to all loaded libraries. See: emscripten-core/emscripten#17295 Differential Revision: https://reviews.llvm.org/D128515
1 parent 0103d4d commit 53217ec

File tree

9 files changed

+46
-44
lines changed

9 files changed

+46
-44
lines changed

lld/test/wasm/data-segments.ll

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
; Also test in combination with PIC/pie
3131
; RUN: wasm-ld --experimental-pic -pie -no-gc-sections --no-entry --shared-memory --max-memory=131072 %t.atomics.bulk-mem.pic.o -o %t.pic.wasm
3232
; RUN: obj2yaml %t.pic.wasm | FileCheck %s --check-prefixes PASSIVE-PIC,PASSIVE32-PIC
33-
; RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors,__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.pic.wasm | FileCheck %s --check-prefixes DIS,PIC-DIS -DPTR=i32
33+
; RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors,__wasm_apply_data_relocs,__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.pic.wasm | FileCheck %s --check-prefixes DIS,PIC-DIS -DPTR=i32
3434

3535
; Also test in combination with PIC/pie + wasm64
3636
; RUN: wasm-ld -mwasm64 --experimental-pic -pie -no-gc-sections --no-entry --shared-memory --max-memory=131072 %t.atomics.bulk-mem.pic-mem64.o -o %t.pic-mem64.wasm
3737
; RUN: obj2yaml %t.pic-mem64.wasm | FileCheck %s --check-prefixes PASSIVE-PIC,PASSIVE64-PIC
38-
; RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors,__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.pic-mem64.wasm | FileCheck %s --check-prefixes DIS,PIC-DIS -DPTR=i64
38+
; RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors,__wasm_apply_data_relocs,__wasm_init_memory --no-show-raw-insn --no-leading-addr %t.pic-mem64.wasm | FileCheck %s --check-prefixes DIS,PIC-DIS -DPTR=i64
3939

4040
@a = hidden global [6 x i8] c"hello\00", align 1
4141
@b = hidden global [8 x i8] c"goodbye\00", align 1
@@ -113,26 +113,26 @@
113113
; PASSIVE-NEXT: Name: __wasm_init_memory
114114

115115
; PASSIVE-PIC: - Type: START
116-
; PASSIVE-PIC-NEXT: StartFunction: 2
116+
; PASSIVE-PIC-NEXT: StartFunction: 3
117117
; PASSIVE-PIC-NEXT: - Type: DATACOUNT
118118
; PASSIVE-PIC-NEXT: Count: 3
119119
; PASSIVE-PIC-NEXT: - Type: CODE
120120
; PASSIVE-PIC-NEXT: Functions:
121121
; PASSIVE-PIC-NEXT: - Index: 0
122122
; PASSIVE-PIC-NEXT: Locals: []
123-
; PASSIVE-PIC-NEXT: Body: 10030B
123+
; PASSIVE-PIC-NEXT: Body: 0B
124124
; PASSIVE-PIC-NEXT: - Index: 1
125125
; PASSIVE-PIC-NEXT: Locals: []
126126
; PASSIVE-PIC-NEXT: Body: {{.*}}
127127
; PASSIVE-PIC-NEXT: - Index: 2
128+
; PASSIVE-PIC-NEXT: Locals: []
129+
; PASSIVE-PIC-NEXT: Body: 0B
130+
; PASSIVE-PIC-NEXT: - Index: 3
128131
; PASSIVE-PIC-NEXT: Locals:
129132
; PASSIVE32-PIC-NEXT: - Type: I32
130133
; PASSIVE64-PIC-NEXT: - Type: I64
131134
; PASSIVE-PIC-NEXT: Count: 2
132135
; PASSIVE-PIC-NEXT: Body: {{.*}}
133-
; PASSIVE-PIC-NEXT: - Index: 3
134-
; PASSIVE-PIC-NEXT: Locals: []
135-
; PASSIVE-PIC-NEXT: Body: 0B
136136
; PASSIVE-PIC-NEXT: - Type: DATA
137137
; PASSIVE-PIC-NEXT: Segments:
138138
; PASSIVE-PIC-NEXT: - SectionOffset: 3
@@ -152,18 +152,19 @@
152152
; PASSIVE-PIC-NEXT: - Index: 1
153153
; PASSIVE-PIC-NEXT: Name: __wasm_init_tls
154154
; PASSIVE-PIC-NEXT: - Index: 2
155-
; PASSIVE-PIC-NEXT: Name: __wasm_init_memory
156-
; PASSIVE-PIC-NEXT: - Index: 3
157155
; PASSIVE-PIC-NEXT: Name: __wasm_apply_data_relocs
156+
; PASSIVE-PIC-NEXT: - Index: 3
157+
; PASSIVE-PIC-NEXT: Name: __wasm_init_memory
158158

159-
; In PIC mode __wasm_call_ctors contains a call to __wasm_apply_data_relocs
160-
; In non-PIC mode __wasm_call_ctors is an emtpy function since there are
161159
; no data relocations.
162160
; DIS-LABEL: <__wasm_call_ctors>:
163161
; DIS-EMPTY:
164-
; PIC-DIS-NEXT: call 3
165162
; DIS-NEXT: end
166163

164+
; In PIC mode __wasm_apply_data_relocs is export seperatly to __wasm_call_ctors
165+
; PIC-DIS: <__wasm_apply_data_relocs>:
166+
; PIC-DIS-EMPTY:
167+
167168
; DIS-LABEL: <__wasm_init_memory>:
168169

169170
; PIC-DIS: .local [[PTR]]

lld/test/wasm/pie.ll

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; RUN: llc -relocation-model=pic -mattr=+mutable-globals -filetype=obj %s -o %t.o
22
; RUN: wasm-ld --no-gc-sections --experimental-pic -pie -o %t.wasm %t.o
33
; RUN: obj2yaml %t.wasm | FileCheck %s
4-
; RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DISASSEM
4+
; RUN: llvm-objdump --disassemble-symbols=__wasm_call_ctors,__wasm_apply_data_relocs --no-show-raw-insn --no-leading-addr %t.wasm | FileCheck %s --check-prefixes DISASSEM
55

66
target triple = "wasm32-unknown-emscripten"
77

@@ -91,11 +91,13 @@ declare void @external_func()
9191
; CHECK-NEXT: Name: _start
9292
; CHECK-NEXT: GlobalNames:
9393

94-
; DISASSEM: <__wasm_call_ctors>:
94+
; DISASSEM-LABEL: <__wasm_call_ctors>:
9595
; DISASSEM-EMPTY:
96-
; DISASSEM-NEXT: call 2
9796
; DISASSEM-NEXT: end
9897

98+
; DISASSEM-LABEL: <__wasm_apply_data_relocs>:
99+
; DISASSEM: end
100+
99101
; Run the same test with extended-const support. When this is available
100102
; we don't need __wasm_apply_global_relocs and instead rely on the add
101103
; instruction in the InitExpr. We also, therefore, do not need these globals
@@ -153,10 +155,10 @@ declare void @external_func()
153155
; SHMEM: - Type: START
154156
; SHMEM-NEXT: StartFunction: 6
155157

156-
; DISASSEM-SHMEM: <__wasm_start>:
158+
; DISASSEM-SHMEM-LABEL: <__wasm_start>:
157159
; DISASSEM-SHMEM-EMPTY:
158160
; DISASSEM-SHMEM-NEXT: call 5
159-
; DISASSEM-SHMEM-NEXT: call 3
161+
; DISASSEM-SHMEM-NEXT: call 4
160162
; DISASSEM-SHMEM-NEXT: end
161163

162164
; SHMEM: FunctionNames:
@@ -167,9 +169,9 @@ declare void @external_func()
167169
; SHMEM-NEXT: - Index: 2
168170
; SHMEM-NEXT: Name: __wasm_init_tls
169171
; SHMEM-NEXT: - Index: 3
170-
; SHMEM-NEXT: Name: __wasm_init_memory
171-
; SHMEM-NEXT: - Index: 4
172172
; SHMEM-NEXT: Name: __wasm_apply_data_relocs
173+
; SHMEM-NEXT: - Index: 4
174+
; SHMEM-NEXT: Name: __wasm_init_memory
173175
; SHMEM-NEXT: - Index: 5
174176
; SHMEM-NEXT: Name: __wasm_apply_global_relocs
175177
; SHMEM-NEXT: - Index: 6

lld/test/wasm/shared-weak-symbols.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ call_weak:
6262
# CHECK-NEXT: - Name: __wasm_call_ctors
6363
# CHECK-NEXT: Kind: FUNCTION
6464
# CHECK-NEXT: Index: 1
65+
# CHECK-NEXT: - Name: __wasm_apply_data_relocs
66+
# CHECK-NEXT: Kind: FUNCTION
67+
# CHECK-NEXT: Index: 2
6568
# CHECK-NEXT: - Name: weak_func
6669
# CHECK-NEXT: Kind: FUNCTION
6770
# CHECK-NEXT: Index: 3

lld/test/wasm/shared.s

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ get_local_func_address:
209209

210210
# DIS: <__wasm_call_ctors>:
211211
# DIS-EMPTY:
212-
# DIS-NEXT: call 1
213212
# DIS-NEXT: end
214213

215214
# DIS: <__wasm_apply_data_relocs>:

lld/test/wasm/shared64.s

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ get_local_func_address:
216216

217217
# DIS: <__wasm_call_ctors>:
218218
# DIS-EMPTY:
219-
# DIS-NEXT: call 1
220219
# DIS-NEXT: end
221220

222221
# DIS: <__wasm_apply_data_relocs>:

lld/test/wasm/tls-export.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ tls1:
4040
# CHECK-NEXT: - Name: __wasm_call_ctors
4141
# CHECK-NEXT: Kind: FUNCTION
4242
# CHECK-NEXT: Index: 0
43+
# CHECK-NEXT: - Name: __wasm_apply_data_relocs
44+
# CHECK-NEXT: Kind: FUNCTION
45+
# CHECK-NEXT: Index: 1
4346
# CHECK-NEXT: - Name: tls1
4447
# CHECK-NEXT: Kind: GLOBAL
4548
# CHECK-NEXT: Index: 2

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ tls1:
127127
# PIE-NEXT: - Name: memory
128128
# PIE-NEXT: Kind: MEMORY
129129
# PIE-NEXT: Index: 0
130+
# PIE-NEXT: - Name: __wasm_apply_data_relocs
131+
# PIE-NEXT: Kind: FUNCTION
132+
# PIE-NEXT: Index: 1
130133
# PIE-NEXT: - Type:
131134

132135
# .tdata and .data are combined into single segment in PIC mode.

lld/wasm/Driver.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,17 @@ static void createSyntheticSymbols() {
656656
is64 ? i64ArgSignature : i32ArgSignature,
657657
"__wasm_init_tls"));
658658
}
659+
660+
if (config->isPic ||
661+
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) {
662+
// For PIC code, or when dynamically importing addresses, we create
663+
// synthetic functions that apply relocations. These get called from
664+
// __wasm_call_ctors before the user-level constructors.
665+
WasmSym::applyDataRelocs = symtab->addSyntheticFunction(
666+
"__wasm_apply_data_relocs",
667+
WASM_SYMBOL_VISIBILITY_DEFAULT | WASM_SYMBOL_EXPORTED,
668+
make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs"));
669+
}
659670
}
660671

661672
static void createOptionalSymbols() {

lld/wasm/Writer.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,17 +1018,6 @@ void Writer::createSyntheticInitFunctions() {
10181018
WasmSym::tlsBase->markLive();
10191019
}
10201020

1021-
if (config->isPic ||
1022-
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) {
1023-
// For PIC code, or when dynamically importing addresses, we create
1024-
// synthetic functions that apply relocations. These get called from
1025-
// __wasm_call_ctors before the user-level constructors.
1026-
WasmSym::applyDataRelocs = symtab->addSyntheticFunction(
1027-
"__wasm_apply_data_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
1028-
make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs"));
1029-
WasmSym::applyDataRelocs->markLive();
1030-
}
1031-
10321021
if (config->isPic && out.globalSec->needsRelocations()) {
10331022
WasmSym::applyGlobalRelocs = symtab->addSyntheticFunction(
10341023
"__wasm_apply_global_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
@@ -1298,8 +1287,8 @@ void Writer::createStartFunction() {
12981287

12991288
// For -shared (PIC) output, we create create a synthetic function which will
13001289
// apply any relocations to the data segments on startup. This function is
1301-
// called `__wasm_apply_data_relocs` and is added at the beginning of
1302-
// `__wasm_call_ctors` before any of the constructors run.
1290+
// called `__wasm_apply_data_relocs` and is expected to be called before
1291+
// any user code (i.e. before `__wasm_call_ctors`).
13031292
void Writer::createApplyDataRelocationsFunction() {
13041293
LLVM_DEBUG(dbgs() << "createApplyDataRelocationsFunction\n");
13051294
// First write the body's contents to a string.
@@ -1352,11 +1341,9 @@ void Writer::createApplyGlobalTLSRelocationsFunction() {
13521341
// Create synthetic "__wasm_call_ctors" function based on ctor functions
13531342
// in input object.
13541343
void Writer::createCallCtorsFunction() {
1355-
// If __wasm_call_ctors isn't referenced, there aren't any ctors, and we
1356-
// aren't calling `__wasm_apply_data_relocs` for Emscripten-style PIC, don't
1344+
// If __wasm_call_ctors isn't referenced, there aren't any ctors, don't
13571345
// define the `__wasm_call_ctors` function.
1358-
if (!WasmSym::callCtors->isLive() && !WasmSym::applyDataRelocs &&
1359-
initFunctions.empty())
1346+
if (!WasmSym::callCtors->isLive() && initFunctions.empty())
13601347
return;
13611348

13621349
// First write the body's contents to a string.
@@ -1365,12 +1352,6 @@ void Writer::createCallCtorsFunction() {
13651352
raw_string_ostream os(bodyContent);
13661353
writeUleb128(os, 0, "num locals");
13671354

1368-
if (WasmSym::applyDataRelocs) {
1369-
writeU8(os, WASM_OPCODE_CALL, "CALL");
1370-
writeUleb128(os, WasmSym::applyDataRelocs->getFunctionIndex(),
1371-
"function index");
1372-
}
1373-
13741355
// Call constructors
13751356
for (const WasmInitEntry &f : initFunctions) {
13761357
writeU8(os, WASM_OPCODE_CALL, "CALL");

0 commit comments

Comments
 (0)