public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: libc-alpha@sourceware.org
Cc: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>,
	Alan Modra <amodra@gmail.com>
Subject: [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie
Date: Mon, 28 Feb 2022 17:10:50 +1030	[thread overview]
Message-ID: <20220228064052.3413334-3-amodra@gmail.com> (raw)
In-Reply-To: <20220228064052.3413334-1-amodra@gmail.com>

libgcc ifunc resolvers that access hwcap via a field in the tcb can't
be called until the thread pointer is set up.  Other ifunc resolvers
might need access to at_platform.  This patch sets up a fake thread
pointer early to a copy of tcbhead_t.  hwcapinfo.c already had local
variables for hwcap and at_platform, replace them with an entire
tcbhead_t.  It's not that large and this way we easily ensure hwcap
and at_platform are at the same relative offsets as they are in the
real thread block.

The patch also conditionally disables part of tst-tlsifunc-static,
"bar address read from IFUNC resolver is incorrect".  We can't get a
proper address for a thread variable before glibc initialises tls.

diff --git a/sysdeps/powerpc/hwcapinfo.c b/sysdeps/powerpc/hwcapinfo.c
index e030e322bd..afde05f863 100644
--- a/sysdeps/powerpc/hwcapinfo.c
+++ b/sysdeps/powerpc/hwcapinfo.c
@@ -20,8 +20,7 @@
 #include <shlib-compat.h>
 #include <dl-procinfo.h>
 
-uint64_t __tcb_hwcap __attribute__ ((visibility ("hidden")));
-uint32_t __tcb_platform __attribute__ ((visibility ("hidden")));
+tcbhead_t __tcb __attribute__ ((visibility ("hidden")));
 
 /* This function parses the HWCAP/HWCAP2 fields, adding the previous supported
    ISA bits, as well as converting the AT_PLATFORM string to a number.  This
@@ -34,7 +33,7 @@ __tcb_parse_hwcap_and_convert_at_platform (void)
   uint64_t h1, h2;
 
   /* Read AT_PLATFORM string from auxv and convert it to a number.  */
-  __tcb_platform = _dl_string_platform (GLRO (dl_platform));
+  __tcb.at_platform = _dl_string_platform (GLRO (dl_platform));
 
   /* Read HWCAP and HWCAP2 from auxv.  */
   h1 = GLRO (dl_hwcap);
@@ -66,8 +65,7 @@ __tcb_parse_hwcap_and_convert_at_platform (void)
 
   /* Consolidate both HWCAP and HWCAP2 into a single doubleword so that
      we can read both in a single load later.  */
-  __tcb_hwcap = h2;
-  __tcb_hwcap = (h1 << 32) | __tcb_hwcap;
+  __tcb.hwcap = (h1 << 32) | (h2 & 0xffffffff);
 
 }
 #if IS_IN (rtld)
diff --git a/sysdeps/powerpc/hwcapinfo.h b/sysdeps/powerpc/hwcapinfo.h
index ac462835ce..1f5e0bcb51 100644
--- a/sysdeps/powerpc/hwcapinfo.h
+++ b/sysdeps/powerpc/hwcapinfo.h
@@ -21,8 +21,7 @@
 #ifndef HWCAPINFO_H
 # define HWCAPINFO_H
 
-extern uint64_t __tcb_hwcap  attribute_hidden;
-extern uint32_t __tcb_platform attribute_hidden;
+extern tcbhead_t __tcb attribute_hidden;
 
 extern void __tcb_parse_hwcap_and_convert_at_platform (void);
 
diff --git a/sysdeps/powerpc/nptl/tls.h b/sysdeps/powerpc/nptl/tls.h
index b80d39ad41..22b0075235 100644
--- a/sysdeps/powerpc/nptl/tls.h
+++ b/sysdeps/powerpc/nptl/tls.h
@@ -45,8 +45,6 @@
 
 #ifndef __ASSEMBLER__
 
-# include <hwcapinfo.h>
-
 /* Get system call information.  */
 # include <sysdep.h>
 
@@ -100,6 +98,8 @@ typedef struct
   dtv_t *dtv;
 } tcbhead_t;
 
+# include <hwcapinfo.h>
+
 /* This is the size of the initial TCB.  */
 # define TLS_INIT_TCB_SIZE	0
 
@@ -137,8 +137,8 @@ typedef struct
 # define TLS_INIT_TP(tcbp) \
   ({ 									      \
     __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET;		      \
-    THREAD_SET_HWCAP (__tcb_hwcap);					      \
-    THREAD_SET_AT_PLATFORM (__tcb_platform);				      \
+    THREAD_SET_HWCAP (__tcb.hwcap);					      \
+    THREAD_SET_AT_PLATFORM (__tcb.at_platform);				      \
     NULL;								      \
   })
 
diff --git a/sysdeps/powerpc/powerpc64/dl-machine.h b/sysdeps/powerpc/powerpc64/dl-machine.h
index 6fab5cbe81..bb0ccd0811 100644
--- a/sysdeps/powerpc/powerpc64/dl-machine.h
+++ b/sysdeps/powerpc/powerpc64/dl-machine.h
@@ -559,6 +559,27 @@ elf_machine_plt_value (struct link_map *map, const Elf64_Rela *reloc,
 #define ARCH_LA_PLTEXIT ppc64v2_gnu_pltexit
 #endif
 
+#if ENABLE_STATIC_PIE && !defined SHARED && !IS_IN (rtld)
+#include <libc-diag.h>
+#include <tcb-offsets.h>
+
+/* Set up r13 for _dl_relocate_static_pie so that libgcc ifuncs that
+   normally access the tcb copy of hwcap will see __tcb.hwcap.  */
+
+static inline void __attribute__ ((always_inline))
+ppc_init_fake_thread_pointer (void)
+{
+  DIAG_PUSH_NEEDS_COMMENT;
+  /* We are playing pointer tricks.  Silence gcc warning.  */
+  DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Warray-bounds");
+  __thread_register = (char *) &__tcb.hwcap - TCB_HWCAP;
+  DIAG_POP_NEEDS_COMMENT;
+}
+
+#define ELF_MACHINE_BEFORE_RTLD_RELOC(map, dynamic_info) \
+  ppc_init_fake_thread_pointer ();
+#endif /* ENABLE_STATIC_PIE && !defined SHARED && !IS_IN (rtld) */
+
 #endif /* dl_machine_h */
 
 #ifdef RESOLVE_MAP
diff --git a/sysdeps/powerpc/tst-tlsifunc-static.c b/sysdeps/powerpc/tst-tlsifunc-static.c
index c0ff8972a9..438b27c0ec 100644
--- a/sysdeps/powerpc/tst-tlsifunc-static.c
+++ b/sysdeps/powerpc/tst-tlsifunc-static.c
@@ -16,4 +16,5 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#define TST_TLSIFUNC_STATIC 1
 #include "tst-tlsifunc.c"
diff --git a/sysdeps/powerpc/tst-tlsifunc.c b/sysdeps/powerpc/tst-tlsifunc.c
index 92313e68f4..6b256c6229 100644
--- a/sysdeps/powerpc/tst-tlsifunc.c
+++ b/sysdeps/powerpc/tst-tlsifunc.c
@@ -101,11 +101,14 @@ do_test (void)
 
   if (&bar == bar_ptr)
     printf ("PASS: bar address read from IFUNC resolver is correct.\n");
+#if !defined TST_TLSIFUNC_STATIC || !defined PIC \
+    || !defined PI_STATIC_AND_HIDDEN
   else
     {
       printf ("FAIL: bar address read from IFUNC resolver is incorrect.\n");
       ret = 1;
     }
+#endif
 
   if (tcb_test ())
     printf ("PASS: tcb_test IFUNC resolver called once.\n");

  parent reply	other threads:[~2022-02-28  6:41 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-23 12:42 [PowerPC64] Use medium model toc accesses throughout Alan Modra
2022-01-24  3:47 ` [PATCH 2/5] [PowerPC64] Set up thread register for _dl_relocate_static_pie Alan Modra
2022-02-16 23:02   ` Paul E Murphy
2022-02-19  0:49     ` Alan Modra
2022-01-24  3:50 ` [PATCH 3/5] [PowerPC] Relocate stinfo->main Alan Modra
2022-01-24  4:48   ` H.J. Lu
2022-01-24  6:51     ` Alan Modra
2022-01-24  3:52 ` [PATCH 4/5] Constify a variable in dl_vdso_vsym Alan Modra
2022-01-24  4:06 ` [PATCH 5/5] Enable static-pie on powerpc64 Alan Modra
2022-01-27 18:39   ` Florian Weimer
2022-01-28  8:45     ` Alan Modra
2022-01-28 17:48 ` [PowerPC64] Use medium model toc accesses throughout Paul E Murphy
2022-01-29  1:24   ` Alan Modra
2022-02-28  6:40 ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
2022-02-28  6:40   ` [PATCH v2 1/4] powerpc64: Use medium model toc accesses throughout Alan Modra
2022-04-08 22:28     ` Tulio Magno Quites Machado Filho
2022-02-28  6:40   ` Alan Modra [this message]
2022-04-08 22:28     ` [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie Tulio Magno Quites Machado Filho
2022-02-28  6:40   ` [PATCH v2 3/4] powerpc: Relocate stinfo->main Alan Modra
2022-04-08 22:32     ` Tulio Magno Quites Machado Filho
2022-02-28  6:40   ` [PATCH v2 4/4] powerpc64: Enable static-pie Alan Modra
2022-04-08 22:49     ` Tulio Magno Quites Machado Filho
2022-04-14  1:16       ` Alan Modra
2022-04-14  3:42         ` Fangrui Song
2022-04-20  7:21         ` [PATCH v3] " Alan Modra
2022-03-04 12:48   ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
2022-04-08  8:06     ` Alan Modra
2022-04-09  0:14       ` Fangrui Song
2022-04-14  0:33         ` Alan Modra
2022-04-14  1:54           ` DT_RELR without libc.so dependency H.J. Lu
2022-04-14  3:43             ` Fangrui Song
2022-04-14  5:18             ` Alan Modra
2022-04-14 17:55               ` H.J. Lu
2022-04-08 22:27   ` [PATCH v2 0/4] PowerPC64 static-pie Tulio Magno Quites Machado Filho
2022-04-11  1:38     ` Alan Modra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220228064052.3413334-3-amodra@gmail.com \
    --to=amodra@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=tuliom@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).