[v2] aarch64: Skip traditional GD/LD TLS which are unsupported by Clang and LLD [BZ #28205]

Message ID 20210810210247.676100-1-maskray@google.com
State Superseded
Delegated to: Szabolcs Nagy
Headers
Series [v2] aarch64: Skip traditional GD/LD TLS which are unsupported by Clang and LLD [BZ #28205] |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Fangrui Song Aug. 10, 2021, 9:02 p.m. UTC
  TLSDESC is the default on aarch64.  Clang doesn't support
-mtls-dialect=trad.  Its integrated assembler doesn't support the
marker.  LLD's doesn't support R_AARCH64_TLSGD_*/R_AARCH64_TLSLD_*
relocations.  Just skip the tests.

With https://sourceware.org/pipermail/libc-alpha/2021-August/129966.html
("aarch64: Make elf_machine_{load_address, dynamic} robust [BZ #28203]"),
if we allow LLD in configure.ac,
`make check` test results of LLD are on par with GNU ld.
---
 config.h.in                  |  3 +++
 elf/tst-tls1.c               |  7 +++++--
 elf/tst-tls2.c               |  6 ++++--
 elf/tst-tls3.c               |  8 ++++----
 elf/tst-tlsmod1.c            |  6 ++++--
 elf/tst-tlsmod2.c            |  4 +++-
 elf/tst-tlsmod3.c            |  5 ++++-
 elf/tst-tlsmod4.c            |  4 +++-
 sysdeps/aarch64/configure    | 30 ++++++++++++++++++++++++++++++
 sysdeps/aarch64/configure.ac | 18 ++++++++++++++++++
 10 files changed, 78 insertions(+), 13 deletions(-)
  

Comments

Fangrui Song Aug. 10, 2021, 9:14 p.m. UTC | #1
On 2021-08-10, Fangrui Song wrote:
>TLSDESC is the default on aarch64.  Clang doesn't support
>-mtls-dialect=trad.  Its integrated assembler doesn't support the
>marker.  LLD's doesn't support R_AARCH64_TLSGD_*/R_AARCH64_TLSLD_*
>relocations.  Just skip the tests.
>
>With https://sourceware.org/pipermail/libc-alpha/2021-August/129966.html
>("aarch64: Make elf_machine_{load_address, dynamic} robust [BZ #28203]"),
>if we allow LLD in configure.ac,
>`make check` test results of LLD are on par with GNU ld.

I messed up in the diff.
The correct diff needs `#include <config.h>` to make HAVE_TRAD_TLS
defined.

---
  config.h.in                  |  3 +++
  elf/tst-tls1.c               |  8 ++++++--
  elf/tst-tls2.c               |  7 +++++--
  elf/tst-tls3.c               |  9 +++++----
  elf/tst-tlsmod1.c            |  7 +++++--
  elf/tst-tlsmod2.c            |  5 ++++-
  elf/tst-tlsmod3.c            |  6 +++++-
  elf/tst-tlsmod4.c            |  5 ++++-
  sysdeps/aarch64/configure    | 30 ++++++++++++++++++++++++++++++
  sysdeps/aarch64/configure.ac | 18 ++++++++++++++++++
  10 files changed, 85 insertions(+), 13 deletions(-)

diff --git a/config.h.in b/config.h.in
index 0d92504f65..7dcce4d3a4 100644
--- a/config.h.in
+++ b/config.h.in
@@ -198,6 +198,9 @@
  /* Define if CC supports attribute retain.  */
  #undef HAVE_GNU_RETAIN
  
+/* Define if CC and LD support traditional TLS GD/LD models.  */
+#define HAVE_TRAD_TLS 1
+
  /* Define to 1 if the assembler needs intermediate aliases to define
     multiple symbol versions for one symbol.  */
  #define SYMVER_NEEDS_ALIAS 0
diff --git a/elf/tst-tls1.c b/elf/tst-tls1.c
index c31da56ce9..1f102b62b7 100644
--- a/elf/tst-tls1.c
+++ b/elf/tst-tls1.c
@@ -1,4 +1,5 @@
  /* glibc test for TLS in ld.so.  */
+#include <config.h>
  #include <stdio.h>
  
  #include "tls-macros.h"
@@ -39,7 +40,8 @@ do_test (void)
        result = 1;
      }
  
-
+  /* Clang and LLD do not support traditional GD/LD TLS on aarch64. */
+#if HAVE_TRAD_TLS
    /* Get variables using local dynamic model.  */
    fputs ("get sum of foo and bar (LD)", stdout);
    ap = TLS_LD (foo);
@@ -56,8 +58,9 @@ do_test (void)
        printf ("bar = %d\n", *bp);
        result = 1;
      }
+#endif
  
-
+#if HAVE_TRAD_TLS
    /* Get variables using generic dynamic model.  */
    fputs ("get sum of foo and bar (GD)", stdout);
    ap = TLS_GD (foo);
@@ -74,6 +77,7 @@ do_test (void)
        printf ("bar = %d\n", *bp);
        result = 1;
      }
+#endif
  
    return result;
  }
diff --git a/elf/tst-tls2.c b/elf/tst-tls2.c
index 963b8d6c88..46e69b049b 100644
--- a/elf/tst-tls2.c
+++ b/elf/tst-tls2.c
@@ -1,4 +1,5 @@
  /* glibc test for TLS in ld.so.  */
+#include <config.h>
  #include <stdio.h>
  
  #include "tls-macros.h"
@@ -39,7 +40,7 @@ do_test (void)
        result = 1;
      }
  
-
+#if HAVE_TRAD_TLS
    /* Get variables using local dynamic model.  */
    fputs ("get sum of foo and bar (LD)", stdout);
    ap = TLS_LD (foo);
@@ -56,8 +57,9 @@ do_test (void)
        printf ("bar = %d\n", *bp);
        result = 1;
      }
+#endif
  
-
+#if HAVE_TRAD_TLS
    /* Get variables using generic dynamic model.  */
    fputs ("get sum of foo and bar (GD)", stdout);
    ap = TLS_GD (foo);
@@ -74,6 +76,7 @@ do_test (void)
        printf ("bar = %d\n", *bp);
        result = 1;
      }
+#endif
  
    return result;
  }
diff --git a/elf/tst-tls3.c b/elf/tst-tls3.c
index 7e0abb4c58..4abdec2857 100644
--- a/elf/tst-tls3.c
+++ b/elf/tst-tls3.c
@@ -1,4 +1,5 @@
  /* glibc test for TLS in ld.so.  */
+#include <config.h>
  #include <stdio.h>
  
  #include "tls-macros.h"
@@ -17,8 +18,7 @@ static int
  do_test (void)
  {
    int result = 0;
-  int *ap, *bp, *cp;
-
+  int *ap, *bp;
  
    /* Set the variable using the local exec model.  */
    puts ("set baz to 3 (LE)");
@@ -33,7 +33,8 @@ do_test (void)
    bp = TLS_IE (bar);
    *bp = 2;
  
-
+#if HAVE_TRAD_TLS
+  int *cp;
    /* Get variables using local dynamic model.  */
    fputs ("get sum of foo, bar (GD) and baz (LD)", stdout);
    ap = TLS_GD (foo);
@@ -56,7 +57,7 @@ do_test (void)
        printf ("baz = %d\n", *cp);
        result = 1;
      }
-
+#endif
  
    result |= in_dso ();
  
diff --git a/elf/tst-tlsmod1.c b/elf/tst-tlsmod1.c
index 8d9156791b..94346415c3 100644
--- a/elf/tst-tlsmod1.c
+++ b/elf/tst-tlsmod1.c
@@ -1,3 +1,4 @@
+#include <config.h>
  #include <stdio.h>
  
  #include "tls-macros.h"
@@ -14,7 +15,7 @@ int
  in_dso (void)
  {
    int result = 0;
-  int *ap, *bp, *cp;
+  int *ap, *bp;
  
    /* Get variables using initial exec model.  */
    fputs ("get sum of foo and bar (IE)", stdout);
@@ -34,7 +35,8 @@ in_dso (void)
        result = 1;
      }
  
-
+#if HAVE_TRAD_TLS
+  int *cp;
    /* Get variables using generic dynamic model.  */
    fputs ("get sum of foo and bar and baz (GD)", stdout);
    ap = TLS_GD (foo);
@@ -57,6 +59,7 @@ in_dso (void)
        printf ("baz = %d\n", *cp);
        result = 1;
      }
+#endif
  
    return result;
  }
diff --git a/elf/tst-tlsmod2.c b/elf/tst-tlsmod2.c
index 40eb1407f8..8028237155 100644
--- a/elf/tst-tlsmod2.c
+++ b/elf/tst-tlsmod2.c
@@ -1,3 +1,4 @@
+#include <config.h>
  #include <stdio.h>
  
  #include "tls-macros.h"
@@ -9,9 +10,10 @@ COMMON_INT_DEF(foo);
  int
  in_dso (int n, int *caller_foop)
  {
-  int *foop;
    int result = 0;
  
+#if HAVE_TRAD_TLS
+  int *foop;
    puts ("foo");			/* Make sure PLT is used before macros.  */
    asm ("" ::: "memory");
  
@@ -29,6 +31,7 @@ in_dso (int n, int *caller_foop)
      }
  
    *foop = 16;
+#endif
  
    return result;
  }
diff --git a/elf/tst-tlsmod3.c b/elf/tst-tlsmod3.c
index 6d186c47ee..d001778247 100644
--- a/elf/tst-tlsmod3.c
+++ b/elf/tst-tlsmod3.c
@@ -1,3 +1,4 @@
+#include <config.h>
  #include <stdio.h>
  
  #include "tls-macros.h"
@@ -12,8 +13,10 @@ COMMON_INT_DEF(comm_n);
  int
  in_dso2 (void)
  {
-  int *foop;
    int result = 0;
+
+#if HAVE_TRAD_TLS
+  int *foop;
    static int n;
    int *np;
  
@@ -32,6 +35,7 @@ in_dso2 (void)
    result |= in_dso (*foop = 42 + n++, foop);
  
    *foop = 16;
+#endif
  
    return result;
  }
diff --git a/elf/tst-tlsmod4.c b/elf/tst-tlsmod4.c
index 86889aac7e..a31c163ca6 100644
--- a/elf/tst-tlsmod4.c
+++ b/elf/tst-tlsmod4.c
@@ -1,3 +1,4 @@
+#include <config.h>
  #include <stdio.h>
  
  #include "tls-macros.h"
@@ -9,9 +10,10 @@ COMMON_INT_DEF(baz);
  int
  in_dso (int n, int *caller_bazp)
  {
-  int *bazp;
    int result = 0;
  
+#if HAVE_TRAD_TLS
+  int *bazp;
    puts ("foo");			/* Make sure PLT is used before macros.  */
    asm ("" ::: "memory");
  
@@ -29,6 +31,7 @@ in_dso (int n, int *caller_bazp)
      }
  
    *bazp = 16;
+#endif
  
    return result;
  }
diff --git a/sysdeps/aarch64/configure b/sysdeps/aarch64/configure
index 4c1fac49f3..eb62e68010 100644
--- a/sysdeps/aarch64/configure
+++ b/sysdeps/aarch64/configure
@@ -332,3 +332,33 @@ if test $libc_cv_aarch64_sve_asm = yes; then
    $as_echo "#define HAVE_AARCH64_SVE_ASM 1" >>confdefs.h
  
  fi
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -mtls-dialect=trad" >&5
+$as_echo_n "checking for -mtls-dialect=trad... " >&6; }
+if ${libc_cv_aarch64_trad_tls+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat > conftest.c <<EOF
+extern __thread int i;
+void foo (void) { i = 10; }
+EOF
+  if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -fPIC
+                     -mtls-dialect=trad -shared -o conftest.so conftest.c'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+  then
+    libc_cv_aarch64_trad_tls=yes
+  else
+    libc_cv_aarch64_trad_tls=no
+  fi
+  rm -f conftest*
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_aarch64_trad_tls" >&5
+$as_echo "$libc_cv_aarch64_trad_tls" >&6; }
+if test $libc_cv_aarch64_trad_tls = no; then
+  $as_echo "#define HAVE_TRAD_TLS 0" >>confdefs.h
+
+fi
diff --git a/sysdeps/aarch64/configure.ac b/sysdeps/aarch64/configure.ac
index 3347c13fa1..6d3b412acc 100644
--- a/sysdeps/aarch64/configure.ac
+++ b/sysdeps/aarch64/configure.ac
@@ -105,3 +105,21 @@ rm -f conftest*])
  if test $libc_cv_aarch64_sve_asm = yes; then
    AC_DEFINE(HAVE_AARCH64_SVE_ASM)
  fi
+
+# Check if both CC and LD support traditional TLS GD/LD models.
+AC_CACHE_CHECK([for -mtls-dialect=trad], libc_cv_aarch64_trad_tls, [dnl
+cat > conftest.c <<EOF
+extern __thread int i;
+void foo (void) { i = 10; }
+EOF
+  if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -fPIC
+                     -mtls-dialect=trad -shared -o conftest.so conftest.c])
+  then
+    libc_cv_aarch64_trad_tls=yes
+  else
+    libc_cv_aarch64_trad_tls=no
+  fi
+  rm -f conftest*])
+if test $libc_cv_aarch64_trad_tls = no; then
+  AC_DEFINE(HAVE_TRAD_TLS, 0)
+fi
  
Szabolcs Nagy Aug. 11, 2021, 12:17 p.m. UTC | #2
The 08/10/2021 14:14, Fangrui Song via Libc-alpha wrote:
> On 2021-08-10, Fangrui Song wrote:
> > TLSDESC is the default on aarch64.  Clang doesn't support
> > -mtls-dialect=trad.  Its integrated assembler doesn't support the
> > marker.  LLD's doesn't support R_AARCH64_TLSGD_*/R_AARCH64_TLSLD_*
> > relocations.  Just skip the tests.
> > 
> > With https://sourceware.org/pipermail/libc-alpha/2021-August/129966.html
> > ("aarch64: Make elf_machine_{load_address, dynamic} robust [BZ #28203]"),
> > if we allow LLD in configure.ac,
> > `make check` test results of LLD are on par with GNU ld.
> 
> I messed up in the diff.
> The correct diff needs `#include <config.h>` to make HAVE_TRAD_TLS
> defined.

the fixed patch looks good to me.

but now i looked at elf/tls-macros.h and it seems
to be for when there is on compiler support.

i think the only thing the compiler does not
support is mixing different tls access models to a
variable in the same tu, but i'm not sure if that's
useful or necessary for the tests: we can use many
files with different -ftls-model= or
__attribute__((tls_model(...))).

is there a need for these hard to maintain macros?
  
Fangrui Song Aug. 11, 2021, 3:57 p.m. UTC | #3
On 2021-08-11, Szabolcs Nagy wrote:
>The 08/10/2021 14:14, Fangrui Song via Libc-alpha wrote:
>> On 2021-08-10, Fangrui Song wrote:
>> > TLSDESC is the default on aarch64.  Clang doesn't support
>> > -mtls-dialect=trad.  Its integrated assembler doesn't support the
>> > marker.  LLD's doesn't support R_AARCH64_TLSGD_*/R_AARCH64_TLSLD_*
>> > relocations.  Just skip the tests.
>> >
>> > With https://sourceware.org/pipermail/libc-alpha/2021-August/129966.html
>> > ("aarch64: Make elf_machine_{load_address, dynamic} robust [BZ #28203]"),
>> > if we allow LLD in configure.ac,
>> > `make check` test results of LLD are on par with GNU ld.
>>
>> I messed up in the diff.
>> The correct diff needs `#include <config.h>` to make HAVE_TRAD_TLS
>> defined.
>
>the fixed patch looks good to me.
>
>but now i looked at elf/tls-macros.h and it seems
>to be for when there is on compiler support.
>
>i think the only thing the compiler does not
>support is mixing different tls access models to a
>variable in the same tu, but i'm not sure if that's
>useful or necessary for the tests: we can use many
>files with different -ftls-model= or
>__attribute__((tls_model(...))).
>
>is there a need for these hard to maintain macros?

In GCC, mixing different tls access models to a variable in the same tu
can be emulated by:

   __thread int a __attribute__((tls_model("global-dynamic")));
   extern __thread int b __attribute__((alias("a"), tls_model("initial-exec")));

In Clang only one model is used.


Seems a good idea to switch them to variable attributes.
  
Fangrui Song Aug. 11, 2021, 9:34 p.m. UTC | #4
On 2021-08-11, Fangrui Song wrote:
>On 2021-08-11, Szabolcs Nagy wrote:
>>The 08/10/2021 14:14, Fangrui Song via Libc-alpha wrote:
>>>On 2021-08-10, Fangrui Song wrote:
>>>> TLSDESC is the default on aarch64.  Clang doesn't support
>>>> -mtls-dialect=trad.  Its integrated assembler doesn't support the
>>>> marker.  LLD's doesn't support R_AARCH64_TLSGD_*/R_AARCH64_TLSLD_*
>>>> relocations.  Just skip the tests.
>>>>
>>>> With https://sourceware.org/pipermail/libc-alpha/2021-August/129966.html
>>>> ("aarch64: Make elf_machine_{load_address, dynamic} robust [BZ #28203]"),
>>>> if we allow LLD in configure.ac,
>>>> `make check` test results of LLD are on par with GNU ld.
>>>
>>>I messed up in the diff.
>>>The correct diff needs `#include <config.h>` to make HAVE_TRAD_TLS
>>>defined.
>>
>>the fixed patch looks good to me.
>>
>>but now i looked at elf/tls-macros.h and it seems
>>to be for when there is on compiler support.
>>
>>i think the only thing the compiler does not
>>support is mixing different tls access models to a
>>variable in the same tu, but i'm not sure if that's
>>useful or necessary for the tests: we can use many
>>files with different -ftls-model= or
>>__attribute__((tls_model(...))).
>>
>>is there a need for these hard to maintain macros?
>
>In GCC, mixing different tls access models to a variable in the same tu
>can be emulated by:
>
>  __thread int a __attribute__((tls_model("global-dynamic")));
>  extern __thread int b __attribute__((alias("a"), tls_model("initial-exec")));
>
>In Clang only one model is used.
>
>
>Seems a good idea to switch them to variable attributes.

alias() doesn't work. asm label works well.


https://sourceware.org/pipermail/libc-alpha/2021-August/130083.html
("elf: Drop elf/tls-macros.h in favor of __thread and tls_model attributes [BZ #28152] [BZ #28205]")
will modernize the tests.
  

Patch

diff --git a/config.h.in b/config.h.in
index 0d92504f65..7dcce4d3a4 100644
--- a/config.h.in
+++ b/config.h.in
@@ -198,6 +198,9 @@ 
 /* Define if CC supports attribute retain.  */
 #undef HAVE_GNU_RETAIN
 
+/* Define if CC and LD support traditional TLS GD/LD models.  */
+#define HAVE_TRAD_TLS 1
+
 /* Define to 1 if the assembler needs intermediate aliases to define
    multiple symbol versions for one symbol.  */
 #define SYMVER_NEEDS_ALIAS 0
diff --git a/elf/tst-tls1.c b/elf/tst-tls1.c
index c31da56ce9..57d5ea2485 100644
--- a/elf/tst-tls1.c
+++ b/elf/tst-tls1.c
@@ -39,7 +39,8 @@  do_test (void)
       result = 1;
     }
 
-
+  /* Clang and LLD do not support traditional GD/LD TLS on aarch64. */
+#ifdef HAVE_TRAD_TLS
   /* Get variables using local dynamic model.  */
   fputs ("get sum of foo and bar (LD)", stdout);
   ap = TLS_LD (foo);
@@ -56,8 +57,9 @@  do_test (void)
       printf ("bar = %d\n", *bp);
       result = 1;
     }
+#endif
 
-
+#ifdef HAVE_TRAD_TLS
   /* Get variables using generic dynamic model.  */
   fputs ("get sum of foo and bar (GD)", stdout);
   ap = TLS_GD (foo);
@@ -74,6 +76,7 @@  do_test (void)
       printf ("bar = %d\n", *bp);
       result = 1;
     }
+#endif
 
   return result;
 }
diff --git a/elf/tst-tls2.c b/elf/tst-tls2.c
index 963b8d6c88..c9dcc084e3 100644
--- a/elf/tst-tls2.c
+++ b/elf/tst-tls2.c
@@ -39,7 +39,7 @@  do_test (void)
       result = 1;
     }
 
-
+#ifdef HAVE_TRAD_TLS
   /* Get variables using local dynamic model.  */
   fputs ("get sum of foo and bar (LD)", stdout);
   ap = TLS_LD (foo);
@@ -56,8 +56,9 @@  do_test (void)
       printf ("bar = %d\n", *bp);
       result = 1;
     }
+#endif
 
-
+#ifdef HAVE_TRAD_TLS
   /* Get variables using generic dynamic model.  */
   fputs ("get sum of foo and bar (GD)", stdout);
   ap = TLS_GD (foo);
@@ -74,6 +75,7 @@  do_test (void)
       printf ("bar = %d\n", *bp);
       result = 1;
     }
+#endif
 
   return result;
 }
diff --git a/elf/tst-tls3.c b/elf/tst-tls3.c
index 7e0abb4c58..6f2e144a7f 100644
--- a/elf/tst-tls3.c
+++ b/elf/tst-tls3.c
@@ -17,8 +17,7 @@  static int
 do_test (void)
 {
   int result = 0;
-  int *ap, *bp, *cp;
-
+  int *ap, *bp;
 
   /* Set the variable using the local exec model.  */
   puts ("set baz to 3 (LE)");
@@ -33,7 +32,8 @@  do_test (void)
   bp = TLS_IE (bar);
   *bp = 2;
 
-
+#ifdef HAVE_TRAD_TLS
+  int *cp;
   /* Get variables using local dynamic model.  */
   fputs ("get sum of foo, bar (GD) and baz (LD)", stdout);
   ap = TLS_GD (foo);
@@ -56,7 +56,7 @@  do_test (void)
       printf ("baz = %d\n", *cp);
       result = 1;
     }
-
+#endif
 
   result |= in_dso ();
 
diff --git a/elf/tst-tlsmod1.c b/elf/tst-tlsmod1.c
index 8d9156791b..9f48e36af4 100644
--- a/elf/tst-tlsmod1.c
+++ b/elf/tst-tlsmod1.c
@@ -14,7 +14,7 @@  int
 in_dso (void)
 {
   int result = 0;
-  int *ap, *bp, *cp;
+  int *ap, *bp;
 
   /* Get variables using initial exec model.  */
   fputs ("get sum of foo and bar (IE)", stdout);
@@ -34,7 +34,8 @@  in_dso (void)
       result = 1;
     }
 
-
+#ifdef HAVE_TRAD_TLS
+  int *cp;
   /* Get variables using generic dynamic model.  */
   fputs ("get sum of foo and bar and baz (GD)", stdout);
   ap = TLS_GD (foo);
@@ -57,6 +58,7 @@  in_dso (void)
       printf ("baz = %d\n", *cp);
       result = 1;
     }
+#endif
 
   return result;
 }
diff --git a/elf/tst-tlsmod2.c b/elf/tst-tlsmod2.c
index 40eb1407f8..78578ebcc2 100644
--- a/elf/tst-tlsmod2.c
+++ b/elf/tst-tlsmod2.c
@@ -9,9 +9,10 @@  COMMON_INT_DEF(foo);
 int
 in_dso (int n, int *caller_foop)
 {
-  int *foop;
   int result = 0;
 
+#ifdef HAVE_TRAD_TLS
+  int *foop;
   puts ("foo");			/* Make sure PLT is used before macros.  */
   asm ("" ::: "memory");
 
@@ -29,6 +30,7 @@  in_dso (int n, int *caller_foop)
     }
 
   *foop = 16;
+#endif
 
   return result;
 }
diff --git a/elf/tst-tlsmod3.c b/elf/tst-tlsmod3.c
index 6d186c47ee..d9f17dbb6f 100644
--- a/elf/tst-tlsmod3.c
+++ b/elf/tst-tlsmod3.c
@@ -12,8 +12,10 @@  COMMON_INT_DEF(comm_n);
 int
 in_dso2 (void)
 {
-  int *foop;
   int result = 0;
+
+#ifdef HAVE_TRAD_TLS
+  int *foop;
   static int n;
   int *np;
 
@@ -32,6 +34,7 @@  in_dso2 (void)
   result |= in_dso (*foop = 42 + n++, foop);
 
   *foop = 16;
+#endif
 
   return result;
 }
diff --git a/elf/tst-tlsmod4.c b/elf/tst-tlsmod4.c
index 86889aac7e..27baaaf501 100644
--- a/elf/tst-tlsmod4.c
+++ b/elf/tst-tlsmod4.c
@@ -9,9 +9,10 @@  COMMON_INT_DEF(baz);
 int
 in_dso (int n, int *caller_bazp)
 {
-  int *bazp;
   int result = 0;
 
+#ifdef HAVE_TRAD_TLS
+  int *bazp;
   puts ("foo");			/* Make sure PLT is used before macros.  */
   asm ("" ::: "memory");
 
@@ -29,6 +30,7 @@  in_dso (int n, int *caller_bazp)
     }
 
   *bazp = 16;
+#endif
 
   return result;
 }
diff --git a/sysdeps/aarch64/configure b/sysdeps/aarch64/configure
index 4c1fac49f3..eb62e68010 100644
--- a/sysdeps/aarch64/configure
+++ b/sysdeps/aarch64/configure
@@ -332,3 +332,33 @@  if test $libc_cv_aarch64_sve_asm = yes; then
   $as_echo "#define HAVE_AARCH64_SVE_ASM 1" >>confdefs.h
 
 fi
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for -mtls-dialect=trad" >&5
+$as_echo_n "checking for -mtls-dialect=trad... " >&6; }
+if ${libc_cv_aarch64_trad_tls+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat > conftest.c <<EOF
+extern __thread int i;
+void foo (void) { i = 10; }
+EOF
+  if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -fPIC
+                     -mtls-dialect=trad -shared -o conftest.so conftest.c'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+  then
+    libc_cv_aarch64_trad_tls=yes
+  else
+    libc_cv_aarch64_trad_tls=no
+  fi
+  rm -f conftest*
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_aarch64_trad_tls" >&5
+$as_echo "$libc_cv_aarch64_trad_tls" >&6; }
+if test $libc_cv_aarch64_trad_tls = no; then
+  $as_echo "#define HAVE_TRAD_TLS 0" >>confdefs.h
+
+fi
diff --git a/sysdeps/aarch64/configure.ac b/sysdeps/aarch64/configure.ac
index 3347c13fa1..6d3b412acc 100644
--- a/sysdeps/aarch64/configure.ac
+++ b/sysdeps/aarch64/configure.ac
@@ -105,3 +105,21 @@  rm -f conftest*])
 if test $libc_cv_aarch64_sve_asm = yes; then
   AC_DEFINE(HAVE_AARCH64_SVE_ASM)
 fi
+
+# Check if both CC and LD support traditional TLS GD/LD models.
+AC_CACHE_CHECK([for -mtls-dialect=trad], libc_cv_aarch64_trad_tls, [dnl
+cat > conftest.c <<EOF
+extern __thread int i;
+void foo (void) { i = 10; }
+EOF
+  if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -fPIC
+                     -mtls-dialect=trad -shared -o conftest.so conftest.c])
+  then
+    libc_cv_aarch64_trad_tls=yes
+  else
+    libc_cv_aarch64_trad_tls=no
+  fi
+  rm -f conftest*])
+if test $libc_cv_aarch64_trad_tls = no; then
+  AC_DEFINE(HAVE_TRAD_TLS, 0)
+fi