[clang] 0ba43f4 - [sanitizer] Add -lresolv only for non-Android non-musl Linux

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 13 19:12:17 PDT 2022


Author: Fangrui Song
Date: 2022-06-13T19:12:13-07:00
New Revision: 0ba43f4c2b263268f6fbc56bb3f6d43936781957

URL: https://github.com/llvm/llvm-project/commit/0ba43f4c2b263268f6fbc56bb3f6d43936781957
DIFF: https://github.com/llvm/llvm-project/commit/0ba43f4c2b263268f6fbc56bb3f6d43936781957.diff

LOG: [sanitizer] Add -lresolv only for non-Android non-musl Linux

Refine the D127145 logic with my original suggestion.
It turns out that many OSes don't have libresolv.

Added: 
    

Modified: 
    clang/lib/Driver/ToolChains/CommonArgs.cpp
    clang/test/Driver/sanitizer-ld.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index db022d133d9d..7be25072c62e 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -838,8 +838,11 @@ void tools::linkSanitizerRuntimeDeps(const ToolChain &TC,
       TC.getTriple().isOSNetBSD() ||
       TC.getTriple().isOSOpenBSD())
     CmdArgs.push_back("-lexecinfo");
-  // There is no libresolv on Android.
-  if (!TC.getTriple().isAndroid())
+  // There is no libresolv on Android, FreeBSD, OpenBSD, etc. On musl
+  // libresolv.a, even if exists, is an empty archive to satisfy POSIX -lresolv
+  // requirement.
+  if (TC.getTriple().isOSLinux() && !TC.getTriple().isAndroid() &&
+      !TC.getTriple().isMusl())
     CmdArgs.push_back("-lresolv");
 }
 

diff  --git a/clang/test/Driver/sanitizer-ld.c b/clang/test/Driver/sanitizer-ld.c
index b043a4d2712c..423ce8beaa84 100644
--- a/clang/test/Driver/sanitizer-ld.c
+++ b/clang/test/Driver/sanitizer-ld.c
@@ -107,7 +107,6 @@
 // CHECK-ASAN-FREEBSD: "--export-dynamic"
 // CHECK-ASAN-FREEBSD: "-lpthread"
 // CHECK-ASAN-FREEBSD: "-lrt"
-// CHECK-ASAN-FREEBSD: "-lresolv"
 
 // RUN: %clang -### %s 2>&1 \
 // RUN:     --target=i386-unknown-freebsd -fuse-ld=ld -fsanitize=address \


        


More information about the cfe-commits mailing list