https://gcc.gnu.org/g:bf0fd736a96fc8560d44f808bb1c27c13e99fa78

commit r16-7416-gbf0fd736a96fc8560d44f808bb1c27c13e99fa78
Author: Eric Botcazou <[email protected]>
Date:   Mon Feb 9 19:12:28 2026 +0100

    MinGW: Reject dll{im,ex}port attribute on TLS variables
    
    The same error is given by Clang/LLVM.
    
    gcc/
            PR target/80881
            * attribs.cc (handle_dll_attribute): If TARGET_WIN32_TLS is 1,
            issue an error if either attribute is set on a TLS variable.
    
    gcc/testsuite/
            * c-c++-common/tls-attr-dll.c: New test.

Diff:
---
 gcc/attribs.cc                            | 17 +++++++++++++++--
 gcc/testsuite/c-c++-common/tls-attr-dll.c |  5 +++++
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/gcc/attribs.cc b/gcc/attribs.cc
index 1205b2fc1dfb..c63d13d7e550 100644
--- a/gcc/attribs.cc
+++ b/gcc/attribs.cc
@@ -2007,10 +2007,16 @@ handle_dll_attribute (tree * pnode, tree name, tree 
args, int flags,
        {
          if (DECL_INITIAL (node))
            {
-             error ("variable %q+D definition is marked dllimport",
-                    node);
+             error ("variable %q+D definition is marked dllimport", node);
              *no_add_attrs = true;
            }
+#if TARGET_WIN32_TLS
+         else if (DECL_THREAD_LOCAL_P (node))
+           {
+             error ("thread-local variable %q+D declared as dllimport", node);
+             *no_add_attrs = true;
+           }
+#endif
 
          /* `extern' needn't be specified with dllimport.
             Specify `extern' now and hope for the best.  Sigh.  */
@@ -2034,6 +2040,13 @@ handle_dll_attribute (tree * pnode, tree name, tree 
args, int flags,
           && flag_keep_inline_dllexport)
     /* An exported function, even if inline, must be emitted.  */
     DECL_EXTERNAL (node) = 0;
+#if TARGET_WIN32_TLS
+  else if (VAR_P (node) && DECL_THREAD_LOCAL_P (node))
+    {
+      error ("thread-local variable %q+D declared as dllexport", node);
+      *no_add_attrs = true;
+    }
+#endif
 
   /*  Report error if symbol is not accessible at global scope.  */
   if (!TREE_PUBLIC (node) && VAR_OR_FUNCTION_DECL_P (node))
diff --git a/gcc/testsuite/c-c++-common/tls-attr-dll.c 
b/gcc/testsuite/c-c++-common/tls-attr-dll.c
new file mode 100644
index 000000000000..4f1f596646d6
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/tls-attr-dll.c
@@ -0,0 +1,5 @@
+/* { dg-do compile { target i?86-*-mingw* x86_64-*-mingw* } } */
+
+__declspec (dllimport) __thread int foo1; /* { dg-error "thread-local 
variable" } */
+
+__declspec (dllexport) __thread int foo2; /* { dg-error "thread-local 
variable" } */

Reply via email to