Linker script: allocation of .bss section
Asked Answered
C

1

7

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?

Comenius answered 25/1, 2016 at 16:34 Comment(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);Marcelline
@michaelpetch thanks for the idea. Could be an answer.Groscr
T
0

The comment by Michael Petch gave me the insight for the answer:

OUTPUT_FORMAT(binary)
SECTIONS
{
    . = 0xFFFF800000000000 ;
    .startup_text : { processor.o(.text) }
    .text : { *(EXCLUDE_FILE (processor.o) .text) }
    .data : { *(.data) }
    .rodata : { *(.rodata) }
    .bss : { *(.bss) }
    .fake : { . = . + SIZEOF(.bss); }
    linker_first_free_page = ALIGN(4096);
}
Townshend answered 22/10, 2023 at 12:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.