Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue preventing GCC from optimizing out global variable

I am using ARM-GCC v4.9 (released 2015-06-23) for a STM32F105RC processor.
I've searched stackoverflow.com and I've found this in order to try to convince gcc not to optimize out a global variable, as you may see below:

static const char AppVersion[] __attribute__((used)) = "v3.05/10.oct.2015";

Yet, to my real surprise, the compiler optimized away the AppVersion variable!
BTW: I am using the optimize level -O0 (default).
I also tried using volatile keyword (as suggested on other thread), but it didn't work either :(
I already tried (void)AppVersion; but it doesn't work...
Smart compiler!? Too smart I suppose...

In the meantime, I use a printf(AppVersion); some place in my code, just to be able to keep the version... But this is a boorish solution :(
So, the question is: Is there any other trick that does the job, i.e. keep the version from being optimized away by GCC?

[EDIT]:
I also tried like this (i.e. without static):

const char AppVersion[] __attribute__((used)) = "v3.05/10.oct.2015";

... and it didn't work either :(

like image 856
סטנלי גרונן Avatar asked Oct 26 '15 17:10

סטנלי גרונן


People also ask

How do I enable optimization in GCC?

The -O level option to gcc turns on compiler optimization, when the specified value of level has the following effects: 0. The default reduces compilation time and has the effect that debugging always yields the expected result. This level is equivalent to not specifying the -O option at all.

How do I know if GCC is not optimized?

The gcc option -O enables different levels of optimization. Use -O0 to disable them and use -S to output assembly.

Does GCC optimize by default?

GCC has a range of optimization levels, plus individual options to enable or disable particular optimizations. The overall compiler optimization level is controlled by the command line option -On, where n is the required optimization level, as follows: -O0 . (default).

How do I keep my compiler from optimizing variables?

Select the compiler. Under the Optimization category change optimization to Zero. When done debugging you can uncheck Override Build Options for the file. In the latter case the volatile defined inside the function can get optimized out quite often. ...


1 Answers

Unfortunately I am not aware of a pragma to do this.
There is however another solution. Change AppVersion to:

static char * AppVersion = "v3.05/10.oct.2015";

and add:

__asm__ ("" : : "" (AppVersion));

to your main function.

You see I dropped the 'used' attribute, according to the documentation this is a function attribute.

Other solutions: Does gcc have any options to add version info in ELF binary file?

Though I found this one to be the easiest. This basically won't let the compiler and linker remove AppVersion since we told it that this piece of inline assembly uses it, even though we don't actually insert any inline assembly.

Hopefully that will be satisfactory to you.

Author: Andre Simoes Dias Vieira
Original link: https://answers.launchpad.net/gcc-arm-embedded/+question/280104

like image 63
סטנלי גרונן Avatar answered Oct 02 '22 00:10

סטנלי גרונן