LD script for symbol hidding in c++

扶醉桌前 提交于 2019-12-13 13:16:43

问题


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 global section will be visible, while everything remaining that matches the local section (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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!