This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
libstdc++/4372: #pragma weak pthread* inclusion causes applications to crash without a linker error when one forgets to link with -lpthread
- To: gcc-gnats at gcc dot gnu dot org
- Subject: libstdc++/4372: #pragma weak pthread* inclusion causes applications to crash without a linker error when one forgets to link with -lpthread
- From: carlo at alinoe dot com
- Date: 21 Sep 2001 00:19:22 -0000
- Reply-To: carlo at alinoe dot com
>Number: 4372
>Category: libstdc++
>Synopsis: #pragma weak pthread* inclusion causes applications to crash without a linker error when one forgets to link with -lpthread
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: wrong-code
>Submitter-Id: net
>Arrival-Date: Thu Sep 20 17:26:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Carlo Wood
>Release: 3.0.1
>Organization:
>Environment:
i686-rawhide-gnu-linux
>Description:
Recently my (test) application started to core dump in
__libc_strtol_internal (according to gdb), apparently
called from pthread_getspecific.
It took me awhile to find out that I had forgotten
to add a -lpthread when linking. I considered it a bug
that the linker didn't give an error so I tried to write
a small test case to reproduce this, but in the test case
the linker DID give me `undefined reference to pthread_getspecific'...
After a little investigation, I found that the difference
between the test case and the problem case could be brought
back to the *.ii file: the problem case has included a
series of #pragma weak pthread... that caused the linker
to think that these functions are present as local functions,
while actually it causes a call to address 0x0 (hence the
`__libc_strtol_internal' I guess).
Because I can not succeed to make a small test case, I'll
give results of the my own investigation using the large
problem case:
~/c++/libcw_branch-threading/src/libcwd/tests>g++-3.0.1 -DHAVE_CONFIG_H -I. -I- -I../include -DCWDEBUG -g -D_REENTRANT -c hello_world.cc -save-temps -v
Reading specs from /usr/local/gcc-3.0.1/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/specs
Configured with: /usr/src/gcc/gcc-3.0.1/configure --prefix=/usr/local/gcc-3.0.1 --enable-shared --with-gnu-as --with-gnu-ld --enable-languages=c++ --enable-debug --enable-threads
Thread model: posix
gcc version 3.0.1
/usr/local/gcc-3.0.1/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/cpp0 -lang-c++ -D__GNUG__=3 -D__GXX_DEPRECATED -D__EXCEPTIONS -D__GXX_ABI_VERSION=100 -v -I. -I- -I../include -D__GNUC__=3 -D__GNUC_MINOR__=0 -D__GNUC_PATCHLEVEL__=1 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -D_GNU_SOURCE -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i686__ -D__tune_pentiumpro__ -DHAVE_CONFIG_H -DCWDEBUG -D_REENTRANT hello_world.cc hello_world.ii
GNU CPP version 3.0.1 (cpplib) (i386 Linux/ELF)
ignoring nonexistent directory "/usr/local/gcc-3.0.1/i686-pc-linux-gnu/include"
#include "..." search starts here:
.
#include <...> search starts here:
../include
/usr/local/gcc-3.0.1/include/g++-v3
/usr/local/gcc-3.0.1/include/g++-v3/i686-pc-linux-gnu
/usr/local/gcc-3.0.1/include/g++-v3/backward
/usr/local/include
/usr/local/gcc-3.0.1/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/include
/usr/include
End of search list.
/usr/local/gcc-3.0.1/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/cc1plus -fpreprocessed hello_world.ii -quiet -dumpbase hello_world.cc -g -version -o hello_world.s
GNU CPP version 3.0.1 (cpplib) (i386 Linux/ELF)
GNU C++ version 3.0.1 (i686-pc-linux-gnu)
compiled by GNU C version 3.0.1.
as --traditional-format -V -Qy -o hello_world.o hello_world.s
GNU assembler version 2.11.90.0.8 (i386-redhat-linux) using BFD version 2.11.90.0.8
Next I edit the *.ii file and remove the #pragma weak lines,
showing that these are the problem:
~/c++/libcw_branch-threading/src/libcwd/tests>vi hello_world.ii
~/c++/libding/src/libcwd/tests>/usr/local/gcc-3.0.1/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/cc1plus -fpreprocessed hello_world.ii -quiet -dumpbase hello_world.cc -g -version -o hello_world.s
GNU CPP version 3.0.1 (cpplib) (i386 Linux/ELF)
GNU C++ version 3.0.1 (i686-pc-linux-gnu)
compiled by GNU C version 3.0.1.
~/c++/libcw_branch-threading/src/libcwd/tests>as --traditional-format -V -Qy -o hello_world.o hello_world.s
GNU assembler version 2.11.90.0.8 (i386-redhat-linux) using BFD version 2.11.90.0.8
~/c++/libcw_branch-threading/src/libcwd/tests>nm hello_world.o | grep pthread
00000330 t _Z20__gthread_mutex_lockP15pthread_mutex_t
00000360 t _Z22__gthread_mutex_unlockP15pthread_mutex_t
U pthread_create
U pthread_getspecific
U pthread_key_create
U pthread_mutex_lock
U pthread_mutex_unlock
U pthread_once
U pthread_setspecific
Next, I do this again but now change a pragma into
#xxxpragma, getting:
~/c++/libcw_branch-threading/src/libcwd/tests>vi hello_world.ii
~/c++/libcw_branch-threading/src/libcwd/tests>/usr/local/gcc-3.0.1/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/cc1plus -fpreprocessed hello_world.ii -quiet -dumpbase hello_world.cc -g -version -o hello_world.s
GNU CPP version 3.0.1 (cpplib) (i386 Linux/ELF)
GNU C++ version 3.0.1 (i686-pc-linux-gnu)
compiled by GNU C version 3.0.1.
In file included from /usr/local/gcc-3.0.1/include/g++-v3/i686-pc-linux-gnu/bits/gthr.h:98,
from /usr/local/gcc-3.0.1/include/g++-v3/i686-pc-linux-gnu/bits/c++io.h:37,
from /usr/local/gcc-3.0.1/include/g++-v3/bits/fpos.h:39,
from /usr/local/gcc-3.0.1/include/g++-v3/bits/std_iosfwd.h:41,
from /usr/local/gcc-3.0.1/include/g++-v3/bits/std_ios.h:39,
from /usr/local/gcc-3.0.1/include/g++-v3/bits/std_ostream.h:39,
from /usr/local/gcc-3.0.1/include/g++-v3/bits/std_iostream.h:40,
from /usr/local/gcc-3.0.1/include/g++-v3/iostream:31,
from ../include/libcw/debug.h:81,
from hello_world_debug.h:18,
from hello_world.cc:28:
/usr/local/gcc-3.0.1/include/g++-v3/i686-pc-linux-gnu/bits/gthr-default.h:48:2: invalid preprocessing directive #xxxpragma
Apparently, the pragma's are in bits/gthr-default.h:48.
Hmm, maybe I can reproduce this after all by including
#include <iosfwd> in the small test case...
~>g++-3.0.1 test.cc -o test
~>./test
Segmentation fault (core dumped)
Yup, that does the trick.
I think this is highly annoying. The #pragma weak
should not make the linker think the function exists :/.
>How-To-Repeat:
Compile the following code snippet and don't link with
-lpthread (no error occurs):
#include <iosfwd>
#include <pthread.h>
static pthread_once_t S_key_once;
static pthread_key_t S_key;
void S_destroy(void* tsd_ptr) throw()
{
}
void S_alloc_key(void) throw()
{
pthread_key_create(&S_key, S_destroy);
}
int main(void)
{
pthread_once(&S_key_once, S_alloc_key);
}
>Fix:
Link with -lpthread
>Release-Note:
>Audit-Trail:
>Unformatted: