Looking for portable libelf sample code

Looking for portable libelf sample code

Post by Chuck Hou » Wed, 12 Feb 1997 04:00:00



I've been trying to write a simple test program which adds a new section
to an ELF format executable using libelf.  I've been able to get the test
program to work under Solaris, but under Irix and Linux I'm having
problems.

Under Irix, the modified executables always core dump.

Under Linux (using Michael Riepe's libelf), the modified executable core
dumps if the added section is too large (over a mega-byte).

Has anyone been able to write portable libelf code that works on all these
platforms or any two?  Is sample source available?

The only source code I found that uses libelf is the scheme interpreter
"Elk", but it only uses it to read symbols from ELF files.

Thanks - Chuck

 
 
 

Looking for portable libelf sample code

Post by Richard Goldstei » Thu, 13 Feb 1997 04:00:00



> I've been trying to write a simple test program which adds a new section
> to an ELF format executable using libelf.  I've been able to get the test
> program to work under Solaris, but under Irix and Linux I'm having
> problems.

> Under Irix, the modified executables always core dump.

> Under Linux (using Michael Riepe's libelf), the modified executable core
> dumps if the added section is too large (over a mega-byte).

> Has anyone been able to write portable libelf code that works on all these
> platforms or any two?  Is sample source available?

> The only source code I found that uses libelf is the scheme interpreter
> "Elk", but it only uses it to read symbols from ELF files.

What you're trying to do can be intrinsically architecture-
dependent, as things like certain relocations being performed
statically at link time, which become invalidated by your mods
to the file.  As well, the runtime linker may have some requirements
about the layout of certain sections (i.e. see the ELF_F_LAYOUT
flag).

rick

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        Richard M. Goldstein

        64-bit Linkers, Libs & Executables
        SunSoft, Inc.

        "Without time we pick up all the streams,
        and find the leaves that drift out inbetween..."
                                -Kirkwood

 
 
 

Looking for portable libelf sample code

Post by David B.Anders » Thu, 13 Feb 1997 04:00:00





>> I've been trying to write a simple test program which adds a new section
>> to an ELF format executable using libelf.  I've been able to get the test
>> program to work under Solaris, but under Irix and Linux I'm having
>> problems.

>> Under Irix, the modified executables always core dump.

>> Under Linux (using Michael Riepe's libelf), the modified executable core
>> dumps if the added section is too large (over a mega-byte).

>> Has anyone been able to write portable libelf code that works on all these
>> platforms or any two?  Is sample source available?

>> The only source code I found that uses libelf is the scheme interpreter
>> "Elk", but it only uses it to read symbols from ELF files.

>What you're trying to do can be intrinsically architecture-
>dependent, as things like certain relocations being performed
>statically at link time, which become invalidated by your mods
>to the file.  As well, the runtime linker may have some requirements
>about the layout of certain sections (i.e. see the ELF_F_LAYOUT
>flag).

Whether the result is usable depends on many things.

The libelf examples in the elf_begin man page
do leave a lot to the imagination.

Perhaps if you posted the output of
        elfdump -h -o -L
before and after your transformation someone could
figure out (or make a useful guess) what was wrong
in the IRIX case.

Hard to guess given the limited info above.

 
 
 

Looking for portable libelf sample code

Post by Andreas Schw » Sat, 15 Feb 1997 04:00:00


|> I've been trying to write a simple test program which adds a new section
|> to an ELF format executable using libelf.  I've been able to get the test
|> program to work under Solaris, but under Irix and Linux I'm having
|> problems.

Changing the layout of a ELF binary can be very tricky, see for example
unexelf.c in the Emacs distribution.
--
Andreas Schwab                                      "And now for something

 
 
 

Looking for portable libelf sample code

Post by Chuck Hou » Sat, 15 Feb 1997 04:00:00



> Changing the layout of a ELF binary can be very tricky, see for example
> unexelf.c in the Emacs distribution.

Yes, unexelf.c is a nightmare.  Even worse, there are actually three
different unexelf's - SunOS, Irix and Sinix versions.

But, unexelf.c is complicated because it directly modifies an elf file -
it does not use libelf.  Using libelf would eliminate alot of the druggery
of recalculating file offsets, etc.

If anyone has written a version of unexelf.c which uses libelf, I'd love
to see it.

- Chuck