title: Linker script: allocation of .bss section
body: <p>I have a linker script like this:</p>
  
  <pre><code>OUTPUT_FORMAT(binary)
  SECTIONS
  {
      . = 0xFFFF800000000000 ;
      .startup_text : { processor.o(.text) }
      .text : { *(EXCLUDE_FILE (processor.o) .text) }
      .data : { *(.data) }
      .rodata : { *(.rodata) }
      linker_first_free_page = ALIGN(4096);
  }
  </code></pre>
  
  <p>A piece of code loads the executable generated by this script, printing these infos:</p>
  
  <pre><code>size of executable (pages)    3
  first free page               0xffff800000003000
  </code></pre>
  
  <p>And the executable itself prints:</p>
  
  <pre><code>&amp;(linker_first_free_page)     0xffff800000003000
  </code></pre>
  
  <p>So everything works good so far. Now my executable needs a <code>.bss</code> section. Note that I don't have a loader capable of loading elf files, so I need a flat binary that can be just read and used, with all sections inside.</p>
  
  <p><strong>First attempt</strong></p>
  
  <pre><code>OUTPUT_FORMAT(binary)
  SECTIONS
  {
      . = 0xFFFF800000000000 ;
      .startup_text : { processor.o(.text) }
      .text : { *(EXCLUDE_FILE (processor.o) .text) }
      .data : { *(.data) }
      .rodata : { *(.rodata) }
      .bss : { *(.bss) }
      linker_first_free_page = ALIGN(4096);
  }
  </code></pre>
  
  <p>That is, simply adding a <code>.bss</code> section. This is the output:</p>
  
  <pre><code>size of executable (pages)    3
  first free page               0xffff800000003000
  &amp;(linker_first_free_page)     0xffff800000004000
  </code></pre>
  
  <p>That is, the linker variable is correctly updated, but the section is not allocated (I guess this is pretty normal for a <code>.bss</code> section).</p>
  
  <p><strong>Second attempt</strong></p>
  
  <pre><code>OUTPUT_FORMAT(binary)
  SECTIONS
  {
      . = 0xFFFF800000000000 ;
      .startup_text : { processor.o(.text) }
      .text : { *(EXCLUDE_FILE (processor.o) .text) }
      .data : { *(.data) *(.bss) }
      .rodata : { *(.rodata) }
      linker_first_free_page = ALIGN(4096);
  }
  </code></pre>
  
  <p>That is, putting <code>.bss</code> section inside the <code>.data</code> one. This is the output:</p>
  
  <pre><code>size of executable (pages)    4
  first free page               0xffff800000004000
  &amp;(linker_first_free_page)     0xffff800000003000
  </code></pre>
  
  <p>That is, the <code>.bss</code> is allocated, but the linker variable is not updated (and I can't understand why...)</p>
  
  <p><strong>Short question</strong></p>
  
  <p>So, given all above, how can I embed the <code>.bss</code> section in a flat binary, so that it can be loaded in memory like a "standard" file and used directly, without a specific loader?</p>