【问题标题】:Initialized vs uninitialized global variables, in which part of the RAM are the stored?已初始化与未初始化的全局变量,存储在 RAM 的哪一部分?
【发布时间】:2021-04-13 12:20:12
【问题描述】:

问题是当我这样定义数组时:

float Data_Set_X[15000];
float Data_Set_Y[15000];
float Data_Set_Z[15000];

我收到 RAM 溢出错误:.bss will not fit in region RAM Timer-Blink-Test_CM7 C/C++ 问题

当我初始化至少一个或三个数组时,错误将消失。

float Data_Set_X[15000]={0};
float Data_Set_Y[15000];
float Data_Set_Z[15000];

我的变量是全局的。

在链接描述文件中写道:

/* Specify the memory areas */ 

MEMORY
{
    RAM_EXEC (rx)   : ORIGIN = 0x24000000, LENGTH = 256K   
    RAM (xrw)   : ORIGIN = 0x24040000, LENGTH = 256K     
}

/* The startup code goes first into RAM_EXEC */
/* The program code and other data goes into RAM_EXEC */
/* Constant data goes into RAM_EXEC */
/* Initialized data sections goes into RAM, load LMA copy after code */

根据链接描述文件,/* Uninitialized data section */ 有一个单独的 RAM 部分。

RAM 大小为 1MB,用户可以访问大约 800KB。 MCU有双核,我使用M7 Core。如链接器脚本文件中所述,该内核可以访问 512KB RAM 区域。这三个数组的总大小是180KB

这是我的微控制器的链接器脚本文件

    /*
******************************************************************************
**
**  File        : LinkerScript.ld
**
**
**  Abstract    : Linker script for STM32H7 series
**                256Kbytes RAM_EXEC and 256Kbytes 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.
**
*****************************************************************************
** @attention
**
** Copyright (c) 2019 STMicroelectronics.
** All rights reserved.
**
** This software component is licensed by ST under BSD 3-Clause license,
** the "License"; You may not use this file except in compliance with the
** License. You may obtain a copy of the License at:
**                        opensource.org/licenses/BSD-3-Clause
**
****************************************************************************
*/


/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_estack = 0x24080000;    /* 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 */

/* Specify the memory areas */ 
MEMORY
{
RAM_EXEC (rx)      : ORIGIN = 0x24000000, LENGTH = 256K    
RAM (xrw)      : ORIGIN = 0x24040000, LENGTH = 256K         
}

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

  /* The program code and other data goes into RAM_EXEC */
  .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 */
  } >RAM_EXEC

  /* Constant data goes into RAM_EXEC */
  .rodata :
  {
    . = ALIGN(4);
    *(.rodata)         /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)        /* .rodata* sections (constants, strings, etc.) */
    . = ALIGN(4);
  } >RAM_EXEC

  .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >RAM_EXEC
  .ARM : {
    __exidx_start = .;
    *(.ARM.exidx*)
    __exidx_end = .;
  } >RAM_EXEC

  .preinit_array     :
  {
    PROVIDE_HIDDEN (__preinit_array_start = .);
    KEEP (*(.preinit_array*))
    PROVIDE_HIDDEN (__preinit_array_end = .);
  } >RAM_EXEC
  .init_array :
  {
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(SORT(.init_array.*)))
    KEEP (*(.init_array*))
    PROVIDE_HIDDEN (__init_array_end = .);
  } >RAM_EXEC
  .fini_array :
  {
    PROVIDE_HIDDEN (__fini_array_start = .);
    KEEP (*(SORT(.fini_array.*)))
    KEEP (*(.fini_array*))
    PROVIDE_HIDDEN (__fini_array_end = .);
  } >RAM_EXEC

  /* 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> RAM_EXEC

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

谢谢!

【问题讨论】:

  • float Data_Set_X[15000]={0}; 在全局范围内定义时等同于 float Data_Set_X[15000];。所以这里有些不对劲。请发帖minimal reproducible example
  • @EugeneSh。较旧的 gcc 版本将零初始化变量放入 .data 部分。
  • @P__JsupportswomeninPoland 真的吗?你知道他们什么时候停下来的吗?
  • @EugeneSh。前一段时间。不记得版本。我的意思是显式初始化为零,例如int x = 0;
  • @EugeneSh。我也经历过这个。 ,arm-gcc 在早期的 4.x 范围内(可能是 4.1?)。 char x[10000] = { 0 }; 会将二进制文件的大小增加 10K 。我的代码库还必须使用另一个编译器构建,它不会对未初始化的静态变量进行零初始化,所以我最终不得不定义一个宏 ZERO_INITIALIZED,它在 gcc 上扩展为空白,在另一个平台上扩展为 = { 0 }

标签: c++ arrays c arm


【解决方案1】:

然后使用此链接描述文件,初始化的静态存储对象将存储在RAM 内存部分中,因为.data 段存储在那里。

初始值将存储在RAM_EXEC 内存部分。启动代码必须将此数据从RAM_EXEC 复制到RAM

问题是某些东西(可能是调试探针或引导加载程序)必须首先对RAM_EXEC 内存部分进行编程

未初始化的静态存储对象将存储在RAM 内存部分中,因为.bss 部分位于那里。启动代码必须将它们归零

最好的方法是查看内存中的实际内容和位置。生成 .map 文件以查看确切位置(您可以增加 RAM 的大小以传递链接。它将不是有效的可执行文件,但您将能够生成 .map 文件)

【讨论】:

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