nm command

 

nm (Name List)

Purpose

The nm command lists symbols (functions, variables, etc.) from object files, static libraries, and executables.

It’s very useful to:

  • Inspect which symbols (functions/variables) are defined or undefined in a file.

  • Understand linker errors like “undefined reference”.

  • Study how the compiler organizes symbols in different sections (.text, .data, .bss).


Basic Syntax

nm [options] filename

Example:

nm a.out

What are Symbols?

Symbols represent identifiers (names) in your program — for example:

  • Functions (like main, printf)

  • Global/static variables

  • External references (things defined in another file)

Each symbol has:

  1. Address (where it’s stored in memory)

  2. Type (function, variable, etc.)

  3. Name


Example 1: Simple Program

sum.c

#include <stdio.h> int a = 5; // global variable int b; // uninitialized global variable int sum(int x, int y) { return x + y; } int main() { int c = sum(a, 10); printf("%d\n", c); return 0; }

Compile it:

gcc -c sum.c # creates object file sum.o

Then run:

nm sum.o

Sample Output:

0000000000000000 T sum 0000000000000010 T main 0000000000000004 D a 0000000000000008 B b U printf

Understanding the Columns

ColumnMeaning
Address            Memory address (if known)
Type            Symbol type (a single letter)
Name            Symbol name

Symbol Type Codes

CodeSectionMeaning
T.text            Function defined in this file
t.text            Local (static) function
D.data            Initialized global variable
d.data            Initialized static variable
B.bss            Uninitialized global variable
b.bss            Uninitialized static variable
U            Undefined (used but not defined here — will be resolved by linker)
C            Common (uninitialized data to be allocated by the linker)
R.rodata            Read-only data (constants, strings)
W            Weak symbol
?            Unknown type

From the Example Above

  • T sumsum() function is defined in this file (in text section).

  • T mainmain() function defined here.

  • D a → Global variable a (initialized) in data section.

  • B b → Global variable b (uninitialized) in BSS section.

  • U printf → Undefined symbol — it’s defined in another file (libc).


Display Symbols in an Executable

After linking:

gcc sum.c -o sum nm sum

You’ll now see resolved symbols (like printf) with addresses.


Sort Output by Symbol Name

nm -n sum

Sorts by symbol address (numeric order).


Show Only External Symbols

nm -g sum.o

Lists only global (external) symbols.


Show Undefined Symbols

nm -u sum.o

Displays only undefined symbols — useful for debugging linker errors.


Show Demangled C++ Names

For C++ programs, you can make nm print readable names (instead of mangled ones):

nm -C hello.o

Simple Lab Exercise

Aim:

To study the use of nm command for listing symbol information in object and executable files.

Procedure:

  1. Write and compile a simple program:

    #include <stdio.h> int x = 10; int y; int square(int n) { return n * n; } int main() { printf("%d\n", square(x)); return 0; }
  2. Compile it to object form:

    gcc -c test.c
  3. Run:

    nm test.o
  4. Observe the symbol table output and identify:

    • Defined functions

    • Initialized/uninitialized variables

    • Undefined external symbols


Learning Outcomes

Students will:

  • Understand how functions and variables are stored in different segments.

  • Identify undefined and external symbols.

  • Gain insight into how linking works in C programs.


Quick Reference Summary

CommandPurpose
nm file.o                List all symbols
nm -n file.o                Sort symbols by address
nm -g file.o                Show only external (global) symbols
nm -u file.o                Show undefined symbols
nm -C file.o                Demangle C++ names

Comments

Popular posts from this blog

Operating Systems OS Lab PCCSL407 Semester 4 KTU BTech CS 2024 Scheme - Dr Binu V P

Exploring the /proc file system

ps command