c - IAR 链接器在初始化期间将代码从 FLASH 复制到 RAM

标签 c linker scatter iar linker-scripts

我想在 RAM 中运行代码并使用 initialize copy by 指令。

我的.icf文件如下:

define symbol __ICFEDIT_intvec_start__ = 0x08000000;
/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__  = 0x08000000;
define symbol __ICFEDIT_region_ROM_end__    = 0x080007ff;
define symbol __ICFEDIT_region_ROM1_start__ = 0x08070000;
define symbol __ICFEDIT_region_ROM1_end__   = 0x0807FFFF;

define symbol __ICFEDIT_region_RAM_start__  = 0x20000000;
define symbol __ICFEDIT_region_RAM_end__    = 0x2000FFFF;

define symbol __ICFEDIT_size_cstack__ = 0x600;
define symbol __ICFEDIT_size_heap__   = 0x00;

/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x600;
define symbol __ICFEDIT_size_heap__   = 0x00;
/**** End of ICF editor section. ###ICF###*/

define memory mem with size = 4G;
define region ROM_region   = mem:[from __ICFEDIT_region_ROM_start__   to __ICFEDIT_region_ROM_end__];
define region ROM1_region  = mem:[from __ICFEDIT_region_ROM1_start__  to __ICFEDIT_region_ROM1_end__];
define region RAM_region   = mem:[from __ICFEDIT_region_RAM_start__   to __ICFEDIT_region_RAM_end__];

define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };

place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };

do not initialize  { section .noinit };

initialize by copy {readwrite};

initialize by copy {
                           object gd32f403_it.o,
                           object gd32f403_exti.o,
                           object gd32f403_fmc.o,
                           object gd32f403_gpio.o,
                           object gd32f403_misc.o,
                           object gd32f403_pmu.o,
                           object gd32f403_rcu.o,
                           object gd32f403_timer.o,
                           object gd32f403_usart.o,
                           object usb_delay.o,
                           object iap_core.o,
                           object usb_core.o,
                           object usbd_core.o,
                           object usbd_int.o,
                           object usbd_std.o,
                           object app.o ,
                   };

place in ROM_region   { 
                        readonly object system_gd32f403.o,
                       };
place in ROM1_region   { 
                           readonly object app.o ,
                           readonly object usb_delay.o,
                           readonly object iap_core.o,
                           readonly object gd32f403_it.o,
                           readonly object gd32f403_exti.o,
                           readonly object gd32f403_fmc.o,
                           readonly object gd32f403_gpio.o,
                           readonly object gd32f403_misc.o,
                           readonly object gd32f403_pmu.o,
                           readonly object gd32f403_rcu.o,
                           readonly object gd32f403_timer.o,
                           readonly object gd32f403_usart.o,
                           readonly object usb_core.o,
                           readonly object usbd_core.o,
                           readonly object usbd_int.o,
                           readonly object usbd_std.o,
                           readonly,
                        };
place in RAM_region   { 
                        readwrite,
                        block CSTACK, block HEAP,
                        };

export symbol __ICFEDIT_region_RAM_start__;
export symbol __ICFEDIT_region_RAM_end__;

但只有 gd32f403_gpio.ogd32f403_misc.o 在 RAM 中执行,其他对象仍在 FLASH 中运行,如输出 .map 文件所示:

"P3", part 1 of 3:                         0x54c
  P3-1                        0x20000000   0x54c  <Init block>
    ...
    .rodata          inited   0x20000000    0x40  app.o [1]
    .rodata          inited   0x20000040    0x44  app.o [1]
    .rodata          inited   0x20000130     0x8  gd32f403_rcu.o [1]
    .rodata          inited   0x20000138    0x14  iap_core.o [1]
    .rodata          inited   0x2000014c     0x4  iap_core.o [1]
    .text            inited   0x20000150    0xc6  gd32f403_gpio.o [1]
    .text            inited   0x20000218    0xd0  gd32f403_misc.o [1]
    .data            inited   0x200002e8   0x120  app.o [1]
    .data            inited   0x20000408    0x30  iap_core.o [1]
    .data            inited   0x20000504    0x48  xfiles.o [3]
    ...
                            - 0x2000054c   0x54c

为什么app.ogd32f403_usart.o等对象没有被复制到RAM中执行?

请!

如果想实现.icf文件中的copy code to RAM的目的,应该怎么办?

最佳答案

Sections that are needed for initialization are not affected by the initialize by copy directive. This includes the __low_level_initfunction and anything it references.

Anything reachable from the programentry label is considered needed for initialization unless reached via a section fragment with a label starting with __iar_init$$done.

关于c - IAR 链接器在初始化期间将代码从 FLASH 复制到 RAM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58641335/

相关文章:

c++ - 在 MSVC++ 2012 下使用 C++Amp 将程序与库链接时未解析的符号

c++ - VS2010 无法链接它应该构建的库

c++ - Apple Mach-O Linker 错误,我不知道该怎么办

google-visualization - 如何让 Google Charts 在散点图中显示多种颜色?

c - 解码 C 中的位

c - 如何从头文件中的struct Hold打印char值?

php - 为什么 phpseclib 生成不兼容的证书?

java - android jni c++ UnsatisfiedLinkError

excel - 使用 vbscript 在 excel 中创建散点图