Edvard Fagerholm
Edvard Fagerholm

Reputation: 862

"Adding" functions to a shared object?

I have a closed sourced application that includes an interpreter that dynamically loads symbols from a shared object, libclosedsource.so, which is closed source. I would like to add a few API extensions of my own. Now the application in question loads the file using dlopen, so I can basically replace libclosedsource.so with whatever library I want. However, that library should contain a superset of the symbols exported by the original.

My question: Is it possible to rename the original shared object to say libclosedsource-orig.so and create my own libclosedsource.so, so that upon loading libclosedsource.so the application will be able to see the symbols exported in libclosedsource-orig.so?

My assumption is somehow that the answer, unfortunately, is no, since symbol loading doesn't seem to be transitive. I'm open to any other suggestions for accomplishing this.

EDIT: The original library contains about 300 functions.

Upvotes: 1

Views: 516

Answers (2)

yugr
yugr

Reputation: 21955

To add to SoronelHaetir answer, wrapping calls to original library can be automated via Implib.so:

$ implib-gen.py libclosedsource-orig.so
$ gcc new-code.c libclosedsource-orig.tramp.S libclosedsource-orig.init.c -shared -fPIC -o libclosedsource.so

Note that you don't need headers of libclosedsource-orig.so, just the binary itself. libclosedsource.so will load libclosedsource-orig.so at startup and redirect calls to original APIs to it.

Upvotes: 1

SoronelHaetir
SoronelHaetir

Reputation: 15172

If you know the signatures of all the exported functions this would be easy though fairly tedious, just re-implement all the functions and pass them through to calls to the original library.

Also, take a look at: https://www.codeproject.com/Articles/70302/Redirecting-functions-in-shared-ELF-libraries

Upvotes: 1

Related Questions