The nm
command is used to display the symbol table of a binary object file. It is a tool for examining the contents of object files, archives, and shared libraries. The symbol table contains information about the symbols defined in the binary object file, such as function and variable names, their types, and their addresses. The nm
command is commonly used by developers to debug and analyze binary files.
Overview
The basic syntax of the nm
command is as follows:
nm [options] file
Here, options
are the various command-line options that can be used with the nm
command, and file
is the binary object file whose symbol table is to be displayed.
Examples
- To display the symbol table of a binary object file named
myprogram
, run the following command:
nm myprogram
This will display a list of symbols defined in the myprogram
file, along with their addresses and types.
- To display only the names of the symbols defined in the binary object file, use the
-o
option:
nm -o myprogram
- To display the symbol table in reverse order (i.e., starting from the end of the file), use the
-r
option:
nm -r myprogram
- To display the symbol table in a specific format, use the
-f
option followed by the format specifier. For example, to display the symbol table in the BSD format, run the following command:
nm -f bsd myprogram
Specific Use Cases
The nm
command is commonly used for the following purposes:
- Debugging: Developers use the
nm
command to analyze the symbol table of a binary object file to debug issues related to linking, symbol resolution, and function calls. - Reverse Engineering: Security researchers and hackers use the
nm
command to analyze the symbol table of a binary object file to identify function names, library dependencies, and other information that can be used to reverse engineer the binary. - Binary Analysis: Malware analysts use the
nm
command to analyze the symbol table of a binary object file to identify suspicious function names, library dependencies, and other information that can be used to analyze the behavior of the binary.
Options
The following table lists the available options for the nm
command:
Option | Description |
---|---|
-a | Display all symbols, including local symbols |
-A | Write the pathname or library name of an object on each line |
-C | Demangle C++ symbols |
-D | Display dynamic symbols |
-f format | Specify the output format (e.g., bsd, posix) |
-g | Display only global symbols |
-l | Display line numbers and filenames |
-n | Sort symbols by address, rather than by name |
-o | Display only symbol names |
-P | Write the name of each symbol’s containing object file |
-r | Reverse the order of the sort |
-S | Display size of symbols |
-t | Display only symbol names and types |
-u | Display undefined symbols |
-v | Display version information |
–defined-only | Display only defined symbols |
–no-sort | Do not sort the symbols |
–print-file-name | Write the pathname of an object on each line |
–synthetic | Display synthetic symbols |
Troubleshooting Tips
- If the
nm
command fails to display the symbol table of a binary object file, ensure that the binary is not corrupted or malformed. - If the
nm
command displays a large number of undefined symbols, ensure that all required libraries and object files are linked correctly. - If the
nm
command displays unexpected symbol names or addresses, ensure that the binary was compiled with the correct options and settings.
Notes
- The
nm
command is a powerful tool for analyzing binary object files, but it requires a good understanding of the binary file format and the symbol table structure. - The output of the
nm
command can be piped to other commands (e.g.,grep
,awk
) to filter and manipulate the output. - The
nm
command is available on most Unix-like operating systems, including Linux, macOS, and FreeBSD.