Question on how to copy an elf file and edit the .preinit_array section

Mark Wielaard
Fri Jun 19 20:15:05 GMT 2020


On Thu, 2020-06-11 at 15:25 +0100, Anastasios Andronidis via Elfutils-
devel wrote:
>  Could someone please help me with this: 
> I'm trying to do some runtime manipulation of an elf file and I'm a
> little bit lost with how to use libelf :/
> Basically I want to copy an elf file to a new one and edit
> .preinit_array section. This is similar to what objcopy --add-section 
> would do, but I only care about the elf headers and using libelf to
> achieve this.

elfutils libelf can certainly help with adding or updating section data
and if requested update the section headers. See for example
tests/addsections.c. But you are responsible for updating and keeping
the program headers in sync with the section data/offsets yourself
(this is why in a lot of test/examples you actually see elf_flagelf
(elf, ELF_C_SET, ELF_F_LAYOUT) (to tell libelf not to assume it can
adjust the section headers itself) when you have an ELF file with
program headers (because you don't want the data offsets to get out of



More information about the Elfutils-devel mailing list