samwise
samwise

Reputation: 181

Can I dynamically load an executable on linux?

I wrote a little testing framework that uses 'nm' to inspect shared libraries and look for test functions. I then use Python's ctypes library to dynamically load the shared object and execute the test functions. Is there a way to do this with an executable? When I tried the same trick on an executable module Python reported that it could not dynamically load an executable.

Upvotes: 2

Views: 6440

Answers (2)

Andre Holzner
Andre Holzner

Reputation: 18695

Try linking the executable with the -pie option (if you have the possibility to do so).

(found this option on this feature request for adding support to dlopen an executable -- dlopen is what is used to load a shared object).

Upvotes: 1

Ben Jackson
Ben Jackson

Reputation: 93880

If this is your own application you could rearrange the build so your executable is only main() { real_main(); } and real_main() is in libapp.so. Then you could test libapp.so with your existing code.

If it's possible to load another executable it probably involves loading ld.so and getting it to do the work. If you run /lib/ld-linux.so (on Linux) it will print a stanza with information.

Upvotes: 3

Related Questions