This is the mail archive of the
gcc-prs@gcc.gnu.org
mailing list for the GCC project.
Re: other/4372: #pragma weak pthread* inclusion causes applications to crash without a linker error when one forgets to link with -lpthread
- From: ehrhardt at mathematik dot uni-ulm dot de
- To: carlo at alinoe dot com, gcc-bugs at gcc dot gnu dot org, gcc-prs at gcc dot gnu dot org, nobody at gcc dot gnu dot org
- Date: 15 Jan 2003 14:29:03 -0000
- Subject: Re: other/4372: #pragma weak pthread* inclusion causes applications to crash without a linker error when one forgets to link with -lpthread
- Reply-to: ehrhardt at mathematik dot uni-ulm dot de, carlo at alinoe dot com, gcc-bugs at gcc dot gnu dot org, gcc-prs at gcc dot gnu dot org, nobody at gcc dot gnu dot org, gcc-gnats at gcc dot gnu dot org
Synopsis: #pragma weak pthread* inclusion causes applications to crash without a linker error when one forgets to link with -lpthread
State-Changed-From-To: closed->analyzed
State-Changed-By: cae
State-Changed-When: Wed Jan 15 06:29:01 2003
State-Changed-Why:
This testcase boils down to the following:
extern void f ();
#pragma weak f
int main(void)
{
f();
}
This compiles and links fine despite the missing symbol for f.
This is correct (documented) behaviour as analyzed below.
BUT: This construct (external declaration combined with weak pragma) is
used in header files provided by gcc (gthr-default.h). The effect
is that programs that use this header file can be linked without error
messages even if they aren't linked against libpthread. Hoever these
programs crash when executed. Is there really any need for these pragmas
at all? As far as I can see the situation is this:
With pragmas:
* If the functions aren't used everything works fine.
* If the functions are used but there is no implementation for them
the program compiles and links but crashes at run time.
Without pragmas:
* If the functions aren't used everything works fine.
If the functions are used but there is no implementation for them
the program compiles (to a .o) but it doesn't link.
I'd highly prefer the latter, i.e. we get an error message instead of
a broken executable. If the intention is to make the implementation
of these functions weak, why can't this be done in the corresponding .c
file that implements the function?
Here are the exceprts form the manual to prove that the current
behaviour of gcc is as documented:
From gccs info file:
| `weak'
| The `weak' attribute causes the declaration to be emitted as a weak
| symbol rather than a global. This is primarily useful in defining
| library functions which can be overridden in user code, though it
| can also be used with non-function declarations. Weak symbols are
| supported for ELF targets, and also for a.out targets when using
| the GNU assembler and linker.
Note that this explicitly mentions declarations, not just definitions.
The nm manual page describes the behaviour of weak symbols as follows:
| "W" The symbol is a weak symbol that has not been
| specifically tagged as a weak object symbol. When a
| weak defined symbol is linked with a normal defined
| symbol, the normal defined symbol is used with no
| error. When a weak undefined symbol is linked and
| the symbol is not defined, the value of the weak
| symbol becomes zero with no error.
The last sentence applies to our situation: A weak undefined symbol
is replaced with zero and no error occurs.
regards Christian Ehrhardt
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=4372