Linux nm Command Tutorial for Beginners (10 Examples)

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'll be glad to know there exists a command line utility - dubbed nm - that you can use in these situations.

In this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it's worth mentioning that all examples here have been tested on an Ubuntu 16.04 LTS machine.

Linux nm command

The nm command line utility basically lists symbols from object files. Here's the tool's syntax:

nm [OPTIONS] OBJECT-FILENAME

And following is how the command's man page explains it:

       GNU nm lists the symbols from object files objfile....  If no object
       files are listed as arguments, nm assumes the file a.out.

Following are some Q&A-styled examples that'll give you a better idea on how nm works.

Q1. How nm command works?

The basic usage of this command is very straight forward - all you have to do is to run the 'nm' command and pass the name of the object file as input to it. For example, I used the nm command with the 'apl' binary file:

nm apl

The following screenshot shows the kind of output the above command produced:

The three columns produced in output represent the symbol value, symbol type, and symbol name, respectively. There are several types of symbols - to know the complete details, head to the nm command man page.

Q2. How to have file names precede each symbol in output?

This you can do using the -A command line option.

nm -A [obj-file]

For example:

nm -A apl

So you can see the file name was added in the beginning of each line.

Q3. How to make nm display debugger symbols as well?

To make nm display debugger symbols as well in the output, use the -a command line option.

nm -a [obj-filename]

For example:

nm -a apl

The above command will display all symbols, including debugger-only symbols which are normally not listed.

Q4. How to make nm decode low-level symbol names?

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.

nm -C [obj-file]

For example:

nm -C apl

Here's what the man page has to say about this option:

           Besides removing any initial underscore prepended by the system,
           this makes C++ function names readable. Different compilers have
           different mangling styles. The optional demangling style argument
           can be used to choose an appropriate demangling style for your
           compiler.

Q5. How to make nm only display dynamic symbols?

In case you want nm to display only dynamic symbols , rather than the normal symbols, use the -D command line option.

nm -D [obj-file]

For example:

nm -D apl

So you can see that nm only produced dynamic symbols in the output.

Q6. How to use different nm output formats?

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 'sysv' and 'posix' formats as well.

nm -f [format] [obj-filename]

For example:

nm -f posix apl

Observe the change in format now.

Q7. How to make nm display only external symbols?

This you can achieve using the -g command line option.

nm -g [obj-file]

For example:

nm -g  apl

Q8. What all sort options nm provides?

By default, the symbols are sorted alphabetically. However, if you want, you can sort them numerically by their addresses using the -n command line option.

nm -n [objfile]

For example:

nm -n apl

So you can see the output is sorted by addresses now.

In case you don'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.

Q9. How to make nm only display undefined symbols?

This you can do using the -u command line option.

nm -u [obj-file]

For example:

nm -u apl

Q10. How to make nm only display defined symbols?

To make nm only display defined symbols, use the --defined-only command line option.

nm --defined-only [obj-file]

For example:

nm --defined-only apl

Conclusion

Agreed, the nm command has a niche audience, but my personal experience says even if you don'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 nm man page to know more.

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

From: Emre at: 2018-04-24 14:16:34

Its good article. Thank you