Symbol visibility using g++

馋奶兔 提交于 2019-11-30 13:53:35

Public or hidden, symbols are still there. nm shows all symbols. The difference is that hidden symbols are not available to the dynamic linker, i.e. not exported and can not be interposed.

You might also like the following 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.

You can strip your binary to remove any unneeded symbols.

On OSX (not sure about others) I found the following.

As mentioned by Maxim, using -fvisibility=hidden or __attribute__((visibility("hidden"))) still puts the symbol in the symbol table, it just gets marked as unexported. The easiest way to see this is with nm, e.g:

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

If the letter after the address is lowercase it means it isn't exported. Here a_function() is hidden, and a_public_function has default visibility.

To strip the non-exported symbols from the symbol table you can use strip -x, which according to the man page:

-x Remove all local symbols (saving only the global symbols).

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

I believe (but am not 100% sure) that using hidden doesn't just amount to changing a flag, and 'unhiding' the symbols wouldn't be trivial.

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