public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2] elf: Remove one-default-version check when searching an unversioned symbol
@ 2022-05-28  0:56 Fangrui Song
  2022-05-30 13:08 ` Florian Weimer
  0 siblings, 1 reply; 9+ messages in thread
From: Fangrui Song @ 2022-05-28  0:56 UTC (permalink / raw)
  To: libc-alpha

When searching an unversioned symbol in an object with DT_VERDEF, we
have two cases:

* there is a VER_NDX_GLOBAL definition
* there are a few version index>=2 definitions, with at most one being
  the default version

`versioned_sym` may be set in the second case.  Since the linker ensures
at most one default version (otherwise ``multiple definition of
`foo'``), it is of very little value for the loader to check this corner
case.  Delete `num_versions` to simplify code.

While here, add some comments.

--
Changes from v1
* Add comments
* Clarify commit message
---
 elf/dl-lookup.c | 52 +++++++++++++++++++------------------------------
 1 file changed, 20 insertions(+), 32 deletions(-)

diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c
index a42f6d5390..0245f76799 100644
--- a/elf/dl-lookup.c
+++ b/elf/dl-lookup.c
@@ -66,8 +66,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);
@@ -92,6 +91,9 @@ check_match (const char *const undef_name,
     return NULL;
 
   const ElfW(Half) *verstab = map->l_versyms;
+  /* If the object is versioned, the linker ensures that either there is a
+     VER_NDX_GLOBAL definition or there are a few version index>=2 definitions,
+     with at most one being the default version. */
   if (version != NULL)
     {
       if (__glibc_unlikely (verstab == NULL))
@@ -124,33 +126,22 @@ check_match (const char *const undef_name,
     }
   else
     {
-      /* No specific version is selected.  There are two ways we
-	 can got here:
+      /* Looking for an unversioned symbol.  If the object is unversioned or
+	 there is a VER_NDX_GLOBAL definition, return sym.  Otherwise:
 
-	 - a binary which does not include versioning information
-	 is loaded
+	 If DL_LOOKUP_RETURN_NEWEST is set, return the default version if
+	 exists.
 
-	 - dlsym() instead of dlvsym() is used to get a symbol which
-	 might exist in more than one form
-
-	 If the library does not provide symbol version information
-	 there is no problem at all: we simply use the symbol if it
-	 is defined.
-
-	 These two lookups need to be handled differently if the
-	 library defines versions.  In the case of the old
-	 unversioned application the oldest (default) version
-	 should be used.  In case of a dlsym() call the latest and
-	 public interface should be returned.  */
+	 If DL_LOOKUP_RETURN_NEWEST is unset, return the index 2 definition if
+	 exists, otherwise return the default version if exists.  When the
+	 object became versioned, the original unversioned definition may take
+	 index 2.  */
       if (verstab != NULL)
 	{
 	  if ((verstab[symidx] & 0x7fff)
 	      >= ((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;
@@ -381,7 +372,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.  */
@@ -415,8 +405,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;
 		      }
@@ -440,18 +429,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 (and when DL_LOOKUP_RETURN_NEWEST is set,
+	 index 2 is also absent).  We don't repeat the check ensured by the
+	 linker: a symbol name has at most one default version.  */
+      sym = versioned_sym;
 
       if (sym != NULL)
 	{
-- 
2.36.1.124.g0e6072fb45-goog


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

end of thread, other threads:[~2022-06-28 20:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-28  0:56 [PATCH v2] elf: Remove one-default-version check when searching an unversioned symbol Fangrui Song
2022-05-30 13:08 ` Florian Weimer
2022-05-31  6:24   ` Fangrui Song
2022-05-31 19:39     ` Florian Weimer
2022-06-01  4:07       ` Fangrui Song
2022-06-01  6:49         ` Florian Weimer
2022-06-01  8:14           ` Fangrui Song
2022-06-10 21:44             ` Fangrui Song
2022-06-28 20:22               ` Fangrui Song

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