[TAG] BSS and Data Segment

Lew Pitcher lpitcher at sympatico.ca
Thu Oct 6 00:26:46 MSD 2005


On October 5, 2005 11:30, Mike Zheng wrote:
> Hi All,
> 
> For a program, the BSS contains un-initialized variables, Data contains
> initialized variables. Why do we want to separated these two categories of
> variables? 

It's an optimization thing. If each binary contained an image of 'data' that 
included the uninitialized variables, then the binary would be larger in size 
than if the image did not include uninitialized variables.  If you don't map 
these variables to your 'load image' (the contents of the binary), but 
instead, map them to memory at execution time, your binaries can be smaller 
without a loss of functionality.

The 'typical' (conceptual) memory map (once a program has been loaded into 
memory) looks something like this...


          +----------------+
          |  stack space   |
          |       :        |
          |       v        |
          |                |
          |                |
          |                |
          |       ^        |
          |       :        |
          |   heap space   |
          | .. .. .. .. .. |
          |      BSS       |
          +----------------+
          |      DATA      |
          |                |
          +----------------+
          |     (CODE)     |
          |                |
          +----------------+

(on most implementations, CODE is kept in a separate address space from 
DATA/BSS/heap/stack)

Heap 'grows' up from the end of BSS towards the highest address
Stack 'grows' down from the highest address towards the end of the BSS
Heap allocation code keeps a boundary between the top end of the heap and the 
bottom end of the stack.

The 'loadable' part of the binary is the CODE and DATA part. BSS is how the 
loader accounts for the /room/ needed to hold the uninitialized data. 

> Some program clear the BSS by itself, is it necessary? 
A mere convenience, nothing more.


HTH
-- 
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/) 
Slackware - Because I know what I'm doing.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.linuxgazette.net/mailman/private/tag/attachments/20051005/8b7d21e7/attachment-0001.pgp 



More information about the TAG mailing list