cc bug in definition of global variables?

Doug Gwyn <gwyn> gwyn at brl-tgr.ARPA
Tue Nov 19 23:46:52 AEST 1985


> The following seems to be a bug in the SYSV.2 C-compiler (loader really):
> You can DEFINE the same global variable in two files and the loader does
> not even issue a warning about it.  In fact, you can DEFINE the same global
> variable in two files as two DIFFERENT data types and the loader does not
> issue a warning as long as the size of the two globals is the same.  Also,
> it really treats them as one global variable.  Thus, the effect is the same
> as declaring the global as the union of the two declarations (as an example,
> try the program at the end of this message).
> 
> This behavior of the compiler is contrary to what is mentioned in K&R.
> To quote, (pp 77, italicized words from the text are shown in upper case):
>     "    There must be only one DEFINITION of an external variable
> 	       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>     among all the files that make up the source program;  others
>     may contain 'extern' declarations to access it."
> Also, on page 206 (Appendix A):
>     "Thus in a multi-file program, an external data definition without
>     the 'extern' specifier must appear in exactly one of the files."
>                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is not a bug.  Although Ritchie preferred the COMMON model
for extern data, which is what most UNIX PCCs implement, there
are some systems supporting C that have unacceptable restrictions
on COMMON data (such as, minimum space 4Kb each, limited quantity
available, etc.).  Therefore the official language description is
in terms of the DEF/REF model of external data linkage, which has
no such limitations on those systems.  If you write your code
according to the (DEF/REF) rules, it will also work correctly on
systems using COMMON for extern data, but not vice-versa.

There is a story that some early release of USG UNIX (5.0?) had
been changed to enforce DEF/REF semantics, and so much code broke
that they had to back out the change (by providing "mcc", or "cc -m",
or some such temporary scheme) to still the outcries.  I heard that
some AT&T internal sites refused to install the changed SGS until
extern data linkage was put back the way it used to be.  If there
is an interesting story here, perhaps someone involved will tell us.

Note that if you had tried initializing the multiple definitions
of extern data with different values, the loader would have issued
a warning.  But there is a special kludge in "ld" that allows
multiple definitions so long as they are all (uninitialized) common
except for at most one (initialized) .data definition.  (The sizes
also are maxed together, as I recall.)



More information about the Comp.lang.c mailing list