Page 1 of 2

Question: How .bss is loaded?

Posted: Wed Nov 27, 2013 8:43 pm
by neurocom
Hi,
This is my first question since I created an account :)
I have a tiny kernel which is a raw binary and it is converted from ELF format usinig linker script
I defined .text, .rodata, .data and .bss in my linker script.

As we know, .bss information is not present in binary/executable file.
Now, here is my question.

Then, how does the memory corresponding to the size of .bss are allocated when machine loads kernel???
This can be a basic knowledge for some of you guys.

Thanks in advance.

Re: Question: How .bss is loaded?

Posted: Wed Nov 27, 2013 9:23 pm
by Hoozim
The bss section contains read-write static data that doesn't require an initial value to be stored in file. Therefore, the ELF file just simply stores the location of the start of this section and its size. The loader (whether it be the boot-loader or an elf parser in your kernel) then allocates that space in memory, and zeros it, done.

Jacob

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 12:22 am
by Combuster
Hoozim wrote:that doesn't require an initial value
.bss is used for uninitialized variables and variables initialized as zero or NULL. You'll have to zero the memory you allocate because of the latter case.

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 1:41 am
by BMW
If you are using a flat binary, wouldn't the .bss be included in the binary executable file?

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 1:56 am
by Brendan
H,
BMW wrote:If you are using a flat binary, wouldn't the .bss be included in the binary executable file?
No. In that case there's still no initialised data in the .bss (and no point storing "nothing" in the file).


Cheers,

Brendan

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 1:58 am
by BMW
Brendan wrote:H,
BMW wrote:If you are using a flat binary, wouldn't the .bss be included in the binary executable file?
No. In that case there's still no initialised data in the .bss (and no point storing "nothing" in the file).


Cheers,

Brendan
Well I don't initialise any .bss in my kernel, I simply load the flat binary and run (and I've never had any issues)... ???

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:00 am
by bluemoon
BMW wrote:Well I don't initialise any .bss in my kernel, I simply load the flat binary and run (and I've never had any issues)... ???
It may work on emulator since the memory is initialized to zero.
On real machine you got random garbage.

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:01 am
by BMW
bluemoon wrote:
BMW wrote:Well I don't initialise any .bss in my kernel, I simply load the flat binary and run (and I've never had any issues)... ???
It may work in emulator since the memory is initialized to zero.
On real machine you got random garbage.
Yes but I don't even allocate any space for it lol. I guess it worked because the .bss was at the end of the file.

To allocate space for it, do I have to make symbols that point to the start/end of .bss?

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:13 am
by BMW
Brendan wrote:No. In that case there's still no initialised data in the .bss (and no point storing "nothing" in the file).
So this (Boot Sequence) is not 100% correct?
OSDev Wiki wrote:a "flat binary" that can be loaded in this simple copy-and-run way

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:15 am
by bwat
BMW wrote: To allocate space for it, do I have to make symbols that point to the start/end of .bss?
If you're using GCC then you'll be able to get the start address of the BSS and the address just after the BSS from the linker. See the manual page for "end" on linux.

This is how I clear my BSS

Code: Select all

	movl	$_end,%eax	
	subl	$_edata,%eax	# eax = $_end - $_edata (size of BSS)
	pushl	%eax		# number of bytes to clear
	pushl	$_edata		# starting location
	call	bzero

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:20 am
by BMW
bwat wrote:If you're using GCC then you'll be able to get the start address of the BSS and the address just after the BSS from the linker. See the manual page for "end" on linux.

This is how I clear my BSS

Code: Select all

	movl	$_end,%eax	
	subl	$_edata,%eax	# eax = $_end - $_edata (size of BSS)
	pushl	%eax		# number of bytes to clear
	pushl	$_edata		# starting location
	call	bzero
Thanks.
So those _end and _edata symbols are defined by GCC? And your .bss is at the end of the file so you don't have to make a space in the middle of your binary?

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:22 am
by bwat
BMW wrote: So this (Boot Sequence) is not 100% correct?
It depends on what you want to load. The BSS as we know it today is just a space optimisation used in certain languages/development tool-chains. You could write assembly code that didn't have a BSS section and load it with a simple load & go bootsrap loader.

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:25 am
by bwat
BMW wrote:Thanks.
So those _end and _edata symbols are defined by GCC? And your .bss is at the end of the file so you don't have to make a space in the middle of your binary?
If you're on linux, type "man end" or go here http://man7.org/linux/man-pages/man3/end.3.html and see how the symbols are used.
You don't have to shift stuff around, you'll only have to clear the BSS section. Unless of course you're doing something I'm not - I just keep it simple.

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:34 am
by BMW
bwat wrote:If you're on linux, type "man end" or go here http://man7.org/linux/man-pages/man3/end.3.html and see how the symbols are used.
You don't have to shift stuff around, you'll only have to clear the BSS section. Unless of course you're doing something I'm not - I just keep it simple.
Hold on, is the .bss present in a flat binary but not initialised? So all I have to do is zero it?

i.e. could I zero the BSS in the file so I didn't have to do it upon loading the binary?

Re: Question: How .bss is loaded?

Posted: Thu Nov 28, 2013 2:43 am
by bwat
BMW wrote:
bwat wrote:If you're on linux, type "man end" or go here http://man7.org/linux/man-pages/man3/end.3.html and see how the symbols are used.
You don't have to shift stuff around, you'll only have to clear the BSS section. Unless of course you're doing something I'm not - I just keep it simple.
Hold on, is the .bss present in a flat binary but not initialised? So all I have to do is zero it?
I have a program that copies the ELF TEXT & DATA segments to an image file which my bootstrap loader reads from disk. I don't create the BSS part of the image so I have to clear it in memory after it has loaded. I could generate a block of zeroes in the image for the BSS if I wanted to but that would increase the size of the image.

By zeroing the BSS section after the OS has loaded, you'll have a method that works regardless of the format of the OS executable that is loaded.