Question: How .bss is loaded?

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.
neurocom
Posts: 8
Joined: Wed Nov 27, 2013 8:33 pm

Question: How .bss is loaded?

Post 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.
Hoozim
Member
Member
Posts: 53
Joined: Fri Jul 23, 2010 8:26 am

Re: Question: How .bss is loaded?

Post 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
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: Question: How .bss is loaded?

Post 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.
"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 ]
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: Question: How .bss is loaded?

Post by BMW »

If you are using a flat binary, wouldn't the .bss be included in the binary executable file?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Question: How .bss is loaded?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: Question: How .bss is loaded?

Post 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)... ???
Last edited by BMW on Thu Nov 28, 2013 2:04 am, edited 2 times in total.
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Question: How .bss is loaded?

Post 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.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: Question: How .bss is loaded?

Post 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?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: Question: How .bss is loaded?

Post 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
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Question: How .bss is loaded?

Post 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
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: Question: How .bss is loaded?

Post 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?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Question: How .bss is loaded?

Post 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.
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Question: How .bss is loaded?

Post 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.
Every universe of discourse has its logical structure --- S. K. Langer.
User avatar
BMW
Member
Member
Posts: 286
Joined: Mon Nov 05, 2012 8:31 pm
Location: New Zealand

Re: Question: How .bss is loaded?

Post 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?
Currently developing Lithium OS (LiOS).

Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
User avatar
bwat
Member
Member
Posts: 359
Joined: Fri Jul 03, 2009 6:21 am

Re: Question: How .bss is loaded?

Post 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.
Every universe of discourse has its logical structure --- S. K. Langer.
Post Reply