This allocates some BSS variables:
.lcomm page_table_l2, 4096
.lcomm page_table_l3, 4096
.lcomm page_table_l4, 4096
The binary I'm building ends up being 4792 bytes, so you can see that the BSS variables are not directly included (or the binary would be >12 KiB).
However, these three need to be 4 KiB aligned, so I change the section to:
.section .bss
.align 4096
.lcomm page_table_l2, 4096
.lcomm page_table_l3, 4096
.lcomm page_table_l4, 4096
…and the binary grows to 8760! Given that BSS is supposed to just be a note in an ELF binary saying to the linker, hey, allocate n bytes of zeroed out storage, why does aligning a BSS variable cause any growth of the binary at all?
You can see this in C, too:
char x[4096] __attribute__ ((aligned (8192))) = {0};
If you vary the alignment, the output object file's size varies with it. (though in my original example, I'm looking at the final binary's size.)
Note that this output binary is an OS kernel; I'm following the tutorial here. I am using the following linker script:
ENTRY(start)
SECTIONS {
. = 1M;
.boot ALIGN(8) : AT(ADDR(.boot))
{
/* ensure that the multiboot header is at the beginning */
KEEP( *(.multiboot_header) )
}
.text :
{
*(.text)
}
}
According to objdump, it kind of looks like the entire program gets 4 KiB aligned in the elf itself, which is a bit weird.
Without .align:
Sections:
Idx Name Size VMA LMA File off Algn
0 .boot 00000018 0000000000100000 0000000000100000 00000078 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
With .align:
Sections:
Idx Name Size VMA LMA File off Algn
0 .boot 00000018 0000000000100000 0000000000100000 00001000 2**0
CONTENTS, ALLOC, LOAD, READONLY, DATA
Note File off, or the offset of the section in the file. Why does that change?
gcc -nostdlib? Making a plain ELF binary? Also, I didn't know multiboot kernels got text/data/bss segments; that's pretty fancy. The tutorial you're following is using NASM, so it's not hard to imagine that gas works slightly differently wrt. aligning things in the BSS. Anyway, making this a minimal reproducible example would only take a couple more lines of commands and notes about which block of code goes in what filename, right? This is interesting and makes me want to try it out, but not badly enough to guess at what exactly you did :P - Peter Cordes.oor plain Linux executable. I wasn't doubting that GRUB's multiboot loader will zero a BSS for you, I was just saying I didn't know that was supported until I read it just now. :P - Peter Cordes