views:

255

answers:

3

Linux: It there a way to edit a compiled shared library ?

specifically I am searching on how to add the DT_SYMBOLIC flag on an already compiled binary shared library?

Here is why I am asking this:

our application is composed of

  • our own libraries (static libXXX.a)
  • some 3rd party libs (binary-only shared libraries libYYY.so)

Everything is fine with the application so far.

Now I have replaced our own static* libXXX.a libraries with shared libraries versions (libXXX.so).

Everything compiles and links fine.

But when I run the application now it starts up and does a lot of expected processing - and then it crashes somewhere on half the way. A gdb backtrace shows that the crash occurs in some of the 3rd party shared libraries.

I searched around on SOF and found an interesting article here.

The tip I am referring to is

... add the DT_SYMBOLIC flag to the dynamic section ...

Although I doubt that it would really help I think it might be worth a try.

Any ideas how I can add the DT_SYMBOLIC flag on an already compiled/linked shared library ?

environment: debian lenny 64bit with g++ v4.2.4 and binutils v20.

EDIT: on solaris there is a program called elfedit. Is there something similar for linux ?

A: 

At the very least, you have to re-link the library with -Bsymbolic or -Wl,-Bsymbolic, depending on your compiler as symbol table mapping is a compile-time operation.

David Titarenco
Already tried to relink my own libs with -Wl,-Bsymbolic. Relinking the 3rd party libs is not possible because they are binary only.
A: 

If it's just a flag the painful approach is to tweak your elf header with a hex editor.

But if the 3rd party lib is crashing I'd personally look at the other end of things: what has changed about the library loading order because of you moving to shared.

ldd

and

export LD_DEBUG=libs

are your friends here.

dlopen()ing your shared libs after the runtime loader loads your 3rd party libs might be enough.

Nathan Kidd
Thanks for advice regarding library loading order. But I fear that I have to use the 'painful approach' you mentioned. Any help on how to edit an elf header is very appreciated!
Again, I can't really recommend this approach, but if I had to I would:1. read the ELF spec2. build a minimal library with and without -Bsymbolic and check the binary diff
Nathan Kidd
@Nathan: "... can't really recommend this approach ...". I agree. But still might be worth a try.
A: 

meanwhile l have found a tool called elfsh. It is an interactive shell for examining and editing ELF objects, binaries and shared libraries.

see here for the project page: http://www.eresi-project.org

Unfortunately it doesn't seem to be very stable. I have experienced a lot of crashes within elfsh itself.