在stm32上半主机

时间:2014-10-05 10:34:46

标签: c embedded-linux stm32f4discovery

我正在尝试在扶手板上实现半主机(正好是stm32f4D07,安装了cortex-m4处理器)以便于程序的调试。 我已按照此nice article中指示的步骤操作。我已经安装了openocd,编译了我的程序,因为它在示例makefile中显示并使用相同的链接器脚本和启动文件。 一切似乎都很好但是当我启动gdb并运行程序时,这就是我得到的:

semihosting is enabled
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x2b007822 msp: 0x4c07b510, semihosting
Loading section .text, size 0xd3f4 lma 0x8000000
Loading section .ARM, size 0x8 lma 0x800d3f4
Loading section .init_array, size 0x8 lma 0x800d3fc
Loading section .fini_array, size 0x4 lma 0x800d404
Loading section .data, size 0x974 lma 0x800d408
Loading section .jcr, size 0x4 lma 0x800dd7c
Start address 0x800aa5c, load size 56704
Transfer rate: 17 KB/sec, 6300 bytes/write.
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x2b007822 msp: 0x4c07b510, semihosting
(gdb) info register pc
pc             0x800aa5c    0x800aa5c <Reset_Handler>
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0xd0022b00 in ?? ()

程序计数器最终指向一个随机地址。我试图跟踪放置断点的执行流程,但无论我把它们放在哪里,它们都永远不会到达(我甚至试图在程序的入口点放置一个断点)。显然由于某种原因,该程序从一个未知的地址开始。可能我正在做一些与我忽略的事情有关的巨大错误,有人可以帮助我吗?

编辑:这是链接器脚本(它与上面链接中提供的相同):

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20020000;    /* end of 128K RAM on AHB bus*/

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0;      /* required amount of heap  */
_Min_Stack_Size = 0x400; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
    FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 1024K
    RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 192K
    MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}

/* Define output sections */
SECTIONS
{
    /* The startup code goes first into FLASH */
  .isr_vector :
  {
      . = ALIGN(4);
      KEEP(*(.isr_vector)) /* Startup code */
      . = ALIGN(4);
  } >FLASH

  /* The program code and other data goes into FLASH */
  .text :
  {
      . = ALIGN(4);
      *(.text)           /* .text sections (code) */
      *(.text*)          /* .text* sections (code) */
      *(.rodata)         /* .rodata sections (constants, strings, etc.) */
      *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
      *(.glue_7)         /* glue arm to thumb code */
      *(.glue_7t)        /* glue thumb to arm code */
      *(.eh_frame)

      KEEP (*(.init))
      KEEP (*(.fini))

      . = ALIGN(4);
      _etext = .;        /* define a global symbols at end of code */
      _exit = .;
  } >FLASH


  .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
  .ARM : {
      __exidx_start = .;
      *(.ARM.exidx*)
      __exidx_end = .;
   } >FLASH

  .preinit_array     :
  {
      PROVIDE_HIDDEN (__preinit_array_start = .);
      KEEP (*(.preinit_array*))
      PROVIDE_HIDDEN (__preinit_array_end = .);
  } >FLASH
 .init_array :
 {
     PROVIDE_HIDDEN (__init_array_start = .);
     KEEP (*(SORT(.init_array.*)))
     KEEP (*(.init_array*))
     PROVIDE_HIDDEN (__init_array_end = .);
 } >FLASH
 .fini_array :
 {
     PROVIDE_HIDDEN (__fini_array_start = .);
     KEEP (*(.fini_array*))
     KEEP (*(SORT(.fini_array.*)))
     PROVIDE_HIDDEN (__fini_array_end = .);
 } >FLASH

 /* used by the startup to initialize data */
 _sidata = .;

 /* Initialized data sections goes into RAM, load LMA copy after code */
.data : AT ( _sidata )
{
    . = ALIGN(4);
    _sdata = .;        /* create a global symbol at data start */
    *(.data)           /* .data sections */
    *(.data*)          /* .data* sections */

    . = ALIGN(4);
    _edata = .;        /* define a global symbol at data end */
 } >RAM

 /* Uninitialized data section */
 . = ALIGN(4);
 .bss :
 {
    /* This is used by the startup in order to initialize the .bss secion */
    _sbss = .;         /* define a global symbol at bss start */
    __bss_start__ = _sbss;
    *(.bss)
    *(.bss*)
    *(COMMON)

    . = ALIGN(4);
    _ebss = .;         /* define a global symbol at bss end */
    __bss_end__ = _ebss;
  } >RAM

 /* User_heap_stack section, used to check that there is enough RAM left */
 ._user_heap_stack :
 {
    . = ALIGN(4);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    PROVIDE ( __end__ = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(4);
 } >RAM

 /* MEMORY_bank1 section, code must be located here explicitly            */
 /* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
 .memory_b1_text :
 {
     *(.mb1text)        /* .mb1text sections (code) */
     *(.mb1text*)       /* .mb1text* sections (code)  */
     *(.mb1rodata)      /* read-only data (constants) */
     *(.mb1rodata*)
 } >MEMORY_B1

 /* Remove information from the standard libraries */
 /DISCARD/ :
 {
    libc.a ( * )
    libm.a ( * )
    libgcc.a ( * )
 }

.ARM.attributes 0 : { *(.ARM.attributes) }
}

这是Reset_Handler函数的代码(同样在上面的链接中提供了相同的代码)。这应该是链接器脚本中明确说明的程序的入口点,但是当我在gdb下运行时,由于某些我无法理解的原因,pc会转到其他地方。

.section  .text.Reset_Handler
  .weak  Reset_Handler
  .type  Reset_Handler, %function
Reset_Handler:  

/* Copy the data segment initializers from flash to SRAM */  
  movs  r1, #0
  b  LoopCopyDataInit

CopyDataInit:
  ldr  r3, =_sidata
  ldr  r3, [r3, r1]
  str  r3, [r0, r1]
  adds  r1, r1, #4

LoopCopyDataInit:
  ldr  r0, =_sdata
  ldr  r3, =_edata
  adds  r2, r0, r1
  cmp  r2, r3
  bcc  CopyDataInit
  ldr  r2, =_sbss
  b  LoopFillZerobss
/* Zero fill the bss segment. */  
FillZerobss:
  movs  r3, #0
  str  r3, [r2], #4

LoopFillZerobss:
  ldr  r3, = _ebss
  cmp  r2, r3
  bcc  FillZerobss

  /* Call static constructors */
  bl __libc_init_array
/* Call the application's entry point.*/
  bl  main
  bx  lr    
.size  Reset_Handler, .-Reset_Handler

1 个答案:

答案 0 :(得分:0)

无论如何都是老问题。我相信你没有在那里正确初始化你的堆栈。

在Reset_Handler中,您需要初始化堆栈指针。 改变这个:

Reset_Handler:  

/* Copy the data segment initializers from flash to SRAM */  
  movs  r1, #0
  b  LoopCopyDataInit

到此:

Reset_Handler:  

  ldr sp, =_estack
/* Copy the data segment initializers from flash to SRAM */  
  movs  r1, #0
  b  LoopCopyDataInit