了解ARM Cortex-M微控制器的linkerscript

时间:2016-11-10 16:14:09

标签: linker arm cortex-m linker-scripts newlib

我正在使用STMicroelectronics的STM32F746NG微控制器。该器件基于ARM Cortex-M7架构。我花了很多时间来理解示例项目中的linkerscript。我想出了基础知识,但我仍然无法掌握它的大部分内容。请帮我理解这些部分。

启动linkerscript

linkerscript开头如下:

/* Entry Point */
ENTRY(Reset_Handler) /* The function named 'Reset_Handler' is defined */
                     /* in the 'startup.s' assembly file.             */

/* Highest address of the user mode stack */
/* Remember: the stack points downwards */
_estack = 0x20050000;    /* End of RAM */

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

/* --------------------------------------------------------------------*/
/*                    MEMORY AREAS                                     */
/* --------------------------------------------------------------------*/
MEMORY
{
    /* FLASH MEMORY */
    /* ------------ */
    /* Remember: the flash memory on this device can   */
    /* get accessed through either the AXIM bus or the */
    /* ITCM bus. Accesses on the ITCM bus start at     */
    /* address 0x0020 0000. Accesses on the AXIM bus   */
    /* at address 0x0800 0000.                         */
    FLASH (rx)     : ORIGIN = 0x08000000, LENGTH = 1024K
    /* FLASH (rx)     : ORIGIN = 0x00200000, LENGTH = 1024K */

    /* RAM MEMORY */
    /* ---------- */
    RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 320K
}

矢量表和程序代码

在定义了内存区域之后,linkerscript继续定义各个部分。 linkerscript中定义的第一部分是向量表。它必须以闪存的第一个字节结束。

/* --------------------------------------------------------------------*/
/*                    OUTPUT SECTIONS                                  */
/* --------------------------------------------------------------------*/
SECTIONS
{
    /****************************/
    /*      VECTOR TABLE        */
    /****************************/
    .isr_vector :
    {
        . = ALIGN(4);
        KEEP(*(.isr_vector)) /* Vector Table */
        . = ALIGN(4);
    } >FLASH

插入向量表后,程序代码的时间为:

    /****************************/
    /*      PROGRAM CODE        */
    /****************************/
    .text :
    {
        . = ALIGN(4);
        *(.text)           /* .text sections (code) */
        *(.text*)          /* .text* sections (code) */
        *(.glue_7)         /* Glue ARM to Thumb code */
        *(.glue_7t)        /* Glue Thumb to ARM code */
        *(.eh_frame)


        /* Note: The function ‘.text.Reset_Handler’ is one of the *(.text*) sections,      */
        /* such that it gets linked into the output .text section somewhere here.          */
        /* We can verify the exact spot where the Reset_Handler section is positioned, by  */
        /* examining the second entry of the vector table.                                 */
        /* A test has given the following results:
        /*    FLASH (rx) : ORIGIN = 0x0800 0000    ==>  Reset_Handler = 0x0800 1C91        */
        /*    FLASH (rx) : ORIGIN = 0x0020 0000    ==>  Reset_Handler = 0x0020 1CB9        */
        /*
        /* In both cases, the Reset_Handler section ends up a few hundred bytes after the  */
        /* vector table in Flash. But in the first case, the “Reset_Handler” symbol points */
        /* to the Reset-code through AXIM-interface, whereas in the latter case it points  */
        /* to the Reset-code through the ITCM-interface.                                   */


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

        . = ALIGN(4);
        _etext = .;        /* Define a global symbol at end of code */

    } >FLASH

linkerscript定义e_text全局符号,表示flash中程序代码结束的地址。

常量数据

只读数据也会在闪存中结束(把它放在RAM中是没有意义的,这是不稳定的)。 linkerscript定义.rodata部分应该在flash中:

    /****************************/
    /*      CONSTANT DATA       */
    /****************************/
    .rodata :
    {
        . = ALIGN(4);
        *(.rodata)         /* .rodata sections (constants, strings, etc.) */
        *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
        . = ALIGN(4);
    } >FLASH

闪光灯中的神秘部分

在定义了常量只读数据的位置之后,linkerscript定义了一些“神秘的”数据。部分也应该以闪存结束:

    .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

我不知道这些部分是什么。所以,这是第一个问题。这些部分是什么,以及它们显示在哪些目标文件中?如您所知,linkerscript需要将一些目标文件链接在一起。我不知道这些神秘部分存在于哪些目标文件中:

  • .ARM.extab
  • .ARM
  • .preinit_array
  • .init_array
  • .fini_array

这是对闪存的分配结束。 linkerscript继续定义最终在RAM中的部分。

RAM中的部分

.data.bss部分对我来说很清楚。没问题。

    /****************************/
    /*    INITIALIZED DATA      */
    /****************************/
    _sidata = LOADADDR(.data);
    .data :
    {
        . = 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 AT> FLASH


    /****************************/
    /*    UNINITIALIZED DATA    */
    /****************************/
    . = ALIGN(4);
    .bss :
    {
        _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

linkerscript还定义了._user_heap_stack部分:

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

显然此部分不会立即使用。它仅定义为检查RAM是否仍有足够的空间用于堆栈和堆。如果不是这种情况,则抛出链接器错误(.超过顶部RAM地址)。

linkerscript的结尾

这是linkerscript结束的方式。老实说,我不知道它做了什么。所以这是第二个问题:以下是什么意思?

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

    .ARM.attributes 0 : { *(.ARM.attributes) }
}
/* END OF LINKERSCRIPT */

2 个答案:

答案 0 :(得分:6)

  1. .ARM.extab和.ARM.exidx与展开有关。您可以在http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044e/index.html找到更多信息。如果您不关心展开,则不需要它们(展开对C ++异常和调试很有用)。

  2. 这些符号与在main()之前/之后调用的C / C ++构造函数和析构函数启动和拆除代码相关。名为.init,.ctors,.preinit_array和.init_array的部分与C / C ++对象的初始化有关,而.fini,.fini_array和.dtors部分用于拆除。开始和结束符号定义与此类操作相关的代码段的开头和结尾,并且可以从运行时支持代码的其他部分引用。

  3. .preinit_array和.init_array节包含指向初始化时将调用的函数的指针数组。 .fini_array是一个将在销毁时调用的函数数组。据推测,开始和结束标签用于走这些列表。

    1. 堆:不是真的,该部分允许为堆保留一些空间和堆栈的一些空间。显然,如果保留区域的总和超出RAM边界,则会出现错误。这是一个例子:

      _Min_Heap_Size = 0; / *所需的堆量 / _Min_Stack_Size = 0x400; / 所需的堆栈数量* /

      ._ user_heap_stack: {   。 = ALIGN(4);   PROVIDE(end =。);   提供(_end =。);   。 =。 + _Min_Heap_Size;   。 =。 + _Min_Stack_Size;   。 = ALIGN(4); }> RAM

    2. 链接库我更喜欢不同的符号,这只是裸机无RTOS C ++项目的一个例子: GROUP(libgcc.a libc_nano.a libstdc ++ _ nano.a libm.a libcr_newlib_nohost.a crti.o crtn.o crtbegin.o crtend.o)

答案 1 :(得分:0)

首先,你理解这个概念的方法是错误的;这就是我的信念。在理解这个概念的过程中,我遇到了类似的问题。

用简单的语言,我可以解释一下,链接描述文件为我们提供了三个主要内容:

  1. 入口点
  2. 主内存中的运行时地址。
  3. 复制程序
  4. 让我们考虑一个例子,

    假设我们的项目中有N个.c文件。现在编译后,每个文件都包含自己的翻译单元,称为目标文件。

    每个目标文件都包含.text部分/段,其中包含实际代码。和.data部分/数据段。

    要组合每个翻译单元的所有.text部分,链接描述文件会为其提供一些特定的命令。 .data部分也是如此。

    合并所有目标文件后,最终的可执行文件即可使用。

    现在遇到一些悖论......

    Cortex-M系列的切入点只是ResetISR。 在执行ResetISR功能并初始化SoC中的其他可屏蔽中断后,下一步是复制程序。

    复制程序只不过是将.data部分复制到RAM中(其中包括有趣的.bss部分,但我现在不考虑该部分。)

    从ROM复制到RAM是必不可少的,因为实际的ELF文件总是存储在ROM中。

    在执行所有这些与启动相关的事情后,我们现在可以调用我们的main()函数。