From 4fbbc81e5fca7f012daf6059d85f2f3d2ca7087b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Sat, 25 Jul 2020 00:00:00 +0000 Subject: [PATCH] Link sanitizers when creating dynamic libraries on macOS --- src/librustc_codegen_ssa/back/link.rs | 15 ++++++++++++++- .../sanitizer-cdylib-link/Makefile | 1 - .../sanitizer-dylib-link/Makefile | 1 - .../sanitizer-staticlib-link/Makefile | 1 - 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/librustc_codegen_ssa/back/link.rs b/src/librustc_codegen_ssa/back/link.rs index d725a60118e..5de7ffbcfcb 100644 --- a/src/librustc_codegen_ssa/back/link.rs +++ b/src/librustc_codegen_ssa/back/link.rs @@ -769,9 +769,22 @@ fn link_natively<'a, B: ArchiveBuilder<'a>>( } fn link_sanitizers(sess: &Session, crate_type: CrateType, linker: &mut dyn Linker) { - if crate_type != CrateType::Executable { + // On macOS the runtimes are distributed as dylibs which should be linked to + // both executables and dynamic shared objects. Everywhere else the runtimes + // are currently distributed as static liraries which should be linked to + // executables only. + let needs_runtime = match crate_type { + CrateType::Executable => true, + CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => { + sess.target.target.options.is_like_osx + } + CrateType::Rlib | CrateType::Staticlib => false, + }; + + if !needs_runtime { return; } + let sanitizer = sess.opts.debugging_opts.sanitizer; if sanitizer.contains(SanitizerSet::ADDRESS) { link_sanitizer_runtime(sess, linker, "asan"); diff --git a/src/test/run-make-fulldeps/sanitizer-cdylib-link/Makefile b/src/test/run-make-fulldeps/sanitizer-cdylib-link/Makefile index b11d4c4cab7..e72fe5a5091 100644 --- a/src/test/run-make-fulldeps/sanitizer-cdylib-link/Makefile +++ b/src/test/run-make-fulldeps/sanitizer-cdylib-link/Makefile @@ -1,6 +1,5 @@ # needs-sanitizer-support # needs-sanitizer-address -# only-linux -include ../tools.mk diff --git a/src/test/run-make-fulldeps/sanitizer-dylib-link/Makefile b/src/test/run-make-fulldeps/sanitizer-dylib-link/Makefile index c2ebd2a6d8c..b9a3f829555 100644 --- a/src/test/run-make-fulldeps/sanitizer-dylib-link/Makefile +++ b/src/test/run-make-fulldeps/sanitizer-dylib-link/Makefile @@ -1,6 +1,5 @@ # needs-sanitizer-support # needs-sanitizer-address -# only-linux -include ../tools.mk diff --git a/src/test/run-make-fulldeps/sanitizer-staticlib-link/Makefile b/src/test/run-make-fulldeps/sanitizer-staticlib-link/Makefile index 5ceff16471c..4894f65b114 100644 --- a/src/test/run-make-fulldeps/sanitizer-staticlib-link/Makefile +++ b/src/test/run-make-fulldeps/sanitizer-staticlib-link/Makefile @@ -1,6 +1,5 @@ # needs-sanitizer-support # needs-sanitizer-address -# only-linux -include ../tools.mk