Skip to content

Commit 4f4449a

Browse files
committed
Override static libraries and warn on linking against that kind of libs
1 parent ace40a9 commit 4f4449a

50 files changed

Lines changed: 149 additions & 87 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/rustc_metadata/src/errors.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,3 +704,18 @@ pub(crate) struct MitigationLessStrictInDependency {
704704
pub mitigation_level: String,
705705
pub extern_crate: Symbol,
706706
}
707+
708+
#[derive(Diagnostic)]
709+
pub(crate) enum StaticLinkingNotSupported<'a> {
710+
#[diag(
711+
"static linking of `{$lib_name}` is not supported on `{$target}`; using dynamic linking instead"
712+
)]
713+
#[help("remove `kind = \"static\"` and ensure a shared library is available")]
714+
UserRequested { lib_name: Symbol, target: &'a str },
715+
716+
#[diag(
717+
"library `{$lib_name}` is linked statically by a dependency, but `{$target}` requires dynamic linking; using dynamic linking instead"
718+
)]
719+
#[help("ensure a shared library is available")]
720+
FromDependency { lib_name: Symbol, target: &'a str },
721+
}

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,28 @@ pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> Vec<NativeLib>
192192
}
193193
}
194194
collector.process_command_line();
195+
for lib in &mut collector.libs {
196+
if tcx.sess.target.env == Env::Pauthtest {
197+
if let NativeLibKind::Static { .. } = lib.kind {
198+
if !tcx.sess.opts.unstable_opts.ui_testing {
199+
let diag = if lib.foreign_module.is_none() {
200+
errors::StaticLinkingNotSupported::UserRequested {
201+
lib_name: lib.name,
202+
target: tcx.sess.target.llvm_target.as_ref(),
203+
}
204+
} else {
205+
errors::StaticLinkingNotSupported::FromDependency {
206+
lib_name: lib.name,
207+
target: tcx.sess.target.llvm_target.as_ref(),
208+
}
209+
};
210+
tcx.dcx().emit_warn(diag);
211+
}
212+
213+
lib.kind = NativeLibKind::Dylib { as_needed: None };
214+
}
215+
}
216+
}
195217
collector.libs
196218
}
197219

src/doc/rustc/src/platform-support/aarch64-unknown-linux-pauthtest.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,9 @@ The following categories are supported (all present in tree):
391391
* End-to-end execution tests
392392
* Rust-driven quicksort (pauth-quicksort-rust-driver)
393393
* C-driven quicksort (pauth-quicksort-c-driver)
394-
* UI error reporting (the target does not support `+crt-static`)
394+
* UI error/warning reporting (the target does not support static linking)
395395
* crt-static-pauthtest.rs
396+
* pauth-static-link-warning
396397

397398
All tests from `assembly-llvm`, `codegen-llvm`, `codegen-units`, `coverage`,
398399
`crashes`, `incremental`, `library`, `mir-opt`, `run-make`, `ui` and
@@ -413,6 +414,7 @@ x.py test --target aarch64-unknown-linux-pauthtest --force-rerun assembly-llvm \
413414
tests/codegen-llvm/pauth/pauth-init-fini.rs \
414415
tests/run-make/pauth-quicksort-rust-driver \
415416
tests/run-make/pauth-quicksort-c-driver \
417+
tests/run-make/pauth-static-link-warning \
416418
tests/ui/statics/crt-static-pauthtest.rs
417419
```
418420

tests/incremental/auxiliary/issue-54059.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ proc_macro_expr_impl! {
4040
}
4141
}
4242

43-
#[cfg_attr(target_env = "pauthtest", link(name = "rust_test_helpers", kind = "dylib"))]
44-
#[cfg_attr(not(target_env = "pauthtest"), link(name = "rust_test_helpers"))]
43+
#[link(name = "rust_test_helpers", kind = "static")]
4544
extern "C" {
4645
pub fn rust_dbg_extern_identity_u64(v: u64) -> u64;
4746
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int helper_function() { return 42; }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[link(name = "helper", kind = "static")]
2+
extern "C" {
3+
fn helper_function() -> i32;
4+
}
5+
6+
fn main() {
7+
unsafe {
8+
assert!(42 == helper_function());
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extern "C" {
2+
fn helper_function() -> i32;
3+
}
4+
5+
fn main() {
6+
unsafe {
7+
assert!(42 == helper_function());
8+
}
9+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Make sure that for `aarch64-unknown-linux-pauthtest` compiler emits warning when static
2+
// libraries are linked. Test both foreign module linked from #[link] directive and command line
3+
// invocations.
4+
5+
//@ only-aarch64-unknown-linux-pauthtest
6+
// ignore-tidy-linelength
7+
8+
use run_make_support::{cc, env_var, regex, run, rustc};
9+
10+
fn main() {
11+
let input = "helper";
12+
let input_name = format!("{input}.c");
13+
let lib_name = format!("{}{input}.{}", "lib", "a");
14+
// Build a static library
15+
cc().out_exe(&lib_name)
16+
.input(&input_name)
17+
.args(&["-target", "aarch64-unknown-linux-pauthtest", "-march=armv8.3-a+pauth", "-c"])
18+
.run();
19+
20+
// Check against foreign module warning: #[link(name = "helper", kind = "static")]
21+
let stderr_foreign_module = rustc()
22+
.target("aarch64-unknown-linux-pauthtest")
23+
.input("main.rs")
24+
.linker(&env_var("CC"))
25+
.link_args(&env_var("CC_DEFAULT_FLAGS"))
26+
.arg("-L.")
27+
.run()
28+
.stderr_utf8();
29+
run("main");
30+
let re_foreign_moule = regex::Regex::new( r"(?s)warning: library `helper`.*linked statically.*aarch64-unknown-linux-pauthtest.*requires dynamic linking.*using dynamic linking instead").unwrap();
31+
assert!(re_foreign_moule.is_match(&stderr_foreign_module));
32+
33+
// Check against command line warning: -lstatic=helper
34+
let stderr_command_line = rustc()
35+
.target("aarch64-unknown-linux-pauthtest")
36+
.input("main_cmd_line.rs")
37+
.linker(&env_var("CC"))
38+
.link_args(&env_var("CC_DEFAULT_FLAGS"))
39+
.arg("-L.")
40+
.arg("-lstatic=helper")
41+
.run()
42+
.stderr_utf8();
43+
run("main_cmd_line");
44+
let re_cmd_line = regex::Regex::new( r"(?s)warning: static linking of `helper`.*is not supported on.*aarch64-unknown-linux-pauthtest.*using dynamic linking instead").unwrap();
45+
assert!(re_cmd_line.is_match(&stderr_command_line));
46+
}

tests/ui/abi/abi-sysv64-arg-passing.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ mod tests {
106106
Some(u64),
107107
}
108108

109-
#[cfg_attr(target_env = "pauthtest", link(name = "rust_test_helpers", kind = "dylib"))]
110-
#[cfg_attr(not(target_env = "pauthtest"), link(name = "rust_test_helpers", kind = "static"))]
109+
#[link(name = "rust_test_helpers", kind = "static")]
111110
extern "sysv64" {
112111
pub fn rust_int8_to_int32(_: i8) -> i32;
113112
pub fn rust_dbg_extern_identity_u8(v: u8) -> u8;

tests/ui/abi/anon-extern-mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ run-pass
22

3-
#[cfg_attr(target_env = "pauthtest", link(name = "rust_test_helpers", kind = "dylib"))]
4-
#[cfg_attr(not(target_env = "pauthtest"), link(name = "rust_test_helpers", kind = "static"))]
3+
#[link(name = "rust_test_helpers", kind = "static")]
54
extern "C" {
65
fn rust_get_test_int() -> isize;
76
}

0 commit comments

Comments
 (0)