From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24703 invoked by alias); 15 Jan 2003 14:29:03 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 24678 invoked by uid 61); 15 Jan 2003 14:29:03 -0000 Date: Wed, 15 Jan 2003 14:29:00 -0000 Message-ID: <20030115142903.24677.qmail@sources.redhat.com> To: carlo@alinoe.com, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org From: ehrhardt@mathematik.uni-ulm.de Reply-To: ehrhardt@mathematik.uni-ulm.de, carlo@alinoe.com, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, nobody@gcc.gnu.org, gcc-gnats@gcc.gnu.org Subject: Re: other/4372: #pragma weak pthread* inclusion causes applications to crash without a linker error when one forgets to link with -lpthread X-SW-Source: 2003-01/txt/msg00951.txt.bz2 List-Id: 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