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:

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-default.png62d5494593519.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="431" loading="lazy" src="data:image/svg xml,” width=”500″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-A-option.png62d54945d7ddb.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="398" loading="lazy" src="data:image/svg xml,” width=”500″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-D-option.png62d549461369b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="105" loading="lazy" src="data:image/svg xml,” width=”331″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-f-option.png62d549463ec78.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="458" loading="lazy" src="data:image/svg xml,” width=”500″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-g-option.png62d54946780f4.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="392" loading="lazy" src="data:image/svg xml,” width=”443″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-n-option.png62d54946a7dfa.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="468" loading="lazy" src="data:image/svg xml,” width=”500″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-u-option.png62d54946def7a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="157" loading="lazy" src="data:image/svg xml,” width=”444″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/07/echo/nm-defined-only-option.png62d54947299bf.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="365" loading="lazy" src="data:image/svg xml,” width=”500″>

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.