OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 4:04 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: .bss -> .text in linker script. Will it be zero-initialized?
PostPosted: Thu May 13, 2021 2:49 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 04, 2021 7:25 am
Posts: 31
Ignoring the size inflation from doing such a thing in the first place, will ld zero-initialize the range of memory that corresponds to the input .bss section, or should I assume it to be link-time garbage? Is there a way to force ld to zero-initialize this memory?

Code:
OUTPUT_FORMAT("elf32-i386")
OUTPUT_ARCH(i386)

ENTRY(kmain)

SECTIONS
{
   . = 0;
   .text : {
      *(.text)
      *(.data)
      *(.bss)
      *(.rodata*)
   }
   _kernel_end = .;
}

_________________
mid.net.ua


Top
 Profile  
 
 Post subject: Re: .bss -> .text in linker script. Will it be zero-initiali
PostPosted: Thu May 13, 2021 3:46 am 
Offline
Member
Member

Joined: Sun May 24, 2020 9:11 am
Posts: 61
Location: /dev/null
Quote:
will ld zero-initialize the range of memory that corresponds to the input .bss section, or should I assume it to be link-time garbage?


BSS means uninitialised memory. LD won't initialize it, and it don't have to, the loader is responsible for that. Initializing it with zeros would be a horrific waste of storage. You should assume that any object in this section is zeroed. Some operating systems fill it with zero it at load time. Some systems clear this memory at first read as an optimalisation.

If we are talking about kernel, however, it will obviously be loaded by the bootloader into free memory, which is zeroed. This means that ALL of your variables (even local) will be set to zero. Obviously, bss section is zeroed out too.


Top
 Profile  
 
 Post subject: Re: .bss -> .text in linker script. Will it be zero-initiali
PostPosted: Thu May 13, 2021 3:51 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 04, 2021 7:25 am
Posts: 31
The point is that my bootloader doesn't have any knowledge of particular sections; it doesn't know what to zero-initialize, because it's all put into one section.

_________________
mid.net.ua


Top
 Profile  
 
 Post subject: Re: .bss -> .text in linker script. Will it be zero-initiali
PostPosted: Thu May 13, 2021 5:15 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 10, 2016 7:35 am
Posts: 167
Location: Lancaster, England, Disunited Kingdom
mid wrote:
The point is that my bootloader doesn't have any knowledge of particular sections; it doesn't know what to zero-initialize, because it's all put into one section.


I zero my OS memory. Doesn't take long and gives me a sense of security. It also means that during development (which I do from inside the OS itself) then unless I have a disastrous crash I can safely fast reboot a new version without having to clear up any mess first.


Top
 Profile  
 
 Post subject: Re: .bss -> .text in linker script. Will it be zero-initiali
PostPosted: Thu May 13, 2021 5:23 am 
Offline
Member
Member
User avatar

Joined: Thu Mar 04, 2021 7:25 am
Posts: 31
MichaelFarthing wrote:
mid wrote:
The point is that my bootloader doesn't have any knowledge of particular sections; it doesn't know what to zero-initialize, because it's all put into one section.


I zero my OS memory. Doesn't take long and gives me a sense of security. It also means that during development (which I do from inside the OS itself) then unless I have a disastrous crash I can safely fast reboot a new version without having to clear up any mess first.


That's great.

I'm still expecting a yes/no answer to my quesion.

_________________
mid.net.ua


Top
 Profile  
 
 Post subject: Re: .bss -> .text in linker script. Will it be zero-initiali
PostPosted: Thu May 13, 2021 6:54 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
mid wrote:
I'm still expecting a yes/no answer to my quesion.

mid wrote:
Ignoring the size inflation from doing such a thing in the first place
No. The .bss section does not influence the executable size in any way.
mid wrote:
will ld zero-initialize the range of memory that corresponds to the input .bss section
No.
mid wrote:
or should I assume it to be link-time garbage?
No. It's going to be run-time garbage.
mid wrote:
Is there a way to force ld to zero-initialize this memory?
As others have already said, it is the loader's duty, or you should write ALL programs specifically either not to care about the garbage or zero out before use. Implementing it in the loader is a better approach.

mid wrote:
The point is that my bootloader doesn't have any knowledge of particular sections; it doesn't know what to zero-initialize, because it's all put into one section.
It doesn't have to. According to your linker script, you're going to have a single segment which your loader must load. It's going to have filesz bytes in the file, and the difference to memsz should be zerod out (that's where the .bss resides).

If your code doesn't have uninitialized values, then memsz == filesz. If you have this for example (outside of functions):
Code:
uint64_t example_uninitialized_variable;
Then in the program headers your only segment will have memsz == filesz + 8.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: .bss -> .text in linker script. Will it be zero-initiali
PostPosted: Thu May 13, 2021 10:28 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
If you force LD to initialize the contents of the .bss section, it will be filled with zeroes.

I'm not sure why you would want LD to do this instead of your bootloader.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot] and 112 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group