Thank you, Mark. It worked. Sequence I followed:
``` // Open ELF readwrite if ((e = elf_begin(fd, ELF_C_RDWR, NULL)) == NULL) errx(EXIT_FAILURE, "elf_begin() failed: %s.", elf_errmsg(-1)); // Read existing section scn = elf_nextscn(e, NULL); shdr = elf64_getshdr(scn); // Create new section (index after last) if ((scn = elf_newscn(e)) == NULL) errx(EXIT_FAILURE, "elf_newscn() failed: %s.", elf_errmsg(-1)); ``` On Fri, Dec 24, 2021 at 4:15 PM Mark Wielaard <m...@klomp.org> wrote: > 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 >