From: Eric Botcazou Date: Mon, 9 Feb 2026 18:12:28 +0000 (+0100) Subject: MinGW: Reject dll{im,ex}port attribute on TLS variables X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf0fd736a96fc8560d44f808bb1c27c13e99fa78;p=thirdparty%2Fgcc.git 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 --git a/gcc/attribs.cc b/gcc/attribs.cc index 1205b2fc1df..c63d13d7e55 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 00000000000..4f1f596646d --- /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" } */