This is the mail archive of the
mailing list for the elfutils project.
elf_cntl(ELF_C_FDREAD) breaks elf64_getshdr() with non-mmap()ed ELF files
- From: Nix <nix at esperi dot org dot uk>
- To: elfutils-devel at lists dot fedorahosted dot org
- Date: Tue, 03 Jul 2012 16:17:31 +0100
- Subject: elf_cntl(ELF_C_FDREAD) breaks elf64_getshdr() with non-mmap()ed ELF files
I'm seeing a problem with all versions of elfutils from 0.152 up to
latest git head (a problem probably originating in 2007).
The symptoms are that using elf_cntl(..., ELF_C_READ, NULL) to pull a
whole ELF file into memory so you can close it causes assertion failures
inside elf64_getshdr() and gelf_getshdr():
scnwalk: elf32_getshdr.c:95: load_shdr_wrlock: Assertion `ehdr->e_ident != 1 || (! 1 && ((uintptr_t) file_shdr & (__alignof__ (Elf64_Shdr) - 1)) != 0)' failed.
These failures do not occur if you haven't used elf_cntl(), or if you
have used ELF_C_READ_MMAP rather than ELF_C_READ at elf_begin() time.
Testcase below. It looks to me like elf_begin.c:file_read_elf() is
concluding that the ELF file will require increased alignment to use (so
cannot be directly mapped), following which
elf32_getshdr.c:load_shdr_wrlock() concludes that it does *not* require
increased alignment, thus the assertion failure. These are perfectly
normal object files compiled on the same architecture, no byteswapping
needed and one would assume no alignment increase either: indeed you can
see this failure when you try to work over the very object files that
comprise the version of elfutils being tested.
I'll look into this further, but for now here's a testcase, which on
64-bit x86 fails for me with the assertion failure above when run with
e.g. elfutils/libelf/elf64_getshdr.o as a parameter (assuming you've
compiled elfutils so the object file is there, of course).
Removing the call to elf_cntl() (and the close(), obviously) fixes it,
as does changing ELF_C_READ to ELF_C_READ_MMAP.
int main (int argc, char *argv)
if (argc < 2)
fprintf (stderr, "syntax: scnwalk filename\n");
if (elf_version(EV_CURRENT) == EV_NONE)
fprintf (stderr, "elf_version failed: %s\n",
if ((fd = open (argv, O_RDONLY)) < 0)
fprintf (stderr, "Cannot open %s: %s\n",
argv, strerror (errno));
foo = elf_begin (fd, ELF_C_READ, NULL);
if (elf_cntl (foo, ELF_C_FDREAD) < 0)
fprintf (stderr, "Cannot elf_cntl(%p) for %s: %s\n", foo, argv,
for (sp = NULL; (sp = elf_nextscn (foo, sp)) != NULL;)
elf64_getshdr (sp); /* boom! */