nm Command in Linux



The nm command in Linux is a useful utility for examining binary files and libraries. It is primarily used to list the symbols from object files, which can include functions, variables, and other entities. This command is useful for understanding the structure and contents of compiled binaries and libraries.

The nm command lists the symbols from object files (objfile). If no object files are specified, nm assumes the file a.out. For each symbol, nm shows −

Attributes of Each Symbol

  • Value − The symbol value, in the radix selected by options (hexadecimal by default).
  • Type − The symbol type. Lowercase indicates a local symbol, and uppercase indicates a global (external) symbol.
  • Name − The symbol name.

Symbol Types

Tag Description
A The symbol's value is absolute and will not be changed by further linking.
B The symbol is in the uninitialized data section (known as BSS).
C The symbol is common. Common symbols are uninitialized data. When linking, multiple common symbols may appear with the same name. If the symbol is defined anywhere, the common symbols are treated as undefined references.
D The symbol is in the initialized data section.
G The symbol is in an initialized data section for small objects. Some object file formats permit more efficient access to small data objects, such as a global int variable as opposed to a large global array.
I The symbol is an indirect reference to another symbol. This is a GNU extension to the a.out object file format which is rarely used.
N The symbol is a debugging symbol.
R The symbol is in a read-only data section.
S The symbol is in an uninitialized data section for small objects.
T The symbol is in the text (code) section.
U The symbol is undefined.
V

The symbol is a weak object. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error.

When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes zero with no error.

W

The symbol is a weak symbol that has not been specifically tagged as a weak object symbol. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error.

When a weak undefined symbol is linked and the symbol is not defined, the value of the symbol is determined in a system-specific manner without error. On some systems, uppercase indicates that a default value has been specified.

- The symbol is a stabs symbol in an a.out object file. In this case, the next values printed are the stabs other field, the stabs desc field, and the stab type. Stabs symbols are used to hold debugging information.
? The symbol type is unknown, or object file format specific.

Table of Contents

Here is a comprehensive guide to the options available with the nm command −

Syntax of nm Command in Linux

The following is the general syntax for the nm command −

nm [OPTIONS] [FILE]...

Where −

  • OPTIONS − Specifies the options to modify the behavior of the nm command.
  • FILE − One or more object files, archives of object files, or shared libraries to examine. If no files are provided, nm assumes the file out.

nm Command Options

The following are different options available for the nm command, including both long and short forms where applicable −

Options Description
-a, --debug-syms Display debugger-only symbols.
-A, --print-file-name Print the name of the input file before every symbol.
-B Same as --format=bsd.
-C, --demangle[=STYLE] Decode mangled/processed symbol names. STYLE can be none, auto, gnu-v3, java, gnat, dlang, rust.
--no-demangle Do not demangle low-level symbol names.
--recurse-limit Enable a demangling recursion limit. (default)
--no-recurse-limit Disable a demangling recursion limit.
-D, --dynamic Display dynamic symbols instead of normal symbols.
-e (ignored)
-f, --format=FORMAT Use the output format FORMAT. FORMAT can be bsd, sysv, posix, or just-symbols. Default is bsd.
-g, --extern-only Display only external symbols.
--ifunc-chars=CHARS Characters to use when displaying ifunc symbols.
-j, --just-symbols Same as --format=just-symbols.
-l, --line-numbers

Use debugging information to find a filename and line number for each symbol. For a defined symbol, look for the line number of the address of the symbol.

For an undefined symbol, look for the line number of a relocation entry which refers to the symbol. If line number information can be found, print it after the other symbol information.

-n, --numeric-sort Sort symbols numerically by address.
-o Same as -A.
-p, --no-sort Do not sort the symbols.
-P, --portability Same as --format=posix.
-r, --reverse-sort Reverse the sense of the sort.
--plugin NAME Load the specified plugin.
-S, --print-size Print the size of defined symbols.
-s, --print-armap Include the index for symbols from archive members.
--quiet Suppress "no symbols" diagnostic.
--size-sort Sort symbols by size.
--special-syms Include special symbols in the output.
--synthetic Display synthetic symbols as well.
-t, --radix=RADIX Use RADIX for printing symbol values.
--target=BFDNAME Specify the target object format as BFDNAME.
-u, --undefined-only Display only undefined symbols.
-U, --defined-only Display only defined symbols.
--unicode={default|show|invalid|hex|escape|highlight} Specify how to treat UTF-8 encoded unicode characters
-W, --no-weak Ignore weak symbols.
--without-symbol-versions Do not display version strings after symbol names.
@FILE

Read command-line options from file. The options read are inserted in place of the original @file option. If file does not exist, or cannot be read, then the option will be treated literally, and not removed.

Options in file are separated by whitespace. A whitespace character may be included in an option by surrounding the entire option in either single or double quotes.

Any character (including a backslash) may be included by prefixing the character to be included with a backslash. The file may itself contain additional @file options; any such options will be processed recursively.

-h, --help Show a summary of the options to nm and exit.
--version Show the version number of nm and exit.
-X This option is ignored for compatibility with the AIX version of nm. It takes one parameter which must be the string 32_64. The default mode of AIX nm corresponds to -X 32, which is not supported by GNU nm.

Examples of nm Command in Linux

The following examples will give you a good grasp of how to use the nm command with various options to analyze object files −

Display All Symbols Including Debugger-Only Symbols

To display all symbols, including those meant for debugging, you can use the following command −

sudo nm -a example.o

This lists all symbols in the object file example.o, even those that are typically hidden.

nm Command in Linux1

Print Name of Input File before Each Symbol

To precede each symbol with the name of the input file, use the following command −

sudo nm -A example.o

This is useful when dealing with multiple object files, as it clearly identifies the source file for each symbol.

nm Command in Linux2

Decode Mangled C++ Symbols

To make C++ function names readable, simply use the following command −

sudo nm -C example.o

This demangles the C++ symbols, making the output more understandable by converting low-level symbols to user-friendly names.

nm Command in Linux3

Display Dynamic Symbols Instead of Normal Symbols

To show only dynamic symbols, you can run the following command −

sudo nm -D libexample.so

This option is useful for examining symbols in shared libraries, as it focuses on dynamic symbols.

nm Command in Linux4

Use BSD Format for Output

To format the output in BSD style, you can use the following command −

sudo nm -f bsd example.o

The bsd format is the default and this command explicitly sets it for clarity.

nm Command in Linux5

Display Only External Symbols

To list only external (global) symbols, you can simply use the nm command with the "-g" flag −

sudo nm -g example.o

This helps in identifying symbols that are accessible outside the given object file.

nm Command in Linux6

List Symbol Names Only

To display just the names of the symbols, you can use the following command −

sudo nm -j example.o

This option filters out additional details and shows only the symbol names.

nm Command in Linux7

Display Filename and Line Number for Each Symbol

To use debugging information for showing filename and line number, you can run the following command −

sudo nm -l example.o

This is particularly useful for debugging, as it associates symbols with their source code locations.

nm Command in Linux8

Sort Symbols Numerically by Address

To sort symbols based on their addresses, you can simply use the nm command with the "-n" flag −

sudo nm -n example.o

This command sorts the symbols numerically, providing a clear view of their memory layout.

nm Command in Linux9

Reverse the Sort Order of Symbols

To reverse the order of symbols in the output, you can use the nm command with the "-r" flag −

sudo nm -r example.o

This command sorts the symbols in descending order by their addresses.

nm Command in Linux10

Print the Size of Defined Symbols

To include the size of each defined symbol in the output, you can run the nm command with the "-S" flag −

sudo nm -S example.o

This is useful for understanding the memory footprint of each symbol.

nm Command in Linux11

Include Index for Symbols from Archive Members

To include the index for symbols from archive members, you can use the nm command with the "-s" flag −

sudo nm -s libexample.a

This option is helpful when working with archive files, as it maps symbols to their respective modules.

nm Command in Linux12

Display Only Undefined Symbols

To show only the undefined symbols in the object file, you can use the nm command with the "-u" flag −

sudo nm -u example.o

This highlights symbols that need to be resolved by linking with other object files or libraries.

nm Command in Linux13

Display Only Defined Symbols

To show only the defined symbols in the object file, you can use the nm command with the "-U" flag −

sudo nm -U example.o

This filters out undefined symbols, focusing solely on those that are defined within the file.

nm Command in Linux14

Conclusion

The nm command is an indispensable tool for developers and system administrators working with binary files and libraries. It provides detailed insights into the symbols within object files, enabling better understanding and debugging of compiled code.

With its wide array of options, including the ability to display symbol types, sort output, filter specific symbols, and decode complex symbol names, the nm command offers flexibility and precision for analyzing binary structures.

Mastering this command can significantly enhance your proficiency in managing and debugging software at the binary level, making it an essential skill in any Unix or Linux toolkit.

Advertisements