Skip to content

Commit 9dbc1ab

Browse files
zero9178mstorsjo
authored andcommitted
[LLD][COFF] Enable linking of __declspec(selectany) symbols from Clang and GCC
When annotating a symbol with __declspec(selectany), Clang assigns it comdat 2 while GCC assigns it comdat 3. This patch enables two object files that contain a __declspec(selectany) symbol, one created by gcc and the other by clang, to be linked together instead of issuing a duplicate symbol error. Differential Revision: https://reviews.llvm.org/D73139
1 parent 4a8dbc6 commit 9dbc1ab

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ void ObjFile::handleComdatSelection(COFFSymbolRef sym, COMDATType &selection,
500500
leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
501501
}
502502

503+
// GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
504+
// Clang on the other hand picks "any". To be able to link two object files
505+
// with a __declspec(selectany) declaration, one compiled with gcc and the
506+
// other with clang, we merge them as proper "same size as"
507+
if (config->mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
508+
leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
509+
(selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
510+
leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
511+
leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
512+
}
513+
503514
// Other than that, comdat selections must match. This is a bit more
504515
// strict than link.exe which allows merging "any" and "largest" if "any"
505516
// is the first symbol the linker sees, and it allows merging "largest"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc %s -triple x86_64-pc-win32 -defsym obj=0 -filetype=obj -o %t1.obj
3+
# RUN: llvm-mc %s -triple x86_64-pc-win32 -defsym obj=1 -filetype=obj -o %t2.obj
4+
# RUN: lld-link /lldmingw /noentry /dll %t1.obj %t2.obj /out:%t3.dll
5+
# RUN: not lld-link /noentry /dll %t1.obj %t2.obj /out:%t3.dll
6+
.if obj==0
7+
.section .text$nm, "", discard, symbol
8+
.else
9+
.section .text$nm, "", same_size, symbol
10+
.endif
11+
.globl symbol
12+
symbol:
13+
.long 1

0 commit comments

Comments
 (0)