[clang] [LinkerWrapper] Forward more arguments to the CPU offloading linker (PR #75757)

2023-12-18 Thread Ye Luo via cfe-commits

https://github.com/ye-luo approved this pull request.


https://github.com/llvm/llvm-project/pull/75757
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LinkerWrapper] Forward more arguments to the CPU offloading linker (PR #75757)

2023-12-18 Thread Ye Luo via cfe-commits

https://github.com/ye-luo closed https://github.com/llvm/llvm-project/pull/75757
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LinkerWrapper] Forward more arguments to the CPU offloading linker (PR #75757)

2023-12-17 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Joseph Huber (jhuber6)


Changes

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: https://github.com/llvm/llvm-project/issues/75651


---
Full diff: https://github.com/llvm/llvm-project/pull/75757.diff


2 Files Affected:

- (modified) clang/test/Driver/linker-wrapper.c (+4-2) 
- (modified) clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp (+25-5) 


``diff
diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index b763a003452ba7..e51c5ea381d31a 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -49,10 +49,12 @@
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o 
-fembed-offload-object=%t.out
+// RUN: llvm-ar rcs %t.a %t.o
 // RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
-// RUN:   --linker-path=/usr/bin/ld.lld -- %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=CPU-LINK
+// RUN:   --linker-path=/usr/bin/ld.lld -- --whole-archive %t.a 
--no-whole-archive \
+// RUN:   %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CPU-LINK
 
-// CPU-LINK: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu 
-march=native -O2 -Wl,--no-undefined {{.*}}.o {{.*}}.o -Wl,-Bsymbolic -shared
+// 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
 
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o
 // RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu 
-mllvm -openmp-opt-disable \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index bebe76355eb46f..122ba1998eb83f 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -396,11 +396,31 @@ Expected clang(ArrayRef InputFiles, 
const ArgList &Args) {
 CmdArgs.push_back("-Wl,-Bsymbolic");
 CmdArgs.push_back("-shared");
 ArgStringList LinkerArgs;
-for (const opt::Arg *Arg : Args.filtered(OPT_library, OPT_library_path))
-  Arg->render(Args, LinkerArgs);
-for (const opt::Arg *Arg : Args.filtered(OPT_rpath))
-  LinkerArgs.push_back(
-  Args.MakeArgString("-Wl,-rpath," + StringRef(Arg->getValue(;
+for (const opt::Arg *Arg :
+ Args.filtered(OPT_INPUT, OPT_library, OPT_library_path, OPT_rpath,
+   OPT_whole_archive, OPT_no_whole_archive)) {
+  // Sometimes needed libraries are passed by name, such as when using
+  // sanitizers. We need to check the file magic for any libraries.
+  if (Arg->getOption().matches(OPT_INPUT)) {
+if (!sys::fs::exists(Arg->getValue()) ||
+sys::fs::is_directory(Arg->getValue()))
+  continue;
+
+file_magic Magic;
+if (auto EC = identify_magic(Arg->getValue(), Magic))
+  return createStringError(inconvertibleErrorCode(),
+   "Failed to open %s", Arg->getValue());
+if (Magic != file_magic::archive &&
+Magic != file_magic::elf_shared_object)
+  continue;
+  }
+  if (Arg->getOption().matches(OPT_whole_archive))
+LinkerArgs.push_back(Args.MakeArgString("-Wl,--whole-archive"));
+  else if (Arg->getOption().matches(OPT_no_whole_archive))
+LinkerArgs.push_back(Args.MakeArgString("-Wl,--no-whole-archive"));
+  else
+Arg->render(Args, LinkerArgs);
+}
 llvm::copy(LinkerArgs, std::back_inserter(CmdArgs));
   }
 

``




https://github.com/llvm/llvm-project/pull/75757
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [LinkerWrapper] Forward more arguments to the CPU offloading linker (PR #75757)

2023-12-17 Thread Joseph Huber via cfe-commits

https://github.com/jhuber6 created 
https://github.com/llvm/llvm-project/pull/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: https://github.com/llvm/llvm-project/issues/75651


>From 0ba0fa00af551bf8d9f69bec5742bbe4e12a4b58 Mon Sep 17 00:00:00 2001
From: Joseph Huber 
Date: Sun, 17 Dec 2023 18:24:46 -0600
Subject: [PATCH] [LinkerWrapper] Forward more arguments to the CPU offloading
 linker

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: https://github.com/llvm/llvm-project/issues/75651
---
 clang/test/Driver/linker-wrapper.c|  6 ++--
 .../ClangLinkerWrapper.cpp| 30 +++
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/clang/test/Driver/linker-wrapper.c 
b/clang/test/Driver/linker-wrapper.c
index b763a003452ba7..e51c5ea381d31a 100644
--- a/clang/test/Driver/linker-wrapper.c
+++ b/clang/test/Driver/linker-wrapper.c
@@ -49,10 +49,12 @@
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu \
 // RUN:   --image=file=%t.elf.o,kind=openmp,triple=x86_64-unknown-linux-gnu
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o 
-fembed-offload-object=%t.out
+// RUN: llvm-ar rcs %t.a %t.o
 // RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
-// RUN:   --linker-path=/usr/bin/ld.lld -- %t.o -o a.out 2>&1 | FileCheck %s 
--check-prefix=CPU-LINK
+// RUN:   --linker-path=/usr/bin/ld.lld -- --whole-archive %t.a 
--no-whole-archive \
+// RUN:   %t.o -o a.out 2>&1 | FileCheck %s --check-prefix=CPU-LINK
 
-// CPU-LINK: clang{{.*}} -o {{.*}}.img --target=x86_64-unknown-linux-gnu 
-march=native -O2 -Wl,--no-undefined {{.*}}.o {{.*}}.o -Wl,-Bsymbolic -shared
+// 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
 
 // RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o
 // RUN: clang-linker-wrapper --dry-run --host-triple=x86_64-unknown-linux-gnu 
-mllvm -openmp-opt-disable \
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp 
b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index bebe76355eb46f..122ba1998eb83f 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -396,11 +396,31 @@ Expected clang(ArrayRef InputFiles, 
const ArgList &Args) {
 CmdArgs.push_back("-Wl,-Bsymbolic");
 CmdArgs.push_back("-shared");
 ArgStringList LinkerArgs;
-for (const opt::Arg *Arg : Args.filtered(OPT_library, OPT_library_path))
-  Arg->render(Args, LinkerArgs);
-for (const opt::Arg *Arg : Args.filtered(OPT_rpath))
-  LinkerArgs.push_back(
-  Args.MakeArgString("-Wl,-rpath," + StringRef(Arg->getValue(;
+for (const opt::Arg *Arg :
+ Args.filtered(OPT_INPUT, OPT_library, OPT_library_path, OPT_rpath,
+   OPT_whole_archive, OPT_no_whole_archive)) {
+  // Sometimes needed libraries are passed by name, such as when using
+  // sanitizers. We need to check the file magic for any libraries.
+  if (Arg->getOption().matches(OPT_INPUT)) {
+if (!sys::fs::exists(Arg->getValue()) ||
+sys::fs::is_directory(Arg->getValue()))
+  continue;
+
+file_magic Magic;
+if (auto EC = identify_magic(Arg->getValue(), Magic))
+  return createStringError(inconvertibleErrorCode(),
+   "Failed to open %s", Arg->getValue());
+if (Magic != file_magic::archive &&
+Magic != file_magic::elf_shared_object)
+  continue;
+  }
+  if (Arg->getOption().matches(OPT_whole_archive))
+LinkerArgs.push_back(Args.MakeArgString("-Wl,--whole-archive"));
+  else if (Arg->getOption().matches(OPT_no_whole_archive))
+LinkerArgs.push_back(Args.MakeArgString("-Wl,--no-whole-archive"));
+  else
+Arg->render(Args, LinkerArgs);
+}
 llvm::copy(LinkerArgs, std::back_inserter(CmdArgs));
   }
 

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https:/