public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* C++/dlerror/pthread problem
@ 2003-07-31 14:28 Thorsten Kukuk
  2003-07-31 16:28 ` David Mosberger
  2003-07-31 17:16 ` [PATCH] Fix dlerror (was Re: C++/dlerror/pthread problem) Jakub Jelinek
  0 siblings, 2 replies; 3+ messages in thread
From: Thorsten Kukuk @ 2003-07-31 14:28 UTC (permalink / raw)
  To: libc-hacker

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


Hi,

The appended example program works fine, if not linked against
libpthread or if it is compiled as C program. But as C++ program
linked against libpthread, it will seg.fault in dlerror().

Does somebody know why? I know it is a bad idea to call dlerror()
without any other dl* call before, but people are doing so ...

-- 
Thorsten Kukuk       http://www.suse.de/~kukuk/        kukuk@suse.de
SuSE Linux AG        Deutschherrnstr. 15-19        D-90429 Nuernberg
--------------------------------------------------------------------    
Key fingerprint = A368 676B 5E1B 3E46 CFCE  2D97 F8FD 4E23 56C6 FB4B

[-- Attachment #2: dlerror.cc --]
[-- Type: text/plain, Size: 175 bytes --]


/* Compile with g++ dlerror.cc -ldl -lpthread */

#include <dlfcn.h>
#include <iostream>

int
main ()
{
  std::cerr << "vor dlerror" << std::endl;
  dlerror();
  return 0;
}

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

* Re: C++/dlerror/pthread problem
  2003-07-31 14:28 C++/dlerror/pthread problem Thorsten Kukuk
@ 2003-07-31 16:28 ` David Mosberger
  2003-07-31 17:16 ` [PATCH] Fix dlerror (was Re: C++/dlerror/pthread problem) Jakub Jelinek
  1 sibling, 0 replies; 3+ messages in thread
From: David Mosberger @ 2003-07-31 16:28 UTC (permalink / raw)
  To: Thorsten Kukuk; +Cc: libc-hacker

>>>>> On Thu, 31 Jul 2003 16:28:01 +0200, Thorsten Kukuk <kukuk@suse.de> said:

  Thorsten> The appended example program works fine, if not linked
  Thorsten> against libpthread or if it is compiled as C program. But
  Thorsten> as C++ program linked against libpthread, it will
  Thorsten> seg.fault in dlerror().

  Thorsten> Does somebody know why? I know it is a bad idea to call
  Thorsten> dlerror() without any other dl* call before, but people
  Thorsten> are doing so ...

Here is a possibility: depending on platform and exact version of
libpthread, here is what happens: you link (and build) against
libpthread, which will give you an "errno" that references a field in
the thread descriptor.  But since you never actually do any pthread
calls, the thread descriptor for the main thread never gets set up.
Thus, any attempt to use errno will cause a segfault.  This is known
to happen on ia64 with LinuxThreads.  NPTL is fine, as far as I know.

	--david

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

* [PATCH] Fix dlerror (was Re: C++/dlerror/pthread problem)
  2003-07-31 14:28 C++/dlerror/pthread problem Thorsten Kukuk
  2003-07-31 16:28 ` David Mosberger
@ 2003-07-31 17:16 ` Jakub Jelinek
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2003-07-31 17:16 UTC (permalink / raw)
  To: Ulrich Drepper, Roland McGrath, Thorsten Kukuk; +Cc: libc-hacker

On Thu, Jul 31, 2003 at 04:28:01PM +0200, Thorsten Kukuk wrote:
> 
> Hi,
> 
> The appended example program works fine, if not linked against
> libpthread or if it is compiled as C program. But as C++ program
> linked against libpthread, it will seg.fault in dlerror().
> 
> Does somebody know why? I know it is a bad idea to call dlerror()
> without any other dl* call before, but people are doing so ...

The problem is that key is only initialized if _dlerror_run is called.
The following patch should fix it:

2003-07-31  Jakub Jelinek  <jakub@redhat.com>

	* dlfcn/dlerror.c (once): New.
	(dlerror): Call __libc_once.
	(_dlerror_run): Remove once.

--- libc/dlfcn/dlerror.c.jj	2003-03-15 15:06:37.000000000 -0500
+++ libc/dlfcn/dlerror.c	2003-07-31 13:09:44.000000000 -0400
@@ -1,5 +1,5 @@
 /* Return error detail for failing <dlfcn.h> functions.
-   Copyright (C) 1995,1996,1997,1998,1999,2000,2002, 2003
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2002,2003
 	Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
@@ -38,6 +38,7 @@ static struct dl_action_result *static_b
 
 /* This is the key for the thread specific memory.  */
 static __libc_key_t key;
+__libc_once_define (static, once);
 
 /* Destructor for the thread-specific data.  */
 static void init (void);
@@ -50,6 +51,9 @@ dlerror (void)
   char *buf = NULL;
   struct dl_action_result *result;
 
+  /* If we have not yet initialized the buffer do it now.  */
+  __libc_once (once, init);
+
   /* Get error string.  */
   result = (struct dl_action_result *) __libc_getspecific (key);
   if (result == NULL)
@@ -100,7 +104,6 @@ int
 internal_function
 _dlerror_run (void (*operate) (void *), void *args)
 {
-  __libc_once_define (static, once);
   struct dl_action_result *result;
 
   /* If we have not yet initialized the buffer do it now.  */


	Jakub

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

end of thread, other threads:[~2003-07-31 17:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-07-31 14:28 C++/dlerror/pthread problem Thorsten Kukuk
2003-07-31 16:28 ` David Mosberger
2003-07-31 17:16 ` [PATCH] Fix dlerror (was Re: C++/dlerror/pthread problem) Jakub Jelinek

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).