Skip to content

Commit d650dd7

Browse files
mstorsjohamishknight
authored andcommitted
[LLD] [COFF] Add a separate option for allowing duplicate weak symbols (llvm#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 4523e78 commit d650dd7

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
@@ -313,6 +313,7 @@ struct Configuration {
313313
bool pseudoRelocs = false;
314314
bool stdcallFixup = false;
315315
bool writeCheckSum = false;
316+
bool allowDuplicateWeak = false;
316317
};
317318

318319
} // namespace lld::coff

lld/COFF/Driver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
19801980
config->stdcallFixup =
19811981
args.hasFlag(OPT_stdcall_fixup, OPT_stdcall_fixup_no, config->mingw);
19821982
config->warnStdcallFixup = !args.hasArg(OPT_stdcall_fixup);
1983+
config->allowDuplicateWeak =
1984+
args.hasFlag(OPT_lld_allow_duplicate_weak,
1985+
OPT_lld_allow_duplicate_weak_no, config->mingw);
19831986

19841987
if (args.hasFlag(OPT_inferasanlibs, OPT_inferasanlibs_no, false))
19851988
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 lldmingw : F<"lldmingw">;
236237
def noseh : F<"noseh">;
237238
def osversion : P_priv<"osversion">;

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)