Why are my constants in flash (custom linker section) reduplicated and how can I configure this?
In order to be able to read-out the version numbers of my firmware (and some magic number) from the .hex file (by some external update tool), I define some constants to be written to a custom flash sector:
/*
* Constants which should go to specific locations in flash
*/
/** Firmware major version */
__attribute__((section(".inv_constants"))) \
static const uint8_t SW_VERSION_MAJOR{0};
__attribute__((section(".inv_constants"))) \
/** Firmware minor version */
__attribute__((section(".inv_constants"))) \
static const uint8_t SW_VERSION_MINOR{1};
/** Magic number for update tool */
__attribute__((section(".inv_constants"))) \
static const uint32_t MAGIC_NUMBER{0x18273645};In my linker script, I have added the following:
/* Memories definition */
MEMORY
{
INV_CONST (r) : ORIGIN = 0x08010008, LENGTH = 63K - 0xF
FLASH (rx) : ORIGIN = 0x08020000, LENGTH = 896K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw) : ORIGIN = 0x10000000, LENGTH = 64K
}.inv_constants :
{
KEEP(*(.inv_constants.SW_VERSION_MAJOR))
KEEP(*(.inv_constants.SW_VERSION_MINOR))
KEEP(*(.inv_constants.MAGIC_NUMBER))
KEEP(*(.inv_constants.*));
KEEP(*(.inv_constants));
} >INV_CONSTThis works for putting the variables in the output .hex file, but six times each:
:020000040801F1
:100008000000000000000101010101014536271828
:1000180045362718453627184536271845362718F0
:04002800453627181A
:020000040802F0
:1000000000000220B1DA0208F9CE020807CF020888This can be better seen in STM32CubeIDE's build analyzer:

I guess the reduplication of the variables in flash is due to padding, but for me it doesn't make sense.
The toolchain I'm using is GNU-Tools for STM32, version 7-2018-q2-update and I'm compiling/linking with g++ (C++ project).
Now comes the problem:
The previous projects were C projects and the same code resulted in 4 times reduplication (now 6 times) of the variables (actually constants) in the custom flash section (by the way, the other variables are placed in flash only once). Thus, the addresses where the other program looks for the constants in flash are no longer valid...
So, my question is, how can I influence the number of reduplication (at best, turn it off)?
Also, if anybody could help me understand why there is this reduplication, I would highly appreciate it (in case of padding I wouldn't understand why there is exactly 6 times reduplication here and why also the longest variable)...
What I tried so far:
- used ALIGN() instruction in the linker script within and before the custom section with different alignment sizes, but there was no change (neither with 2 nor with 8)
- inserted ". += 1" to insert some gap manually, but this only added another instance of the variables...
Anybody an idea?
Kind regards,
Markus
