From 2a9d1ed538546483702622a22132f75e24f34ab9 Mon Sep 17 00:00:00 2001 From: Chris Wailes Date: Tue, 17 Oct 2023 16:18:59 -0700 Subject: [PATCH 1/3] Add `-Z external-sanitizer-runtime` This adds the unstable `-Z external-sanitizer-runtime` flag that will prevent rustc from emitting linker paths for the in-tree LLVM sanitizer runtime library. --- compiler/rustc_codegen_ssa/src/back/link.rs | 1 + compiler/rustc_session/src/options.rs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index e70cc9b621616..a37663b59724a 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1206,6 +1206,7 @@ fn add_sanitizer_libraries( // Everywhere else the runtimes are currently distributed as static // libraries which should be linked to executables only. let needs_runtime = !sess.target.is_like_android + && !sess.opts.unstable_opts.external_clangrt && match crate_type { CrateType::Executable => true, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => { diff --git a/compiler/rustc_session/src/options.rs b/compiler/rustc_session/src/options.rs index 2f018fbaa8679..bf7f047af5bdc 100644 --- a/compiler/rustc_session/src/options.rs +++ b/compiler/rustc_session/src/options.rs @@ -1645,6 +1645,8 @@ options! { "emit the bc module with thin LTO info (default: yes)"), export_executable_symbols: bool = (false, parse_bool, [TRACKED], "export symbols from executables, as if they were dynamic libraries"), + external_clangrt: bool = (false, parse_bool, [UNTRACKED], + "rely on user specified linker commands to find clangrt"), extra_const_ub_checks: bool = (false, parse_bool, [TRACKED], "turns on more checks to detect const UB, which can be slow (default: no)"), #[rustc_lint_opt_deny_field_access("use `Session::fewer_names` instead of this field")] From bf2858a05f510b8a0317d25d7dc587afbd16acfc Mon Sep 17 00:00:00 2001 From: Chris Wailes Date: Wed, 13 Mar 2024 11:13:25 -0700 Subject: [PATCH 2/3] Split a complex conditional into separate statements --- compiler/rustc_codegen_ssa/src/back/link.rs | 36 +++++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index a37663b59724a..0491b3a26953f 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1201,21 +1201,29 @@ fn add_sanitizer_libraries( crate_type: CrateType, linker: &mut dyn Linker, ) { - // On macOS and Windows using MSVC the runtimes are distributed as dylibs - // which should be linked to both executables and dynamic libraries. - // Everywhere else the runtimes are currently distributed as static - // libraries which should be linked to executables only. - let needs_runtime = !sess.target.is_like_android - && !sess.opts.unstable_opts.external_clangrt - && match crate_type { - CrateType::Executable => true, - CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => { - sess.target.is_like_osx || sess.target.is_like_msvc - } - CrateType::Rlib | CrateType::Staticlib => false, - }; + if sess.target.is_like_android { + // Sanitizer runtime libraries are provided dynamically on Android + // targets. + return; + } - if !needs_runtime { + if sess.opts.unstable_opts.external_clangrt { + // Linking against in-tree sanitizer runtimes is disabled via + // `-Z external-clangrt` + return; + } + + // On macOS the runtimes are distributed as dylibs which should be linked to + // both executables and dynamic shared objects. On most other platforms the + // runtimes are currently distributed as static libraries which should be + // linked to executables only. + if matches!(crate_type, CrateType::Rlib | CrateType::Staticlib) { + return; + } + + if matches!(crate_type, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) + && (sess.target.is_like_osx || sess.target.is_like_msvc) + { return; } From f46aceaaf75e6072e4e7227d71bcbf0119201212 Mon Sep 17 00:00:00 2001 From: Chris Wailes Date: Thu, 14 Mar 2024 11:06:39 -0700 Subject: [PATCH 3/3] Restore correct version of comment and fix logic bug --- compiler/rustc_codegen_ssa/src/back/link.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 0491b3a26953f..8d01832c678d4 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1213,16 +1213,16 @@ fn add_sanitizer_libraries( return; } - // On macOS the runtimes are distributed as dylibs which should be linked to - // both executables and dynamic shared objects. On most other platforms the - // runtimes are currently distributed as static libraries which should be - // linked to executables only. if matches!(crate_type, CrateType::Rlib | CrateType::Staticlib) { return; } + // On macOS and Windows using MSVC the runtimes are distributed as dylibs + // which should be linked to both executables and dynamic libraries. + // Everywhere else the runtimes are currently distributed as static + // libraries which should be linked to executables only. if matches!(crate_type, CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro) - && (sess.target.is_like_osx || sess.target.is_like_msvc) + && !(sess.target.is_like_osx || sess.target.is_like_msvc) { return; }