../

Adding Debug Symbols to a Binary with objcopy and GDB

In the previous blog post I discussed about removing symbol files from binaries. In this blog post I will discuss about how to add/attach a extracted symbol file to a stripped binary.

There are two methods that we can use: 1. Add it to the binary itself using objcopy 2. Load the symbol file within GDB

Why do we need symbol files ?

In the software development world, sometimes you need to peer behind the curtain. When a program misbehaves, understanding its inner workings becomes crucial. This is where debug symbols come in – they act as a map, revealing function names, variable locations, and other valuable information that debuggers like GDB can use to navigate the program’s code.

However, by default, binaries often ship without these symbols. This keeps the file size down and protects sensitive information. But for development and debugging purposes, adding debug symbols back can be a lifesaver. Here’s where objcopy, a versatile tool from the GNU Binutils package, enters the scene.

1. Adding to the binary itself

Imagine you have a stripped binary named myprogram. To add debug symbols from a separate debug symbol file (debug_symbols.dbg), you’d use the following command:

objcopy --add-gnu-debuglink=debug_symbols.dbg myprogram myprogram_with_symbols

This command does three things:

  1. --add-gnu-debuglink : This flag tells objcopy to add debug information.
  2. debug_symbols.dbg : This specifies the file containing the debug symbols.
  3. myprogram myprogram_with_symbols : These indicates the stripped binary to be modified and the resulting file name with debug symbols respectively.

objcopy offers more control over the debug symbol addition process. You can explore the man page for objcopy to discover options like --only-keep-debug for creating a new binary containing only the symbols, and --extract-symbol for extracting specific symbols from an existing binary.

By mastering objcopy for adding debug symbols, you can unlock the secrets hidden within binaries, enabling a more efficient and insightful debugging experience.

2. Using GDB

Debuggers like GDB are powerful tools for investigating program misbehavior. However, their effectiveness heavily relies on the presence of debug symbols in the binary. These symbols provide crucial information about functions, variables, and source code, allowing GDB to pinpoint issues with greater accuracy.

symbol-file ./debug_symbols.dbg

Tags: /gdb/ /debugging/ /symbol-files/ /objcopy/ /gnu/ /binutils/