[clang] b9ee2ac - [LinkerWrapper] report on missing libraries
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 2 13:28:16 PDT 2022
Author: Joseph Huber
Date: 2022-11-02T15:28:09-05:00
New Revision: b9ee2acc9c5c129bce5503dd8d2ab4757e42c990
URL: https://github.com/llvm/llvm-project/commit/b9ee2acc9c5c129bce5503dd8d2ab4757e42c990
DIFF: https://github.com/llvm/llvm-project/commit/b9ee2acc9c5c129bce5503dd8d2ab4757e42c990.diff
LOG: [LinkerWrapper] report on missing libraries
The linker wrapper does its own library searching for static archives
that can contain device code. The device linking phases happen before
the host linking phases so that we can generate the necessary
registration code and link it in with the rest of the code. Previously,
If a library containing needed device code was not found the execution
would continue silently until it failed with undefined symbols. This
patch allows the linker wrapper to perform its own check beforehand to
catch these errors.
Reviewed By: jdoerfert
Differential Revision: https://reviews.llvm.org/D137180
Added:
Modified:
clang/test/Driver/linker-wrapper-image.c
clang/test/Driver/linker-wrapper.c
clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Removed:
################################################################################
diff --git a/clang/test/Driver/linker-wrapper-image.c b/clang/test/Driver/linker-wrapper-image.c
index a54ba4d19122..abf41d386d2d 100644
--- a/clang/test/Driver/linker-wrapper-image.c
+++ b/clang/test/Driver/linker-wrapper-image.c
@@ -115,7 +115,7 @@
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o \
// RUN: -fembed-offload-object=%t.out
// RUN: clang-linker-wrapper --print-wrapped-module --dry-run --host-triple=x86_64-unknown-linux-gnu \
-// RUN: -linker-path /usr/bin/ld -- %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=HIP
+// RUN: --linker-path=/usr/bin/ld -- %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=HIP
// HIP: @.fatbin_image = internal constant [0 x i8] zeroinitializer, section ".hip_fatbin"
// HIP-NEXT: @.fatbin_wrapper = internal constant %fatbin_wrapper { i32 1212764230, i32 1, ptr @.fatbin_image, ptr null }, section ".hipFatBinSegment", align 8
diff --git a/clang/test/Driver/linker-wrapper.c b/clang/test/Driver/linker-wrapper.c
index 51f3ea3bc545..b2d73f362108 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -115,8 +115,8 @@
// RUN: --image=file=%S/Inputs/dummy-elf.o,kind=hip,triple=amdgcn-amd-amdhsa,arch=gfx908
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o \
// RUN: -fembed-offload-object=%t.out
-// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu -linker-path \
-// RUN: /usr/bin/ld -- %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=HIP
+// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu \
+// RUN: --linker-path=/usr/bin/ld -- %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=HIP
// HIP: lld{{.*}}-flavor gnu --no-undefined -shared -plugin-opt=-amdgpu-internalize-symbols -plugin-opt=mcpu=gfx908 -o {{.*}}.out {{.*}}.o
// HIP: lld{{.*}}-flavor gnu --no-undefined -shared -plugin-opt=-amdgpu-internalize-symbols -plugin-opt=mcpu=gfx90a -o {{.*}}.out {{.*}}.o
@@ -134,6 +134,12 @@
// LINKER_ARGS: lld{{.*}}-flavor gnu --no-undefined -shared -plugin-opt=-amdgpu-internalize-symbols -plugin-opt=mcpu=gfx908 -o {{.*}}.out {{.*}}.o a
// LINKER_ARGS: nvlink{{.*}}-m64 -o {{.*}}.out -arch sm_70 {{.*}}.o a b
+// RUN: not clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu -ldummy \
+// RUN: --linker-path=/usr/bin/ld --device-linker=a --device-linker=nvptx64-nvidia-cuda=b -- \
+// RUN: -o a.out 2>&1 | FileCheck %s --check-prefix=MISSING-LIBRARY
+
+// MISSING-LIBRARY: error: unable to find library -ldummy
+
/// Ensure that temp files aren't leftoever from static libraries.
// RUN: clang-offload-packager -o %t-lib.out \
// RUN: --image=file=%S/Inputs/dummy-elf.o,kind=openmp,triple=nvptx64-nvidia-cuda,arch=sm_70 \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 40825ac831a5..3ad22be755f3 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -1219,7 +1219,7 @@ Optional<std::string> searchLibraryBaseName(StringRef Name, StringRef Root,
ArrayRef<StringRef> SearchPaths) {
for (StringRef Dir : SearchPaths) {
if (Optional<std::string> File = findFile(Dir, Root, "lib" + Name + ".so"))
- return None;
+ return File;
if (Optional<std::string> File = findFile(Dir, Root, "lib" + Name + ".a"))
return File;
}
@@ -1266,7 +1266,7 @@ Expected<SmallVector<OffloadFile>> getDeviceInput(const ArgList &Args) {
return std::move(Err);
}
- // Try to extract input from input libraries.
+ // Try to extract input from input archive libraries.
for (const opt::Arg *Arg : Args.filtered(OPT_library)) {
if (auto Library = searchLibrary(Arg->getValue(), Root, LibraryPaths)) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
@@ -1274,8 +1274,15 @@ Expected<SmallVector<OffloadFile>> getDeviceInput(const ArgList &Args) {
if (std::error_code EC = BufferOrErr.getError())
reportError(createFileError(*Library, EC));
+ if (identify_magic((*BufferOrErr)->getBuffer()) != file_magic::archive)
+ continue;
+
if (Error Err = extractOffloadBinaries(**BufferOrErr, LazyInputFiles))
return std::move(Err);
+ } else {
+ reportError(createStringError(inconvertibleErrorCode(),
+ "unable to find library -l%s",
+ Arg->getValue()));
}
}
More information about the cfe-commits
mailing list