[binutils-gdb] readelf: Don't dump GOT section after seeing error

"H.J. Lu 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=f94273aeb37908c32aa26530ff56e862f0060daf

commit f94273aeb37908c32aa26530ff56e862f0060daf
Author: H.J. Lu <[email protected]>
Date:   Thu Aug 6 10:33:02 2026 +0800

    readelf: Don't dump GOT section after seeing error
    
    Don't dump GOT section contents after seeing errors in input:
    
    readelf: Error: Section 10 has invalid sh_entsize of 0
    readelf: Error: (Using the expected size of 18 for the rest of this dump)
    readelf: Error: Too many program headers - 0x3030 - the file is not that big
    
            PR binutils/34473
            * elfcomm.c (seen_error): New.
            (seen_elf_error): Likewise.
            (clear_elf_error): Likewise.
            (error): Set seen_error to true.
            * elfcomm.h (seen_elf_error): New.
            (clear_elf_error): Likewise.
            * readelf.c (process_got_section_contents): Return false if
            seen_elf_error returns true.
            (main): Call clear_elf_error before calling process_file.
    
    Signed-off-by: H.J. Lu <[email protected]>

Diff:
---
 binutils/elfcomm.c | 16 ++++++++++++++++
 binutils/elfcomm.h |  3 +++
 binutils/readelf.c | 10 ++++++++--
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/binutils/elfcomm.c b/binutils/elfcomm.c
index 4a4f5368220..d54de39ecdd 100644
--- a/binutils/elfcomm.c
+++ b/binutils/elfcomm.c
@@ -35,11 +35,27 @@
 
 extern char *program_name;
 
+static bool seen_error = false;
+
+bool
+seen_elf_error (void)
+{
+  return seen_error;
+}
+
+void
+clear_elf_error (void)
+{
+  seen_error = false;
+}
+
 void
 error (const char *message, ...)
 {
   va_list args;
 
+  seen_error = true;
+
   /* Try to keep error messages in sync with the program's normal output.  */
   fflush (stdout);
 
diff --git a/binutils/elfcomm.h b/binutils/elfcomm.h
index 953bc3d1bc3..5bd61e9587d 100644
--- a/binutils/elfcomm.h
+++ b/binutils/elfcomm.h
@@ -30,6 +30,9 @@ extern void error (const char *, ...) ATTRIBUTE_PRINTF_1;
 extern void warn (const char *, ...) ATTRIBUTE_PRINTF_1;
 extern void inform (const char *, ...) ATTRIBUTE_PRINTF_1;
 
+extern bool seen_elf_error (void);
+extern void clear_elf_error (void);
+
 extern void (*byte_put) (unsigned char *, uint64_t, unsigned int);
 extern void byte_put_little_endian (unsigned char *, uint64_t, unsigned int);
 extern void byte_put_big_endian (unsigned char *, uint64_t, unsigned int);
diff --git a/binutils/readelf.c b/binutils/readelf.c
index a52fe2b8d63..c7076d22e5c 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -21667,6 +21667,9 @@ process_got_section_contents (Filedata * filedata)
   if (!do_got_section_contents || all_relocations_count == 0)
     return res;
 
+  if (seen_elf_error ())
+    return false;
+
   switch (filedata->file_header.e_type)
     {
     case ET_DYN:
@@ -25607,8 +25610,11 @@ main (int argc, char ** argv)
 
   err = false;
   while (optind < argc)
-    if (! process_file (argv[optind++]))
-      err = true;
+    {
+      clear_elf_error ();
+      if (! process_file (argv[optind++]))
+	err = true;
+    }
 
   free (cmdline.dump_sects);
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.