Subject: question regarding bss section

From: "Teresa Tao" <[email-masked]>
Date: Mon, 27 Oct 2003 09:44:05 -0800

Hi there,

I have several questions and hope somebody could help me with the answers:
1. how to use gcc to compile the user mode program with larger stack size?
2. Inside the user mode program, I have declared some gloabal data which is being put on the bss section and I would like to know whom initialize the bss section? How big is the bss section? Under what kind of situation, the bss section data could be corrupted?
3. What's the difference to compile the program with -G 0 option? That menas I don't use the $gp register, will there be any side effect?

Thanks in advance!

Teresa

---

From: Ralf Baechle <[email-masked]>
Date: Mon, 27 Oct 2003 19:09:57 +0100

> 1. how to use gcc to compile the user mode program with larger stack size?

Not at all.  Stack size is a resource limit which is set with can be
manipulated with setrlimit(2) or in bash with the ulimit command.  It
defaults to 8MB.

For pthread applications the stack size is a thead attribute, seem libc
manual.

> 2. Inside the user mode program, I have declared some gloabal data which
> is being put on the bss section and I would like to know whom initialize
> the bss section?

.bss is uninitialized.  Initialized data can't be in .bss.

> How big is the bss section? Under what kind of situation,

As big as needed for everything in it.

> the bss section data could be corrupted?

There's millions of ways of writing broken code.

> 3. What's the difference to compile the program with -G 0 option? That
> menas I don't use the $gp register, will there be any side effect?

-G 0 is the default for userspace.  Iow. passing the option changed nothing.

  Ralf

---

From: Wolfgang Denk <[email-masked]>
Date: Mon, 27 Oct 2003 19:16:24 +0100

Dear Teresa,

> 1. how to use gcc to compile the user mode program with larger stack size?

You don't. The Linux kernel will autyomagically grow the stack as needed.

> 2. Inside the user mode program, I have declared some gloabal data
> which is being put on the bss section and I would like to know whom
> initialize the bss section? How big is the bss section? Under what
> kind of situation, the bss section data could be corrupted?

The kernel resp. the C runtime environment will  initialize  the  BSS
for you. The BSS will be initialized with zero values.

The BSS section is as big as needed to put all  objects  of  matching
type into it (this is determined at link time).

The BSS may get corrupted in exactly the  same  situations  when  any
other memroy corruption can happen, i. e. typically writing through a
rogue  pointer,  writing  beyound  the valid index range of an array,
errors using malloc() and free(0, etc.

> 3. What's the difference to compile the program with -G  0  option?
> That  menas  I  don't  use  the  $gp register, will there be any side
> effect?

Quoting the GCC info pages:

`-G NUM'
     Put global and static objects less than or equal to NUM bytes into
     the small data or bss sections instead of the normal data or bss
     sections.  The default value of NUM is 8.  The `-msdata' option
     must be set to one of `sdata' or `use' for this option to have any
     effect.

Best regards,

Wolfgang Denk

-- 
Software Engineering:  Embedded and Realtime Systems,  Embedded Linux
Phone: (+49)-8142-4596-87  Fax: (+49)-8142-4596-88  Email: [email-masked]
"I dislike companies that have a we-are-the-high-priests-of-hardware-
so-you'll-like-what-we-give-you attitude. I like commodity markets in
which iron-and-silicon hawkers know that they exist to  provide  fast
toys for software types like me to play with..."    - Eric S. Raymond

---

From: Wolfgang Denk <[email-masked]>
Date: Mon, 27 Oct 2003 19:17:12 +0100

> .bss is uninitialized.  Initialized data can't be in .bss.

No. BSS is initialized as zero.

Best regards,

Wolfgang Denk

-- 
Software Engineering:  Embedded and Realtime Systems,  Embedded Linux
Phone: (+49)-8142-4596-87  Fax: (+49)-8142-4596-88  Email: [email-masked]
Life is a game. Money is how we keep score.              - Ted Turner

---

From: Ralf Baechle <[email-masked]>
Date: Mon, 27 Oct 2003 20:08:29 +0100

> No. BSS is initialized as zero.

RTFM.  It's unitialized because not contained in the binaries.

  Ralf

---

From: Wolfgang Denk <[email-masked]>
Date: Mon, 27 Oct 2003 20:49:15 +0100

> RTFM.  It's unitialized because not contained in the binaries.

When an application runs it will see the BSS space as initialized  as
zero.

In most implementations of ANSI C that I am aware of (including GCC /
glibc), the BSS segment will be used for uninitialized variables with
"static" storage  class.  Also,  I've  seen  some  compilers  to  put
variables eplicitly initialized to zero into the BSS segment, too. To
quote the C FAQ:

    Uninitialized variables with "static" duration (that is, those
    declared outside of functions, and those declared with the
    storage class static), are guaranteed to start out as zero, as if
    the programmer had typed "= 0". Therefore, such variables

Best regards,

Wolfgang Denk

-- 
Software Engineering:  Embedded and Realtime Systems,  Embedded Linux
Phone: (+49)-8142-4596-87  Fax: (+49)-8142-4596-88  Email: [email-masked]
Quantum particles: The dreams that stuff is made of.

---

From: Ralf Baechle <[email-masked]>
Date: Tue, 28 Oct 2003 00:01:36 +0100

>     the programmer had typed "= 0". Therefore, such variables

C doesn't know about .bss at all - no single mentioning in the ISO C
standard.  But .bss is a section name used in the ELF binary format which
most Linux systems are using.  The gABI says defines .bss:

.bss	This section holds uninitialized data that contribute to the
	program s memory image. By definition, the system initializes
	the data with zeros when the program begins to run. The section
	occupies no file space, as indicated by the section type,
	SHT_NOBITS.

Certainly the term ``uninitialized'' isn't as precise as desirable but
that's the wording used in the relevant standard.

  Ralf