[Bug target/126394] arm: stack misalignment when leaf procedures read tp via __aeabi_read_tp

kalvisd at gmail dot com via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126394

Kalvis Duckmanton <kalvisd at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kalvisd at gmail dot com

--- Comment #1 from Kalvis Duckmanton <kalvisd at gmail dot com> ---
The following patch should allow leaf functions using thread-local storage to
be detected (and thus allow the stack to be aligned as needed)

diff --git a/external/gpl3/gcc/dist/gcc/config/arm/arm.cc
b/external/gpl3/gcc/dist/gcc/config/arm/arm.cc
index d3f7a08f99c7..fef0dbf4d304 100644
--- a/external/gpl3/gcc/dist/gcc/config/arm/arm.cc
+++ b/external/gpl3/gcc/dist/gcc/config/arm/arm.cc
@@ -9529,6 +9529,8 @@ arm_legitimize_address (rtx x, rtx orig_x, machine_mode
mode)

       x = legitimize_tls_address (x, NULL_RTX);

+      cfun->machine->uses_tls = true;
+
       if (addend)
        {
          x = gen_rtx_PLUS (SImode, x, addend);
@@ -21981,6 +21983,9 @@ arm_output_function_prologue (FILE *f)
               frame_pointer_needed,
               cfun->machine->uses_anonymous_args);

+  asm_fprintf (f, "\t%@ uses_tls = %d, is_leaf = %d\n",
+              cfun->machine->uses_tls, crtl->is_leaf);
+
   if (cfun->machine->lr_save_eliminated)
     asm_fprintf (f, "\t%@ link register save eliminated.\n");

@@ -23162,10 +23167,10 @@ arm_compute_frame_layout (void)
         think of this as a leaf routine.  Even if __aeabi_read_tp
         itself doesn't use the stack, resolving the symbol may take a
         detour through a procedure call to the dynamic linker.  We
-        should really enforce alignment only if the procedure actually
-        uses __aeabi_read_tp (load_tp_soft*) but I don't know how to
-        query that here.  */
-      && !TARGET_SOFT_TP)
+        enforce alignment only if the procedure actually uses
+        __aeabi_read_tp (load_tp_soft*).  */
+      && ! TARGET_SOFT_TP
+      && ! cfun->machine->uses_tls)
     {
       offsets->outgoing_args = offsets->soft_frame;
       offsets->locals_base = offsets->soft_frame;
diff --git a/external/gpl3/gcc/dist/gcc/config/arm/arm.h
b/external/gpl3/gcc/dist/gcc/config/arm/arm.h
index d18077b32847..e71c1090da6f 100644
--- a/external/gpl3/gcc/dist/gcc/config/arm/arm.h
+++ b/external/gpl3/gcc/dist/gcc/config/arm/arm.h
@@ -1651,6 +1651,8 @@ typedef struct GTY(()) machine_function
   /* Set to 1 when pointer authentication operation uses value of SP other
      than the incoming stack pointer value.  */
   int pacspval_needed;
+  /* Set to 1 if this function accesses thread-local storage */
+  int uses_tls;
 }
 machine_function;
 #endif
diff --git a/external/gpl3/gcc/dist/gcc/config/arm/arm.md
b/external/gpl3/gcc/dist/gcc/config/arm/arm.md
index 13a8fbf7a147..83a3727d247d 100644
--- a/external/gpl3/gcc/dist/gcc/config/arm/arm.md
+++ b/external/gpl3/gcc/dist/gcc/config/arm/arm.md
@@ -6523,7 +6523,10 @@
      data and load its address to a register.  Offsets have been split off
      already.  */
   if (arm_tls_referenced_p (operands[1]))
-    operands[1] = legitimize_tls_address (operands[1], tmp);
+    {
+      operands[1] = legitimize_tls_address (operands[1], tmp);
+      cfun->machine->uses_tls = true;
+    }
   else if (flag_pic
           && (CONSTANT_P (operands[1])
               || symbol_mentioned_p (operands[1])
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.