Skip to content

Commit c771916

Browse files
committed
Add test for garbage collection of encapsulation symbols
1 parent 81d8edc commit c771916

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This test checks that encapsulation symbols are not garbage collected by the linker.
2+
// LLD will remove them by default, so this test checks that we pass `-znostart-stop-gc` to LLD
3+
// to avoid that behavior. Without that flag, the test should fail.
4+
// This test is inspired by the behavior of the linkme crate.
5+
//
6+
//@ build-pass
7+
//@ only-x86_64
8+
9+
unsafe extern "Rust" {
10+
// The __start_ section name is magical for the linker,
11+
// It will put link sections named EXTERNFNS after it.
12+
#[link_name = "__start_EXTERNFNS"]
13+
static SECTION_START: fn();
14+
}
15+
16+
#[used]
17+
#[unsafe(link_section = "EXTERNFNS")]
18+
static EXTERN_FN_LOCAL: fn() = extern_fn;
19+
20+
fn extern_fn() {}
21+
22+
fn main() {
23+
// We need to reference the SECTION_START symbol to avoid it being garbage collected
24+
let slice = unsafe { SECTION_START };
25+
}

0 commit comments

Comments
 (0)