Re: excluding constant variable from optimization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2013/1/11 David Brown <david@xxxxxxxxxxxxxxx>:
> 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
>

I've ended up with non-const
volatile uint16_t CFlashSignature::SIGNATURE __attribute__ ((section
(".text"))) = 0xA1A1;
definition
and so far it's optimization resistant on all levels.

Thanks for suggestion guys, that thing was bugging me for some time.

Marcin


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux