Skip to content

Commit a67ae8c

Browse files
authored
[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.
1 parent c5dacb6 commit a67ae8c

File tree

5 files changed

+11
-1
lines changed

5 files changed

+11
-1
lines changed

lld/COFF/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ struct Configuration {
316316
bool stdcallFixup = false;
317317
bool writeCheckSum = false;
318318
EmitKind emit = EmitKind::Obj;
319+
bool allowDuplicateWeak = false;
319320
};
320321

321322
} // namespace lld::coff

lld/COFF/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
20282028
config->stdcallFixup =
20292029
args.hasFlag(OPT_stdcall_fixup, OPT_stdcall_fixup_no, config->mingw);
20302030
config->warnStdcallFixup = !args.hasArg(OPT_stdcall_fixup);
2031+
config->allowDuplicateWeak =
2032+
args.hasFlag(OPT_lld_allow_duplicate_weak,
2033+
OPT_lld_allow_duplicate_weak_no, config->mingw);
20312034

20322035
if (args.hasFlag(OPT_inferasanlibs, OPT_inferasanlibs_no, false))
20332036
warn("ignoring '/inferasanlibs', this flag is not supported");

lld/COFF/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void checkAndSetWeakAlias(COFFLinkerContext &ctx, InputFile *f,
8181
// of another symbol emitted near the weak symbol.
8282
// Just use the definition from the first object file that defined
8383
// this weak symbol.
84-
if (ctx.config.mingw)
84+
if (ctx.config.allowDuplicateWeak)
8585
return;
8686
ctx.symtab.reportDuplicate(source, f);
8787
}

lld/COFF/Options.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ defm demangle : B<"demangle",
232232
def include_optional : Joined<["/", "-", "/?", "-?"], "includeoptional:">,
233233
HelpText<"Add symbol as undefined, but allow it to remain undefined">;
234234
def kill_at : F<"kill-at">;
235+
defm lld_allow_duplicate_weak : B_priv<"lld-allow-duplicate-weak">;
235236
def lldemit : P<"lldemit", "Specify output type">;
236237
def lldmingw : F<"lldmingw">;
237238
def noseh : F<"noseh">;

lld/test/COFF/gnu-weak.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
RUN: lld-link -lldmingw %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe
2+
RUN: lld-link -lld-allow-duplicate-weak %S/Inputs/gnu-weak.o %S/Inputs/gnu-weak2.o -out:%t.exe
3+
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
4+
5+
DEFAULT-ERROR: error: duplicate symbol: weakfunc
6+
27

38
GNU ld can handle several definitions of the same weak symbol, and
49
unless there is a strong definition of it, it just picks the first

0 commit comments

Comments
 (0)