I'm using CodeSourcery GCC for ARM and have this problem where when I enable any level of optimization (1,2 or 3), this struct I have defined as const is being optimized out. It's a block of information that I have deliberately put into a certain part of flash, so it's not accessed by it's struct name, but by address. GCC insists on removing it unless I use -o0. I have tried to fool it by assigning another variable this struct's address, but it doesn't work. Is there an easy way to simply tell GCC not to optimize this away?
You can try to use GCC _attributes_ to mark the structs as "used":
struct x y __attribute__((__used__));
Darn, didn't work. No warning or errors, it still discards the struct. There doesn't seem to be anything in the documentation for CS GCC about this, however.
What about adding a pseudo-assembler instruction that uses the address of the variable "my_struct"; something like:
__asm__ __volatile__("" :: "m" (my_struct));
Cool, that worked, thank you.
Jerry Milner wrote: > Darn, didn't work. No warning or errors, it still discards the struct. What is "it"? Compiler or linker? The attribute only tells the compiler that the variable is used, the linker can still toss it out. Check if the variable is in the object file. I am not a GNU expert, but it would appear that the ld-option `--print-gc-sections' gives you a report about removed input sections. Regards Marcus http://www.doulos.com/arm/
I don't know if the linker or compiler discarded it, but after adding the _asm_ statement, it appears in the .map file and most importantly, the code now works with optimization on. The struct in question contains strings that are displayed in a menu, so it's obvious when this struct is missing.