{"id":3109,"date":"2018-03-12T16:37:55","date_gmt":"2018-03-12T13:37:55","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-nm-command\/"},"modified":"2018-03-12T16:37:55","modified_gmt":"2018-03-12T13:37:55","slug":"linux-nm-command-tutorial-for-beginners-10-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-nm-command-tutorial-for-beginners-10-examples\/","title":{"rendered":"Linux nm Command Tutorial for Beginners (10 Examples)"},"content":{"rendered":"<p>If you are a Linux user who is also into system level software development, you may find yourself in situations where-in you need information related to symbols in an object file. You&#8217;ll be glad to know there exists a command line utility &#8211; dubbed <strong>nm<\/strong> &#8211; that you can use in these situations.<\/p>\n<p>In this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples here have been tested on an Ubuntu 16.04 LTS machine.<\/p>\n<h2 id=\"linux-nm-command\">Linux nm command<\/h2>\n<p>The nm command line utility basically lists symbols from object files. Here&#8217;s the tool&#8217;s syntax:<\/p>\n<p class=\"command\">nm [OPTIONS] OBJECT-FILENAME<\/p>\n<p>And following is how the command&#8217;s man page explains it:<\/p>\n<pre> GNU nm lists the symbols from object files objfile....\u00a0 If no object<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 files are listed as arguments, nm assumes the file a.out.<\/pre>\n<p>Following are some Q&amp;A-styled examples that&#8217;ll give you a better idea on how nm works.<\/p>\n<h2 id=\"q-how-nm-command-works\">Q1. How nm command works?<\/h2>\n<p>The basic usage of this command is very straight forward &#8211; all you have to do is to run the &#8216;nm&#8217; command and pass the name of the object file as input to it. For example, I used the nm command with the &#8216;apl&#8217; binary file:<\/p>\n<p class=\"command\">nm apl<\/p>\n<p>The following screenshot shows the kind of output the above command produced:<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-default\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-default.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p>The three columns produced in output represent the symbol value, symbol type, and symbol name, respectively. There are several types of symbols &#8211; to know the complete details, head to the nm command man page.<\/p>\n<h2 id=\"q-how-to-have-file-names-precede-each-symbol-in-output\">Q2. How to have file names precede each symbol in output?<\/h2>\n<p>This you can do using the <strong>-A<\/strong> command line option.<\/p>\n<p class=\"command\">nm -A [obj-file]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -A apl<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-A-option\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-A-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples-1.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p>So you can see the file name was added in the beginning of each line.<\/p>\n<h2 id=\"q-how-to-make-nm-display-debugger-symbols-as-well\">Q3. How to make nm display debugger symbols as well?<\/h2>\n<p>To make nm display debugger symbols as well in the output, use the <strong>-a<\/strong> command line option.<\/p>\n<p class=\"command\">nm -a [obj-filename]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -a apl<\/p>\n<p>The above command will display all symbols, including debugger-only symbols which are normally not listed.<\/p>\n<h2 id=\"q-how-to-make-nm-decode-lowlevel-symbol-names\">Q4. How to make nm decode low-level symbol names?<\/h2>\n<p>If you want, you can even force nm to decode low-level symbol names into user-level names. This you can do using the -C command line option.<\/p>\n<p class=\"command\">nm -C [obj-file]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -C apl<\/p>\n<p>Here&#8217;s what the man page has to say about this option:<\/p>\n<pre> Besides removing any initial underscore prepended by the system,<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 this makes C++ function names readable. Different compilers have<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 different mangling styles. The optional demangling style argument<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 can be used to choose an appropriate demangling style for your<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 compiler.<\/pre>\n<h2 id=\"q-how-to-make-nm-only-display-dynamic-symbols\">Q5. How to make nm only display dynamic symbols?<\/h2>\n<p>In case you want nm to display only dynamic symbols , rather than the normal symbols, use the <strong>-D<\/strong> command line option.<\/p>\n<p class=\"command\">nm -D [obj-file]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -D apl<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-D-option\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-D-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples-2.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p>So you can see that nm only produced dynamic symbols in the output.<\/p>\n<h2 id=\"q-how-to-use-different-nm-output-formats\">Q6. How to use different nm output formats?<\/h2>\n<p>To enable different output formats, use the -f command line option. By default, the output is produced in the bsd format, however, if you want, you can enable &#8216;sysv&#8217; and &#8216;posix&#8217; formats as well.<\/p>\n<p class=\"command\">nm -f [format] [obj-filename]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -f posix apl<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-f-option\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-f-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples-3.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p>Observe the change in format now.<\/p>\n<h2 id=\"q-how-to-make-nm-display-only-external-symbols\">Q7. How to make nm display only external symbols?<\/h2>\n<p>This you can achieve using the -g command line option.<\/p>\n<p class=\"command\">nm -g [obj-file]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -g\u00a0 apl<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-g-option\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-g-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples-4.png\" alt=\"\" title=\"\"><\/a><\/p>\n<h2 id=\"q-what-all-sort-options-nm-provides\">Q8. What all sort options nm provides?<\/h2>\n<p>By default, the symbols are sorted alphabetically. However, if you want, you can sort them numerically by their addresses using the <strong>-n<\/strong> command line option.<\/p>\n<p class=\"command\">nm -n [objfile]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -n apl<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-n-option\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-n-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples-5.png\" alt=\"\" title=\"\"><\/a><\/p>\n<p>So you can see the output is sorted by addresses now.<\/p>\n<p>In case you don&#8217;t want nm to apply any kind of sorting (including the default one it uses), use the -p command line option. Moving on, to reverse any existing sort, use the -r command line option.<\/p>\n<h2 id=\"q-how-to-make-nm-only-display-undefined-symbols\">Q9. How to make nm only display undefined symbols?<\/h2>\n<p>This you can do using the<strong> -u<\/strong> command line option.<\/p>\n<p class=\"command\">nm -u [obj-file]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm -u apl<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-u-option\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-u-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples-6.png\" alt=\"\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-to-make-nm-only-display-defined-symbols\">Q10. How to make nm only display defined symbols?<\/h2>\n<p>To make nm only display defined symbols, use the <strong>&#8211;defined-only<\/strong> command line option.<\/p>\n<p class=\"command\">nm &#8211;defined-only [obj-file]<\/p>\n<p>For example:<\/p>\n<p class=\"command\">nm &#8211;defined-only apl<\/p>\n<p><a class=\"fancybox\" id=\"img-nm-defined-only-option\" href=\"https:\/\/www.howtoforge.com\/images\/command_tutorial_placeholder_27\/big\/nm-defined-only-option.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/03\/linux-nm-command-tutorial-for-beginners-10-examples-7.png\" alt=\"\" title=\"\"><\/a><\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Agreed, the nm command has a niche audience, but my personal experience says even if you don&#8217;t consider yourself as part of that audience, do keep in mind that such a tool exists. And for those who want to use this utility, once done with practicing all examples here, head to the\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/nm\" target=\"_blank\" rel=\"noopener noreferrer\">nm man page<\/a> to know more.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you are a Linux user who is also into system level software development, you may find yourself in situations where-in you need information related to symbols in an object file. You&#8217;ll be glad to know there exists a command line utility &#8211; dubbed nm &#8211; that you can use in these situations. In this &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-3109","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=3109"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3109\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=3109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=3109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=3109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}