[PATCH v3] readelf: Save and dump the original section header values

H.J. Lu hjl.tools@gmail.com
Sat Jul 4 11:06:13 GMT 2026


On Sat, Jul 4, 2026 at 11:01 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Sat, Jul 4, 2026 at 8:39 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > On Sat, Jul 4, 2026 at 7:49 AM Alan Modra <amodra@gmail.com> wrote:
> > >
> > > On Fri, Jul 03, 2026 at 10:30:14PM +0800, H.J. Lu wrote:
> > > > validate_section_info clears the garbage values in the section header
> > > > to avoid crash later.  Save and dump the original section header values
> > > > to make the garbage values in the section header visible when dumping
> > > > section headers.
> > >
> > > I think this would be better done the other way around.  ie. have a
> > > Elf_Internal_Shdr **sane_section_headers that is initialised to point
> > > at entries in section_headers, with sane_section_headers[i] allocated
> > > as necessary when needing to correct a bogus header.
> > >
> >
> > I have thought about something similar and decided against it
> > since filedata->section_headers is used in many places.   However,
> > I can change filedata->orig_section_headers to on demand.
> >
> > --
> > H.J.
>
> Changes in v2:
>
> 1.  filedata->orig_section_headers is changed to on demand.

Changes in v3:

1.  Clear filedata->orig_section_headers first.

> --
> H.J.
> ---
> validate_section_info clears the garbage values in the section header
> to avoid crash later.  Save and dump the original section header values
> to make the garbage values in the section header visible when dumping
> section headers.
>
> Note: orig_section_headers, instead of sane_section_headers, is added to
> filedata since filedata->section_headers is used in many places.  Replace
> filedata->section_headers with filedata->sane_section_headers requires
> a much bigger change.
>
> * readelf.c (filedata): Add orig_section_headers.
> (save_original_section_header_values): New.
> (validate_section_info): Add a pointer to the original section
> header and call save_original_section_header_values to save the
> original section header values before clearing the section header
> fields.
> (get_32bit_section_headers): Allocate the original section header
> buffer.  Pass the original section header pointer to
> validate_section_info.
> (get_64bit_section_headers): Likewise.
> (process_section_headers): Dump the original section header
> values if they exist.
> (process_relocs): Pass a dummy original section pointer to
> validate_section_info.
> (free_filedata): Free filedata->orig_section_headers.
> * testsuite/binutils-all/corrupt-1.elf.bz2: New file.
> * testsuite/binutils-all/corrupt-1.r: Likewise.
> * testsuite/binutils-all/readelf.exp: Run corrupt-1.elf test.



-- 
H.J.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v3-0001-readelf-Save-and-dump-the-original-section-header.patch
Type: text/x-patch
Size: 12628 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/binutils/attachments/20260704/66d3ff21/attachment-0001.bin>


More information about the Binutils mailing list