public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* .text variable and optimization issue
@ 2012-07-04 17:56 Marcin S
  2012-07-04 20:08 ` Václav Zeman
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin S @ 2012-07-04 17:56 UTC (permalink / raw)
  To: gcc-help

Hello,

I'm trying to write a helper functions for arm microcontroller, that
will help to determine if program is run for the first time after
programming. I'm using STM32 chip, with ability to modify it's flash
memory at runtime.
The idea is to put const variable into flash memory (.text)  let's
name it SIGNATURE, and then, during initialization sequence check for
that variable value and eventually change it in flash under it's
address (&SIGNATURE). When program will be launched next time, this
variable will be already changed, letting you know that this is not a
first run.

Here is a simple  code snippet to do that

const uint16_t CFlashSignature::SIGNATURE = 0xFFFF; //initial value in flash.

bool CFlashSignature::isFristRun()
{
	return *(uint16_t*)&SIGNATURE; //it must be written that way,
otherwise program will not always notice change at SIGNATURE's address
}

void CFlashSignature::setSignature()
{
	if(isFristRun())
	{
		FLASH_Unlock();
		FLASH_ProgramHalfWord((uint32)&SIGNATURE,0); //this function writes
0 at signature's address
		FLASH_Lock();
		
	}
}


and those functions are working as intended, but inly with NO
optimization enabled, when i add any optimization (-Ox) isFirstRun()
method always returns true! as if

return *(uint16_t*)&SIGNATURE;

was translated to

return 0xffff;

or something, i'm not exactly sure.

I could disable optimization for this translation unit as a
workaround, but there is no guarantee it stays that way in future
versions of gcc or something.

So in general, I need 2 byte variable that will be always in .text and
it's address should obtainable at any point. Any suggestions how to
achieve it to be 100% it will be there?

Regards
Marcin.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: .text variable and optimization issue
  2012-07-04 17:56 .text variable and optimization issue Marcin S
@ 2012-07-04 20:08 ` Václav Zeman
  2012-07-04 20:30   ` Marcin S
  0 siblings, 1 reply; 5+ messages in thread
From: Václav Zeman @ 2012-07-04 20:08 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2179 bytes --]

On 07/04/2012 07:56 PM, Marcin S wrote:
> Hello,
>
> I'm trying to write a helper functions for arm microcontroller, that
> will help to determine if program is run for the first time after
> programming. I'm using STM32 chip, with ability to modify it's flash
> memory at runtime.
> The idea is to put const variable into flash memory (.text)  let's
> name it SIGNATURE, and then, during initialization sequence check for
> that variable value and eventually change it in flash under it's
> address (&SIGNATURE). When program will be launched next time, this
> variable will be already changed, letting you know that this is not a
> first run.
>
> Here is a simple  code snippet to do that
>
> const uint16_t CFlashSignature::SIGNATURE = 0xFFFF; //initial value in flash.
>
> bool CFlashSignature::isFristRun()
> {
> 	return *(uint16_t*)&SIGNATURE; //it must be written that way,
> otherwise program will not always notice change at SIGNATURE's address
> }
>
> void CFlashSignature::setSignature()
> {
> 	if(isFristRun())
> 	{
> 		FLASH_Unlock();
> 		FLASH_ProgramHalfWord((uint32)&SIGNATURE,0); //this function writes
> 0 at signature's address
> 		FLASH_Lock();
> 		
> 	}
> }
>
>
> and those functions are working as intended, but inly with NO
> optimization enabled, when i add any optimization (-Ox) isFirstRun()
> method always returns true! as if
>
> return *(uint16_t*)&SIGNATURE;
>
> was translated to
>
> return 0xffff;
>
> or something, i'm not exactly sure.
The compiler probably sees through your games. The language defines that
constants are really, constant, AFAIK. Try changing the 'const uint16_t
CFlashSignature::SIGNATURE' to 'const volatile uint16_t
CFlashSignature::SIGNATURE' or just drop the const entirely (and add
volatile).
>
> I could disable optimization for this translation unit as a
> workaround, but there is no guarantee it stays that way in future
> versions of gcc or something.
>
> So in general, I need 2 byte variable that will be always in .text and
> it's address should obtainable at any point. Any suggestions how to
> achieve it to be 100% it will be there?
>
-- 
VZ



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 294 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: .text variable and optimization issue
  2012-07-04 20:08 ` Václav Zeman
@ 2012-07-04 20:30   ` Marcin S
  2012-07-04 21:24     ` Ángel González
  0 siblings, 1 reply; 5+ messages in thread
From: Marcin S @ 2012-07-04 20:30 UTC (permalink / raw)
  To: Václav Zeman; +Cc: gcc-help

2012/7/4 Václav Zeman <vhaisman@gmail.com>:
> On 07/04/2012 07:56 PM, Marcin S wrote:
>> Hello,
>>
>> I'm trying to write a helper functions for arm microcontroller, that
>> will help to determine if program is run for the first time after
>> programming. I'm using STM32 chip, with ability to modify it's flash
>> memory at runtime.
>> The idea is to put const variable into flash memory (.text)  let's
>> name it SIGNATURE, and then, during initialization sequence check for
>> that variable value and eventually change it in flash under it's
>> address (&SIGNATURE). When program will be launched next time, this
>> variable will be already changed, letting you know that this is not a
>> first run.
>>
>> Here is a simple  code snippet to do that
>>
>> const uint16_t CFlashSignature::SIGNATURE = 0xFFFF; //initial value in flash.
>>
>> bool CFlashSignature::isFristRun()
>> {
>>       return *(uint16_t*)&SIGNATURE; //it must be written that way,
>> otherwise program will not always notice change at SIGNATURE's address
>> }
>>
>> void CFlashSignature::setSignature()
>> {
>>       if(isFristRun())
>>       {
>>               FLASH_Unlock();
>>               FLASH_ProgramHalfWord((uint32)&SIGNATURE,0); //this function writes
>> 0 at signature's address
>>               FLASH_Lock();
>>
>>       }
>> }
>>
>>
>> and those functions are working as intended, but inly with NO
>> optimization enabled, when i add any optimization (-Ox) isFirstRun()
>> method always returns true! as if
>>
>> return *(uint16_t*)&SIGNATURE;
>>
>> was translated to
>>
>> return 0xffff;
>>
>> or something, i'm not exactly sure.
> The compiler probably sees through your games. The language defines that
> constants are really, constant, AFAIK. Try changing the 'const uint16_t
> CFlashSignature::SIGNATURE' to 'const volatile uint16_t
> CFlashSignature::SIGNATURE' or just drop the const entirely (and add
> volatile).

Hi,
Yes, i did tried this before, unfortunately with this modifier
(volatile or omitting const at all) makes variable not to land in
.text (flash) but in RAM, so, this won't do me any good if flag will
reset after power down - i need it to be persistent, thats the whole
point.

>>
>> I could disable optimization for this translation unit as a
>> workaround, but there is no guarantee it stays that way in future
>> versions of gcc or something.
>>
>> So in general, I need 2 byte variable that will be always in .text and
>> it's address should obtainable at any point. Any suggestions how to
>> achieve it to be 100% it will be there?
>>
> --
> VZ
>
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: .text variable and optimization issue
  2012-07-04 20:30   ` Marcin S
@ 2012-07-04 21:24     ` Ángel González
  2012-07-04 21:45       ` Marcin S
  0 siblings, 1 reply; 5+ messages in thread
From: Ángel González @ 2012-07-04 21:24 UTC (permalink / raw)
  To: Marcin S, gcc-help

On 04/07/12 22:30, Marcin S wrote:
> 2012/7/4 Václav Zeman:
>> The compiler probably sees through your games. The language defines that
>> constants are really, constant, AFAIK. Try changing the 'const uint16_t
>> CFlashSignature::SIGNATURE' to 'const volatile uint16_t
>> CFlashSignature::SIGNATURE' or just drop the const entirely (and add
>> volatile).
> Hi,
> Yes, i did tried this before, unfortunately with this modifier
> (volatile or omitting const at all) makes variable not to land in
> .text (flash) but in RAM, so, this won't do me any good if flag will
> reset after power down - i need it to be persistent, thats the whole
> point.
Have you tried adding an explicit section attribute?

__attribute__ ((section (".text")));

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bsection_007d-function-attribute-2598


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: .text variable and optimization issue
  2012-07-04 21:24     ` Ángel González
@ 2012-07-04 21:45       ` Marcin S
  0 siblings, 0 replies; 5+ messages in thread
From: Marcin S @ 2012-07-04 21:45 UTC (permalink / raw)
  To: Ángel González; +Cc: gcc-help

2012/7/4 Ángel González <keisial@gmail.com>:
> On 04/07/12 22:30, Marcin S wrote:
>> 2012/7/4 Václav Zeman:
>>> The compiler probably sees through your games. The language defines that
>>> constants are really, constant, AFAIK. Try changing the 'const uint16_t
>>> CFlashSignature::SIGNATURE' to 'const volatile uint16_t
>>> CFlashSignature::SIGNATURE' or just drop the const entirely (and add
>>> volatile).
>> Hi,
>> Yes, i did tried this before, unfortunately with this modifier
>> (volatile or omitting const at all) makes variable not to land in
>> .text (flash) but in RAM, so, this won't do me any good if flag will
>> reset after power down - i need it to be persistent, thats the whole
>> point.
> Have you tried adding an explicit section attribute?
>
> __attribute__ ((section (".text")));
>
> http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bsection_007d-function-attribute-2598


Yes, when tried to put non-const var into .text, the attribute is
ignored and it lands in ram anyway :(
But i came up with new implementation, so far its working with any
level of optimization and it leaves little or no room for it.

const uint16_t CFlashSignature::SIGNATURE = 0xFFFF;
uint32_t CFlashSignature::addr = (uint32_t)&CFlashSignature::SIGNATURE;

bool CFlashSignature::isFristRun()
{
	return *(uint16_t*)addr;
}

CFlashSignature::TFlashUpdResult CFlashSignature::setSignature()
{
	if(isFristRun())
	{
		FLASH_Unlock();
                FLASH_ProgramHalfWord(addr,0);
		FLASH_Lock();
	}
}

I've introduced new  RW variable "uint32_t addr" located in ram, which
holds an address to SIGNATURE and its used for reading and writing
operations. It's in global scope and its loaded at run time, ATM i
don't see how this could be optimized.

Any thoughts on that solution?

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-07-04 21:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-04 17:56 .text variable and optimization issue Marcin S
2012-07-04 20:08 ` Václav Zeman
2012-07-04 20:30   ` Marcin S
2012-07-04 21:24     ` Ángel González
2012-07-04 21:45       ` Marcin S

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).