[PATCH 5/5] readelf: Consolidate get_[32|64]bit_dynamic_section

"H.J. Lu" <[email protected]>
Newsgroups gmane.comp.gnu.binutils
Message-ID <[email protected]>
Consolidate get_32bit_elf_symbols and get_64bit_dynamic_section into
get_dynamic_section.  Use BYTE_GET_SIZE to retrieve external dynamic
tags.

	PR binutils/34356
	* readelf.c (get_32bit_dynamic_section): Renamed to ...
	(get_dynamic_section): This.  Updated to support 64-bit dynamic
	section.
	(get_64bit_dynamic_section): Removed.

Signed-off-by: H.J. Lu <[email protected]>
---
 binutils/readelf.c | 91 +++++++++-------------------------------------
 1 file changed, 18 insertions(+), 73 deletions(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index fe87467a032..bcf7c4cfac7 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -12406,79 +12406,35 @@ dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
 }
 
 static bool
-get_32bit_dynamic_section (Filedata * filedata)
+get_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;
-}
+  if (filedata->dynamic_section)
+    return true;
 
-static bool
-get_64bit_dynamic_section (Filedata * filedata)
-{
-  Elf64_External_Dyn * edyn;
-  Elf64_External_Dyn * ext;
+  void * edyn;
+  void * 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"));
+  edyn = get_data (NULL, filedata, filedata->dynamic_addr, 1,
+		   filedata->dynamic_size, _("dynamic section"));
   if (!edyn)
     return false;
 
+  unsigned int elf_class_size = is_32bit_elf ? 4 : 8;
+
   /* 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++)
+       ext += elf_class_size)
     {
+      bfd_vma d_tag;
       filedata->dynamic_nent++;
-      if (BYTE_GET (ext->d_tag) == DT_NULL)
+      BYTE_GET_SIZE (d_tag, ext, elf_class_size);
+      if (d_tag == DT_NULL)
 	break;
     }
 
@@ -12493,12 +12449,13 @@ get_64bit_dynamic_section (Filedata * filedata)
     }
 
   /* Convert from external to internal formats.  */
-  for (ext = edyn, entry = filedata->dynamic_section;
+  ext = edyn;
+  for (entry = filedata->dynamic_section;
        entry < filedata->dynamic_section + filedata->dynamic_nent;
-       ext++, entry++)
+       entry++)
     {
-      entry->d_tag      = BYTE_GET (ext->d_tag);
-      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
+      BYTE_GET_SIZE (entry->d_tag, ext, elf_class_size);
+      BYTE_GET_SIZE (entry->d_un.d_val, ext, elf_class_size);
     }
 
   free (edyn);
@@ -12506,18 +12463,6 @@ get_64bit_dynamic_section (Filedata * filedata)
   return true;
 }
 
-static bool
-get_dynamic_section (Filedata *filedata)
-{
-  if (filedata->dynamic_section)
-    return true;
-
-  if (is_32bit_elf)
-    return get_32bit_dynamic_section (filedata);
-  else
-    return get_64bit_dynamic_section (filedata);
-}
-
 static void
 print_dynamic_flags (uint64_t flags)
 {
-- 
2.55.0
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.