hurd: Avoid local PLTs in libpthread.
[glibc.git] / sysdeps / mach / hurd / htl / pt-sysdep.c
blob673f048c37cac39c6526276052b076d00b7bb04c
1 /* System dependent pthreads code. Hurd version.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <stddef.h>
21 #include <stdint.h>
23 #include <mach.h>
24 #include <mach/mig_support.h>
26 #include <pt-internal.h>
27 #include <pthreadP.h>
29 __thread struct __pthread *___pthread_self;
31 /* Forward. */
32 static void *init_routine (void);
34 /* OK, the name of this variable isn't really appropriate, but I don't
35 want to change it yet. */
36 void *(*_cthread_init_routine) (void) = &init_routine;
38 /* This function is called from the Hurd-specific startup code. It
39 should return a new stack pointer for the main thread. The caller
40 will switch to this new stack before doing anything serious. */
41 static void *
42 _init_routine (void *stack)
44 struct __pthread *thread;
45 int err;
46 pthread_attr_t attr, *attrp = 0;
48 if (__pthread_threads != NULL)
49 /* Already initialized */
50 return 0;
52 /* Initialize the library. */
53 ___pthread_init ();
55 if (stack != NULL)
57 /* We are getting initialized due to dlopening a library using libpthread
58 while the main program was not linked against libpthread. */
59 /* Avoid allocating another stack */
60 attrp = &attr;
61 __pthread_attr_init (attrp);
62 __pthread_attr_setstack (attrp, stack, __vm_page_size);
65 /* Create the pthread structure for the main thread (i.e. us). */
66 err = __pthread_create_internal (&thread, attrp, 0, 0);
67 assert_perror (err);
69 /* XXX The caller copies the command line arguments and the environment
70 to the new stack. Pretend it wasn't allocated so that it remains
71 valid if the main thread terminates. */
72 thread->stack = 0;
74 ___pthread_self = thread;
76 /* Decrease the number of threads, to take into account that the
77 signal thread (which will be created by the glibc startup code
78 when we return from here) shouldn't be seen as a user thread. */
79 __pthread_total--;
81 /* Make MiG code thread aware. */
82 __mig_init (thread->stackaddr);
84 return thread->mcontext.sp;
87 static void *
88 init_routine (void)
90 return _init_routine (0);
93 #ifdef SHARED
94 __attribute__ ((constructor))
95 static void
96 dynamic_init_routine (void)
98 _init_routine (__libc_stack_end);
100 #endif