Skip to content

Commit 695680b

Browse files
committed
[LLD] [COFF] Add a separate option for allowing duplicate weak symbols
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 503bc5f commit 695680b

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
@@ -314,6 +314,7 @@ struct Configuration {
314314
bool stdcallFixup = false;
315315
bool writeCheckSum = false;
316316
EmitKind emit = EmitKind::Obj;
317+
bool allowDuplicateWeak = false;
317318
};
318319

319320
} // namespace lld::coff

lld/COFF/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
19931993
config->stdcallFixup =
19941994
args.hasFlag(OPT_stdcall_fixup, OPT_stdcall_fixup_no, config->mingw);
19951995
config->warnStdcallFixup = !args.hasArg(OPT_stdcall_fixup);
1996+
config->allowDuplicateWeak =
1997+
args.hasFlag(OPT_lld_allow_duplicate_weak,
1998+
OPT_lld_allow_duplicate_weak_no, config->mingw);
19961999

19972000
if (args.hasFlag(OPT_inferasanlibs, OPT_inferasanlibs_no, false))
19982001
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)