Skip to content

Commit 8e2cc19

Browse files
authored
[LinkerWrapper] Forward more arguments to the CPU offloading linker (#75757)
Summary: The CPU target currently inherits all the libraries from the normal link job to ensure that it has access to the same envrionment that the host does. However, this previously was not respecting argument libraries that are passed by name rather than `-l` as well as the whole archive flags. This patch fixes this to allow the CPU linker to correctly pick up the libraries associated with things like address sanitizers. Fixes: #75651
1 parent 245cdda commit 8e2cc19

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

clang/test/Driver/linker-wrapper.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
5050
// RUN: --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
5151
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out
52+
// RUN: llvm-ar rcs %t.a %t.o
5253
// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
53-
// RUN: --linker-path=/usr/bin/ld.lld -- %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CPU-LINK
54+
// RUN: --linker-path=/usr/bin/ld.lld -- --whole-archive %t.a --no-whole-archive \
55+
// RUN: %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CPU-LINK
5456

55-
// CPU-LINK: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu -march=native -O2 -Wl,--no-undefined {{.*}}.o {{.*}}.o -Wl,-Bsymbolic -shared
57+
// CPU-LINK: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu -march=native -O2 -Wl,--no-undefined {{.*}}.o {{.*}}.o -Wl,-Bsymbolic -shared -Wl,--whole-archive {{.*}}.a -Wl,--no-whole-archive
5658

5759
// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o
5860
// RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu -mllvm -openmp-opt-disable \

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,31 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args) {
396396
CmdArgs.push_back("-Wl,-Bsymbolic");
397397
CmdArgs.push_back("-shared");
398398
ArgStringList LinkerArgs;
399-
for (const opt::Arg *Arg : Args.filtered(OPT_library, OPT_library_path))
400-
Arg->render(Args, LinkerArgs);
401-
for (const opt::Arg *Arg : Args.filtered(OPT_rpath))
402-
LinkerArgs.push_back(
403-
Args.MakeArgString("-Wl,-rpath," + StringRef(Arg->getValue())));
399+
for (const opt::Arg *Arg :
400+
Args.filtered(OPT_INPUT, OPT_library, OPT_library_path, OPT_rpath,
401+
OPT_whole_archive, OPT_no_whole_archive)) {
402+
// Sometimes needed libraries are passed by name, such as when using
403+
// sanitizers. We need to check the file magic for any libraries.
404+
if (Arg->getOption().matches(OPT_INPUT)) {
405+
if (!sys::fs::exists(Arg->getValue()) ||
406+
sys::fs::is_directory(Arg->getValue()))
407+
continue;
408+
409+
file_magic Magic;
410+
if (auto EC = identify_magic(Arg->getValue(), Magic))
411+
return createStringError(inconvertibleErrorCode(),
412+
"Failed to open %s", Arg->getValue());
413+
if (Magic != file_magic::archive &&
414+
Magic != file_magic::elf_shared_object)
415+
continue;
416+
}
417+
if (Arg->getOption().matches(OPT_whole_archive))
418+
LinkerArgs.push_back(Args.MakeArgString("-Wl,--whole-archive"));
419+
else if (Arg->getOption().matches(OPT_no_whole_archive))
420+
LinkerArgs.push_back(Args.MakeArgString("-Wl,--no-whole-archive"));
421+
else
422+
Arg->render(Args, LinkerArgs);
423+
}
404424
llvm::copy(LinkerArgs, std::back_inserter(CmdArgs));
405425
}
406426

0 commit comments

Comments
 (0)