Linux nm Command

Linux 命令大全Linux Command Reference


nmIt is an important command-line tool in Linux systems, with the full name "Name List". It is used to display symbol information in binary object files (such as library files and executables), and is a common tool for programmers and for analyzing binary files.

Main Features

  • List symbols (functions, variables, etc.) in object files
  • Display symbol types and attributes
  • Help analyze program linking issues
  • Assist in debugging and reverse engineering

Basic Syntax

nm [选项] 文件名

Common Option Parameters

Option Description
-a Display all symbols, including debug symbols
-g Display only external (global) symbols
-u Display only undefined symbols
-D Display dynamic symbols (for shared libraries)
-C Demangle C++ symbol names
-l Display line numbers where symbols are located (requires debug information)
-S Display symbol sizes
-t Specify output format (d-decimal, o-octal, x-hexadecimal)
--size-sort Sort by symbol size
--defined-only Display only defined symbols

Symbol Type Description

nmThe symbol type in the output is represented by a letter; common ones are:

Type Description
A Absolute symbol, not changed at link time
B/b Symbol in uninitialized data segment (BSS segment)
D/d Symbol in initialized data segment
T/t Symbol in 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 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 (demangled names)

nm -C my_program

Example output:

0000000000000000 T main
0000000000000000 T std::cout
0000000000000000 T std::basic_ostream<char, std::char_traits >& std::operator<< <std::char_traits >(std::basic_ostream<char, std::char_traits >&, 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 Use Cases

1. Resolving link errors

When encountering an "undefined reference" error, you can usenmCheck which symbols are undefined:

nm -u my_program.o

2. Analyzing library file contents

View symbols exported by a shared library:

nm -D libexample.so

3. Comparing two versions of a binary file

Example

nm old_version > old.txt
nm new_version > new.txt
diff old.txt new.txt

4. Finding specific symbols

nm my_program | grep "main"

Notes

  1. For stripped binary files,nmuseful information may not be displayed
  2. Binary files of different architectures may require using the one from the cross-compilation toolchainnm
  3. Dynamic symbols (in shared libraries) need to be viewed using the-Doption to view
  4. For C++ programs, it is recommended to always use the-Coption to demangle symbol names

Advanced Tips

Combining 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

Writing scripts to analyze symbols

Example

#!/bin/bash
# Count symbol type distribution
nm $1 | awk '{print $2}' | sort | uniq -c | sort -nr

Creating symbol mapping files

nm -n my_program > symbol_map.txt

Summary

nmThe command is one of the important tools for Linux developers. Mastering it allows you to:

  • Better understand program structure
  • Quickly locate linking issues
  • Analyze the content 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 thenmcore functionality of the command. In actual work, you can flexibly use various options and parameters according to specific needs.


Linux 命令大全Linux Command Reference

Other Extensions