Loading Multiple (Binary) Files with GDB

A typical debugging session involves just one ELF/Dwarf binary or executable. But what if I need to program multiple binary files with gdb? Things like loading both the bootloader and the application binary? Or I have a an on-chip file system or data section I need to program?

In this article I show how I can use gdb to load and program extra data, like a binary (.bin) file, both using command line interface and using an IDE.

Binary File

To demonstrate how this can be done, I need a binary file. Use a binary file editor, or use gdb to read and dump a file. Another way is to use the Hex Editor in Eclipse (EHEP):

Convert to ELF

I cannot load a binary file directly in gdb: it first has to be converted into an ELF file (see “Converting a Raw Binary File into an ELF/Dwarf File for Loading and Debugging“):

arm-none-eabi-objcopy --input-target=binary --output-target=elf32-little data.bin data.bin.elf

gdb Server

That .elf file now can be loaded with GDB. First, I’ll show how it can be loaded with the gdb command line version. In my case I use a J-Link GDB server, but any other GDB server could be used.

That server is listening on TCP/IP port 2331.

gdb Client

Next, I start the gdb client:

arm-none-eabi-gdb

Then I connect to the gdb server using port 2331 for a SEGGER J-Link:

(gdb)target remote localhost:2331

Then using the ‘load’ command to load file into memory. In my case I specify 0x7’0000 as the address where to load the data:

(gdb) load data.bin.elf 0x70000

To check the memory I can use the ‘x’ command. Below I ask gdb to examine 16 bytes in hexadecimal format starting from address 0x7’0000

(gdb) x/16xb 0x70000

Everything looking good! So this is how I can load any arbitrary data into my target memory.

From here on I can use the ‘file’ and ‘load’ command to load my application file or any other file and debug it.

To quit gdb:

(gdb) quit

IDE

Next, how to do the same thing with an IDE like Eclipse? The solution is to add the extra load command(s) to the gdb commands executed. Inside the Launch Configuration, look for the ‘Startup’ tab, and on the bottom there is a text box where I can enter extra commands:

There I can specify my extra load commands with the data files I have created. And I can add as many files I want:

With this, I can easily load multiple files:

Summary

It is easy to load multiple binaries with gdb. I only have to convert them into an ELF file first using the objcopy utility. Then I can use the load command of gdb, both using the command line version or the IDE launch configuration.

Happy loading 🙂

Links

4 thoughts on “Loading Multiple (Binary) Files with GDB

  1. I’ve just recently started work with a Renesas processor (because unlike NXP it can actually be purchased!) and it uses e2 Studio which is another Eclipse based IDE but interesting to me is that it has a simple built-in feature to load multiple files
    (not sure if I can put images in these comments, let’s try this link)

    Like

    • Hi Ian,
      yes, part availability is a HUGE issue for us too, and we already had to switch to different vendors as well. Thanks for that image! Yes, some vendors started to add this kind of stuff to their integrations, so I hope it will get more common too. Definitely easier than working low-level with gdb commands! On the plus side: with the gdb commands you can stay independent of any vendor implementation.

      Liked by 1 person

  2. How does this set-up treat debug information coming from multiple ELF files? How are concurring or even contradictory debug information handled?
    Imagine having an application with part of the code being in RAM/Flash and another part in mask ROM which is used as a library from RAM/Flash. Both code parts are maintained in dedicated builds, so there are 2 ELF files. The symbols from mask ROM code which are referenced in RAM/Flash code appear in both ELF files.
    How does your set-up handled such a scenario?

    Like

What do you think?

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.