Skip to content

What is nm Linux command?

The Linux nm command is a powerful tool used to display symbol information from object files. It helps users examine binary files and libraries to understand the symbols present within them. This command is commonly used in debugging and analyzing compiled programs.

nm Syntax:

Terminal window
nm [options] [object-file]

nm Options:

OptionDescription
-A, —print-file-namePrint the name of the file before its symbols.
-C, —demangleDecode (demangle) low-level symbol names into user-level names.
-D, —dynamicDisplay dynamic symbols instead of normal symbols.
-f , —format=Specify the output format for the symbols.
-l, —line-numbersShow line numbers where symbols were found.
-n, —numeric-sortSort symbols numerically.
-p, —no-sortDo not sort the symbols.
-t , —radix-sort=Sort symbols on the specified column.
-S, —print-sizeShow the size of the symbols.
-u, —undefined-onlyShow only undefined symbols.

Parameters:

ParameterDescription
object-fileThe object file to examine for symbols.

nm Command Usage Examples:

Display Symbols in a Binary File

Terminal window
nm /usr/bin/ls

Displays the symbols (functions and variables) in the binary file “ls”.

Display Only External Symbols

Terminal window
nm -g /usr/bin/ls

Displays only the external symbols in the binary file “ls”.

Display Symbols in a Shared Library

Terminal window
nm /usr/lib/libc.so

Displays the symbols in the shared library “libc.so”.

Display Demangled C++ Symbols

Terminal window
nm -C /usr/bin/program

Displays demangled C++ symbols in the binary file “program”.

Filter Symbols by Name

Terminal window
nm /usr/bin/program | grep my_function

Filters and displays symbols with the name “my_function” in the binary file “program”.

How do I use nm in Linux?

To use the nm command in bash, execute the following command:

Terminal window
nm /path/to/your/file

What are the common options used with nm in Linux?

To display the symbol table of an object file using nm in Linux, you can use the -S option. Here’s an example:

Terminal window
nm -S /path/to/your/file

How can I filter the output of nm in Linux?

You can filter the output of nm in Linux by using grep to search for specific symbols. For example:

Terminal window
nm /path/to/your/file | grep main

How do I show only undefined symbols with nm in Linux?

To display only undefined symbols using nm in Linux, you can use the -u option. Here’s how you can do it:

Terminal window
nm -u /path/to/your/file

How can I demangle C++ symbol names with nm in Linux?

To demangle C++ symbol names when using nm in Linux, you can use the --demangle option. Here’s an example:

Terminal window
nm --demangle /path/to/your/file

How do I display the size of symbols with nm in Linux?

You can display the size of symbols using nm in Linux by using the -S and -l options together. Here’s how you can do it:

Terminal window
nm -S -l /path/to/your/file

How can I get the full path for symbols with nm in Linux?

To show the full path for each symbol when using nm in Linux, you can use the -A option. Here’s an example:

Terminal window
nm -A /path/to/your/file

How do I list only external defined symbols with nm in Linux?

To list only external defined symbols using nm in Linux, you can use the -g and -D options together. Here’s an example:

Terminal window
nm -g -D /path/to/your/file

Applications of the nm command

  • Display symbols from object files
  • List shared library dependencies
  • Show the type of symbols (e.g., function, object, etc.)
  • Check for the presence of specific symbols in binary files
  • Identify undefined symbols in an object file