How to list symbols in object file

nm is a very useful command for programmers. It can be used to list the symbols defined in an executable, object file (.o) or shared object library file (.so).

I typically need this command when the compiler throws me a undefined reference error. By searching for the reference symbol among the symbols exported by a library file, I can figure out if that is the one that needs to be linked into the target executable.

  • To list all symbols exported by an object file, use the -g option:
$ nm -g

Other options that I find useful:

  • -C: This option demangles symbol names to the names used in C or C++ code. By default, symbol names are mangled. The C symbols have extra underscores and the C++ symbols are completely mangled to encode the namespace, class and type information.

  • -A: Prepend each line with path of object file. This is useful when you pipe this output through other programs, like grep.

  • -D: Display dynamic symbols, rather than normal symbols. By default, the symbols in the .symtab and .strtab sections are shown. By specifying this option, the symbols in .dynsym and .dynstr sections are shown.

  • --defined-only: Use this option to get only the symbols defined in the object file. By default, all the symbols in the object file are shown. I typically use this when I am solving an undefined reference problem.

Tried with: Binutils 2.24 and Ubuntu 14.04

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s