https://github.com/jhuber6 updated 
https://github.com/llvm/llvm-project/pull/220697

>From 980bd5fa38a8a50147033b4c95e1c9ac38d8827b Mon Sep 17 00:00:00 2001
From: Joseph Huber <[email protected]>
Date: Wed, 2 Sep 2026 13:56:19 -0500
Subject: [PATCH 1/2] [Clang] Always apply `-frtlib-add-rpath` if requested

Summary:
This path used to try to infer if it was needed by using the language
options. The result was that if you tried to use this on a `.o` link job
you would not get the rpath behavior you would expect.

The reason it was this way stems from the fact that this used to be
implicit behavior, so it was being conservative. At some point one of
the maintainers dictated that we cannot add rpath by default, so we
ended up with this, but still being conservative.

If the user passes `-frtlib-add-rpath`, just add the rpath. Flang does
this already.
---
 clang/lib/Driver/ToolChains/CommonArgs.cpp    |  3 +-
 .../test/Driver/arch-specific-libdir-rpath.c  | 36 +++++++++----------
 2 files changed, 19 insertions(+), 20 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 7157c55e9f9e8..81599914f0df8 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -575,9 +575,10 @@ void tools::AddLinkerInputs(const ToolChain &TC, const 
InputInfoList &Inputs,
         CmdArgs.push_back(Args.MakeArgString("-lm"));
       if (Triple.isOSLinux())
         CmdArgs.push_back(Args.MakeArgString("--pop-state"));
-      addArchSpecificRPath(TC, Args, CmdArgs);
     }
   }
+
+  addArchSpecificRPath(TC, Args, CmdArgs);
 }
 
 const char *tools::getLDMOption(const llvm::Triple &T, const ArgList &Args) {
diff --git a/clang/test/Driver/arch-specific-libdir-rpath.c 
b/clang/test/Driver/arch-specific-libdir-rpath.c
index 1e6bbbc5929ac..bffd99f2c1733 100644
--- a/clang/test/Driver/arch-specific-libdir-rpath.c
+++ b/clang/test/Driver/arch-specific-libdir-rpath.c
@@ -16,20 +16,11 @@
 // RUN:     -fno-rtlib-add-rpath 2>&1 \
 // RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,NO-RPATH-X86_64 %s
 //
-// Test that -rpath is added only under the right circumstance even if
-// -frtlib-add-rpath is specified.
-//
-// Add LIBPATH but no RPATH for -fsanitizer=address w/o -shared-libasan
-// RUN: %clang %s -### --target=x86_64-linux -fsanitize=undefined \
-// RUN:     -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
-// RUN:     -frtlib-add-rpath 2>&1 \
-// RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,NO-RPATH-X86_64 %s
-//
-// Add LIBPATH but no RPATH for -fsanitizer=address w/o -shared-libasan
+// Add RPATH if requested.
 // RUN: %clang %s -### --target=x86_64-linux -fsanitize=undefined \
 // RUN:     -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
 // RUN:     -frtlib-add-rpath 2>&1 \
-// RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,NO-RPATH-X86_64 %s
+// RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,RPATH-X86_64 %s
 //
 // Add LIBPATH, RPATH for -fsanitize=address -shared-libasan
 // RUN: %clang %s -### --target=x86_64-linux \
@@ -57,23 +48,30 @@
 // RUN:     -frtlib-add-rpath 2>&1 \
 // RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,RPATH-X86_64 %s
 //
-// Add LIBPATH but no RPATH for ubsan (or any other sanitizer)
+// Add LIBPATH, RPATH for ubsan (or any other sanitizer)
 // RUN: %clang %s -### -fsanitize=undefined --target=x86_64-linux \
 // RUN:     -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
 // RUN:     -frtlib-add-rpath 2>&1 \
-// RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,NO-RPATH-X86_64 %s
+// RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,RPATH-X86_64 %s
 //
-// Add LIBPATH but no RPATH if no sanitizer or runtime is specified
+// Add LIBPATH, RPATH if no sanitizer or runtime is specified
 // RUN: %clang %s -### --target=x86_64-linux \
 // RUN:     -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
 // RUN:     -frtlib-add-rpath 2>&1 \
-// RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,NO-RPATH-X86_64 %s
+// RUN:   | FileCheck --check-prefixes=RESDIR,LIBPATH-X86_64,RPATH-X86_64 %s
+//
+// Same for a link-only job, which has no input language.
+// RUN: %clang -c %s -o %t.o
+// RUN: %clang %t.o -### --target=x86_64-linux \
+// RUN:     -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
+// RUN:     -frtlib-add-rpath 2>&1 \
+// RUN:   | FileCheck --check-prefixes=LINKONLY-X86_64 %s
 //
-// Do not add LIBPATH or RPATH if arch-specific subdir doesn't exist
+// Old-layout lib/linux/<arch> is not used when that subdirectory does not 
exist.
 // RUN: %clang %s -### --target=x86_64-linux \
 // RUN:     -resource-dir=%S/Inputs/resource_dir \
 // RUN:     -frtlib-add-rpath 2>&1 \
-// RUN:   | FileCheck --check-prefixes=RESDIR,NO-LIBPATH,NO-RPATH %s
+// RUN:   | FileCheck 
--check-prefixes=RESDIR,NO-LIBPATH-X86_64,NO-RPATH-X86_64 %s
 
 // Test that the driver adds an per-target arch-specific subdirectory in
 // {RESOURCE_DIR}/lib/{triple} to the linker search path and to '-rpath'
@@ -95,8 +93,8 @@
 // LIBPATH-AARCH64: -L[[RESDIR]]{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)aarch64}}
 // RPATH-AARCH64:   "-rpath" 
"[[RESDIR]]{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)aarch64}}"
 //
-// NO-LIBPATH-NOT: "-L{{[^"]*Inputs(/|\\\\)resource_dir}}"
-// NO-RPATH-NOT:   "-rpath" {{.*(/|\\\\)Inputs(/|\\\\)resource_dir}}
+// LINKONLY-X86_64: 
-L{{[^"]*}}resource_dir_with_arch_subdir{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}
+// LINKONLY-X86_64: "-rpath" 
"{{[^"]*}}resource_dir_with_arch_subdir{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}"
 
 // PERTARGET: "-resource-dir" "[[PTRESDIR:[^"]*]]"
 // PERTARGET: -L[[PTRESDIR]]{{(/|\\\\)lib(/|\\\\)x86_64-unknown-linux-gnu}}

>From 4f1b8b40a446b3af837d9082bb63f9c71feb37f4 Mon Sep 17 00:00:00 2001
From: Joseph Huber <[email protected]>
Date: Wed, 2 Sep 2026 14:59:03 -0500
Subject: [PATCH 2/2] comments

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp     |  4 +++-
 clang/test/Driver/arch-specific-libdir-rpath.c | 12 ++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 81599914f0df8..05c434a141f35 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1480,7 +1480,9 @@ void tools::addArchSpecificRPath(const ToolChain &TC, 
const ArgList &Args,
                     options::OPT_fno_rtlib_add_rpath, false))
     return;
 
-  if (TC.getTriple().isOSAIX()) // TODO: AIX doesn't support -rpath option.
+  // Using -rpath is a host ELF/Mach-O linker option.
+  const llvm::Triple &Triple = TC.getTriple();
+  if ((!Triple.isOSBinFormatELF() && !Triple.isOSBinFormatMachO()))
     return;
 
   SmallVector<std::string> CandidateRPaths(TC.getArchSpecificLibPaths());
diff --git a/clang/test/Driver/arch-specific-libdir-rpath.c 
b/clang/test/Driver/arch-specific-libdir-rpath.c
index bffd99f2c1733..0bc178f5f1cdc 100644
--- a/clang/test/Driver/arch-specific-libdir-rpath.c
+++ b/clang/test/Driver/arch-specific-libdir-rpath.c
@@ -72,6 +72,16 @@
 // RUN:     -resource-dir=%S/Inputs/resource_dir \
 // RUN:     -frtlib-add-rpath 2>&1 \
 // RUN:   | FileCheck 
--check-prefixes=RESDIR,NO-LIBPATH-X86_64,NO-RPATH-X86_64 %s
+//
+// Do not pass -rpath to unsupported linkers.
+// RUN: %clang %s -### --target=x86_64-pc-windows-msvc \
+// RUN:     -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
+// RUN:     -frtlib-add-rpath 2>&1 \
+// RUN:   | FileCheck --check-prefixes=RESDIR,NO-RPATH-UNSUPPORTED %s
+// RUN: %clang %s -### --target=wasm32-unknown-unknown \
+// RUN:     -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
+// RUN:     -frtlib-add-rpath 2>&1 \
+// RUN:   | FileCheck --check-prefixes=RESDIR,NO-RPATH-UNSUPPORTED %s
 
 // Test that the driver adds an per-target arch-specific subdirectory in
 // {RESOURCE_DIR}/lib/{triple} to the linker search path and to '-rpath'
@@ -95,6 +105,8 @@
 //
 // LINKONLY-X86_64: 
-L{{[^"]*}}resource_dir_with_arch_subdir{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}
 // LINKONLY-X86_64: "-rpath" 
"{{[^"]*}}resource_dir_with_arch_subdir{{(/|\\\\)lib(/|\\\\)linux(/|\\\\)x86_64}}"
+//
+// NO-RPATH-UNSUPPORTED-NOT: "-rpath"
 
 // PERTARGET: "-resource-dir" "[[PTRESDIR:[^"]*]]"
 // PERTARGET: -L[[PTRESDIR]]{{(/|\\\\)lib(/|\\\\)x86_64-unknown-linux-gnu}}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to