From 2969c3b37b228c34566e7fee63ee2395a1c227ad Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 9 Feb 2022 22:24:44 +1030 Subject: [PATCH] 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. --- 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)) -- 2.30.2