This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Global variable not initialized by __main() function

Hi.

I hope this is the right place to ask to open this question.

I define an initialized variable in my C code, let's say:

...

int MyVariable = 0xDEADABBA;

...

Using a scatter file, I instruct the arm linker to locate my variable "MyVariable" at address 0x04020000 :

LR_RAM_1 RAM_1_START RAM_1_SIZE                  ; load region starts at 0x04020000

{                                                                                     ; start of execution region descriptions

   ER_SRAM8 RAM_1_START SRAM8_SIZE

   {

       * (+RW, +ZI)                                   ; All RW sections and All ZI sections are placed consecutively into this region

   }

....

}

I expect the __main() function to initialize "MyVariable" with value 0xDEADABBA at start time, before my application "main()" function is executed.

However, I can clearly see in my RTL simulation that, for some reasons, MyVariable is not initialized, as if the compiler/linker consider the execution region "ER_SRAM8" as being already initialized (basically as if it was a ROM memory).

How to instruct the ARM linker that any initialized variable placed in execution region "ER_SRAM8" must be initialzed by the __main() function at boot time since this correspond to a SRAM area which by definition does not contain any valid data at boot time ?

Thanks in advance for your answers.

ARM Linker, 5.03 [Build 24]

ARM C/C++ Compiler, 5.03 [Build 24]

Didier C.

Parents
  • I suspect your problem lies with having the LOAD location identical to the EXEC location (both being RAM_1_START).

    This implies that whatever "loaded" the image had already put the correct value at the "execution" location, i.e. you are relying on a debugger to do the loading, or the RAM already having the correct value.

    If you want __main() to do the work, then I believe the LOAD location must be different from the EXEC location (typically flash for LOAD and RAM for EXEC), i.e. there must be a reference copy of the value that can be copied to RAM during __main(), and your scatter file should look more like:

    LOAD_FLASH FLASH_START FLASH_SIZE
    {
        EXEC_CODE +0
        {
            * (+RO)
        }
        EXEC_RAM RAM_START RAM_SIZE
        {
            * (+RW, +ZI)
        }
    }
    

    In addition, the compiler may well optimise away MyVariable if it is not referenced.

    hth

    Simon.

Reply
  • I suspect your problem lies with having the LOAD location identical to the EXEC location (both being RAM_1_START).

    This implies that whatever "loaded" the image had already put the correct value at the "execution" location, i.e. you are relying on a debugger to do the loading, or the RAM already having the correct value.

    If you want __main() to do the work, then I believe the LOAD location must be different from the EXEC location (typically flash for LOAD and RAM for EXEC), i.e. there must be a reference copy of the value that can be copied to RAM during __main(), and your scatter file should look more like:

    LOAD_FLASH FLASH_START FLASH_SIZE
    {
        EXEC_CODE +0
        {
            * (+RO)
        }
        EXEC_RAM RAM_START RAM_SIZE
        {
            * (+RW, +ZI)
        }
    }
    

    In addition, the compiler may well optimise away MyVariable if it is not referenced.

    hth

    Simon.

Children