[binutils-gdb] readelf: fold get_{32,64}bit_dynamic_section()

Jan Beulich via Binutils-cvs <[email protected]>
Newsgroups gmane.comp.gnu.binutils.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e200cc751e1d796cb5473742bb14590a51210ef2

commit e200cc751e1d796cb5473742bb14590a51210ef2
Author: Jan Beulich <[email protected]>
Date:   Fri Aug 14 10:55:36 2026 +0200

    readelf: fold get_{32,64}bit_dynamic_section()
    
    PR binutils/34356
    
    They're identical except for the types used, which can be addressed by
    compiling the same code twice.

Diff:
---
 binutils/readelf-nn.c |  48 +++++++++++++++++++++++
 binutils/readelf.c    | 105 +-------------------------------------------------
 2 files changed, 50 insertions(+), 103 deletions(-)

diff --git a/binutils/readelf-nn.c b/binutils/readelf-nn.c
index 50d9605892e..071fd80cc46 100644
--- a/binutils/readelf-nn.c
+++ b/binutils/readelf-nn.c
@@ -280,4 +280,52 @@ ElfXX(_get_symbols) (Filedata *filedata, const Elf_Internal_Shdr *section,
   return isyms;
 }
 
+static bool
+ElfXX(_get_dynamic_section) (Filedata * filedata)
+{
+  ElfXX(_External_Dyn) * edyn, * ext;
+  Elf_Internal_Dyn * entry;
+
+  edyn = get_data (NULL, filedata, filedata->dynamic_addr, 1,
+		   filedata->dynamic_size, _("dynamic section"));
+  if (!edyn)
+    return false;
+
+  /* SGI's ELF has more than one section in the DYNAMIC segment, and we
+     might not have the luxury of section headers.  Look for the DT_NULL
+     terminator to determine the number of entries.  */
+  for (ext = edyn, filedata->dynamic_nent = 0;
+       /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
+       (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
+       ext++)
+    {
+      filedata->dynamic_nent++;
+      if (BYTE_GET (ext->d_tag) == DT_NULL)
+	break;
+    }
+
+  filedata->dynamic_section
+    = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
+  if (filedata->dynamic_section == NULL)
+    {
+      error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
+	     filedata->dynamic_nent);
+      free (edyn);
+      return false;
+    }
+
+  /* Convert from external to internal formats.  */
+  for (ext = edyn, entry = filedata->dynamic_section;
+       entry < filedata->dynamic_section + filedata->dynamic_nent;
+       ext++, entry++)
+    {
+      entry->d_tag      = BYTE_GET (ext->d_tag);
+      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
+    }
+
+  free (edyn);
+
+  return true;
+}
+
 #undef ElfXX
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 4560dc55c5e..0a8659fca93 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -12132,107 +12132,6 @@ dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
   putchar ('\n');
 }
 
-static bool
-get_32bit_dynamic_section (Filedata * filedata)
-{
-  Elf32_External_Dyn * edyn;
-  Elf32_External_Dyn * ext;
-  Elf_Internal_Dyn * entry;
-
-  edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
-					  filedata->dynamic_addr, 1,
-					  filedata->dynamic_size,
-					  _("dynamic section"));
-  if (!edyn)
-    return false;
-
-  /* SGI's ELF has more than one section in the DYNAMIC segment, and we
-     might not have the luxury of section headers.  Look for the DT_NULL
-     terminator to determine the number of entries.  */
-  for (ext = edyn, filedata->dynamic_nent = 0;
-       (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
-       ext++)
-    {
-      filedata->dynamic_nent++;
-      if (BYTE_GET (ext->d_tag) == DT_NULL)
-	break;
-    }
-
-  filedata->dynamic_section
-    = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
-  if (filedata->dynamic_section == NULL)
-    {
-      error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
-	     filedata->dynamic_nent);
-      free (edyn);
-      return false;
-    }
-
-  for (ext = edyn, entry = filedata->dynamic_section;
-       entry < filedata->dynamic_section + filedata->dynamic_nent;
-       ext++, entry++)
-    {
-      entry->d_tag      = BYTE_GET (ext->d_tag);
-      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
-    }
-
-  free (edyn);
-
-  return true;
-}
-
-static bool
-get_64bit_dynamic_section (Filedata * filedata)
-{
-  Elf64_External_Dyn * edyn;
-  Elf64_External_Dyn * ext;
-  Elf_Internal_Dyn * entry;
-
-  /* Read in the data.  */
-  edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
-					  filedata->dynamic_addr, 1,
-					  filedata->dynamic_size,
-					  _("dynamic section"));
-  if (!edyn)
-    return false;
-
-  /* SGI's ELF has more than one section in the DYNAMIC segment, and we
-     might not have the luxury of section headers.  Look for the DT_NULL
-     terminator to determine the number of entries.  */
-  for (ext = edyn, filedata->dynamic_nent = 0;
-       /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer.  */
-       (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
-       ext++)
-    {
-      filedata->dynamic_nent++;
-      if (BYTE_GET (ext->d_tag) == DT_NULL)
-	break;
-    }
-
-  filedata->dynamic_section
-    = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
-  if (filedata->dynamic_section == NULL)
-    {
-      error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
-	     filedata->dynamic_nent);
-      free (edyn);
-      return false;
-    }
-
-  /* Convert from external to internal formats.  */
-  for (ext = edyn, entry = filedata->dynamic_section;
-       entry < filedata->dynamic_section + filedata->dynamic_nent;
-       ext++, entry++)
-    {
-      entry->d_tag      = BYTE_GET (ext->d_tag);
-      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
-    }
-
-  free (edyn);
-
-  return true;
-}
-
 static bool
 get_dynamic_section (Filedata *filedata)
 {
@@ -12240,9 +12139,9 @@ get_dynamic_section (Filedata *filedata)
     return true;
 
   if (is_32bit_elf)
-    return get_32bit_dynamic_section (filedata);
+    return Elf32_get_dynamic_section (filedata);
   else
-    return get_64bit_dynamic_section (filedata);
+    return Elf64_get_dynamic_section (filedata);
 }
 
 static void
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.