我正在尝试在主机板上实现半主机(精确地为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下运行它时,由于某种原因,我的电脑会移到其他地方,我不了解。

.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

最佳答案

古老的问题,但无论如何。我相信您没有在那里正确地初始化堆栈。

在您的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

关于c - 在stm32上的半主机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26201782/

10-11 06:29
查看更多