[binutils-gdb] PR28763, SIGSEGV during processing of program headers via readelf

Alan Modra amodra@sourceware.org
Wed Feb 9 12:02:10 GMT 2022


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

commit 2969c3b37b228c34566e7fee63ee2395a1c227ad
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Feb 9 22:24:44 2022 +1030

    PR28763, SIGSEGV during processing of program headers via readelf
    
            PR 28763
            * readelf.c (process_file_header): Discard any cached program
            headers if there is an extension field for e_phnum in first
            section header.

Diff:
---
 binutils/readelf.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index e56e9b56393..b208db59334 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -5631,10 +5631,7 @@ process_file_header (Filedata * filedata)
       if (filedata->section_headers != NULL
 	  && header->e_phnum == PN_XNUM
 	  && filedata->section_headers[0].sh_info != 0)
-	{
-	  header->e_phnum = filedata->section_headers[0].sh_info;
-	  printf (" (%u)", header->e_phnum);
-	}
+	printf (" (%u)", filedata->section_headers[0].sh_info);
       putc ('\n', stdout);
       printf (_("  Size of section headers:           %u (bytes)\n"),
 	      header->e_shentsize);
@@ -5667,7 +5664,12 @@ process_file_header (Filedata * filedata)
     {
       if (header->e_phnum == PN_XNUM
 	  && filedata->section_headers[0].sh_info != 0)
-	header->e_phnum = filedata->section_headers[0].sh_info;
+	{
+	  /* Throw away any cached read of PN_XNUM headers.  */
+	  free (filedata->program_headers);
+	  filedata->program_headers = NULL;
+	  header->e_phnum = filedata->section_headers[0].sh_info;
+	}
       if (header->e_shnum == SHN_UNDEF)
 	header->e_shnum = filedata->section_headers[0].sh_size;
       if (header->e_shstrndx == (SHN_XINDEX & 0xffff))


More information about the Binutils-cvs mailing list