[lld] a67ae8c - [LLD] [COFF] Add a separate option for allowing duplicate weak symbols (#68077)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 20 13:44:49 PDT 2023


Author: Martin Storsjö
Date: 2023-10-20T23:44:44+03:00
New Revision: a67ae8c0fd301a11e2a058e8035304cfc70a3e91

URL: https://github.com/llvm/llvm-project/commit/a67ae8c0fd301a11e2a058e8035304cfc70a3e91
DIFF: https://github.com/llvm/llvm-project/commit/a67ae8c0fd301a11e2a058e8035304cfc70a3e91.diff

LOG: [LLD] [COFF] Add a separate option for allowing duplicate weak symbols (#68077)

The MinGW mode (enabled with the flag -lldmingw) does allow duplicate
weak symbols. A test in
compiler-rt/test/profile/Windows/coverage-weak-lld.cpp does currently
enable the -lldmingw flag in an MSVC context, in order to deal with
duplicate weak symbols.

Add a new, separate, lld specific flag for enabling this. In MinGW mode,
this is enabled by default, otherwise it is disabled.

This allows making the MinGW mode more restrictive in adding libpaths
from the surrounding environment; in MinGW mode, all libpaths are passed
explicitly by the compiler driver to the linker, which is attempted in
https://reviews.llvm.org/D144084.

Added: 
    

Modified: 
    lld/COFF/Config.h
    lld/COFF/Driver.cpp
    lld/COFF/InputFiles.cpp
    lld/COFF/Options.td
    lld/test/COFF/gnu-weak.test

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Config.h b/lld/COFF/Config.h
index 2d5d4d63d69fa4a..1c338cc63fa87d2 100644
--- a/lld/COFF/Config.h
+++ b/lld/COFF/Config.h
@@ -316,6 +316,7 @@ struct Configuration {
   bool stdcallFixup = false;
   bool writeCheckSum = false;
   EmitKind emit = EmitKind::Obj;
+  bool allowDuplicateWeak = false;
 };
 
 } // namespace lld::coff

diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 0fbfefdf43cf1ad..1d9a7e498f151f0 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2028,6 +2028,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   config->stdcallFixup =
       args.hasFlag(OPT_stdcall_fixup, OPT_stdcall_fixup_no, config->mingw);
   config->warnStdcallFixup = !args.hasArg(OPT_stdcall_fixup);
+  config->allowDuplicateWeak =
+      args.hasFlag(OPT_lld_allow_duplicate_weak,
+                   OPT_lld_allow_duplicate_weak_no, config->mingw);
 
   if (args.hasFlag(OPT_inferasanlibs, OPT_inferasanlibs_no, false))
     warn("ignoring '/inferasanlibs', this flag is not supported");

diff  --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index b66ef418b3039fa..38ce29e6ab68c04 100644
--- a/lld/COFF/InputFiles.cpp
+++ b/lld/COFF/InputFiles.cpp
@@ -81,7 +81,7 @@ static void checkAndSetWeakAlias(COFFLinkerContext &ctx, InputFile *f,
       // of another symbol emitted near the weak symbol.
       // Just use the definition from the first object file that defined
       // this weak symbol.
-      if (ctx.config.mingw)
+      if (ctx.config.allowDuplicateWeak)
         return;
       ctx.symtab.reportDuplicate(source, f);
     }

diff  --git a/lld/COFF/Options.td b/lld/COFF/Options.td
index e7bb952808863be..977657a433dc581 100644
--- a/lld/COFF/Options.td
+++ b/lld/COFF/Options.td
@@ -232,6 +232,7 @@ defm demangle : B<"demangle",
 def include_optional : Joined<["/", "-", "/?", "-?"], "includeoptional:">,
     HelpText<"Add symbol as undefined, but allow it to remain undefined">;
 def kill_at : F<"kill-at">;
+defm lld_allow_duplicate_weak : B_priv<"lld-allow-duplicate-weak">;
 def lldemit : P<"lldemit", "Specify output type">;
 def lldmingw : F<"lldmingw">;
 def noseh : F<"noseh">;

diff  --git a/lld/test/COFF/gnu-weak.test b/lld/test/COFF/gnu-weak.test
index 20284d7f02436e3..08e59734e042472 100644
--- a/lld/test/COFF/gnu-weak.test
+++ b/lld/test/COFF/gnu-weak.test
@@ -1,4 +1,9 @@
 RUN: lld-link -lldmingw %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe
+RUN: lld-link -lld-allow-duplicate-weak %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe
+RUN: not lld-link %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe 2>&1 | FileCheck %s --check-prefix=DEFAULT-ERROR
+
+DEFAULT-ERROR: error: duplicate symbol: weakfunc
+
 
 GNU ld can handle several definitions of the same weak symbol, and
 unless there is a strong definition of it, it just picks the first


        


More information about the llvm-commits mailing list