[clang] [Clang] Add support for -rpath on AIX (PR #89279)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 18 10:48:37 PDT 2024


https://github.com/madanial0 updated https://github.com/llvm/llvm-project/pull/89279

>From 2863ddddf621ad2d6f1778aceac2612de7352eaa Mon Sep 17 00:00:00 2001
From: Mark Danial <madanial at dixon.rtp.raleigh.ibm.com>
Date: Wed, 17 Apr 2024 11:05:08 -0400
Subject: [PATCH] [Clang] Add support for -rpath on AIX

---
 clang/lib/Driver/ToolChains/AIX.cpp        | 26 ++++++++++++++
 clang/lib/Driver/ToolChains/CommonArgs.cpp |  4 +++
 clang/test/Driver/aix-rpath.c              | 41 ++++++++++++++++++++++
 clang/test/Driver/at_file_missing.c        |  8 +++--
 4 files changed, 77 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Driver/aix-rpath.c

diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp
index c1b350893b3744..b2ac1efdc217c1 100644
--- a/clang/lib/Driver/ToolChains/AIX.cpp
+++ b/clang/lib/Driver/ToolChains/AIX.cpp
@@ -230,6 +230,32 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   // '-bnocdtors' that '-Wl' might forward.
   CmdArgs.push_back("-bcdtors:all:0:s");
 
+  if (Args.hasArg(options::OPT_rpath)) {
+    for (const auto &bopt : Args.getAllArgValues(options::OPT_b))
+      // Check -b opts prefix for "libpath:" or exact match for "nolibpath"
+      if (!bopt.rfind("libpath:", 0) || bopt == "nolibpath")
+        D.Diag(diag::err_drv_cannot_mix_options) << "-rpath" << "-b" + bopt;
+
+    for (const auto &wlopt : Args.getAllArgValues(options::OPT_Wl_COMMA))
+      // Check -Wl, opts prefix for "-blibpath:" or exact match for
+      // "-bnolibpath"
+      if (!wlopt.rfind("-blibpath:", 0) || wlopt == "-bnolibpath")
+        D.Diag(diag::err_drv_cannot_mix_options) << "-rpath" << "-Wl," + wlopt;
+
+    for (const auto &xopt : Args.getAllArgValues(options::OPT_Xlinker))
+      // Check -Xlinker opts prefix for "-blibpath:" or exact match for
+      // "-bnolibpath"
+      if (!xopt.rfind("-blibpath:", 0) || xopt == "-bnolibpath")
+        D.Diag(diag::err_drv_cannot_mix_options)
+            << "-rpath" << "-Xlinker " + xopt;
+
+    std::string BlibPathStr = "";
+    for (const auto &dir : Args.getAllArgValues(options::OPT_rpath))
+      BlibPathStr += dir + ":";
+    BlibPathStr += "/usr/lib:/lib";
+    CmdArgs.push_back(Args.MakeArgString(Twine("-blibpath:") + BlibPathStr));
+  }
+
   // Specify linker input file(s).
   AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
 
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index f10aa4dfaa9ddd..d38e9fe09f7d18 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -436,6 +436,10 @@ void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
       TC.AddCXXStdlibLibArgs(Args, CmdArgs);
     else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext))
       TC.AddCCKextLibArgs(Args, CmdArgs);
+    // Do not pass OPT_rpath to linker in AIX
+    else if (A.getOption().matches(options::OPT_rpath) &&
+             TC.getTriple().isOSAIX())
+      continue;
     else
       A.renderAsInput(Args, CmdArgs);
   }
diff --git a/clang/test/Driver/aix-rpath.c b/clang/test/Driver/aix-rpath.c
new file mode 100644
index 00000000000000..c717307282ff43
--- /dev/null
+++ b/clang/test/Driver/aix-rpath.c
@@ -0,0 +1,41 @@
+// Test -R passing search directories to the linker
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -### 2>&1  --target=powerpc-ibm-aix | FileCheck %s
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -### 2>&1  --target=powerpc64-ibm-aix | FileCheck %s
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -bfakelibpath -### 2>&1  --target=powerpc-ibm-aix | FileCheck %s
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -bfakelibpath -### 2>&1  --target=powerpc64-ibm-aix | FileCheck %s
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bloadmap:-blibpath -### 2>&1  --target=powerpc-ibm-aix | FileCheck %s
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bloadmap:-blibpath -### 2>&1  --target=powerpc64-ibm-aix | FileCheck %s
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-fakeblibpath -### 2>&1  --target=powerpc-ibm-aix | FileCheck %s
+// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-fakeblibpath -### 2>&1  --target=powerpc64-ibm-aix | FileCheck %s
+
+// RUN: %clang %s -bsvr4 -Wl,-R/dir1/ -Wl,-blibpath:/dir2/ -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
+// RUN: %clang %s -bsvr4 -Wl,-R/dir1/ -Wl,-blibpath:/dir2/ -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
+// RUN: %clang %s -bsvr4 -Xlinker -R/dir1/ -Xlinker -blibpath:/dir2/ -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
+// RUN: %clang %s -bsvr4 -Xlinker -R/dir1/ -Xlinker -blibpath:/dir2/ -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
+//
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -bnolibpath -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERBN %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -bnolibpath -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERBN %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnolibpath -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnolibpath -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentry,-bnolibpath -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentry,-bnolibpath -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -bnolibpath -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERXBN %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -bnolibpath -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERXBN %s
+//
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -blibpath:/dir3/ -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERB %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -blibpath:/dir3/ -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERB %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-blibpath:/dir3/ -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-blibpath:/dir3/ -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentr,-blibpath:/dir3/ -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentr,-blibpath:/dir3/ -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -blibpath:/dir3/ -### 2>&1  --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERX %s
+// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -blibpath:/dir3/ -### 2>&1  --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERX %s
+
+//CHECK: -blibpath:/dir1/:/dir2/:/usr/lib:/lib
+//CHECK-LAST: -blibpath:/dir2/
+//CHECK-ERBN: error: cannot specify '-bnolibpath' along with '-rpath'
+//CHECK-ERWLBN: error: cannot specify '-Wl,-bnolibpath' along with '-rpath'
+//CHECK-ERXBN: error: cannot specify '-Xlinker -bnolibpath' along with '-rpath'
+//CHECK-ERB: error: cannot specify '-blibpath:/dir3/' along with '-rpath'
+//CHECK-ERWL: error: cannot specify '-Wl,-blibpath:/dir3/' along with '-rpath'
+//CHECK-ERX: error: cannot specify '-Xlinker -blibpath:/dir3/' along with '-rpath'
diff --git a/clang/test/Driver/at_file_missing.c b/clang/test/Driver/at_file_missing.c
index 23645a5d3f93a2..306c53bf8c757c 100644
--- a/clang/test/Driver/at_file_missing.c
+++ b/clang/test/Driver/at_file_missing.c
@@ -2,6 +2,10 @@
 // stream, and also that @file arguments continue to be processed.
 
 // RUN: echo "-D FOO" > %t.args
-// RUN: %clang -rpath @executable_path/../lib @%t.args %s -### 2>&1 | FileCheck %s
-// CHECK: "-D" "FOO"
+// RUN: %clang -rpath @executable_path/../lib @%t.args %s -### 2>&1 | FileCheck %s \
+// RUN:        --check-prefixes=%if system-aix %{CHECK-AIX,CHECK-ALL%} \
+// RUN:        %else %{CHECK-ALL,CHECK%}
+
+// CHECK-ALL: "-D" "FOO"
 // CHECK: "-rpath" "@executable_path/../lib"
+// CHECK-AIX: "-blibpath:@executable_path/../lib:/usr/lib:/lib"



More information about the cfe-commits mailing list