title: Alignment in linker scripts
body: <p>I am looking at trezor's bootloader linker script:</p>
  
  <pre><code>/* TREZORv2 bootloader linker script */
  
  ENTRY(reset_handler)
  
  MEMORY {
    FLASH  (rx)  : ORIGIN = 0x08020000, LENGTH = 128K
    CCMRAM (wal) : ORIGIN = 0x10000000, LENGTH = 64K
    SRAM   (wal) : ORIGIN = 0x20000000, LENGTH = 192K
  }
  
  main_stack_base = ORIGIN(CCMRAM) + LENGTH(CCMRAM); /* 8-byte aligned full descending stack */
  
  /* used by the startup code to populate variables used by the C code */
  data_lma = LOADADDR(.data);
  data_vma = ADDR(.data);
  data_size = SIZEOF(.data);
  
  /* used by the startup code to wipe memory */
  ccmram_start = ORIGIN(CCMRAM);
  ccmram_end = ORIGIN(CCMRAM) + LENGTH(CCMRAM);
  
  /* used by the startup code to wipe memory */
  sram_start = ORIGIN(SRAM);
  sram_end = ORIGIN(SRAM) + LENGTH(SRAM);
  
  _codelen = SIZEOF(.flash) + SIZEOF(.data);
  
  SECTIONS {
    .header : ALIGN(4) {
      KEEP(*(.header));
    } &gt;FLASH AT&gt;FLASH
  
    .flash : ALIGN(512) {
      KEEP(*(.vector_table));
      . = ALIGN(4);
      *(.text*);
      . = ALIGN(4);
      *(.rodata*);
      . = ALIGN(512);
    } &gt;FLASH AT&gt;FLASH
  
    .data : ALIGN(4) {
      *(.data*);
      . = ALIGN(512);
    } &gt;CCMRAM AT&gt;FLASH
  
    .bss : ALIGN(4) {
      *(.bss*);
      . = ALIGN(4);
    } &gt;CCMRAM
  
    .stack : ALIGN(8) {
      . = 4K; /* this acts as a build time assertion that at least this much memory is available for stack use */
    } &gt;CCMRAM
  }
  </code></pre>
  
  <p>It can be found <a href="https://github.com/trezor/trezor-core/blob/master/embed/bootloader/memory.ld" rel="nofollow noreferrer">here</a>.</p>
  
  <p>I understand that the code needs to be 32bit ( ALIGN(4) ) aligned, because the ARM processor can crash if it tries to access unaligned address, but I do not understand why the stack alignment is 8 bytes and furthermore why the hell do you need to waste(?) 512 bytes for alignment of the flash section?!</p>
  
  <p>I would like to understand how the alignment is decided when writing a linker script.</p>
  
  <p>Thank you in advance for your answers!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I think i answered my own question:</p>
  
  <p><strong>1. .flash section:</strong></p>
  
  <p>It is aligned like that, because the vector table, that is inside it always needs to be <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0203h/Cihdidh2.html" rel="nofollow noreferrer">"32-word aligned"</a>. This can also be seen be the case in <a href="https://github.com/trezor/trezor-core/blob/master/embed/boardloader/memory.ld" rel="nofollow noreferrer">Trezor's boardloader linker script</a>. As you can see the vector table is 512 byte (4 x 32-word) aligned.</p>
  
  <p><strong>2. .stack section:</strong></p>
  
  <p>According to <a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html" rel="nofollow noreferrer">ARM's own documentation</a> the stack section needs to always be 8 byte aligned.</p>
  
  <p>P.S. Of course if this is not the case, please correct me.</p>