[clang] [ClangLinkerWrapper] Forward --allow-multiple-definition to linker. (PR #182282)
Abid Qadeer via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 19 06:02:48 PST 2026
https://github.com/abidh created https://github.com/llvm/llvm-project/pull/182282
The clang-linker-wrapper ignores `--allow-multiple-definition` option which could cause a build (with --offload-arch) to fail with duplicate symbols error. Same build will pass without --offload-arch= because this option reaches the linker. This PR fixes the issue by forwarding the option so that it reaches the linker.
>From 83c3ddfd92ac0e5a0e06b79ccdb146b2e886d3aa Mon Sep 17 00:00:00 2001
From: Abid Qadeer <haqadeer at amd.com>
Date: Thu, 19 Feb 2026 13:44:46 +0000
Subject: [PATCH] [ClangLinkerWrapper] Forward --allow-multiple-definition to
linker.
The clang-linker-wrapper would ignore this option which could cause a
build (with --offload-arch) to fail with duplicate symbols error.
---
...linker-wrapper-allow-multiple-definition.c | 19 +++++++++++++++++++
.../ClangLinkerWrapper.cpp | 3 +++
.../clang-linker-wrapper/LinkerWrapperOpts.td | 2 ++
3 files changed, 24 insertions(+)
create mode 100644 clang/test/Driver/linker-wrapper-allow-multiple-definition.c
diff --git a/clang/test/Driver/linker-wrapper-allow-multiple-definition.c b/clang/test/Driver/linker-wrapper-allow-multiple-definition.c
new file mode 100644
index 0000000000000..5472c8289ab7e
--- /dev/null
+++ b/clang/test/Driver/linker-wrapper-allow-multiple-definition.c
@@ -0,0 +1,19 @@
+// UNSUPPORTED: system-windows
+// REQUIRES: x86-registered-target
+// REQUIRES: amdgpu-registered-target
+
+// An externally visible variable so static libraries extract.
+__attribute__((visibility("protected"), used)) int x;
+
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.elf.o
+// RUN: %clang -cc1 %s -triple amdgcn-amd-amdhsa -emit-llvm-bc -o %t.amdgpu.bc
+
+// RUN: llvm-offload-binary -o %t.out \
+// RUN: --image=file=%t.amdgpu.bc,kind=openmp,triple=amdgcn-amd-amdhsa,arch=gfx908
+// RUN: %clang -cc1 %s -triple x86_64-unknown-linux-gnu -emit-obj -o %t.o -fembed-offload-object=%t.out
+// RUN: clang-linker-wrapper --host-triple=x86_64-unknown-linux-gnu --dry-run \
+// RUN: --linker-path=/usr/bin/ld.lld --allow-multiple-definition \
+// RUN: %t.o -o a.out 2>&1 | FileCheck %s
+
+// CHECK: clang{{.*}} -Wl,--allow-multiple-definition
+// CHECK: /usr/bin/ld.lld{{.*}} --allow-multiple-definition
diff --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 619e539857fc6..b6836c809d773 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -574,6 +574,9 @@ Expected<StringRef> clang(ArrayRef<StringRef> InputFiles, const ArgList &Args,
if (Args.hasArg(OPT_embed_bitcode))
CmdArgs.push_back("-Wl,--lto-emit-llvm");
+ if (Args.hasArg(OPT_allow_multiple_definition))
+ CmdArgs.push_back("-Wl,--allow-multiple-definition");
+
// For linking device code with the SYCL offload kind, special handling is
// required. Passing --sycl-link to clang results in a call to
// clang-sycl-linker. Additional linker flags required by clang-sycl-linker
diff --git a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
index ef3a16b2f58bb..e6de40e194b28 100644
--- a/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
+++ b/clang/tools/clang-linker-wrapper/LinkerWrapperOpts.td
@@ -34,6 +34,8 @@ def verbose : Flag<["--"], "wrapper-verbose">,
def embed_bitcode : Flag<["--"], "embed-bitcode">,
Flags<[WrapperOnlyOption]>,
HelpText<"Embed linked bitcode in the module">;
+def allow_multiple_definition : Flag<["--"], "allow-multiple-definition">,
+ HelpText<"Allow multiple definitions">;
def print_wrapped_module : Flag<["--"], "print-wrapped-module">,
Flags<[WrapperOnlyOption]>,
HelpText<"Print the wrapped module's IR for testing">;
More information about the cfe-commits
mailing list