public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Meaning of -fcommon and -fno-common
@ 2012-05-27 20:10 Amittai Aviram
  2012-05-28 11:16 ` David Given
  2012-05-29 22:40 ` Ian Lance Taylor
  0 siblings, 2 replies; 3+ messages in thread
From: Amittai Aviram @ 2012-05-27 20:10 UTC (permalink / raw)
  To: gcc-help

The GCC manual explains the -fcommon and -fno-common flag with reference to traditional Unix practice:  "In C code, controls the placement of uninitialized global variables. Unix C compilers have traditionally permitted multiple definitions of such variables in different compilation units by placing the variables in a common block."  What exactly is a "common block" in this context?  Where is it in the file and--assuming it's an ELF executable to be run on a Linux system--what segment (virtual address range) would the OS use for it, as opposed to the data section?  Thanks!

Amittai

Amittai Aviram
PhD Student in Computer Science
Yale University
646 483 2639
amittai.aviram@yale.edu
http://www.amittai.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Meaning of -fcommon and -fno-common
  2012-05-27 20:10 Meaning of -fcommon and -fno-common Amittai Aviram
@ 2012-05-28 11:16 ` David Given
  2012-05-29 22:40 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: David Given @ 2012-05-28 11:16 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1353 bytes --]

Amittai Aviram wrote:
> The GCC manual explains the -fcommon and -fno-common flag with reference to traditional Unix practice:  "In C code, controls the placement of uninitialized global variables. Unix C compilers have traditionally permitted multiple definitions of such variables in different compilation units by placing the variables in a common block."  What exactly is a "common block" in this context?  Where is it in the file and--assuming it's an ELF executable to be run on a Linux system--what segment (virtual address range) would the OS use for it, as opposed to the data section?  Thanks!

Variables in common blocks get folded into each other. So:

---file 1---
int i;
---file 2---
int i;
---end---

...will refer to the *same* global variable. After linking they end up
in the data segment just like any other global variable. They're not
really recommended these days due to principle of least surprise; people
are much better off using explicit declarations with extern instead.

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "Parents let children ride bicycles on the street. But parents do not
│ allow children to hear vulgar words. Therefore we can deduce that
│ cursing is more dangerous than being hit by a car." --- Scott Adams


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Meaning of -fcommon and -fno-common
  2012-05-27 20:10 Meaning of -fcommon and -fno-common Amittai Aviram
  2012-05-28 11:16 ` David Given
@ 2012-05-29 22:40 ` Ian Lance Taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Ian Lance Taylor @ 2012-05-29 22:40 UTC (permalink / raw)
  To: Amittai Aviram; +Cc: gcc-help

Amittai Aviram <amittai.aviram@yale.edu> writes:

> The GCC manual explains the -fcommon and -fno-common flag with
> reference to traditional Unix practice: "In C code, controls the
> placement of uninitialized global variables. Unix C compilers have
> traditionally permitted multiple definitions of such variables in
> different compilation units by placing the variables in a common
> block."  What exactly is a "common block" in this context?  Where is
> it in the file and--assuming it's an ELF executable to be run on a
> Linux system--what segment (virtual address range) would the OS use
> for it, as opposed to the data section?  Thanks!

Common symbols exist in object files, not executables.  The linker will
group the common symbols and put them in the data segment.

Ian

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-05-29 22:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-27 20:10 Meaning of -fcommon and -fno-common Amittai Aviram
2012-05-28 11:16 ` David Given
2012-05-29 22:40 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).