This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Treat a non-const custom data section as const in FLASH

I'm working with an ARM Cortex-M4 using the TI CGT tools.  I'm working with a large set of legacy code that has a lot of global initialized data that is never changed at run time and would otherwise be marked as "const".  The issue is that there are so many globals, we are looking to use a section pragma "SET_DATA_SECTION("mysect") on whole files to relocate its structures to FLASH at link time in order to save precious RAM.  Yes, I know the propper fix is to add the const attibute to each global object, but there are so many of them, we are trying to save some effort.

So we have some code like such:

#pragma SET_DATA_SECTION("mysect")

int var1 = 2;
int var2 = 4;
int var3 = 5;
...etc...

Next in our linker script I tried something like:

SECTIONS
{
...

    mysect : LOAD = FLASH_CODE
...
}

This does not seem to work, and after reviewing the following Wiki pages, I could not find a solution:

What I need to have the linker do is put the constant data into FLASH when the *.out file is loaded, yet I need to make sure that it is not initialized like a .data section would be in the C run time startup.

Thoughts?

Stuart

  • Unfortunately, the const keyword is the only way to get the result you want.

    I suppose you could try something like this ...

    #define int const int
    // lots of int global variables initialized here
    #undef int
    // ordinary code resumes

    But that is very underhanded and likely to backfire somehow.

    Thanks and regards,

    -George