Keil Logo

ARM: UNINITIALIZED VARIABLES GET INITIALIZED


Information in this knowledgebase article applies to:

  • MDK-ARM

SYMPTOM

I have a section of memory that I do not want to be initialized. In my scatter file I have this region of memory marked as UNINIT. I am using __attribute__((section(""))) to place variables in the UNINIT region.

Scatter file:

LR_IROM1 0x08000000 0x00080000  {    ; load region size_region
  ER_IROM1 0x08000000 0x00080000  {  ; load address = execution address
   *.o (RESET, +First)
   *(InRoot$$Sections)
   .ANY (+RO)
  }
  RW_IRAM1 0x20000000 UNINIT 0x00000100  { ;no init section
        *(NoInit)
   }
  RW_IRAM2 0x20000100 0x0000FFF0  {                ;all other rw data
        .ANY(+RW +ZI)
  }
}

C Code:

unsigned long NI_longVar __attribute__( ( section( "NoInit")) ) ;


int     main( void )
{
        while( 1 )
        {
        }
}

The variable is being placed in the UNINIT region properly, however the memory contents of the variable are still being initialized. It appears as if the UNINIT tag in the scatter file is being ignored. Why is this?

CAUSE

For optimization reasons, global ZI variables of 8 bytes or less are placed into RW data sections.

Only ZI data in UNINIT sections remain un-initialized, however any RW data in the UNINIT section does get initialized. Since the variable in the code sample above is less than 8 bytes, it is being changed from ZI to RW which causes initialization to take place.

RESOLUTION

Add the zero_init attribute to the variable in order to prevent the optimization. This keeps the variable as ZI data which will not be initialized.

//armcc
unsigned long NI_longVar __attribute__( ( section( "NoInit"),zero_init) ) ;

//armclang
//For an armclang equivalent, use the following line in a C source file:
unsigned long NI_longVar __attribute__( ( section( ".bss.NoInit")) ) ;
//and modify one line of the scatterfile from:
//        *(NoInit)
//to:
//        *(.bss.NoInit)

MORE INFORMATION

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

FORUM THREADS

The following Discussion Forum threads may provide information related to this topic.

Last Reviewed: Monday, May 21, 2018


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.