Home > Environment & Utility > nm

nm: View Symbols from Object Files

The nm command displays a list of symbols (functions, variables, etc.) from compiled object files, static libraries, and shared libraries. This helps in understanding the program's structure and aids in debugging.

Overview

nm analyzes the symbol table within object files to output each symbol's name, type, and address. It is useful for diagnosing linking issues, inspecting library contents, and analyzing code optimization.

Key Features

  • Outputs a list of symbols from object files
  • Identifies symbol types (function, variable, defined, undefined, etc.)
  • Provides symbol address and size information
  • Supports C++ name demangling

Key Options

The nm command offers various options to filter symbol lists and control output format.

Symbol Filtering

Output Formatting

Generated command:

Try combining the commands.

Description:

`nm` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Explore various ways to inspect symbol information from object files using the nm command.

View Basic Symbol List

gcc -c main.c -o main.o
nm main.o

Outputs all symbols from a compiled executable or object file in the default format.

View Undefined Symbols Only

nm -u main.o

Useful for diagnosing linking issues by displaying only undefined symbols, such as external functions or variables that are not linked.

Demangle C++ Symbols

g++ -c my_class.cpp -o my_class.o
nm -C my_class.o

Converts complex C++ symbol names in an object file into a human-readable format.

Output with Symbol Sizes

nm -S main.o

Displays the size (in bytes) of each symbol along with its address.

Tips & Notes

Tips and points to consider for effectively using the nm command.

Symbol Type Characters

The characters displayed next to each symbol in the nm output indicate the symbol's type. Uppercase letters denote global symbols, while lowercase letters denote local symbols.

  • A: Absolute symbol
  • B/b: Uninitialized data section (BSS)
  • D/d: Initialized data section
  • T/t: Text (code) section
  • U: Undefined symbol
  • W/w: Weak symbol

Debugging Utility

nm is highly useful for diagnosing linker errors or verifying if specific functions or variables are correctly included in libraries. The -u option, in particular, helps identify undefined references and resolve linking problems.


Same category commands