Shoud bss section be zeroized by loader?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
torshie
Member
Member
Posts: 89
Joined: Sun Jan 11, 2009 7:41 pm

Shoud bss section be zeroized by loader?

Post 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
Laksen
Member
Member
Posts: 140
Joined: Fri Nov 09, 2007 3:30 am
Location: Aalborg, Denmark

Re: Shoud bss section be zeroized by loader?

Post 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
http://j-software.dk | JPasKernel - My Object Pascal kernel
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Shoud bss section be zeroized by loader?

Post 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).
User avatar
ehenkes
Member
Member
Posts: 124
Joined: Mon Mar 23, 2009 3:15 am
Location: Germany
Contact:

Re: Shoud bss section be zeroized by loader?

Post 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 $
Post Reply