function::print_syms - Linux


Overview

print_syms is a Linux command that displays the symbol table of an executable file. A symbol table is a data structure that maps symbolic names to their corresponding memory addresses. This command is commonly used during debugging to identify the location of functions, variables, and other symbols in a program.

Syntax

print_syms [OPTIONS] <executable_file>

Options/Flags

  • -a, –all : Print all symbols, including local symbols.
  • -e, –extern : Only print external symbols.
  • -f, –file : Print the file name where each symbol is defined.
  • -g, –global : Only print global symbols.
  • -h, –help : Print the help menu.
  • -p, –prefix : Only print symbols with the specified prefix.
  • -s, –size : Print the size of each symbol.
  • -t, –type : Print the type of each symbol (e.g., function, variable, data).
  • -v, –verbose : Print additional information about each symbol.
  • -x, –hex : Print the address of each symbol in hexadecimal.

Examples

To print all the symbols in the my_program executable:

print_syms my_program

To print only the external symbols:

print_syms -e my_program

To print the file name where each symbol is defined:

print_syms -f my_program

Common Issues

One common issue is that print_syms may not be able to find the symbol table if the executable file is stripped. Stripping removes the symbol table from an executable to reduce its size. To avoid this issue, use the -g option when compiling the program to ensure that the symbol table is preserved.

Integration

print_syms can be integrated with other debugging tools, such as GDB, to provide more information about the symbols in a program. For example, the following command chain combines print_syms with GDB to print the address of the main function:

print_syms -x my_program | grep 'main' | gawk '{print $1}' | xargs -i{} gdb -ex "p/x {}" my_program

Related Commands

  • nm : Prints the symbol table of an object file or executable.
  • objdump : Disassembles an object file or executable and displays its symbol table.
  • gdb : A powerful debugger that can be used to inspect the symbol table of a running program.
  • addr2line : Translates memory addresses to line numbers and file names in a program.