08c3a6
commit 3be79b72d556e3ac37075ad6b99eb5eac18e1402
08c3a6
Author: John David Anglin <danglin@gcc.gnu.org>
08c3a6
Date:   Sun Mar 6 15:56:57 2022 +0000
08c3a6
08c3a6
    Fix elf/tst-audit2 on hppa
08c3a6
    
08c3a6
    The test elf/tst-audit2 fails on hppa with a segmentation fault in the
08c3a6
    long branch stub used to call malloc from calloc.  This occurs because
08c3a6
    the test is not a PIC executable and calloc is called from the dynamic
08c3a6
    linker before the dp register is initialized in _dl_start_user.
08c3a6
    
08c3a6
    The fix is to move the dp register initialization into
08c3a6
    elf_machine_runtime_setup.  Since the address of $global$ can't be
08c3a6
    loaded directly, we continue to use the DT_PLTGOT value from the
08c3a6
    the main_map to initialize dp.  Since l_main_map is not available
08c3a6
    in v2.34 and earlier, we use a new function, elf_machine_main_map,
08c3a6
    to find the main map.
08c3a6
08c3a6
diff --git a/sysdeps/hppa/dl-machine.h b/sysdeps/hppa/dl-machine.h
08c3a6
index f048fd20728ccde6..24f0f47d8f1e25cd 100644
08c3a6
--- a/sysdeps/hppa/dl-machine.h
08c3a6
+++ b/sysdeps/hppa/dl-machine.h
08c3a6
@@ -27,6 +27,7 @@
08c3a6
 #include <string.h>
08c3a6
 #include <link.h>
08c3a6
 #include <errno.h>
08c3a6
+#include <ldsodefs.h>
08c3a6
 #include <dl-fptr.h>
08c3a6
 #include <abort-instr.h>
08c3a6
 #include <tls.h>
08c3a6
@@ -159,6 +160,24 @@ elf_machine_plt_value (struct link_map *map, const Elf32_Rela *reloc,
08c3a6
   return (struct fdesc) { value.ip + reloc->r_addend, value.gp };
08c3a6
 }
08c3a6
 
08c3a6
+static inline struct link_map *
08c3a6
+elf_machine_main_map (void)
08c3a6
+{
08c3a6
+  struct link_map *main_map;
08c3a6
+
08c3a6
+#if defined SHARED && IS_IN (rtld)
08c3a6
+  asm (
08c3a6
+"	bl	1f,%0\n"
08c3a6
+"	addil	L'_rtld_local - ($PIC_pcrel$0 - 1),%0\n"
08c3a6
+"1:	ldw	R'_rtld_local - ($PIC_pcrel$0 - 5)(%%r1),%0\n"
08c3a6
+   : "=r" (main_map) : : "r1");
08c3a6
+#else
08c3a6
+  main_map = NULL;
08c3a6
+#endif
08c3a6
+
08c3a6
+  return main_map;
08c3a6
+}
08c3a6
+
08c3a6
 /* Set up the loaded object described by L so its unrelocated PLT
08c3a6
    entries will jump to the on-demand fixup code in dl-runtime.c.  */
08c3a6
 
08c3a6
@@ -174,6 +193,15 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
08c3a6
     Elf32_Addr i[2];
08c3a6
   } sig = {{0x00,0xc0,0xff,0xee, 0xde,0xad,0xbe,0xef}};
08c3a6
 
08c3a6
+  /* Initialize dp register for main executable.  */
08c3a6
+  if (l == elf_machine_main_map ())
08c3a6
+    {
08c3a6
+      register Elf32_Addr dp asm ("%r27");
08c3a6
+
08c3a6
+      dp = D_PTR (l, l_info[DT_PLTGOT]);
08c3a6
+      asm volatile ("" : : "r" (dp));
08c3a6
+    }
08c3a6
+
08c3a6
   /* If we don't have a PLT we can just skip all this... */
08c3a6
   if (__builtin_expect (l->l_info[DT_JMPREL] == NULL,0))
08c3a6
     return lazy;
08c3a6
@@ -336,16 +364,6 @@ elf_machine_runtime_setup (struct link_map *l, int lazy, int profile)
08c3a6
    its return value is the user program's entry point.  */
08c3a6
 
08c3a6
 #define RTLD_START \
08c3a6
-/* Set up dp for any non-PIC lib constructors that may be called.  */	\
08c3a6
-static struct link_map * __attribute__((used))				\
08c3a6
-set_dp (struct link_map *map)						\
08c3a6
-{									\
08c3a6
-  register Elf32_Addr dp asm ("%r27");					\
08c3a6
-  dp = D_PTR (map, l_info[DT_PLTGOT]);					\
08c3a6
-  asm volatile ("" : : "r" (dp));					\
08c3a6
-  return map;								\
08c3a6
-}									\
08c3a6
-									\
08c3a6
 asm (									\
08c3a6
 "	.text\n"							\
08c3a6
 "	.globl _start\n"						\
08c3a6
@@ -445,14 +463,11 @@ asm (									\
08c3a6
 "	stw	%r24,-44(%sp)\n"					\
08c3a6
 									\
08c3a6
 ".Lnofix:\n"								\
08c3a6
+	/* Call _dl_init(main_map, argc, argv, envp). */		\
08c3a6
 "	addil	LT'_rtld_local,%r19\n"					\
08c3a6
 "	ldw	RT'_rtld_local(%r1),%r26\n"				\
08c3a6
-"	bl	set_dp, %r2\n"						\
08c3a6
 "	ldw	0(%r26),%r26\n"						\
08c3a6
 									\
08c3a6
-	/* Call _dl_init(_dl_loaded, argc, argv, envp). */		\
08c3a6
-"	copy	%r28,%r26\n"						\
08c3a6
-									\
08c3a6
 	/* envp = argv + argc + 1 */					\
08c3a6
 "	sh2add	%r25,%r24,%r23\n"					\
08c3a6
 "	bl	_dl_init,%r2\n"						\