Non-.bss uninitialized data section

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
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Non-.bss uninitialized data section

Post by Rusky »

I'm using a custom linker script to split a kernel image into two parts. The first is normal code and data, and the second is initialization code and data to be discarded when it's no longer needed. The initialization part is also not shared between address spaces the way the kernel proper is, so anything there gets copied on fork() if it's still around (it is in these early phases of development).

I have allocated a small kernel stack to use while booting, but from what I can see, I can only put it either in the .bss section where it gets shared between address spaces or in the init area where it can't be stored as uninitialized data. I would like to store it in the init part of the image as uninitialized data so that each process gets its own copy.

I can think of two potential ways to do this, but I haven't been able to find out if they're possible or how I would tell the linker to do them. The first would be to put uninitialized regions in non-.bss sections, but I'm not sure that's possible- I don't think you can mix sections like that. The second would be to create a second .bss-like section that only stores uninitialized data, which I could put in the initialization pat of the linker script.

Any ideas? For completeness, here's the linker script I'm using:

Code: Select all

ENTRY(_start)

_kernel_offset = _start_kernel - _start_kernel_phys;

SECTIONS {
    _start_init = 0x100000;

    .init _start_init : AT(ADDR(.init)) { *(.mboot .init*) }
    .ctors : {
        __CTOR_NUM__ = .; LONG((__CTOR_END__ - __CTOR_LIST__) / 4)
        __CTOR_LIST__ = .; *(.ctors*)
        __CTOR_END__ = .;
    }

    _end_init = .;

    . = ALIGN(4M);
    _start_kernel_phys = .;
    _start_kernel = 0xF0000000;

    .text _start_kernel : AT(ADDR(.text) - _kernel_offset) { *(.text*) }
    .data ALIGN(4K) : AT(ADDR(.data) - _kernel_offset) { *(.rodata* .data*) }
    .bss ALIGN(4K) : AT(ADDR(.bss) - _kernel_offset) { *(.bss) *(COMMON) }

    _end_kernel = .;
    _end_kernel_phys = _end_kernel - _kernel_offset;

    /DISCARD/ : { *(.eh_frame .comment) }
}
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Non-.bss uninitialized data section

Post by JamesM »

We do it for pedigree, but I can't quite remember how off the top of my head.

Link below in sig, or I'll try and look it up when I get home from work tonight
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Non-.bss uninitialized data section

Post by Rusky »

Ah, nobits did it. The section gets declared in assembly as ".section .init.bss, "aw", @nobits". It does exactly what I need, although somehow that convinced the linker to put the noalloc .mboot section at the end of the file where grub couldn't find it. It doesn't really matter at this point because it's in the part of the image that gets freed and it's only 12 bytes, but for academic reasons I'd like to know if anyone has a way around that.
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Non-.bss uninitialized data section

Post by Rusky »

It looks pretty forced to me:

Code: Select all

SECTIONS {
	_start_init = 0x100000;
	
	.init.mboot _start_init : { *(.init.mboot) }
	.init.text : { *(.init.text) }
	.init.data : { *(.init.data) }
	.init.bss : { *(.init.bss) }
	...
Is there anything else that might help?
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Non-.bss uninitialized data section

Post by Rusky »

Code: Select all

.section .init.mboot

.align 4
.long HEADER_MAGIC
.long HEADER_FLAGS
.long HEADER_CHECKSUM
With .section .init.mboot, "a" it works, otherwise it gets pushed to the end of the file.
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Non-.bss uninitialized data section

Post by Rusky »

It's at the very beginning of the file.

This is the output of objdump -x:

Code: Select all

Program Header:
    LOAD off    0x00001010 vaddr 0x00100010 paddr 0x00100010 align 2**12
         filesz 0x000007aa memsz 0x000007aa flags r-x
    LOAD off    0x00002000 vaddr 0x00101000 paddr 0x00101000 align 2**12
         filesz 0x00004000 memsz 0x00008000 flags rw-
    LOAD off    0x00006000 vaddr 0x00109000 paddr 0x00109000 align 2**12
         filesz 0x0000001c memsz 0x0000001c flags rw-
    LOAD off    0x00007000 vaddr 0xf0000000 paddr 0x00400000 align 2**12
         filesz 0x0000272c memsz 0x0010a051 flags rwx

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  0 .init.mboot   0000000c  00100000  00100000  0000972c  2**2
                  CONTENTS, READONLY
  1 .init.text    000007aa  00100010  00100010  00001010  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  2 .init.data    00004000  00101000  00101000  00002000  2**12
                  CONTENTS, ALLOC, LOAD, DATA
  3 .init.bss     00004000  00105000  00105000  00006000  2**0
                  ALLOC
  4 .init.ctors   0000001c  00109000  00109000  00006000  2**2
                  CONTENTS, ALLOC, LOAD, DATA
  5 .text         00001b0d  f0000000  00400000  00007000  2**4
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
  6 .data         00000c0c  f0001b20  00401b20  00008b20  2**5
                  CONTENTS, ALLOC, LOAD, DATA
  7 .bss          00107051  f0003000  00403000  0000972c  2**12
                  ALLOC
  8 .stab         00005ce8  00000000  00000000  00009738  2**2
                  CONTENTS, READONLY, DEBUGGING
  9 .stabstr      00005632  00000000  00000000  0000f420  2**0
                  CONTENTS, READONLY, DEBUGGING
... symbol table ...
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Non-.bss uninitialized data section

Post by Rusky »

Hmm. I would expect it to look for the header in the file rather than the loaded image, but if that's how it works I guess it doesn't really matter.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Non-.bss uninitialized data section

Post by Combuster »

berkus wrote:mboot has to be ALLOC otherwise grub won't even load it and therefore won't find mboot signature.
Wouldn't that be in violation of the multiboot standard? It finds the header in non-ELF files too and the only restriction is that it should exist in the first 4K.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply