6

I have a linker script like this:

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);
}

A piece of code loads the executable generated by this script, printing these infos:

size of executable (pages)    3
first free page               0xffff800000003000

And the executable itself prints:

&(linker_first_free_page)     0xffff800000003000

So everything works good so far. Now my executable needs a .bss 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.

First attempt

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);
}

That is, simply adding a .bss section. This is the output:

size of executable (pages)    3
first free page               0xffff800000003000
&(linker_first_free_page)     0xffff800000004000

That is, the linker variable is correctly updated, but the section is not allocated (I guess this is pretty normal for a .bss section).

Second attempt

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);
}

That is, putting .bss section inside the .data one. This is the output:

size of executable (pages)    4
first free page               0xffff800000004000
&(linker_first_free_page)     0xffff800000003000

That is, the .bss is allocated, but the linker variable is not updated (and I can't understand why...)

Short question

So, given all above, how can I embed the .bss section in a flat binary, so that it can be loaded in memory like a "standard" file and used directly, without a specific loader?

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
lodo
  • 2,314
  • 19
  • 31
  • 2
    The _BSS_ section generally isn't read from the file. By default _BSS_ falls at the end of the address space of the binary file. This means you don't generally need the _BSS_ region inside the file. Since you are trying to do it this way, a kludge might be to define _BSS_ right after _DATA_ section in the linker script, and update the location counter using SIZEOF. Something like `.bss : { *(.bss) }` followed by `. = . + SIZEOF(.bss);` – Michael Petch Jan 25 '16 at 22:40
  • @michaelpetch thanks for the idea. Could be an answer. – Alister Lee Jan 30 '20 at 11:55

0 Answers0