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

Anastasios Andronidis
Wed Jun 24 13:02:31 GMT 2020

Hello Mark,

thanks a billion for you answer! You gave me a very good starting point!


> On 19 Jun 2020, at 21:15, Mark Wielaard <> wrote:
> Hi,
> 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
> sync).
> Cheers,
> Mark

More information about the Elfutils-devel mailing list