英文:
ARM GCC force variable to be loaded in flash even if unused
问题
Linker script memory definition
/* 指定存储器区域 */
MEMORY
{
FLASH_FSBL (rx) : ORIGIN = 0x08000000, LENGTH = 126K
FLASH_FSBL_AESKEY (rx) : ORIGIN = 0x0801F900, LENGTH = 256
}
I defined my own region for AES key
/* AES密钥的区域 */
.aes_key_sect :
{
PROVIDE(region_aes_key_start = .);
KEEP (*(.aes_key_sect))
PROVIDE(region_aes_key_end = .);
} > FLASH_FSBL_AESKEY
In boot code, I have:
#define LOCATION_AES_KEY __attribute__((section(".aes_key_sect")))
LOCATION_AES_KEY static uint8_t aeskey_const[] = {
#include "../../../aes256_key.txt";
};
When compiled, I see that it was not included in the build
FLASH_FSBL: 24444字节 126千字节 18.95%
FLASH_FSBL_AESKEY: 0千字节 0千字节
The only way to do it so far, is to dummy-access it in the init function, like so:
void init() {
const volatile uint8_t* k1 = aeskey_const;
(void)*k1;
}
When compiled, I see it is part of build:
Memory region Used Size Region Size %age Used
FLASH_FSBL: 24452字节 126千字节 18.95%
FLASH_FSBL_AESKEY: 32字节 0千字节
is there no better way than this? if I change definition to const
, there is no effect.
英文:
I have a AES variable that would like to force-load it to specific address in the bootloader application (Cortex-M7 based app). ARM GCC 10.3.1 compiler. it will be used in the later stage, by the main app. It is considered UNUSED in the bootloader app.
Linker script memory definition
/* Specify the memory areas */
MEMORY
{
FLASH_FSBL (rx) : ORIGIN = 0x08000000, LENGTH = 126K
FLASH_FSBL_AESKEY (rx) : ORIGIN = 0x0801F900, LENGTH = 256
}
I defined my own region for AES key
/* Section for AES key */
.aes_key_sect :
{
PROVIDE(region_aes_key_start = .);
KEEP (*(.aes_key_sect))
PROVIDE(region_aes_key_end = .);
} >FLASH_FSBL_AESKEY
In boot code, I have:
#define LOCATION_AES_KEY __attribute__((section(".aes_key_sect")))
LOCATION_AES_KEY static uint8_t aeskey_const[] = {
#include "../../../aes256_key.txt"
};
When compiled, I see that it was not included in the build
FLASH_FSBL: 24444 B 126 KB 18.95%
FLASH_FSBL_AESKEY: 0 GB 0 GB
The only way to do it so far, is to dummy-access it in the init function, like so:
void init() {
const volatile uint8_t* k1 = aeskey_const;
(void)*k1;
}
When compiled, I see it is part of build:
Memory region Used Size Region Size %age Used
FLASH_FSBL: 24452 B 126 KB 18.95%
FLASH_FSBL_AESKEY: 32 B 0 GB
is there no better way than this? if I change definition to const
, there is no effect.
答案1
得分: 1
GCC提供了C标准中未定义的特殊属性,正如您显然知道的那样。其中一个特殊属性是"used",它告诉编译器对象实际上是需要的,并防止编译器将其删除。在您的情况下使用如下:
__attribute__((__used__)) LOCATION_AES_KEY static uint8_t aeskey_const[] = {
#include "../../../aes256_key.txt";
};
英文:
GCC provides special attributes not defined in the C standards, as you apparently know. One such attribute is "used" that tells the compiler the object is in fact needed and prevents it from removing it. The usage in your case would be :
__attribute__((__used__)) LOCATION_AES_KEY static uint8_t aeskey_const[] = {
#include "../../../aes256_key.txt"
};
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论