Linux nm Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux nm command, which is used to display the symbol information for object files. The nm command is a useful tool for developers and system administrators to understand the internal structure of executable files. We will learn how to use the nm command to display symbol information, filter the output, and gain insights into the structure of an executable program.

The lab covers the following steps: Introduction to the nm Command, Displaying Symbol Information of an Executable, and Filtering Symbol Information Using nm Options. These steps will provide a comprehensive understanding of the nm command and its practical applications in software development and system administration tasks.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") subgraph Lab Skills linux/printf -.-> lab-422841{{"`Linux nm Command with Practical Examples`"}} end

Introduction to the nm Command

In this step, we will explore the Linux nm command, which is used to display the symbol information for object files. The nm command is a useful tool for developers and system administrators to understand the internal structure of executable files.

First, let's create a simple C program and compile it into an executable file:

cd ~/project
nano hello.c

Add the following code to the hello.c file:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Now, compile the program:

gcc -o hello hello.c

To view the symbol information of the hello executable, we can use the nm command:

nm hello

Example output:

0000000000001119 T main
                 U printf

The output shows that the executable file hello has a symbol named main at the address 0x0000000000001119, and it also references the printf symbol, which is likely provided by the C standard library.

The nm command provides various options to customize the output and filter the symbol information. We will explore these options in the next step.

Displaying Symbol Information of an Executable

In this step, we will explore the various options of the nm command to display more detailed symbol information for the hello executable we created in the previous step.

First, let's take a closer look at the output of the basic nm command:

nm hello

Example output:

0000000000001119 T main
                 U printf

The output shows the symbol name and its address in the executable file. The letter T indicates that main is a global function, and U means that printf is an undefined symbol, likely provided by an external library.

To get more detailed information, we can use the following nm options:

  • nm -A hello: Displays the file name along with the symbol information.
  • nm -n hello: Sorts the output by symbol name instead of address.
  • nm -p hello: Displays the output in a more readable, post-processed format.
  • nm -C hello: Demangles C++ symbol names for better readability.

Example output:

hello:0000000000001119 T main
hello:                 U printf

You can combine these options to customize the output further. For example:

nm -nC hello

Example output:

hello:                 U std::ostream::operator<<(char const*)
hello:0000000000001119 T main

This command displays the symbols sorted by name, with C++ symbol names demangled for better readability.

The nm command provides a wealth of information about the internal structure of executable files, which can be useful for developers, system administrators, and security researchers.

Filtering Symbol Information Using nm Options

In this step, we will explore how to use the nm command to filter the symbol information for the hello executable.

Sometimes, the output of the nm command can be overwhelming, especially for large executables. To focus on specific types of symbols, we can use the following nm options:

  • nm -D hello: Displays only the dynamic symbols (symbols used by the dynamic linker).
  • nm -T hello: Displays only the static (global) symbols.
  • nm -t <format> hello: Displays the symbol addresses in a specific format (d for decimal, x for hexadecimal, o for octal).
  • nm --defined-only hello: Displays only the defined symbols (not external/undefined symbols).
  • nm --undefined-only hello: Displays only the undefined symbols.

For example, to display the dynamic symbols in hexadecimal format:

nm -Dt hello

Example output:

0000000000001119 T main
                 U printf

To display only the static (global) symbols:

nm -T hello

Example output:

0000000000001119 T main

You can combine these options to further refine the output. For instance, to display only the defined symbols in a more readable format:

nm --defined-only -nC hello

Example output:

hello:0000000000001119 T main

Filtering the symbol information can be particularly useful when working with large or complex executables, as it allows you to focus on the specific symbols you're interested in.

Summary

In this lab, we explored the Linux nm command, which is used to display the symbol information for object files. We first created a simple C program and compiled it into an executable file, then used the nm command to view the symbol information of the executable. We learned that the nm command provides various options to customize the output and filter the symbol information, such as displaying the file name, sorting the output by symbol name, and showing the symbol type and value. The lab provided a practical understanding of how to use the nm command to analyze the internal structure of executable files.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like