Search code examples
c++linkervisibilitydynamic-library

c++: runtime-linking shared object with host app, symbol table issue


I have a hostapp.cpp that loads a object.so shared object at run-time, the shared object is compiled using only with the needed .h files from the host app but at run-time it needs to access those functions (present at the host app).

Compiling the host app with -rdynamic apparently solves this issue but it unnecessarily exposes the object to the full symbol table of the host app, even though it only needs to resolve a few of them.

How can I specify exactly what host-app symbols will be known by the shared object?

Edit: I'm building and running on GNU/Linux with the GNU toolchain.


Solution

  • Your question is under-specified: you never said what platform you are building for, what linker you use, etc.

    Assuming you build for Linux, you can specify symbols to export from the main executable using one of the following methods:

    1. If you are using gold (the GNU ELF linker), --export-dynamic-symbol will do what you need.
    2. If you are using binutils linker, you can use linker version script to do the same (example).
    3. You can mark symbols to be exported with __attribute__((visibility("default"))), compile with -fvisibility-hidden, and link with -rdynamic. That should hide most of the symbols, but will not work well if you link in libraries which you can't recompile.