gnu链接器脚本 - stm32f4xx - 全局变量

时间:2017-11-02 12:01:37

标签: c gnu stm32 stm32f4discovery linker-scripts

我想在我的固件中提供内存映射(部分的开始和结束地址):示例:我想知道user_data部分的起源在哪里(0x8800000)以及它的长度(512K) 。我在一个例子中看过它,可以在链接器脚本中定义全局变量,然后固件可以访问它(extern unsigned long GLOBAL_VAR_USER_SECTION_BEGIN)。有人可以帮助我在哪里以及如何声明全局变量?

链接器脚本内存映射

MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw)    : ORIGIN = 0x10000000, LENGTH = 64K
/* BL (rx)         : ORIGIN = 0x8000000,  LENGTH = 32K */
FLASH (rx)     : ORIGIN = 0x8000000,  LENGTH = 512K
USER_DATA (rw) : ORIGIN = 0x8800000,  LENGTH = 512K /* user data section of 512K */
}

链接描述文件

/*
*****************************************************************************
**
**  File        : LinkerScript.ld
**
**  Abstract    : Linker script for STM32F407VGTx Device with
**                1024KByte FLASH, 128KByte RAM
**
**                Set heap size, stack size and stack location according
**                to application requirements.
**
**                Set memory bank area and size if external memory is used.
**
**  Target      : STMicroelectronics STM32
**
**
**  Distribution: The file is distributed as is, without any warranty
**                of any kind.
**
**  (c)Copyright Ac6.
**  You may use this file as-is or modify it according to the needs of your
**  project. Distribution of this file (unmodified or modified) is not
**  permitted. Ac6 permit registered System Workbench for MCU users the
**  rights to distribute the assembled, compiled & linked contents of this
**  file as part of an application binary file, provided that it is built
**  using the System Workbench for MCU toolchain.
**
*****************************************************************************
*/

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x20020000;    /* end of RAM */

/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0x0;    /* required amount of heap  : heap is not used */
_Min_Stack_Size = 0x400; /* required amount of stack : 1K minimal stack size */

/* Specify the memory areas                                     */
/* (xrw) : execute, read, write                                 */
/* Flash: 1M, RAM 128K, CCMRAM: 64K                             */
/* Examples: xK == x'000 * 1.024 B : 128K == 131'072 (0x20'000) */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw)    : ORIGIN = 0x10000000, LENGTH = 64K
/* BL (rx)         : ORIGIN = 0x8000000,  LENGTH = 32K */
FLASH (rx)     : ORIGIN = 0x8000000,  LENGTH = 512K
USER_DATA (rw) : ORIGIN = 0x8800000,  LENGTH = 512K /* user data section of 512K */
}

/* 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) */
    *(.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 */
  } >FLASH

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

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

  /* Initialized data sections goes into RAM, load LMA copy after code */
  .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

  _siccmram = LOADADDR(.ccmram);

  /* CCM-RAM section 
  * 
  * IMPORTANT NOTE! 
  * If initialized variables will be placed in this section,
  * the startup code needs to be modified to copy the init-values.  
  */
  .ccmram :
  {
    . = ALIGN(4);
    _sccmram = .;       /* create a global symbol at ccmram start */
    *(.ccmram)
    *(.ccmram*)

    . = ALIGN(4);
    _eccmram = .;       /* create a global symbol at ccmram end */
  } >CCMRAM AT> FLASH

  /* user data section : 3 different names!                                 */
  /* this places every code with this sections attribute to this section    */ 
  /*                                                                        */
  /* __attribute__((section(".USER_Data"))) int placed_logic()              */
  /* {                                                                      */
  /*   TODO - Add your application code here                                */
  /*   return 1;                                                            */
  /* }                                                                      */
  .user_data :
  {
    *(.USER_Data*);
  } > USER_DATA

  /* 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(8);
    PROVIDE ( end = . );
    PROVIDE ( _end = . );
    . = . + _Min_Heap_Size;
    . = . + _Min_Stack_Size;
    . = ALIGN(8);
  } >RAM

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

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

2 个答案:

答案 0 :(得分:0)

您可以在make中生成内存和链接描述符文件,然后在编译器命令行上使用变量来定义常量(即WAAAAAOUHH!!!!!!!!!!选项)。或者你甚至可以生成一个充满+'a' s的头文件:

mem.tbl:

-D

生成文件:

#define

或者你更进一步,使用the GNUmake table toolkit,它提供了make中的构建管理功能。它是以多平台和交叉开发/嵌入式编写的,并提供算术,表格处理和其他方便的东西。

答案 1 :(得分:-1)

可以轻松完成在链接描述文件中定义符号,您的文件包含一些示例。在.data部分中,已定义符号_sdata_edata,如评论中所述(create a global symbol at data start)。

在链接描述文件中创建后,您可以使用C代码中的那些符号:

#include <stdio.h>
void display_data_start(void)
{
    extern int _sdata;

    printf("_sdata:%x\n", &_sdata);
}

因此&_sdata将包含.data部分的起始地址。