如何查看.so文件的目标文件内容 [英] How to see the object file contents of a .so file

查看:2192
本文介绍了如何查看.so文件的目标文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查看.o文件由.so文件组成?

how to see what .o files constitute .so file?

表示如何注意到目标文件是用来从.so文件构建.so文件的(如果我只有.so文件的话)

Means how to notice what are the object files are used to build the .so file from the .so file (If I have only the .so file)

推荐答案

在仅提供了共享库的情况下,您无法知道 编译进去.如果幸运的话,您也许可以做出合理的猜测.

You can't know, given just a shared library, what object files were compiled into it. If you're lucky, you may be able to make a reasonable guess.

链接器通过目标文件和共享库创建共享库 可能还有其他共享库,但它不包含目标文件 或共享它的库.另一方面,一个 static 库 由存档器 ar 制成,确实包含目的 文件:它只是目标文件的ar存档.

A shared library is made, by the linker, from object files and possibly other shared libraries, but it does not contain the object files or shared libraries from which it was made. A static library, on the other hand, which is made by the archiver ar, does contain object files: it is just an ar archive of object files.

如果尚未除去共享库的调试信息,则 出于调试目的,其符号表将包含 source 文件的名称 共享库中链接的目标文件从这些文件中编译而成-至少是那些包含调试信息的源文件. 从这些源文件的名称中,您可以推断出目标文件的名称. 有合理的信心,但不确定.

If a shared library has not been stripped of debugging information, then for debugging purposes its symbol table will contain the names of the source files from which the object files were compiled that were linked in the shared library - at least those source files which were compiled with debugging information. From the names of those source files you can infer the names of the object files with reasonable confidence, but not with certainty.

例如,在这里,我们从源文件foo.cbar.c创建一个共享库.

For example, here we make a shared library from source files foo.c and bar.c.

将源文件编译为目标文件:

Compile the source files to object files:

$ gcc -Wall -fPIC -c -o foo.o foo.c
$ gcc -Wall -fPIC -c -o bar.o bar.c

链接目标文件以创建共享库:

Link the object files to make a shared library:

$ gcc -shared -o libfoobar.so foo.o bar.o

然后:

$ readelf -s libfoobar.so | grep FILE
26: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
35: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS foo.c
37: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS bar.c
39: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
42: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS 

表示三个源文件已将调试信息添加到 库,然后我们推断出它们被编译到的目标文件 可能是:

indicates that three source files have contributed debugging info to the library, and we'd infer that the object files to which they were compiled were likely to be:

crtstuff.o
foo.o
bar.o

请注意,crtstuff.c不是我们 编译的源文件之一.它 碰巧包含了C运行时库中的程序初始化和完成代码, 我们的库来自默认链接的C运行时对象文件.

Note that crtstuff.c is not one of the source files that we compiled. It happens to contain program initialization and finalization code from the C runtime library, which has got into our library from a C runtime object file that is linked by default.

对于任何文件,这种推断可能是错误的,因为:

This inference could be wrong about any of the files, since:

$ gcc -Wall -fPIC -c -o abc.o foo.c
$ gcc -Wall -fPIC -c -o xyz.o bar.c
$ gcc -shared -o libfoobar.so abc.o xyz.o

也是一种完美的编译和链接库的方式.

is also a perfectly possible way of compiling and linking the library.

如果已从库中剥离了调试信息:

If debugging information has been stripped from the library:

$ strip -g libfoobar.so

那我们就不走运了:

$ readelf -s libfoobar.so | grep FILE
$

没有更多的FILE符号.

这篇关于如何查看.so文件的目标文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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