I am porting an application that is heavily dependent on being able to store data in address range 0x101000 to 0x1010ff and also in range 0x10D800 to 0x10FDFF.
I made the following declarations in my Main.c
#pragma location = 0x101000
const unsigned char cProvDataTable[256];
#pragma location = 0x10D800
const unsigned char cIndexTable[1024];
These spaces are linked at the appropriate addresses and I can read and write to the address range via my FLASH routines. The old app references the addresses directly (I know it sucks but it is not my app, I am just hired to port it).
The problem becomes if I enable Optimization, the linker detects that I am not really using these and optimizes them away and links code into these address ranges.
It depends heavily on your compiler (#pragma’s are compiler-specific anyway) so have a real good look in the compiler documentation.
What compiler are you using anyway?
For GCC, I think you can specify your own section in the link file and then use an attribute in the source files to put a specific variable in that section. Something like:
Yeah, indeed that would be much easier. Simply referencing the data should stop the compiler from optimising it away. So where the data is used, simply try something like
I was considering using Segment definitions but there are three or four of these tables and I didn’t want to make things too complicated in the linker file.
I am using IAR’s compiler with ATMEL’s at91sam7s as the target chip.
Amazingly, even with some dummy assignments in the code, IAR’s optimizer eliminates the cProvTable. I will try the “volatile” attribute. I was hoping to avoid writing a bunch of code to make the cProvTable look like it is used.
And as I said, I am hired to do a simple port, so rewriting the code to eliminate the references to 0x101000 and make it look like code written by a software professional is not what my client desires.
GCC has a attribute((unused)) (or something like that) specifically to deal with this situation; if volatile doesnt work then you could try looking for an IAR equivalent.