If you’re a Linux enthusiast with a knack for coding and system-level software development, there may be times when you need detailed information about symbols in an object file. Luckily, there’s a handy command-line utility called nm that comes to the rescue in such scenarios.
This tutorial explores the fundamental aspects of the nm tool, showcasing its capabilities through straightforward examples. Note that all the examples provided here are tested on an Ubuntu 22.04 LTS system.
Exploring the Linux nm Command
The nm command-line utility is designed to list symbols from object files. Its basic syntax is as follows:
nm [OPTIONS] OBJECT-FILENAME
According to the command’s manual:
GNU nm lists the symbols from object files objfile... If no object files are listed as arguments, nm assumes the file a.out.
Below are various Q&A-style examples to help you better understand the functionality of the nm command.
Q1. How does the nm command work?
The most straightforward use of this command involves running ‘nm’ with the object file name as an input. For instance, here is how I used the nm command with the ‘apl’ binary file:
nm apl
The output of the above command is shown in the following image:
The three columns in the output indicate the symbol value, symbol type, and symbol name, respectively. You can refer to the nm command manual for a comprehensive list of symbols and their types.
Q2. How do you include file names before each symbol in the output?
Use the -A option to prefix each symbol with its filename.
nm -A [obj-file]
Example:
nm -A apl
Notice that the file name appears at the start of each line.
Q3. How can you make nm display debugger symbols?
To include debugger symbols in the output, use the -a option.
nm -a [obj-filename]
Example:
nm -a apl
This command will list all symbols, including those relevant only for debugging purposes.
Q4. How can you convert low-level symbol names to user-level names?
To decode low-level symbol names into user-level names, use the -C option.
nm -C [obj-file]
Example:
nm -C apl
The manual explains this option as follows:
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 can you list only dynamic symbols?
To make nm display only dynamic symbols, use the -D option.
nm -D [obj-file]
Example:
nm -D apl
Notice that now only dynamic symbols are displayed in the output.
Q6. How can you use different nm output formats?
To switch between output formats, use the -f option. The default format is ‘bsd’, but you can select ‘sysv’ or ‘posix’ as needed.
nm -f [format] [obj-filename]
Example:
nm -f posix apl
Observe how the format of the output has changed.
Q7. How do you display only external symbols?
Use the -g option to display exclusively external symbols.
nm -g [obj-file]
Example:
nm -g apl
Q8. What sorting options does nm provide?
By default, symbols are sorted alphabetically. To sort them numerically by address, use the -n option.
nm -n [objfile]
Example:
nm -n apl
The output is now sorted by addresses.
To disable any sorting, including the default, use the -p option. To reverse an existing sort, use the -r option.
Q9. How do you display only undefined symbols?
Use the -u option to see only undefined symbols.
nm -u [obj-file]
Example:
nm -u apl
Q10. How do you display only defined symbols?
To see only defined symbols, use the –defined-only option.
nm --defined-only [obj-file]
Example:
nm --defined-only apl
Conclusion
While the nm command caters to a specialized audience, it’s a powerful tool to have in your arsenal, even if it doesn’t seem immediately relevant. For those intending to delve deeper, once you’ve practiced with the examples shared in this guide, refer to the nm manual page to explore further.
FAQs
What are object files?
Object files are intermediate files generated by a compiler that contain machine code, but are not directly executable.
Can nm be used on executable files?
Yes, nm can be used to extract symbol information from both object files and executable binaries.
What symbol types can nm display?
Nm can display various types of symbols, including function symbols, variable symbols, and debugging symbols.
Is nm specific to Linux?
While nm is commonly used in Linux environments, equivalent tools are available in other operating systems, such as macOS and Windows.