Page 1 of 1

Shoud bss section be zeroized by loader?

Posted: Sat Aug 29, 2009 4:29 am
by torshie
Hi,
My kernel is a 64-bit higher-half kernel. I use a secondary loader to load my kernel into higher-half. If my loader does not zeroize the bss section, the kernel will get triple fault. After hours of debugging I get the root cause of the triple fault:
G++ put some static variables into .bss.* sections. The C++ standard says all static variables will be initialized to zero(If I am not wrong). If the loader doesn't zeroize the the bss section, the assumption that static variables will be initialized to zero made by G++ will be broken, so unexpected things happen and the kernel get triple fault.

My loader just reads the section table and loads sections needed into memory.
This is part of my ld script.

Code: Select all

    .bss : {
        *(.dynbss)
        *(.bss .bss.* .gnu.linkonce.b.*)
        *(COMMON)
        . = ALIGN(ALIGNMENT);
    }
My question is, shoud bss section be zeriozed or my loader is wrong or my ld script is wrong or any others?

Thanks in advance
torshie

Re: Shoud bss section be zeroized by loader?

Posted: Sat Aug 29, 2009 5:24 am
by Laksen
If you use ELF, the specification states very clearly, in simple terms, that the .bss section should be initialized to zeroes after allocation

Re: Shoud bss section be zeroized by loader?

Posted: Sat Aug 29, 2009 5:56 am
by pcmattman
To back up Laksen, your loader must do that zeroing, as the BSS section in most ELF binaries doesn't actually exist as actual zeroes (ie, the filesz < memsz).

Re: Shoud bss section be zeroized by loader?

Posted: Sat Aug 29, 2009 1:34 pm
by ehenkes
linker script and starter for a user space program in elf format:

Code: Select all

ENTRY(_start)
OUTPUT_FORMAT(elf32-i386)
SECTIONS
{
    . = 0x400000;
    .text   : { __code_start = .;   *(.text*)         }
    .data   : { __data_start = .;   *(.data)          }    
    .rodata : { __rodata_start = .; *(.rodata)        }
    .bss    : { __bss_start = .;    *(.bss) *(COMMON) }
     __end = .; 	
}

Code: Select all

; start.asm

[BITS 32]
extern __bss_start
extern __end
extern _main
global _start

_start:
    mov edi, __bss_start
    mov ecx, __end
    sub ecx, __bss_start
    mov al, 0
    rep stosb  ; repeats instruction decrementing ECX until zero
	           ; and stores value from AL incrementing ES:EDI

    mov esp, 0x600000 ; stackpointer
    call _main
	
    jmp $