且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Android NDK .so文件中删除符号?

更新时间:2023-02-02 21:22:30

由于.so是将动态加载的共享库,因此它需要在外部具有一定数量的符号.要查看这些内容,请使用nm -D libMeow.so. Strip不会删除它们,否则会使该库不可用.

Since the .so is a shared library that will be loaded dynamically, it needs to have some amount of symbols available externally. To view these, use nm -D libMeow.so. Strip won't remove these, or it would make the library unusable.

由于某些功能需要从外部加载,因此您不能仅删除所有动态符号,因为这样没人可以与.so进行接口.如果.so是JNI库,则需要使JNI入口点函数在外部可见,而如果它是另一个.so链接到的共享库,则至少需要使库的公共接口可见

Since the some functions need to be loaded externally, you can't just remove all dynamic symbols, because then nobody would be able to interface with the .so. If your .so is a JNI library, you need to have the JNI entry point functions visible externally, while if it is a shared library that another .so links against, you need to have at least the public interface of your library visible.

要隐藏内部符号,您可以阅读 https://gcc.gnu.org/wiki/可见性.大致来说,您的选择是:

To make the internal symbols hidden, you can read https://gcc.gnu.org/wiki/Visibility for the full story. Roughly, your options are:

  • 对不想在库外显示的每个符号使用__attribute__ ((visibility ("hidden"))). (这可能是很多,要追踪每个人,需要做很多工作.)
  • 使用-fvisibility=hidden构建,这会在每个外部符号上进行隐式设置,并在实际需要导出的符号上添加__attribute__ ((visibility ("default")))(可能要少得多)
  • 使用版本脚本"来限制要导出到选择列表的功能.链接时,传递-Wl,-version-script -Wl,mylib.ver.
  • Use __attribute__ ((visibility ("hidden"))) on every symbol you don't want to be visible outside of the library. (This probably is quite a few and it's a lot of work to track down every single one.)
  • Build with -fvisibility=hidden, which implicitly sets this on every single external symbol, and add __attribute__ ((visibility ("default"))) on the ones that you actually need to have exported (probably much fewer)
  • Use a "version script" to limit what functions to export to a select list. When linking, pass -Wl,-version-script -Wl,mylib.ver.

对于版本脚本,mylib.ver应该看起来像这样:

For the version script case, mylib.ver should look like this:

{ global:
PublicFunction1;
PublicFunction2;
local: *; };