将变量放置在特定地址会生成大的二进制文件 [英] Placing variables at specific address generates large binary file

查看:30
本文介绍了将变量放置在特定地址会生成大的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将数组放在内存中的特定地址.我正在使用 GCC.

I have to place array at specific address in memory. I'm using GCC.

我像这样声明变量:

uint8_t __attribute__((section (".mySection"))) buffer[1234];

在链接脚本中我有:

MEMORY
{
FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 1024K
RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 145K
MYSEC (x)       : ORIGIN = 0x20025000, LENGTH = 155K
}

及以后:

.mySection :
{
  *(.mySection);
} > MYSEC 

当然是嵌入式系统(ARM)的代码.通常我的程序需要 22 KB,经过这个修改需要 384 MB (!).

It's of course code for embedded system (ARM). Normally my program takes 22 KB, with this modification it takes 384 MB (!).

我不明白为什么.如果我删除 __attribute__ 它又需要 22 KB.我错过了什么?

I don't understand why. If I remove __attribute__ it takes 22 KB again. What am I missing?


使用的代码:

#inculde (...)

uint8_t __attribute__((section (".mySection"))) buffer = 5;

int main(void){
  buffer = 10;
}

完整的链接脚本(默认,不是我写的,部分被缩短:

Full linker script (default, not written by me, parts are shortened:

/* Entry Point */
ENTRY(Reset_Handler)

/* Highest address of the user mode stack */
_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 */

/* Specify the memory areas */
MEMORY
{
FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 1024K
RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 145K
MYSEC (x)       : ORIGIN = 0x20025000, LENGTH = 155K
}

/* Define output sections */
SECTIONS
{
  /* The startup code goes first into FLASH */
  .isr_vector :
  {
    (...)
  } >FLASH

  /* The program code and other data goes into FLASH */
  .text :
  {
    (...)
  } >FLASH

  /* Constant data goes into FLASH */
  .rodata :
  {
    (...)
  } >FLASH

  .ARM.extab   : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
  .ARM : {
    (...)
  } >FLASH

  .preinit_array     :
  {
    (...)
  } >FLASH
  .init_array :
  {
    (...)
  } >FLASH
  .fini_array :
  {
    (...)
  } >FLASH

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

  /* Initialized data sections goes into RAM, load LMA copy after code */
  .data : 
  {
    (...)
  } >RAM AT> FLASH



  .mySection :
  {
    *(.mySection);
  } > MYSEC 


  /* Uninitialized data section */
  . = ALIGN(4);
  .bss :
  {
    (...)
  } >RAM

  /* User_heap_stack section, used to check that there is enough RAM left */
  ._user_heap_stack :
  {
    (...)
  } >RAM

  /* Remove information from the standard libraries */
  /DISCARD/ :
  {
    (...)
  }

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

推荐答案

我假设您的输出文件是纯二进制格式,而不是 ELF,因此您没有 ELF 引导加载程序.在这种情况下,将初始化数据放在高地址显然会创建一个大的输出文件.这是因为在二进制格式中,您没有任何机制来提供特定地址,因此文件被 1:1 映射到内存中.这意味着直到您的自定义初始化变量的整个地址空间都需要包含在输出文件中.这与 .data 部分的情况相同,这就是为什么在启动时将 .data 部分从 Flash 显式复制到 RAM 区域的原因.

I assume your output file is a pure binary format, not an ELF so you don't have ELF bootloader. In that case, placing initialized data at high address obviously will create a big output file. This is because in the binary format you don't have any mechanism to provide specific address, so the file is mapped 1:1 into memory. It means the whole address space up to your custom initialized variable needs to be included in the output file. This is the same situation as with .data section and this is why the .data section is explicitly copied from Flash into RAM area at the startup.

附言你会发现我的文章很有帮助(这个问题在那里描述).

p.s. You can find my article helpful (this issue is described there).

这篇关于将变量放置在特定地址会生成大的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆