On Mon, Mar 04, 2013 at 17:46, sven falempin wrote:
> Dear misc readers,
> 
> I have a home small c++ program, i used it for a while with no fuss and use
> the -static on my command line.

> Today i correct a 'feature' in the program (like deep inside), pass unit
> test and then rebuilt the all just
> like before on openbsd 5.2
> and now it crash before the main routine in pthread_self (librthread)

> Program received signal SIGSEGV, Segmentation fault.
> pthread_self () at /usr/src/lib/librthread/rthread.c:247
> 247     /usr/src/lib/librthread/rthread.c: No such file or directory.
> in /usr/src/lib/librthread/rthread.c
> Current language:  auto; currently c

Looks like there could be a bug.  The TCB for the main thread is
set by a constructor in rthread (_rthread_initlib).  If that's not
running, bad things would happen.  You mention the crash is before
main, in one of your constructors which is now calling into the
pthread code before everything is ready.

I think a patch like this will work.

Index: rthread.c
===================================================================
RCS file: /cvs/src/lib/librthread/rthread.c,v
retrieving revision 1.68
diff -u -p -r1.68 rthread.c
--- rthread.c   15 Feb 2013 22:01:24 -0000      1.68
+++ rthread.c   6 Mar 2013 03:17:18 -0000
@@ -100,11 +100,15 @@ _spinunlock(_spinlock_lock_t *lock)
 void _rthread_initlib(void) __attribute__((constructor));
 void _rthread_initlib(void)
 {
+       static int done;
        struct thread_control_block *tcb = &_initial_thread_tcb;
 
+       if (done)
+               return;
        /* use libc's errno for the main thread */
        TCB_INIT(tcb, &_initial_thread, ___errno());
        TCB_SET(tcb);
+       done = 1;
 }
 
 int *
@@ -162,6 +166,8 @@ _rthread_init(void)
 {
        pthread_t thread = &_initial_thread;
        struct sigaction sa;
+
+       _rthread_initlib(); /* in case of racing constructors */
 
        thread->tid = getthrid();
        thread->donesem.lock = _SPINLOCK_UNLOCKED;

Reply via email to