diff mbox series

[2/5,PowerPC64] Set up thread register for _dl_relocate_static_pie

Message ID Ye4hVIPjYpPZxj5j@squeak.grove.modra.org
State Superseded
Headers show
Series [PowerPC64] Use medium model toc accesses throughout | expand

Commit Message

Alan Modra Jan. 24, 2022, 3:47 a.m. UTC
On Sun, Jan 23, 2022 at 11:12:13PM +1030, Alan Modra wrote:
> There's a serious problem in libgcc too.  libgcc ifuncs access the
> AT_HWCAP words stored in the tcb with an offset from the thread
> pointer (r13), but r13 isn't set at the time _dl_relocate_static_pie
> runs, and I'm loathe to try calling init_tls early.  A better approach
> that might work is to fake r13 so that _dl_hwcap is at the expected
> offset where we'd normally find the tcb hwcap words.

Like this.

libgcc ifunc resolvers that access hwcap via a field in the tcb can't
be called until the thread pointer is set up.  This patch sets up a
fake thread pointer early so that static-pies won't segfault on
attempting to relocate themselves.

Comments

Paul E Murphy Feb. 16, 2022, 11:02 p.m. UTC | #1
On 1/23/22 9:47 PM, Alan Modra via Libc-alpha wrote:
> On Sun, Jan 23, 2022 at 11:12:13PM +1030, Alan Modra wrote:
>> There's a serious problem in libgcc too.  libgcc ifuncs access the
>> AT_HWCAP words stored in the tcb with an offset from the thread
>> pointer (r13), but r13 isn't set at the time _dl_relocate_static_pie
>> runs, and I'm loathe to try calling init_tls early.  A better approach
>> that might work is to fake r13 so that _dl_hwcap is at the expected
>> offset where we'd normally find the tcb hwcap words.
> 
> Like this.
> 
> libgcc ifunc resolvers that access hwcap via a field in the tcb can't
> be called until the thread pointer is set up.  This patch sets up a
> fake thread pointer early so that static-pies won't segfault on
> attempting to relocate themselves.
> 

I suspect the thread pointer needs to be setup more.  How much, I am not 
sure.

Looking into the failure of tst-tlsifunc-static, we would need similar 
access to at_platform when resolving ifunc for similar reasons of hwcap. 
That seems like an easy fix.

However, I am not sure what to make of the other failure in this test. 
A pointer into tls is created as part of running an ifunc resolver.  Do 
we need to preserve that behavior?
Alan Modra Feb. 19, 2022, 12:49 a.m. UTC | #2
On Wed, Feb 16, 2022 at 05:02:51PM -0600, Paul E Murphy wrote:
> 
> 
> On 1/23/22 9:47 PM, Alan Modra via Libc-alpha wrote:
> > On Sun, Jan 23, 2022 at 11:12:13PM +1030, Alan Modra wrote:
> > > There's a serious problem in libgcc too.  libgcc ifuncs access the
> > > AT_HWCAP words stored in the tcb with an offset from the thread
> > > pointer (r13), but r13 isn't set at the time _dl_relocate_static_pie
> > > runs, and I'm loathe to try calling init_tls early.  A better approach
> > > that might work is to fake r13 so that _dl_hwcap is at the expected
> > > offset where we'd normally find the tcb hwcap words.
> > 
> > Like this.
> > 
> > libgcc ifunc resolvers that access hwcap via a field in the tcb can't
> > be called until the thread pointer is set up.  This patch sets up a
> > fake thread pointer early so that static-pies won't segfault on
> > attempting to relocate themselves.
> > 
> 
> I suspect the thread pointer needs to be setup more.  How much, I am not
> sure.
> 
> Looking into the failure of tst-tlsifunc-static, we would need similar
> access to at_platform when resolving ifunc for similar reasons of hwcap.
> That seems like an easy fix.

Yes, it means arranging to have a copy of at_platform accessible from
the fake tls pointer reg, not just __tcb_hwcap.  A tcbhead_t in
sysdeps/powerpc/hwcapinfo.c replacing __tcb_hwcap and __tcb_platform
there would be best, I think.

> However, I am not sure what to make of the other failure in this test. A
> pointer into tls is created as part of running an ifunc resolver.  Do we
> need to preserve that behavior?

Tulio will be better placed to answer this question.  Note that a
number of glibc test failures disappear with binutils commit
3a3a4c1fe4c.
diff mbox series

Patch

diff --git a/sysdeps/powerpc/powerpc64/dl-machine.h b/sysdeps/powerpc/powerpc64/dl-machine.h
index eb41c85280..413bbe7c06 100644
--- a/sysdeps/powerpc/powerpc64/dl-machine.h
+++ b/sysdeps/powerpc/powerpc64/dl-machine.h
@@ -589,6 +589,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