excluding constant variable from optimization
David Brown
david@westcontrol.com
Fri Jan 11 12:39:00 GMT 2013
On 10/01/13 19:32, Marcin S wrote:
> Hello,
>
> I have a program for ARM microcontroller (barebone STM32). Default
> linker script puts all global const variables in .text block, which is
> later loaded into flash memory of the cpu. Processor I am using have
> an ability to change contents of flash on the fly, what I want to
> achieve is to read const variable from flash, that might been changed
> from other location of the program.
>
> Example
>
> const uint16_t SIGNATURE = 0xA1A1; //default value
>
> bool getSignature()
> {
> return SIGNATURE == 0xA1A1;
> }
>
> void main(void)
> {
> if(getSignature())
> {
> printf("NOT CHANGED");
> }else
> {
> printf("CHANGED");
> }
> }
>
>
> And this works well, but only with disabled optimizations -O0,
> however, with -O1+ it all gets "eaten" into simple printf("NOT
> CHANGED) because variable is constant and under normal circumstances
> result is always the same.
> I have tried various tricks to avoid optimizing of that varaible, and
> some are working, but I don't like it and it gives me no guarantee it
> will work in the future.
>
> Is there any way to ensure this variable allocation in .text as it's
> in source file (other then expilicitly disabling optimization for unit
> it is defined in)?
>
> Marcin
>
Have you tried declaring the data as "volatile const"?
mvh.,
David
More information about the Gcc-help
mailing list