https://gcc.gnu.org/g:2104f1a937eb8c7a701fb7338bee9bfb5f72f902

commit r17-6145-g2104f1a937eb8c7a701fb7338bee9bfb5f72f902
Author: Matt Turner <[email protected]>
Date:   Wed Oct 28 11:55:37 2026 -0400

    c-family: Recompute TLS model of weak variables [PR c/125418]
    
    Since r16-3673 (C) and r16-3637 (C++) the front ends upgrade a TLS
    variable's access model to decl_default_tls_model as soon as its
    declaration has been processed.  If the variable becomes weak only
    afterwards, the model is chosen while the symbol still appears to bind
    locally.  A hidden extern TLS variable that is then made weak keeps a
    model that assumes it is defined in the same module, which is wrong for
    an undefined weak symbol: with local-exec the linker has no TLS segment
    offset to use.  This breaks static links of glibc on ia64, and on alpha
    when the linker lacks the fix for PR ld/34165.  glibc declares its
    _nl_current_LC_* variables as
    
      extern __thread struct __locale_data *const *_nl_current_LC_CTYPE
        attribute_hidden attribute_tls_model_ie;
      #pragma weak _nl_current_LC_CTYPE
    
    There are two ways the variable becomes weak too late.  #pragma weak is
    applied after the declaration has been processed.  And a redeclaration
    has its model chosen on its own, before duplicate_decls merges it with
    an earlier weak declaration and merge_weak makes it weak, and that model
    then replaces the earlier declaration's.  The second happens with
    __attribute__((weak)) on the earlier declaration as well as with
    #pragma weak.
    
    Add weak_decl_tls_model, which recomputes the default model of a weak
    TLS variable and lowers the model if the new default is weaker.  An
    explicit tls_model attribute remains a lower bound, as it is when the
    declaration is first processed, so the example above ends up
    initial-exec rather than local-exec, and an explicit local-exec is left
    alone.  Call it from apply_pragma_weak, and from duplicate_decls in both
    front ends once a merged variable is weak.  To read the attribute, split
    the string matching out of handle_tls_model_attribute, and make that
    function drop an attribute whose argument is not a string once it has
    diagnosed it, so a tls_model attribute left on a declaration always has
    a string argument.
    
    apply_pragma_weak recomputes nothing for "#pragma weak sym = target".
    That makes the symbol an alias defined in this translation unit, so it
    binds no less locally than before, but decl_default_tls_model cannot see
    that yet and would treat it as weak undefined.  By the time a
    redeclaration is merged it can, so the alias keeps its model.  The test
    for this scans the early visibility dump, because the whole-program
    visibility pass upgrades the model of an attribute-free variable again
    and would hide the difference.
    
    gcc/c-family/ChangeLog:
    
            PR c/125418
            * c-attribs.cc (parse_tls_model): New function, split out of...
            (handle_tls_model_attribute): ...here.  Use it.  Do not add the
            attribute if its argument is not a string.
            (weak_decl_tls_model): New function.
            * c-common.h (parse_tls_model, weak_decl_tls_model): Declare.
            * c-pragma.cc (apply_pragma_weak): Call weak_decl_tls_model after
            declaring a symbol weak.
    
    gcc/c/ChangeLog:
    
            PR c/125418
            * c-decl.cc (merge_decls): Call weak_decl_tls_model for a weak
            thread-local variable.
    
    gcc/cp/ChangeLog:
    
            PR c/125418
            * decl.cc (duplicate_decls): Call weak_decl_tls_model for a weak
            thread-local variable.
    
    gcc/testsuite/ChangeLog:
    
            PR c/125418
            * c-c++-common/tls-pragma-weak-1.c: New test.
            * c-c++-common/tls-pragma-weak-2.c: New test.
            * c-c++-common/tls-pragma-weak-3.c: New test.
            * c-c++-common/tls-pragma-weak-4.c: New test.
            * gcc.dg/tls/pragma-weak-alias.c: New test.

Diff:
---
 gcc/c-family/c-attribs.cc                      | 57 +++++++++++++++++++++-----
 gcc/c-family/c-common.h                        |  2 +
 gcc/c-family/c-pragma.cc                       |  6 +++
 gcc/c/c-decl.cc                                |  8 +++-
 gcc/cp/decl.cc                                 |  8 ++++
 gcc/testsuite/c-c++-common/tls-pragma-weak-1.c | 22 ++++++++++
 gcc/testsuite/c-c++-common/tls-pragma-weak-2.c | 22 ++++++++++
 gcc/testsuite/c-c++-common/tls-pragma-weak-3.c | 18 ++++++++
 gcc/testsuite/c-c++-common/tls-pragma-weak-4.c | 40 ++++++++++++++++++
 gcc/testsuite/gcc.dg/tls/pragma-weak-alias.c   | 31 ++++++++++++++
 10 files changed, 202 insertions(+), 12 deletions(-)

diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index aab9f2cb9e3b..5d45a345d26c 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -3010,13 +3010,55 @@ handle_weakref_attribute (tree *node, tree name, tree 
args,
   return NULL_TREE;
 }
 
+/* If ID, the STRING_CST argument of a "tls_model" attribute, names a TLS
+   access model, store that model in *KIND and return true.  Otherwise
+   return false.  */
+
+bool
+parse_tls_model (const_tree id, enum tls_model *kind)
+{
+  if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
+    *kind = TLS_MODEL_LOCAL_EXEC;
+  else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
+    *kind = TLS_MODEL_INITIAL_EXEC;
+  else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
+    *kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
+  else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
+    *kind = TLS_MODEL_GLOBAL_DYNAMIC;
+  else
+    return false;
+
+  return true;
+}
+
+/* DECL has just become weak.  If it is a thread-local variable, the TLS
+   access model chosen for it may assume that it binds locally, which a
+   weak symbol need not, so recompute the model.  An explicit tls_model
+   attribute remains a lower bound.  */
+
+void
+weak_decl_tls_model (tree decl)
+{
+  if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
+    return;
+
+  tls_model model = decl_default_tls_model (decl);
+  tree attr = lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl));
+  tls_model attr_model;
+  if (attr
+      && parse_tls_model (TREE_VALUE (TREE_VALUE (attr)), &attr_model)
+      && attr_model > model)
+    model = attr_model;
+  if (model < DECL_TLS_MODEL (decl))
+    set_decl_tls_model (decl, model);
+}
+
 /* Handle an "tls_model" attribute; arguments as in
    struct attribute_spec.handler.  */
 
 static tree
 handle_tls_model_attribute (tree *node, tree name, tree args,
-                           int ARG_UNUSED (flags),
-                           bool *ARG_UNUSED (no_add_attrs))
+                           int ARG_UNUSED (flags), bool *no_add_attrs)
 {
   tree id;
   tree decl = *node;
@@ -3042,18 +3084,11 @@ handle_tls_model_attribute (tree *node, tree name, tree 
args,
   if (TREE_CODE (id) != STRING_CST)
     {
       error ("%qE argument not a string", name);
+      *no_add_attrs = true;
       return NULL_TREE;
     }
 
-  if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
-    kind = TLS_MODEL_LOCAL_EXEC;
-  else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
-    kind = TLS_MODEL_INITIAL_EXEC;
-  else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
-    kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
-  else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
-    kind = TLS_MODEL_GLOBAL_DYNAMIC;
-  else
+  if (!parse_tls_model (id, &kind))
     error ("%qE argument must be one of %qs, %qs, %qs, or %qs",
           name,
           "local-exec", "initial-exec", "local-dynamic", "global-dynamic");
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index 1eebd0430e79..c572b637a949 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1693,6 +1693,8 @@ extern void check_for_xor_used_as_pow (location_t 
lhs_loc, tree lhs_val,
 
 /* In c-attribs.cc.  */
 extern bool attribute_takes_identifier_p (const_tree);
+extern bool parse_tls_model (const_tree, enum tls_model *);
+extern void weak_decl_tls_model (tree);
 extern tree handle_deprecated_attribute (tree *, tree, tree, int, bool *);
 extern tree handle_unused_attribute (tree *, tree, tree, int, bool *);
 extern tree handle_fallthrough_attribute (tree *, tree, tree, int, bool *);
diff --git a/gcc/c-family/c-pragma.cc b/gcc/c-family/c-pragma.cc
index 38fd14ad8089..e06bb70a5c16 100644
--- a/gcc/c-family/c-pragma.cc
+++ b/gcc/c-family/c-pragma.cc
@@ -270,6 +270,12 @@ apply_pragma_weak (tree decl, tree value)
             "results in unspecified behavior", decl);
 
   declare_weak (decl);
+
+  /* With VALUE, DECL is now an alias defined in this translation unit,
+     which binds no less locally than the declaration its TLS model was
+     chosen for, but decl_default_tls_model cannot see that yet.  */
+  if (!value)
+    weak_decl_tls_model (decl);
 }
 
 void
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 22eaa13d7ebd..51555f33822a 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -3198,7 +3198,13 @@ merge_decls (tree newdecl, tree olddecl, tree newtype, 
tree oldtype)
                extern int __thread x;
             as we'll lose the "local-exec" model.  */
          if (VAR_P (olddecl) && DECL_THREAD_LOCAL_P (newdecl))
-           set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
+           {
+             set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl));
+             /* NEWDECL's model was chosen for NEWDECL alone, which may not
+                have been weak until merge_weak.  */
+             if (DECL_WEAK (olddecl))
+               weak_decl_tls_model (olddecl);
+           }
          break;
        }
 
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 90604623fd98..a53ae4a00a71 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -3594,6 +3594,14 @@ duplicate_decls (tree newdecl, tree olddecl, bool 
hiding, bool was_hidden)
      Update OLDDECL to be the same.  */
   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
 
+  /* NEWDECL's TLS model was chosen for NEWDECL alone, which may not have
+     been weak until merge_weak.  */
+  if (VAR_P (olddecl)
+      && CP_DECL_THREAD_LOCAL_P (olddecl)
+      && DECL_WEAK (olddecl)
+      && !processing_template_decl)
+    weak_decl_tls_model (olddecl);
+
   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
     so that encode_section_info has a chance to look at the new decl
     flags and attributes.  */
diff --git a/gcc/testsuite/c-c++-common/tls-pragma-weak-1.c 
b/gcc/testsuite/c-c++-common/tls-pragma-weak-1.c
new file mode 100644
index 000000000000..884f64db5c3a
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/tls-pragma-weak-1.c
@@ -0,0 +1,22 @@
+/* PR c/125418 */
+/* { dg-do compile { target tls_native } } */
+/* { dg-require-weak "" } */
+/* { dg-require-visibility "" } */
+/* { dg-options "-O2 -fdump-ipa-whole-program" } */
+
+/* A hidden TLS variable made weak by #pragma weak may be undefined, so it
+   does not bind locally and must not get a local TLS access model, whether
+   the pragma follows or precedes the declaration.  */
+
+__attribute__((visibility("hidden")))
+extern __thread int t1;
+#pragma weak t1
+
+#pragma weak t2
+__attribute__((visibility("hidden")))
+extern __thread int t2;
+
+int *f1 (void) { return &t1; }
+int *f2 (void) { return &t2; }
+
+/* { dg-final { scan-ipa-dump-not "Varpool flags: tls-local" "whole-program" } 
} */
diff --git a/gcc/testsuite/c-c++-common/tls-pragma-weak-2.c 
b/gcc/testsuite/c-c++-common/tls-pragma-weak-2.c
new file mode 100644
index 000000000000..2c01f1435c15
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/tls-pragma-weak-2.c
@@ -0,0 +1,22 @@
+/* PR c/125418 */
+/* { dg-do compile { target tls_native } } */
+/* { dg-require-weak "" } */
+/* { dg-require-visibility "" } */
+/* { dg-options "-O2 -fdump-ipa-whole-program" } */
+
+/* As tls-pragma-weak-1.c, but with an explicit tls_model attribute, which
+   is how glibc declares _nl_current_LC_*.  The attribute is a lower bound:
+   the model must stay initial-exec rather than be upgraded to local-exec.  */
+
+__attribute__((visibility("hidden"), tls_model("initial-exec")))
+extern __thread int t1;
+#pragma weak t1
+
+#pragma weak t2
+__attribute__((visibility("hidden"), tls_model("initial-exec")))
+extern __thread int t2;
+
+int *f1 (void) { return &t1; }
+int *f2 (void) { return &t2; }
+
+/* { dg-final { scan-ipa-dump-times "Varpool flags: tls-initial-exec" 2 
"whole-program" } } */
diff --git a/gcc/testsuite/c-c++-common/tls-pragma-weak-3.c 
b/gcc/testsuite/c-c++-common/tls-pragma-weak-3.c
new file mode 100644
index 000000000000..920016ab50f4
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/tls-pragma-weak-3.c
@@ -0,0 +1,18 @@
+/* PR c/125418 */
+/* { dg-do compile { target tls_native } } */
+/* { dg-require-weak "" } */
+/* { dg-require-visibility "" } */
+
+/* A tls_model attribute whose argument is not a string is rejected and
+   must not be left on the declaration for #pragma weak to read.  */
+
+__attribute__((visibility("hidden"), tls_model(1)))
+extern __thread int t1; /* { dg-error "argument not a string" } */
+#pragma weak t1
+
+#pragma weak t2
+__attribute__((visibility("hidden"), tls_model(1)))
+extern __thread int t2; /* { dg-error "argument not a string" } */
+
+int *f1 (void) { return &t1; }
+int *f2 (void) { return &t2; }
diff --git a/gcc/testsuite/c-c++-common/tls-pragma-weak-4.c 
b/gcc/testsuite/c-c++-common/tls-pragma-weak-4.c
new file mode 100644
index 000000000000..f5d4467f3a6f
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/tls-pragma-weak-4.c
@@ -0,0 +1,40 @@
+/* PR c/125418 */
+/* { dg-do compile { target tls_native } } */
+/* { dg-require-weak "" } */
+/* { dg-require-visibility "" } */
+/* { dg-options "-O2 -fdump-ipa-whole-program" } */
+
+/* As tls-pragma-weak-1.c and tls-pragma-weak-2.c, but with the variable
+   redeclared after it has become weak.  The model is chosen for the
+   redeclaration before it is merged with the weak declaration, so it must
+   be recomputed then, however the variable was made weak.  */
+
+__attribute__((visibility("hidden")))
+extern __thread int t1;
+#pragma weak t1
+__attribute__((visibility("hidden")))
+extern __thread int t1;
+
+#pragma weak t2
+__attribute__((visibility("hidden")))
+extern __thread int t2;
+__attribute__((visibility("hidden")))
+extern __thread int t2;
+
+__attribute__((visibility("hidden"), weak))
+extern __thread int t3;
+__attribute__((visibility("hidden")))
+extern __thread int t3;
+
+__attribute__((visibility("hidden"), tls_model("initial-exec")))
+extern __thread int t4;
+#pragma weak t4
+__attribute__((visibility("hidden"), tls_model("initial-exec")))
+extern __thread int t4;
+
+int *f1 (void) { return &t1; }
+int *f2 (void) { return &t2; }
+int *f3 (void) { return &t3; }
+int *f4 (void) { return &t4; }
+
+/* { dg-final { scan-ipa-dump-not "Varpool flags: tls-local" "whole-program" } 
} */
diff --git a/gcc/testsuite/gcc.dg/tls/pragma-weak-alias.c 
b/gcc/testsuite/gcc.dg/tls/pragma-weak-alias.c
new file mode 100644
index 000000000000..15701e1368dd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tls/pragma-weak-alias.c
@@ -0,0 +1,31 @@
+/* PR c/125418 */
+/* { dg-do compile { target tls_native } } */
+/* { dg-require-weak "" } */
+/* { dg-require-alias "" } */
+/* { dg-require-visibility "" } */
+/* { dg-options "-O2 -fdump-ipa-visibility" } */
+
+/* A hidden TLS variable that #pragma weak makes an alias of a variable
+   defined here still binds locally, so the front end must not weaken its
+   TLS access model the way it does for a weak undefined symbol.  Scan the
+   early dump: later IPA passes would upgrade the model again.  The same
+   holds when the alias is redeclared.  */
+
+static __thread int local_def;
+
+__attribute__((visibility("hidden")))
+extern __thread int t;
+#pragma weak t = local_def
+
+static __thread int local_def2;
+
+__attribute__((visibility("hidden")))
+extern __thread int t2;
+#pragma weak t2 = local_def2
+__attribute__((visibility("hidden")))
+extern __thread int t2;
+
+int *f (void) { return &t; }
+int *f2 (void) { return &t2; }
+
+/* { dg-final { scan-ipa-dump-not "Varpool flags: tls-initial-exec" 
"visibility" } } */

Reply via email to