How to set stack pointer from gdb using JLink and Cortex M4?

I am using the Jg-Link Segger base with gdb for Linux to debug the Atmel SAM4S8C MCU. I have the following in the gdb batch file that I specify with the --command argument when gdb starts:

 target remote localhost:2331 monitor flash device SAM4S8C 

I also specify my ELF file and load program in the batch file.

I noticed that when I load my program (using load ), the stack pointer is not set correctly. monitor reset also does not change the stack pointer.

The J-Link User Guide says (for Cortex-M3 devices):

In addition, the user needs to adjust the stack pointer (R13) and the PC (R15) manually after reset to debug the application.

What is the command for this from gdb?

It is strange that I needed to do this manually. The stack pointer is the first record of the reset vector. When I develop my code, the start stack pointer changes (in particular, switching between applications that have very different start stack pointers, as I first noticed).

Is there a way to get gdb to read this from an ELF file during load and automatically install it?

+6
source share
2 answers

Cortex-M was designed so that assembly build code was not required:
When launched, Cortex-M automatically loads sp and pc registers from the first two 32-bit flash words.
In the case of my LPC1788, this means that I need to load r13 / sp with the contents of 0x00000000 and r15 / pc with the contents of 0x00000004:

 target remote localhost:2331 monitor interface SWD monitor endian little monitor speed auto monitor reset monitor halt monitor flash device = LPC1788 monitor flash download = 1 monitor flash breakpoints = 1 file program.elf load program.elf break main monitor reg r13 = (0x00000000) monitor reg r15 = (0x00000004) continue 
+2
source

I had this problem since my product used a bootloader located at the beginning of the flash memory that I wanted to skip during debugging. Since the value of the stack pointer should be saved as the first word in your executable file, and reset as the next, you need something like:

 $sp = {int} program_start_address $pc = {int} program_start_address + 4 

eg. for a binary file starting with 0x08001000

 $sp = {int} 0x08001000 $pc = {int} 0x08001004 
+2
source

All Articles