Nm Command
List Symbol Names in Object Files
The nm command is a powerful utility for listing
symbols from object files. It's essential for debugging and
understanding the structure of compiled code. This tool helps
developers inspect symbol tables, which contain information about
functions, variables, and other identifiers within executable files,
object files, and libraries.
Common Nm Command Usages
Here are some of the most frequent ways to use the
nm command:
# nm
# List symbol names in object files
# Demangle C++ symbols (make them readable):
nm --demangle file.o
# Display object files that refer to a symbol
nm -A ./*.o | grep func
# Display all undefined symbols in a file
nm -u executable
# List all symbols, even debugging symbols:
nm -a executable
# Display all symbols in executable
# (sorted in order of their addresses)
nm -n executable
# Search for a symbols and display its size
nm -S 1 | grep abc
# Display dynamic symbols in executable
nm -D executable
# Change the format of the nm output
# (display the output of nm command in POSIX style)
nm -u -f posix executable
# Display only the external symbols of executable
nm -g executable
# Sort the nm output by the symbol size
nm -g --size-sort executable
# Specify nm options in a file
nm @nm_file
Understanding Nm Command Options
The nm command offers a variety of options to customize
its output. For instance, --demangle is crucial for
making C++ symbols readable, while -u specifically
lists undefined symbols, which is often helpful in identifying
linking errors. The -a flag includes debugging symbols,
providing a more comprehensive view of the file's contents. Sorting
options like -n (by address) and
--size-sort allow for structured analysis of symbol
information.
Advanced Nm Command Techniques
For more complex scenarios, nm can be combined with
other command-line tools. Piping the output to grep, as
shown in the example nm -A ./*.o | grep func, allows
you to quickly find specific symbols across multiple files.
Displaying dynamic symbols with -D is particularly
useful when working with shared libraries. Furthermore, using an
options file with @nm_file can streamline repetitive
command executions.
External Resources for Nm Command
- Linux Man Pages: nm(1) - Official documentation for the nm command.
- Stack Overflow: nm tag - Community discussions and solutions related to the nm command.
- Wikipedia: Symbol Table - Learn about the underlying concept of symbol tables.