[PATCH] D73139: [LLD][COFF] Enable linking of __declspec(selectany) symbols from Clang and GCC

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 23 00:58:13 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG9dbc1ab23268: [LLD][COFF] Enable linking of __declspec(selectany) symbols from Clang and GCC (authored by zero9178, committed by mstorsjo).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D73139/new/

https://reviews.llvm.org/D73139

Files:
  lld/COFF/InputFiles.cpp
  lld/test/COFF/comdat-gcc-compatibility.s


Index: lld/test/COFF/comdat-gcc-compatibility.s
===================================================================
--- /dev/null
+++ lld/test/COFF/comdat-gcc-compatibility.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+# RUN: llvm-mc %s -triple x86_64-pc-win32 -defsym obj=0 -filetype=obj -o %t1.obj
+# RUN: llvm-mc %s -triple x86_64-pc-win32 -defsym obj=1 -filetype=obj -o %t2.obj
+# RUN: lld-link /lldmingw /noentry /dll %t1.obj %t2.obj /out:%t3.dll
+# RUN: not lld-link /noentry /dll %t1.obj %t2.obj /out:%t3.dll
+.if obj==0
+        .section .text$nm, "", discard, symbol
+.else
+        .section .text$nm, "", same_size, symbol
+.endif
+        .globl symbol
+symbol:
+        .long 1
Index: lld/COFF/InputFiles.cpp
===================================================================
--- lld/COFF/InputFiles.cpp
+++ lld/COFF/InputFiles.cpp
@@ -500,6 +500,17 @@
     leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
   }
 
+  // GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
+  // Clang on the other hand picks "any". To be able to link two object files
+  // with a __declspec(selectany) declaration, one compiled with gcc and the
+  // other with clang, we merge them as proper "same size as"
+  if (config->mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
+                         leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
+                        (selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
+                         leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
+    leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
+  }
+
   // Other than that, comdat selections must match.  This is a bit more
   // strict than link.exe which allows merging "any" and "largest" if "any"
   // is the first symbol the linker sees, and it allows merging "largest"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73139.239803.patch
Type: text/x-patch
Size: 1818 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200123/e3e08be5/attachment.bin>


More information about the llvm-commits mailing list