YouTip LogoYouTip

Linux Comm Nm

Linux nm Command | Rookie Tutorial [![Image 1: Linux Command Manual](#) Linux Command Manual](#) * * * `nm` is an important command-line tool in Linux systems, with the full name "Name List". It is used to display symbol information in binary object files (such as library files, executable files), and is a commonly used tool for programmers and binary file analysis. ### Main Functions * List symbols (functions, variables, etc.) in object files * Display symbol types and attributes * Help analyze program linking issues * Assist in debugging and reverse engineering * * * ## Basic Syntax nm filename ### Common Option Parameters Explanation | Option | Description | | --- | --- | | `-a` | Display all symbols, including debug symbols | | `-g` | Display only external (global) symbols | | `-u` | Display only undefined symbols | | `-D` | Display dynamic symbols (for shared libraries) | | `-C` | Decode (demangle) C++ symbol names | | `-l` | Display the line number where the symbol is located (requires debug information) | | `-S` | Display symbol size | | `-t` | Specify output format (d-decimal, o-octal, x-hexadecimal) | | `--size-sort` | Sort by symbol size | | `--defined-only` | Display only defined symbols | * * * ## Symbol Type Explanation The symbol types output by `nm` are represented by a single letter, common ones include: | Type | Description | | --- | --- | | A | Absolute symbol, will not be changed during linking | | B/b | Symbol in the uninitialized data segment (BSS segment) | | D/d | Symbol in the initialized data segment | | T/t | Symbol in the code segment (T indicates global, t indicates local) | | U | Undefined symbol (needs to be linked from other files) | | W/w | Weak symbol | | R/r | Symbol in the read-only data segment | | C | Common symbol | | I | Indirect reference to other symbols | * * * ## Practical Application Examples ### Example 1: View the symbol table of an executable file nm /bin/ls Sample output: 0000000000000000 A _IO_stdin_used 0000000000000000 R _fp_hw 0000000000000000 T _init 0000000000000000 W _ITM_deregisterTMCloneTable 0000000000000000 W _ITM_registerTMCloneTable 0000000000000000 W __cxa_finalize 0000000000000000 W __gmon_start__ 0000000000000000 T __libc_csu_fini 0000000000000000 T __libc_csu_init ... ### Example 2: View only undefined symbols nm -u /bin/ls Sample output: U __ctype_toupper_loc U __errno_location U __overflow U __stack_chk_fail U __strtoul_internal U _obstack_begin U _obstack_newchunk U abort U access ... ### Example 3: View symbols of a C++ program (decoded names) nm -C my_program Sample output: 0000000000000000 T main 0000000000000000 T std::cout 0000000000000000 T std::basic_ostream<char, std::char_traits >& std::operator<< <std::char_traits >(std::basic_ostream<char, std::char_traits >&, char const*)... ### Example 4: View symbol sizes and sort by size nm -S --size-sort my_library.so Sample output: 0000000000000001 0000000000000001 T func1 0000000000000002 0000000000000002 T func2 0000000000000004 0000000000000004 D global_var 0000000000000008 0000000000000008 B large_buffer ... * * * ## Common Usage Scenarios ### 1. Resolve linking errors When encountering "undefined reference" errors, you can use `nm` to check which symbols are undefined: nm -u my_program.o ### 2. Analyze library file contents View symbols exported by a shared library: nm -D libexample.so ### 3. Compare two versions of binary files ## Example nm old_version > old.txt nm new_version > new.txt diff old.txt new.txt ### 4. Search for specific symbols nm my_program | grep "main" * * * ## Notes 1. For stripped binary files, `nm` may not be able to display useful information 2. Binary files of different architectures may require using `nm` from the cross-compilation toolchain 3. Dynamic symbols (in shared libraries) need to be viewed using the `-D` option 4. For C++ programs, it is recommended to always use the `-C` option to decode symbol names * * * ## Advanced Techniques ### Combined use with other tools ## Example # Use objdump to view more detailed symbol information objdump -t my_program # Use readelf to view ELF file header information readelf -s my_program ### Writing scripts to analyze symbols ## Example #!/bin/bash # Count symbol type distribution nm$1|awk'{print $2}'|sort|uniq-c|sort-nr ### Create symbol mapping files nm -n my_program > symbol_map.txt * * * ## Summary The `nm` command is one of the important tools for Linux developers. Mastering it can: * Better understand program structure * Quickly locate linking issues * Analyze third-party library contents * Assist in debugging and reverse engineering Through the basic usage and practical examples introduced in this article, you should have mastered the core functions of the `nm` command. In actual work, you can flexibly use various option parameters according to specific needs. * * Linux Command Manual](#)
← Linux Comm LxcLinux Comm Objdump β†’