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
Example:
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:
-
Address (where it’s stored in memory)
-
Type (function, variable, etc.)
-
Name
Example 1: Simple Program
sum.c
Compile it:
Then run:
Sample Output:
Understanding the Columns
| Column | Meaning |
|---|---|
Address | Memory address (if known) |
Type | Symbol type (a single letter) |
Name | Symbol name |
Symbol Type Codes
| Code | Section | Meaning |
|---|---|---|
| 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 sum→sum()function is defined in this file (in text section). -
T main→main()function defined here. -
D a→ Global variablea(initialized) in data section. -
B b→ Global variableb(uninitialized) in BSS section. -
U printf→ Undefined symbol — it’s defined in another file (libc).
Display Symbols in an Executable
After linking:
You’ll now see resolved symbols (like printf) with addresses.
Sort Output by Symbol Name
Sorts by symbol address (numeric order).
Show Only External Symbols
Lists only global (external) symbols.
Show Undefined Symbols
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):
Simple Lab Exercise
Aim:
To study the use of nm command for listing symbol information in object and executable files.
Procedure:
-
Write and compile a simple program:
-
Compile it to object form:
-
Run:
-
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
| Command | Purpose |
|---|---|
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
Post a Comment