[TALK] Proving fundamental Unix guarantees

Gary Schmidt gary.schmidt at oz.quest.com
Mon Jun 30 16:19:15 EST 2003

> From: Adam Donnison [mailto:adam at saki.com.au]
> Wiser heads than mine may well contradict me here, however my
> understanding is as follows.  BSS contains initialised global
> variables, however this is done by the compiler, not the OS.
> Other memory, allocated on either the stack or the heap, is
> not initialised and may contain trash.
> Adam
Well, since the folk who _really_ know the answer, at great length, are obviously still tied up in End-Of-Financial-Year crud, I'll try to remember more...

An (idealised) image consists of 5 sections (not necessarily in this order, or even contiguous):
	1 - Text
		The executable code.  (Yes, pedants, it can contain data,
		and should be read-only, etcetera.)
	2 - Data
		This is where _uninitialised_ data goes.
	3 - BSS (Backing Storage Section???)
		This is where _initialised_ data goes.
	4 - Heap.
		This is where heap data is allocated from.
	5 - Stack.
		Surprise, the stack!!

I may have the meanings of the Data and BSS sections swapped, and I am ignoring "constant" data sections, and so forth, just to keep it simple-ish.

Now, what variables go where depends on:
	The rules of the language the program is written in.  If the language requires that all "global" data is initialised it will be placed in BSS.
	The decisions made by the language implementor.  If there is no requirement, they may choose to initialise all uninitialised data to some sensible default value, so it ends up in the BSS rather than Data.
	The decisions of the OS implementor.  They may choose that all "new" memory assigned to an image be initialised to some value.
	And, for all I know, a dozen more...

So, the rule of thumb is "if data is not _directly_ initialised, make no assumptions about it."  (And I still get caught by compilers that initialise data in debug mode, but not in release mode.)

		Gary	B-)

More information about the Talk mailing list