source: azure_iot_hub/trunk/musl-1.1.18/src/env/__init_tls.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 3.3 KB
Line 
1#include <elf.h>
2#include <limits.h>
3#include <sys/mman.h>
4#include <string.h>
5#include <stddef.h>
6#include "pthread_impl.h"
7#include "libc.h"
8#include "atomic.h"
9#include "syscall.h"
10
11int __init_tp(void *p)
12{
13 pthread_t td = p;
14 td->self = td;
15 int r = __set_thread_area(TP_ADJ(p));
16 if (r < 0) return -1;
17 if (!r) libc.can_do_threads = 1;
18 td->tid = __syscall(SYS_set_tid_address, &td->tid);
19 td->locale = &libc.global_locale;
20 td->robust_list.head = &td->robust_list.head;
21 return 0;
22}
23
24static struct builtin_tls {
25 char c;
26 struct pthread pt;
27 void *space[16];
28} builtin_tls[1];
29#define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
30
31static struct tls_module main_tls;
32
33void *__copy_tls(unsigned char *mem)
34{
35 pthread_t td;
36 struct tls_module *p;
37 size_t i;
38 void **dtv;
39
40#ifdef TLS_ABOVE_TP
41 dtv = (void **)(mem + libc.tls_size) - (libc.tls_cnt + 1);
42
43 mem += -((uintptr_t)mem + sizeof(struct pthread)) & (libc.tls_align-1);
44 td = (pthread_t)mem;
45 mem += sizeof(struct pthread);
46
47 for (i=1, p=libc.tls_head; p; i++, p=p->next) {
48 dtv[i] = mem + p->offset;
49 memcpy(dtv[i], p->image, p->len);
50 }
51#else
52 dtv = (void **)mem;
53
54 mem += libc.tls_size - sizeof(struct pthread);
55 mem -= (uintptr_t)mem & (libc.tls_align-1);
56 td = (pthread_t)mem;
57
58 for (i=1, p=libc.tls_head; p; i++, p=p->next) {
59 dtv[i] = mem - p->offset;
60 memcpy(dtv[i], p->image, p->len);
61 }
62#endif
63 dtv[0] = (void *)libc.tls_cnt;
64 td->dtv = td->dtv_copy = dtv;
65 return td;
66}
67
68#if ULONG_MAX == 0xffffffff
69typedef Elf32_Phdr Phdr;
70#else
71typedef Elf64_Phdr Phdr;
72#endif
73
74#ifndef __c2__
75__attribute__((__weak__, __visibility__("hidden")))
76#endif
77extern const size_t _DYNAMIC[];
78
79static void static_init_tls(size_t *aux)
80{
81 unsigned char *p;
82 size_t n;
83 Phdr *phdr, *tls_phdr=0;
84 size_t base = 0;
85 void *mem;
86
87 for (p=(void *)aux[AT_PHDR],n=aux[AT_PHNUM]; n; n--,p+=aux[AT_PHENT]) {
88 phdr = (void *)p;
89 if (phdr->p_type == PT_PHDR)
90 base = aux[AT_PHDR] - phdr->p_vaddr;
91 if (phdr->p_type == PT_DYNAMIC && _DYNAMIC)
92 base = (size_t)_DYNAMIC - phdr->p_vaddr;
93 if (phdr->p_type == PT_TLS)
94 tls_phdr = phdr;
95 }
96
97 if (tls_phdr) {
98 main_tls.image = (void *)(base + tls_phdr->p_vaddr);
99 main_tls.len = tls_phdr->p_filesz;
100 main_tls.size = tls_phdr->p_memsz;
101 main_tls.align = tls_phdr->p_align;
102 libc.tls_cnt = 1;
103 libc.tls_head = &main_tls;
104 }
105
106 main_tls.size += (-main_tls.size - (uintptr_t)main_tls.image)
107 & (main_tls.align-1);
108 if (main_tls.align < MIN_TLS_ALIGN) main_tls.align = MIN_TLS_ALIGN;
109#ifndef TLS_ABOVE_TP
110 main_tls.offset = main_tls.size;
111#endif
112
113 libc.tls_align = main_tls.align;
114 libc.tls_size = 2*sizeof(void *) + sizeof(struct pthread)
115 + main_tls.size + main_tls.align
116 + MIN_TLS_ALIGN-1 & -MIN_TLS_ALIGN;
117
118 if (libc.tls_size > sizeof builtin_tls) {
119#ifndef SYS_mmap2
120#define SYS_mmap2 SYS_mmap
121#endif
122 mem = (void *)__syscall(
123 SYS_mmap2,
124 0, libc.tls_size, PROT_READ|PROT_WRITE,
125 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
126 /* -4095...-1 cast to void * will crash on dereference anyway,
127 * so don't bloat the init code checking for error codes and
128 * explicitly calling a_crash(). */
129 } else {
130 mem = builtin_tls;
131 }
132
133 /* Failure to initialize thread pointer is always fatal. */
134 if (__init_tp(__copy_tls(mem)) < 0)
135 a_crash();
136}
137
138#ifndef __c2__
139weak_alias(static_init_tls, __init_tls);
140#else
141void __init_tls(size_t *aux)
142{
143 static_init_tls(aux);
144}
145#endif
Note: See TracBrowser for help on using the repository browser.