Linux nm command
nm is an important command-line tool in Linux, short for "Name List". It is used to display symbol information in binary object files (such as library files and executable files) and is a common tool for programmers analyzing binaries.
Main Function
- List the symbols in an object file (functions, variables, etc.)
- Display the type and attributes of symbols
- Help analyze program linking problems
- Assist in debugging and reverse engineering
Basic Syntax
nm [选项] 文件名
Description of common options and parameters
| Option | Description |
|---|---|
-a |
Display all symbols, including debugging symbols |
-g |
Display only external (global) symbols |
-u |
Display only undefined symbols |
-D |
Display dynamic symbols (for shared libraries) |
-C |
Decode (demangle) C++ symbol names |
-l |
Display the line numbers where symbols are located (requires debug information) |
-S |
Display symbol sizes |
-t |
Specify the output format (d for decimal, o for octal, x for hexadecimal) |
--size-sort |
Sort by symbol size |
--defined-only |
Display only defined symbols |
Symbol type descriptions
nm The symbol types in the output are represented by a single letter; common ones are:
| Type | Description |
|---|---|
| A | Absolute symbol, not changed at link time |
| B/b | Symbol in the uninitialized data segment (BSS segment) |
| D/d | Symbol in the initialized data segment |
| T/t | Symbol in the code segment (T for global, t for local) |
| U | Undefined symbol (needs to be linked from other files) |
| W/w | Weak symbol |
| R/r | Symbol in the read-only data segment |
| C | Common symbol |
| I | Indirect reference to another symbol |
Practical Application Examples
Example 1: View the symbol table of an executable file
nm /bin/ls
Example output:
0000000000000000 A _IO_stdin_used
0000000000000000 R _fp_hw
0000000000000000 T _init
0000000000000000 W _ITM_deregisterTMCloneTable
0000000000000000 W _ITM_registerTMCloneTable
0000000000000000 W __cxa_finalize
0000000000000000 W __gmon_start__
0000000000000000 T __libc_csu_fini
0000000000000000 T __libc_csu_init
...
Example 2: View only undefined symbols
nm -u /bin/ls
Example output:
U __ctype_toupper_loc
U __errno_location
U __overflow
U __stack_chk_fail
U __strtoul_internal
U _obstack_begin
U _obstack_newchunk
U abort
U access
...
Example 3: View symbols of a C++ program (decode names)
nm -C my_program
Example output:
0000000000000000 T main
0000000000000000 T std::cout
0000000000000000 T std::basic_ostream& std::operator<< (std::basic_ostream&, char const*)
...
Example 4: View symbol sizes and sort by size
nm -S --size-sort my_library.so
Example output:
0000000000000001 0000000000000001 T func1
0000000000000002 0000000000000002 T func2
0000000000000004 0000000000000004 D global_var
0000000000000008 0000000000000008 B large_buffer
...
Common usage scenarios
1. Resolve linking errors
When you encounter an "undefined reference" error, you can use nm to check which symbols are undefined:
nm -u my_program.o
2. Analyze the contents of a library file
View the symbols exported by a shared library:
nm -D libexample.so
3. Compare two versions of a binary file
Example
nm
old_version
&
gt; old.txt
nm
new_version
&
gt; new.txt
diff
old.txt new.txt
4. Find a specific symbol
nm my_program | grep "main"
Notes
- For binaries that have been stripped,
nmmay not be able to display useful information - Binaries of different architectures may require using the toolchain from
nm - Dynamic symbols (in shared libraries) require the use of
-Dan option to view them - For C++ programs, it is recommended to always use the
-Coption to decode symbol names
Advanced Tips
Use in combination with other tools
Example
Use objdump to view more detailed symbol information
objdump
-t
my_program
Use readelf to view ELF file header information
readelf
-s
my_program
Write scripts to analyze symbols
Example
#!/bin/bash
Count the distribution of symbol types
nm
$1
|
awk
'{print $2}'
|
sort
|
uniq
-c
|
sort
-nr
Create a symbol map file
nm -n my_program > symbol_map.txt
Summary
nm is one of the important tools for Linux developers; mastering it allows you to:
- Better understand program structure
- Quickly locate linking problems
- Analyze the contents of third-party libraries
- Assist in debugging and reverse engineering
Through the basic usage and practical examples introduced in this article, you should have mastered the nm core functionality of the command. In actual work, you can flexibly use various options and parameters according to specific needs.
Linux Command Reference