【问题标题】:Linker sector global variables addresing problem链接器扇区全局变量解决问题
【发布时间】:2022-10-24 23:00:18
【问题描述】:

我已经制作了一个解决方法,但我对这里发生的事情有疑问。 设置:

  1. StmCUBEIDE(版本 10)
  2. Stm32F4 在带局域网的定制板上
  3. 8Mb 额外 SDRAM
  4. 单启动模式下的 1Mb FLASH
    默认从 0x08000000 开始

    长话短说,我已经将我的内存(闪存)分割成链接器中提供的扇区。请参阅下面的链接器:

    /*
    *****************************************************************************
    **
    
    **  File        : LinkerScript.ld
    **
    **  Abstract    : Linker script for STM32F4xx Device with
    **                1024Kbytes FLASH, 192Kbytes 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 = 0x20030000; /* end of RAM */
    /* Generate a link error if heap and stack don't fit into RAM */
    _Min_Heap_Size = 128K;  /* required amount of heap  */
    _Min_Stack_Size = 2K; /* required amount of stack */
    
    /* Specify the memory areas
    * reserved                           sector 0  (16kb sectors)
    * flash (bootloader flash)           sector 4 & 5 (64kb + 128kb = 192kb)
    * sectore stable                     sector 6 to 8 ( 128 +128 +128 = 384kb)
    * sector latest                      sector 9 to 11 ( 128 +128 +128 = 384kb)
    *   
    */
    MEMORY
    {
      FLASH_RESERVED (rxw): ORIGIN = 0x08000000, LENGTH = 16K
      FLASH_SX (rw) : ORIGIN = 0x08004000, LENGTH = 32K
      FLASH_COMMON  (rw) : ORIGIN = 0x0800C000, LENGTH = 16K
      FLASH_BOOTLOADER (rx)         : ORIGIN = 0x08010000, LENGTH = 192K 
      FLASH_STABLE (rxw)   : ORIGIN = 0x08040000, LENGTH = 384K
      FLASH_LATEST (rxw)   : ORIGIN = 0x080A0000, LENGTH = 384K
      RAM (rwx)          : ORIGIN = 0x20000000, LENGTH = 192K
      EXRAM (rwx)          : ORIGIN = 0xD0000000, LENGTH = 8192K
      
    }
    
    /* Define output sections */
    SECTIONS
    {
      /* The startup code goes first into FLASH_BOOTLOADER */
      .isr_vector :
      {
        . = ALIGN(4);
        KEEP(*(.isr_vector)) /* Startup code */
        . = ALIGN(4);
      } >FLASH_BOOTLOADER
        /* keep this section for OTA capabilities*/
        .otaComBuff 0x08000000:
        {
            KEEP(*(.otaComBuff))
        } >FLASH_RESERVED = 0x00
        
      /* The program code and other data goes into FLASH_BOOTLOADER */
      .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_BOOTLOADER
      /* sectors for dual boot and reserved data */
        .reserved :
        {
            . = ALIGN(4);
            __reserved_start__ = .;
            KEEP(*(.reserved*))
            
            . = ALIGN(4);
            __reserved_end__ =  .;
        } >FLASH_RESERVED
        ASSERT(LENGTH(FLASH_RESERVED) >= (__reserved_end__ -__reserved_start__), "sector Reserved overflow")
        .sx (NOLOAD):
        {
            . = ALIGN(4);
            __sx_start__ = .;
            KEEP(*(.sx*))
            
            . = ALIGN(4);
            __sx_end__ =  .;
        } >FLASH_SX 
        ASSERT(LENGTH(FLASH_SX) >= (__sx_end__ -__sx_start__), "sector sx overflow")
    
        .common :
        {
            . = ALIGN(4);
            __common_start__ = .;
            KEEP(*(.common*))
            
            . = ALIGN(4);
            __common_end__ =  .;
        } >FLASH_COMMON
        ASSERT(LENGTH(FLASH_COMMON) >= (__common_end__ -__common_start__), "sector common overflow")
    
        .bootloader  :
        {
            . = ALIGN(4);
            __bootloader_start__ = .;
            KEEP(*(.bootloader*))
            
            . = ALIGN(4);
            __bootloader_end__ = .;
        } >FLASH_BOOTLOADER
        ASSERT(LENGTH(FLASH_BOOTLOADER) >= (__bootloader_end__ -__bootloader_start__), "sector bootloader overflow")
    
        .stable :
        {
            . = ALIGN(4);
            __stable_start__ = .;
            KEEP(*(.stable*))
            
            . = ALIGN(4);
            __stable_end__ = .;
        } >FLASH_STABLE 
        ASSERT(LENGTH(FLASH_STABLE) >= (__stable_end__ -__stable_start__), "sector stable overflow")
    
        .latest  :
        {
            . = ALIGN(4);
            __latest_start__ = .;
            KEEP(*(.latest*))
            
            . = ALIGN(4);
            __latest_end__ = .;
        } >FLASH_LATEST
        ASSERT(LENGTH(FLASH_LATEST) >= (__latest_end__ -__latest_start__), "sector latest overflow")
    
      /* Constant data goes into FLASH_BOOTLOADER */
      .rodata :
      {
        . = ALIGN(4);
        *(.rodata)         /* .rodata sections (constants, strings, etc.) */
        *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
        . = ALIGN(4);
      } >FLASH_BOOTLOADER
    
      .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH_BOOTLOADER
      .ARM : {
        __exidx_start = .;
        *(.ARM.exidx*)
        __exidx_end = .;
      } >FLASH_BOOTLOADER
    
      .preinit_array     :
      {
        PROVIDE_HIDDEN (__preinit_array_start = .);
        KEEP (*(.preinit_array*))
        PROVIDE_HIDDEN (__preinit_array_end = .);
      } >FLASH_BOOTLOADER
      .init_array :
      {
        PROVIDE_HIDDEN (__init_array_start = .);
        KEEP (*(SORT(.init_array.*)))
        KEEP (*(.init_array*))
        PROVIDE_HIDDEN (__init_array_end = .);
      } >FLASH_BOOTLOADER
      .fini_array :
      {
        PROVIDE_HIDDEN (__fini_array_start = .);
        KEEP (*(SORT(.fini_array.*)))
        KEEP (*(.fini_array*))
        PROVIDE_HIDDEN (__fini_array_end = .);
      } >FLASH_BOOTLOADER
    
      /* 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_BOOTLOADER
    
      
      /* 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
        /* Uninitialized external data section */
      . = ALIGN(4);
      .extbss (NOLOAD):
      {
        /* This is used by the startup in order to initialize the .bss secion */
        _extsbss = .;         /* define a global symbol at bss start */
        __extbss_start__ = _extsbss;
        *(.extbss)
        *(.extbss*)
        *(COMMON)
    
        . = ALIGN(4);
        _extebss = .;         /* define a global symbol at bss end */
        __extbss_end__ = _extebss;
      } >EXRAM
    
      /* User_heap_stack section, used to check that there is enough RAM left */
      ._user_heap_stack :
      {
        . = ALIGN(4);
        PROVIDE ( end = . );
        PROVIDE ( _end = . );
        . = . + _Min_Heap_Size;
        PROVIDE ( _max_heap = . );
        . = . + _Min_Stack_Size;
        . = ALIGN(4);
      } >RAM
    
      /* Remove information from the standard libraries */
      /DISCARD/ :
      {
        libc.a ( * )
        libm.a ( * )
        libgcc.a ( * )
      }
    
      .ARM.attributes 0 : { *(.ARM.attributes) }
    }
    
    
    
    

    接下来我的文件中有:

    #include "stm32f4xx_hal.h"
    #include "stdint.h"
    #include "FreeRTOS.h"
    #include "task.h"
    #include "error.h"
    #include "debug.h"
    
    extern unsigned int __bootloader_start__ ;
    extern unsigned int __bootloader_end__ ;
    
    extern unsigned int __sx_start__ ;
    extern unsigned int __sx_end__ ;
    /*external linker symbols for sector address feching*/
    extern unsigned int __stable_start__ ;
    extern unsigned int __stable_end__ ;
    extern unsigned int __latest_start__ ;
    extern unsigned int __latest_end__ ;
    /*external linker symbol OTA flash region*/
    extern unsigned int __reserved_start__ ;
    
    #define ADR_SectorBootloaderStart ((uint32_t)(&__bootloader_start__))
    #define ADR_SectorBootloaderEnd ((uint32_t)(&__bootloader_end_))
    
    #define ADR_SectorSxStart ((uint32_t)(&__sx_start__))
    #define ADR_SectorSxEnd ((uint32_t)(&__sx_end__))
    #define ADR_SectorStableStart ((uint32_t)(&__stable_start__))
    #define ADR_SectorStableEnd ((uint32_t)(&__stable_end__))
    #define ADR_SectorLatestStart ((uint32_t)(&__latest_start__))
    #define ADR_SectorLatestoEnd ((uint32_t)(&__latest_end__))
    /*OTA communiaction sector*/
    #define ADR_Reserved ((uint32_t)(&__reserved_start__))
    

    问题是只有ADR_SectorSxStart 在我得到垃圾的其他变量中返回真实地址。
    这个变量取决于闪烁逻辑,因为长时间保护引导加载程序扇区的功能不起作用,在将地址硬编码到功能后一切正常。我需要让其余的定义工作。知道为什么只有一个变量正确引用吗?
    谢谢你。

    @大忙人
    地图文件也得到错误的地址:

    .sx             0x0000000008004000        0x0
                    0x0000000008004000                . = ALIGN (0x4)
                    0x0000000008004000                __sx_start__ = .
     *(.sx*)
                    0x0000000008004000                . = ALIGN (0x4)
                    0x0000000008004000                __sx_end__ = .
                    0x0000000000000001                ASSERT ((LENGTH (FLASH_SX) >= (__sx_end__ - __sx_start__)), sector sx overflow)
    
    .common         0x000000000800c000        0x0
                    0x000000000800c000                . = ALIGN (0x4)
                    0x000000000800c000                __common_start__ = .
     *(.common*)
                    0x000000000800c000                . = ALIGN (0x4)
                    0x000000000800c000                __common_end__ = .
                    0x0000000000000001                ASSERT ((LENGTH (FLASH_COMMON) >= (__common_end__ - __common_start__)), sector common overflow)
    
    .bootloader     0x0000000008030a54        0x0
                    0x0000000008030a54                . = ALIGN (0x4)
                    0x0000000008030a54                __bootloader_start__ = .
     *(.bootloader*)
                    0x0000000008030a54                . = ALIGN (0x4)
                    0x0000000008030a54                __bootloader_end__ = .
                    0x0000000000000001                ASSERT ((LENGTH (FLASH_BOOTLOADER) >= (__bootloader_end__ - __bootloader_start__)), sector bootloader overflow)
    
    .stable         0x0000000008040000        0x0
    

【问题讨论】:

  • 地图文件说什么?它应该包含链接器产生的一些提示。 - 如果您通过最小化它来消除一些“噪音”,您可以大大增强您的问题。可能两个部分(等等)就足够了。
  • 我已经扩展了我的问题并从 .map 添加了 sn-p 我已经使用硬编码的地址测试了这段代码,一切都很好。它启动等。当我做一个内存查看器时。引导加载程序放置在正确的地址。
  • __bootloader_start__ 地址有什么问题? __common_start__ 相同,它是有效的(除了 .common 部分看起来是空的)
  • SECTOR_BOOTLOADER 设置为与地图文件显示不同的地址。

标签: c linker stm32 stm32cubeide


【解决方案1】:

一段时间后,我找到了答案。像往常一样,它在我眼前是纯文本。 事实是 SECTOR_BOOTLOADER 的地址实际上被正确放置,告诉编译器在哪里放置所有 .text 变量。

当您逐行仔细查看时,您会看到我实际上已经创建了内存区域 FLASH_BOOTLOADER,然后将其用作我的主程序地址。这是正确的。比我使用链接器放置所有 .text 变量,然后我创建了我的 REGION BOOTLOADER。

这个错误是由于不了解链接器脚本和命名问题造成的。 谢谢你的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多