我在Linux/Mac下编译了一个C++库,其中隐藏了符号。我对所有的类都使用了__attribute__((可见性(“隐藏”))
,并使用选项(-c-O2-fPIC-MMD-MP-MF
)进行编译。在Mac下,使用MacDependencies,这项工作做得很好,因为我只看到了我的导出(我实际上看到了前后的差异)。
然而,我注意到,使用nm
,我仍然可以看到所有符号的名称。这在Mac和Linux下都会发生。
为什么?有什么办法可以避免这种情况吗?
我在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
并不意味着只是就等于更改了一个标志,取消隐藏符号也不是一件小事。
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 ...
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
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 ...
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/...
I ve just installed Zend Studio 7.0.2 on my Linux-Ubuntu 9.10 system. There were no problems during the installation but when I try to create a new project, the New Project form hangs when I click ...
I am running valgrind as follows:- /usr/local/bin/valgrind "process_name" After excecution its giving me following error ==21731== ==21731== Warning: Can t execute setuid/setgid executable: ==...
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 ...
Is there anything other than DDD that will draw diagrams of my data structures like DDD does that runs on Linux? ddd is okay and runs, just kind of has an old klunky feeling to it, just wanted to ...