IAR目标代码4字节对齐

向工程添加文件

eof.c :

// 文件头
#if defined(__CC_ARM)
// MDK
//    uint32_t g_update_flag[2] __attribute__((zero_init, at(0x1000FFF0)));

    const unsigned long gc_eof __attribute__((used)) = 0xFFFFFFFFul;
#elif defined(__ICCARM__)
// IAR
    __root const unsigned long gc_eof = 0xFFFFFFFFul;
#endif

__root 是IAR的扩展关键字,强制编译,保证没有使用的函数或者变量也能够包含在目标代码中
文件名可以任取,但到和后面自定义的段对应起来。

修改IAR的链接脚本icf文件

/*###ICF### Section handled by ICF editor, don't touch! ****/
/*-Editor annotation file-*/
/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
/*-Specials-*/
define symbol __ICFEDIT_intvec_start__ = 0x08020400;
/*-Memory Regions-*/
define symbol __ICFEDIT_region_ROM_start__     = 0x08020000;
define symbol __ICFEDIT_region_ROM_end__       = 0x080FFFFF;
define symbol __ICFEDIT_region_RAM0_start__    = 0x20000000;
define symbol __ICFEDIT_region_RAM0_end__      = 0x2001BFFF;
define symbol __ICFEDIT_region_RAM1_start__    = 0x2001C000;
define symbol __ICFEDIT_region_RAM1_end__      = 0x2001FFFF;
define symbol __ICFEDIT_region_RAM2_start__    = 0x20020000;
define symbol __ICFEDIT_region_RAM2_end__      = 0x2002FFFF;
define symbol __ICFEDIT_region_TCMSRAM_start__ = 0x10000000;
define symbol __ICFEDIT_region_TCMSRAM_end__   = 0x1000FFFF;
/*-Sizes-*/
define symbol __ICFEDIT_size_cstack__ = 0x8000;
define symbol __ICFEDIT_size_heap__   = 0x8000;
/**** End of ICF editor section. ###ICF###*/
define symbol __ICFEDIT_region_RAM_start__     = __ICFEDIT_region_RAM0_start__;
define symbol __ICFEDIT_region_RAM_end__       = __ICFEDIT_region_RAM2_end__;
export symbol __ICFEDIT_region_RAM_end__;

define memory mem with size = 4G;
define region ROM_region        = mem:[from __ICFEDIT_region_ROM_start__      to __ICFEDIT_region_ROM_end__];
define region RAM_region        = mem:[from __ICFEDIT_region_RAM0_start__     to __ICFEDIT_region_RAM0_end__]
                                | mem:[from __ICFEDIT_region_RAM2_start__     to __ICFEDIT_region_RAM2_end__];
define region NETRAM_region     = mem:[from __ICFEDIT_region_RAM1_start__     to __ICFEDIT_region_RAM1_end__];
define region TCMSRAM_region    = mem:[from __ICFEDIT_region_TCMSRAM_start__  to __ICFEDIT_region_TCMSRAM_end__];

define block CSTACK    with alignment = 8, size = __ICFEDIT_size_cstack__   { };
define block HEAP      with alignment = 8, size = __ICFEDIT_size_heap__     { };
define block EOF       with alignment = 4, size = 4 {readonly object eof.o };

initialize by copy { readwrite };
do not initialize  { section .noinit };

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

//place in ROM_region     { readonly };
place in RAM_region     { readwrite,
                          block CSTACK, block HEAP }; 
place in ROM_region   { readonly, last block EOF};
/*place in ROM_region   { readonly, last object eof.o};*/
place in NETRAM_region  { section .netram };						
place in TCMSRAM_region { section .ccsram };

加入如下自定义段:

  • define block EOF with alignment = 4, size = 4 {readonly object eof.o }; 定义段,eof.o就是文件名
  • place in ROM_region { readonly, last block EOF}; 将定义的段放到目标代码的末尾

原理

利用编译器一般定义数据变量都是4字节对齐的原理,当生成的目标代码是奇数字节,这时候就不是4字节对齐,当给目标代码最后加入一个4字节对齐的变量时,按照4字节对齐原理,最后的奇数字节后面的位置就被空出来达到4字节对齐的目的
在这里插入图片描述
如上图所示,在最后强加4字节后,n+1这个字节就会空出来将前面的本来的奇数字节补齐成4字节。
无论最后是奇数是1个字节、2个字节、3个字节都是一样的,这里画演示图用3个字节。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欲盖弥彰1314

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值