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.
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
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.
Next, I start the gdb client:
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:
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:
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 🙂
- Using Eclipse to Program Binary Files to an Embedded Target
- Converting Binary Files to Intel Hex Format with the SRecord Tool
- Converting a Raw Binary File into an ELF/Dwarf File for Loading and Debugging
- gdb online documentation: https://sourceware.org/gdb/onlinedocs/gdb/Files.html
- MCUXpresso IDE: S-Record, Intel Hex and Binary Files
- Command Line Programming and Debugging with GDB