This is the mail archive of the elfutils-devel@sourceware.org mailing list for the elfutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 09/10] readelf: Use elf_getshdrnum in print_shdr and print_phdr.


print_shdr didn't print the correct number of sections if there were
more than SHN_LORESERVE sections. print_phdr wouldn't match up the
(allocated) sections and segements if there were more than SHN_LORESERVE
sections in the ELF file.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog |  5 +++++
 src/readelf.c | 25 ++++++++++++++++++++-----
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 524c81a..6a702ee 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,10 @@
 2018-09-13  Mark Wielaard  <mark@klomp.org>
 
+	* readelf.c (print_shdr): Get number of section with elf_getshdrnum.
+	(print_phdr): Likewise.
+
+2018-09-13  Mark Wielaard  <mark@klomp.org>
+
 	* strip.c (handle_elf): Check against shstrndx, not e_shstrndx.
 	Explicitly set shdrstrndx for debug file.
 	* unstrip.c (copy_elf): Explicitly copy shstrndx.
diff --git a/src/readelf.c b/src/readelf.c
index 7b488ac..bddcd70 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -1184,15 +1184,24 @@ print_shdr (Ebl *ebl, GElf_Ehdr *ehdr)
   size_t shstrndx;
 
   if (! print_file_header)
-    printf (gettext ("\
-There are %d section headers, starting at offset %#" PRIx64 ":\n\
+    {
+      size_t sections;
+      if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
+	error (EXIT_FAILURE, 0,
+	       gettext ("cannot get number of sections: %s"),
+	       elf_errmsg (-1));
+
+      printf (gettext ("\
+There are %zd section headers, starting at offset %#" PRIx64 ":\n\
 \n"),
-	    ehdr->e_shnum, ehdr->e_shoff);
+	      sections, ehdr->e_shoff);
+    }
 
   /* Get the section header string table index.  */
   if (unlikely (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0))
     error (EXIT_FAILURE, 0,
-	   gettext ("cannot get section header string table index"));
+	   gettext ("cannot get section header string table index: %s"),
+	   elf_errmsg (-1));
 
   puts (gettext ("Section Headers:"));
 
@@ -1384,7 +1393,13 @@ print_phdr (Ebl *ebl, GElf_Ehdr *ehdr)
 	}
     }
 
-  if (ehdr->e_shnum == 0)
+  size_t sections;
+  if (unlikely (elf_getshdrnum (ebl->elf, &sections) < 0))
+    error (EXIT_FAILURE, 0,
+           gettext ("cannot get number of sections: %s"),
+           elf_errmsg (-1));
+
+  if (sections == 0)
     /* No sections in the file.  Punt.  */
     return;
 
-- 
1.8.3.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]