用于C ++中符号隐藏的LD脚本 [英] LD script for symbol hidding in c++

查看:104
本文介绍了用于C ++中符号隐藏的LD脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用GNU LD版本脚本来隐藏c ++共享库中不需要的符号.说我的头文件看起来像这样:

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?

推荐答案

这种方法应该可以解决问题:

Something like this should do the trick:

{
global:
    extern "C++" {
        "a()";
        "a(int)";
        B::*;
        "operator<<(std::ostream&, B const&)";
    };
local:
    *;
};

如果将此文件另存为foo.map,请将-Wl,--version-script,foo.map作为参数传递给链接器.语法的简要介绍:

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.

global部分匹配的所有内容都是可见的,而与local部分匹配的其余所有内容(在本例中为glob *)都将被隐藏.

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.

extern "C++" { ... };块表示,链接器应在尝试与包含的模式匹配之前,根据C ++ ABI对符号进行脱粒.

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.

可以在此处找到版本脚本文件格式的更多详细信息: https://sourceware .org/binutils/docs/ld/VERSION.html

More details of the version script file format can be found here: https://sourceware.org/binutils/docs/ld/VERSION.html

这篇关于用于C ++中符号隐藏的LD脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆