nm – Display the Symbol Table of a Binary Object File

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

  1. 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.

  1. To display only the names of the symbols defined in the binary object file, use the -o option:
nm -o myprogram
  1. To display the symbol table in reverse order (i.e., starting from the end of the file), use the -r option:
nm -r myprogram
  1. 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:

OptionDescription
-aDisplay all symbols, including local symbols
-AWrite the pathname or library name of an object on each line
-CDemangle C++ symbols
-DDisplay dynamic symbols
-f formatSpecify the output format (e.g., bsd, posix)
-gDisplay only global symbols
-lDisplay line numbers and filenames
-nSort symbols by address, rather than by name
-oDisplay only symbol names
-PWrite the name of each symbol’s containing object file
-rReverse the order of the sort
-SDisplay size of symbols
-tDisplay only symbol names and types
-uDisplay undefined symbols
-vDisplay version information
–defined-onlyDisplay only defined symbols
–no-sortDo not sort the symbols
–print-file-nameWrite the pathname of an object on each line
–syntheticDisplay 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.