446cf2
commit 23c1c256ae7b0f010d0fcaff60682b620887b164
446cf2
Author: Mihailo Stojanovic <mihailo.stojanovic@rt-rk.com>
446cf2
Date:   Thu Aug 29 20:11:42 2019 +0000
446cf2
446cf2
    MIPS support for GNU hash
446cf2
    
446cf2
    This patch is a reimplementation of [1], which was submitted back in
446cf2
    2015. Copyright issue has been sorted [2] last year. It proposed a new
446cf2
    section (.gnu.xhash) and related dynamic tag (GT_GNU_XHASH). The new
446cf2
    section would be virtually identical to the existing .gnu.hash except
446cf2
    for the translation table (xlat) which would contain correct MIPS
446cf2
    .dynsym indexes corresponding to the hashvals in chains. This is because
446cf2
    MIPS ABI imposes a different ordering of the dynsyms than the one
446cf2
    expected by the .gnu.hash section. Another addition would be a leading
446cf2
    word at the beggining of the section, which would contain the number of
446cf2
    entries in the translation table.
446cf2
    
446cf2
    In this patch, the new section name and dynamic tag are changed to
446cf2
    reflect the fact that the section should be treated as MIPS specific
446cf2
    (.MIPS.xhash and DT_MIPS_XHASH).
446cf2
    
446cf2
    This patch addresses the alignment issue reported in [3] which is caused
446cf2
    by the leading word of the .MIPS.xhash section. Leading word is now
446cf2
    removed in the corresponding binutils patch, and the number of entries
446cf2
    in the translation table is computed using DT_MIPS_SYMTABNO dynamic tag.
446cf2
    
446cf2
    Since the MIPS specific dl-lookup.c file was removed following the
446cf2
    initial patch submission, I opted for the definition of three new macros
446cf2
    in the generic ldsodefs.h. ELF_MACHINE_GNU_HASH_ADDRIDX defines the
446cf2
    index of the dynamic tag in the l_info array. ELF_MACHINE_HASH_SYMIDX is
446cf2
    used to calculate the index of a symbol in GNU hash. On MIPS, it is
446cf2
    defined to look up the symbol index in the translation table.
446cf2
    ELF_MACHINE_XHASH_SETUP is defined for MIPS only. It initializes the
446cf2
    .MIPS.xhash pointer in the link_map_machine struct.
446cf2
    
446cf2
    The other major change is bumping the highest EI_ABIVERSION value for
446cf2
    MIPS to suggest that the dynamic linker now supports GNU hash.
446cf2
    
446cf2
    The patch was tested by running the glibc testsuite for the three MIPS
446cf2
    ABIs (o32, n32 and n64) and for x86_64-linux-gnu.
446cf2
    
446cf2
    [1] https://sourceware.org/ml/binutils/2015-10/msg00057.html
446cf2
    [2] https://sourceware.org/ml/binutils/2018-03/msg00025.html
446cf2
    [3] https://sourceware.org/ml/binutils/2016-01/msg00006.html
446cf2
    
446cf2
            * elf/dl-addr.c (determine_info): Calculate the symbol index
446cf2
            using the newly defined ELF_MACHINE_HASH_SYMIDX macro.
446cf2
            * elf/dl-lookup.c (do_lookup_x): Ditto.
446cf2
            (_dl_setup_hash): Initialize MIPS xhash translation table.
446cf2
            * elf/elf.h (SHT_MIPS_XHASH): New define.
446cf2
            (DT_MIPS_XHASH): New define.
446cf2
            * sysdeps/generic/ldsodefs.h (ELF_MACHINE_GNU_HASH_ADDRIDX): New
446cf2
            define.
446cf2
            (ELF_MACHINE_HASH_SYMIDX): Ditto.
446cf2
            (ELF_MACHINE_XHASH_SETUP): Ditto.
446cf2
            * sysdeps/mips/ldsodefs.h (ELF_MACHINE_GNU_HASH_ADDRIDX): New
446cf2
            define.
446cf2
            (ELF_MACHINE_HASH_SYMIDX): Ditto.
446cf2
            (ELF_MACHINE_XHASH_SETUP): Ditto.
446cf2
            * sysdeps/mips/linkmap.h (struct link_map_machine): New member.
446cf2
            * sysdeps/unix/sysv/linux/mips/ldsodefs.h: Increment valid ABI
446cf2
            version.
446cf2
            * sysdeps/unix/sysv/linux/mips/libc-abis: New ABI version.
446cf2
446cf2
diff --git a/elf/dl-addr.c b/elf/dl-addr.c
446cf2
index e6c7d020945c51d2..b146fed09a46ff76 100644
446cf2
--- a/elf/dl-addr.c
446cf2
+++ b/elf/dl-addr.c
446cf2
@@ -42,7 +42,7 @@ determine_info (const ElfW(Addr) addr, struct link_map *match, Dl_info *info,
446cf2
   ElfW(Word) strtabsize = match->l_info[DT_STRSZ]->d_un.d_val;
446cf2
 
446cf2
   const ElfW(Sym) *matchsym = NULL;
446cf2
-  if (match->l_info[ADDRIDX (DT_GNU_HASH)] != NULL)
446cf2
+  if (match->l_info[ELF_MACHINE_GNU_HASH_ADDRIDX] != NULL)
446cf2
     {
446cf2
       /* We look at all symbol table entries referenced by the hash
446cf2
 	 table.  */
446cf2
@@ -57,6 +57,7 @@ determine_info (const ElfW(Addr) addr, struct link_map *match, Dl_info *info,
446cf2
 		{
446cf2
 		  /* The hash table never references local symbols so
446cf2
 		     we can omit that test here.  */
446cf2
+		  symndx = ELF_MACHINE_HASH_SYMIDX (match, hasharr);
446cf2
 		  if ((symtab[symndx].st_shndx != SHN_UNDEF
446cf2
 		       || symtab[symndx].st_value != 0)
446cf2
 		      && symtab[symndx].st_shndx != SHN_ABS
446cf2
@@ -65,8 +66,6 @@ determine_info (const ElfW(Addr) addr, struct link_map *match, Dl_info *info,
446cf2
 					    matchsym, addr)
446cf2
 		      && symtab[symndx].st_name < strtabsize)
446cf2
 		    matchsym = (ElfW(Sym) *) &symtab[symndx];
446cf2
-
446cf2
-		  ++symndx;
446cf2
 		}
446cf2
 	      while ((*hasharr++ & 1u) == 0);
446cf2
 	    }
446cf2
diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
446cf2
index 01724a54f8840f9f..42fdaed99296137f 100644
446cf2
--- a/elf/dl-lookup.c
446cf2
+++ b/elf/dl-lookup.c
446cf2
@@ -432,7 +432,7 @@ do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
446cf2
 		  do
446cf2
 		    if (((*hasharr ^ new_hash) >> 1) == 0)
446cf2
 		      {
446cf2
-			symidx = hasharr - map->l_gnu_chain_zero;
446cf2
+			symidx = ELF_MACHINE_HASH_SYMIDX (map, hasharr);
446cf2
 			sym = check_match (undef_name, ref, version, flags,
446cf2
 					   type_class, &symtab[symidx], symidx,
446cf2
 					   strtab, map, &versioned_sym,
446cf2
@@ -961,10 +961,10 @@ _dl_setup_hash (struct link_map *map)
446cf2
 {
446cf2
   Elf_Symndx *hash;
446cf2
 
446cf2
-  if (__glibc_likely (map->l_info[ADDRIDX (DT_GNU_HASH)] != NULL))
446cf2
+  if (__glibc_likely (map->l_info[ELF_MACHINE_GNU_HASH_ADDRIDX] != NULL))
446cf2
     {
446cf2
       Elf32_Word *hash32
446cf2
-	= (void *) D_PTR (map, l_info[ADDRIDX (DT_GNU_HASH)]);
446cf2
+	= (void *) D_PTR (map, l_info[ELF_MACHINE_GNU_HASH_ADDRIDX]);
446cf2
       map->l_nbuckets = *hash32++;
446cf2
       Elf32_Word symbias = *hash32++;
446cf2
       Elf32_Word bitmask_nwords = *hash32++;
446cf2
@@ -979,6 +979,10 @@ _dl_setup_hash (struct link_map *map)
446cf2
       map->l_gnu_buckets = hash32;
446cf2
       hash32 += map->l_nbuckets;
446cf2
       map->l_gnu_chain_zero = hash32 - symbias;
446cf2
+
446cf2
+      /* Initialize MIPS xhash translation table.  */
446cf2
+      ELF_MACHINE_XHASH_SETUP (hash32, symbias, map);
446cf2
+
446cf2
       return;
446cf2
     }
446cf2
 
446cf2
diff --git a/elf/elf.h b/elf/elf.h
446cf2
index 74f7f479ce817040..d6506ea1c7160dea 100644
446cf2
--- a/elf/elf.h
446cf2
+++ b/elf/elf.h
446cf2
@@ -1698,6 +1698,7 @@ typedef struct
446cf2
 #define SHT_MIPS_EH_REGION	0x70000027
446cf2
 #define SHT_MIPS_XLATE_OLD	0x70000028
446cf2
 #define SHT_MIPS_PDR_EXCEPTION	0x70000029
446cf2
+#define SHT_MIPS_XHASH		0x7000002b
446cf2
 
446cf2
 /* Legal values for sh_flags field of Elf32_Shdr.  */
446cf2
 
446cf2
@@ -1945,7 +1946,9 @@ typedef struct
446cf2
    in a PIE as it stores a relative offset from the address of the tag
446cf2
    rather than an absolute address.  */
446cf2
 #define DT_MIPS_RLD_MAP_REL  0x70000035
446cf2
-#define DT_MIPS_NUM	     0x36
446cf2
+/* GNU-style hash table with xlat.  */
446cf2
+#define DT_MIPS_XHASH	     0x70000036
446cf2
+#define DT_MIPS_NUM	     0x37
446cf2
 
446cf2
 /* Legal values for DT_MIPS_FLAGS Elf32_Dyn entry.  */
446cf2
 
446cf2
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
446cf2
index f0185ce0d16c0f69..3bdbdd6e67dacc85 100644
446cf2
--- a/sysdeps/generic/ldsodefs.h
446cf2
+++ b/sysdeps/generic/ldsodefs.h
446cf2
@@ -47,6 +47,23 @@ __BEGIN_DECLS
446cf2
 #define ADDRIDX(tag)	(DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
446cf2
 			 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
446cf2
 
446cf2
+/* Type of GNU hash which the machine uses.  */
446cf2
+#ifndef ELF_MACHINE_GNU_HASH_ADDRIDX
446cf2
+# define ELF_MACHINE_GNU_HASH_ADDRIDX ADDRIDX (DT_GNU_HASH)
446cf2
+#endif
446cf2
+
446cf2
+/* Calculate the index of a symbol in GNU hash.  */
446cf2
+#ifndef ELF_MACHINE_HASH_SYMIDX
446cf2
+# define ELF_MACHINE_HASH_SYMIDX(map, hasharr) \
446cf2
+  ((hasharr) - (map)->l_gnu_chain_zero)
446cf2
+#endif
446cf2
+
446cf2
+/* Setup MIPS xhash.  Defined only for MIPS.  */
446cf2
+#ifndef ELF_MACHINE_XHASH_SETUP
446cf2
+# define ELF_MACHINE_XHASH_SETUP(hash32, symbias, map) \
446cf2
+  ((void) (hash32), (void) (symbias), (void) (map))
446cf2
+#endif
446cf2
+
446cf2
 /* We use this macro to refer to ELF types independent of the native wordsize.
446cf2
    `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'.  */
446cf2
 #define ELFW(type)	_ElfW (ELF, __ELF_NATIVE_CLASS, type)
446cf2
diff --git a/sysdeps/mips/ldsodefs.h b/sysdeps/mips/ldsodefs.h
446cf2
index c6e5ce7e660325c1..35043b7c6d416c50 100644
446cf2
--- a/sysdeps/mips/ldsodefs.h
446cf2
+++ b/sysdeps/mips/ldsodefs.h
446cf2
@@ -26,6 +26,21 @@ struct La_mips_32_retval;
446cf2
 struct La_mips_64_regs;
446cf2
 struct La_mips_64_retval;
446cf2
 
446cf2
+#define ELF_MACHINE_GNU_HASH_ADDRIDX (DT_MIPS_XHASH - DT_LOPROC + DT_NUM)
446cf2
+
446cf2
+/* Calculate the index of a symbol in MIPS xhash.  */
446cf2
+#define ELF_MACHINE_HASH_SYMIDX(map, hasharr) \
446cf2
+  ((map)->l_mach.mips_xlat_zero[(hasharr) - (map)->l_gnu_chain_zero])
446cf2
+
446cf2
+/* Setup MIPS xhash.  */
446cf2
+#define ELF_MACHINE_XHASH_SETUP(hash32, symbias, map)			    \
446cf2
+  do									    \
446cf2
+    {									    \
446cf2
+      (hash32) += (map)->l_info[DT_MIPS (SYMTABNO)]->d_un.d_val - (symbias); \
446cf2
+      (map)->l_mach.mips_xlat_zero = (hash32) - (symbias);		    \
446cf2
+    }									    \
446cf2
+  while (0)
446cf2
+
446cf2
 #define ARCH_PLTENTER_MEMBERS						    \
446cf2
     Elf32_Addr (*mips_o32_gnu_pltenter) (Elf32_Sym *, unsigned int,	    \
446cf2
 					 uintptr_t *, uintptr_t *,	    \
446cf2
diff --git a/sysdeps/mips/linkmap.h b/sysdeps/mips/linkmap.h
446cf2
index 1fb9678a6d1625fd..1e640c3ba9bd18e4 100644
446cf2
--- a/sysdeps/mips/linkmap.h
446cf2
+++ b/sysdeps/mips/linkmap.h
446cf2
@@ -3,4 +3,5 @@ struct link_map_machine
446cf2
     ElfW(Addr) plt; /* Address of .plt */
446cf2
     ElfW(Word) fpabi; /* FP ABI of the object */
446cf2
     unsigned int odd_spreg; /* Does the object require odd_spreg support? */
446cf2
+    const Elf32_Word *mips_xlat_zero; /* .MIPS.xhash */
446cf2
   };
446cf2
diff --git a/sysdeps/unix/sysv/linux/mips/ldsodefs.h b/sysdeps/unix/sysv/linux/mips/ldsodefs.h
446cf2
index d2912cadabfd6877..03f3e12f202a0563 100644
446cf2
--- a/sysdeps/unix/sysv/linux/mips/ldsodefs.h
446cf2
+++ b/sysdeps/unix/sysv/linux/mips/ldsodefs.h
446cf2
@@ -34,7 +34,7 @@ extern void _dl_static_init (struct link_map *map);
446cf2
 #undef VALID_ELF_ABIVERSION
446cf2
 #define VALID_ELF_ABIVERSION(osabi,ver)			\
446cf2
   (ver == 0						\
446cf2
-   || (osabi == ELFOSABI_SYSV && ver < 5)		\
446cf2
+   || (osabi == ELFOSABI_SYSV && ver < 6)		\
446cf2
    || (osabi == ELFOSABI_GNU && ver < LIBC_ABI_MAX))
446cf2
 
446cf2
 #endif /* ldsodefs.h */
446cf2
diff --git a/sysdeps/unix/sysv/linux/mips/libc-abis b/sysdeps/unix/sysv/linux/mips/libc-abis
446cf2
index eaea558720f42a48..c0b67dae3ece1511 100644
446cf2
--- a/sysdeps/unix/sysv/linux/mips/libc-abis
446cf2
+++ b/sysdeps/unix/sysv/linux/mips/libc-abis
446cf2
@@ -16,3 +16,5 @@ UNIQUE
446cf2
 MIPS_O32_FP64   mips*-*-linux*
446cf2
 # Absolute (SHN_ABS) symbols working correctly.
446cf2
 ABSOLUTE
446cf2
+# GNU-style hash table with translation table.
446cf2
+MIPS_XHASH