Skip to content

Commit 50fcb74

Browse files
authored
[Clang] Add support for -rpath on AIX (#89279)
Add support for existing -rpath option to AIX. Prior to this PR, if -rpath is passed on AIX it gets passed to the linker and crashes as the linker on AIX cannot process it.
1 parent ce5c702 commit 50fcb74

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,32 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
230230
// '-bnocdtors' that '-Wl' might forward.
231231
CmdArgs.push_back("-bcdtors:all:0:s");
232232

233+
if (Args.hasArg(options::OPT_rpath)) {
234+
for (const auto &bopt : Args.getAllArgValues(options::OPT_b))
235+
// Check -b opts prefix for "libpath:" or exact match for "nolibpath"
236+
if (!bopt.rfind("libpath:", 0) || bopt == "nolibpath")
237+
D.Diag(diag::err_drv_cannot_mix_options) << "-rpath" << "-b" + bopt;
238+
239+
for (const auto &wlopt : Args.getAllArgValues(options::OPT_Wl_COMMA))
240+
// Check -Wl, opts prefix for "-blibpath:" or exact match for
241+
// "-bnolibpath"
242+
if (!wlopt.rfind("-blibpath:", 0) || wlopt == "-bnolibpath")
243+
D.Diag(diag::err_drv_cannot_mix_options) << "-rpath" << "-Wl," + wlopt;
244+
245+
for (const auto &xopt : Args.getAllArgValues(options::OPT_Xlinker))
246+
// Check -Xlinker opts prefix for "-blibpath:" or exact match for
247+
// "-bnolibpath"
248+
if (!xopt.rfind("-blibpath:", 0) || xopt == "-bnolibpath")
249+
D.Diag(diag::err_drv_cannot_mix_options)
250+
<< "-rpath" << "-Xlinker " + xopt;
251+
252+
std::string BlibPathStr = "";
253+
for (const auto &dir : Args.getAllArgValues(options::OPT_rpath))
254+
BlibPathStr += dir + ":";
255+
BlibPathStr += "/usr/lib:/lib";
256+
CmdArgs.push_back(Args.MakeArgString(Twine("-blibpath:") + BlibPathStr));
257+
}
258+
233259
// Specify linker input file(s).
234260
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
235261

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,10 @@ void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
491491
TC.AddCXXStdlibLibArgs(Args, CmdArgs);
492492
else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext))
493493
TC.AddCCKextLibArgs(Args, CmdArgs);
494+
// Do not pass OPT_rpath to linker in AIX
495+
else if (A.getOption().matches(options::OPT_rpath) &&
496+
TC.getTriple().isOSAIX())
497+
continue;
494498
else
495499
A.renderAsInput(Args, CmdArgs);
496500
}

clang/test/Driver/aix-rpath.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Test -R passing search directories to the linker
2+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -### 2>&1 --target=powerpc-ibm-aix | FileCheck %s
3+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -### 2>&1 --target=powerpc64-ibm-aix | FileCheck %s
4+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -bfakelibpath -### 2>&1 --target=powerpc-ibm-aix | FileCheck %s
5+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -bfakelibpath -### 2>&1 --target=powerpc64-ibm-aix | FileCheck %s
6+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bloadmap:-blibpath -### 2>&1 --target=powerpc-ibm-aix | FileCheck %s
7+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bloadmap:-blibpath -### 2>&1 --target=powerpc64-ibm-aix | FileCheck %s
8+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-fakeblibpath -### 2>&1 --target=powerpc-ibm-aix | FileCheck %s
9+
// RUN: %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-fakeblibpath -### 2>&1 --target=powerpc64-ibm-aix | FileCheck %s
10+
11+
// RUN: %clang %s -bsvr4 -Wl,-R/dir1/ -Wl,-blibpath:/dir2/ -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
12+
// RUN: %clang %s -bsvr4 -Wl,-R/dir1/ -Wl,-blibpath:/dir2/ -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
13+
// RUN: %clang %s -bsvr4 -Xlinker -R/dir1/ -Xlinker -blibpath:/dir2/ -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
14+
// RUN: %clang %s -bsvr4 -Xlinker -R/dir1/ -Xlinker -blibpath:/dir2/ -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-LAST %s
15+
//
16+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -bnolibpath -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERBN %s
17+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -bnolibpath -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERBN %s
18+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnolibpath -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
19+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnolibpath -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
20+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentry,-bnolibpath -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
21+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentry,-bnolibpath -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWLBN %s
22+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -bnolibpath -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERXBN %s
23+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -bnolibpath -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERXBN %s
24+
//
25+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -blibpath:/dir3/ -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERB %s
26+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -blibpath:/dir3/ -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERB %s
27+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-blibpath:/dir3/ -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
28+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-blibpath:/dir3/ -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
29+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentr,-blibpath:/dir3/ -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
30+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Wl,-bnoentr,-blibpath:/dir3/ -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERWL %s
31+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -blibpath:/dir3/ -### 2>&1 --target=powerpc-ibm-aix | FileCheck --check-prefix=CHECK-ERX %s
32+
// RUN: not %clang %s -rpath /dir1/ -rpath /dir2/ -Xlinker -blibpath:/dir3/ -### 2>&1 --target=powerpc64-ibm-aix | FileCheck --check-prefix=CHECK-ERX %s
33+
34+
//CHECK: -blibpath:/dir1/:/dir2/:/usr/lib:/lib
35+
//CHECK-LAST: -blibpath:/dir2/
36+
//CHECK-ERBN: error: cannot specify '-bnolibpath' along with '-rpath'
37+
//CHECK-ERWLBN: error: cannot specify '-Wl,-bnolibpath' along with '-rpath'
38+
//CHECK-ERXBN: error: cannot specify '-Xlinker -bnolibpath' along with '-rpath'
39+
//CHECK-ERB: error: cannot specify '-blibpath:/dir3/' along with '-rpath'
40+
//CHECK-ERWL: error: cannot specify '-Wl,-blibpath:/dir3/' along with '-rpath'
41+
//CHECK-ERX: error: cannot specify '-Xlinker -blibpath:/dir3/' along with '-rpath'

clang/test/Driver/at_file_missing.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// stream, and also that @file arguments continue to be processed.
33

44
// RUN: echo "-D FOO" > %t.args
5-
// RUN: %clang -rpath @executable_path/../lib @%t.args %s -### 2>&1 | FileCheck %s
6-
// CHECK: "-D" "FOO"
5+
// RUN: %clang -rpath @executable_path/../lib @%t.args %s -### 2>&1 | FileCheck %s \
6+
// RUN: --check-prefixes=%if system-aix %{CHECK-AIX,CHECK-ALL%} \
7+
// RUN: %else %{CHECK-ALL,CHECK%}
8+
9+
// CHECK-ALL: "-D" "FOO"
710
// CHECK: "-rpath" "@executable_path/../lib"
11+
// CHECK-AIX: "-blibpath:@executable_path/../lib:/usr/lib:/lib"

0 commit comments

Comments
 (0)