Skip to content

Commit e2bb0a2

Browse files
authored
provide __stack_high and __stack_low globals when linking (#2375)
* provide `__stack_high` and `__stack_low` globals when linking Beginning with WASI-SDK 28, `libc.so` requires these globals to be provided by the main module, presumably due to [these changes](WebAssembly/wasi-libc@abe7b08#diff-d570c8ef878addffd443fcdd7163ff830895658a0f43d08667b3545d7b323def). Signed-off-by: Joel Dice <[email protected]> * set `__stack_high` to the top of the stack allocation When static linking, `wasm-ld` takes care of allocating the stack, but when "dynamic" linking with `wit-component`, it's our job to do so and set the `__stack_high` and `__stack_low` variables accordingly. Signed-off-by: Joel Dice <[email protected]> --------- Signed-off-by: Joel Dice <[email protected]>
1 parent 1578635 commit e2bb0a2

File tree

26 files changed

+495
-215
lines changed

26 files changed

+495
-215
lines changed

crates/wit-component/src/linking.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ fn make_env_module<'a>(
369369
add_global_export("__asyncify_data", 0, true);
370370
}
371371

372+
// The libc.so in WASI-SDK 28+ requires these:
373+
add_global_export("__stack_high", stack_size_bytes, true);
374+
add_global_export("__stack_low", 0, true);
375+
372376
for metadata in metadata {
373377
memory_offset = align(memory_offset, 1 << metadata.mem_info.memory_alignment);
374378
table_offset = align(table_offset, 1 << metadata.mem_info.table_alignment);
@@ -1641,12 +1645,16 @@ impl Linker {
16411645
.iter()
16421646
.copied()
16431647
.map(global_item)
1644-
.chain(["__heap_base", "__heap_end"].into_iter().map(|name| Item {
1645-
alias: name.into(),
1646-
kind: ExportKind::Global,
1647-
which: MainOrAdapter::Main,
1648-
name: name.into(),
1649-
}))
1648+
.chain(
1649+
["__heap_base", "__heap_end", "__stack_high", "__stack_low"]
1650+
.into_iter()
1651+
.map(|name| Item {
1652+
alias: name.into(),
1653+
kind: ExportKind::Global,
1654+
which: MainOrAdapter::Main,
1655+
name: name.into(),
1656+
}),
1657+
)
16501658
.collect();
16511659

16521660
let func_items = metadata

crates/wit-component/src/linking/metadata.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ impl<'a> Metadata<'a> {
389389
}) = import.ty
390390
{
391391
match name {
392-
"__heap_base" | "__heap_end" => (),
392+
"__heap_base" | "__heap_end" | "__stack_high"
393+
| "__stack_low" => (),
393394
_ => {
394395
result.memory_address_imports.insert(name);
395396
}

crates/wit-component/tests/components/link-asyncify/component.wat

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@
55
(global (;0;) (mut i32) i32.const 1048576)
66
(global (;1;) (mut i32) i32.const 0)
77
(global (;2;) (mut i32) i32.const 0)
8-
(global (;3;) i32 i32.const 1048592)
9-
(global (;4;) i32 i32.const 1)
10-
(global (;5;) i32 i32.const 1048608)
8+
(global (;3;) (mut i32) i32.const 1048576)
9+
(global (;4;) (mut i32) i32.const 0)
10+
(global (;5;) i32 i32.const 1048592)
1111
(global (;6;) i32 i32.const 1)
12-
(global (;7;) (mut i32) i32.const 1048624)
13-
(global (;8;) (mut i32) i32.const 1114112)
12+
(global (;7;) i32 i32.const 1048608)
13+
(global (;8;) i32 i32.const 1)
14+
(global (;9;) (mut i32) i32.const 1048624)
15+
(global (;10;) (mut i32) i32.const 1114112)
1416
(export "__stack_pointer" (global 0))
1517
(export "__asyncify_state" (global 1))
1618
(export "__asyncify_data" (global 2))
17-
(export "bar:memory_base" (global 3))
18-
(export "bar:table_base" (global 4))
19-
(export "foo:memory_base" (global 5))
20-
(export "foo:table_base" (global 6))
21-
(export "__heap_base" (global 7))
22-
(export "__heap_end" (global 8))
19+
(export "__stack_high" (global 3))
20+
(export "__stack_low" (global 4))
21+
(export "bar:memory_base" (global 5))
22+
(export "bar:table_base" (global 6))
23+
(export "foo:memory_base" (global 7))
24+
(export "foo:table_base" (global 8))
25+
(export "__heap_base" (global 9))
26+
(export "__heap_end" (global 10))
2327
(export "__indirect_function_table" (table 0))
2428
(export "memory" (memory 0))
2529
(@producers

crates/wit-component/tests/components/link-bare-funcs/component.wat

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,24 @@
99
(table (;0;) 1 funcref)
1010
(memory (;0;) 17)
1111
(global (;0;) (mut i32) i32.const 1048576)
12-
(global (;1;) i32 i32.const 1048592)
13-
(global (;2;) i32 i32.const 1)
12+
(global (;1;) (mut i32) i32.const 1048576)
13+
(global (;2;) (mut i32) i32.const 0)
1414
(global (;3;) i32 i32.const 1048592)
1515
(global (;4;) i32 i32.const 1)
16-
(global (;5;) (mut i32) i32.const 1048608)
17-
(global (;6;) (mut i32) i32.const 1114112)
16+
(global (;5;) i32 i32.const 1048592)
17+
(global (;6;) i32 i32.const 1)
18+
(global (;7;) (mut i32) i32.const 1048608)
19+
(global (;8;) (mut i32) i32.const 1114112)
1820
(export "cabi_realloc" (func 0))
1921
(export "__stack_pointer" (global 0))
20-
(export "c:memory_base" (global 1))
21-
(export "c:table_base" (global 2))
22-
(export "foo:memory_base" (global 3))
23-
(export "foo:table_base" (global 4))
24-
(export "__heap_base" (global 5))
25-
(export "__heap_end" (global 6))
22+
(export "__stack_high" (global 1))
23+
(export "__stack_low" (global 2))
24+
(export "c:memory_base" (global 3))
25+
(export "c:table_base" (global 4))
26+
(export "foo:memory_base" (global 5))
27+
(export "foo:table_base" (global 6))
28+
(export "__heap_base" (global 7))
29+
(export "__heap_end" (global 8))
2630
(export "__indirect_function_table" (table 0))
2731
(export "memory" (memory 0))
2832
(@producers

crates/wit-component/tests/components/link-circular/component.wat

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@
1111
(table (;0;) 2 funcref)
1212
(memory (;0;) 17)
1313
(global (;0;) (mut i32) i32.const 1048576)
14-
(global (;1;) i32 i32.const 1048592)
15-
(global (;2;) i32 i32.const 1)
14+
(global (;1;) (mut i32) i32.const 1048576)
15+
(global (;2;) (mut i32) i32.const 0)
1616
(global (;3;) i32 i32.const 1048592)
1717
(global (;4;) i32 i32.const 1)
18-
(global (;5;) (mut i32) i32.const 1048592)
19-
(global (;6;) (mut i32) i32.const 1114112)
18+
(global (;5;) i32 i32.const 1048592)
19+
(global (;6;) i32 i32.const 1)
20+
(global (;7;) (mut i32) i32.const 1048592)
21+
(global (;8;) (mut i32) i32.const 1114112)
2022
(export "__stack_pointer" (global 0))
21-
(export "bar:memory_base" (global 1))
22-
(export "bar:table_base" (global 2))
23-
(export "foo:memory_base" (global 3))
24-
(export "foo:table_base" (global 4))
25-
(export "__heap_base" (global 5))
26-
(export "__heap_end" (global 6))
23+
(export "__stack_high" (global 1))
24+
(export "__stack_low" (global 2))
25+
(export "bar:memory_base" (global 3))
26+
(export "bar:table_base" (global 4))
27+
(export "foo:memory_base" (global 5))
28+
(export "foo:table_base" (global 6))
29+
(export "__heap_base" (global 7))
30+
(export "__heap_end" (global 8))
2731
(export "foo" (func 0))
2832
(export "__indirect_function_table" (table 0))
2933
(export "memory" (memory 0))

crates/wit-component/tests/components/link-dl-openable-builtin-libdl-with-unused/component.wat

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@
1010
(table (;0;) 4 funcref)
1111
(memory (;0;) 17)
1212
(global (;0;) (mut i32) i32.const 1048576)
13-
(global (;1;) i32 i32.const 1048704)
14-
(global (;2;) i32 i32.const 4)
13+
(global (;1;) (mut i32) i32.const 1048576)
14+
(global (;2;) (mut i32) i32.const 0)
1515
(global (;3;) i32 i32.const 1048704)
1616
(global (;4;) i32 i32.const 4)
1717
(global (;5;) i32 i32.const 1048704)
1818
(global (;6;) i32 i32.const 4)
19-
(global (;7;) (mut i32) i32.const 1049120)
20-
(global (;8;) (mut i32) i32.const 1114112)
19+
(global (;7;) i32 i32.const 1048704)
20+
(global (;8;) i32 i32.const 4)
21+
(global (;9;) (mut i32) i32.const 1049120)
22+
(global (;10;) (mut i32) i32.const 1114112)
2123
(export "__stack_pointer" (global 0))
22-
(export "foo:memory_base" (global 1))
23-
(export "foo:table_base" (global 2))
24-
(export "libc.so:memory_base" (global 3))
25-
(export "libc.so:table_base" (global 4))
26-
(export "libdl.so:memory_base" (global 5))
27-
(export "libdl.so:table_base" (global 6))
28-
(export "__heap_base" (global 7))
29-
(export "__heap_end" (global 8))
24+
(export "__stack_high" (global 1))
25+
(export "__stack_low" (global 2))
26+
(export "foo:memory_base" (global 3))
27+
(export "foo:table_base" (global 4))
28+
(export "libc.so:memory_base" (global 5))
29+
(export "libc.so:table_base" (global 6))
30+
(export "libdl.so:memory_base" (global 7))
31+
(export "libdl.so:table_base" (global 8))
32+
(export "__heap_base" (global 9))
33+
(export "__heap_end" (global 10))
3034
(export "__indirect_function_table" (table 0))
3135
(export "memory" (memory 0))
3236
(@producers

crates/wit-component/tests/components/link-dl-openable-builtin-libdl/component.wat

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,31 @@
1010
(table (;0;) 4 funcref)
1111
(memory (;0;) 17)
1212
(global (;0;) (mut i32) i32.const 1048576)
13-
(global (;1;) i32 i32.const 1048688)
14-
(global (;2;) i32 i32.const 4)
13+
(global (;1;) (mut i32) i32.const 1048576)
14+
(global (;2;) (mut i32) i32.const 0)
1515
(global (;3;) i32 i32.const 1048688)
1616
(global (;4;) i32 i32.const 4)
1717
(global (;5;) i32 i32.const 1048688)
1818
(global (;6;) i32 i32.const 4)
19-
(global (;7;) i32 i32.const 1049096)
19+
(global (;7;) i32 i32.const 1048688)
2020
(global (;8;) i32 i32.const 4)
21-
(global (;9;) (mut i32) i32.const 1049104)
22-
(global (;10;) (mut i32) i32.const 1114112)
21+
(global (;9;) i32 i32.const 1049096)
22+
(global (;10;) i32 i32.const 4)
23+
(global (;11;) (mut i32) i32.const 1049104)
24+
(global (;12;) (mut i32) i32.const 1114112)
2325
(export "__stack_pointer" (global 0))
24-
(export "foo:memory_base" (global 1))
25-
(export "foo:table_base" (global 2))
26-
(export "libc.so:memory_base" (global 3))
27-
(export "libc.so:table_base" (global 4))
28-
(export "libdl.so:memory_base" (global 5))
29-
(export "libdl.so:table_base" (global 6))
30-
(export "wit-component:stubs:memory_base" (global 7))
31-
(export "wit-component:stubs:table_base" (global 8))
32-
(export "__heap_base" (global 9))
33-
(export "__heap_end" (global 10))
26+
(export "__stack_high" (global 1))
27+
(export "__stack_low" (global 2))
28+
(export "foo:memory_base" (global 3))
29+
(export "foo:table_base" (global 4))
30+
(export "libc.so:memory_base" (global 5))
31+
(export "libc.so:table_base" (global 6))
32+
(export "libdl.so:memory_base" (global 7))
33+
(export "libdl.so:table_base" (global 8))
34+
(export "wit-component:stubs:memory_base" (global 9))
35+
(export "wit-component:stubs:table_base" (global 10))
36+
(export "__heap_base" (global 11))
37+
(export "__heap_end" (global 12))
3438
(export "__indirect_function_table" (table 0))
3539
(export "memory" (memory 0))
3640
(@producers

crates/wit-component/tests/components/link-dl-openable/component.wat

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
(table (;0;) 4 funcref)
1111
(memory (;0;) 17)
1212
(global (;0;) (mut i32) i32.const 1048576)
13-
(global (;1;) i32 i32.const 1048688)
14-
(global (;2;) i32 i32.const 4)
15-
(global (;3;) (mut i32) i32.const 1048688)
16-
(global (;4;) (mut i32) i32.const 1114112)
13+
(global (;1;) (mut i32) i32.const 1048576)
14+
(global (;2;) (mut i32) i32.const 0)
15+
(global (;3;) i32 i32.const 1048688)
16+
(global (;4;) i32 i32.const 4)
17+
(global (;5;) (mut i32) i32.const 1048688)
18+
(global (;6;) (mut i32) i32.const 1114112)
1719
(export "__stack_pointer" (global 0))
18-
(export "foo:memory_base" (global 1))
19-
(export "foo:table_base" (global 2))
20-
(export "__heap_base" (global 3))
21-
(export "__heap_end" (global 4))
20+
(export "__stack_high" (global 1))
21+
(export "__stack_low" (global 2))
22+
(export "foo:memory_base" (global 3))
23+
(export "foo:table_base" (global 4))
24+
(export "__heap_base" (global 5))
25+
(export "__heap_end" (global 6))
2226
(export "__indirect_function_table" (table 0))
2327
(export "memory" (memory 0))
2428
(@producers

crates/wit-component/tests/components/link-duplicate-symbols/component.wat

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,23 @@
1111
(table (;0;) 2 funcref)
1212
(memory (;0;) 17)
1313
(global (;0;) (mut i32) i32.const 1048576)
14-
(global (;1;) i32 i32.const 1048592)
15-
(global (;2;) i32 i32.const 1)
14+
(global (;1;) (mut i32) i32.const 1048576)
15+
(global (;2;) (mut i32) i32.const 0)
1616
(global (;3;) i32 i32.const 1048592)
1717
(global (;4;) i32 i32.const 1)
18-
(global (;5;) (mut i32) i32.const 1048592)
19-
(global (;6;) (mut i32) i32.const 1114112)
18+
(global (;5;) i32 i32.const 1048592)
19+
(global (;6;) i32 i32.const 1)
20+
(global (;7;) (mut i32) i32.const 1048592)
21+
(global (;8;) (mut i32) i32.const 1114112)
2022
(export "__stack_pointer" (global 0))
21-
(export "bar:memory_base" (global 1))
22-
(export "bar:table_base" (global 2))
23-
(export "foo:memory_base" (global 3))
24-
(export "foo:table_base" (global 4))
25-
(export "__heap_base" (global 5))
26-
(export "__heap_end" (global 6))
23+
(export "__stack_high" (global 1))
24+
(export "__stack_low" (global 2))
25+
(export "bar:memory_base" (global 3))
26+
(export "bar:table_base" (global 4))
27+
(export "foo:memory_base" (global 5))
28+
(export "foo:table_base" (global 6))
29+
(export "__heap_base" (global 7))
30+
(export "__heap_end" (global 8))
2731
(export "foo" (func 0))
2832
(export "__indirect_function_table" (table 0))
2933
(export "memory" (memory 0))

crates/wit-component/tests/components/link-got-func/component.wat

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@
1111
(table (;0;) 2 funcref)
1212
(memory (;0;) 17)
1313
(global (;0;) (mut i32) i32.const 1048576)
14-
(global (;1;) i32 i32.const 1048592)
15-
(global (;2;) i32 i32.const 1)
14+
(global (;1;) (mut i32) i32.const 1048576)
15+
(global (;2;) (mut i32) i32.const 0)
1616
(global (;3;) i32 i32.const 1048592)
1717
(global (;4;) i32 i32.const 1)
18-
(global (;5;) (mut i32) i32.const 1)
19-
(global (;6;) (mut i32) i32.const 1048592)
20-
(global (;7;) (mut i32) i32.const 1114112)
18+
(global (;5;) i32 i32.const 1048592)
19+
(global (;6;) i32 i32.const 1)
20+
(global (;7;) (mut i32) i32.const 1)
21+
(global (;8;) (mut i32) i32.const 1048592)
22+
(global (;9;) (mut i32) i32.const 1114112)
2123
(export "__stack_pointer" (global 0))
22-
(export "bar:memory_base" (global 1))
23-
(export "bar:table_base" (global 2))
24-
(export "foo:memory_base" (global 3))
25-
(export "foo:table_base" (global 4))
26-
(export "foo:foo" (global 5))
27-
(export "__heap_base" (global 6))
28-
(export "__heap_end" (global 7))
24+
(export "__stack_high" (global 1))
25+
(export "__stack_low" (global 2))
26+
(export "bar:memory_base" (global 3))
27+
(export "bar:table_base" (global 4))
28+
(export "foo:memory_base" (global 5))
29+
(export "foo:table_base" (global 6))
30+
(export "foo:foo" (global 7))
31+
(export "__heap_base" (global 8))
32+
(export "__heap_end" (global 9))
2933
(export "foo" (func 0))
3034
(export "__indirect_function_table" (table 0))
3135
(export "memory" (memory 0))

0 commit comments

Comments
 (0)