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