elf: Remove one-default-version check when searching an unversioned symbol

Message ID 20220501020643.1538749-1-maskray@google.com
State New
Headers
Series elf: Remove one-default-version check when searching an unversioned symbol |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

Fangrui Song May 1, 2022, 2:06 a.m. UTC
  When searching an unversioned symbol, a definition of version
VER_NDX_GLOBAL is preferred while a default version symbol is a
fallback. The linker ensures that two default versions cannot be
attached to the same symbol (``multiple definition of `foo'``), so it is
of very little value for the loader to check this corner case. Delete
`num_versions` to simplify code.
---
 elf/dl-lookup.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
  

Patch

diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index 989b073e4f..5c6b2dc3df 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -65,8 +65,7 @@  check_match (const char *const undef_name,
 	     const Elf_Symndx symidx,
 	     const char *const strtab,
 	     const struct link_map *const map,
-	     const ElfW(Sym) **const versioned_sym,
-	     int *const num_versions)
+	     const ElfW(Sym) **const versioned_sym)
 {
   unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
   assert (ELF_RTYPE_CLASS_PLT == 1);
@@ -147,9 +146,7 @@  check_match (const char *const undef_name,
 	      >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
 	    {
 	      /* Don't accept hidden symbols.  */
-	      if ((verstab[symidx] & 0x8000) == 0
-		  && (*num_versions)++ == 0)
-		/* No version so far.  */
+	      if ((verstab[symidx] & 0x8000) == 0)
 		*versioned_sym = sym;
 
 	      return NULL;
@@ -380,7 +377,6 @@  do_lookup_x (const char *undef_name, unsigned int new_hash,
 	continue;
 
       Elf_Symndx symidx;
-      int num_versions = 0;
       const ElfW(Sym) *versioned_sym = NULL;
 
       /* The tables for this map.  */
@@ -414,8 +410,7 @@  do_lookup_x (const char *undef_name, unsigned int new_hash,
 			symidx = ELF_MACHINE_HASH_SYMIDX (map, hasharr);
 			sym = check_match (undef_name, ref, version, flags,
 					   type_class, &symtab[symidx], symidx,
-					   strtab, map, &versioned_sym,
-					   &num_versions);
+					   strtab, map, &versioned_sym);
 			if (sym != NULL)
 			  goto found_it;
 		      }
@@ -439,18 +434,17 @@  do_lookup_x (const char *undef_name, unsigned int new_hash,
 	    {
 	      sym = check_match (undef_name, ref, version, flags,
 				 type_class, &symtab[symidx], symidx,
-				 strtab, map, &versioned_sym,
-				 &num_versions);
+				 strtab, map, &versioned_sym);
 	      if (sym != NULL)
 		goto found_it;
 	    }
 	}
 
-      /* If we have seen exactly one versioned symbol while we are
-	 looking for an unversioned symbol and the version is not the
-	 default version we still accept this symbol since there are
-	 no possible ambiguities.  */
-      sym = num_versions == 1 ? versioned_sym : NULL;
+      /* When looking for an unversioned symbol, a default version is a fallback
+	 when VER_NDX_GLOBAL is absent.  We don't repeat the check ensured by
+	 the linker: no two default version can be attached to the same symbol.
+	 */
+      sym = versioned_sym;
 
       if (sym != NULL)
 	{