There are use cases where the Elf image created by elf_memory is
manipulated, through the libelf interfaces, in place. This doesn't
work anymore since we changed elf_memory to assume the memory is
read-only in elfutils 0.191. commit
cc44ac674 ('libelf: Treat
elf_memory as if using ELF_C_READ_MMAP').
The reason for that change was that if elf_memory was given a
read-only memory image then decompressing a section with elf_compress
would crash. Since it directly writes the updated Shdr size. If you do
want to use elf_compress on an Elf created by elf_memory you have make
sure the memory is writable. You can do this for example by using mmap
PROTE_WRITE and MAP_PRIVATE.
* libelf/elf_memory.c (elf_memory): Call
__libelf_read_mmaped_file with ELF_C_READ_MMAP_PRIVATE.
* tests/elfgetzdata.c (main): Use mmap PROT_WRITE and MAP_PRIVATE.
Signed-off-by: Mark Wielaard <mark@klomp.org>
return NULL;
}
- return __libelf_read_mmaped_file (-1, image, 0, size, ELF_C_READ_MMAP, NULL);
+ return __libelf_read_mmaped_file (-1, image, 0, size,
+ ELF_C_READ_MMAP_PRIVATE, NULL);
}
else
{
assert (do_mem);
- // We mmap the memory ourselves, explicitly PROT_READ only
+ // We mmap the memory ourselves, explicitly PROT_READ | PROT_WRITE
+ // elf_memory needs writable memory when using elf_compress.
struct stat st;
if (fstat (fd, &st) != 0)
{
continue;
}
map_size = st.st_size;
- map_address = mmap (NULL, map_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ map_address = mmap (NULL, map_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE, fd, 0);
if (map_address == MAP_FAILED)
{
printf ("%s cannot mmap %s\n", argv[cnt], strerror (errno));