我如何查看 Linux .so 或 .a 对象并查看它们包含哪些功能?

How can I look inside a Linux .so or .a object and see what functions they contain?

链接器大概可以做到这一点,那么有没有命令行工具可以列出目标文件中的函数并告诉我函数的名称及其签名?

你可以nm Linux.so,它会显示 .so 文件中的函数和变量。

对于共享库,您必须使用:

nm -D /path/to/libwhatever.so.<num>

没有 -Dnm 转储 debug 符号; -D指的是实际用于动态链接的动态符号。来自 Ubuntu 12 个会话:

$ nm /lib/i386-linux-gnu/libc.so.6 
nm: /lib/i386-linux-gnu/libc.so.6: no symbols
$ nm -D /lib/i386-linux-gnu/libc.so.6 | tail
0011fc20 T xdr_wrapstring
001202c0 T xdrmem_create
00115540 T xdrrec_create
001157f0 T xdrrec_endofrecord
00115740 T xdrrec_eof
00115690 T xdrrec_skiprecord
00120980 T xdrstdio_create
00120c70 T xencrypt
0011d330 T xprt_register
0011d450 T xprt_unregister

在这个系统上 libc.so 没有调试符号,所以 nm 什么也没有显示;但当然还有 nm -D.

揭示的动态链接机制的符号

对于 .a 存档或 .o 目标文件,只需 nm。符号就是符号;如果这些文件被剥离,这些对象不能用于链接。

this similar question 所述:

Exported sumbols are indicated by a T. Required symbols that must be loaded from other shared objects have a U. Note that the symbol table does not include just functions, but exported variables as well.

Or if you only want to see exported symbols, add the --defined-only flag. eg: nm -D --defined-only /lib/libtest.so