Question: How .bss is loaded?
Question: How .bss is loaded?
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.
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?
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
Jacob
- Combuster
- 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?
.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.Hoozim wrote:that doesn't require an initial value
Re: Question: How .bss is loaded?
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: Question: How .bss is loaded?
H,
Cheers,
Brendan
No. In that case there's still no initialised data in the .bss (and no point storing "nothing" in the file).BMW wrote:If you are using a flat binary, wouldn't the .bss be included in the binary executable 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.
Re: Question: How .bss is loaded?
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)... ???Brendan wrote:H,
No. In that case there's still no initialised data in the .bss (and no point storing "nothing" in the file).BMW wrote:If you are using a flat binary, wouldn't the .bss be included in the binary executable file?
Cheers,
Brendan
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: Question: How .bss is loaded?
It may work on emulator since the memory is initialized to zero.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)... ???
On real machine you got random garbage.
Re: Question: How .bss is loaded?
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.bluemoon wrote:It may work in emulator since the memory is initialized to zero.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)... ???
On real machine you got random garbage.
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: Question: How .bss is loaded?
So this (Boot Sequence) is not 100% correct?Brendan wrote:No. In that case there's still no initialised data in the .bss (and no point storing "nothing" in the file).
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: Question: How .bss is loaded?
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.BMW wrote: To allocate space for it, do I have to make symbols that point to the start/end of .bss?
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.
Re: Question: How .bss is loaded?
Thanks.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 BSSCode: 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
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: Question: How .bss is loaded?
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.BMW wrote: So this (Boot Sequence) is not 100% correct?
Every universe of discourse has its logical structure --- S. K. Langer.
Re: Question: How .bss is loaded?
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.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?
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.
Re: Question: How .bss is loaded?
Hold on, is the .bss present in a flat binary but not initialised? So all I have to do is zero it?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.
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."
Recursive paging saves lives.
"I want to change the world, but they won't give me the source code."
Re: Question: How .bss is loaded?
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.BMW wrote:Hold on, is the .bss present in a flat binary but not initialised? So all I have to do is zero it?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.
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.