How to relink existing shared library with extra object file

I have existing Linux shared object file (shared library) which has been stripped. I want to produce a new version of the library with some additional functions included. I had hoped that something like the following would work, but does not:

ld -o newfuncs.o --whole-archive

I do not have the source to the existing library. I could get it but getting a full build environment with the necessary dependencies in place would be a lot of effort for what seems like a simple problem.


You might like to try coming at this from a slightly different angle by loading your object using preloading.

Set LD_PRELOAD to point to your new object

export LD_PRELOAD=/my/newfuncs/dir/newfuncs.o

and specify the existing library in the same way via your LD_LIBRARY_PATH.

This will then instruct the run time linker to search for needed symbols in your object before looking in objects located in your LD_LIBRARY_PATH.

BTW You can put calls in your object to then call the function that would've been called if you hadn't specified an LD_PRELOAD object or objects. This is why this is sometimes called interposing.

This is how many memory allocation analysis tools work. They interpose versions of malloc() and free() that records the calls to alloc() and free() before then calling the actual system alloc and free functions to perform the memory management.

There's plenty of tutorials on the interwebs on using LD_PRELOAD. One of the original and best is still "Building library interposers for fun and profit". Though written nine years ago and written for Solaris it is still an excellent resource.

HTH and good luck.

Completely untested idea:

# mv
# ld -o -shared newfuncs.o -lexistinglib-real

The dynamic linker, when loading a program that expects to load, will find your version, and also load on which it depends. It doesn't quite achieve the stated goal of your question, but should look as if it does to the program loading the library.

Shared libraries are not archives, they are really more like executables. You cannot therefore just stuff extra content into them in the same way you could for a .a static library.

Short answer: exactly what you asked for can't be done.

Longer answer: depending on why you want to do this, and exactly how has been linked, you may get close to desired behavior. In addition to LD_PRELOAD and renaming already mentioned, you could also use a linker script (cat /lib/ to see what I mean).

Need Your Help

how to filter and limit dynamodb data with lambda and api gateway?

amazon-web-services aws-lambda amazon-dynamodb aws-api-gateway

I am using amazon dynamodb, lambda and api gateway services. I have a table in dynamodb call photo-group and it's Primary key is pid and other data such ownerid, photosrc,photosize etc.