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