如何从Android NDK .so文件中删除符号? [英] How to strip symbols from Android NDK .so file?

查看:665
本文介绍了如何从Android NDK .so文件中删除符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Android .so本机代码库中删除符号?

How do you strip symbols from an Android .so native code library?

我建立了一个.so,它具有成千上万个在十六进制编辑器中清晰可见的符号. IDA Pro会根据可执行文件中的符号自动使用正确的符号进行反汇编.

I have a .so built that has thousands of symbols clearly visible in a hex editor. IDA Pro automatically disassembles with proper symbols based on the ones in the executable.

但是,如果我要求nm转储符号表,它说没有. stripobjcopy也无效.

However, if I ask nm to dump the symbol table, it says there are none. strip and objcopy also have no effect.

C:\AndroidProject.apk\lib\armeabi-v7a>arm-linux-androideabi-strings.exe libMeow.so | findstr _ZN11SecretClass14SecretFunctionERKS_
_ZN11SecretClass14SecretFunctionERKS_

C:\AndroidProject.apk\lib\armeabi-v7a>arm-linux-androideabi-nm.exe libMeow.so
arm-linux-androideabi-nm.exe: libMeow.so: no symbols

C:\AndroidProject.apk\lib\armeabi-v7a>copy /y libMeow.so libMeow-test.so
        1 file(s) copied.

C:\AndroidProject.apk\lib\armeabi-v7a>sha1sum libMeow.so libMeow-test.so
0a36701ba44b4cfb31e6f6506349493d5466cd70 *libMeow.so
0a36701ba44b4cfb31e6f6506349493d5466cd70 *libMeow-test.so

C:\AndroidProject.apk\lib\armeabi-v7a>arm-linux-androideabi-strip.exe libMeow-test.so

C:\AndroidProject.apk\lib\armeabi-v7a>sha1sum libMeow.so libMeow-test.so
0a36701ba44b4cfb31e6f6506349493d5466cd70 *libMeow.so
0a36701ba44b4cfb31e6f6506349493d5466cd70 *libMeow-test.so

C:\AndroidProject.apk\lib\armeabi-v7a>arm-linux-androideabi-strip.exe -g libMeow-test.so

C:\AndroidProject.apk\lib\armeabi-v7a>sha1sum libMeow.so libMeow-test.so
0a36701ba44b4cfb31e6f6506349493d5466cd70 *libMeow.so
0a36701ba44b4cfb31e6f6506349493d5466cd70 *libMeow-test.so

名称已更改,以保护罪行.

Names have been changed to protect the guilty.

推荐答案

由于.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: *; };

这篇关于如何从Android NDK .so文件中删除符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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