Cont.: Adding a new section to an ELF file aborts with Assertion `shdr != NULL'

Mark Wielaard mark@klomp.org
Fri Dec 24 22:15:17 GMT 2021


Hi,

On Fri, Dec 24, 2021 at 02:42:05PM -0600, P.B. via Elfutils-devel wrote:
> Sorry I for some reason could not reply to the original thread here to
> follow up:
> https://www.mail-archive.com/elfutils-devel@sourceware.org/msg04076.html
>
> But I am facing the same issue, adding the section to a very simple ELF
> file fails with  Assertion `shdr != NULL'. I am new to this so I am sure I
> am doing something wrong, but not exactly certain what it is. I have also
> tried to open ELF with ELF_C_WRITE as opposed to ELF_C_RDWR but then my
> error is "elf_newdata() failed: executable header not created first" so I
> am not sure what the proper sequence of events would be to add a section.
> 
> Below is my code and a run with the message.

Unfortunately the original report didn't come with full example
sources and your sources aren't complete either. But I just played a
bit with some simple code that adds a section and I think it is a bug
in libelf. As the original report said (in a followup) actually
reading any of the existing section headers works around the assert. I
think we missed this bug because normally you would at least read the
existing section headers before adding new sections.

Could you try first reading some section header before adding a new one?
e.g. just add something like:
    scn = elf_nextscn(e, NULL);
    shdr = elf64_getshdr(scn);

Hope that helps to workaround the bug for now.

Cheers,

Mark


More information about the Elfutils-devel mailing list