C++ 有没有办法在不加载的情况下读取 .so 文件的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19396048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:46:26  来源:igfitidea点击:

Is there a way to read the contents of .so file without loading it?

c++shared-objectsshared-libraries

提问by Guruji

Is there any way to read the content of a .SO(shared objects) file without loading it?

有没有办法在不加载的情况下读取 .SO(共享对象)文件的内容?

My use-case scenario is:

我的用例场景是:

  1. I have a .so file on windows. I need to query for some methods whether they are present in .so or not.
  2. To know all the classes in a .so file.
  3. Given a class name need to find all the methods on this class.
  1. 我在 Windows 上有一个 .so 文件。我需要查询某些方法是否存在于 .so 中。
  2. 了解 .so 文件中的所有类。
  3. 给定一个类名需要找到这个类上的所有方法。

Note: I can easily do these things on DLL. Also I am working on windows so I can't load a .SO file.

注意:我可以很容易地在 DLL 上做这些事情。此外,我正在 Windows 上工作,因此无法加载 .SO 文件。

Thanks

谢谢

回答by TCAllen07

To screen: readelf -a <file>

向屏幕: readelf -a <file>

To save the output, dump it into a file. For example, I'm learning about the Python RPi.GPIO module on Raspberry Pi, which is stored in /usr/lib/python2.7/dist-packages/RPi, so I run: readelf -a GPIO.so > ~/gpio.so.out

要保存输出,请将其转储到文件中。例如,我正在学习Raspberry Pi上的Python RPi.GPIO模块,该模块存储在/usr/lib/python2.7/dist-packages/RPi中,因此我运行: readelf -a GPIO.so > ~/gpio.so.out

回答by ypnos

You can read these files with tools included in the GNU binutils. While GNU binutils is typically installed on Linux systems, this is not the case for Windows. However, they run within Cygwin or minGW under Windows.

您可以使用 GNU binutils 中包含的工具读取这些文件。虽然 GNU binutils 通常安装在 Linux 系统上,但 Windows 并非如此。但是,它们在 Windows 下的 Cygwin 或 minGW 中运行。

Resources:

资源:

  1. Binutils: http://www.gnu.org/software/binutils/
  2. Cygwin: http://www.cygwin.com/
  3. MinGW: http://www.mingw.org/
  4. MinGW Binutils: http://sourceforge.net/projects/mingw/files/MinGW/Base/binutils/
  1. Binutils:http: //www.gnu.org/software/binutils/
  2. Cygwin:http: //www.cygwin.com/
  3. MinGW:http: //www.mingw.org/
  4. MinGW Binutils:http: //sourceforge.net/projects/mingw/files/MinGW/Base/binutils/

Note that with MinGW (3.), you do not need Cygwin (2.) and compile Binutils (1.) yourself. Binutils is included in MinGW (3.), but you can also try to download just the Binutils part of MinGW (4.).

请注意,使用 MinGW (3.),您不需要 Cygwin (2.) 并自己编译 Binutils (1.)。Binutils 包含在 MinGW (3.) 中,但您也可以尝试仅下载 MinGW (4.) 的 Binutils 部分。

How to use nmand readelfto obtain the information is explained here: How do I list the symbols in a .so file

此处解释了如何使用nmreadelf获取信息: How do I list the symbols in a .so file

If you want to include this functionality into a C++ program, you could either incorporate the source code of these tools (beware of the license!) or call them from within your program. Probably you will need a Cygwin environment to get the source code compile under Windows.

如果您想将此功能包含到 C++ 程序中,您可以合并这些工具的源代码(注意许可证!)或从您的程序中调用它们。可能您需要一个 Cygwin 环境才能在 Windows 下编译源代码。

回答by Guruji

yes needed solution for windows OS. I used the nm.exe tool present in ndk tools in android sdk/ndk.

是的,需要 Windows 操作系统的解决方案。我使用了 android sdk/ndk 中 ndk 工具中的 nm.exe 工具。

i dumped the outputof nm.exe in some file and then using regular expression extracted out all the classes and methods from it.

我将 nm.exe 的输出转储到某个文件中,然后使用正则表达式从中提取出所有类和方法。