GNU链接器(LD):输入分区的垃圾回收及保护处理(KEEP命令)介绍

0 参考资料

GNU-LD-v2.30-中文手册.pdf
GNU linker.pdf

1 前言

一个完整的编译工具链应该包含以下4个部分:
(1)编译器
(2)汇编器
(3)链接器
(4)lib库
在GNU工具链中,对应的是:
(1)编译器:GCC(GNU Compiler Collection,GNU编译器套件)
(2)汇编器:GAS(GNU Assembler,GNU汇编器)
(3)链接器:LD(GNU Linker,GNU链接器)
(4)lib库:glibc(GNU C Library,GNU C 库)
本文介绍GNU链接器(LD)链接器脚本中输入分区的垃圾回收及保护处理(KEEP命令)。

2 GNU链接器(LD):输入分区的垃圾收集处理(KEEP命令)介绍

当我们的编译命令添加了’–gc-sections’命令选项,链接时会进行垃圾收集,可能会将不需要被当做垃圾处理的分区处理掉。为了避免这种情况发生,可以使用KEEP()命令将不需要淘汰的输入分区保护起来。例子如下:

KEEP(*(.init))

以上KEEP命令保护.init段,.init段会被libc_nano(精简版C库)使用到,要避免被优化掉。

/* lcf file for MPC5634M processor */ /* */ /* 1.5 MB Flash, 94KB SRAM */ MEMORY { resetvector: org = 0x00000000, len = 0x00000008 APP_ENTRY: org = 0x00010000, len = 0x00000100 init: org = 0x00010100, len = 0x00000F00 exception_handlers: org = 0x00011000, len = 0x00001000 internal_flash: org = 0x00012000, len = 0x0006C800 /*434K*/ crc32_flash: org = 0x0007E800, len = 0x00001800 /*6K*/ ASW_flash_A: org = 0x00080000, len = 0x00060000 /*384K*/ cal_flash_A: org = 0x000E0000, len = 0x00020000 /*128K*/ ASW_flash_B: org = 0x00100000, len = 0x00060000 /*384K*/ cal_flash_B: org = 0x00160000, len = 0x00020000 /*128K*/ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ cal_ram: org = 0x40000000, len = 0x00008000 /*32K*/ internal_ram: org = 0x40008000, len = 0x0000A800 /*42K*/ crc32_ram: org = 0x40012800, len = 0x00001800 /*6K*/ heap : org = 0x40014000, len = 0x00002000 /*8K*/ stack : org = 0x40016000, len = 0x00001800 /*6K*/ } /* This will ensure the rchw and reset vector are not stripped by the linker */ FORCEACTIVE { "bam_rchw" "bam_resetvector" } SECTIONS { .__bam_bootarea LOAD (0x00000000): {} > resetvector .app_entry (VLECODE) LOAD(0x00010000) : {} > APP_ENTRY GROUP : { .init LOAD (0x10100) : {} .init_vle (VLECODE) LOAD (_e_init) : { *(.init) *(.init_vle) } } > init .__exception_handlers (VLECODE) LOAD (0x00011000) : {} > exception_handlers GROUP : { .text : {} .text_vle (VLECODE) ALIGN(0x08): { *(.text) *(.text_vle) } .rodata (CONST) : { *(.rdata) *(.rodata) } .ctors : {} .dtors : {} extab : {} extabindex : {} } > internal_flash /************将应用层代码和const数据放在ASW_flash************/ GROUP : { .ASW_text (VLECODE) ALIGN(0x08) LOAD (0x00080000): { KEEP(*(.ASW_function)) Sensor.o(.text) EEpromSaveVal.o(.text) CanSend.o(.text) CanReceive.o(.text) BKLrn.o(.text) ClTrqMapLrn.o(.text) TmPosCal.o(.text) EngMotCtrl.o(.text) ClCtrl.o(.text) SlopeEst_Single.o(.text) VehCtl.o(.text) SftMotPidCtrl.o(.text) ClMotPidCtrl.o(.text) TrgtGearSub.o(.text) SftCtrl.o(.text) SftCtrl_TrgtCurrSub.o(.text) TempEst.o(.text) AveCalculate_DBzCpi0h.o(.text) BINARYSEARCH_real32_T.o(.text) InValStdJdgSub_EJoZRQLM.o(.text) InValStdJdgSub_INONT01g.o(.text) look1_iflf_binlcapw.o(.text) look1_iflf_binlxpw.o(.text) look2_iflf_binlcapw.o(.text) look2_iflf_binlcpw.o(.text) look2_iflf_binlxpw.o(.text) LookUp_real32_T_real32_T.o(.text) Myfun_TimeChr2eWq0hbv.o(.text) rt_nonfinite.o(.text) rtGetInf.o(.text) rtGetNaN.o(.text) } .ASW_data (CONST) : { Sensor_data.o(.rodata) const_params.o(.rodata) ClCtrl.o(.rodata) VehCtl_data.o(.rodata) TempEst_data.o(.rodata) } } > ASW_flash_A GROUP : { .__uninitialized_intc_handlertable ALIGN(2048) : {} .data : {} .sdata : {} .sbss : {} .sdata2 : {} .sbss2 : {} .bss : {} } > internal_ram .crc32_section LOAD (0x0007E800): { KEEP(*(.crc32_section)) } > crc32_ram /************param_cal************/ .cal_section LOAD (0x000E0000) : { KEEP(*(.cal_section)) } > cal_ram } /* Freescale CodeWarrior compiler address designations */ _stack_addr = ADDR(stack)+SIZEOF(stack); _stack_end = ADDR(stack); _heap_addr = ADDR(heap); _heap_end = ADDR(heap)+SIZEOF(heap); /* Exceptions Handlers Location (used in Exceptions.c IVPR initialization)*/ EXCEPTION_HANDLERS = ADDR(exception_handlers); /* L2 SRAM Location (used for L2 SRAM initialization) */ L2SRAM_LOCATION = 0x40000000; ~~~~~~~~~~~ mpc5634链接文件flash分区, 应用层程序对应的flash区域为:ASW_flash_A/ASW_flash_B, 标定量对应的flash区域为:cal_flash_A/cal_flash_B, 都做了两个分区,即一个为激活分区,一个为备用分区,想实现对应的flash区域刷写失败后,可以切换到备用分区,我不太清楚具体怎么实现,在链接文件中,我只能指定代码或者标定参数放在ASW_flash_A,cal_flash_A中,没有办法同时放在两个段,A-B的数据刷写只能在刷写A之前写入到B中,那么在程序运行的时候,我怎么指定是运行A中或者B中的代码或者参数呢?标定参数上电是需要复制到ram中的,这个我可以处理,比较麻烦的是代码是直接在flash中运行的,怎么指定使用哪个区域的代码呢?
最新发布
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

NW嵌入式开发

感谢您的支持,让我们一起进步!

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

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

打赏作者

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

抵扣说明:

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

余额充值