Page 1 of 1

Order of .bss section in nasm & ld

Posted: Wed Jan 04, 2012 9:37 am
by evoex
Hello everyone,

I'm using nasm and ld for my OS. Now, at the very end of the OS I would like to reserve some memory. I do this in the .bss section as follows:

Code: Select all

[section .bss]
memory_start:
  resb 100*1024

kernel_end:
I do want this to be the very last part of the kernel, such that any unused of this pre-allocated memory can still be used. However, what if I would like to use the .bss section in another file as well? For instance, let's say somewhere else I do:

Code: Select all

[section .bss]
some_variable resd 1
Then I assume it is no longer guaranteed for this kernel_end label to be at the very end of the kernel. How can I make sure that .bss section comes at the very end of the kernel?

My linker script is as follows:

Code: Select all

OUTPUT_FORMAT("elf32-i386") 
ENTRY(start) 

SECTIONS
{
	. = 0x100000;

	.setup :
	{
		*(.setup)
	}

	. += 0xC0000000;

	.text : AT(ADDR(.text) - 0xC0000000)
	{
		*(.text)
	}

	.data ALIGN (4096) : AT(ADDR(.data) - 0xC0000000)
	{
		*(.data)
		*(.rodata*)
	}

	.bss ALIGN (4096) : AT(ADDR(.bss) - 0xC0000000)
	{
		*(COMMON*)
		*(.bss*)
	}
}
Thanks in advance

Re: Order of .bss section in nasm & ld

Posted: Wed Jan 04, 2012 9:41 am
by AJ
Hi,

How about defining a new section name and adding it to your linker script after bss?

Cheers,
Adam

Re: Order of .bss section in nasm & ld

Posted: Wed Jan 04, 2012 9:53 am
by evoex
AJ wrote:Hi,

How about defining a new section name and adding it to your linker script after bss?

Cheers,
Adam
Thanks for your answer... I have tried that, but nasm gives me warnings about not initializing the data while it's not in a .bss section. It reports, for instance:

Code: Select all

warning: uninitialized space declared in non-BSS section `.bss_final': zeroing
So how can I resolve that?
EDIT: And also, what would the linker script look like? All my attempts got me the error warning "section `.bss' type changed to PROGBITS". I have to admit, I've never used linker scripts before...

Re: Order of .bss section in nasm & ld

Posted: Wed Jan 04, 2012 10:17 am
by evoex
I think I got it working. Both errors seemed to be the same: the need for section attributes. This seems to work, is it correct?

Code: Select all

[section .bss_final nobits align=4]
memory_start:
	resb	100*1024

kernel_end:
And the linker script:

Code: Select all

OUTPUT_FORMAT("elf32-i386") 
ENTRY(start) 

SECTIONS
{
	. = 0x100000;

	.setup :
	{
		*(.setup)
	}

	. += 0xC0000000;

	.text : AT(ADDR(.text) - 0xC0000000)
	{
		*(.text)
	}

	.data ALIGN (4096) : AT(ADDR(.data) - 0xC0000000)
	{
		*(.data)
		*(.rodata*)
	}

	.bss ALIGN (4096) : AT(ADDR(.bss) - 0xC0000000)
	{
		*(COMMON*)
		*(.bss*)
	}

	.bss ALIGN (4096) :
	{
		* (.bss_final*);
	}
}
Can somebody please confirm this is indeed correct?


Thanks for your help!

Re: Order of .bss section in nasm & ld

Posted: Wed Jan 04, 2012 10:36 am
by bluemoon
How about define kernel end point at link script? This is my kernel.ld

Code: Select all

ENTRY (bootstrap)

SECTIONS{
    . = 0xC0100400;  /* 3G + 1M + skip 1024 bytes ELF header */

    _kernel_start = .;

    .text : {
        *bootstrap_a.o(.text*)
        *(.text*)
        *(.gnu.linkonce.t*)
    }

    .rodata ALIGN(4096) : {

        /* . = ALIGN(8); */
        ctor_start = .;
        *(.ctor*)
        ctor_end = .;

        dtor_start = .;
        *(.dtor*)
        dtor_end = .;

        *(.rodata*)
        *(.gnu.linkonce.r*)
    }

    .data ALIGN(4096) : {
        *(.data*)
        *(.gnu.linkonce.d*)
    }
    
    .symtab ALIGN(4096) : {
        *(.symtab*)
        *(.strtab*)
        *(.shstrtab*)
    }
    
    .bss ALIGN(4096) : {
        sbss = .;
        *(COMMON*)
        *(.bss*)
        *(.gnu.linkonce.b*)
        ebss = .;
    }

    . = ALIGN(4096);
    _kernel_end = .;
    end = .;

    /DISCARD/ : {
        *(.comment)
        *(.eh_frame) /* You should discard this unless you're implementing runtime support for C++ exceptions. */
    }

}

Re: Order of .bss section in nasm & ld

Posted: Thu Jan 05, 2012 6:08 am
by evoex
Thanks guys :). Works like a charm!