Skip to content

Commit 1efa872

Browse files
committed
[Driver] Improve error when a compiler-rt library is not found
BSD, Linux, and z/OS enable `LLVM_ENABLE_PER_TARGET_RUNTIME_DIR` by default. When a compiler-rt library is not found, we currently report an incorrect filename `libclang_rt.XXX-$arch.a` ``` % /tmp/Debug/bin/clang++ a.cc -fsanitize=address -o a ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/linux/libclang_rt.asan-x86_64.a: No such file or directory clang++: error: linker command failed with exit code 1 (use -v to see invocation) ``` With this change, we will correctly report: ``` % /tmp/Debug/bin/clang++ a.cc -fsanitize=address -o a ld.lld: error: cannot open /tmp/Debug/lib/clang/19/lib/x86_64-unknown-linux-gnu/libclang_rt.asan.a: No such file or directory clang++: error: linker command failed with exit code 1 (use -v to see invocation) ``` Link: https://discourse.llvm.org/t/runtime-directory-fallback/76860
1 parent d9f9775 commit 1efa872

32 files changed

+114
-128
lines changed

clang/lib/Driver/ToolChain.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,19 +656,29 @@ std::string ToolChain::getCompilerRT(const ArgList &Args, StringRef Component,
656656
// Check for runtime files in the new layout without the architecture first.
657657
std::string CRTBasename =
658658
buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/false);
659+
SmallString<128> Path;
659660
for (const auto &LibPath : getLibraryPaths()) {
660661
SmallString<128> P(LibPath);
661662
llvm::sys::path::append(P, CRTBasename);
662663
if (getVFS().exists(P))
663664
return std::string(P);
665+
if (Path.empty())
666+
Path = P;
664667
}
668+
if (getTriple().isOSAIX())
669+
Path.clear();
665670

666-
// Fall back to the old expected compiler-rt name if the new one does not
667-
// exist.
671+
// Check the filename for the old layout if the new one does not exist.
668672
CRTBasename =
669673
buildCompilerRTBasename(Args, Component, Type, /*AddArch=*/true);
670-
SmallString<128> Path(getCompilerRTPath());
671-
llvm::sys::path::append(Path, CRTBasename);
674+
SmallString<128> OldPath(getCompilerRTPath());
675+
llvm::sys::path::append(OldPath, CRTBasename);
676+
if (Path.empty() || getVFS().exists(OldPath))
677+
return std::string(OldPath);
678+
679+
// If none is found, use a file name from the new layout, which may get
680+
// printed in an error message, aiding users in knowing what Clang is
681+
// looking for.
672682
return std::string(Path);
673683
}
674684

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.msan.a

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.msan.a.syms

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.msan_cxx.a

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.msan_cxx.a.syms

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.tsan.a

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.tsan.a.syms

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.tsan_cxx.a

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir/lib/x86_64-unknown-linux/libclang_rt.tsan_cxx.a.syms

Whitespace-only changes.

clang/test/Driver/arch-specific-libdir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@
4949
//
5050
// Have a stricter check for no-archdir - that the driver doesn't add any
5151
// subdirectory from the provided resource directory.
52-
// NO-ARCHDIR-NOT: -L[[FILE_PATH]]/Inputs/resource_dir
52+
// NO-ARCHDIR-NOT: -L[[FILE_PATH]]/Inputs/resource_dir"

clang/test/Driver/arm-compiler-rt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: -resource-dir=%S/Inputs/resource_dir_with_arch_subdir \
44
// RUN: -rtlib=compiler-rt -### %s 2>&1 \
55
// RUN: | FileCheck %s -check-prefix ARM-EABI
6-
// ARM-EABI: "-lclang_rt.builtins-arm"
6+
// ARM-EABI: "-lclang_rt.builtins"
77

88
// RUN: %clang -target arm-linux-gnueabi \
99
// RUN: --sysroot=%S/Inputs/resource_dir_with_arch_subdir \

clang/test/Driver/baremetal-sysroot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
// CHECK-V6M-C-SAME: "-x" "c++" "{{.*}}baremetal-sysroot.cpp"
1919
// CHECK-V6M-C-NEXT: "{{[^"]*}}ld{{(\.(lld|bfd|gold))?}}{{(\.exe)?}}" "{{.*}}.o" "-Bstatic"
2020
// CHECK-V6M-C-SAME: "-L{{.*}}/baremetal_default_sysroot{{[/\\]+}}bin{{[/\\]+}}..{{[/\\]+}}lib{{[/\\]+}}clang-runtimes{{[/\\]+}}armv6m-none-eabi{{[/\\]+}}lib"
21-
// CHECK-V6M-C-SAME: "-lc" "-lm" "-lclang_rt.builtins-armv6m"
21+
// CHECK-V6M-C-SAME: "-lc" "-lm" "-lclang_rt.builtins"
2222
// CHECK-V6M-C-SAME: "-o" "{{.*}}.o"

clang/test/Driver/baremetal.cpp

Lines changed: 19 additions & 43 deletions
Large diffs are not rendered by default.

clang/test/Driver/compiler-rt-unwind.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
// RUN: --target=x86_64-unknown-linux -rtlib=compiler-rt \
5353
// RUN: --gcc-toolchain="" -resource-dir=%S/Inputs/resource_dir \
5454
// RUN: | FileCheck --check-prefix=RTLIB-COMPILER-RT %s
55-
// RTLIB-COMPILER-RT: "{{.*}}libclang_rt.builtins-x86_64.a"
55+
// RTLIB-COMPILER-RT: "{{.*}}libclang_rt.builtins.a"
5656
//
5757
// RUN: %clang -### %s 2>&1 \
5858
// RUN: --target=x86_64-unknown-linux -rtlib=compiler-rt --unwindlib=libunwind \
@@ -62,7 +62,7 @@
6262
// RUN: --target=x86_64-unknown-linux -rtlib=compiler-rt --unwindlib=libunwind \
6363
// RUN: --gcc-toolchain="" -resource-dir=%S/Inputs/resource_dir \
6464
// RUN: | FileCheck --check-prefix=RTLIB-COMPILER-RT-UNWINDLIB-COMPILER-RT %s
65-
// RTLIB-COMPILER-RT-UNWINDLIB-COMPILER-RT: "{{.*}}libclang_rt.builtins-x86_64.a"
65+
// RTLIB-COMPILER-RT-UNWINDLIB-COMPILER-RT: "{{.*}}libclang_rt.builtins.a"
6666
// RTLIB-COMPILER-RT-UNWINDLIB-COMPILER-RT-SAME: "--as-needed"
6767
// RTLIB-COMPILER-RT-UNWINDLIB-COMPILER-RT-SAME: "-lunwind"
6868
// RTLIB-COMPILER-RT-UNWINDLIB-COMPILER-RT-SAME: "--no-as-needed"
@@ -71,14 +71,14 @@
7171
// RUN: --target=x86_64-unknown-linux -rtlib=compiler-rt --unwindlib=libgcc \
7272
// RUN: --gcc-toolchain="" -resource-dir=%S/Inputs/resource_dir \
7373
// RUN: | FileCheck --check-prefix=RTLIB-COMPILER-RT-UNWINDLIB-GCC %s
74-
// RTLIB-COMPILER-RT-UNWINDLIB-GCC: "{{.*}}libclang_rt.builtins-x86_64.a"
74+
// RTLIB-COMPILER-RT-UNWINDLIB-GCC: "{{.*}}libclang_rt.builtins.a"
7575
// RTLIB-COMPILER-RT-UNWINDLIB-GCC-SAME: "-lgcc_s"
7676
//
7777
// RUN: %clang -### %s 2>&1 \
7878
// RUN: --target=x86_64-unknown-linux -rtlib=compiler-rt --unwindlib=libgcc \
7979
// RUN: -static --gcc-toolchain="" -resource-dir=%S/Inputs/resource_dir \
8080
// RUN: | FileCheck --check-prefix=RTLIB-COMPILER-RT-UNWINDLIB-GCC-STATIC %s
81-
// RTLIB-COMPILER-RT-UNWINDLIB-GCC-STATIC: "{{.*}}libclang_rt.builtins-x86_64.a"
81+
// RTLIB-COMPILER-RT-UNWINDLIB-GCC-STATIC: "{{.*}}libclang_rt.builtins.a"
8282
// RTLIB-COMPILER-RT-UNWINDLIB-GCC-STATIC-SAME: "-lgcc_eh"
8383
//
8484
// RUN: not %clang %s 2> %t.err \

clang/test/Driver/coverage-ld.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// RUN: | FileCheck --check-prefix=CHECK-LINUX-I386 %s
1414
//
1515
// CHECK-LINUX-I386-NOT: "-u__llvm_profile_runtime"
16-
// CHECK-LINUX-I386: /Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-i386.a"
16+
// CHECK-LINUX-I386: /Inputs/resource_dir{{/|\\\\}}lib{{.*}}i386-unknown-linux{{.*}}libclang_rt.profile.a"
1717
// CHECK-LINUX-I386-NOT: "-u__llvm_profile_runtime"
1818
// CHECK-LINUX-I386: "-lc"
1919
//
@@ -24,7 +24,7 @@
2424
// RUN: | FileCheck --check-prefix=CHECK-LINUX-X86-64 %s
2525
//
2626
// CHECK-LINUX-X86-64: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
27-
// CHECK-LINUX-X86-64: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-x86_64.a" {{.*}} "-lc"
27+
// CHECK-LINUX-X86-64: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{.*}}linux{{.*}}libclang_rt.profile.a" {{.*}} "-lc"
2828
//
2929
// RUN: %clang -### %s 2>&1 \
3030
// RUN: --target=x86_64-unknown-freebsd --coverage -fuse-ld=ld \

clang/test/Driver/fuchsia.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
// CHECK-SCUDO-X86: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
188188
// CHECK-SCUDO-X86: "-fsanitize=safe-stack,scudo"
189189
// CHECK-SCUDO-X86: "-pie"
190-
// CHECK-SCUDO-X86: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}fuchsia{{/|\\\\}}libclang_rt.scudo_standalone-x86_64.so"
190+
// CHECK-SCUDO-X86: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{.*}}fuchsia{{.*}}libclang_rt.scudo_standalone.so"
191191

192192
// RUN: %clang -### %s --target=aarch64-unknown-fuchsia \
193193
// RUN: -fsanitize=scudo 2>&1 \
@@ -197,7 +197,7 @@
197197
// CHECK-SCUDO-AARCH64: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
198198
// CHECK-SCUDO-AARCH64: "-fsanitize=shadow-call-stack,scudo"
199199
// CHECK-SCUDO-AARCH64: "-pie"
200-
// CHECK-SCUDO-AARCH64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}fuchsia{{/|\\\\}}libclang_rt.scudo_standalone-aarch64.so"
200+
// CHECK-SCUDO-AARCH64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{.*}}fuchsia{{.*}}libclang_rt.scudo_standalone.so"
201201

202202
// RUN: %clang -### %s --target=x86_64-unknown-fuchsia \
203203
// RUN: -fsanitize=scudo -fPIC -shared 2>&1 \
@@ -206,7 +206,7 @@
206206
// RUN: | FileCheck %s -check-prefix=CHECK-SCUDO-SHARED
207207
// CHECK-SCUDO-SHARED: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
208208
// CHECK-SCUDO-SHARED: "-fsanitize=safe-stack,scudo"
209-
// CHECK-SCUDO-SHARED: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}fuchsia{{/|\\\\}}libclang_rt.scudo_standalone-x86_64.so"
209+
// CHECK-SCUDO-SHARED: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{.*}}fuchsia{{.*}}libclang_rt.scudo_standalone.so"
210210

211211
// RUN: %clang -### %s --target=aarch64-unknown-fuchsia \
212212
// RUN: -fsanitize=leak 2>&1 \

clang/test/Driver/instrprof-ld.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// RUN: | FileCheck --check-prefix=CHECK-LINUX-I386 %s
88
//
99
// CHECK-LINUX-I386: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
10-
// CHECK-LINUX-I386: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-i386.a" {{.*}} "-lc"
10+
// CHECK-LINUX-I386: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}i386-unknown-linux{{/|\\\\}}libclang_rt.profile.a" {{.*}} "-lc"
1111
//
1212
// RUN: %clang -### %s 2>&1 \
1313
// RUN: --target=x86_64-unknown-linux -fprofile-instr-generate -fuse-ld=ld \
@@ -16,7 +16,7 @@
1616
// RUN: | FileCheck --check-prefix=CHECK-LINUX-X86-64 %s
1717
//
1818
// CHECK-LINUX-X86-64: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
19-
// CHECK-LINUX-X86-64: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-x86_64.a" {{.*}} "-lc"
19+
// CHECK-LINUX-X86-64: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{.*}}linux{{.*}}libclang_rt.profile.a" {{.*}} "-lc"
2020
//
2121
// RUN: %clang -### %s 2>&1 \
2222
// RUN: --target=x86_64-unknown-linux -fprofile-instr-generate -nostdlib -fuse-ld=ld \
@@ -25,7 +25,7 @@
2525
// RUN: | FileCheck --check-prefix=CHECK-LINUX-NOSTDLIB-X86-64 %s
2626
//
2727
// CHECK-LINUX-NOSTDLIB-X86-64: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
28-
// CHECK-LINUX-NOSTDLIB-X86-64: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-x86_64.a"
28+
// CHECK-LINUX-NOSTDLIB-X86-64: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{.*}}linux{{.*}}libclang_rt.profile.a"
2929
//
3030
// RUN: %clang -### %s 2>&1 \
3131
// RUN: --target=x86_64-unknown-freebsd -fprofile-instr-generate -fuse-ld=ld \
@@ -62,7 +62,7 @@
6262
// RUN: | FileCheck --check-prefix=CHECK-LINUX-I386-SHARED %s
6363
//
6464
// CHECK-LINUX-I386-SHARED: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
65-
// CHECK-LINUX-I386-SHARED: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-i386.a" {{.*}} "-lc"
65+
// CHECK-LINUX-I386-SHARED: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{.*}}i386-unknown-linux{{.*}}libclang_rt.profile.a" {{.*}} "-lc"
6666
//
6767
// RUN: %clang -### %s 2>&1 \
6868
// RUN: -shared \
@@ -72,7 +72,7 @@
7272
// RUN: | FileCheck --check-prefix=CHECK-LINUX-X86-64-SHARED %s
7373
//
7474
// CHECK-LINUX-X86-64-SHARED: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}"
75-
// CHECK-LINUX-X86-64-SHARED: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-x86_64.a" {{.*}} "-lc"
75+
// CHECK-LINUX-X86-64-SHARED: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{.*}}linux{{.*}}libclang_rt.profile.a" {{.*}} "-lc"
7676
//
7777
// RUN: %clang -### %s 2>&1 \
7878
// RUN: -shared \

clang/test/Driver/linux-ld.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@
6161
// CHECK-LD-RT: "--eh-frame-hdr"
6262
// CHECK-LD-RT: "-m" "elf_x86_64"
6363
// CHECK-LD-RT: "-dynamic-linker"
64-
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtbegin-x86_64.o"
64+
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-linux{{/|\\\\}}clang_rt.crtbegin.o"
6565
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/10.2.0"
6666
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib/gcc/x86_64-unknown-linux/10.2.0/../../../../x86_64-unknown-linux/lib"
6767
// CHECK-LD-RT: "-L[[SYSROOT]]/lib"
6868
// CHECK-LD-RT: "-L[[SYSROOT]]/usr/lib"
69-
// CHECK-LD-RT: libclang_rt.builtins-x86_64.a"
69+
// CHECK-LD-RT: libclang_rt.builtins.a"
7070
// CHECK-LD-RT: "-lc"
71-
// CHECK-LD-RT: libclang_rt.builtins-x86_64.a"
72-
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtend-x86_64.o"
71+
// CHECK-LD-RT: libclang_rt.builtins.a"
72+
// CHECK-LD-RT: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-unknown-linux{{/|\\\\}}clang_rt.crtend.o"
7373
//
7474
// RUN: %clang -### %s -no-pie 2>&1 \
7575
// RUN: --target=i686-unknown-linux \
@@ -84,15 +84,15 @@
8484
// CHECK-LD-RT-I686: "--eh-frame-hdr"
8585
// CHECK-LD-RT-I686: "-m" "elf_i386"
8686
// CHECK-LD-RT-I686: "-dynamic-linker"
87-
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtbegin-i386.o"
87+
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}i686-unknown-linux{{/|\\\\}}clang_rt.crtbegin.o"
8888
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/usr/lib/gcc/i686-unknown-linux/10.2.0"
8989
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/usr/lib/gcc/i686-unknown-linux/10.2.0/../../../../i686-unknown-linux/lib"
9090
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/lib"
9191
// CHECK-LD-RT-I686: "-L[[SYSROOT]]/usr/lib"
92-
// CHECK-LD-RT-I686: libclang_rt.builtins-i386.a"
92+
// CHECK-LD-RT-I686: libclang_rt.builtins.a"
9393
// CHECK-LD-RT-I686: "-lc"
94-
// CHECK-LD-RT-I686: libclang_rt.builtins-i386.a"
95-
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}clang_rt.crtend-i386.o"
94+
// CHECK-LD-RT-I686: libclang_rt.builtins.a"
95+
// CHECK-LD-RT-I686: "[[RESDIR]]{{/|\\\\}}lib{{/|\\\\}}i686-unknown-linux{{/|\\\\}}clang_rt.crtend.o"
9696
//
9797
// RUN: %clang -### %s -no-pie 2>&1 \
9898
// RUN: --target=arm-linux-androideabi \

clang/test/Driver/print-libgcc-file-name-clangrt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
// RUN: --sysroot=%S/Inputs/resource_dir_with_arch_subdir \
5656
// RUN: -resource-dir=%S/Inputs/resource_dir_with_arch_subdir 2>&1 \
5757
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-ARM-BAREMETAL %s
58-
// CHECK-CLANGRT-ARM-BAREMETAL: libclang_rt.builtins-armv7m.a
58+
// CHECK-CLANGRT-ARM-BAREMETAL: libclang_rt.builtins.a
5959

6060
// RUN: %clang -rtlib=compiler-rt -print-libgcc-file-name \
6161
// RUN: --target=armv7m-vendor-none-eabi \

0 commit comments

Comments
 (0)