Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 234bc21

Browse files
madsmtmdianqk
authored andcommitted
Add a test case for #[used] with archive
1 parent 1805b33 commit 234bc21

File tree

5 files changed

+70
-6
lines changed

5 files changed

+70
-6
lines changed

tests/run-make/include-all-symbols-linking/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod foo {
2-
#[link_section = ".rodata.STATIC"]
2+
#[cfg_attr(target_os = "linux", link_section = ".rodata.STATIC")]
3+
#[cfg_attr(target_vendor = "apple", link_section = "__DATA,STATIC")]
34
#[used]
45
static STATIC: [u32; 10] = [1; 10];
56
}

tests/run-make/include-all-symbols-linking/rmake.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77
// See https://github.com/rust-lang/rust/pull/95604
88
// See https://github.com/rust-lang/rust/issues/47384
99

10-
//@ only-linux
11-
// Reason: differences in object file formats on OSX and Windows
12-
// causes errors in the llvm_objdump step
10+
//@ ignore-wasm differences in object file formats causes errors in the llvm_objdump step.
11+
//@ ignore-windows differences in object file formats causes errors in the llvm_objdump step.
1312

14-
use run_make_support::{dynamic_lib_name, llvm_objdump, llvm_readobj, rustc};
13+
use run_make_support::{dynamic_lib_name, llvm_objdump, llvm_readobj, rustc, target};
1514

1615
fn main() {
1716
rustc().crate_type("lib").input("lib.rs").run();
18-
rustc().crate_type("cdylib").link_args("-Tlinker.ld").input("main.rs").run();
17+
let mut main = rustc();
18+
main.crate_type("cdylib");
19+
if target().contains("linux") {
20+
main.link_args("-Tlinker.ld");
21+
}
22+
main.input("main.rs").run();
23+
1924
// Ensure `#[used]` and `KEEP`-ed section is there
2025
llvm_objdump()
2126
.arg("--full-contents")
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//! Add a constructor that runs pre-main, similar to what the `ctor` crate does.
2+
//!
3+
//! #[ctor]
4+
//! fn constructor() {
5+
//! printf(c"constructor\n");
6+
//! }
7+
8+
//@ edition:2021
9+
//@ no-prefer-dynamic explicitly test with crates that are built as an archive
10+
#![crate_type = "rlib"]
11+
12+
#[cfg_attr(
13+
any(
14+
target_os = "linux",
15+
target_os = "android",
16+
target_os = "freebsd",
17+
target_os = "netbsd",
18+
target_os = "openbsd",
19+
target_os = "dragonfly",
20+
target_os = "illumos",
21+
target_os = "haiku"
22+
),
23+
link_section = ".init_array"
24+
)]
25+
#[cfg_attr(target_vendor = "apple", link_section = "__DATA,__mod_init_func,mod_init_funcs")]
26+
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
27+
#[used]
28+
static CONSTRUCTOR: extern "C" fn() = constructor;
29+
30+
#[cfg_attr(any(target_os = "linux", target_os = "android"), link_section = ".text.startup")]
31+
extern "C" fn constructor() {
32+
extern "C" {
33+
fn printf(format: *const std::ffi::c_char, ...) -> std::ffi::c_int;
34+
}
35+
unsafe { printf(c"constructor\n".as_ptr()) };
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Ensure that `#[used]` in archives are correctly registered.
2+
//!
3+
//! Regression test for https://github.com/rust-lang/rust/issues/133491.
4+
5+
//@ edition:2021
6+
//@ run-pass
7+
//@ check-run-results
8+
//@ aux-build: used_pre_main_constructor.rs
9+
10+
//@ ignore-wasm ctor doesn't work on WASM
11+
12+
// Make sure `rustc` links the archive, but intentionally do not import/use any items.
13+
extern crate used_pre_main_constructor as _;
14+
15+
fn main() {
16+
extern "C" {
17+
fn printf(format: *const std::ffi::c_char, ...) -> std::ffi::c_int;
18+
}
19+
unsafe { printf(c"main\n".as_ptr()) };
20+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
constructor
2+
main

0 commit comments

Comments
 (0)