.bss -> .text in linker script. Will it be zero-initialized?

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
mid
Member
Member
Posts: 31
Joined: Thu Mar 04, 2021 7:25 am

.bss -> .text in linker script. Will it be zero-initialized?

Post by mid »

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: Select all

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

ENTRY(kmain)

SECTIONS
{
	. = 0;
	.text : {
		*(.text)
		*(.data)
		*(.bss)
		*(.rodata*)
	}
	_kernel_end = .;
}
antoni
Member
Member
Posts: 61
Joined: Sun May 24, 2020 9:11 am
Location: /dev/null

Re: .bss -> .text in linker script. Will it be zero-initiali

Post by antoni »

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.
User avatar
mid
Member
Member
Posts: 31
Joined: Thu Mar 04, 2021 7:25 am

Re: .bss -> .text in linker script. Will it be zero-initiali

Post by mid »

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.
User avatar
MichaelFarthing
Member
Member
Posts: 167
Joined: Thu Mar 10, 2016 7:35 am
Location: Lancaster, England, Disunited Kingdom

Re: .bss -> .text in linker script. Will it be zero-initiali

Post by MichaelFarthing »

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.
User avatar
mid
Member
Member
Posts: 31
Joined: Thu Mar 04, 2021 7:25 am

Re: .bss -> .text in linker script. Will it be zero-initiali

Post by mid »

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.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: .bss -> .text in linker script. Will it be zero-initiali

Post by bzt »

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: Select all

uint64_t example_uninitialized_variable;
Then in the program headers your only segment will have memsz == filesz + 8.

Cheers,
bzt
Octocontrabass
Member
Member
Posts: 5568
Joined: Mon Mar 25, 2013 7:01 pm

Re: .bss -> .text in linker script. Will it be zero-initiali

Post by Octocontrabass »

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.
Post Reply