simple pthread program and static link

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

simple pthread program and static link

Atsushi Nemoto
When I compiled and linked this simple pthread program statically,
set_robust_list system call is never called.  (on i386-linux or
mips-linux)

#define _GNU_SOURCE
#include <pthread.h>

int main(int argc, char **argv)
{
        pthread_mutex_t mu;
        pthread_mutexattr_t attr;
        pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
        pthread_mutex_init(&mu, NULL);
        pthread_mutex_lock(&mu);
        pthread_mutex_unlock(&mu);
        return 0;
}

This is because nptl/init.o was not linked to the exectable.  No
problem if I linked it dynamically.

And this problem happens if I configured libc with
--enable-kernel=2.6.18 option.  If libc was configured with
--enable-kernel=2.6.12, nptl/init.o is linked as expected.

I suppose this version-dependency is because __set_robust_list_avail
in init.c is required if __ASSUME_SET_ROBUST_LIST was not defined.
(__ASSUME_SET_ROBUST_LIST is enabled by --enable-kernel=2.6.18)


How can I make sure nptl/init.o always linked to pthread programs
(even if it was statically linked) ?

Usually __static_tls_size, etc. are required by pthread_create() or
something, so nptl/init.o is linked anyway.  But if simple test
program like this did not work properly, it might surprise some
people.

---
Atsushi Nemoto
Reply | Threaded
Open this post in threaded view
|

Re: simple pthread program and static link

Jakub Jelinek
On Wed, Oct 03, 2007 at 11:10:30AM +0900, Atsushi Nemoto wrote:

> When I compiled and linked this simple pthread program statically,
> set_robust_list system call is never called.  (on i386-linux or
> mips-linux)
>
> #define _GNU_SOURCE
> #include <pthread.h>
>
> int main(int argc, char **argv)
> {
> pthread_mutex_t mu;
> pthread_mutexattr_t attr;
> pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP);
> pthread_mutex_init(&mu, NULL);
> pthread_mutex_lock(&mu);
> pthread_mutex_unlock(&mu);
> return 0;
> }

> Usually __static_tls_size, etc. are required by pthread_create() or
> something, so nptl/init.o is linked anyway.  But if simple test
> program like this did not work properly, it might surprise some
> people.

The answer is simple, don't link threaded programs statically, there are
many reasons why it is a wrong thing to do.
If you really must do so, the safest solution is
-Wl,--whole-archive -lpthread -Wl,--no-while-archive
while will bloat the program, but will ensure everything needed (and a lot
of other stuff) is linked in.

        Jakub
Reply | Threaded
Open this post in threaded view
|

Re: simple pthread program and static link

Atsushi Nemoto
On Wed, 3 Oct 2007 09:38:48 +0200, Jakub Jelinek <[hidden email]> wrote:

> > Usually __static_tls_size, etc. are required by pthread_create() or
> > something, so nptl/init.o is linked anyway.  But if simple test
> > program like this did not work properly, it might surprise some
> > people.
>
> The answer is simple, don't link threaded programs statically, there are
> many reasons why it is a wrong thing to do.
> If you really must do so, the safest solution is
> -Wl,--whole-archive -lpthread -Wl,--no-while-archive
> while will bloat the program, but will ensure everything needed (and a lot
> of other stuff) is linked in.

Thanks, I see.  I know static-link + nptl + gdb issue, but are there
so many other problems with static-link with nptl?  It would be useful
if problems or limitations with static-link were listed on somewhere.

---
Atsushi Nemoto