2

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.

chown
  • 51,908
  • 16
  • 134
  • 170
samwise
  • 181
  • 2
  • 10
  • What are the compile flags you used to build the .SO when python complained? – tMC Jun 14 '11 at 16:39
  • SO worked fine, it was when I build an executable that things didn't work. For the exe I used: g++ -Wl,-rpath,. -Wl,-rpath-link,build/debug/final -o build/debug/final/dummy build/debug/intermediate/dummy/projects/dummy/main.o – samwise Jun 14 '11 at 16:51
  • if you add the `-shared` arg, is it no longer executable? – tMC Jun 14 '11 at 16:58

2 Answers2

3

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.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • Thanks. Yes, I could move all of my code to a shared object. I am building tools for a team though so I would rather not force that on all my programmers. – samwise Jun 14 '11 at 16:53
1

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).

Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
  • 2
    Thanks! that was a great pointer. In addition to -pie I also had to link with -Wl,--export-dynamic. Worked wonderfully. – samwise Jun 14 '11 at 18:38
  • @samwise: you can also pass `-rdynamic` to the compiler driver, if `-Wl,--export-dynamic` feels too verbose... – ninjalj Jun 14 '11 at 18:54
  • @samwise: or, if you don't want to export all symbols from the executable, use `--dynamic-list` as explained in: http://stackoverflow.com/questions/6292473/how-to-call-function-in-executable-from-my-library/6298434#6298434 – ninjalj Jun 14 '11 at 18:56