English 中文(简体)
使用g的符号可见性++
原标题:Symbol visibility using g++

我在Linux/Mac下编译了一个C++库,其中隐藏了符号。我对所有的类都使用了__attribute__((可见性(“隐藏”)),并使用选项(-c-O2-fPIC-MMD-MP-MF)进行编译。在Mac下,使用MacDependencies,这项工作做得很好,因为我只看到了我的导出(我实际上看到了前后的差异)。

然而,我注意到,使用nm,我仍然可以看到所有符号的名称。这在Mac和Linux下都会发生。

为什么?有什么办法可以避免这种情况吗?

最佳回答

无论是公开的还是隐藏的,符号仍然存在nm显示所有符号。不同之处在于,隐藏符号对动态链接器不可用,即无法导出和插入。

您可能还喜欢以下man gcc

   -fvisibility=default|internal|hidden|protected
       ...
       A good explanation of the benefits offered by ensuring ELF symbols
       have the correct visibility is given by "How To Write Shared
       Libraries" by Ulrich Drepper (which can be found at
       <http://people.redhat.com/~drepper/>)---however a superior solution
       made possible by this option to marking things hidden when the
       default is public is to make the default hidden and mark things
       public.  This is the norm with DLL s on Windows and with
       -fvisibility=hidden and "__attribute__ ((visibility("default")))"
       instead of "__declspec(dllexport)" you get almost identical
       semantics with identical syntax.  This is a great boon to those
       working with cross-platform projects.
问题回答

您可以strip您的二进制文件以删除任何不需要的符号。

在OSX上(不确定其他人),我发现了以下内容。

正如Maxim所提到的,使用-fvisibility=hidden__attribute__((visibility(“hidden”))仍然将符号放在符号表中,它只是被标记为未导出。最简单的方法是使用nm,例如:

$ nm libfoo.dylib 
...
0000000000001fa0 t __Z10a_functionv
0000000000002140 T __Z17a_public_functionv
...

如果地址后面的字母是小写字母,则表示它未导出。这里,a_function()是隐藏的,a_public_function具有默认可见性。

要从符号表中删除未导出的符号,可以使用strip-x,根据手册页:

-x删除所有局部符号(仅保存全局符号)。

$ strip -x libfoo.dylib
$ nm libfoo.dylib 
...
0000000000002140 T __Z17a_public_functionv
...

我相信(但不是100%确定)使用hidden并不意味着只是就等于更改了一个标志,取消隐藏符号也不是一件小事。





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签