【问题标题】:GNU LD linker script - stack placementGNU LD 链接描述文件 - 堆栈放置
【发布时间】:2018-11-09 16:20:42
【问题描述】:

这是我的 STM32L476 链接器脚本:

/* Generate a link error if heap and stack don't fit into RAM */
__heap_size = 0x200;;      /* required amount of heap  */
__stack_size = 0x800;; /* required amount of stack */

/* Specify the memory areas */
MEMORY
{
    FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
    RAM (rwx)  : ORIGIN = 0x20000000, LENGTH = 96K
}

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

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

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

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

  /* Constant data goes into FLASH */
  .rodata :
  {
    . = ALIGN(8);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(8);
  } >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 (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
  } >FLASH

  /* The startup code goes first into FLASH */
  .exceptions :
  {
    . = ALIGN(8);
    KEEP(*(.exceptions)) /* RAM vector table */
    . = ALIGN(8);
  } >RAM

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

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

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


  /* 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)
    *(.bss*)
    *(COMMON)

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

  /* User_heap_stack section, used to check that there is enough RAM left */
  .heap :
  {
    . = ALIGN(4);
    PROVIDE ( end = . );
    _sheap = .;
    . = . + __heap_size;
    . = ALIGN(4);
    _eheap = .;
  } >RAM

  .stack :
  {
    . = ALIGN(4);
    _estack = .;
    . = . + __stack_size;
    . = ALIGN(4);
    _sstack = .;
  } >RAM

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

对应的地图文件为:

.stack          0x20002844      0x800 load address 0x0801cc68
                0x20002844                . = ALIGN (0x4)
                0x20002844                _estack = .
                0x20003044                . = (. + __stack_size)
 *fill*         0x20002844      0x800 
                0x20003044                . = ALIGN (0x4)
                0x20003044                _sstack = .

我想修改它以使堆栈位于 RAM 的末尾。 我尝试了几种方法(包括讨论过的here,但没有一种方法有效。即使放置硬编码地址也会返回错误(该芯片上的 RAM 高达 0x20018000,因此它应该适合):

     .stack :
  {
    . = 0x20001000;
    _estack = .;
    . = . + __stack_size;
    . = ALIGN(4);
    _sstack = .;
  } >RAM

错误是:

20:01:46 **** Build of configuration Debug for project CardioNexion ****
make app=unit_test board=nucleo-l476rg V=1 all 
c:/program files (x86)/atollic/truestudio for stm32 9.0.0/armtools/bin/../lib/gcc/arm-atollic-eabi/6.3.1/../../../../arm-atollic-eabi/bin/ld.exe: region `RAM' overflowed by 536789060 bytes
collect2.exe: error: ld returned 1 exit status
make: *** [link] Error 1

20:01:50 Build Finished (took 4s.3ms)

知道什么可能导致问题以及如何做到这一点? (将堆栈放在 ram 的末端)。

【问题讨论】:

    标签: gcc arm gnu ld stm32


    【解决方案1】:

    如果您想将堆栈放在 RAM 顶部,您可以在链接描述文件中使用简单的算术,如下所示(已简化):

    MEMORY {
        FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
        RAM (rwx)  : ORIGIN = 0x20000000, LENGTH = 96K
    }
    
    SECTIONS {
        __stacktop = ORIGIN(RAM) + LENGTH(RAM);
    
        . = ORIGIN(FLASH);
        .text : {
            KEEP(*(.stack))
            KEEP(*(.vectors))
            KEEP(*(.text))
            . = ALIGN(4);
            KEEP(*(.rodata))
            . = ALIGN(4);
        } >FLASH
    
        .data ALIGN(4) : {
            __data_start = .;
            *(.data)
            . = ALIGN(4);
            __data_end = .;
        } >RAM AT >FLASH
    
        .bss ALIGN(4) (NOLOAD) : {
            __bss_start = .;
            *(.bss)
            . = ALIGN(4);
            __bss_end = .;
        } >RAM
    
        . = ALIGN(4);
        __heap_start = .;
    }
    

    KEEP(*(.stack)) 在 flash 的开头很重要,然后在代码中将 __stacktop 放到此部分,如下所示:

    启动文件之一:

    // top of stack
    extern unsigned __stacktop;
    
    // initial stack pointer is first address of program
    __attribute__((section(".stack"), used)) unsigned *__stack_init = &__stacktop;
    

    所有未使用的 RAM 将从一侧用于 HEAP,从另一侧用于 STACK。

    简单而完整的例子在这里:https://github.com/cortexm/baremetal

    【讨论】:

    • 我仍然想知道为什么使用 LD 链接器来做到这一点如此复杂?我已经习惯了 Keil 和 IAR,据我所知,很明显将堆栈放置在我想要的位置。特别是为什么为堆栈开始指定硬编码值不起作用?
    【解决方案2】:

    这种“传统”方法对裸机开发不是很好。

    更好的方法是将堆栈放在 RAM 的开头。没有静默变量覆盖的危险,堆栈溢出会产生异常 - 其例程可以采取适当的行动(例如将设备切换到“安全”模式,重新启动,紧急停止受控机器等。

    【讨论】:

    • 你完全正确。它不会直接回答问题(在给定位置放置一个部分),但它可能是正确的方法
    • @GuillaumePetitjean 请记住,故障处理程序此时将有没有堆栈,处理程序中的任何堆栈操作都会导致重置,除非您单独设置预先设置应用程序和系统堆栈,或者作为故障处理程序中的第一件事。
    • @berendi 这对于玩链接器脚本的人来说是很明显的。我想。
    • @P__J__ 只是想知道:由于上下文切换,ARM 架构在处理硬故障处理程序时会推送寄存器(至少,推送 )。如果没有剩余 ram,则推送操作将由于硬故障硬故障而以复位结束。硬故障处理程序应该是一个将堆栈放在 ram 开头的 na_k_ed 函数吗?
    • 如果将堆栈放在 RAM 的开头,甚至无法启动堆栈外异常处理程序,因为这涉及将异常信息推送到不再存在的堆栈上。所以,双重硬故障=重置,没有办法解决。如果这是您想要的,那很好,但请记住,您不能在这种情况下设置调试器陷阱。
    猜你喜欢
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多