Jump to content

Nm (Unix): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m restrictive clause, "nm" fmt, and list
Adding R & r symbol description.
(24 intermediate revisions by 16 users not shown)
Line 1: Line 1:
{{short description|Unix command}}
{{lowercase|nm (Unix)}}
{{lowercase|nm (Unix)}}
{{Infobox software
The <tt>'''nm'''</tt> command ships with a number of later versions of [[Unix]] and similar [[operating system]]s. <tt>nm</tt> is used to examine [[binary file]]s (including [[Library (computing)|libraries]], compiled [[Object file|object modules]], shared-object files, and standalone [[executable]]s) and to display the contents of those files, or [[Metadata|meta information]] stored in them, specifically the [[symbol table]]. The output from <tt>nm</tt> distinguishes between various symbol types, for example it differentiates between a [[subroutine|function]] that is supplied by an object module and a function that is required by it. <tt>nm</tt> is used as an aid for [[debugging]], to help resolve problems arising from name conflicts and [[C++]] [[name mangling]], and to validate other parts of the [[toolchain]].
| name = nm
| logo =
| screenshot =
| screenshot size =
| caption =
| author = [[Dennis Ritchie]],<br />[[Ken Thompson]]<br />([[AT&T Bell Laboratories]])
| developer = Various [[open-source software|open-source]] and [[commercial software|commercial]] developers
| released = {{Start date and age|1971|11|3}}
| latest release version =
| latest release date =
| programming language = [[C (programming language)|C]]
| operating system = [[Unix]], [[Unix-like]], [[Plan 9 from Bell Labs|Plan 9]]
| platform = [[Cross-platform]]
| genre = [[Command (computing)|Command]]
| license = Plan 9: [[MIT License]]
| website =
}}
<code>'''nm'''</code> ([[name mangling]]) is a [[Unix]] command used to dump the [[symbol table]] and their attributes from a [[binary file|binary]] [[executable]] file (including [[Library (computing)|libraries]], compiled [[Object file|object modules]], shared-object files, and standalone [[executable]]s).


The output from <code>nm</code> distinguishes between various symbol types. For example, it differentiates between a [[subroutine|function]] that is supplied by an object module and a function that is required by it. <code>nm</code> is used as an aid for [[debugging]], to help resolve problems arising from name conflicts and [[C++]] name mangling, and to validate other parts of the [[toolchain]].
The [[GNU Project]] ships an implementation of <tt>nm</tt> as part of the [[GNU Binutils]] package.

This command is shipped with a number of later versions of [[Unix]] and [[Unix-like|similar]] [[operating system]]s including [[Plan 9 from Bell Labs|Plan 9]]. The [[GNU Project]] ships an implementation of <code>nm</code> as part of the [[GNU Binutils]] package.


==nm output sample==
==nm output sample==


<source lang="c">
<syntaxhighlight lang="c">
/*
/*
* File name: test.c
* File name: test.c
Line 13: Line 34:
*
*
* For C++ code compile with:
* For C++ code compile with:
* g++ -c test.c
* g++ -c test.cpp
*/
*/

int global_var;
int global_var;
int global_var_init = 26;
int global_var_init = 26;
Line 59: Line 81:
}
}


</syntaxhighlight>
</source>


If the previous code is compiled with the [[GNU Compiler Collection|gcc]] C compiler, the output of <tt>nm</tt> command would be the following:
If the previous code is compiled with the [[GNU Compiler Collection|gcc]] C compiler, the output of the <code>nm</code> command is the following:


<syntaxhighlight lang="console">
<pre># nm test.o
# nm test.o
0000000a T global_function
0000000a T global_function
00000025 T global_function2
00000025 T global_function2
Line 75: Line 98:
00000000 b static_var
00000000 b static_var
00000004 d static_var_init
00000004 d static_var_init
</syntaxhighlight>
</pre>


When using the c++ compiler the output differs:
When the C++ compiler is used, the output differs:
<syntaxhighlight lang="console">
<pre>
# nm test.o
# nm test.o
0000000a T _Z15global_functioni
0000000a T _Z15global_functioni
Line 92: Line 115:
0000003b T main
0000003b T main
00000036 T non_mangled_function
00000036 T non_mangled_function
</syntaxhighlight>
</pre>


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 differences between the outputs also show an example of solving the name mangling problem by using [[extern "C"]] in C++ code.


{| class="wikitable"
|+ Symbol Types
|-
! Symbol Type
! Description
|-
| A
|Global absolute symbol.
|-
| a
| Local absolute symbol.
|-
| B
| Global bss symbol.
|-
| b
| Local bss symbol.
|-
| D
| Global data symbol.
|-
| d
| Local data symbol.
|-
| f
| Source file name symbol.
|-
| L
| Global thread-local symbol (TLS).
|-
| l
| Static thread-local symbol (TLS).
|-
| R
| Global read only symbol.
|-
| r
| Local read only symbol.
|-
| T
| Global text symbol.
|-
| t
| Local text symbol.
|-
| U
| Undefined symbol.
|}


==See also==
==See also==
Line 100: Line 173:


==External links==
==External links==
{{Wikibooks|Guide to Unix|Commands}}
*{{man|cu|nm|SUS|write the name list of an object file}}
*{{man|cu|nm|SUS|write the name list of an object file}}
*{{man|1|nm||List symbols from object files}}
*{{man|1|nm|Plan 9}}

{{Unix commands}}
{{Plan 9 commands}}


[[Category:Unix programming tools]]
[[Category:Unix programming tools]]
[[Category:Unix SUS2008 utilities]]
[[Category:Unix SUS2008 utilities]]
[[Category:Articles with example C code]]
[[Category:Plan 9 commands]]


{{unix-stub}}
{{unix-stub}}

[[el:Nm (Unix)]]
[[es:Nm (Unix)]]
[[ko:Nm (유닉스)]]
[[ja:Nm (UNIX)]]
[[ru:Nm]]

Revision as of 17:42, 7 July 2023

nm
Original author(s)Dennis Ritchie,
Ken Thompson
(AT&T Bell Laboratories)
Developer(s)Various open-source and commercial developers
Initial releaseNovember 3, 1971; 52 years ago (1971-11-03)
Written inC
Operating systemUnix, Unix-like, Plan 9
PlatformCross-platform
TypCommand
LicensePlan 9: MIT License

nm (name mangling) is a Unix command used to dump the symbol table and their attributes from a binary executable file (including libraries, compiled object modules, shared-object files, and standalone executables).

The output from nm distinguishes between various symbol types. For example, it differentiates between a function that is supplied by an object module and a function that is required by it. nm is used as an aid for debugging, to help resolve problems arising from name conflicts and C++ name mangling, and to validate other parts of the toolchain.

This command is shipped with a number of later versions of Unix and similar operating systems including Plan 9. The GNU Project 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.cpp
 */

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;
}

#ifdef __cplusplus
extern "C"
#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 C compiler, the output of the nm command is 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 the C++ compiler is used, the output differs:

# 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 also show an example of solving the name mangling problem by using extern "C" in C++ code.


Symbol Types
Symbol Type Description
A Global absolute symbol.
a Local absolute symbol.
B Global bss symbol.
b Local bss symbol.
D Global data symbol.
d Local data symbol.
f Source file name symbol.
L Global thread-local symbol (TLS).
l Static thread-local symbol (TLS).
R Global read only symbol.
r Local read only symbol.
T Global text symbol.
t Local text symbol.
U Undefined symbol.

See also