[PATCH] Fix "gdb --write" with core files

Alan Modra amodra@gmail.com
Fri May 13 02:57:06 GMT 2022


On Thu, May 12, 2022 at 03:49:15PM +0100, Pedro Alves wrote:
> That happens because _bfd_elf_set_section_contents does this to seek
> to the section's offset:
> 
>   pos = hdr->sh_offset + offset;
>   if (bfd_seek (abfd, pos, SEEK_SET) != 0
>       || bfd_bwrite (location, count, abfd) != count)
>     return false;
> 
> ... and 'hdr->sh_offset' is zero, so we seek to just OFFSET, which is
> incorrect.  The reason 'hdr->sh_offset' is zero is that
> kernel-generated core files normally don't even have a section header
> table (gdb-generated ones do, but that's more an accident than a
> feature), and indeed elf_core_file_p doesn't even try to read sections
> at all:

I'm a little concerned about your patch keying off both_direction.  In
particular it seems odd to change bfd behaviour when reading depending
on whether the file was opened "r" or "r+".

Did you try changing
  pos = hdr->sh_offset + offset;
to
  pos = section->filepos + offset;
in _bfd_elf_set_section_contents?  I think that should work for you,
and not break ELF targets.

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list