我是靠谱客的博主 暴躁短靴,这篇文章主要介绍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文件查看:

最后

以上就是暴躁短靴最近收集整理的关于GCC编译器如何把常量编译到固定的地址(针对ARM-Cortex-M系列)的全部内容,更多相关GCC编译器如何把常量编译到固定内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(141)

评论列表共有 0 条评论

立即
投稿
返回
顶部