abe59f
commit 9e94f57484a2aba0fe67ea2059b5843f651887c2
abe59f
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
abe59f
Date:   Fri Feb 4 15:54:59 2022 -0300
abe59f
abe59f
    hppa: Fix bind-now audit (BZ #28857)
abe59f
    
abe59f
    On hppa, a function pointer returned by la_symbind is actually a function
abe59f
    descriptor has the plabel bit set (bit 30).  This must be cleared to get
abe59f
    the actual address of the descriptor.  If the descriptor has been bound,
abe59f
    the first word of the descriptor is the physical address of theA function,
abe59f
    otherwise, the first word of the descriptor points to a trampoline in the
abe59f
    PLT.
abe59f
    
abe59f
    This patch also adds a workaround on tests because on hppa (and it seems
abe59f
    to be the only ABI I have see it), some shared library adds a dynamic PLT
abe59f
    relocation to am empty symbol name:
abe59f
    
abe59f
    $ readelf -r elf/tst-audit25mod1.so
abe59f
    [...]
abe59f
    Relocation section '.rela.plt' at offset 0x464 contains 6 entries:
abe59f
     Offset     Info    Type            Sym.Value  Sym. Name + Addend
abe59f
    00002008  00000081 R_PARISC_IPLT                508
abe59f
    [...]
abe59f
    
abe59f
    It breaks some assumptions on the test, where a symbol with an empty
abe59f
    name ("") is passed on la_symbind.
abe59f
    
abe59f
    Checked on x86_64-linux-gnu and hppa-linux-gnu.
abe59f
abe59f
diff --git a/elf/Makefile b/elf/Makefile
abe59f
index 73d347339762fc9e..6d39b400060a73f3 100644
abe59f
--- a/elf/Makefile
abe59f
+++ b/elf/Makefile
abe59f
@@ -2113,7 +2113,7 @@ $(objpfx)tst-audit24c.out: $(objpfx)tst-auditmod24c.so
abe59f
 $(objpfx)tst-audit24c: $(objpfx)tst-audit24amod1.so \
abe59f
 		       $(objpfx)tst-audit24amod2.so
abe59f
 tst-audit24c-ENV = LD_BIND_NOW=1 LD_AUDIT=$(objpfx)tst-auditmod24c.so
abe59f
-LDFLAGS-tst-audit24b = -Wl,-z,lazy
abe59f
+LDFLAGS-tst-audit24c = -Wl,-z,lazy
abe59f
 
abe59f
 $(objpfx)tst-audit24d.out: $(objpfx)tst-auditmod24d.so
abe59f
 $(objpfx)tst-audit24d: $(objpfx)tst-audit24dmod1.so \
abe59f
diff --git a/elf/dl-audit.c b/elf/dl-audit.c
abe59f
index 72a50717ef60a357..ec9b032eae37c103 100644
abe59f
--- a/elf/dl-audit.c
abe59f
+++ b/elf/dl-audit.c
abe59f
@@ -257,7 +257,8 @@ _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result,
abe59f
       reloc_result->flags = flags;
abe59f
     }
abe59f
 
abe59f
-  DL_FIXUP_BINDNOW_RELOC (value, new_value, sym.st_value);
abe59f
+  if (flags & LA_SYMB_ALTVALUE)
abe59f
+    DL_FIXUP_BINDNOW_RELOC (value, new_value, sym.st_value);
abe59f
 }
abe59f
 
abe59f
 void
abe59f
diff --git a/elf/tst-auditmod24a.c b/elf/tst-auditmod24a.c
abe59f
index d8e88f3984af1707..3075dfae2fd3d288 100644
abe59f
--- a/elf/tst-auditmod24a.c
abe59f
+++ b/elf/tst-auditmod24a.c
abe59f
@@ -110,5 +110,7 @@ la_symbind32 (Elf32_Sym *sym, unsigned int ndx,
abe59f
       return sym->st_value;
abe59f
     }
abe59f
 
abe59f
-  abort ();
abe59f
+  if (symname[0] != '\0')
abe59f
+    abort ();
abe59f
+  return sym->st_value;
abe59f
 }
abe59f
diff --git a/elf/tst-auditmod24d.c b/elf/tst-auditmod24d.c
abe59f
index 8c803ecc0a48f21b..badc6be451ee0357 100644
abe59f
--- a/elf/tst-auditmod24d.c
abe59f
+++ b/elf/tst-auditmod24d.c
abe59f
@@ -116,5 +116,7 @@ la_symbind32 (Elf32_Sym *sym, unsigned int ndx,
abe59f
 	}
abe59f
     }
abe59f
 
abe59f
-  abort ();
abe59f
+  if (symname[0] != '\0')
abe59f
+    abort ();
abe59f
+  return sym->st_value;
abe59f
 }
abe59f
diff --git a/elf/tst-auditmod25.c b/elf/tst-auditmod25.c
abe59f
index 526f5c54bc2c3b8c..20640a8daf346b5f 100644
abe59f
--- a/elf/tst-auditmod25.c
abe59f
+++ b/elf/tst-auditmod25.c
abe59f
@@ -72,7 +72,7 @@ la_symbind32 (Elf32_Sym *sym, unsigned int ndx,
abe59f
 	      unsigned int *flags, const char *symname)
abe59f
 #endif
abe59f
 {
abe59f
-  if (*refcook != -1 && *defcook != -1)
abe59f
+  if (*refcook != -1 && *defcook != -1 && symname[0] != '\0')
abe59f
     fprintf (stderr, "la_symbind: %s %u\n", symname,
abe59f
 	     *flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) ? 1 : 0);
abe59f
   return sym->st_value;
abe59f
diff --git a/sysdeps/hppa/dl-lookupcfg.h b/sysdeps/hppa/dl-lookupcfg.h
abe59f
index c3fea1fe5776b17a..86f6a04af46c87ba 100644
abe59f
--- a/sysdeps/hppa/dl-lookupcfg.h
abe59f
+++ b/sysdeps/hppa/dl-lookupcfg.h
abe59f
@@ -79,7 +79,9 @@ void attribute_hidden _dl_unmap (struct link_map *map);
abe59f
 /* Extract the code address from a fixup value */
abe59f
 #define DL_FIXUP_VALUE_CODE_ADDR(value) ((value).ip)
abe59f
 #define DL_FIXUP_VALUE_ADDR(value) ((uintptr_t) &(value))
abe59f
-#define DL_FIXUP_ADDR_VALUE(addr) (*(struct fdesc *) (addr))
abe59f
+/* Clear the plabel bit to get the actual address of the descriptor.  */
abe59f
+#define DL_FIXUP_ADDR_VALUE(addr) \
abe59f
+  (*(DL_FIXUP_VALUE_TYPE *) ((uintptr_t) (addr) & ~2))
abe59f
 #define DL_FIXUP_BINDNOW_ADDR_VALUE(addr) (addr)
abe59f
-#define DL_FIXUP_BINDNOW_RELOC(value, new_value, st_value) \
abe59f
-  (*value) = *(struct fdesc *) (st_value)
abe59f
+#define DL_FIXUP_BINDNOW_RELOC(value, new_value, st_value)	\
abe59f
+  *(value) = *(DL_FIXUP_VALUE_TYPE *) ((uintptr_t) (new_value) & ~2)