fix ordering of shared library ctors with respect to libc init
[musl] / src / ldso / dynlink.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 #include <elf.h>
8 #include <sys/mman.h>
9 #include <limits.h>
10 #include <stdint.h>
11 #include <fcntl.h>
12 #include <sys/stat.h>
13 #include <errno.h>
14 #include <limits.h>
15 #include <elf.h>
16 #include <link.h>
17 #include <setjmp.h>
18 #include <pthread.h>
19 #include <ctype.h>
20 #include <dlfcn.h>
21 #include "pthread_impl.h"
22 #include "libc.h"
23
24 static int errflag;
25 static char errbuf[128];
26
27 #ifdef SHARED
28
29 #if ULONG_MAX == 0xffffffff
30 typedef Elf32_Ehdr Ehdr;
31 typedef Elf32_Phdr Phdr;
32 typedef Elf32_Sym Sym;
33 #define R_TYPE(x) ((x)&255)
34 #define R_SYM(x) ((x)>>8)
35 #else
36 typedef Elf64_Ehdr Ehdr;
37 typedef Elf64_Phdr Phdr;
38 typedef Elf64_Sym Sym;
39 #define R_TYPE(x) ((x)&0xffffffff)
40 #define R_SYM(x) ((x)>>32)
41 #endif
42
43 #define MAXP2(a,b) (-(-(a)&-(b)))
44 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
45
46 struct debug {
47         int ver;
48         void *head;
49         void (*bp)(void);
50         int state;
51         void *base;
52 };
53
54 struct dso {
55         unsigned char *base;
56         char *name;
57         size_t *dynv;
58         struct dso *next, *prev;
59
60         Phdr *phdr;
61         int phnum;
62         int refcnt;
63         Sym *syms;
64         uint32_t *hashtab;
65         uint32_t *ghashtab;
66         char *strings;
67         unsigned char *map;
68         size_t map_len;
69         dev_t dev;
70         ino_t ino;
71         signed char global;
72         char relocated;
73         char constructed;
74         struct dso **deps;
75         void *tls_image;
76         size_t tls_len, tls_size, tls_align, tls_id, tls_offset;
77         void **new_dtv;
78         unsigned char *new_tls;
79         int new_dtv_idx, new_tls_idx;
80         struct dso *fini_next;
81         char *shortname;
82         char buf[];
83 };
84
85 struct symdef {
86         Sym *sym;
87         struct dso *dso;
88 };
89
90 #include "reloc.h"
91
92 void __init_ssp(size_t *);
93 void *__install_initial_tls(void *);
94
95 static struct dso *head, *tail, *ldso, *fini_head;
96 static char *env_path, *sys_path, *r_path;
97 static unsigned long long gencnt;
98 static int ssp_used;
99 static int runtime;
100 static int ldd_mode;
101 static int ldso_fail;
102 static jmp_buf rtld_fail;
103 static pthread_rwlock_t lock;
104 static struct debug debug;
105 static size_t tls_cnt, tls_offset, tls_align = 4*sizeof(size_t);
106 static pthread_mutex_t init_fini_lock = { ._m_type = PTHREAD_MUTEX_RECURSIVE };
107
108 struct debug *_dl_debug_addr = &debug;
109
110 #define AUX_CNT 38
111 #define DYN_CNT 34
112
113 static void decode_vec(size_t *v, size_t *a, size_t cnt)
114 {
115         memset(a, 0, cnt*sizeof(size_t));
116         for (; v[0]; v+=2) if (v[0]<cnt) {
117                 a[0] |= 1ULL<<v[0];
118                 a[v[0]] = v[1];
119         }
120 }
121
122 static int search_vec(size_t *v, size_t *r, size_t key)
123 {
124         for (; v[0]!=key; v+=2)
125                 if (!v[0]) return 0;
126         *r = v[1];
127         return 1;
128 }
129
130 static uint32_t sysv_hash(const char *s0)
131 {
132         const unsigned char *s = (void *)s0;
133         uint_fast32_t h = 0;
134         while (*s) {
135                 h = 16*h + *s++;
136                 h ^= h>>24 & 0xf0;
137         }
138         return h & 0xfffffff;
139 }
140
141 static uint32_t gnu_hash(const char *s0)
142 {
143         const unsigned char *s = (void *)s0;
144         uint_fast32_t h = 5381;
145         for (; *s; s++)
146                 h = h*33 + *s;
147         return h;
148 }
149
150 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
151 {
152         size_t i;
153         Sym *syms = dso->syms;
154         uint32_t *hashtab = dso->hashtab;
155         char *strings = dso->strings;
156         for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
157                 if (!strcmp(s, strings+syms[i].st_name))
158                         return syms+i;
159         }
160         return 0;
161 }
162
163 static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
164 {
165         Sym *sym;
166         char *strings;
167         uint32_t *hashtab = dso->ghashtab;
168         uint32_t nbuckets = hashtab[0];
169         uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
170         uint32_t h2;
171         uint32_t *hashval;
172         uint32_t n = buckets[h1 % nbuckets];
173
174         if (!n) return 0;
175
176         strings = dso->strings;
177         sym = dso->syms + n;
178         hashval = buckets + nbuckets + (n - hashtab[1]);
179
180         for (h1 |= 1; ; sym++) {
181                 h2 = *hashval++;
182                 if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name))
183                         return sym;
184                 if (h2 & 1) break;
185         }
186
187         return 0;
188 }
189
190 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
191 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
192
193 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
194 {
195         uint32_t h = 0, gh = 0;
196         struct symdef def = {0};
197         if (dso->ghashtab) {
198                 gh = gnu_hash(s);
199                 if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
200         } else {
201                 h = sysv_hash(s);
202                 if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
203         }
204         for (; dso; dso=dso->next) {
205                 Sym *sym;
206                 if (!dso->global) continue;
207                 if (dso->ghashtab) {
208                         if (!gh) gh = gnu_hash(s);
209                         sym = gnu_lookup(s, gh, dso);
210                 } else {
211                         if (!h) h = sysv_hash(s);
212                         sym = sysv_lookup(s, h, dso);
213                 }
214                 if (!sym) continue;
215                 if (!sym->st_shndx)
216                         if (need_def || (sym->st_info&0xf) == STT_TLS)
217                                 continue;
218                 if (!sym->st_value)
219                         if ((sym->st_info&0xf) != STT_TLS)
220                                 continue;
221                 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
222                 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
223
224                 if (def.sym && sym->st_info>>4 == STB_WEAK) continue;
225                 def.sym = sym;
226                 def.dso = dso;
227                 if (sym->st_info>>4 == STB_GLOBAL) break;
228         }
229         return def;
230 }
231
232 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
233 {
234         unsigned char *base = dso->base;
235         Sym *syms = dso->syms;
236         char *strings = dso->strings;
237         Sym *sym;
238         const char *name;
239         void *ctx;
240         int type;
241         int sym_index;
242         struct symdef def;
243
244         for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
245                 type = R_TYPE(rel[1]);
246                 sym_index = R_SYM(rel[1]);
247                 if (sym_index) {
248                         sym = syms + sym_index;
249                         name = strings + sym->st_name;
250                         ctx = IS_COPY(type) ? head->next : head;
251                         def = find_sym(ctx, name, IS_PLT(type));
252                         if (!def.sym && sym->st_info>>4 != STB_WEAK) {
253                                 snprintf(errbuf, sizeof errbuf,
254                                         "Error relocating %s: %s: symbol not found",
255                                         dso->name, name);
256                                 if (runtime) longjmp(rtld_fail, 1);
257                                 dprintf(2, "%s\n", errbuf);
258                                 ldso_fail = 1;
259                                 continue;
260                         }
261                 } else {
262                         sym = 0;
263                         def.sym = 0;
264                         def.dso = 0;
265                 }
266                 do_single_reloc(dso, base, (void *)(base + rel[0]), type,
267                         stride>2 ? rel[2] : 0, sym, sym?sym->st_size:0, def,
268                         def.sym?(size_t)(def.dso->base+def.sym->st_value):0);
269         }
270 }
271
272 /* A huge hack: to make up for the wastefulness of shared libraries
273  * needing at least a page of dirty memory even if they have no global
274  * data, we reclaim the gaps at the beginning and end of writable maps
275  * and "donate" them to the heap by setting up minimal malloc
276  * structures and then freeing them. */
277
278 static void reclaim(unsigned char *base, size_t start, size_t end)
279 {
280         size_t *a, *z;
281         start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
282         end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
283         if (start>end || end-start < 4*sizeof(size_t)) return;
284         a = (size_t *)(base + start);
285         z = (size_t *)(base + end);
286         a[-2] = 1;
287         a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
288         z[1] = 1;
289         free(a);
290 }
291
292 static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
293 {
294         for (; phcnt--; ph=(void *)((char *)ph+phent)) {
295                 if (ph->p_type!=PT_LOAD) continue;
296                 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
297                 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
298                 reclaim(base, ph->p_vaddr+ph->p_memsz,
299                         ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
300         }
301 }
302
303 static void *map_library(int fd, struct dso *dso)
304 {
305         Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
306         size_t phsize;
307         size_t addr_min=SIZE_MAX, addr_max=0, map_len;
308         size_t this_min, this_max;
309         off_t off_start;
310         Ehdr *eh;
311         Phdr *ph;
312         unsigned prot;
313         unsigned char *map, *base;
314         size_t dyn;
315         size_t tls_image=0;
316         size_t i;
317
318         ssize_t l = read(fd, buf, sizeof buf);
319         if (l<sizeof *eh) return 0;
320         eh = buf;
321         phsize = eh->e_phentsize * eh->e_phnum;
322         if (phsize + sizeof *eh > l) return 0;
323         if (eh->e_phoff + phsize > l) {
324                 l = pread(fd, buf+1, phsize, eh->e_phoff);
325                 if (l != phsize) return 0;
326                 eh->e_phoff = sizeof *eh;
327         }
328         ph = (void *)((char *)buf + eh->e_phoff);
329         dso->phdr = ph;
330         dso->phnum = eh->e_phnum;
331         for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
332                 if (ph->p_type == PT_DYNAMIC)
333                         dyn = ph->p_vaddr;
334                 if (ph->p_type == PT_TLS) {
335                         tls_image = ph->p_vaddr;
336                         dso->tls_align = ph->p_align;
337                         dso->tls_len = ph->p_filesz;
338                         dso->tls_size = ph->p_memsz;
339                 }
340                 if (ph->p_type != PT_LOAD) continue;
341                 if (ph->p_vaddr < addr_min) {
342                         addr_min = ph->p_vaddr;
343                         off_start = ph->p_offset;
344                         prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
345                                 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
346                                 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
347                 }
348                 if (ph->p_vaddr+ph->p_memsz > addr_max) {
349                         addr_max = ph->p_vaddr+ph->p_memsz;
350                 }
351         }
352         if (!dyn) return 0;
353         addr_max += PAGE_SIZE-1;
354         addr_max &= -PAGE_SIZE;
355         addr_min &= -PAGE_SIZE;
356         off_start &= -PAGE_SIZE;
357         map_len = addr_max - addr_min + off_start;
358         /* The first time, we map too much, possibly even more than
359          * the length of the file. This is okay because we will not
360          * use the invalid part; we just need to reserve the right
361          * amount of virtual address space to map over later. */
362         map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
363         if (map==MAP_FAILED) return 0;
364         base = map - addr_min;
365         ph = (void *)((char *)buf + eh->e_phoff);
366         for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
367                 if (ph->p_type != PT_LOAD) continue;
368                 /* Reuse the existing mapping for the lowest-address LOAD */
369                 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
370                 this_min = ph->p_vaddr & -PAGE_SIZE;
371                 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
372                 off_start = ph->p_offset & -PAGE_SIZE;
373                 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
374                         ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
375                         ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
376                 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
377                         goto error;
378                 if (ph->p_memsz > ph->p_filesz) {
379                         size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
380                         size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
381                         memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
382                         if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
383                                 goto error;
384                 }
385         }
386         for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
387                 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
388                         if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
389                                 goto error;
390                         break;
391                 }
392         if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
393                 eh->e_phentsize, eh->e_phnum);
394         dso->map = map;
395         dso->map_len = map_len;
396         dso->base = base;
397         dso->dynv = (void *)(base+dyn);
398         if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
399         return map;
400 error:
401         munmap(map, map_len);
402         return 0;
403 }
404
405 static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
406 {
407         const char *s=search, *z;
408         int l, fd;
409         for (;;) {
410                 while (*s==':') s++;
411                 if (!*s) return -1;
412                 z = strchr(s, ':');
413                 l = z ? z-s : strlen(s);
414                 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
415                 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
416                 s += l;
417         }
418 }
419
420 static void decode_dyn(struct dso *p)
421 {
422         size_t dyn[DYN_CNT] = {0};
423         decode_vec(p->dynv, dyn, DYN_CNT);
424         p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
425         p->strings = (void *)(p->base + dyn[DT_STRTAB]);
426         if (dyn[0]&(1<<DT_HASH))
427                 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
428         if (search_vec(p->dynv, dyn, DT_GNU_HASH))
429                 p->ghashtab = (void *)(p->base + *dyn);
430 }
431
432 static struct dso *load_library(const char *name)
433 {
434         char buf[2*NAME_MAX+2];
435         const char *pathname;
436         unsigned char *base, *map;
437         size_t map_len;
438         struct dso *p, temp_dso = {0};
439         int fd;
440         struct stat st;
441         size_t alloc_size;
442         int n_th = 0;
443
444         /* Catch and block attempts to reload the implementation itself */
445         if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
446                 static const char *rp, reserved[] =
447                         "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
448                 char *z = strchr(name, '.');
449                 if (z) {
450                         size_t l = z-name;
451                         for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
452                         if (*rp) {
453                                 if (!ldso->prev) {
454                                         tail->next = ldso;
455                                         ldso->prev = tail;
456                                         tail = ldso->next ? ldso->next : ldso;
457                                 }
458                                 return ldso;
459                         }
460                 }
461         }
462         if (strchr(name, '/')) {
463                 pathname = name;
464                 fd = open(name, O_RDONLY|O_CLOEXEC);
465         } else {
466                 /* Search for the name to see if it's already loaded */
467                 for (p=head->next; p; p=p->next) {
468                         if (p->shortname && !strcmp(p->shortname, name)) {
469                                 p->refcnt++;
470                                 return p;
471                         }
472                 }
473                 if (strlen(name) > NAME_MAX) return 0;
474                 fd = -1;
475                 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
476                 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
477                 if (fd < 0) {
478                         if (!sys_path) {
479                                 FILE *f = fopen(ETC_LDSO_PATH, "rbe");
480                                 if (f) {
481                                         if (getline(&sys_path, (size_t[1]){0}, f) > 0) {
482                                                 size_t l = strlen(sys_path);
483                                                 if (l && sys_path[l-1]=='\n')
484                                                         sys_path[l-1] = 0;
485                                         }
486                                         fclose(f);
487                                 }
488                         }
489                         if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
490                         fd = path_open(name, sys_path, buf, sizeof buf);
491                 }
492                 pathname = buf;
493         }
494         if (fd < 0) return 0;
495         if (fstat(fd, &st) < 0) {
496                 close(fd);
497                 return 0;
498         }
499         for (p=head->next; p; p=p->next) {
500                 if (p->dev == st.st_dev && p->ino == st.st_ino) {
501                         /* If this library was previously loaded with a
502                          * pathname but a search found the same inode,
503                          * setup its shortname so it can be found by name. */
504                         if (!p->shortname && pathname != name)
505                                 p->shortname = strrchr(p->name, '/')+1;
506                         close(fd);
507                         p->refcnt++;
508                         return p;
509                 }
510         }
511         map = map_library(fd, &temp_dso);
512         close(fd);
513         if (!map) return 0;
514
515         /* Allocate storage for the new DSO. When there is TLS, this
516          * storage must include a reservation for all pre-existing
517          * threads to obtain copies of both the new TLS, and an
518          * extended DTV capable of storing an additional slot for
519          * the newly-loaded DSO. */
520         alloc_size = sizeof *p + strlen(pathname) + 1;
521         if (runtime && temp_dso.tls_image) {
522                 size_t per_th = temp_dso.tls_size + temp_dso.tls_align
523                         + sizeof(void *) * (tls_cnt+3);
524                 n_th = libc.threads_minus_1 + 1;
525                 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
526                 else alloc_size += n_th * per_th;
527         }
528         p = calloc(1, alloc_size);
529         if (!p) {
530                 munmap(map, map_len);
531                 return 0;
532         }
533         memcpy(p, &temp_dso, sizeof temp_dso);
534         decode_dyn(p);
535         p->dev = st.st_dev;
536         p->ino = st.st_ino;
537         p->refcnt = 1;
538         p->name = p->buf;
539         strcpy(p->name, pathname);
540         /* Add a shortname only if name arg was not an explicit pathname. */
541         if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
542         if (p->tls_image) {
543                 if (runtime && !__pthread_self_init()) {
544                         free(p);
545                         munmap(map, map_len);
546                         return 0;
547                 }
548                 p->tls_id = ++tls_cnt;
549                 tls_align = MAXP2(tls_align, p->tls_align);
550 #ifdef TLS_ABOVE_TP
551                 p->tls_offset = tls_offset + ( (tls_align-1) &
552                         -(tls_offset + (uintptr_t)p->tls_image) );
553                 tls_offset += p->tls_size;
554 #else
555                 tls_offset += p->tls_size + p->tls_align - 1;
556                 tls_offset -= (tls_offset + (uintptr_t)p->tls_image)
557                         & (p->tls_align-1);
558                 p->tls_offset = tls_offset;
559 #endif
560                 p->new_dtv = (void *)(-sizeof(size_t) &
561                         (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
562                 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
563         }
564
565         tail->next = p;
566         p->prev = tail;
567         tail = p;
568
569         if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base);
570
571         return p;
572 }
573
574 static void load_deps(struct dso *p)
575 {
576         size_t i, ndeps=0;
577         struct dso ***deps = &p->deps, **tmp, *dep;
578         for (; p; p=p->next) {
579                 for (i=0; p->dynv[i]; i+=2) {
580                         if (p->dynv[i] != DT_RPATH) continue;
581                         r_path = (void *)(p->strings + p->dynv[i+1]);
582                 }
583                 for (i=0; p->dynv[i]; i+=2) {
584                         if (p->dynv[i] != DT_NEEDED) continue;
585                         dep = load_library(p->strings + p->dynv[i+1]);
586                         if (!dep) {
587                                 snprintf(errbuf, sizeof errbuf,
588                                         "Error loading shared library %s: %m (needed by %s)",
589                                         p->strings + p->dynv[i+1], p->name);
590                                 if (runtime) longjmp(rtld_fail, 1);
591                                 dprintf(2, "%s\n", errbuf);
592                                 ldso_fail = 1;
593                                 continue;
594                         }
595                         if (runtime) {
596                                 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
597                                 if (!tmp) longjmp(rtld_fail, 1);
598                                 tmp[ndeps++] = dep;
599                                 tmp[ndeps] = 0;
600                                 *deps = tmp;
601                         }
602                 }
603                 r_path = 0;
604         }
605 }
606
607 static void load_preload(char *s)
608 {
609         int tmp;
610         char *z;
611         for (z=s; *z; s=z) {
612                 for (   ; *s && isspace(*s); s++);
613                 for (z=s; *z && !isspace(*z); z++);
614                 tmp = *z;
615                 *z = 0;
616                 load_library(s);
617                 *z = tmp;
618         }
619 }
620
621 static void make_global(struct dso *p)
622 {
623         for (; p; p=p->next) p->global = 1;
624 }
625
626 static void reloc_all(struct dso *p)
627 {
628         size_t dyn[DYN_CNT] = {0};
629         for (; p; p=p->next) {
630                 if (p->relocated) continue;
631                 decode_vec(p->dynv, dyn, DYN_CNT);
632 #ifdef NEED_ARCH_RELOCS
633                 do_arch_relocs(p, head);
634 #endif
635                 do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
636                         2+(dyn[DT_PLTREL]==DT_RELA));
637                 do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2);
638                 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
639                 p->relocated = 1;
640         }
641 }
642
643 static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
644 {
645         for (; cnt--; ph = (void *)((char *)ph + stride))
646                 if (ph->p_type == PT_DYNAMIC)
647                         return ph->p_vaddr;
648         return 0;
649 }
650
651 static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p)
652 {
653         size_t min_addr = -1, max_addr = 0;
654         for (; cnt--; ph = (void *)((char *)ph + stride)) {
655                 if (ph->p_type != PT_LOAD) continue;
656                 if (ph->p_vaddr < min_addr)
657                         min_addr = ph->p_vaddr;
658                 if (ph->p_vaddr+ph->p_memsz > max_addr)
659                         max_addr = ph->p_vaddr+ph->p_memsz;
660         }
661         min_addr &= -PAGE_SIZE;
662         max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
663         p->map = p->base + min_addr;
664         p->map_len = max_addr - min_addr;
665 }
666
667 static void do_fini()
668 {
669         struct dso *p;
670         size_t dyn[DYN_CNT] = {0};
671         for (p=fini_head; p; p=p->fini_next) {
672                 if (!p->constructed) continue;
673                 decode_vec(p->dynv, dyn, DYN_CNT);
674                 ((void (*)(void))(p->base + dyn[DT_FINI]))();
675         }
676 }
677
678 static void do_init_fini(struct dso *p)
679 {
680         size_t dyn[DYN_CNT] = {0};
681         int need_locking = libc.threads_minus_1;
682         /* Allow recursive calls that arise when a library calls
683          * dlopen from one of its constructors, but block any
684          * other threads until all ctors have finished. */
685         if (need_locking) pthread_mutex_lock(&init_fini_lock);
686         for (; p; p=p->prev) {
687                 if (p->constructed) continue;
688                 p->constructed = 1;
689                 decode_vec(p->dynv, dyn, DYN_CNT);
690                 if (dyn[0] & (1<<DT_FINI)) {
691                         p->fini_next = fini_head;
692                         fini_head = p;
693                 }
694                 if (dyn[0] & (1<<DT_INIT))
695                         ((void (*)(void))(p->base + dyn[DT_INIT]))();
696         }
697         if (need_locking) pthread_mutex_unlock(&init_fini_lock);
698 }
699
700 void _dl_debug_state(void)
701 {
702 }
703
704 void *__copy_tls(unsigned char *mem)
705 {
706         pthread_t td;
707         struct dso *p;
708
709         if (!tls_cnt) return mem;
710
711         void **dtv = (void *)mem;
712         dtv[0] = (void *)tls_cnt;
713
714 #ifdef TLS_ABOVE_TP
715         mem += sizeof(void *) * (tls_cnt+1);
716         mem += -((uintptr_t)mem + sizeof(struct pthread)) & (tls_align-1);
717         td = (pthread_t)mem;
718         mem += sizeof(struct pthread);
719
720         for (p=head; p; p=p->next) {
721                 if (!p->tls_id) continue;
722                 dtv[p->tls_id] = mem + p->tls_offset;
723                 memcpy(dtv[p->tls_id], p->tls_image, p->tls_len);
724         }
725 #else
726         mem += libc.tls_size - sizeof(struct pthread);
727         mem -= (uintptr_t)mem & (tls_align-1);
728         td = (pthread_t)mem;
729
730         for (p=head; p; p=p->next) {
731                 if (!p->tls_id) continue;
732                 dtv[p->tls_id] = mem - p->tls_offset;
733                 memcpy(dtv[p->tls_id], p->tls_image, p->tls_len);
734         }
735 #endif
736         td->dtv = dtv;
737         return td;
738 }
739
740 void *__tls_get_addr(size_t *v)
741 {
742         pthread_t self = __pthread_self();
743         if (self->dtv && v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]])
744                 return (char *)self->dtv[v[0]]+v[1];
745
746         /* Block signals to make accessing new TLS async-signal-safe */
747         sigset_t set;
748         pthread_sigmask(SIG_BLOCK, SIGALL_SET, &set);
749         if (self->dtv && v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]]) {
750                 pthread_sigmask(SIG_SETMASK, &set, 0);
751                 return (char *)self->dtv[v[0]]+v[1];
752         }
753
754         /* This is safe without any locks held because, if the caller
755          * is able to request the Nth entry of the DTV, the DSO list
756          * must be valid at least that far out and it was synchronized
757          * at program startup or by an already-completed call to dlopen. */
758         struct dso *p;
759         for (p=head; p->tls_id != v[0]; p=p->next);
760
761         /* Get new DTV space from new DSO if needed */
762         if (!self->dtv || v[0] > (size_t)self->dtv[0]) {
763                 void **newdtv = p->new_dtv +
764                         (v[0]+1)*sizeof(void *)*a_fetch_add(&p->new_dtv_idx,1);
765                 if (self->dtv) memcpy(newdtv, self->dtv,
766                         ((size_t)self->dtv[0]+1) * sizeof(void *));
767                 newdtv[0] = (void *)v[0];
768                 self->dtv = newdtv;
769         }
770
771         /* Get new TLS memory from new DSO */
772         unsigned char *mem = p->new_tls +
773                 (p->tls_size + p->tls_align) * a_fetch_add(&p->new_tls_idx,1);
774         mem += ((uintptr_t)p->tls_image - (uintptr_t)mem) & (p->tls_align-1);
775         self->dtv[v[0]] = mem;
776         memcpy(mem, p->tls_image, p->tls_len);
777         pthread_sigmask(SIG_SETMASK, &set, 0);
778         return mem + v[1];
779 }
780
781 static void update_tls_size()
782 {
783         libc.tls_size = ALIGN(
784                 (1+tls_cnt) * sizeof(void *) +
785                 tls_offset +
786                 sizeof(struct pthread) +
787                 tls_align * 2,
788         tls_align);
789 }
790
791 void *__dynlink(int argc, char **argv)
792 {
793         size_t aux[AUX_CNT] = {0};
794         size_t i;
795         Phdr *phdr;
796         Ehdr *ehdr;
797         static struct dso builtin_dsos[3];
798         struct dso *const app = builtin_dsos+0;
799         struct dso *const lib = builtin_dsos+1;
800         struct dso *const vdso = builtin_dsos+2;
801         char *env_preload=0;
802         size_t vdso_base;
803         size_t *auxv;
804
805         /* Find aux vector just past environ[] */
806         for (i=argc+1; argv[i]; i++)
807                 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
808                         env_path = argv[i]+16;
809                 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
810                         env_preload = argv[i]+11;
811         auxv = (void *)(argv+i+1);
812
813         decode_vec(auxv, aux, AUX_CNT);
814
815         /* Only trust user/env if kernel says we're not suid/sgid */
816         if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
817           || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
818                 env_path = 0;
819                 env_preload = 0;
820         }
821
822         /* If the dynamic linker was invoked as a program itself, AT_BASE
823          * will not be set. In that case, we assume the base address is
824          * the start of the page containing the PHDRs; I don't know any
825          * better approach... */
826         if (!aux[AT_BASE]) {
827                 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
828                 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
829         }
830
831         /* The dynamic linker load address is passed by the kernel
832          * in the AUX vector, so this is easy. */
833         lib->base = (void *)aux[AT_BASE];
834         lib->name = lib->shortname = "libc.so";
835         lib->global = 1;
836         ehdr = (void *)lib->base;
837         lib->phnum = ehdr->e_phnum;
838         lib->phdr = (void *)(aux[AT_BASE]+ehdr->e_phoff);
839         find_map_range(lib->phdr, ehdr->e_phnum, ehdr->e_phentsize, lib);
840         lib->dynv = (void *)(lib->base + find_dyn(lib->phdr,
841                     ehdr->e_phnum, ehdr->e_phentsize));
842         decode_dyn(lib);
843
844         if (aux[AT_PHDR]) {
845                 size_t interp_off = 0;
846                 size_t tls_image = 0;
847                 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
848                 app->phdr = phdr = (void *)aux[AT_PHDR];
849                 app->phnum = aux[AT_PHNUM];
850                 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
851                         if (phdr->p_type == PT_PHDR)
852                                 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
853                         else if (phdr->p_type == PT_INTERP)
854                                 interp_off = (size_t)phdr->p_vaddr;
855                         else if (phdr->p_type == PT_TLS) {
856                                 tls_image = phdr->p_vaddr;
857                                 app->tls_len = phdr->p_filesz;
858                                 app->tls_size = phdr->p_memsz;
859                                 app->tls_align = phdr->p_align;
860                         }
861                 }
862                 if (app->tls_size) app->tls_image = (char *)app->base + tls_image;
863                 if (interp_off) lib->name = (char *)app->base + interp_off;
864                 app->name = argv[0];
865                 app->dynv = (void *)(app->base + find_dyn(
866                         (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
867                 find_map_range((void *)aux[AT_PHDR],
868                         aux[AT_PHNUM], aux[AT_PHENT], app);
869         } else {
870                 int fd;
871                 char *ldname = argv[0];
872                 size_t l = strlen(ldname);
873                 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
874                 *argv++ = (void *)-1;
875                 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
876                 if (!argv[0]) {
877                         dprintf(2, "musl libc/dynamic program loader\n");
878                         dprintf(2, "usage: %s pathname%s\n", ldname,
879                                 ldd_mode ? "" : " [args]");
880                         _exit(1);
881                 }
882                 fd = open(argv[0], O_RDONLY);
883                 if (fd < 0) {
884                         dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
885                         _exit(1);
886                 }
887                 runtime = 1;
888                 ehdr = (void *)map_library(fd, app);
889                 if (!ehdr) {
890                         dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
891                         _exit(1);
892                 }
893                 runtime = 0;
894                 close(fd);
895                 lib->name = ldname;
896                 app->name = argv[0];
897                 app->phnum = ehdr->e_phnum;
898                 app->phdr = (void *)(app->base + ehdr->e_phoff);
899                 aux[AT_ENTRY] = ehdr->e_entry;
900         }
901         if (app->tls_size) {
902                 app->tls_id = tls_cnt = 1;
903 #ifdef TLS_ABOVE_TP
904                 app->tls_offset = 0;
905                 tls_offset = app->tls_size
906                         + ( -((uintptr_t)app->tls_image + app->tls_size)
907                         & (app->tls_align-1) );
908 #else
909                 tls_offset = app->tls_offset = app->tls_size
910                         + ( -((uintptr_t)app->tls_image + app->tls_size)
911                         & (app->tls_align-1) );
912 #endif
913                 tls_align = MAXP2(tls_align, app->tls_align);
914         }
915         app->global = 1;
916         app->constructed = 1;
917         decode_dyn(app);
918
919         /* Attach to vdso, if provided by the kernel */
920         if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
921                 ehdr = (void *)vdso_base;
922                 vdso->phdr = phdr = (void *)(vdso_base + ehdr->e_phoff);
923                 vdso->phnum = ehdr->e_phnum;
924                 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
925                         if (phdr->p_type == PT_DYNAMIC)
926                                 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
927                         if (phdr->p_type == PT_LOAD)
928                                 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
929                 }
930                 vdso->name = "";
931                 vdso->shortname = "linux-gate.so.1";
932                 vdso->global = 1;
933                 decode_dyn(vdso);
934                 vdso->prev = lib;
935                 lib->next = vdso;
936         }
937
938         /* Initial dso chain consists only of the app. We temporarily
939          * append the dynamic linker/libc so we can relocate it, then
940          * restore the initial chain in preparation for loading third
941          * party libraries (preload/needed). */
942         head = tail = app;
943         ldso = lib;
944         app->next = lib;
945         reloc_all(lib);
946         app->next = 0;
947
948         /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
949
950         /* Donate unused parts of app and library mapping to malloc */
951         reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
952         ehdr = (void *)lib->base;
953         reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
954                 ehdr->e_phentsize, ehdr->e_phnum);
955
956         /* Load preload/needed libraries, add their symbols to the global
957          * namespace, and perform all remaining relocations. The main
958          * program must be relocated LAST since it may contain copy
959          * relocations which depend on libraries' relocations. */
960         if (env_preload) load_preload(env_preload);
961         load_deps(app);
962         make_global(app);
963
964         reloc_all(app->next);
965         reloc_all(app);
966
967         update_tls_size();
968         if (tls_cnt) {
969                 void *mem = mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
970                         MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
971                 if (mem==MAP_FAILED ||
972                     !__install_initial_tls(__copy_tls(mem))) {
973                         dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
974                                 argv[0], libc.tls_size);
975                         _exit(127);
976                 }
977         }
978
979         if (ldso_fail) _exit(127);
980         if (ldd_mode) _exit(0);
981
982         /* Switch to runtime mode: any further failures in the dynamic
983          * linker are a reportable failure rather than a fatal startup
984          * error. If the dynamic loader (dlopen) will not be used, free
985          * all memory used by the dynamic linker. */
986         runtime = 1;
987
988 #ifndef DYNAMIC_IS_RO
989         for (i=0; app->dynv[i]; i+=2)
990                 if (app->dynv[i]==DT_DEBUG)
991                         app->dynv[i+1] = (size_t)&debug;
992 #endif
993         debug.ver = 1;
994         debug.bp = _dl_debug_state;
995         debug.head = head;
996         debug.base = lib->base;
997         debug.state = 0;
998         _dl_debug_state();
999
1000         if (ssp_used) __init_ssp((void *)aux[AT_RANDOM]);
1001
1002         errno = 0;
1003         return (void *)aux[AT_ENTRY];
1004 }
1005
1006 void __init_ldso_ctors(void)
1007 {
1008         atexit(do_fini);
1009         do_init_fini(tail);
1010 }
1011
1012 void *dlopen(const char *file, int mode)
1013 {
1014         struct dso *volatile p, *orig_tail, *next;
1015         size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
1016         size_t i;
1017         int cs;
1018
1019         if (!file) return head;
1020
1021         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
1022         pthread_rwlock_wrlock(&lock);
1023         __inhibit_ptc();
1024
1025         p = 0;
1026         orig_tls_cnt = tls_cnt;
1027         orig_tls_offset = tls_offset;
1028         orig_tls_align = tls_align;
1029         orig_tail = tail;
1030
1031         if (setjmp(rtld_fail)) {
1032                 /* Clean up anything new that was (partially) loaded */
1033                 if (p && p->deps) for (i=0; p->deps[i]; i++)
1034                         if (p->deps[i]->global < 0)
1035                                 p->deps[i]->global = 0;
1036                 for (p=orig_tail->next; p; p=next) {
1037                         next = p->next;
1038                         munmap(p->map, p->map_len);
1039                         free(p->deps);
1040                         free(p);
1041                 }
1042                 tls_cnt = orig_tls_cnt;
1043                 tls_offset = orig_tls_offset;
1044                 tls_align = orig_tls_align;
1045                 tail = orig_tail;
1046                 tail->next = 0;
1047                 p = 0;
1048                 errflag = 1;
1049                 goto end;
1050         } else p = load_library(file);
1051
1052         if (!p) {
1053                 snprintf(errbuf, sizeof errbuf,
1054                         "Error loading shared library %s: %m", file);
1055                 errflag = 1;
1056                 goto end;
1057         }
1058
1059         /* First load handling */
1060         if (!p->deps) {
1061                 load_deps(p);
1062                 if (p->deps) for (i=0; p->deps[i]; i++)
1063                         if (!p->deps[i]->global)
1064                                 p->deps[i]->global = -1;
1065                 if (!p->global) p->global = -1;
1066                 reloc_all(p);
1067                 if (p->deps) for (i=0; p->deps[i]; i++)
1068                         if (p->deps[i]->global < 0)
1069                                 p->deps[i]->global = 0;
1070                 if (p->global < 0) p->global = 0;
1071         }
1072
1073         if (mode & RTLD_GLOBAL) {
1074                 if (p->deps) for (i=0; p->deps[i]; i++)
1075                         p->deps[i]->global = 1;
1076                 p->global = 1;
1077         }
1078
1079         update_tls_size();
1080
1081         if (ssp_used) __init_ssp(libc.auxv);
1082
1083         _dl_debug_state();
1084         orig_tail = tail;
1085 end:
1086         __release_ptc();
1087         if (p) gencnt++;
1088         pthread_rwlock_unlock(&lock);
1089         if (p) do_init_fini(orig_tail);
1090         pthread_setcancelstate(cs, 0);
1091         return p;
1092 }
1093
1094 static void *do_dlsym(struct dso *p, const char *s, void *ra)
1095 {
1096         size_t i;
1097         uint32_t h = 0, gh = 0;
1098         Sym *sym;
1099         if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
1100                 if (p == RTLD_DEFAULT) {
1101                         p = head;
1102                 } else if (p == RTLD_NEXT) {
1103                         for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
1104                         if (!p) p=head;
1105                         p = p->next;
1106                 }
1107                 struct symdef def = find_sym(p, s, 0);
1108                 if (!def.sym) goto failed;
1109                 if ((def.sym->st_info&0xf) == STT_TLS)
1110                         return __tls_get_addr((size_t []){def.dso->tls_id, def.sym->st_value});
1111                 return def.dso->base + def.sym->st_value;
1112         }
1113         if (p->ghashtab) {
1114                 gh = gnu_hash(s);
1115                 sym = gnu_lookup(s, gh, p);
1116         } else {
1117                 h = sysv_hash(s);
1118                 sym = sysv_lookup(s, h, p);
1119         }
1120         if (sym && (sym->st_info&0xf) == STT_TLS)
1121                 return __tls_get_addr((size_t []){p->tls_id, sym->st_value});
1122         if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1123                 return p->base + sym->st_value;
1124         if (p->deps) for (i=0; p->deps[i]; i++) {
1125                 if (p->deps[i]->ghashtab) {
1126                         if (!gh) gh = gnu_hash(s);
1127                         sym = gnu_lookup(s, gh, p->deps[i]);
1128                 } else {
1129                         if (!h) h = sysv_hash(s);
1130                         sym = sysv_lookup(s, h, p->deps[i]);
1131                 }
1132                 if (sym && (sym->st_info&0xf) == STT_TLS)
1133                         return __tls_get_addr((size_t []){p->deps[i]->tls_id, sym->st_value});
1134                 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1135                         return p->deps[i]->base + sym->st_value;
1136         }
1137 failed:
1138         errflag = 1;
1139         snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
1140         return 0;
1141 }
1142
1143 int __dladdr(void *addr, Dl_info *info)
1144 {
1145         struct dso *p;
1146         Sym *sym;
1147         uint32_t nsym;
1148         char *strings;
1149         size_t i;
1150         void *best = 0;
1151         char *bestname;
1152
1153         pthread_rwlock_rdlock(&lock);
1154         for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
1155         pthread_rwlock_unlock(&lock);
1156
1157         if (!p) return 0;
1158
1159         sym = p->syms;
1160         strings = p->strings;
1161         if (p->hashtab) {
1162                 nsym = p->hashtab[1];
1163         } else {
1164                 uint32_t *buckets;
1165                 uint32_t *hashval;
1166                 buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
1167                 sym += p->ghashtab[1];
1168                 for (i = 0; i < p->ghashtab[0]; i++) {
1169                         if (buckets[i] > nsym)
1170                                 nsym = buckets[i];
1171                 }
1172                 if (nsym) {
1173                         nsym -= p->ghashtab[1];
1174                         hashval = buckets + p->ghashtab[0] + nsym;
1175                         do nsym++;
1176                         while (!(*hashval++ & 1));
1177                 }
1178         }
1179
1180         for (; nsym; nsym--, sym++) {
1181                 if (sym->st_shndx && sym->st_value
1182                  && (1<<(sym->st_info&0xf) & OK_TYPES)
1183                  && (1<<(sym->st_info>>4) & OK_BINDS)) {
1184                         void *symaddr = p->base + sym->st_value;
1185                         if (symaddr > addr || symaddr < best)
1186                                 continue;
1187                         best = symaddr;
1188                         bestname = strings + sym->st_name;
1189                         if (addr == symaddr)
1190                                 break;
1191                 }
1192         }
1193
1194         if (!best) return 0;
1195
1196         info->dli_fname = p->name;
1197         info->dli_fbase = p->base;
1198         info->dli_sname = bestname;
1199         info->dli_saddr = best;
1200
1201         return 1;
1202 }
1203
1204 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1205 {
1206         void *res;
1207         pthread_rwlock_rdlock(&lock);
1208         res = do_dlsym(p, s, ra);
1209         pthread_rwlock_unlock(&lock);
1210         return res;
1211 }
1212
1213 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
1214 {
1215         struct dso *current;
1216         struct dl_phdr_info info;
1217         int ret = 0;
1218         for(current = head; current;) {
1219                 info.dlpi_addr      = (uintptr_t)current->base;
1220                 info.dlpi_name      = current->name;
1221                 info.dlpi_phdr      = current->phdr;
1222                 info.dlpi_phnum     = current->phnum;
1223                 info.dlpi_adds      = gencnt;
1224                 info.dlpi_subs      = 0;
1225                 info.dlpi_tls_modid = current->tls_id;
1226                 info.dlpi_tls_data  = current->tls_image;
1227
1228                 ret = (callback)(&info, sizeof (info), data);
1229
1230                 if (ret != 0) break;
1231
1232                 pthread_rwlock_rdlock(&lock);
1233                 current = current->next;
1234                 pthread_rwlock_unlock(&lock);
1235         }
1236         return ret;
1237 }
1238 #else
1239 void *dlopen(const char *file, int mode)
1240 {
1241         return 0;
1242 }
1243 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1244 {
1245         return 0;
1246 }
1247 int __dladdr (void *addr, Dl_info *info)
1248 {
1249         return 0;
1250 }
1251 #endif
1252
1253 char *dlerror()
1254 {
1255         if (!errflag) return 0;
1256         errflag = 0;
1257         return errbuf;
1258 }
1259
1260 int dlclose(void *p)
1261 {
1262         return 0;
1263 }