Nm (Unix)
Encyclopedia
The nm command ships with a number of later versions of Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 and similar operating system
Operating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...

s. nm is used to examine binary file
Binary file
A binary file is a computer file which may contain any type of data, encoded in binary form for computer storage and processing purposes; for example, computer document files containing formatted text...

s (including libraries, compiled object modules
Object file
An object file is a file containing relocatable format machine code that is usually not directly executable. Object files are produced by an assembler, compiler, or other language translator, and used as input to the linker....

, shared-object files, and standalone executable
Executable
In computing, an executable file causes a computer "to perform indicated tasks according to encoded instructions," as opposed to a data file that must be parsed by a program to be meaningful. These instructions are traditionally machine code instructions for a physical CPU...

s) and to display the contents of those files, or meta information
Metadata
The term metadata is an ambiguous term which is used for two fundamentally different concepts . Although the expression "data about data" is often used, it does not apply to both in the same way. Structural metadata, the design and specification of data structures, cannot be about data, because at...

 stored in them, specifically the symbol table
Symbol table
In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source, such as its type, scope level and...

. The output from nm distinguishes between various symbol types, for example it differentiates between a function
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

 which is supplied by an object module and a function which is required by it. nm is used as an aid for debugging
Debugging
Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a computer program or a piece of electronic hardware, thus making it behave as expected. Debugging tends to be harder when various subsystems are tightly coupled, as changes in one may cause bugs to emerge...

, to help resolve problems arising from name conflicts and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...

 name mangling
Name mangling
In compiler construction, name mangling is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages....

, and to validate other parts of the toolchain
Toolchain
In software, a toolchain is the set of programming tools that are used to create a product...

.

The GNU Project
GNU Project
The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT. It initiated GNU operating system development in January, 1984...

 ships an implementation of nm as part of the GNU Binutils package.

nm output sample


/*
* File name: test.c
* For C code compile with:
* gcc -c test.c
*
* For C++ code compile with:
* g++ -c test.c
*/
int global_var;
int global_var_init = 26;

static int static_var;
static int static_var_init = 25;

static int static_function
{
return 0;
}

int global_function(int p)
{
static int local_static_var;
static int local_static_var_init=5;

local_static_var = p;

return local_static_var_init + local_static_var;
}

int global_function2
{
int x;
int y;
return x+y;
}
  1. ifdef __cplusplus

extern "C"
  1. endif

void non_mangled_function
{
// I do nothing
}

int main(void)
{
global_var = 1;
static_var = 2;

return 0;
}



If the previous code is compiled with the gcc
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

 C compiler the output of nm command would be the following:

# nm test.o
0000000a T global_function
00000025 T global_function2
00000004 C global_var
00000000 D global_var_init
00000004 b local_static_var.1255
00000008 d local_static_var_init.1256
0000003b T main
00000036 T non_mangled_function
00000000 t static_function
00000000 b static_var
00000004 d static_var_init


When using the c++ compiler the output differs:
  1. nm test.o

0000000a T _Z15global_functioni
00000025 T _Z16global_function2v
00000004 b _ZL10static_var
00000000 t _ZL15static_functionv
00000004 d _ZL15static_var_init
00000008 b _ZZ15global_functioniE16local_static_var
00000008 d _ZZ15global_functioniE21local_static_var_init
U __gxx_personality_v0
00000000 B global_var
00000000 D global_var_init
0000003b T main
00000036 T non_mangled_function


The differences between the outputs show also an example on how to solve the name mangling problem by using extern "C" in C++ code.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK