Skip to content

Commit f772459

Browse files
Do not strip the dylink.0 section from libdl.so (#1490)
The `dylink.0` section is required to correctly layout the main memory space and provide `__memory_base` and `__table_base` symbols. The section was being stripped by the `strip` tool, and it led to the unpredictable corruption of `LIBRARIES` global variable.
1 parent c85632b commit f772459

File tree

5 files changed

+108
-37
lines changed

5 files changed

+108
-37
lines changed

crates/wit-component/dl/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
CARGO_PROFILE_RELEASE_LTO=true RUSTFLAGS="-C relocation-model=pic" cargo build --release --target=wasm32-wasi
99
$WASI_SDK_PATH/bin/clang -shared -o $1 -Wl,--whole-archive ../../../target/wasm32-wasi/release/libdl.a -Wl,--no-whole-archive
10-
cargo run --manifest-path ../../../Cargo.toml -- strip -a $1 -o $1
10+
cargo run --manifest-path ../../../Cargo.toml -- strip $1 -o $1
11+
cargo run --manifest-path ../../../Cargo.toml -- strip --delete name $1 -o $1

crates/wit-component/libdl.so

29 Bytes
Binary file not shown.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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 1048704)
19+
(global (;7;) (mut i32) i32.const 1048864)
2020
(global (;8;) (mut i32) i32.const 1114112)
2121
(export "__stack_pointer" (global 0))
2222
(export "foo:memory_base" (global 1))
@@ -72,6 +72,10 @@
7272
(start $start)
7373
)
7474
(core module (;2;)
75+
(@dylink.0
76+
(mem-info (memory 152 2))
77+
(needed "libc.so")
78+
)
7579
(type (;0;) (func (param i32) (result i32)))
7680
(type (;1;) (func (param i32 i32 i32) (result i32)))
7781
(type (;2;) (func))

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

Lines changed: 96 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,57 @@
1616
(global (;4;) i32 i32.const 4)
1717
(global (;5;) i32 i32.const 1048688)
1818
(global (;6;) i32 i32.const 4)
19-
(global (;7;) (mut i32) i32.const 1048688)
20-
(global (;8;) (mut i32) i32.const 1114112)
19+
(global (;7;) i32 i32.const 1048840)
20+
(global (;8;) i32 i32.const 4)
21+
(global (;9;) (mut i32) i32.const 1048848)
22+
(global (;10;) (mut i32) i32.const 1114112)
2123
(export "__stack_pointer" (global 0))
2224
(export "foo:memory_base" (global 1))
2325
(export "foo:table_base" (global 2))
24-
(export "libdl.so:memory_base" (global 3))
25-
(export "libdl.so:table_base" (global 4))
26-
(export "wit-component:stubs:memory_base" (global 5))
27-
(export "wit-component:stubs:table_base" (global 6))
28-
(export "__heap_base" (global 7))
29-
(export "__heap_end" (global 8))
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))
3034
(export "__indirect_function_table" (table 0))
3135
(export "memory" (memory 0))
3236
(@producers
3337
(processed-by "wit-component" "$CARGO_PKG_VERSION")
3438
)
3539
)
3640
(core module (;1;)
41+
(@dylink.0
42+
(mem-info (memory 0 4))
43+
)
44+
(type (;0;) (func))
45+
(type (;1;) (func (param i32) (result i32)))
46+
(import "GOT.mem" "__heap_base" (global $__heap_base (;0;) (mut i32)))
47+
(import "GOT.mem" "__heap_end" (global $__heap_end (;1;) (mut i32)))
48+
(func $start (;0;) (type 0)
49+
global.get $__heap_base
50+
global.set $heap
51+
)
52+
(func $malloc (;1;) (type 1) (param i32) (result i32)
53+
global.get $heap
54+
global.get $heap
55+
local.get 0
56+
i32.add
57+
global.set $heap
58+
)
59+
(func $abort (;2;) (type 0)
60+
unreachable
61+
)
62+
(global $errno (;2;) i32 i32.const 0)
63+
(global $heap (;3;) (mut i32) i32.const 0)
64+
(export "malloc" (func $malloc))
65+
(export "abort" (func $abort))
66+
(export "errno" (global $errno))
67+
(start $start)
68+
)
69+
(core module (;2;)
3770
(type (;0;) (func (param i32 i32 i32) (result i32)))
3871
(type (;1;) (func (param i32) (result i32)))
3972
(func (;0;) (type 0) (param i32 i32 i32) (result i32)
@@ -48,7 +81,11 @@
4881
(processed-by "wit-component" "$CARGO_PKG_VERSION")
4982
)
5083
)
51-
(core module (;2;)
84+
(core module (;3;)
85+
(@dylink.0
86+
(mem-info (memory 152 2))
87+
(needed "libc.so")
88+
)
5289
(type (;0;) (func (param i32) (result i32)))
5390
(type (;1;) (func (param i32 i32 i32) (result i32)))
5491
(type (;2;) (func))
@@ -478,7 +515,7 @@
478515
(export "__wasm_set_libraries" (func 10))
479516
(data (;0;) (global.get 0) "invalid library handle\00library not found\00dlopen flags not yet supported\00symbol not found\00dlsym RTLD_NEXT and RTLD_DEFAULT not yet supported\00\00\00\00\00\00\00\00\00\00\00\00\00")
480517
)
481-
(core module (;3;)
518+
(core module (;4;)
482519
(@dylink.0
483520
(mem-info (memory 0 4))
484521
)
@@ -495,7 +532,7 @@
495532
(export "baz" (func $foo))
496533
(export "what" (global $what))
497534
)
498-
(core module (;4;)
535+
(core module (;5;)
499536
(type (;0;) (func))
500537
(type (;1;) (func (param i32)))
501538
(type (;2;) (func (param i32) (result i32)))
@@ -542,8 +579,8 @@
542579
(alias core export 0 "memory" (core memory (;1;)))
543580
(alias core export 0 "__indirect_function_table" (core table (;0;)))
544581
(alias core export 0 "__stack_pointer" (core global (;2;)))
545-
(alias core export 0 "wit-component:stubs:memory_base" (core global (;3;)))
546-
(alias core export 0 "wit-component:stubs:table_base" (core global (;4;)))
582+
(alias core export 0 "libc.so:memory_base" (core global (;3;)))
583+
(alias core export 0 "libc.so:table_base" (core global (;4;)))
547584
(core instance (;3;)
548585
(export "memory" (memory 1))
549586
(export "__indirect_function_table" (table 0))
@@ -567,18 +604,14 @@
567604
(alias core export 0 "memory" (core memory (;2;)))
568605
(alias core export 0 "__indirect_function_table" (core table (;1;)))
569606
(alias core export 0 "__stack_pointer" (core global (;7;)))
570-
(alias core export 0 "libdl.so:memory_base" (core global (;8;)))
571-
(alias core export 0 "libdl.so:table_base" (core global (;9;)))
572-
(alias core export 4 "memcmp" (core func (;0;)))
573-
(alias core export 4 "strlen" (core func (;1;)))
607+
(alias core export 0 "wit-component:stubs:memory_base" (core global (;8;)))
608+
(alias core export 0 "wit-component:stubs:table_base" (core global (;9;)))
574609
(core instance (;7;)
575610
(export "memory" (memory 2))
576611
(export "__indirect_function_table" (table 1))
577612
(export "__stack_pointer" (global 7))
578613
(export "__memory_base" (global 8))
579614
(export "__table_base" (global 9))
580-
(export "memcmp" (func 0))
581-
(export "strlen" (func 1))
582615
)
583616
(core instance (;8;) (instantiate 2
584617
(with "GOT.mem" (instance 5))
@@ -596,38 +629,68 @@
596629
(alias core export 0 "memory" (core memory (;3;)))
597630
(alias core export 0 "__indirect_function_table" (core table (;2;)))
598631
(alias core export 0 "__stack_pointer" (core global (;12;)))
599-
(alias core export 0 "foo:memory_base" (core global (;13;)))
600-
(alias core export 0 "foo:table_base" (core global (;14;)))
601-
(alias core export 8 "dlopen" (core func (;2;)))
632+
(alias core export 0 "libdl.so:memory_base" (core global (;13;)))
633+
(alias core export 0 "libdl.so:table_base" (core global (;14;)))
634+
(alias core export 8 "memcmp" (core func (;0;)))
635+
(alias core export 8 "strlen" (core func (;1;)))
602636
(core instance (;11;)
603637
(export "memory" (memory 3))
604638
(export "__indirect_function_table" (table 2))
605639
(export "__stack_pointer" (global 12))
606640
(export "__memory_base" (global 13))
607641
(export "__table_base" (global 14))
642+
(export "memcmp" (func 0))
643+
(export "strlen" (func 1))
644+
)
645+
(core instance (;12;) (instantiate 3
646+
(with "GOT.mem" (instance 9))
647+
(with "GOT.func" (instance 10))
648+
(with "env" (instance 11))
649+
)
650+
)
651+
(alias core export 0 "__heap_base" (core global (;15;)))
652+
(alias core export 0 "__heap_end" (core global (;16;)))
653+
(core instance (;13;)
654+
(export "__heap_base" (global 15))
655+
(export "__heap_end" (global 16))
656+
)
657+
(core instance (;14;))
658+
(alias core export 0 "memory" (core memory (;4;)))
659+
(alias core export 0 "__indirect_function_table" (core table (;3;)))
660+
(alias core export 0 "__stack_pointer" (core global (;17;)))
661+
(alias core export 0 "foo:memory_base" (core global (;18;)))
662+
(alias core export 0 "foo:table_base" (core global (;19;)))
663+
(alias core export 12 "dlopen" (core func (;2;)))
664+
(core instance (;15;)
665+
(export "memory" (memory 4))
666+
(export "__indirect_function_table" (table 3))
667+
(export "__stack_pointer" (global 17))
668+
(export "__memory_base" (global 18))
669+
(export "__table_base" (global 19))
608670
(export "dlopen" (func 2))
609671
)
610672
(alias export 0 "foo" (func (;0;)))
611673
(core func (;3;) (canon lower (func 0)))
612-
(core instance (;12;)
674+
(core instance (;16;)
613675
(export "foo" (func 3))
614676
)
615-
(core instance (;13;) (instantiate 3
616-
(with "GOT.mem" (instance 9))
617-
(with "GOT.func" (instance 10))
618-
(with "env" (instance 11))
619-
(with "test:test/test" (instance 12))
677+
(core instance (;17;) (instantiate 4
678+
(with "GOT.mem" (instance 13))
679+
(with "GOT.func" (instance 14))
680+
(with "env" (instance 15))
681+
(with "test:test/test" (instance 16))
620682
)
621683
)
622-
(core instance (;14;) (instantiate 4
684+
(core instance (;18;) (instantiate 5
623685
(with "env" (instance 0))
624-
(with "foo" (instance 13))
625-
(with "libdl.so" (instance 8))
626-
(with "wit-component:stubs" (instance 4))
686+
(with "foo" (instance 17))
687+
(with "libc.so" (instance 4))
688+
(with "libdl.so" (instance 12))
689+
(with "wit-component:stubs" (instance 8))
627690
)
628691
)
629692
(type (;1;) (func (param "v" s32) (result s32)))
630-
(alias core export 13 "test:test/test#foo" (core func (;4;)))
693+
(alias core export 17 "test:test/test#foo" (core func (;4;)))
631694
(func (;1;) (type 1) (canon lift (core func 4)))
632695
(component (;0;)
633696
(type (;0;) (func (param "v" s32) (result s32)))

src/bin/wasm-tools/strip.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ impl Opts {
4646
return to_delete.is_match(name);
4747
}
4848

49-
// Finally default strip everything but the `name` and any `component-type` sections.
50-
name != "name" && !name.starts_with("component-type:")
49+
// Finally default strip everything but:
50+
// * the `name` section
51+
// * any `component-type` sections
52+
// * the `dylink.0` section
53+
name != "name" && !name.starts_with("component-type:") && name != "dylink.0"
5154
};
5255

5356
let mut output = Vec::new();

0 commit comments

Comments
 (0)