AnsweredAssumed Answered

Startup: why is .data alignment = 8

Question asked by robitzki.torsten on Apr 8, 2016
Latest reply on Apr 8, 2016 by dembek.radoslaw.001
Hello,
while writing a bootloader, I had the need for a few bytes (4) that retain the content during a software reset. I’ve read that is true for the first bytes of the SRAM. So I’ve added a new section at the beginning of the RAM to my linker scripted. Everything looked great until I’ve stumbled over a wrong initialized static variable.

The reason for this is, that the section followed my new section, is the .data section. And that second definition starts with:

  .data :
  {
    . = ALIGN(8);
    _sdata = .;        /* create a global symbol at data start */

    . = ALIGN(4);
    _edata = .;        /* define a global symbol at data end */

which leads to _sdata (0x20000008) and _edata (0x20000010) being 8 byte aligned. Because my 4 byte long new section is in front of the .data section, the .data section starts on a 4 byte aligned address (0x20000004), the linker adds 4 byte padding at the front of the .data .

Now the startup code, initializes the .data segment with values from _sidata, which is the load address of the .data segment:

    _sidata = LOADADDR(.data);

And as the .data segment is padded with 4 bytes at the beginning, the first static initialized variable is filled with that padding, the second variable is initialized with the value from the first and so on.

My question: why are the first values of the .data segment aligned to 8 bytes? I’ve changed the alignment to 4 and now everything works fine.

TIA
Torsten

Outcomes