I have the following string declared as a constant in my code. The purpose is to provide a crude and simple way of storing simple metadata in the compiled output.
const char myString1[] ="abc123\0";
const char myString2[] = {'a','b','c','1','2','3','\0'};
When I inspect the output with a hex editor, I see other string constants but “abc123” does not appear. This leads me to believe that the optimizations that are enabled are causing the lines not to be compiled, as they are never referenced in the program.
Is there a way in code to force this to compile, or another way (in code) of getting this metadata into the binary? I don’t want to do any manipulation of the binary post-compile, the goal is to keep it as simple as possible.
compiler flags
-O2 -g -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -mcpu=cortex-m3 -mthumb
I’ve come up with a solution that uses attributes and involves modifying the link script.
First I define a custom section called “.metadata”.
Then, in the
SECTIONSblock of the .ld script I added aKEEP(*(.metadata))which will force the linker to include.metadataeven if it’s not usedNOTE
I found that the
__attribute__keyword had to be on the same line as the variable or else it didn’t actually show up in the binary, though the .metadata section did show up in the memory map.