[binutils-gdb/binutils-2_42-branch] elf: Add glibc version dependency only if needed

H.J. Lu hjl@sourceware.org
Tue Jul 9 21:45:28 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c439c1e1f56e7f9a678051626a9d4bd892257b66

commit c439c1e1f56e7f9a678051626a9d4bd892257b66
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jul 9 01:48:54 2024 -0700

    elf: Add glibc version dependency only if needed
    
    There is no need to add a needed glibc version if the glibc base version
    includes the needed glibc version.
    
            PR ld/31966
            * elflink.c (elf_link_add_glibc_verneed): Add glibc_minor_base.
            Skip if the glibc base version includes the needed glibc version.
            (_bfd_elf_link_add_glibc_version_dependency): Initialize
            glibc_minor_base to INT_MAX and pass it to
            elf_link_add_glibc_verneed.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
    (cherry picked from commit 7757f66fdbe2a9fa4b51aa7792532fddda4b295c)

Diff:
---
 bfd/elflink.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index c2494b3e12e..0fd06d11c24 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -2206,16 +2206,19 @@ _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data)
 

 /* Return the glibc version reference if VERSION_DEP is added to the
    list of glibc version dependencies successfully.  VERSION_DEP will
-   be put into the .gnu.version_r section.  */
+   be put into the .gnu.version_r section.  GLIBC_MINOR_BASE is the
+   pointer to the glibc minor base version.  */
 
 static Elf_Internal_Verneed *
 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
 			    Elf_Internal_Verneed *glibc_verref,
-			    const char *version_dep)
+			    const char *version_dep,
+			    int *glibc_minor_base)
 {
   Elf_Internal_Verneed *t;
   Elf_Internal_Vernaux *a;
   size_t amt;
+  int minor_version = -1;
 
   if (glibc_verref != NULL)
     {
@@ -2231,8 +2234,6 @@ elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
     }
   else
     {
-      bool is_glibc;
-
       for (t = elf_tdata (rinfo->info->output_bfd)->verref;
 	   t != NULL;
 	   t = t->vn_nextref)
@@ -2246,7 +2247,6 @@ elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
       if (t == NULL)
 	return t;
 
-      is_glibc = false;
       for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
 	{
 	  /* Return if VERSION_DEP dependency has been added.  */
@@ -2255,12 +2255,24 @@ elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo,
 	    return t;
 
 	  /* Check if libc.so provides GLIBC_2.XX version.  */
-	  if (!is_glibc && startswith (a->vna_nodename, "GLIBC_2."))
-	    is_glibc = true;
+	  if (startswith (a->vna_nodename, "GLIBC_2."))
+	    {
+	      minor_version = strtol (a->vna_nodename + 8, NULL, 10);
+	      if (minor_version < *glibc_minor_base)
+		*glibc_minor_base = minor_version;
+	    }
 	}
 
       /* Skip if it isn't linked against glibc.  */
-      if (!is_glibc)
+      if (minor_version < 0)
+	return NULL;
+    }
+
+  /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP.  */
+  if (startswith (version_dep, "GLIBC_2."))
+    {
+      minor_version = strtol (version_dep + 8, NULL, 10);
+      if (minor_version <= *glibc_minor_base)
 	return NULL;
     }
 
@@ -2292,10 +2304,12 @@ _bfd_elf_link_add_glibc_version_dependency
    const char *version_dep[])
 {
   Elf_Internal_Verneed *t = NULL;
+  int glibc_minor_base = INT_MAX;
 
   do
     {
-      t = elf_link_add_glibc_verneed (rinfo, t, *version_dep);
+      t = elf_link_add_glibc_verneed (rinfo, t, *version_dep,
+				      &glibc_minor_base);
       /* Return if there is no glibc version reference.  */
       if (t == NULL)
 	return;


More information about the Binutils-cvs mailing list