问题
I would like to use an GNU LD version script to hide unwanted symbols in c++ shared library. Say my header file looks like this:
int a();
int a(int);
class B {
B(){}
~B(){}
int x(int);
};
std::ostream& operator<< (std::ostream& out, const B& b );
I would like to hide everything which is not stated in the header file.
How would a version script for this look like?
回答1:
Something like this should do the trick:
{
global:
extern "C++" {
"a()";
"a(int)";
B::*;
"operator<<(std::ostream&, B const&)";
};
local:
*;
};
If you saved this file as foo.map, pass -Wl,--version-script,foo.map as an argument to the linker. A quick rundown of the syntax:
Since we didn't specify a version label at the top level of the script, the symbols in the library won't have versions attached: the effect of the script is simply to pick which symbols are visible.
Everything matched by the
globalsection will be visible, while everything remaining that matches thelocalsection (in this case, the glob*) will be hidden.The
extern "C++" { ... };block says the linker should demangle symbols according to the C++ ABI before trying to match against the enclosed patterns.Patterns in quotes are matched directly, while unquoted patterns are treated as glob patterns.
More details of the version script file format can be found here: https://sourceware.org/binutils/docs/ld/VERSION.html
来源:https://stackoverflow.com/questions/8432431/ld-script-for-symbol-hidding-in-c