GCC编译器如何把常量编译到固定的地址(针对ARM-Cortex-M系列)

实验背景:IDE为 S32 Design Studio for ARM

编译器为:gcc-6.3-arm32-eabi

MCU:S32k144

1.修改ld文件:

/* Specify the memory areas */
MEMORY
{
  
	/* Flash */
	m_interrupts 	(RX) : ORIGIN = 0x00000000, LENGTH = 0x00000400
	m_flash_config 	(RX) : ORIGIN = 0x00000400, LENGTH = 0x00000010
	m_text 			(RX) : ORIGIN = 0x00000410, LENGTH = 0x0002A9E0
	m_config 		(RX) : ORIGIN = 0x0002BA00, LENGTH = 0x00000100


  /* SRAM_L */
  m_data                (RW)  : ORIGIN = 0x1FFF8000, LENGTH = 0x00008000

  /* SRAM_U */
  m_data_2              (RW)  : ORIGIN = 0x20000000, LENGTH = 0x00007000
  
}

注意flash段的每个段的地址范围不能重复。

2.SECTIONS加入用户自定义的段:

/* Define output sections */
SECTIONS
{
  /* The startup code goes first into internal flash */
  
  /* The program code and other data goes into internal flash */
  .text :
  {
    . = ALIGN(4);
    *(.text)                 /* .text sections (code) */
    *(.text*)                /* .text* sections (code) */
    *(.rodata)               /* .rodata sections (constants, strings, etc.) */
    *(.rodata*)              /* .rodata* sections (constants, strings, etc.) */
    *(.init)                 /* section used in crti.o files */
    *(.fini)                 /* section used in crti.o files */
    *(.eh_frame)             /* section used in crtbegin.o files */
    . = ALIGN(4);
  } > m_text

	.const_var :    /*用户自定义空间 */
	{
		. = ALIGN(4);
		KEEP(*(.m_const_config))
	} > m_config

3.代码中可以这样定义,flash中是按顺序排列的。

const char config_alpha[11]	__attribute__ ((section(".m_const_config"))) = "Hello,casy!";
const int config_beta[8]	__attribute__ ((section(".m_const_config"))) = {1,2,3,4};

编译后,就可以从flash中看到对应内容了:

同时,也可以通过map文件查看:

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值