public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PowerPC64] Use medium model toc accesses throughout
@ 2022-01-23 12:42 Alan Modra
  2022-01-24  3:47 ` [PATCH 2/5] [PowerPC64] Set up thread register for _dl_relocate_static_pie Alan Modra
                   ` (5 more replies)
  0 siblings, 6 replies; 35+ messages in thread
From: Alan Modra @ 2022-01-23 12:42 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho

The PowerPC64 linker edits medium model toc-indirect code to toc-pointer
relative:
	addis r9,r2,tc_entry_for_var@toc@ha
	ld r9,tc_entry_for_var@toc@l(r9)
becomes
	addis r9,r2,(var-.TOC.)@ha
	addi r9,r9,(var-.TOC.)@l
when "var" is known to be local to the binary.  This isn't done for
small-model toc-indirect code, because "var" is almost guaranteed to
be too far away from .TOC. for a 16-bit signed offset.  And, because
the analysis of which .toc entry can be removed becomes much more
complicated in objects that mix code models, they aren't removed if
any small-model toc sequence appears in an object file.

Unfortunately glibc's build of ld.so smashes the needed objects
together in a ld -r linking stage.  This means the GOT/TOC is left
with a whole lot of relative relocations which is untidy, but in
itself is not a serious problem.  However, static-pie on powerpc64
bombs due to a segfault caused by one of the small-model accesses
before _dl_relocate_static_pie.  (The very first one in rcrt1.o
passing start_addresses in r8 to __libc_start_main.)

So this patch makes all the toc/got accesses in assembly medium code
model, and a couple of functions hidden.  By itself this is *not*
enough to give us working static-pie, but it is useful anyway to
enable better linker optimisation.

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.

Tested for regressions with a powerpc64le-linux build and test run.
OK to apply?

diff --git a/sysdeps/powerpc/powerpc64/__longjmp-common.S b/sysdeps/powerpc/powerpc64/__longjmp-common.S
index 4d71b9e102..5f629e1e0f 100644
--- a/sysdeps/powerpc/powerpc64/__longjmp-common.S
+++ b/sysdeps/powerpc/powerpc64/__longjmp-common.S
@@ -47,12 +47,14 @@
 ENTRY (__longjmp)
 	CALL_MCOUNT 2
 #ifndef __NO_VMX__
-	ld    r5,.LC__dl_hwcap@toc(r2)
+	addis	r5,r2,.LC__dl_hwcap@toc@ha
+	ld	r5,.LC__dl_hwcap@toc@l(r5)
 # ifdef SHARED
 	/* Load _rtld-global._dl_hwcap.  */
-	ld    r5,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r5)
+	ld	r5,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r5)
 # else
-	ld    r5,0(r5) /* Load extern _dl_hwcap.  */
+	/* Load extern _dl_hwcap.  */
+	ld	r5,0(r5)
 # endif
 	andis.  r5,r5,(PPC_FEATURE_HAS_ALTIVEC >> 16)
 	beq	L(no_vmx)
diff --git a/sysdeps/powerpc/powerpc64/dl-machine.h b/sysdeps/powerpc/powerpc64/dl-machine.h
index a505998351..eb41c85280 100644
--- a/sysdeps/powerpc/powerpc64/dl-machine.h
+++ b/sysdeps/powerpc/powerpc64/dl-machine.h
@@ -175,9 +175,12 @@ BODY_PREFIX "_dl_start_user:\n"						\
 /* the address of _start in r30.  */					\
 "	mr	30,3\n"							\
 /* &_dl_argc in 29, &_dl_argv in 27, and _dl_loaded in 28.  */		\
-"	ld	28,.LC__rtld_local@toc(2)\n"				\
-"	ld	29,.LC__dl_argc@toc(2)\n"				\
-"	ld	27,.LC__dl_argv@toc(2)\n"				\
+"	addis	28,2,.LC__rtld_local@toc@ha\n"				\
+"	ld	28,.LC__rtld_local@toc@l(28)\n"				\
+"	addis	29,2,.LC__dl_argc@toc@ha\n"				\
+"	ld	29,.LC__dl_argc@toc@l(29)\n"				\
+"	addis	27,2,.LC__dl_argv@toc@ha\n"				\
+"	ld	27,.LC__dl_argv@toc@l(27)\n"				\
 /* _dl_init (_dl_loaded, _dl_argc, _dl_argv, _dl_argv+_dl_argc+1).  */	\
 "	ld	3,0(28)\n"						\
 "	lwa	4,0(29)\n"						\
@@ -204,7 +207,8 @@ BODY_PREFIX "_dl_start_user:\n"						\
 "	addi	6,6,8\n"						\
 /* Pass a termination function pointer (in this case _dl_fini) in	\
    r7.  */								\
-"	ld	7,.LC__dl_fini@toc(2)\n"				\
+"	addis	7,2,.LC__dl_fini@toc@ha\n"				\
+"	ld	7,.LC__dl_fini@toc@l(7)\n"				\
 /* Pass the stack pointer in r1 (so far so good), pointing to a NULL	\
    value.  This lets our startup code distinguish between a program	\
    linked statically, which linux will call with argc on top of the	\
diff --git a/sysdeps/powerpc/powerpc64/dl-trampoline.S b/sysdeps/powerpc/powerpc64/dl-trampoline.S
index 23debc2faf..45b821607b 100644
--- a/sysdeps/powerpc/powerpc64/dl-trampoline.S
+++ b/sysdeps/powerpc/powerpc64/dl-trampoline.S
@@ -32,6 +32,7 @@
    because gcc as of 2010/05 doesn't allocate a proper stack frame for
    a function that makes no calls except for __tls_get_addr and we
    might be here resolving the __tls_get_addr call.  */
+	.hidden _dl_runtime_resolve
 #define INT_PARMS FRAME_MIN_SIZE
 ENTRY (_dl_runtime_resolve, 4)
 	stdu	r1,-FRAME_SIZE(r1)
@@ -195,6 +196,7 @@ END(_dl_runtime_resolve)
    parm1 (r3) and the index (r0) needs to be converted to an offset
    (index * 24) in parm2 (r4).  */
 #ifndef PROF
+	.hidden _dl_profile_resolve
 ENTRY (_dl_profile_resolve, 4)
 /* Spill r30, r31 to preserve the link_map* and reloc_addr, in case we
    need to call _dl_audit_pltexit.  */
@@ -225,12 +227,14 @@ ENTRY (_dl_profile_resolve, 4)
 	std	r9,INT_PARMS+48(r1)
 	std	r10,INT_PARMS+56(r1)
 	std	r8,CALLING_SP(r1)
-	ld	r12,.LC__dl_hwcap@toc(r2)
+	addis   r12,r2,.LC__dl_hwcap@toc@ha
+	ld	r12,.LC__dl_hwcap@toc@l(r12)
 #ifdef SHARED
 	/* Load _rtld_local_ro._dl_hwcap.  */
 	ld	r12,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r12)
 #else
-	ld	r12,0(r12) /* Load extern _dl_hwcap.  */
+	/* Load extern _dl_hwcap.  */
+	ld	r12,0(r12)
 #endif
 	andis.  r0,r12,(PPC_FEATURE_HAS_ALTIVEC >> 16)
 	beq	L(saveFP)
diff --git a/sysdeps/powerpc/powerpc64/setjmp-common.S b/sysdeps/powerpc/powerpc64/setjmp-common.S
index 41812e3427..19e76d59ee 100644
--- a/sysdeps/powerpc/powerpc64/setjmp-common.S
+++ b/sysdeps/powerpc/powerpc64/setjmp-common.S
@@ -132,12 +132,14 @@ JUMPTARGET(GLUE(__sigsetjmp_symbol,_ent)):
 	std  r31,((JB_GPRS+17)*8)(3)
 	stfd fp31,((JB_FPRS+17)*8)(3)
 #ifndef __NO_VMX__
-	ld    r6,.LC__dl_hwcap@toc(r2)
+	addis	r6,r2,.LC__dl_hwcap@toc@ha
+	ld	r6,.LC__dl_hwcap@toc@l(r6)
 # ifdef SHARED
 	/* Load _rtld-global._dl_hwcap.  */
-	ld    r6,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r6)
+	ld	r6,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r6)
 # else
-	ld    r6,0(r6) /* Load extern _dl_hwcap.  */
+	/* Load extern _dl_hwcap.  */
+	ld	r6,0(r6)
 # endif
 	andis.  r6,r6,(PPC_FEATURE_HAS_ALTIVEC >> 16)
 	beq	L(no_vmx)
diff --git a/sysdeps/powerpc/powerpc64/start.S b/sysdeps/powerpc/powerpc64/start.S
index 4319dc8d3e..244d9da07b 100644
--- a/sysdeps/powerpc/powerpc64/start.S
+++ b/sysdeps/powerpc/powerpc64/start.S
@@ -74,7 +74,8 @@ ENTRY (_start)
 
  /* put the address of start_addresses in r8...  **
 ** PPC64 ABI uses R13 for thread local, so we leave it alone */
-	ld	r8,.L01@toc(r2)
+	addis	r8,r2,.L01@toc@ha
+	ld	r8,.L01@toc@l(r8)
 
  /* and continue in libc-start, in glibc.  */
 	b	JUMPTARGET(__libc_start_main)
diff --git a/sysdeps/powerpc/powerpc64/sysdep.h b/sysdeps/powerpc/powerpc64/sysdep.h
index 3fec06e0df..011068b290 100644
--- a/sysdeps/powerpc/powerpc64/sysdep.h
+++ b/sysdeps/powerpc/powerpc64/sysdep.h
@@ -469,14 +469,16 @@ LT_LABELSUFFIX(name,_name_end): ; \
 	.tc _rtld_global_ro[TC],_rtld_global_ro
 # endif
 # define __GLRO(rOUT, var, offset)		\
-	ld	rOUT,.LC__ ## var@toc(r2);	\
+	addis	rOUT,r2,.LC__ ## var@toc@ha;	\
+	ld	rOUT,.LC__ ## var@toc@l(rOUT);	\
 	lwz	rOUT,offset(rOUT)
 #else
 # define __GLRO_DEF(var)			\
 .LC__ ## var:					\
 	.tc _ ## var[TC],_ ## var
 # define __GLRO(rOUT, var, offset)		\
-	ld	rOUT,.LC__ ## var@toc(r2);	\
+	addis	rOUT,r2,.LC__ ## var@toc@ha;	\
+	ld	rOUT,.LC__ ## var@toc@l(rOUT);	\
 	lwz	rOUT,0(rOUT)
 #endif
 

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 2/5] [PowerPC64] Set up thread register for _dl_relocate_static_pie
  2022-01-23 12:42 [PowerPC64] Use medium model toc accesses throughout Alan Modra
@ 2022-01-24  3:47 ` Alan Modra
  2022-02-16 23:02   ` Paul E Murphy
  2022-01-24  3:50 ` [PATCH 3/5] [PowerPC] Relocate stinfo->main Alan Modra
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-01-24  3:47 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho

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.

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

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 3/5] [PowerPC] Relocate stinfo->main
  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-01-24  3:50 ` Alan Modra
  2022-01-24  4:48   ` H.J. Lu
  2022-01-24  3:52 ` [PATCH 4/5] Constify a variable in dl_vdso_vsym Alan Modra
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-01-24  3:50 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho

The previous 2 patches got us to the point of segfaulting when trying
to transfer to main.

start_addresses in sysdeps/powerpc/powerpc64/start.S is historical
baggage that should disappear.  Until someone does that, relocating
stinfo->main by hand is one solution to the fact that the field may be
unrelocated at the time it is accessed.  This is similar to what is
done for dynamic tags via the D_PTR macro.  The makefile change is
needed to pick up elf/dl-static-tls.h from dl-machine.h.

diff --git a/sysdeps/unix/sysv/linux/powerpc/Makefile b/sysdeps/unix/sysv/linux/powerpc/Makefile
index fc7c29c695..93783cae00 100644
--- a/sysdeps/unix/sysv/linux/powerpc/Makefile
+++ b/sysdeps/unix/sysv/linux/powerpc/Makefile
@@ -27,3 +27,9 @@ tests += $(tests-static)
 tests += test-gettimebasefreq
 tests += test-powerpc-linux-sysconf
 endif
+
+ifeq ($(subdir),csu)
+# to relocate stinfo->main
+CPPFLAGS-libc-start.o += -I../elf
+CPPFLAGS-libc-start.op += -I../elf
+endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/libc-start.c b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
index bb97d16145..8f0d3a0eac 100644
--- a/sysdeps/unix/sysv/linux/powerpc/libc-start.c
+++ b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
@@ -23,6 +23,10 @@
 #ifndef SHARED
 #include <hwcapinfo.h>
 #endif
+#if ENABLE_STATIC_PIE && !defined SHARED
+/* For elf_machine_load_address.  */
+#include <dl-machine.h>
+#endif
 
 /* The main work is done in the generic function.  */
 #define LIBC_START_MAIN generic_start_main
@@ -95,8 +99,15 @@ __libc_start_main_impl (int argc, char **argv,
   __tcb_parse_hwcap_and_convert_at_platform ();
 #endif
 
-  return generic_start_main (stinfo->main, argc, argv, auxvec,
-			     stinfo->init, stinfo->fini, rtld_fini,
+  void *main = stinfo->main;
+#if ENABLE_STATIC_PIE && !defined SHARED
+  struct link_map *map = _dl_get_dl_main_map ();
+  if (!map->l_relocated)
+    main = (char *) main + elf_machine_load_address ();
+#endif
+
+  return generic_start_main (main, argc, argv, auxvec,
+			     NULL, NULL, rtld_fini,
 			     stack_on_entry);
 }
 DEFINE_LIBC_START_MAIN_VERSION

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 4/5] Constify a variable in dl_vdso_vsym
  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-01-24  3:50 ` [PATCH 3/5] [PowerPC] Relocate stinfo->main Alan Modra
@ 2022-01-24  3:52 ` Alan Modra
  2022-01-24  4:06 ` [PATCH 5/5] Enable static-pie on powerpc64 Alan Modra
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 35+ messages in thread
From: Alan Modra @ 2022-01-24  3:52 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho

Just a tidy.

Compilers may decide to put the rfv variable in .data rather than on
the stack.  It's slightly better to put it in .data.rel.ro.local
instead.  Also, making it const may enable further optimisations.

diff --git a/sysdeps/unix/sysv/linux/dl-vdso.h b/sysdeps/unix/sysv/linux/dl-vdso.h
index bfefce8f35..8611488acc 100644
--- a/sysdeps/unix/sysv/linux/dl-vdso.h
+++ b/sysdeps/unix/sysv/linux/dl-vdso.h
@@ -45,7 +45,7 @@ dl_vdso_vsym (const char *name)
   ElfW (Sym) wsym = { 0 };
   wsym.st_info = (unsigned char) ELFW (ST_INFO (STB_WEAK, STT_NOTYPE));
 
-  struct r_found_version rfv = { VDSO_NAME, VDSO_HASH, 1, NULL };
+  const struct r_found_version rfv = { VDSO_NAME, VDSO_HASH, 1, NULL };
 
   /* Search the scope of the vdso map.  */
   const ElfW (Sym) *ref = &wsym;

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH 5/5] Enable static-pie on powerpc64
  2022-01-23 12:42 [PowerPC64] Use medium model toc accesses throughout Alan Modra
                   ` (2 preceding siblings ...)
  2022-01-24  3:52 ` [PATCH 4/5] Constify a variable in dl_vdso_vsym Alan Modra
@ 2022-01-24  4:06 ` Alan Modra
  2022-01-27 18:39   ` Florian Weimer
  2022-01-28 17:48 ` [PowerPC64] Use medium model toc accesses throughout Paul E Murphy
  2022-02-28  6:40 ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-01-24  4:06 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho

Also regenerate another powerpc configure file that wasn't done when
making the corresponding configure.ac change.

With this series and latest gcc + binutils I get mostly working
static-pie on powerpc64le-linux.  There are a number of regressions:
FAIL: elf/tst-glibc-hwcaps-cache
FAIL: elf/tst-glibc-hwcaps-prepend-cache
FAIL: elf/tst-ldconfig-bad-aux-cache
FAIL: elf/tst-ldconfig-ld_so_conf-update
FAIL: elf/tst-tlsifunc-static

	* sysdeps/powerpc/powerpc64/configure.ac (SUPPORT_STATIC_PIE): Define.
	(PI_STATIC_AND_HIDDEN): Define.
	* sysdeps/powerpc/powerpc64/configure: Regenerate.
	* sysdeps/unix/sysv/linux/powerpc/configure: Regenerate.

diff --git a/sysdeps/powerpc/powerpc64/configure b/sysdeps/powerpc/powerpc64/configure
index fddea0355a..4a219ab8ac 100644
--- a/sysdeps/powerpc/powerpc64/configure
+++ b/sysdeps/powerpc/powerpc64/configure
@@ -1,6 +1,13 @@
 # This file is generated from configure.ac by Autoconf.  DO NOT EDIT!
  # Local configure fragment for sysdeps/powerpc/powerpc64.
 
+$as_echo "#define PI_STATIC_AND_HIDDEN 1" >>confdefs.h
+
+
+# Static PIE is supported.
+$as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
+
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for support for overlapping .opd entries" >&5
 $as_echo_n "checking for support for overlapping .opd entries... " >&6; }
 if ${libc_cv_overlapping_opd+:} false; then :
diff --git a/sysdeps/powerpc/powerpc64/configure.ac b/sysdeps/powerpc/powerpc64/configure.ac
index 1f3d54414c..5eedd2f81c 100644
--- a/sysdeps/powerpc/powerpc64/configure.ac
+++ b/sysdeps/powerpc/powerpc64/configure.ac
@@ -1,6 +1,13 @@
 GLIBC_PROVIDES dnl See aclocal.m4 in the top level source directory.
 # Local configure fragment for sysdeps/powerpc/powerpc64.
 
+dnl It is always possible to access static and hidden symbols in an
+dnl position independent way.
+AC_DEFINE(PI_STATIC_AND_HIDDEN)
+
+# Static PIE is supported.
+AC_DEFINE(SUPPORT_STATIC_PIE)
+
 AC_CACHE_CHECK(for support for overlapping .opd entries,
 libc_cv_overlapping_opd, [dnl
 libc_cv_overlapping_opd=no
diff --git a/sysdeps/unix/sysv/linux/powerpc/configure b/sysdeps/unix/sysv/linux/powerpc/configure
index a4c809fd98..7183573bc0 100644
--- a/sysdeps/unix/sysv/linux/powerpc/configure
+++ b/sysdeps/unix/sysv/linux/powerpc/configure
@@ -93,7 +93,13 @@ EOF
   $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
   test $ac_status = 0; }; }
   then
-    libc_linker_feature=yes
+    if ${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp -Wl,--no-tls-get-addr-optimize -nostdlib \
+	-nostartfiles -fPIC -shared -o conftest.so conftest.c 2>&1 \
+	| grep "warning: --no-tls-get-addr-optimize ignored" > /dev/null 2>&1; then
+      true
+    else
+      libc_linker_feature=yes
+    fi
   fi
   rm -f conftest*
 fi

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 3/5] [PowerPC] Relocate stinfo->main
  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
  0 siblings, 1 reply; 35+ messages in thread
From: H.J. Lu @ 2022-01-24  4:48 UTC (permalink / raw)
  To: Alan Modra; +Cc: GNU C Library, Tulio Magno Quites Machado Filho

On Sun, Jan 23, 2022 at 7:51 PM Alan Modra via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> The previous 2 patches got us to the point of segfaulting when trying
> to transfer to main.
>
> start_addresses in sysdeps/powerpc/powerpc64/start.S is historical
> baggage that should disappear.  Until someone does that, relocating
> stinfo->main by hand is one solution to the fact that the field may be
> unrelocated at the time it is accessed.  This is similar to what is
> done for dynamic tags via the D_PTR macro.  The makefile change is
> needed to pick up elf/dl-static-tls.h from dl-machine.h.
>
> diff --git a/sysdeps/unix/sysv/linux/powerpc/Makefile b/sysdeps/unix/sysv/linux/powerpc/Makefile
> index fc7c29c695..93783cae00 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/Makefile
> +++ b/sysdeps/unix/sysv/linux/powerpc/Makefile
> @@ -27,3 +27,9 @@ tests += $(tests-static)
>  tests += test-gettimebasefreq
>  tests += test-powerpc-linux-sysconf
>  endif
> +
> +ifeq ($(subdir),csu)
> +# to relocate stinfo->main
> +CPPFLAGS-libc-start.o += -I../elf
> +CPPFLAGS-libc-start.op += -I../elf
> +endif
> diff --git a/sysdeps/unix/sysv/linux/powerpc/libc-start.c b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
> index bb97d16145..8f0d3a0eac 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/libc-start.c
> +++ b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
> @@ -23,6 +23,10 @@
>  #ifndef SHARED
>  #include <hwcapinfo.h>
>  #endif
> +#if ENABLE_STATIC_PIE && !defined SHARED
> +/* For elf_machine_load_address.  */
> +#include <dl-machine.h>
> +#endif
>
>  /* The main work is done in the generic function.  */
>  #define LIBC_START_MAIN generic_start_main
> @@ -95,8 +99,15 @@ __libc_start_main_impl (int argc, char **argv,
>    __tcb_parse_hwcap_and_convert_at_platform ();
>  #endif
>
> -  return generic_start_main (stinfo->main, argc, argv, auxvec,
> -                            stinfo->init, stinfo->fini, rtld_fini,
> +  void *main = stinfo->main;
> +#if ENABLE_STATIC_PIE && !defined SHARED
> +  struct link_map *map = _dl_get_dl_main_map ();
> +  if (!map->l_relocated)
> +    main = (char *) main + elf_machine_load_address ();
> +#endif

Can you use __ehdr_start t here, similar to

https://gitlab.com/x86-glibc/glibc/-/commit/1df93c500d0f81d30d1b7e323153512a8768bb72#ffb950808032d5085fdf882753d7bf8889e6a3d7


> +  return generic_start_main (main, argc, argv, auxvec,
> +                            NULL, NULL, rtld_fini,
>                              stack_on_entry);
>  }
>  DEFINE_LIBC_START_MAIN_VERSION
>
> --
> Alan Modra
> Australia Development Lab, IBM



--
H.J.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 3/5] [PowerPC] Relocate stinfo->main
  2022-01-24  4:48   ` H.J. Lu
@ 2022-01-24  6:51     ` Alan Modra
  0 siblings, 0 replies; 35+ messages in thread
From: Alan Modra @ 2022-01-24  6:51 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library, Tulio Magno Quites Machado Filho

On Sun, Jan 23, 2022 at 08:48:44PM -0800, H.J. Lu wrote:
> On Sun, Jan 23, 2022 at 7:51 PM Alan Modra via Libc-alpha
> > +  void *main = stinfo->main;
> > +#if ENABLE_STATIC_PIE && !defined SHARED
> > +  struct link_map *map = _dl_get_dl_main_map ();
> > +  if (!map->l_relocated)
> > +    main = (char *) main + elf_machine_load_address ();
> > +#endif
> 
> Can you use __ehdr_start t here, similar to
> 
> https://gitlab.com/x86-glibc/glibc/-/commit/1df93c500d0f81d30d1b7e323153512a8768bb72#ffb950808032d5085fdf882753d7bf8889e6a3d7

That idea hadn't occurred to me.  Yes, that likely would have worked
too.  I used the old traditional way of calculating l_addr, which
happens to be quite cheap on powerpc64, but admittedly not so good on
powerpc32.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 5/5] Enable static-pie on powerpc64
  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
  0 siblings, 1 reply; 35+ messages in thread
From: Florian Weimer @ 2022-01-27 18:39 UTC (permalink / raw)
  To: Alan Modra via Libc-alpha; +Cc: Alan Modra, Tulio Magno Quites Machado Filho

* Alan Modra via Libc-alpha:

> diff --git a/sysdeps/powerpc/powerpc64/configure.ac b/sysdeps/powerpc/powerpc64/configure.ac
> index 1f3d54414c..5eedd2f81c 100644
> --- a/sysdeps/powerpc/powerpc64/configure.ac
> +++ b/sysdeps/powerpc/powerpc64/configure.ac
> @@ -1,6 +1,13 @@
>  GLIBC_PROVIDES dnl See aclocal.m4 in the top level source directory.
>  # Local configure fragment for sysdeps/powerpc/powerpc64.
>  
> +dnl It is always possible to access static and hidden symbols in an
> +dnl position independent way.
> +AC_DEFINE(PI_STATIC_AND_HIDDEN)

I must say that this is the most surprising glibc patch I've seen in a
while.

But I think it's true—once you've set up the TOC pointer, the link
editor can generate a fixed offset offset for local and hidden symbols.
Has this always been the case for powerpc64le?

Thanks,
Florian


^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 5/5] Enable static-pie on powerpc64
  2022-01-27 18:39   ` Florian Weimer
@ 2022-01-28  8:45     ` Alan Modra
  0 siblings, 0 replies; 35+ messages in thread
From: Alan Modra @ 2022-01-28  8:45 UTC (permalink / raw)
  To: Florian Weimer
  Cc: Alan Modra via Libc-alpha, Tulio Magno Quites Machado Filho

On Thu, Jan 27, 2022 at 07:39:44PM +0100, Florian Weimer wrote:
> * Alan Modra via Libc-alpha:
> 
> > diff --git a/sysdeps/powerpc/powerpc64/configure.ac b/sysdeps/powerpc/powerpc64/configure.ac
> > index 1f3d54414c..5eedd2f81c 100644
> > --- a/sysdeps/powerpc/powerpc64/configure.ac
> > +++ b/sysdeps/powerpc/powerpc64/configure.ac
> > @@ -1,6 +1,13 @@
> >  GLIBC_PROVIDES dnl See aclocal.m4 in the top level source directory.
> >  # Local configure fragment for sysdeps/powerpc/powerpc64.
> >  
> > +dnl It is always possible to access static and hidden symbols in an
> > +dnl position independent way.
> > +AC_DEFINE(PI_STATIC_AND_HIDDEN)
> 
> I must say that this is the most surprising glibc patch I've seen in a
> while.
> 
> But I think it's true—once you've set up the TOC pointer, the link
> editor can generate a fixed offset offset for local and hidden symbols.
> Has this always been the case for powerpc64le?

2010-06-25 binutils git commit ba761f19f5.  I'm not at all sure the
optimisation was perfect in binutils-2.21, but yes, medium and large
code model toc-indirect code has been converted to toc-relative for a
long time.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PowerPC64] Use medium model toc accesses throughout
  2022-01-23 12:42 [PowerPC64] Use medium model toc accesses throughout Alan Modra
                   ` (3 preceding siblings ...)
  2022-01-24  4:06 ` [PATCH 5/5] Enable static-pie on powerpc64 Alan Modra
@ 2022-01-28 17:48 ` Paul E Murphy
  2022-01-29  1:24   ` Alan Modra
  2022-02-28  6:40 ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
  5 siblings, 1 reply; 35+ messages in thread
From: Paul E Murphy @ 2022-01-28 17:48 UTC (permalink / raw)
  To: Alan Modra, libc-alpha; +Cc: Tulio Magno Quites Machado Filho



On 1/23/22 6:42 AM, Alan Modra via Libc-alpha wrote:
> The PowerPC64 linker edits medium model toc-indirect code to toc-pointer
> relative:
> 	addis r9,r2,tc_entry_for_var@toc@ha
> 	ld r9,tc_entry_for_var@toc@l(r9)
> becomes
> 	addis r9,r2,(var-.TOC.)@ha
> 	addi r9,r9,(var-.TOC.)@l
> when "var" is known to be local to the binary.  This isn't done for
> small-model toc-indirect code, because "var" is almost guaranteed to
> be too far away from .TOC. for a 16-bit signed offset.  And, because
> the analysis of which .toc entry can be removed becomes much more
> complicated in objects that mix code models, they aren't removed if
> any small-model toc sequence appears in an object file.
> 
> Unfortunately glibc's build of ld.so smashes the needed objects
> together in a ld -r linking stage.  This means the GOT/TOC is left
> with a whole lot of relative relocations which is untidy, but in
> itself is not a serious problem.  However, static-pie on powerpc64
> bombs due to a segfault caused by one of the small-model accesses
> before _dl_relocate_static_pie.  (The very first one in rcrt1.o
> passing start_addresses in r8 to __libc_start_main.)
> 
> So this patch makes all the toc/got accesses in assembly medium code
> model, and a couple of functions hidden.  By itself this is *not*
> enough to give us working static-pie, but it is useful anyway to
> enable better linker optimisation.
> 
> 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.
> 
> Tested for regressions with a powerpc64le-linux build and test run.
> OK to apply?
> 

> diff --git a/sysdeps/powerpc/powerpc64/dl-trampoline.S b/sysdeps/powerpc/powerpc64/dl-trampoline.S
> index 23debc2faf..45b821607b 100644
> --- a/sysdeps/powerpc/powerpc64/dl-trampoline.S
> +++ b/sysdeps/powerpc/powerpc64/dl-trampoline.S
> @@ -32,6 +32,7 @@
>      because gcc as of 2010/05 doesn't allocate a proper stack frame for
>      a function that makes no calls except for __tls_get_addr and we
>      might be here resolving the __tls_get_addr call.  */
> +	.hidden _dl_runtime_resolve
>   #define INT_PARMS FRAME_MIN_SIZE
>   ENTRY (_dl_runtime_resolve, 4)
>   	stdu	r1,-FRAME_SIZE(r1)
> @@ -195,6 +196,7 @@ END(_dl_runtime_resolve)
>      parm1 (r3) and the index (r0) needs to be converted to an offset
>      (index * 24) in parm2 (r4).  */
>   #ifndef PROF
> +	.hidden _dl_profile_resolve
>   ENTRY (_dl_profile_resolve, 4)
>   /* Spill r30, r31 to preserve the link_map* and reloc_addr, in case we
>      need to call _dl_audit_pltexit.  */

LGTM (this should wait until after the freeze), though these two changes 
seem orthogonal to the commit title.

For my education, why are are small model accesses used in these files?

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PowerPC64] Use medium model toc accesses throughout
  2022-01-28 17:48 ` [PowerPC64] Use medium model toc accesses throughout Paul E Murphy
@ 2022-01-29  1:24   ` Alan Modra
  0 siblings, 0 replies; 35+ messages in thread
From: Alan Modra @ 2022-01-29  1:24 UTC (permalink / raw)
  To: Paul E Murphy; +Cc: libc-alpha, Tulio Magno Quites Machado Filho

On Fri, Jan 28, 2022 at 11:48:44AM -0600, Paul E Murphy wrote:
> > --- a/sysdeps/powerpc/powerpc64/dl-trampoline.S
> > +++ b/sysdeps/powerpc/powerpc64/dl-trampoline.S
> > @@ -32,6 +32,7 @@
> >      because gcc as of 2010/05 doesn't allocate a proper stack frame for
> >      a function that makes no calls except for __tls_get_addr and we
> >      might be here resolving the __tls_get_addr call.  */
> > +	.hidden _dl_runtime_resolve
> >   #define INT_PARMS FRAME_MIN_SIZE
> >   ENTRY (_dl_runtime_resolve, 4)
> >   	stdu	r1,-FRAME_SIZE(r1)
> > @@ -195,6 +196,7 @@ END(_dl_runtime_resolve)
> >      parm1 (r3) and the index (r0) needs to be converted to an offset
> >      (index * 24) in parm2 (r4).  */
> >   #ifndef PROF
> > +	.hidden _dl_profile_resolve
> >   ENTRY (_dl_profile_resolve, 4)
> >   /* Spill r30, r31 to preserve the link_map* and reloc_addr, in case we
> >      need to call _dl_audit_pltexit.  */
> 
> LGTM (this should wait until after the freeze), though these two changes
> seem orthogonal to the commit title.

True.  It could have been a separate patch.

> For my education, why are are small model accesses used in these files?

dl-trampoline.S is an old file, from before the linker edited
toc-indirect code to toc-pointer relative.  Without the link editing
small model is more efficient if the toc is small enough.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 2/5] [PowerPC64] Set up thread register for _dl_relocate_static_pie
  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
  0 siblings, 1 reply; 35+ messages in thread
From: Paul E Murphy @ 2022-02-16 23:02 UTC (permalink / raw)
  To: Alan Modra, libc-alpha; +Cc: Tulio Magno Quites Machado Filho



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?



^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH 2/5] [PowerPC64] Set up thread register for _dl_relocate_static_pie
  2022-02-16 23:02   ` Paul E Murphy
@ 2022-02-19  0:49     ` Alan Modra
  0 siblings, 0 replies; 35+ messages in thread
From: Alan Modra @ 2022-02-19  0:49 UTC (permalink / raw)
  To: Paul E Murphy; +Cc: libc-alpha, Tulio Magno Quites Machado Filho

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.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v2 0/4] PowerPC64 static-pie
  2022-01-23 12:42 [PowerPC64] Use medium model toc accesses throughout Alan Modra
                   ` (4 preceding siblings ...)
  2022-01-28 17:48 ` [PowerPC64] Use medium model toc accesses throughout Paul E Murphy
@ 2022-02-28  6:40 ` Alan Modra
  2022-02-28  6:40   ` [PATCH v2 1/4] powerpc64: Use medium model toc accesses throughout Alan Modra
                     ` (5 more replies)
  5 siblings, 6 replies; 35+ messages in thread
From: Alan Modra @ 2022-02-28  6:40 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho, Alan Modra

This is a repost of the series at
https://sourceware.org/pipermail/libc-alpha/2022-January/135598.html
incorporating review comments.

Changes are:
- subject lines changed to comply with glibc commit log standards
- patch 1/4 log now mentions making a couple of symbols hidden
- patch 2/4 makes at_platform available to early ifunc resolvers
  as well as hwcap.
- patch 3/4 log mentions stinfo->init and stinfo->fini being unused
- patch 4/4 PI_STATIC_AND_HIDDEN comment explains what is going on

patch 2/4 is the only code change from the previous series.  Testsuite
results now show no regressions on powerpc64le-linux.

I haven't reposted the old patch 4/5 as it was just a side-issue I
noticed when looking over relative relocations.
https://sourceware.org/pipermail/libc-alpha/2022-January/135603.html 
I think it qualifies as an obvious patch so will commit it separately
from this series.

Alan Modra (4):
  powerpc64: Use medium model toc accesses throughout
  powerpc64: Set up thread register for _dl_relocate_static_pie
  powerpc: Relocate stinfo->main
  powerpc64: Enable static-pie

 sysdeps/powerpc/hwcapinfo.c                  |  8 ++---
 sysdeps/powerpc/hwcapinfo.h                  |  3 +-
 sysdeps/powerpc/nptl/tls.h                   |  8 ++---
 sysdeps/powerpc/powerpc64/__longjmp-common.S |  8 +++--
 sysdeps/powerpc/powerpc64/configure          |  6 ++++
 sysdeps/powerpc/powerpc64/configure.ac       |  9 ++++++
 sysdeps/powerpc/powerpc64/dl-machine.h       | 33 +++++++++++++++++---
 sysdeps/powerpc/powerpc64/dl-trampoline.S    |  8 +++--
 sysdeps/powerpc/powerpc64/setjmp-common.S    |  8 +++--
 sysdeps/powerpc/powerpc64/start.S            |  3 +-
 sysdeps/powerpc/powerpc64/sysdep.h           |  6 ++--
 sysdeps/unix/sysv/linux/powerpc/Makefile     |  6 ++++
 sysdeps/unix/sysv/linux/powerpc/libc-start.c | 15 +++++++--
 13 files changed, 93 insertions(+), 28 deletions(-)


^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v2 1/4] powerpc64: Use medium model toc accesses throughout
  2022-02-28  6:40 ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
@ 2022-02-28  6:40   ` Alan Modra
  2022-04-08 22:28     ` Tulio Magno Quites Machado Filho
  2022-02-28  6:40   ` [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie Alan Modra
                     ` (4 subsequent siblings)
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-02-28  6:40 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho, Alan Modra

The PowerPC64 linker edits medium model toc-indirect code to toc-pointer
relative:
	addis r9,r2,tc_entry_for_var@toc@ha
	ld r9,tc_entry_for_var@toc@l(r9)
becomes
	addis r9,r2,(var-.TOC.)@ha
	addi r9,r9,(var-.TOC.)@l
when "var" is known to be local to the binary.  This isn't done for
small-model toc-indirect code, because "var" is almost guaranteed to
be too far away from .TOC. for a 16-bit signed offset.  And, because
the analysis of which .toc entry can be removed becomes much more
complicated in objects that mix code models, they aren't removed if
any small-model toc sequence appears in an object file.

Unfortunately, glibc's build of ld.so smashes the needed objects
together in a ld -r linking stage.  This means the GOT/TOC is left
with a whole lot of relative relocations which is untidy, but in
itself is not a serious problem.  However, static-pie on powerpc64
bombs due to a segfault caused by one of the small-model accesses
before _dl_relocate_static_pie.  (The very first one in rcrt1.o
passing start_addresses in r8 to __libc_start_main.)

So this patch makes all the toc/got accesses in assembly medium code
model, and a couple of functions hidden.  By itself this is not
enough to give us working static-pie, but it is useful in isolation to
enable better linker optimisation.

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.
A followup patch will fix that.

diff --git a/sysdeps/powerpc/powerpc64/__longjmp-common.S b/sysdeps/powerpc/powerpc64/__longjmp-common.S
index 4d71b9e102..5f629e1e0f 100644
--- a/sysdeps/powerpc/powerpc64/__longjmp-common.S
+++ b/sysdeps/powerpc/powerpc64/__longjmp-common.S
@@ -47,12 +47,14 @@
 ENTRY (__longjmp)
 	CALL_MCOUNT 2
 #ifndef __NO_VMX__
-	ld    r5,.LC__dl_hwcap@toc(r2)
+	addis	r5,r2,.LC__dl_hwcap@toc@ha
+	ld	r5,.LC__dl_hwcap@toc@l(r5)
 # ifdef SHARED
 	/* Load _rtld-global._dl_hwcap.  */
-	ld    r5,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r5)
+	ld	r5,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r5)
 # else
-	ld    r5,0(r5) /* Load extern _dl_hwcap.  */
+	/* Load extern _dl_hwcap.  */
+	ld	r5,0(r5)
 # endif
 	andis.  r5,r5,(PPC_FEATURE_HAS_ALTIVEC >> 16)
 	beq	L(no_vmx)
diff --git a/sysdeps/powerpc/powerpc64/dl-machine.h b/sysdeps/powerpc/powerpc64/dl-machine.h
index 5da5de7a0a..6fab5cbe81 100644
--- a/sysdeps/powerpc/powerpc64/dl-machine.h
+++ b/sysdeps/powerpc/powerpc64/dl-machine.h
@@ -175,9 +175,12 @@ BODY_PREFIX "_dl_start_user:\n"						\
 /* the address of _start in r30.  */					\
 "	mr	30,3\n"							\
 /* &_dl_argc in 29, &_dl_argv in 27, and _dl_loaded in 28.  */		\
-"	ld	28,.LC__rtld_local@toc(2)\n"				\
-"	ld	29,.LC__dl_argc@toc(2)\n"				\
-"	ld	27,.LC__dl_argv@toc(2)\n"				\
+"	addis	28,2,.LC__rtld_local@toc@ha\n"				\
+"	ld	28,.LC__rtld_local@toc@l(28)\n"				\
+"	addis	29,2,.LC__dl_argc@toc@ha\n"				\
+"	ld	29,.LC__dl_argc@toc@l(29)\n"				\
+"	addis	27,2,.LC__dl_argv@toc@ha\n"				\
+"	ld	27,.LC__dl_argv@toc@l(27)\n"				\
 /* _dl_init (_dl_loaded, _dl_argc, _dl_argv, _dl_argv+_dl_argc+1).  */	\
 "	ld	3,0(28)\n"						\
 "	lwa	4,0(29)\n"						\
@@ -204,7 +207,8 @@ BODY_PREFIX "_dl_start_user:\n"						\
 "	addi	6,6,8\n"						\
 /* Pass a termination function pointer (in this case _dl_fini) in	\
    r7.  */								\
-"	ld	7,.LC__dl_fini@toc(2)\n"				\
+"	addis	7,2,.LC__dl_fini@toc@ha\n"				\
+"	ld	7,.LC__dl_fini@toc@l(7)\n"				\
 /* Pass the stack pointer in r1 (so far so good), pointing to a NULL	\
    value.  This lets our startup code distinguish between a program	\
    linked statically, which linux will call with argc on top of the	\
diff --git a/sysdeps/powerpc/powerpc64/dl-trampoline.S b/sysdeps/powerpc/powerpc64/dl-trampoline.S
index 23debc2faf..45b821607b 100644
--- a/sysdeps/powerpc/powerpc64/dl-trampoline.S
+++ b/sysdeps/powerpc/powerpc64/dl-trampoline.S
@@ -32,6 +32,7 @@
    because gcc as of 2010/05 doesn't allocate a proper stack frame for
    a function that makes no calls except for __tls_get_addr and we
    might be here resolving the __tls_get_addr call.  */
+	.hidden _dl_runtime_resolve
 #define INT_PARMS FRAME_MIN_SIZE
 ENTRY (_dl_runtime_resolve, 4)
 	stdu	r1,-FRAME_SIZE(r1)
@@ -195,6 +196,7 @@ END(_dl_runtime_resolve)
    parm1 (r3) and the index (r0) needs to be converted to an offset
    (index * 24) in parm2 (r4).  */
 #ifndef PROF
+	.hidden _dl_profile_resolve
 ENTRY (_dl_profile_resolve, 4)
 /* Spill r30, r31 to preserve the link_map* and reloc_addr, in case we
    need to call _dl_audit_pltexit.  */
@@ -225,12 +227,14 @@ ENTRY (_dl_profile_resolve, 4)
 	std	r9,INT_PARMS+48(r1)
 	std	r10,INT_PARMS+56(r1)
 	std	r8,CALLING_SP(r1)
-	ld	r12,.LC__dl_hwcap@toc(r2)
+	addis   r12,r2,.LC__dl_hwcap@toc@ha
+	ld	r12,.LC__dl_hwcap@toc@l(r12)
 #ifdef SHARED
 	/* Load _rtld_local_ro._dl_hwcap.  */
 	ld	r12,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r12)
 #else
-	ld	r12,0(r12) /* Load extern _dl_hwcap.  */
+	/* Load extern _dl_hwcap.  */
+	ld	r12,0(r12)
 #endif
 	andis.  r0,r12,(PPC_FEATURE_HAS_ALTIVEC >> 16)
 	beq	L(saveFP)
diff --git a/sysdeps/powerpc/powerpc64/setjmp-common.S b/sysdeps/powerpc/powerpc64/setjmp-common.S
index 41812e3427..19e76d59ee 100644
--- a/sysdeps/powerpc/powerpc64/setjmp-common.S
+++ b/sysdeps/powerpc/powerpc64/setjmp-common.S
@@ -132,12 +132,14 @@ JUMPTARGET(GLUE(__sigsetjmp_symbol,_ent)):
 	std  r31,((JB_GPRS+17)*8)(3)
 	stfd fp31,((JB_FPRS+17)*8)(3)
 #ifndef __NO_VMX__
-	ld    r6,.LC__dl_hwcap@toc(r2)
+	addis	r6,r2,.LC__dl_hwcap@toc@ha
+	ld	r6,.LC__dl_hwcap@toc@l(r6)
 # ifdef SHARED
 	/* Load _rtld-global._dl_hwcap.  */
-	ld    r6,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r6)
+	ld	r6,RTLD_GLOBAL_RO_DL_HWCAP_OFFSET(r6)
 # else
-	ld    r6,0(r6) /* Load extern _dl_hwcap.  */
+	/* Load extern _dl_hwcap.  */
+	ld	r6,0(r6)
 # endif
 	andis.  r6,r6,(PPC_FEATURE_HAS_ALTIVEC >> 16)
 	beq	L(no_vmx)
diff --git a/sysdeps/powerpc/powerpc64/start.S b/sysdeps/powerpc/powerpc64/start.S
index 4319dc8d3e..244d9da07b 100644
--- a/sysdeps/powerpc/powerpc64/start.S
+++ b/sysdeps/powerpc/powerpc64/start.S
@@ -74,7 +74,8 @@ ENTRY (_start)
 
  /* put the address of start_addresses in r8...  **
 ** PPC64 ABI uses R13 for thread local, so we leave it alone */
-	ld	r8,.L01@toc(r2)
+	addis	r8,r2,.L01@toc@ha
+	ld	r8,.L01@toc@l(r8)
 
  /* and continue in libc-start, in glibc.  */
 	b	JUMPTARGET(__libc_start_main)
diff --git a/sysdeps/powerpc/powerpc64/sysdep.h b/sysdeps/powerpc/powerpc64/sysdep.h
index 3fec06e0df..011068b290 100644
--- a/sysdeps/powerpc/powerpc64/sysdep.h
+++ b/sysdeps/powerpc/powerpc64/sysdep.h
@@ -469,14 +469,16 @@ LT_LABELSUFFIX(name,_name_end): ; \
 	.tc _rtld_global_ro[TC],_rtld_global_ro
 # endif
 # define __GLRO(rOUT, var, offset)		\
-	ld	rOUT,.LC__ ## var@toc(r2);	\
+	addis	rOUT,r2,.LC__ ## var@toc@ha;	\
+	ld	rOUT,.LC__ ## var@toc@l(rOUT);	\
 	lwz	rOUT,offset(rOUT)
 #else
 # define __GLRO_DEF(var)			\
 .LC__ ## var:					\
 	.tc _ ## var[TC],_ ## var
 # define __GLRO(rOUT, var, offset)		\
-	ld	rOUT,.LC__ ## var@toc(r2);	\
+	addis	rOUT,r2,.LC__ ## var@toc@ha;	\
+	ld	rOUT,.LC__ ## var@toc@l(rOUT);	\
 	lwz	rOUT,0(rOUT)
 #endif
 

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie
  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-02-28  6:40   ` Alan Modra
  2022-04-08 22:28     ` Tulio Magno Quites Machado Filho
  2022-02-28  6:40   ` [PATCH v2 3/4] powerpc: Relocate stinfo->main Alan Modra
                     ` (3 subsequent siblings)
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-02-28  6:40 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho, Alan Modra

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");

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v2 3/4] powerpc: Relocate stinfo->main
  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-02-28  6:40   ` [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie Alan Modra
@ 2022-02-28  6:40   ` 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
                     ` (2 subsequent siblings)
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-02-28  6:40 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho, Alan Modra

start_addresses in sysdeps/powerpc/powerpc64/start.S is historical
baggage that should disappear.  Until someone does that, relocating
stinfo->main by hand is one solution to the fact that the field may be
unrelocated at the time it is accessed.  This is similar to what is
done for dynamic tags via the D_PTR macro.  stinfo->init and
stinfo->fini are zero in both powerpc64/start.S and powerpc32/start.S,
so make it a little more obvious they are unused by passing NULLs to
LIBC_START_MAIN.  The makefile change is needed to pick up
elf/dl-static-tls.h from dl-machine.h.

diff --git a/sysdeps/unix/sysv/linux/powerpc/Makefile b/sysdeps/unix/sysv/linux/powerpc/Makefile
index fc7c29c695..93783cae00 100644
--- a/sysdeps/unix/sysv/linux/powerpc/Makefile
+++ b/sysdeps/unix/sysv/linux/powerpc/Makefile
@@ -27,3 +27,9 @@ tests += $(tests-static)
 tests += test-gettimebasefreq
 tests += test-powerpc-linux-sysconf
 endif
+
+ifeq ($(subdir),csu)
+# to relocate stinfo->main
+CPPFLAGS-libc-start.o += -I../elf
+CPPFLAGS-libc-start.op += -I../elf
+endif
diff --git a/sysdeps/unix/sysv/linux/powerpc/libc-start.c b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
index bb97d16145..8f0d3a0eac 100644
--- a/sysdeps/unix/sysv/linux/powerpc/libc-start.c
+++ b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
@@ -23,6 +23,10 @@
 #ifndef SHARED
 #include <hwcapinfo.h>
 #endif
+#if ENABLE_STATIC_PIE && !defined SHARED
+/* For elf_machine_load_address.  */
+#include <dl-machine.h>
+#endif
 
 /* The main work is done in the generic function.  */
 #define LIBC_START_MAIN generic_start_main
@@ -95,8 +99,15 @@ __libc_start_main_impl (int argc, char **argv,
   __tcb_parse_hwcap_and_convert_at_platform ();
 #endif
 
-  return generic_start_main (stinfo->main, argc, argv, auxvec,
-			     stinfo->init, stinfo->fini, rtld_fini,
+  void *main = stinfo->main;
+#if ENABLE_STATIC_PIE && !defined SHARED
+  struct link_map *map = _dl_get_dl_main_map ();
+  if (!map->l_relocated)
+    main = (char *) main + elf_machine_load_address ();
+#endif
+
+  return generic_start_main (main, argc, argv, auxvec,
+			     NULL, NULL, rtld_fini,
 			     stack_on_entry);
 }
 DEFINE_LIBC_START_MAIN_VERSION

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v2 4/4] powerpc64: Enable static-pie
  2022-02-28  6:40 ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
                     ` (2 preceding siblings ...)
  2022-02-28  6:40   ` [PATCH v2 3/4] powerpc: Relocate stinfo->main Alan Modra
@ 2022-02-28  6:40   ` Alan Modra
  2022-04-08 22:49     ` Tulio Magno Quites Machado Filho
  2022-03-04 12:48   ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
  2022-04-08 22:27   ` [PATCH v2 0/4] PowerPC64 static-pie Tulio Magno Quites Machado Filho
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-02-28  6:40 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho, Alan Modra

	* sysdeps/powerpc/powerpc64/configure.ac (SUPPORT_STATIC_PIE): Define.
	(PI_STATIC_AND_HIDDEN): Define.
	* sysdeps/powerpc/powerpc64/configure: Regenerate.

diff --git a/sysdeps/powerpc/powerpc64/configure b/sysdeps/powerpc/powerpc64/configure
index fddea0355a..f19dd5578e 100644
--- a/sysdeps/powerpc/powerpc64/configure
+++ b/sysdeps/powerpc/powerpc64/configure
@@ -1,6 +1,12 @@
 # This file is generated from configure.ac by Autoconf.  DO NOT EDIT!
  # Local configure fragment for sysdeps/powerpc/powerpc64.
 
+$as_echo "#define PI_STATIC_AND_HIDDEN 1" >>confdefs.h
+
+
+$as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
+
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for support for overlapping .opd entries" >&5
 $as_echo_n "checking for support for overlapping .opd entries... " >&6; }
 if ${libc_cv_overlapping_opd+:} false; then :
diff --git a/sysdeps/powerpc/powerpc64/configure.ac b/sysdeps/powerpc/powerpc64/configure.ac
index 1f3d54414c..7764a65822 100644
--- a/sysdeps/powerpc/powerpc64/configure.ac
+++ b/sysdeps/powerpc/powerpc64/configure.ac
@@ -1,6 +1,15 @@
 GLIBC_PROVIDES dnl See aclocal.m4 in the top level source directory.
 # Local configure fragment for sysdeps/powerpc/powerpc64.
 
+dnl It is possible to access static and hidden symbols in a position
+dnl independent way requiring no relocation on powerpc64.  The linker
+dnl edits medium and large model code that uses GOT/TOC entries (which
+dnl would require relocation) to r2 relative accesses.
+AC_DEFINE(PI_STATIC_AND_HIDDEN)
+
+dnl Static PIE is supported.
+AC_DEFINE(SUPPORT_STATIC_PIE)
+
 AC_CACHE_CHECK(for support for overlapping .opd entries,
 libc_cv_overlapping_opd, [dnl
 libc_cv_overlapping_opd=no

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 0/4] PowerPC64 static-pie
  2022-02-28  6:40 ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
                     ` (3 preceding siblings ...)
  2022-02-28  6:40   ` [PATCH v2 4/4] powerpc64: Enable static-pie Alan Modra
@ 2022-03-04 12:48   ` Alan Modra
  2022-04-08  8:06     ` Alan Modra
  2022-04-08 22:27   ` [PATCH v2 0/4] PowerPC64 static-pie Tulio Magno Quites Machado Filho
  5 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-03-04 12:48 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho

On Mon, Feb 28, 2022 at 05:10:48PM +1030, Alan Modra wrote:
> This is a repost of the series at
> https://sourceware.org/pipermail/libc-alpha/2022-January/135598.html
> incorporating review comments.

I neglected to say that this series was tested powerpc64le-linux
showing no regressions.  I also tested powerpc64-linux today, both
static-pie and dt_relr no regressions.  (Well, there were 3 tests that
regressed, but passed when run by hand.  Probably some sort of
resource starvation.)

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 0/4] PowerPC64 static-pie
  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
  0 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-04-08  8:06 UTC (permalink / raw)
  To: libc-alpha; +Cc: Tulio Magno Quites Machado Filho

On Fri, Mar 04, 2022 at 11:18:10PM +1030, Alan Modra wrote:
> On Mon, Feb 28, 2022 at 05:10:48PM +1030, Alan Modra wrote:
> > This is a repost of the series at
> > https://sourceware.org/pipermail/libc-alpha/2022-January/135598.html
> > incorporating review comments.
> 
> I neglected to say that this series was tested powerpc64le-linux
> showing no regressions.  I also tested powerpc64-linux today, both
> static-pie and dt_relr no regressions.  (Well, there were 3 tests that
> regressed, but passed when run by hand.  Probably some sort of
> resource starvation.)

Ping.

https://sourceware.org/pipermail/libc-alpha/2022-February/136727.html

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 0/4] PowerPC64 static-pie
  2022-02-28  6:40 ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
                     ` (4 preceding siblings ...)
  2022-03-04 12:48   ` [PATCH v2 0/4] PowerPC64 static-pie Alan Modra
@ 2022-04-08 22:27   ` Tulio Magno Quites Machado Filho
  2022-04-11  1:38     ` Alan Modra
  5 siblings, 1 reply; 35+ messages in thread
From: Tulio Magno Quites Machado Filho @ 2022-04-08 22:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: Alan Modra

Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:

> This is a repost of the series at
> https://sourceware.org/pipermail/libc-alpha/2022-January/135598.html
> incorporating review comments.
>
> Changes are:
> - subject lines changed to comply with glibc commit log standards
> - patch 1/4 log now mentions making a couple of symbols hidden
> - patch 2/4 makes at_platform available to early ifunc resolvers
>   as well as hwcap.
> - patch 3/4 log mentions stinfo->init and stinfo->fini being unused
> - patch 4/4 PI_STATIC_AND_HIDDEN comment explains what is going on
>
> patch 2/4 is the only code change from the previous series.  Testsuite
> results now show no regressions on powerpc64le-linux.

I'm still seeing the following failures on powerpc64le-linux:

FAIL: elf/tst-tls1-static-non-pie
FAIL: gmon/tst-gmon-static
FAIL: gmon/tst-gmon-static-gprof

Am I missing anything?
I used different GCC and Binutils versions ranging between:
 - GCC v8 and v11
 - Binutils 2.30 and 2.38 (2020-03-04)

Anyway, this is a summary of what I think about these patches:

- Patch 1: Looks good to me.
- Patch 2: Looks good to me.
- Patch 3: Looks good to me after a minor change.
- Patch 4: Hopefully I'm just missing a detail. Otherwise, I believe we should
           delay it until the previous issues are fixed.

Let me elaborate this replying to each patch.

-- 
Tulio Magno

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 1/4] powerpc64: Use medium model toc accesses throughout
  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
  0 siblings, 0 replies; 35+ messages in thread
From: Tulio Magno Quites Machado Filho @ 2022-04-08 22:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: Alan Modra

Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:

> The PowerPC64 linker edits medium model toc-indirect code to toc-pointer
> relative:
> 	addis r9,r2,tc_entry_for_var@toc@ha
> 	ld r9,tc_entry_for_var@toc@l(r9)
> becomes
> 	addis r9,r2,(var-.TOC.)@ha
> 	addi r9,r9,(var-.TOC.)@l
> when "var" is known to be local to the binary.  This isn't done for
> small-model toc-indirect code, because "var" is almost guaranteed to
> be too far away from .TOC. for a 16-bit signed offset.  And, because
> the analysis of which .toc entry can be removed becomes much more
> complicated in objects that mix code models, they aren't removed if
> any small-model toc sequence appears in an object file.
>
> Unfortunately, glibc's build of ld.so smashes the needed objects
> together in a ld -r linking stage.  This means the GOT/TOC is left
> with a whole lot of relative relocations which is untidy, but in
> itself is not a serious problem.  However, static-pie on powerpc64
> bombs due to a segfault caused by one of the small-model accesses
> before _dl_relocate_static_pie.  (The very first one in rcrt1.o
> passing start_addresses in r8 to __libc_start_main.)
>
> So this patch makes all the toc/got accesses in assembly medium code
> model, and a couple of functions hidden.  By itself this is not
> enough to give us working static-pie, but it is useful in isolation to
> enable better linker optimisation.
>
> 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.
> A followup patch will fix that.

Reviewed-by: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>

-- 
Tulio Magno

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie
  2022-02-28  6:40   ` [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie Alan Modra
@ 2022-04-08 22:28     ` Tulio Magno Quites Machado Filho
  0 siblings, 0 replies; 35+ messages in thread
From: Tulio Magno Quites Machado Filho @ 2022-04-08 22:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: Alan Modra

Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:

> 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.

Reviewed-by: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>

-- 
Tulio Magno

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 3/4] powerpc: Relocate stinfo->main
  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
  0 siblings, 0 replies; 35+ messages in thread
From: Tulio Magno Quites Machado Filho @ 2022-04-08 22:32 UTC (permalink / raw)
  To: libc-alpha; +Cc: Alan Modra

Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:

> diff --git a/sysdeps/unix/sysv/linux/powerpc/libc-start.c b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
> index bb97d16145..8f0d3a0eac 100644
> --- a/sysdeps/unix/sysv/linux/powerpc/libc-start.c
> +++ b/sysdeps/unix/sysv/linux/powerpc/libc-start.c
> @@ -23,6 +23,10 @@
>  #ifndef SHARED
>  #include <hwcapinfo.h>
>  #endif
> +#if ENABLE_STATIC_PIE && !defined SHARED
> +/* For elf_machine_load_address.  */
> +#include <dl-machine.h>
> +#endif
>  
>  /* The main work is done in the generic function.  */
>  #define LIBC_START_MAIN generic_start_main
> @@ -95,8 +99,15 @@ __libc_start_main_impl (int argc, char **argv,
>    __tcb_parse_hwcap_and_convert_at_platform ();
>  #endif
>  
> -  return generic_start_main (stinfo->main, argc, argv, auxvec,
> -			     stinfo->init, stinfo->fini, rtld_fini,
> +  void *main = stinfo->main;

Some GCC versions (e.g. GCC 8) complain about the name of this variable:

../sysdeps/unix/sysv/linux/powerpc/libc-start.c: In function ‘__libc_start_main_impl’:
../sysdeps/unix/sysv/linux/powerpc/libc-start.c:102:9: error: ‘main’ is usually a function [-Werror=main]

Looks good to me if this variable is either renamed or if this part of the code
is built with -Wno-main.

Reviewed-by: Tulio Magno Quites Machado Filho <tuliom@linux.ibm.com>

-- 
Tulio Magno

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 4/4] powerpc64: Enable static-pie
  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
  0 siblings, 1 reply; 35+ messages in thread
From: Tulio Magno Quites Machado Filho @ 2022-04-08 22:49 UTC (permalink / raw)
  To: libc-alpha; +Cc: Alan Modra

Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:

> 	* sysdeps/powerpc/powerpc64/configure.ac (SUPPORT_STATIC_PIE): Define.
> 	(PI_STATIC_AND_HIDDEN): Define.
> 	* sysdeps/powerpc/powerpc64/configure: Regenerate.

While the code in this patch is perfect, I'm slightly inclined to think its
merge should be delayed until the failures on both powerpc64-linux and
powerpc64le-linux are fixed.

Another option would be to restrict this code to powerpc64le-linux only,
although I haven't had the success you mentioned in the cover letter.

Anyway, in one of my tests in particular (powerpc64le-linux, GCC 8, Binutils
2.30, without --enable-static-pie) I noticed many tests failing.  I'm still
investigating what happened.

-- 
Tulio Magno

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 0/4] PowerPC64 static-pie
  2022-04-08  8:06     ` Alan Modra
@ 2022-04-09  0:14       ` Fangrui Song
  2022-04-14  0:33         ` Alan Modra
  0 siblings, 1 reply; 35+ messages in thread
From: Fangrui Song @ 2022-04-09  0:14 UTC (permalink / raw)
  To: Alan Modra; +Cc: libc-alpha, Tulio Magno Quites Machado Filho

On 2022-04-08, Alan Modra via Libc-alpha wrote:
>On Fri, Mar 04, 2022 at 11:18:10PM +1030, Alan Modra wrote:
>> On Mon, Feb 28, 2022 at 05:10:48PM +1030, Alan Modra wrote:
>> > This is a repost of the series at
>> > https://sourceware.org/pipermail/libc-alpha/2022-January/135598.html
>> > incorporating review comments.
>>
>> I neglected to say that this series was tested powerpc64le-linux
>> showing no regressions.  I also tested powerpc64-linux today, both
>> static-pie and dt_relr no regressions.  (Well, there were 3 tests that
>> regressed, but passed when run by hand.  Probably some sort of
>> resource starvation.)
>
>Ping.
>
>https://sourceware.org/pipermail/libc-alpha/2022-February/136727.html

HJ's DT_RELR patch series has been upgraded to v7
(https://patchwork.sourceware.org/project/glibc/list/?series=8295)

   git-pw series apply 8295
   # `Add --disable-default-dt-relr` does not apply cleanly

If no regressions with default DT_RELR, that will be cool!

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 0/4] PowerPC64 static-pie
  2022-04-08 22:27   ` [PATCH v2 0/4] PowerPC64 static-pie Tulio Magno Quites Machado Filho
@ 2022-04-11  1:38     ` Alan Modra
  0 siblings, 0 replies; 35+ messages in thread
From: Alan Modra @ 2022-04-11  1:38 UTC (permalink / raw)
  To: Tulio Magno Quites Machado Filho; +Cc: libc-alpha

On Fri, Apr 08, 2022 at 07:27:32PM -0300, Tulio Magno Quites Machado Filho wrote:
> Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:
> 
> > This is a repost of the series at
> > https://sourceware.org/pipermail/libc-alpha/2022-January/135598.html
> > incorporating review comments.
> >
> > Changes are:
> > - subject lines changed to comply with glibc commit log standards
> > - patch 1/4 log now mentions making a couple of symbols hidden
> > - patch 2/4 makes at_platform available to early ifunc resolvers
> >   as well as hwcap.
> > - patch 3/4 log mentions stinfo->init and stinfo->fini being unused
> > - patch 4/4 PI_STATIC_AND_HIDDEN comment explains what is going on
> >
> > patch 2/4 is the only code change from the previous series.  Testsuite
> > results now show no regressions on powerpc64le-linux.
> 
> I'm still seeing the following failures on powerpc64le-linux:
> 
> FAIL: elf/tst-tls1-static-non-pie
> FAIL: gmon/tst-gmon-static
> FAIL: gmon/tst-gmon-static-gprof
> 
> Am I missing anything?
> 
> I used different GCC and Binutils versions ranging between:
>  - GCC v8 and v11
>  - Binutils 2.30 and 2.38 (2020-03-04)

I was testing with mainline binutils and gcc (gcc-12 20220122).  It is
quite likeley that bleeding edge tools are required for static-pie on
ppc64le.  I'll see if I can recreate some of your results, in
particular with 2.38 binutils.  Mainline ppc64 binutils has a patch
that changes linker behaviour for absolute symbols.  I haven't
backported that patch, or the DT_RELR support, to the 2.38 branch
because those changes might result in breakage.  I'd rather let them
mature a while on mainline.

Here are the results I was seeing with both static-pie and relr
support enabled for ppc64 vs. basline glibc.  Same toolchain for both
glibc builds.

--- base_results   2022-03-04 04:36:09.71152949 -0600
+++ staticpierelr_results   2022-03-04 04:36:17.263676034 -0600
@@ -24,7 +24,7 @@
 UNSUPPORTED: time/tst-settimeofday
 Summary of test results:
       3 FAIL
-   4798 PASS
+   4802 PASS
      19 UNSUPPORTED
      16 XFAIL
       2 XPASS

The three fails common to both test runs were
FAIL: math/test-ibm128-llround
FAIL: math/test-ibm128-y1
FAIL: nptl/tst-pthread-gdb-attach-static
The extra PASSes are new DT_RELR tests I think.

> Anyway, this is a summary of what I think about these patches:
> 
> - Patch 1: Looks good to me.
> - Patch 2: Looks good to me.
> - Patch 3: Looks good to me after a minor change.
> - Patch 4: Hopefully I'm just missing a detail. Otherwise, I believe we should
>            delay it until the previous issues are fixed.

Thanks, I'm happy to leave out patch 4 for the time being, and perhaps
change it so that --enable-static-pie on the configure line is needed
for ppc64.

First three pushed along with the dl_vdso_vsym tweak, but I did see
this weird warning, whatever that means..

alan@squeak:~/src/glibc-current$ git push origin 1a85970f41ea1e5abe6da2298a5e8fedcea26b70:master
Enumerating objects: 62, done.
Counting objects: 100% (62/62), done.
Delta compression using up to 32 threads
Compressing objects: 100% (37/37), done.
Writing objects: 100% (39/39), 6.06 KiB | 6.06 MiB/s, done.
Total 39 (delta 33), reused 0 (delta 0), pack-reused 0
remote: Traceback (most recent call last):
remote:   File "/usr/local/bin/irkerhook.py", line 545, in <module>
remote:     ship(extractor, commit, not notify)
remote:   File "/usr/local/bin/irkerhook.py", line 467, in ship
remote:     privmsg = unicode(metadata)
remote:   File "/usr/local/bin/irkerhook.py", line 99, in __unicode__
remote:     if e.code == 401:
remote: AttributeError: 'URLError' object has no attribute 'code'
remote: *** !!! WARNING: /git/glibc.git/hooks-bin/post-receive returned code: 1.
To ssh://sourceware.org/git/glibc.git
   c0efbf8920..1a85970f41  1a85970f41ea1e5abe6da2298a5e8fedcea26b70 -> master

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 0/4] PowerPC64 static-pie
  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
  0 siblings, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-04-14  0:33 UTC (permalink / raw)
  To: Fangrui Song, H.J. Lu; +Cc: libc-alpha, Tulio Magno Quites Machado Filho

On Fri, Apr 08, 2022 at 05:14:12PM -0700, Fangrui Song wrote:
> HJ's DT_RELR patch series has been upgraded to v7
> (https://patchwork.sourceware.org/project/glibc/list/?series=8295)
> 
>   git-pw series apply 8295
>   # `Add --disable-default-dt-relr` does not apply cleanly
> 
> If no regressions with default DT_RELR, that will be cool!

I did find one error when testing a build of glibc using Ubuntu gcc-8.
elf/filter fails with "error while loading shared libraries:
.../elf/filtmod1.so: DT_RELR without GLIBC_ABI_DT_RELR dependency".

A little analysis shows the problem occurs when filtmod1.so is linked
with --as-needed and libc.so is not needed.  filtmod1.so ends up with
no .gnu.version or .gnu.version_r sections, and of course no
GLIBC_ABI_DT_RELR version.

The error check is not one that belongs in ld.so.  If you have the
error checking code, then you have DT_RELR support in ld.so and there
is no reason at all to refuse to run the program!  The check should be
in the linker, if anywhere.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 4/4] powerpc64: Enable static-pie
  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
  0 siblings, 2 replies; 35+ messages in thread
From: Alan Modra @ 2022-04-14  1:16 UTC (permalink / raw)
  To: Tulio Magno Quites Machado Filho; +Cc: libc-alpha

On Fri, Apr 08, 2022 at 07:49:05PM -0300, Tulio Magno Quites Machado Filho wrote:
> Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:
> 
> > 	* sysdeps/powerpc/powerpc64/configure.ac (SUPPORT_STATIC_PIE): Define.
> > 	(PI_STATIC_AND_HIDDEN): Define.
> > 	* sysdeps/powerpc/powerpc64/configure: Regenerate.
> 
> While the code in this patch is perfect, I'm slightly inclined to think its
> merge should be delayed until the failures on both powerpc64-linux and
> powerpc64le-linux are fixed.
> 
> Another option would be to restrict this code to powerpc64le-linux only,
> although I haven't had the success you mentioned in the cover letter.
> 
> Anyway, in one of my tests in particular (powerpc64le-linux, GCC 8, Binutils
> 2.30, without --enable-static-pie) I noticed many tests failing.  I'm still
> investigating what happened.

binutils-2.30 is too old.  You'll find __rela_iplt_start and
__rela_iplt_end symbols being defined in PIEs.  Those symbols are
supposed to mark out IFUNC relocs in ET_EXEC static binaries.  Their
presence in ET_DYN static binaries is bad, especially since it seems
they have garbage values not pointing at the correct set of
relocations.  I suspect, but haven't checked, that the garbage values
are due to -z combreloc.  This bug in ld was fixed with commit
795e3bb7de9, so you'll need at least binutils-2.33 for static-pie.

I'll note that static-pie is enabled in glibc without any user
--enable-static-pie, ie. it is the default if SUPPORT_STATIC_PIE is
set.  SUPPORT_STATIC_PIE is enabled unconditionally for x86 and
aarch64 without any toolchain checks, and I foolishly followed that
precedent.  Likely x86 would fail the glibc testsuite all over the
place with an old binutils too.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* DT_RELR without libc.so dependency
  2022-04-14  0:33         ` Alan Modra
@ 2022-04-14  1:54           ` H.J. Lu
  2022-04-14  3:43             ` Fangrui Song
  2022-04-14  5:18             ` Alan Modra
  0 siblings, 2 replies; 35+ messages in thread
From: H.J. Lu @ 2022-04-14  1:54 UTC (permalink / raw)
  To: Alan Modra; +Cc: Fangrui Song, GNU C Library, Tulio Magno Quites Machado Filho

On Wed, Apr 13, 2022 at 5:34 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Fri, Apr 08, 2022 at 05:14:12PM -0700, Fangrui Song wrote:
> > HJ's DT_RELR patch series has been upgraded to v7
> > (https://patchwork.sourceware.org/project/glibc/list/?series=8295)
> >
> >   git-pw series apply 8295
> >   # `Add --disable-default-dt-relr` does not apply cleanly
> >
> > If no regressions with default DT_RELR, that will be cool!
>
> I did find one error when testing a build of glibc using Ubuntu gcc-8.
> elf/filter fails with "error while loading shared libraries:
> .../elf/filtmod1.so: DT_RELR without GLIBC_ABI_DT_RELR dependency".
>
> A little analysis shows the problem occurs when filtmod1.so is linked
> with --as-needed and libc.so is not needed.  filtmod1.so ends up with
> no .gnu.version or .gnu.version_r sections, and of course no
> GLIBC_ABI_DT_RELR version.
>
> The error check is not one that belongs in ld.so.  If you have the
> error checking code, then you have DT_RELR support in ld.so and there
> is no reason at all to refuse to run the program!  The check should be
> in the linker, if anywhere.
>

The GLIBC_ABI_DT_RELR dependency is added to avoid the random
crush at run-time with older glibc binaries.   Since it is possible to create
a DSO with DT_RELR, but without the libc.so dependency.   Should ld.so
skip the GLIBC_ABI_DT_RELR check if the DSO doesn't depend on
libc.so?

-- 
H.J.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: [PATCH v2 4/4] powerpc64: Enable static-pie
  2022-04-14  1:16       ` Alan Modra
@ 2022-04-14  3:42         ` Fangrui Song
  2022-04-20  7:21         ` [PATCH v3] " Alan Modra
  1 sibling, 0 replies; 35+ messages in thread
From: Fangrui Song @ 2022-04-14  3:42 UTC (permalink / raw)
  To: Alan Modra; +Cc: Tulio Magno Quites Machado Filho, libc-alpha

On 2022-04-14, Alan Modra via Libc-alpha wrote:
>On Fri, Apr 08, 2022 at 07:49:05PM -0300, Tulio Magno Quites Machado Filho wrote:
>> Alan Modra via Libc-alpha <libc-alpha@sourceware.org> writes:
>>
>> > 	* sysdeps/powerpc/powerpc64/configure.ac (SUPPORT_STATIC_PIE): Define.
>> > 	(PI_STATIC_AND_HIDDEN): Define.
>> > 	* sysdeps/powerpc/powerpc64/configure: Regenerate.
>>
>> While the code in this patch is perfect, I'm slightly inclined to think its
>> merge should be delayed until the failures on both powerpc64-linux and
>> powerpc64le-linux are fixed.
>>
>> Another option would be to restrict this code to powerpc64le-linux only,
>> although I haven't had the success you mentioned in the cover letter.
>>
>> Anyway, in one of my tests in particular (powerpc64le-linux, GCC 8, Binutils
>> 2.30, without --enable-static-pie) I noticed many tests failing.  I'm still
>> investigating what happened.
>
>binutils-2.30 is too old.  You'll find __rela_iplt_start and
>__rela_iplt_end symbols being defined in PIEs.  Those symbols are
>supposed to mark out IFUNC relocs in ET_EXEC static binaries.  Their
>presence in ET_DYN static binaries is bad, especially since it seems
>they have garbage values not pointing at the correct set of
>relocations.  I suspect, but haven't checked, that the garbage values
>are due to -z combreloc.  This bug in ld was fixed with commit
>795e3bb7de9, so you'll need at least binutils-2.33 for static-pie.

I think conditional definition of __rela_iplt_start depending on
-no-pie/-pie is inelegant: it makes __rela_iplt_start the only symbol
with -no-pie/-pie internal linker script difference.
-no-pie/-pie internal linker scripts could otherwise be very similar,
with just the image base difference.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: DT_RELR without libc.so dependency
  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
  1 sibling, 0 replies; 35+ messages in thread
From: Fangrui Song @ 2022-04-14  3:43 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Alan Modra, GNU C Library, Tulio Magno Quites Machado Filho

On 2022-04-13, H.J. Lu wrote:
>On Wed, Apr 13, 2022 at 5:34 PM Alan Modra <amodra@gmail.com> wrote:
>>
>> On Fri, Apr 08, 2022 at 05:14:12PM -0700, Fangrui Song wrote:
>> > HJ's DT_RELR patch series has been upgraded to v7
>> > (https://patchwork.sourceware.org/project/glibc/list/?series=8295)
>> >
>> >   git-pw series apply 8295
>> >   # `Add --disable-default-dt-relr` does not apply cleanly
>> >
>> > If no regressions with default DT_RELR, that will be cool!
>>
>> I did find one error when testing a build of glibc using Ubuntu gcc-8.
>> elf/filter fails with "error while loading shared libraries:
>> .../elf/filtmod1.so: DT_RELR without GLIBC_ABI_DT_RELR dependency".
>>
>> A little analysis shows the problem occurs when filtmod1.so is linked
>> with --as-needed and libc.so is not needed.  filtmod1.so ends up with
>> no .gnu.version or .gnu.version_r sections, and of course no
>> GLIBC_ABI_DT_RELR version.
>>
>> The error check is not one that belongs in ld.so.  If you have the
>> error checking code, then you have DT_RELR support in ld.so and there
>> is no reason at all to refuse to run the program!  The check should be
>> in the linker, if anywhere.
>>
>
>The GLIBC_ABI_DT_RELR dependency is added to avoid the random
>crush at run-time with older glibc binaries.   Since it is possible to create
>a DSO with DT_RELR, but without the libc.so dependency.   Should ld.so
>skip the GLIBC_ABI_DT_RELR check if the DSO doesn't depend on
>libc.so?

Looks good.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: DT_RELR without libc.so dependency
  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
  1 sibling, 1 reply; 35+ messages in thread
From: Alan Modra @ 2022-04-14  5:18 UTC (permalink / raw)
  To: H.J. Lu; +Cc: Fangrui Song, GNU C Library, Tulio Magno Quites Machado Filho

On Wed, Apr 13, 2022 at 06:54:17PM -0700, H.J. Lu wrote:
> On Wed, Apr 13, 2022 at 5:34 PM Alan Modra <amodra@gmail.com> wrote:
> >
> > On Fri, Apr 08, 2022 at 05:14:12PM -0700, Fangrui Song wrote:
> > > HJ's DT_RELR patch series has been upgraded to v7
> > > (https://patchwork.sourceware.org/project/glibc/list/?series=8295)
> > >
> > >   git-pw series apply 8295
> > >   # `Add --disable-default-dt-relr` does not apply cleanly
> > >
> > > If no regressions with default DT_RELR, that will be cool!
> >
> > I did find one error when testing a build of glibc using Ubuntu gcc-8.
> > elf/filter fails with "error while loading shared libraries:
> > .../elf/filtmod1.so: DT_RELR without GLIBC_ABI_DT_RELR dependency".
> >
> > A little analysis shows the problem occurs when filtmod1.so is linked
> > with --as-needed and libc.so is not needed.  filtmod1.so ends up with
> > no .gnu.version or .gnu.version_r sections, and of course no
> > GLIBC_ABI_DT_RELR version.
> >
> > The error check is not one that belongs in ld.so.  If you have the
> > error checking code, then you have DT_RELR support in ld.so and there
> > is no reason at all to refuse to run the program!  The check should be
> > in the linker, if anywhere.
> >
> 
> The GLIBC_ABI_DT_RELR dependency is added to avoid the random
> crush at run-time with older glibc binaries.   Since it is possible to create
> a DSO with DT_RELR, but without the libc.so dependency.   Should ld.so
> skip the GLIBC_ABI_DT_RELR check if the DSO doesn't depend on
> libc.so?

I understand why you want a dependency, but I do not see a hard
requirement for l_abi_version or any code using it.  If you try to run
a new binary with DT_RELR using an old glibc or even current glibc
without relr support, you'll get "version `GLIBC_ABI_DT_RELR' not
found".  That is sufficient, presuming there is a GLIBC_ABI_DT_RELR
version in the binary.  If there *isn't* a GLIBC_ABI_DT_RELR version
then running that binary on an older glibc probably will crash.
Putting a check in a newer ld.so doesn't help much with that, except
show up this case where the dependency isn't there.  I guess the check
is justifiable under some DL_DEBUG flag.

Lack of a dependency is either a linker bug or a relr design bug.
Nitpick: isn't the actual dependency on ld.so rather than libc.so?

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

* Re: DT_RELR without libc.so dependency
  2022-04-14  5:18             ` Alan Modra
@ 2022-04-14 17:55               ` H.J. Lu
  0 siblings, 0 replies; 35+ messages in thread
From: H.J. Lu @ 2022-04-14 17:55 UTC (permalink / raw)
  To: Alan Modra; +Cc: Fangrui Song, GNU C Library, Tulio Magno Quites Machado Filho

On Wed, Apr 13, 2022 at 10:19 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Wed, Apr 13, 2022 at 06:54:17PM -0700, H.J. Lu wrote:
> > On Wed, Apr 13, 2022 at 5:34 PM Alan Modra <amodra@gmail.com> wrote:
> > >
> > > On Fri, Apr 08, 2022 at 05:14:12PM -0700, Fangrui Song wrote:
> > > > HJ's DT_RELR patch series has been upgraded to v7
> > > > (https://patchwork.sourceware.org/project/glibc/list/?series=8295)
> > > >
> > > >   git-pw series apply 8295
> > > >   # `Add --disable-default-dt-relr` does not apply cleanly
> > > >
> > > > If no regressions with default DT_RELR, that will be cool!
> > >
> > > I did find one error when testing a build of glibc using Ubuntu gcc-8.
> > > elf/filter fails with "error while loading shared libraries:
> > > .../elf/filtmod1.so: DT_RELR without GLIBC_ABI_DT_RELR dependency".
> > >
> > > A little analysis shows the problem occurs when filtmod1.so is linked
> > > with --as-needed and libc.so is not needed.  filtmod1.so ends up with
> > > no .gnu.version or .gnu.version_r sections, and of course no
> > > GLIBC_ABI_DT_RELR version.
> > >
> > > The error check is not one that belongs in ld.so.  If you have the
> > > error checking code, then you have DT_RELR support in ld.so and there
> > > is no reason at all to refuse to run the program!  The check should be
> > > in the linker, if anywhere.
> > >
> >
> > The GLIBC_ABI_DT_RELR dependency is added to avoid the random
> > crush at run-time with older glibc binaries.   Since it is possible to create
> > a DSO with DT_RELR, but without the libc.so dependency.   Should ld.so
> > skip the GLIBC_ABI_DT_RELR check if the DSO doesn't depend on
> > libc.so?
>
> I understand why you want a dependency, but I do not see a hard
> requirement for l_abi_version or any code using it.  If you try to run
> a new binary with DT_RELR using an old glibc or even current glibc
> without relr support, you'll get "version `GLIBC_ABI_DT_RELR' not
> found".  That is sufficient, presuming there is a GLIBC_ABI_DT_RELR
> version in the binary.  If there *isn't* a GLIBC_ABI_DT_RELR version
> then running that binary on an older glibc probably will crash.
> Putting a check in a newer ld.so doesn't help much with that, except
> show up this case where the dependency isn't there.  I guess the check
> is justifiable under some DL_DEBUG flag.
>
> Lack of a dependency is either a linker bug or a relr design bug.
> Nitpick: isn't the actual dependency on ld.so rather than libc.so?
>

The goal is to require a glibc with GLIBC_ABI_DT_RELR when
DT_RELR is used.  Since a shared library usually isn't linked
with ld.so, libc.so is checked.  I am working on v9 to support:

1. A DT_RELR shared library without DT_NEEDED.
2. A DT_RELR shared library without DT_VERNEED.
3. A DT_RELR shared library without libc.so on DT_NEEDED.

-- 
H.J.

^ permalink raw reply	[flat|nested] 35+ messages in thread

* [PATCH v3] powerpc64: Enable static-pie
  2022-04-14  1:16       ` Alan Modra
  2022-04-14  3:42         ` Fangrui Song
@ 2022-04-20  7:21         ` Alan Modra
  1 sibling, 0 replies; 35+ messages in thread
From: Alan Modra @ 2022-04-20  7:21 UTC (permalink / raw)
  To: Tulio Magno Quites Machado Filho; +Cc: libc-alpha

On Thu, Apr 14, 2022 at 10:46:14AM +0930, Alan Modra wrote:
> I'll note that static-pie is enabled in glibc without any user
> --enable-static-pie, ie. it is the default if SUPPORT_STATIC_PIE is
> set.  SUPPORT_STATIC_PIE is enabled unconditionally for x86 and
> aarch64 without any toolchain checks, and I foolishly followed that
> precedent.

Conditionally define SUPPORT_STATIC_PIE if binutils-2.33 or later.
The test ifunc results in .iplt relocations, and with a weak reference
to __rela_iplt_start will cause older linkers to generate that symbol.

PI_STATIC_AND_HIDDEN should be OK with the base binutils-2.25.

	* sysdeps/powerpc/powerpc64/configure.ac (SUPPORT_STATIC_PIE): Define.
	(PI_STATIC_AND_HIDDEN): Define.
	* sysdeps/powerpc/powerpc64/configure: Regenerate.

Regression tested powerpc64le-linux.

diff --git a/sysdeps/powerpc/powerpc64/configure b/sysdeps/powerpc/powerpc64/configure
index fddea0355a..0db93080d3 100644
--- a/sysdeps/powerpc/powerpc64/configure
+++ b/sysdeps/powerpc/powerpc64/configure
@@ -1,6 +1,44 @@
 # This file is generated from configure.ac by Autoconf.  DO NOT EDIT!
  # Local configure fragment for sysdeps/powerpc/powerpc64.
 
+$as_echo "#define PI_STATIC_AND_HIDDEN 1" >>confdefs.h
+
+
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking if the toolchain supports static-pie" >&5
+$as_echo_n "checking if the toolchain supports static-pie... " >&6; }
+if ${libc_cv_ppc64_static_pie+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+    cat > conftest.c <<EOF
+static long __foo_0 (long x) { return x; }
+static long __foo_1 (long x) { return x; }
+volatile int select;
+typedef long longf (long);
+static longf *foo_ifunc (void) { return select ? __foo_1 : __foo_0; }
+static long __attribute__ ((ifunc ("foo_ifunc"))) foo (long);
+extern const char *__rela_iplt_start __attribute__ ((weak));
+int _start (void) { return foo ((long) __rela_iplt_start); };
+EOF
+  libc_cv_ppc64_static_pie=no
+  if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS -static-pie -nostdlib -nostartfiles -o conftest 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
+        if ! nm conftest | grep -q __rela_iplt; then
+      libc_cv_ppc64_static_pie=yes
+    fi
+  fi
+  rm -rf conftest.*
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_ppc64_static_pie" >&5
+$as_echo "$libc_cv_ppc64_static_pie" >&6; }
+if test x$libc_cv_ppc64_static_pie = xyes; then
+    $as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
+
+fi
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for support for overlapping .opd entries" >&5
 $as_echo_n "checking for support for overlapping .opd entries... " >&6; }
 if ${libc_cv_overlapping_opd+:} false; then :
diff --git a/sysdeps/powerpc/powerpc64/configure.ac b/sysdeps/powerpc/powerpc64/configure.ac
index 1f3d54414c..7de4de871c 100644
--- a/sysdeps/powerpc/powerpc64/configure.ac
+++ b/sysdeps/powerpc/powerpc64/configure.ac
@@ -1,6 +1,37 @@
 GLIBC_PROVIDES dnl See aclocal.m4 in the top level source directory.
 # Local configure fragment for sysdeps/powerpc/powerpc64.
 
+dnl It is possible to access static and hidden symbols in a position
+dnl independent way requiring no relocation on powerpc64.  The linker
+dnl edits medium and large model code that uses GOT/TOC entries (which
+dnl would require relocation) to r2 relative accesses.
+AC_DEFINE(PI_STATIC_AND_HIDDEN)
+
+AC_CACHE_CHECK(if the toolchain supports static-pie,
+libc_cv_ppc64_static_pie, [dnl
+  cat > conftest.c <<EOF
+static long __foo_0 (long x) { return x; }
+static long __foo_1 (long x) { return x; }
+volatile int select;
+typedef long longf (long);
+static longf *foo_ifunc (void) { return select ? __foo_1 : __foo_0; }
+static long __attribute__ ((ifunc ("foo_ifunc"))) foo (long);
+extern const char *__rela_iplt_start[] __attribute__ ((weak));
+int _start (void) { return foo ((long) __rela_iplt_start); };
+EOF
+  libc_cv_ppc64_static_pie=no
+  if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -static-pie -nostdlib -nostartfiles -o conftest conftest.c]); then
+    dnl binutils-2.33 or newer is needed
+    if ! nm conftest | grep -q __rela_iplt; then
+      libc_cv_ppc64_static_pie=yes
+    fi
+  fi
+  rm -rf conftest.*])
+if test x$libc_cv_ppc64_static_pie = xyes; then
+  dnl Static PIE is supported.
+  AC_DEFINE(SUPPORT_STATIC_PIE)
+fi
+
 AC_CACHE_CHECK(for support for overlapping .opd entries,
 libc_cv_overlapping_opd, [dnl
 libc_cv_overlapping_opd=no

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2022-04-20  7:21 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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   ` [PATCH v2 2/4] powerpc64: Set up thread register for _dl_relocate_static_pie Alan Modra
2022-04-08 22:28     ` 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

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).