How to link an existing shared library with an additional object file
I have an existing Linux shared partition file (shared library) that has been removed. I want to create a new version of the library with some additional functionality. I was hoping that something like the following would work, but it doesn't:
ld -o newlib.so newfuncs.o --whole-archive existinglib.so
I have no source for the existing library. I could get it, but getting a complete build environment with the required dependencies would be a lot of effort for what seems like a simple problem.
source to share
You may like to try to approach this from a slightly different angle by loading your object using preload.
Set LD_PRELOAD to point to a new object
export LD_PRELOAD=/my/newfuncs/dir/newfuncs.o
and point the existing library in the same way via LD_LIBRARY_PATH.
It will then instruct the linker when to search for the required symbols in your object, before looking for objects located in your LD_LIBRARY_PATH.
BTW You can place calls into your object so that you can then call a function that would have been called if you had not specified the LD_PRELOAD object or objects. This is why it is sometimes called interposing.
This is how the memory allocation analysis tools work. They insert versions of malloc () and free () that write calls to alloc () and free () and then call the actual system allocation and free functions to perform memory management.
Interwebs has many tutorials on how to use LD_PRELOAD. One of the original and best still is " Making Library Intermediates for Fun and Profit ". Although written nine years ago and written for Solaris, it is still a great resource.
HTH and good luck.
source to share
Completely untested idea:
# mv existinglib.so existinglib-real.so
# ld -o exlistinglib.so -shared newfuncs.o -lexistinglib-real
The dynamic linker, when loading a pending load program, existinglib.so
will find your version as well as the load existinglib-real.so
that it depends on. It doesn't quite accomplish the intended purpose of your question, but should look like it was loading a library.
source to share
The short answer is: exactly what you asked for cannot be done.
Longer answer: depending on why you want to do this, and how exactly existinglib.so
was linked, you might get closer to the desired behavior. In addition to LD_PRELOAD
and renaming existinglib.so
already mentioned, you can also use a linker script ( cat /lib/libc.so
to get a feel for what I mean).
source to share