Inspecting Debug Symbols with the nm Utility

Vishal Rashmika published on
3 min, 520 words

Categories: Debugging

When debugging a program, it's often necessary to understand the layout of the program's memory and the relationships between different code segments. The nm utility is a powerful tool that allows you to inspect the debug symbols of an executable or object file, giving you valuable insights into the program's internal workings.

What is nm?

nm is a command-line utility that stands for "Name Manager". It's part of the GNU Binutils package and is commonly used to display information about the symbols in an object file or executable. The nm utility can be used to inspect the symbol table of a file, which contains information about the names, addresses, and types of variables, functions, and labels defined in the file.

Basic Syntax

The basic syntax for using nm is as follows:

nm [options] filename

Where file is the name of the executable or object file you want to inspect. You can specify multiple files by separating them with spaces.

Common Options

Here are some common options you can use with nm:

  • -a or --demangle : Demangles C++ symbol names.
  • -C or --check-summaries : Displays summary information for each section.
  • -D or --dynamic-relocs : Displays dynamic relocations.
  • -l or --line-numbers : Displays line numbers for each symbol.
  • -n or --numeric-sort : Sorts symbols numerically by value.
  • -p or --public-only : Displays only public symbols.
  • -r or --reverse-sort : Sorts symbols in reverse order.
  • -S or --symbolic : Displays symbolic information only.
  • -t or --target=<target> : Specifies the target format (e.g. elf32, elf64, etc.).

Symbol types

Symbol Typeawdadawdawdawd
AAbsolute Symbol
BIn the Uninitialized Data Section (BSS)
DIn the Initialized Data Section
TDebugging Symbol
NIn the Text Section
USymbol Undefined right now
  • Lower case symbols are Local symbols
  • Upper case sybols is External symbols

For more information see the manual of NM

Example Usage

Let's use the nm utility to inspect a simple C program called hello.c. First, we'll compile the program using gcc:

gcc -ggdb hello.c -o hello_debug

Now, let's use nm to display a list of all symbols in the object file:

nm ./hello_debug

This will produce a list of symbols in the format:

Here, we see a mix of global variables, functions, and labels. The T, D, and U prefixes indicate the type of symbol

  • T: Global variable (Initialized)
  • D: Global variable (Uninitialized)
  • U: External reference

nm -n ... (Display in Sorted Order)

It will sort using the virtual Addresses

nm -n hello_debug

nm -g (List External Symbols)

nm -g hello_debug

nm -S (display Size)

nm -S hello_debug

Demo

Conclusion

The nm utility is a powerful tool for inspecting debug symbols in executables and object files. By using various options and flags, you can customize the output to suit your needs. Whether you're debugging a complex program or simply trying to understand how a library works, nm is an essential tool to have in your toolkit.