]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
MinGW: Reject dll{im,ex}port attribute on TLS variables
authorEric Botcazou <ebotcazou@adacore.com>
Mon, 9 Feb 2026 18:12:28 +0000 (19:12 +0100)
committerEric Botcazou <ebotcazou@adacore.com>
Mon, 9 Feb 2026 18:14:13 +0000 (19:14 +0100)
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.

gcc/attribs.cc
gcc/testsuite/c-c++-common/tls-attr-dll.c [new file with mode: 0644]

index 1205b2fc1dfb4ec16c380175c3c959b6b36cf52a..c63d13d7e550c4b142d090bf7a0506ed4215af21 100644 (file)
@@ -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 (file)
index 0000000..4f1f596
--- /dev/null
@@ -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" } */