Order of .bss section in nasm & ld

Programming, for all ages and all languages.
Post Reply
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Order of .bss section in nasm & ld

Post 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
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Order of .bss section in nasm & ld

Post by AJ »

Hi,

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

Cheers,
Adam
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: Order of .bss section in nasm & ld

Post 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...
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: Order of .bss section in nasm & ld

Post 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!
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Order of .bss section in nasm & ld

Post 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. */
    }

}
evoex
Member
Member
Posts: 103
Joined: Tue Dec 13, 2011 4:11 pm

Re: Order of .bss section in nasm & ld

Post by evoex »

Thanks guys :). Works like a charm!
Post Reply