Linux nm 命令使用及含义笔记

很多时候我们并不首要关注库本身的实现,或者根本无法看到其底层逻辑,但又必须确认某些函数或变量的命名(如排查定义冲突)问题,这时候就需要用到一个很基础但是很有用的工具“nm”了。

1 nm 命令介绍

NAME
       nm - list symbols from object files

SYNOPSIS
       nm [-A|-o|--print-file-name] [-a|--debug-syms]
          [-B|--format=bsd] [-C|--demangle[=style]]
          [-D|--dynamic] [-fformat|--format=format]
          [-g|--extern-only] [-h|--help]
          [-l|--line-numbers] [-n|-v|--numeric-sort]
          [-P|--portability] [-p|--no-sort]
          [-r|--reverse-sort] [-S|--print-size]
          [-s|--print-armap] [-t radix|--radix=radix]
          [-u|--undefined-only] [-V|--version]
          [-X 32_64] [--defined-only] [--no-demangle]
          [--plugin name] [--size-sort] [--special-syms]
          [--synthetic] [--target=bfdname]
          [objfile...]

DESCRIPTION
       GNU nm lists the symbols from object files objfile....  If no object
       files are listed as arguments, nm assumes the file a.out.

       For each symbol, nm shows:
        # ... run man nm for detials.

简单说的话,就是可以帮你列举出该目标中定义的符合要求的符号。要求可以很多,主要通过参数实现:外部引入的、内部定义的、动态的… 也可以添加参数使nm同时打印行号、文件名等相关信息。

2 nm 结果含义

nm 将找到的符号值使用十六进制缺省表示,并在函数前添加其类型,类型主要有:

ValueDescripitionNote
AThe symbol’s value is absolute, and will not be changed by further linking.符号绝对,链接过程不会改变
B/bThe symbol is in the uninitialized data section (known as BSS).非初始化符号
CThe symbol is common.公有符号,链接时会被同名符号覆盖
D/dThe symbol is in the initialized data section.初始化符号
G/gThe symbol is in an initialized data section for small objects.初始化符号,面向小数据访问优化
IThe symbol is an indirect reference to another symbol.其它符号的间接引用
NThe symbol is a debugging symbol.调试符号
PThe symbols is in a stack unwind section.栈区符号(清空)
R/rThe symbol is in a read only data section.符号只读
S/sThe symbol is in an uninitialized data section for small objects.非初始化符号,面向小数据访问优化
T/tThe symbol is in the text (code) section.代码区符号
UThe symbol is undefined.未定义或在外部定义的符号
uThe symbol is a unique global symbol.全局唯一,GNU保留符
V/vThe symbol is a weak object.弱定义符(详见C++强弱符号定义)
W/wThe symbol is a weak symbol that has not been specifically tagged as a weak object symbol.emm…绕口令符号
The symbol is a stabs symbol in an a.out object file.stabs格式符号
?The symbol type is unknown, or object file format specific.NM也不认识的符号

简单的举个栗子:

rew@rew:/usr/lib64$ nm libpthread.a

nptl-init.o:
                 U __default_pthread_attr
                 U __default_pthread_attr_lock
                 U _dl_cpuclock_offset
                 U _dl_get_tls_static_info
                 U _dl_init_static_tls
                 U _dl_pagesize
                 U _dl_wait_lookup_done
                 U __fork_generation
                 U __getrlimit
                 U __is_smp
                 U __libc_fatal
0000000000000008 C __libc_multiple_threads_ptr
                 U __libc_pthread_init
                 U __libc_setup_tls
                 U __libc_sigaction
                 U __libc_stack_end
                 U __lll_lock_wait_private
                 U __lll_unlock_wake_private
0000000000000000 b __nptl_initial_report_events
00000000000001b0 T __nptl_set_robust
                 U __nptl_setxid_error
0000000000000000 r nptl_version
00000000000004b0 T __pthread_get_minstack
00000000000001d0 T __pthread_initialize_minimal
00000000000001d0 T __pthread_initialize_minimal_internal

有了nm的分析结果搭配上符号定义的解释,就可以很愉快的找到想要的符号啦!

    原文作者:hwrenx
    原文地址: https://www.jianshu.com/p/a86bd1b8e4a5
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞