refactor some more dynamic linker load address computations
[oweals/musl.git] / src / ldso / dynlink.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <stddef.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdint.h>
9 #include <elf.h>
10 #include <sys/mman.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14 #include <errno.h>
15 #include <link.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 #include "dynlink.h"
23
24 static void error(const char *, ...);
25
26 #ifdef SHARED
27
28 #define MAXP2(a,b) (-(-(a)&-(b)))
29 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
30
31 struct debug {
32         int ver;
33         void *head;
34         void (*bp)(void);
35         int state;
36         void *base;
37 };
38
39 struct td_index {
40         size_t args[2];
41         struct td_index *next;
42 };
43
44 struct dso {
45         unsigned char *base;
46         char *name;
47         size_t *dynv;
48         struct dso *next, *prev;
49
50         Phdr *phdr;
51         int phnum;
52         size_t phentsize;
53         int refcnt;
54         Sym *syms;
55         uint32_t *hashtab;
56         uint32_t *ghashtab;
57         int16_t *versym;
58         char *strings;
59         unsigned char *map;
60         size_t map_len;
61         dev_t dev;
62         ino_t ino;
63         signed char global;
64         char relocated;
65         char constructed;
66         char kernel_mapped;
67         struct dso **deps, *needed_by;
68         char *rpath_orig, *rpath;
69         void *tls_image;
70         size_t tls_len, tls_size, tls_align, tls_id, tls_offset;
71         size_t relro_start, relro_end;
72         void **new_dtv;
73         unsigned char *new_tls;
74         volatile int new_dtv_idx, new_tls_idx;
75         struct td_index *td_index;
76         struct dso *fini_next;
77         char *shortname;
78         char buf[];
79 };
80
81 struct symdef {
82         Sym *sym;
83         struct dso *dso;
84 };
85
86 int __init_tp(void *);
87 void __init_libc(char **, char *);
88
89 const char *__libc_get_version(void);
90
91 static struct builtin_tls {
92         char c;
93         struct pthread pt;
94         void *space[16];
95 } builtin_tls[1];
96 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
97
98 #define ADDEND_LIMIT 4096
99 static size_t *saved_addends, *apply_addends_to;
100
101 static struct dso ldso;
102 static struct dso *head, *tail, *fini_head;
103 static char *env_path, *sys_path;
104 static unsigned long long gencnt;
105 static int runtime;
106 static int ldd_mode;
107 static int ldso_fail;
108 static int noload;
109 static jmp_buf *rtld_fail;
110 static pthread_rwlock_t lock;
111 static struct debug debug;
112 static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN;
113 static size_t static_tls_cnt;
114 static pthread_mutex_t init_fini_lock = { ._m_type = PTHREAD_MUTEX_RECURSIVE };
115
116 struct debug *_dl_debug_addr = &debug;
117
118 static int dl_strcmp(const char *l, const char *r)
119 {
120         for (; *l==*r && *l; l++, r++);
121         return *(unsigned char *)l - *(unsigned char *)r;
122 }
123 #define strcmp(l,r) dl_strcmp(l,r)
124
125 /* Compute load address for a virtual address in a given dso. */
126 #define laddr(p, v) (void *)((p)->base + (v))
127
128 static void decode_vec(size_t *v, size_t *a, size_t cnt)
129 {
130         size_t i;
131         for (i=0; i<cnt; i++) a[i] = 0;
132         for (; v[0]; v+=2) if (v[0]-1<cnt-1) {
133                 a[0] |= 1UL<<v[0];
134                 a[v[0]] = v[1];
135         }
136 }
137
138 static int search_vec(size_t *v, size_t *r, size_t key)
139 {
140         for (; v[0]!=key; v+=2)
141                 if (!v[0]) return 0;
142         *r = v[1];
143         return 1;
144 }
145
146 static uint32_t sysv_hash(const char *s0)
147 {
148         const unsigned char *s = (void *)s0;
149         uint_fast32_t h = 0;
150         while (*s) {
151                 h = 16*h + *s++;
152                 h ^= h>>24 & 0xf0;
153         }
154         return h & 0xfffffff;
155 }
156
157 static uint32_t gnu_hash(const char *s0)
158 {
159         const unsigned char *s = (void *)s0;
160         uint_fast32_t h = 5381;
161         for (; *s; s++)
162                 h += h*32 + *s;
163         return h;
164 }
165
166 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
167 {
168         size_t i;
169         Sym *syms = dso->syms;
170         uint32_t *hashtab = dso->hashtab;
171         char *strings = dso->strings;
172         for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
173                 if ((!dso->versym || dso->versym[i] >= 0)
174                     && (!strcmp(s, strings+syms[i].st_name)))
175                         return syms+i;
176         }
177         return 0;
178 }
179
180 static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s)
181 {
182         uint32_t nbuckets = hashtab[0];
183         uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
184         uint32_t i = buckets[h1 % nbuckets];
185
186         if (!i) return 0;
187
188         uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
189
190         for (h1 |= 1; ; i++) {
191                 uint32_t h2 = *hashval++;
192                 if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0)
193                     && !strcmp(s, dso->strings + dso->syms[i].st_name))
194                         return dso->syms+i;
195                 if (h2 & 1) break;
196         }
197
198         return 0;
199 }
200
201 static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask)
202 {
203         const size_t *bloomwords = (const void *)(hashtab+4);
204         size_t f = bloomwords[fofs & (hashtab[2]-1)];
205         if (!(f & fmask)) return 0;
206
207         f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
208         if (!(f & 1)) return 0;
209
210         return gnu_lookup(h1, hashtab, dso, s);
211 }
212
213 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
214 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
215
216 #ifndef ARCH_SYM_REJECT_UND
217 #define ARCH_SYM_REJECT_UND(s) 0
218 #endif
219
220 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
221 {
222         uint32_t h = 0, gh, gho, *ght;
223         size_t ghm = 0;
224         struct symdef def = {0};
225         for (; dso; dso=dso->next) {
226                 Sym *sym;
227                 if (!dso->global) continue;
228                 if ((ght = dso->ghashtab)) {
229                         if (!ghm) {
230                                 gh = gnu_hash(s);
231                                 int maskbits = 8 * sizeof ghm;
232                                 gho = gh / maskbits;
233                                 ghm = 1ul << gh % maskbits;
234                         }
235                         sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
236                 } else {
237                         if (!h) h = sysv_hash(s);
238                         sym = sysv_lookup(s, h, dso);
239                 }
240                 if (!sym) continue;
241                 if (!sym->st_shndx)
242                         if (need_def || (sym->st_info&0xf) == STT_TLS
243                             || ARCH_SYM_REJECT_UND(sym))
244                                 continue;
245                 if (!sym->st_value)
246                         if ((sym->st_info&0xf) != STT_TLS)
247                                 continue;
248                 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
249                 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
250
251                 if (def.sym && sym->st_info>>4 == STB_WEAK) continue;
252                 def.sym = sym;
253                 def.dso = dso;
254                 if (sym->st_info>>4 == STB_GLOBAL) break;
255         }
256         return def;
257 }
258
259 __attribute__((__visibility__("hidden")))
260 ptrdiff_t __tlsdesc_static(), __tlsdesc_dynamic();
261
262 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
263 {
264         unsigned char *base = dso->base;
265         Sym *syms = dso->syms;
266         char *strings = dso->strings;
267         Sym *sym;
268         const char *name;
269         void *ctx;
270         int type;
271         int sym_index;
272         struct symdef def;
273         size_t *reloc_addr;
274         size_t sym_val;
275         size_t tls_val;
276         size_t addend;
277         int skip_relative = 0, reuse_addends = 0, save_slot = 0;
278
279         if (dso == &ldso) {
280                 /* Only ldso's REL table needs addend saving/reuse. */
281                 if (rel == apply_addends_to)
282                         reuse_addends = 1;
283                 skip_relative = 1;
284         }
285
286         for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
287                 if (skip_relative && IS_RELATIVE(rel[1])) continue;
288                 type = R_TYPE(rel[1]);
289                 if (type == REL_NONE) continue;
290                 sym_index = R_SYM(rel[1]);
291                 reloc_addr = laddr(dso, rel[0]);
292                 if (sym_index) {
293                         sym = syms + sym_index;
294                         name = strings + sym->st_name;
295                         ctx = type==REL_COPY ? head->next : head;
296                         def = find_sym(ctx, name, type==REL_PLT);
297                         if (!def.sym && (sym->st_shndx != SHN_UNDEF
298                             || sym->st_info>>4 != STB_WEAK)) {
299                                 error("Error relocating %s: %s: symbol not found",
300                                         dso->name, name);
301                                 if (runtime) longjmp(*rtld_fail, 1);
302                                 continue;
303                         }
304                 } else {
305                         sym = 0;
306                         def.sym = 0;
307                         def.dso = dso;
308                 }
309
310                 if (stride > 2) {
311                         addend = rel[2];
312                 } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) {
313                         addend = 0;
314                 } else if (reuse_addends) {
315                         /* Save original addend in stage 2 where the dso
316                          * chain consists of just ldso; otherwise read back
317                          * saved addend since the inline one was clobbered. */
318                         if (head==&ldso)
319                                 saved_addends[save_slot] = *reloc_addr;
320                         addend = saved_addends[save_slot++];
321                 } else {
322                         addend = *reloc_addr;
323                 }
324
325                 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0;
326                 tls_val = def.sym ? def.sym->st_value : 0;
327
328                 switch(type) {
329                 case REL_NONE:
330                         break;
331                 case REL_OFFSET:
332                         addend -= (size_t)reloc_addr;
333                 case REL_SYMBOLIC:
334                 case REL_GOT:
335                 case REL_PLT:
336                         *reloc_addr = sym_val + addend;
337                         break;
338                 case REL_RELATIVE:
339                         *reloc_addr = (size_t)base + addend;
340                         break;
341                 case REL_SYM_OR_REL:
342                         if (sym) *reloc_addr = sym_val + addend;
343                         else *reloc_addr = (size_t)base + addend;
344                         break;
345                 case REL_COPY:
346                         memcpy(reloc_addr, (void *)sym_val, sym->st_size);
347                         break;
348                 case REL_OFFSET32:
349                         *(uint32_t *)reloc_addr = sym_val + addend
350                                 - (size_t)reloc_addr;
351                         break;
352                 case REL_DTPMOD:
353                         *reloc_addr = def.dso->tls_id;
354                         break;
355                 case REL_DTPOFF:
356                         *reloc_addr = tls_val + addend - DTP_OFFSET;
357                         break;
358 #ifdef TLS_ABOVE_TP
359                 case REL_TPOFF:
360                         *reloc_addr = tls_val + def.dso->tls_offset + TPOFF_K + addend;
361                         break;
362 #else
363                 case REL_TPOFF:
364                         *reloc_addr = tls_val - def.dso->tls_offset + addend;
365                         break;
366                 case REL_TPOFF_NEG:
367                         *reloc_addr = def.dso->tls_offset - tls_val + addend;
368                         break;
369 #endif
370                 case REL_TLSDESC:
371                         if (stride<3) addend = reloc_addr[1];
372                         if (runtime && def.dso->tls_id >= static_tls_cnt) {
373                                 struct td_index *new = malloc(sizeof *new);
374                                 if (!new) {
375                                         error(
376                                         "Error relocating %s: cannot allocate TLSDESC for %s",
377                                         dso->name, sym ? name : "(local)" );
378                                         longjmp(*rtld_fail, 1);
379                                 }
380                                 new->next = dso->td_index;
381                                 dso->td_index = new;
382                                 new->args[0] = def.dso->tls_id;
383                                 new->args[1] = tls_val + addend;
384                                 reloc_addr[0] = (size_t)__tlsdesc_dynamic;
385                                 reloc_addr[1] = (size_t)new;
386                         } else {
387                                 reloc_addr[0] = (size_t)__tlsdesc_static;
388 #ifdef TLS_ABOVE_TP
389                                 reloc_addr[1] = tls_val + def.dso->tls_offset
390                                         + TPOFF_K + addend;
391 #else
392                                 reloc_addr[1] = tls_val - def.dso->tls_offset
393                                         + addend;
394 #endif
395                         }
396                         break;
397                 default:
398                         error("Error relocating %s: unsupported relocation type %d",
399                                 dso->name, type);
400                         if (runtime) longjmp(*rtld_fail, 1);
401                         continue;
402                 }
403         }
404 }
405
406 /* A huge hack: to make up for the wastefulness of shared libraries
407  * needing at least a page of dirty memory even if they have no global
408  * data, we reclaim the gaps at the beginning and end of writable maps
409  * and "donate" them to the heap by setting up minimal malloc
410  * structures and then freeing them. */
411
412 static void reclaim(struct dso *dso, size_t start, size_t end)
413 {
414         size_t *a, *z;
415         if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end;
416         if (end   >= dso->relro_start && end   < dso->relro_end) end = dso->relro_start;
417         start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
418         end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
419         if (start>end || end-start < 4*sizeof(size_t)) return;
420         a = laddr(dso, start);
421         z = laddr(dso, end);
422         a[-2] = 1;
423         a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
424         z[1] = 1;
425         free(a);
426 }
427
428 static void reclaim_gaps(struct dso *dso)
429 {
430         Phdr *ph = dso->phdr;
431         size_t phcnt = dso->phnum;
432
433         for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) {
434                 if (ph->p_type!=PT_LOAD) continue;
435                 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
436                 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
437                 reclaim(dso, ph->p_vaddr+ph->p_memsz,
438                         ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
439         }
440 }
441
442 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
443 {
444         char *q = mmap(p, n, prot, flags, fd, off);
445         if (q != MAP_FAILED || errno != EINVAL) return q;
446         /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
447         if (flags & MAP_ANONYMOUS) {
448                 memset(p, 0, n);
449                 return p;
450         }
451         ssize_t r;
452         if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
453         for (q=p; n; q+=r, off+=r, n-=r) {
454                 r = read(fd, q, n);
455                 if (r < 0 && errno != EINTR) return MAP_FAILED;
456                 if (!r) {
457                         memset(q, 0, n);
458                         break;
459                 }
460         }
461         return p;
462 }
463
464 static void *map_library(int fd, struct dso *dso)
465 {
466         Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
467         void *allocated_buf=0;
468         size_t phsize;
469         size_t addr_min=SIZE_MAX, addr_max=0, map_len;
470         size_t this_min, this_max;
471         off_t off_start;
472         Ehdr *eh;
473         Phdr *ph, *ph0;
474         unsigned prot;
475         unsigned char *map=MAP_FAILED, *base;
476         size_t dyn=0;
477         size_t tls_image=0;
478         size_t i;
479
480         ssize_t l = read(fd, buf, sizeof buf);
481         eh = buf;
482         if (l<0) return 0;
483         if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC))
484                 goto noexec;
485         phsize = eh->e_phentsize * eh->e_phnum;
486         if (phsize > sizeof buf - sizeof *eh) {
487                 allocated_buf = malloc(phsize);
488                 if (!allocated_buf) return 0;
489                 l = pread(fd, allocated_buf, phsize, eh->e_phoff);
490                 if (l < 0) goto error;
491                 if (l != phsize) goto noexec;
492                 ph = ph0 = allocated_buf;
493         } else if (eh->e_phoff + phsize > l) {
494                 l = pread(fd, buf+1, phsize, eh->e_phoff);
495                 if (l < 0) goto error;
496                 if (l != phsize) goto noexec;
497                 ph = ph0 = (void *)(buf + 1);
498         } else {
499                 ph = ph0 = (void *)((char *)buf + eh->e_phoff);
500         }
501         for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
502                 if (ph->p_type == PT_DYNAMIC) {
503                         dyn = ph->p_vaddr;
504                 } else if (ph->p_type == PT_TLS) {
505                         tls_image = ph->p_vaddr;
506                         dso->tls_align = ph->p_align;
507                         dso->tls_len = ph->p_filesz;
508                         dso->tls_size = ph->p_memsz;
509                 } else if (ph->p_type == PT_GNU_RELRO) {
510                         dso->relro_start = ph->p_vaddr & -PAGE_SIZE;
511                         dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
512                 }
513                 if (ph->p_type != PT_LOAD) continue;
514                 if (ph->p_vaddr < addr_min) {
515                         addr_min = ph->p_vaddr;
516                         off_start = ph->p_offset;
517                         prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
518                                 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
519                                 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
520                 }
521                 if (ph->p_vaddr+ph->p_memsz > addr_max) {
522                         addr_max = ph->p_vaddr+ph->p_memsz;
523                 }
524         }
525         if (!dyn) goto noexec;
526         addr_max += PAGE_SIZE-1;
527         addr_max &= -PAGE_SIZE;
528         addr_min &= -PAGE_SIZE;
529         off_start &= -PAGE_SIZE;
530         map_len = addr_max - addr_min + off_start;
531         /* The first time, we map too much, possibly even more than
532          * the length of the file. This is okay because we will not
533          * use the invalid part; we just need to reserve the right
534          * amount of virtual address space to map over later. */
535         map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
536         if (map==MAP_FAILED) goto error;
537         /* If the loaded file is not relocatable and the requested address is
538          * not available, then the load operation must fail. */
539         if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) {
540                 errno = EBUSY;
541                 goto error;
542         }
543         base = map - addr_min;
544         dso->phdr = 0;
545         dso->phnum = 0;
546         for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
547                 if (ph->p_type != PT_LOAD) continue;
548                 /* Check if the programs headers are in this load segment, and
549                  * if so, record the address for use by dl_iterate_phdr. */
550                 if (!dso->phdr && eh->e_phoff >= ph->p_offset
551                     && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) {
552                         dso->phdr = (void *)(base + ph->p_vaddr
553                                 + (eh->e_phoff-ph->p_offset));
554                         dso->phnum = eh->e_phnum;
555                         dso->phentsize = eh->e_phentsize;
556                 }
557                 /* Reuse the existing mapping for the lowest-address LOAD */
558                 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
559                 this_min = ph->p_vaddr & -PAGE_SIZE;
560                 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
561                 off_start = ph->p_offset & -PAGE_SIZE;
562                 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
563                         ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
564                         ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
565                 if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
566                         goto error;
567                 if (ph->p_memsz > ph->p_filesz) {
568                         size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
569                         size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
570                         memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
571                         if (pgbrk-(size_t)base < this_max && mmap_fixed((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
572                                 goto error;
573                 }
574         }
575         for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
576                 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
577                         if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
578                             && errno != ENOSYS)
579                                 goto error;
580                         break;
581                 }
582         dso->map = map;
583         dso->map_len = map_len;
584         dso->base = base;
585         dso->dynv = (void *)(base+dyn);
586         if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
587         if (!runtime) reclaim_gaps(dso);
588         free(allocated_buf);
589         return map;
590 noexec:
591         errno = ENOEXEC;
592 error:
593         if (map!=MAP_FAILED) munmap(map, map_len);
594         free(allocated_buf);
595         return 0;
596 }
597
598 static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
599 {
600         size_t l;
601         int fd;
602         for (;;) {
603                 s += strspn(s, ":\n");
604                 l = strcspn(s, ":\n");
605                 if (l-1 >= INT_MAX) return -1;
606                 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) {
607                         if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
608                         switch (errno) {
609                         case ENOENT:
610                         case ENOTDIR:
611                         case EACCES:
612                         case ENAMETOOLONG:
613                                 break;
614                         default:
615                                 /* Any negative value but -1 will inhibit
616                                  * futher path search. */
617                                 return -2;
618                         }
619                 }
620                 s += l;
621         }
622 }
623
624 static int fixup_rpath(struct dso *p, char *buf, size_t buf_size)
625 {
626         size_t n, l;
627         const char *s, *t, *origin;
628         char *d;
629         if (p->rpath || !p->rpath_orig) return 0;
630         if (!strchr(p->rpath_orig, '$')) {
631                 p->rpath = p->rpath_orig;
632                 return 0;
633         }
634         n = 0;
635         s = p->rpath_orig;
636         while ((t=strchr(s, '$'))) {
637                 if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9))
638                         return 0;
639                 s = t+1;
640                 n++;
641         }
642         if (n > SSIZE_MAX/PATH_MAX) return 0;
643
644         if (p->kernel_mapped) {
645                 /* $ORIGIN searches cannot be performed for the main program
646                  * when it is suid/sgid/AT_SECURE. This is because the
647                  * pathname is under the control of the caller of execve.
648                  * For libraries, however, $ORIGIN can be processed safely
649                  * since the library's pathname came from a trusted source
650                  * (either system paths or a call to dlopen). */
651                 if (libc.secure)
652                         return 0;
653                 l = readlink("/proc/self/exe", buf, buf_size);
654                 if (l == -1) switch (errno) {
655                 case ENOENT:
656                 case ENOTDIR:
657                 case EACCES:
658                         break;
659                 default:
660                         return -1;
661                 }
662                 if (l >= buf_size)
663                         return 0;
664                 buf[l] = 0;
665                 origin = buf;
666         } else {
667                 origin = p->name;
668         }
669         t = strrchr(origin, '/');
670         l = t ? t-origin : 0;
671         p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1);
672         if (!p->rpath) return -1;
673
674         d = p->rpath;
675         s = p->rpath_orig;
676         while ((t=strchr(s, '$'))) {
677                 memcpy(d, s, t-s);
678                 d += t-s;
679                 memcpy(d, origin, l);
680                 d += l;
681                 /* It was determined previously that the '$' is followed
682                  * either by "ORIGIN" or "{ORIGIN}". */
683                 s = t + 7 + 2*(t[1]=='{');
684         }
685         strcpy(d, s);
686         return 0;
687 }
688
689 static void decode_dyn(struct dso *p)
690 {
691         size_t dyn[DYN_CNT];
692         decode_vec(p->dynv, dyn, DYN_CNT);
693         p->syms = laddr(p, dyn[DT_SYMTAB]);
694         p->strings = laddr(p, dyn[DT_STRTAB]);
695         if (dyn[0]&(1<<DT_HASH))
696                 p->hashtab = laddr(p, dyn[DT_HASH]);
697         if (dyn[0]&(1<<DT_RPATH))
698                 p->rpath_orig = p->strings + dyn[DT_RPATH];
699         if (dyn[0]&(1<<DT_RUNPATH))
700                 p->rpath_orig = p->strings + dyn[DT_RUNPATH];
701         if (search_vec(p->dynv, dyn, DT_GNU_HASH))
702                 p->ghashtab = laddr(p, *dyn);
703         if (search_vec(p->dynv, dyn, DT_VERSYM))
704                 p->versym = laddr(p, *dyn);
705 }
706
707 static struct dso *load_library(const char *name, struct dso *needed_by)
708 {
709         char buf[2*NAME_MAX+2];
710         const char *pathname;
711         unsigned char *map;
712         struct dso *p, temp_dso = {0};
713         int fd;
714         struct stat st;
715         size_t alloc_size;
716         int n_th = 0;
717         int is_self = 0;
718
719         if (!*name) {
720                 errno = EINVAL;
721                 return 0;
722         }
723
724         /* Catch and block attempts to reload the implementation itself */
725         if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
726                 static const char *rp, reserved[] =
727                         "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
728                 char *z = strchr(name, '.');
729                 if (z) {
730                         size_t l = z-name;
731                         for (rp=reserved; *rp && strncmp(name+3, rp, l-3); rp+=strlen(rp)+1);
732                         if (*rp) {
733                                 if (ldd_mode) {
734                                         /* Track which names have been resolved
735                                          * and only report each one once. */
736                                         static unsigned reported;
737                                         unsigned mask = 1U<<(rp-reserved);
738                                         if (!(reported & mask)) {
739                                                 reported |= mask;
740                                                 dprintf(1, "\t%s => %s (%p)\n",
741                                                         name, ldso.name,
742                                                         ldso.base);
743                                         }
744                                 }
745                                 is_self = 1;
746                         }
747                 }
748         }
749         if (!strcmp(name, ldso.name)) is_self = 1;
750         if (is_self) {
751                 if (!ldso.prev) {
752                         tail->next = &ldso;
753                         ldso.prev = tail;
754                         tail = ldso.next ? ldso.next : &ldso;
755                 }
756                 return &ldso;
757         }
758         if (strchr(name, '/')) {
759                 pathname = name;
760                 fd = open(name, O_RDONLY|O_CLOEXEC);
761         } else {
762                 /* Search for the name to see if it's already loaded */
763                 for (p=head->next; p; p=p->next) {
764                         if (p->shortname && !strcmp(p->shortname, name)) {
765                                 p->refcnt++;
766                                 return p;
767                         }
768                 }
769                 if (strlen(name) > NAME_MAX) return 0;
770                 fd = -1;
771                 if (env_path) fd = path_open(name, env_path, buf, sizeof buf);
772                 for (p=needed_by; fd == -1 && p; p=p->needed_by) {
773                         if (fixup_rpath(p, buf, sizeof buf) < 0)
774                                 fd = -2; /* Inhibit further search. */
775                         if (p->rpath)
776                                 fd = path_open(name, p->rpath, buf, sizeof buf);
777                 }
778                 if (fd == -1) {
779                         if (!sys_path) {
780                                 char *prefix = 0;
781                                 size_t prefix_len;
782                                 if (ldso.name[0]=='/') {
783                                         char *s, *t, *z;
784                                         for (s=t=z=ldso.name; *s; s++)
785                                                 if (*s=='/') z=t, t=s;
786                                         prefix_len = z-ldso.name;
787                                         if (prefix_len < PATH_MAX)
788                                                 prefix = ldso.name;
789                                 }
790                                 if (!prefix) {
791                                         prefix = "";
792                                         prefix_len = 0;
793                                 }
794                                 char etc_ldso_path[prefix_len + 1
795                                         + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
796                                 snprintf(etc_ldso_path, sizeof etc_ldso_path,
797                                         "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
798                                         (int)prefix_len, prefix);
799                                 FILE *f = fopen(etc_ldso_path, "rbe");
800                                 if (f) {
801                                         if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
802                                                 free(sys_path);
803                                                 sys_path = "";
804                                         }
805                                         fclose(f);
806                                 } else if (errno != ENOENT) {
807                                         sys_path = "";
808                                 }
809                         }
810                         if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
811                         fd = path_open(name, sys_path, buf, sizeof buf);
812                 }
813                 pathname = buf;
814         }
815         if (fd < 0) return 0;
816         if (fstat(fd, &st) < 0) {
817                 close(fd);
818                 return 0;
819         }
820         for (p=head->next; p; p=p->next) {
821                 if (p->dev == st.st_dev && p->ino == st.st_ino) {
822                         /* If this library was previously loaded with a
823                          * pathname but a search found the same inode,
824                          * setup its shortname so it can be found by name. */
825                         if (!p->shortname && pathname != name)
826                                 p->shortname = strrchr(p->name, '/')+1;
827                         close(fd);
828                         p->refcnt++;
829                         return p;
830                 }
831         }
832         map = noload ? 0 : map_library(fd, &temp_dso);
833         close(fd);
834         if (!map) return 0;
835
836         /* Allocate storage for the new DSO. When there is TLS, this
837          * storage must include a reservation for all pre-existing
838          * threads to obtain copies of both the new TLS, and an
839          * extended DTV capable of storing an additional slot for
840          * the newly-loaded DSO. */
841         alloc_size = sizeof *p + strlen(pathname) + 1;
842         if (runtime && temp_dso.tls_image) {
843                 size_t per_th = temp_dso.tls_size + temp_dso.tls_align
844                         + sizeof(void *) * (tls_cnt+3);
845                 n_th = libc.threads_minus_1 + 1;
846                 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
847                 else alloc_size += n_th * per_th;
848         }
849         p = calloc(1, alloc_size);
850         if (!p) {
851                 munmap(map, temp_dso.map_len);
852                 return 0;
853         }
854         memcpy(p, &temp_dso, sizeof temp_dso);
855         decode_dyn(p);
856         p->dev = st.st_dev;
857         p->ino = st.st_ino;
858         p->refcnt = 1;
859         p->needed_by = needed_by;
860         p->name = p->buf;
861         strcpy(p->name, pathname);
862         /* Add a shortname only if name arg was not an explicit pathname. */
863         if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
864         if (p->tls_image) {
865                 p->tls_id = ++tls_cnt;
866                 tls_align = MAXP2(tls_align, p->tls_align);
867 #ifdef TLS_ABOVE_TP
868                 p->tls_offset = tls_offset + ( (tls_align-1) &
869                         -(tls_offset + (uintptr_t)p->tls_image) );
870                 tls_offset += p->tls_size;
871 #else
872                 tls_offset += p->tls_size + p->tls_align - 1;
873                 tls_offset -= (tls_offset + (uintptr_t)p->tls_image)
874                         & (p->tls_align-1);
875                 p->tls_offset = tls_offset;
876 #endif
877                 p->new_dtv = (void *)(-sizeof(size_t) &
878                         (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
879                 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
880         }
881
882         tail->next = p;
883         p->prev = tail;
884         tail = p;
885
886         if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
887
888         return p;
889 }
890
891 static void load_deps(struct dso *p)
892 {
893         size_t i, ndeps=0;
894         struct dso ***deps = &p->deps, **tmp, *dep;
895         for (; p; p=p->next) {
896                 for (i=0; p->dynv[i]; i+=2) {
897                         if (p->dynv[i] != DT_NEEDED) continue;
898                         dep = load_library(p->strings + p->dynv[i+1], p);
899                         if (!dep) {
900                                 error("Error loading shared library %s: %m (needed by %s)",
901                                         p->strings + p->dynv[i+1], p->name);
902                                 if (runtime) longjmp(*rtld_fail, 1);
903                                 continue;
904                         }
905                         if (runtime) {
906                                 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
907                                 if (!tmp) longjmp(*rtld_fail, 1);
908                                 tmp[ndeps++] = dep;
909                                 tmp[ndeps] = 0;
910                                 *deps = tmp;
911                         }
912                 }
913         }
914 }
915
916 static void load_preload(char *s)
917 {
918         int tmp;
919         char *z;
920         for (z=s; *z; s=z) {
921                 for (   ; *s && (isspace(*s) || *s==':'); s++);
922                 for (z=s; *z && !isspace(*z) && *z!=':'; z++);
923                 tmp = *z;
924                 *z = 0;
925                 load_library(s, 0);
926                 *z = tmp;
927         }
928 }
929
930 static void make_global(struct dso *p)
931 {
932         for (; p; p=p->next) p->global = 1;
933 }
934
935 static void do_mips_relocs(struct dso *p, size_t *got)
936 {
937         size_t i, j, rel[2];
938         unsigned char *base = p->base;
939         i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO);
940         if (p==&ldso) {
941                 got += i;
942         } else {
943                 while (i--) *got++ += (size_t)base;
944         }
945         j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
946         i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
947         Sym *sym = p->syms + j;
948         rel[0] = (unsigned char *)got - base;
949         for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) {
950                 rel[1] = sym-p->syms << 8 | R_MIPS_JUMP_SLOT;
951                 do_relocs(p, rel, sizeof rel, 2);
952         }
953 }
954
955 static void reloc_all(struct dso *p)
956 {
957         size_t dyn[DYN_CNT];
958         for (; p; p=p->next) {
959                 if (p->relocated) continue;
960                 decode_vec(p->dynv, dyn, DYN_CNT);
961                 if (NEED_MIPS_GOT_RELOCS)
962                         do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT]));
963                 do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
964                         2+(dyn[DT_PLTREL]==DT_RELA));
965                 do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
966                 do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
967
968                 if (head != &ldso && p->relro_start != p->relro_end &&
969                     mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
970                     && errno != ENOSYS) {
971                         error("Error relocating %s: RELRO protection failed: %m",
972                                 p->name);
973                         if (runtime) longjmp(*rtld_fail, 1);
974                 }
975
976                 p->relocated = 1;
977         }
978 }
979
980 static void kernel_mapped_dso(struct dso *p)
981 {
982         size_t min_addr = -1, max_addr = 0, cnt;
983         Phdr *ph = p->phdr;
984         for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) {
985                 if (ph->p_type == PT_DYNAMIC) {
986                         p->dynv = laddr(p, ph->p_vaddr);
987                 } else if (ph->p_type == PT_GNU_RELRO) {
988                         p->relro_start = ph->p_vaddr & -PAGE_SIZE;
989                         p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
990                 }
991                 if (ph->p_type != PT_LOAD) continue;
992                 if (ph->p_vaddr < min_addr)
993                         min_addr = ph->p_vaddr;
994                 if (ph->p_vaddr+ph->p_memsz > max_addr)
995                         max_addr = ph->p_vaddr+ph->p_memsz;
996         }
997         min_addr &= -PAGE_SIZE;
998         max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
999         p->map = p->base + min_addr;
1000         p->map_len = max_addr - min_addr;
1001         p->kernel_mapped = 1;
1002 }
1003
1004 static void do_fini()
1005 {
1006         struct dso *p;
1007         size_t dyn[DYN_CNT];
1008         for (p=fini_head; p; p=p->fini_next) {
1009                 if (!p->constructed) continue;
1010                 decode_vec(p->dynv, dyn, DYN_CNT);
1011                 if (dyn[0] & (1<<DT_FINI_ARRAY)) {
1012                         size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
1013                         size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY])+n;
1014                         while (n--) ((void (*)(void))*--fn)();
1015                 }
1016 #ifndef NO_LEGACY_INITFINI
1017                 if ((dyn[0] & (1<<DT_FINI)) && dyn[DT_FINI])
1018                         ((void (*)(void))laddr(p, dyn[DT_FINI]))();
1019 #endif
1020         }
1021 }
1022
1023 static void do_init_fini(struct dso *p)
1024 {
1025         size_t dyn[DYN_CNT];
1026         int need_locking = libc.threads_minus_1;
1027         /* Allow recursive calls that arise when a library calls
1028          * dlopen from one of its constructors, but block any
1029          * other threads until all ctors have finished. */
1030         if (need_locking) pthread_mutex_lock(&init_fini_lock);
1031         for (; p; p=p->prev) {
1032                 if (p->constructed) continue;
1033                 p->constructed = 1;
1034                 decode_vec(p->dynv, dyn, DYN_CNT);
1035                 if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
1036                         p->fini_next = fini_head;
1037                         fini_head = p;
1038                 }
1039 #ifndef NO_LEGACY_INITFINI
1040                 if ((dyn[0] & (1<<DT_INIT)) && dyn[DT_INIT])
1041                         ((void (*)(void))laddr(p, dyn[DT_INIT]))();
1042 #endif
1043                 if (dyn[0] & (1<<DT_INIT_ARRAY)) {
1044                         size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
1045                         size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]);
1046                         while (n--) ((void (*)(void))*fn++)();
1047                 }
1048                 if (!need_locking && libc.threads_minus_1) {
1049                         need_locking = 1;
1050                         pthread_mutex_lock(&init_fini_lock);
1051                 }
1052         }
1053         if (need_locking) pthread_mutex_unlock(&init_fini_lock);
1054 }
1055
1056 static void dl_debug_state(void)
1057 {
1058 }
1059
1060 weak_alias(dl_debug_state, _dl_debug_state);
1061
1062 void __reset_tls()
1063 {
1064         pthread_t self = __pthread_self();
1065         struct dso *p;
1066         for (p=head; p; p=p->next) {
1067                 if (!p->tls_id || !self->dtv[p->tls_id]) continue;
1068                 memcpy(self->dtv[p->tls_id], p->tls_image, p->tls_len);
1069                 memset((char *)self->dtv[p->tls_id]+p->tls_len, 0,
1070                         p->tls_size - p->tls_len);
1071                 if (p->tls_id == (size_t)self->dtv[0]) break;
1072         }
1073 }
1074
1075 void *__copy_tls(unsigned char *mem)
1076 {
1077         pthread_t td;
1078         struct dso *p;
1079         void **dtv;
1080
1081 #ifdef TLS_ABOVE_TP
1082         dtv = (void **)(mem + libc.tls_size) - (tls_cnt + 1);
1083
1084         mem += -((uintptr_t)mem + sizeof(struct pthread)) & (tls_align-1);
1085         td = (pthread_t)mem;
1086         mem += sizeof(struct pthread);
1087
1088         for (p=head; p; p=p->next) {
1089                 if (!p->tls_id) continue;
1090                 dtv[p->tls_id] = mem + p->tls_offset;
1091                 memcpy(dtv[p->tls_id], p->tls_image, p->tls_len);
1092         }
1093 #else
1094         dtv = (void **)mem;
1095
1096         mem += libc.tls_size - sizeof(struct pthread);
1097         mem -= (uintptr_t)mem & (tls_align-1);
1098         td = (pthread_t)mem;
1099
1100         for (p=head; p; p=p->next) {
1101                 if (!p->tls_id) continue;
1102                 dtv[p->tls_id] = mem - p->tls_offset;
1103                 memcpy(dtv[p->tls_id], p->tls_image, p->tls_len);
1104         }
1105 #endif
1106         dtv[0] = (void *)tls_cnt;
1107         td->dtv = td->dtv_copy = dtv;
1108         return td;
1109 }
1110
1111 __attribute__((__visibility__("hidden")))
1112 void *__tls_get_new(size_t *v)
1113 {
1114         pthread_t self = __pthread_self();
1115
1116         /* Block signals to make accessing new TLS async-signal-safe */
1117         sigset_t set;
1118         __block_all_sigs(&set);
1119         if (v[0]<=(size_t)self->dtv[0]) {
1120                 __restore_sigs(&set);
1121                 return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
1122         }
1123
1124         /* This is safe without any locks held because, if the caller
1125          * is able to request the Nth entry of the DTV, the DSO list
1126          * must be valid at least that far out and it was synchronized
1127          * at program startup or by an already-completed call to dlopen. */
1128         struct dso *p;
1129         for (p=head; p->tls_id != v[0]; p=p->next);
1130
1131         /* Get new DTV space from new DSO if needed */
1132         if (v[0] > (size_t)self->dtv[0]) {
1133                 void **newdtv = p->new_dtv +
1134                         (v[0]+1)*sizeof(void *)*a_fetch_add(&p->new_dtv_idx,1);
1135                 memcpy(newdtv, self->dtv,
1136                         ((size_t)self->dtv[0]+1) * sizeof(void *));
1137                 newdtv[0] = (void *)v[0];
1138                 self->dtv = self->dtv_copy = newdtv;
1139         }
1140
1141         /* Get new TLS memory from all new DSOs up to the requested one */
1142         unsigned char *mem;
1143         for (p=head; ; p=p->next) {
1144                 if (!p->tls_id || self->dtv[p->tls_id]) continue;
1145                 mem = p->new_tls + (p->tls_size + p->tls_align)
1146                         * a_fetch_add(&p->new_tls_idx,1);
1147                 mem += ((uintptr_t)p->tls_image - (uintptr_t)mem)
1148                         & (p->tls_align-1);
1149                 self->dtv[p->tls_id] = mem;
1150                 memcpy(mem, p->tls_image, p->tls_len);
1151                 if (p->tls_id == v[0]) break;
1152         }
1153         __restore_sigs(&set);
1154         return mem + v[1] + DTP_OFFSET;
1155 }
1156
1157 static void update_tls_size()
1158 {
1159         libc.tls_size = ALIGN(
1160                 (1+tls_cnt) * sizeof(void *) +
1161                 tls_offset +
1162                 sizeof(struct pthread) +
1163                 tls_align * 2,
1164         tls_align);
1165 }
1166
1167 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1168  * following stage 2 and stage 3 functions via primitive symbolic lookup
1169  * since it does not have access to their addresses to begin with. */
1170
1171 /* Stage 2 of the dynamic linker is called after relative relocations 
1172  * have been processed. It can make function calls to static functions
1173  * and access string literals and static data, but cannot use extern
1174  * symbols. Its job is to perform symbolic relocations on the dynamic
1175  * linker itself, but some of the relocations performed may need to be
1176  * replaced later due to copy relocations in the main program. */
1177
1178 void __dls2(unsigned char *base, size_t *sp)
1179 {
1180         Ehdr *ehdr = (void *)base;
1181         ldso.base = base;
1182         ldso.name = ldso.shortname = "libc.so";
1183         ldso.global = 1;
1184         ldso.phnum = ehdr->e_phnum;
1185         ldso.phdr = (void *)(base + ehdr->e_phoff);
1186         ldso.phentsize = ehdr->e_phentsize;
1187         kernel_mapped_dso(&ldso);
1188         decode_dyn(&ldso);
1189
1190         /* Prepare storage for to save clobbered REL addends so they
1191          * can be reused in stage 3. There should be very few. If
1192          * something goes wrong and there are a huge number, abort
1193          * instead of risking stack overflow. */
1194         size_t dyn[DYN_CNT];
1195         decode_vec(ldso.dynv, dyn, DYN_CNT);
1196         size_t *rel = laddr(&ldso, dyn[DT_REL]);
1197         size_t rel_size = dyn[DT_RELSZ];
1198         size_t symbolic_rel_cnt = 0;
1199         apply_addends_to = rel;
1200         for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t))
1201                 if (!IS_RELATIVE(rel[1])) symbolic_rel_cnt++;
1202         if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash();
1203         size_t addends[symbolic_rel_cnt+1];
1204         saved_addends = addends;
1205
1206         head = &ldso;
1207         reloc_all(&ldso);
1208
1209         ldso.relocated = 0;
1210
1211         /* Call dynamic linker stage-3, __dls3, looking it up
1212          * symbolically as a barrier against moving the address
1213          * load across the above relocation processing. */
1214         struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
1215         ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp);
1216 }
1217
1218 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1219  * fully functional. Its job is to load (if not already loaded) and
1220  * process dependencies and relocations for the main application and
1221  * transfer control to its entry point. */
1222
1223 _Noreturn void __dls3(size_t *sp)
1224 {
1225         static struct dso app, vdso;
1226         size_t aux[AUX_CNT], *auxv;
1227         size_t i;
1228         char *env_preload=0;
1229         size_t vdso_base;
1230         int argc = *sp;
1231         char **argv = (void *)(sp+1);
1232         char **argv_orig = argv;
1233         char **envp = argv+argc+1;
1234
1235         /* Find aux vector just past environ[] and use it to initialize
1236          * global data that may be needed before we can make syscalls. */
1237         __environ = envp;
1238         for (i=argc+1; argv[i]; i++);
1239         libc.auxv = auxv = (void *)(argv+i+1);
1240         decode_vec(auxv, aux, AUX_CNT);
1241         __hwcap = aux[AT_HWCAP];
1242         libc.page_size = aux[AT_PAGESZ];
1243         libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1244                 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
1245
1246         /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1247          * use during dynamic linking. If possible it will also serve as the
1248          * thread pointer at runtime. */
1249         libc.tls_size = sizeof builtin_tls;
1250         if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) {
1251                 a_crash();
1252         }
1253
1254         /* Only trust user/env if kernel says we're not suid/sgid */
1255         if (!libc.secure) {
1256                 env_path = getenv("LD_LIBRARY_PATH");
1257                 env_preload = getenv("LD_PRELOAD");
1258         }
1259
1260         /* If the main program was already loaded by the kernel,
1261          * AT_PHDR will point to some location other than the dynamic
1262          * linker's program headers. */
1263         if (aux[AT_PHDR] != (size_t)ldso.phdr) {
1264                 size_t interp_off = 0;
1265                 size_t tls_image = 0;
1266                 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1267                 Phdr *phdr = app.phdr = (void *)aux[AT_PHDR];
1268                 app.phnum = aux[AT_PHNUM];
1269                 app.phentsize = aux[AT_PHENT];
1270                 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
1271                         if (phdr->p_type == PT_PHDR)
1272                                 app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
1273                         else if (phdr->p_type == PT_INTERP)
1274                                 interp_off = (size_t)phdr->p_vaddr;
1275                         else if (phdr->p_type == PT_TLS) {
1276                                 tls_image = phdr->p_vaddr;
1277                                 app.tls_len = phdr->p_filesz;
1278                                 app.tls_size = phdr->p_memsz;
1279                                 app.tls_align = phdr->p_align;
1280                         }
1281                 }
1282                 if (app.tls_size) app.tls_image = laddr(&app, tls_image);
1283                 if (interp_off) ldso.name = laddr(&app, interp_off);
1284                 if ((aux[0] & (1UL<<AT_EXECFN))
1285                     && strncmp((char *)aux[AT_EXECFN], "/proc/", 6))
1286                         app.name = (char *)aux[AT_EXECFN];
1287                 else
1288                         app.name = argv[0];
1289                 kernel_mapped_dso(&app);
1290         } else {
1291                 int fd;
1292                 char *ldname = argv[0];
1293                 size_t l = strlen(ldname);
1294                 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
1295                 argv++;
1296                 while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') {
1297                         char *opt = argv[0]+2;
1298                         *argv++ = (void *)-1;
1299                         if (!*opt) {
1300                                 break;
1301                         } else if (!memcmp(opt, "list", 5)) {
1302                                 ldd_mode = 1;
1303                         } else if (!memcmp(opt, "library-path", 12)) {
1304                                 if (opt[12]=='=') env_path = opt+13;
1305                                 else if (opt[12]) *argv = 0;
1306                                 else if (*argv) env_path = *argv++;
1307                         } else if (!memcmp(opt, "preload", 7)) {
1308                                 if (opt[7]=='=') env_preload = opt+8;
1309                                 else if (opt[7]) *argv = 0;
1310                                 else if (*argv) env_preload = *argv++;
1311                         } else {
1312                                 argv[0] = 0;
1313                         }
1314                 }
1315                 argv[-1] = (void *)(argc - (argv-argv_orig));
1316                 if (!argv[0]) {
1317                         dprintf(2, "musl libc\n"
1318                                 "Version %s\n"
1319                                 "Dynamic Program Loader\n"
1320                                 "Usage: %s [options] [--] pathname%s\n",
1321                                 __libc_get_version(), ldname,
1322                                 ldd_mode ? "" : " [args]");
1323                         _exit(1);
1324                 }
1325                 fd = open(argv[0], O_RDONLY);
1326                 if (fd < 0) {
1327                         dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
1328                         _exit(1);
1329                 }
1330                 runtime = 1;
1331                 Ehdr *ehdr = (void *)map_library(fd, &app);
1332                 if (!ehdr) {
1333                         dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
1334                         _exit(1);
1335                 }
1336                 runtime = 0;
1337                 close(fd);
1338                 ldso.name = ldname;
1339                 app.name = argv[0];
1340                 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry);
1341                 /* Find the name that would have been used for the dynamic
1342                  * linker had ldd not taken its place. */
1343                 if (ldd_mode) {
1344                         for (i=0; i<app.phnum; i++) {
1345                                 if (app.phdr[i].p_type == PT_INTERP)
1346                                         ldso.name = (void *)(app.base
1347                                                 + app.phdr[i].p_vaddr);
1348                         }
1349                         dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base);
1350                 }
1351         }
1352         if (app.tls_size) {
1353                 app.tls_id = tls_cnt = 1;
1354 #ifdef TLS_ABOVE_TP
1355                 app.tls_offset = 0;
1356                 tls_offset = app.tls_size
1357                         + ( -((uintptr_t)app.tls_image + app.tls_size)
1358                         & (app.tls_align-1) );
1359 #else
1360                 tls_offset = app.tls_offset = app.tls_size
1361                         + ( -((uintptr_t)app.tls_image + app.tls_size)
1362                         & (app.tls_align-1) );
1363 #endif
1364                 tls_align = MAXP2(tls_align, app.tls_align);
1365         }
1366         app.global = 1;
1367         decode_dyn(&app);
1368
1369         /* Attach to vdso, if provided by the kernel */
1370         if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
1371                 Ehdr *ehdr = (void *)vdso_base;
1372                 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff);
1373                 vdso.phnum = ehdr->e_phnum;
1374                 vdso.phentsize = ehdr->e_phentsize;
1375                 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
1376                         if (phdr->p_type == PT_DYNAMIC)
1377                                 vdso.dynv = (void *)(vdso_base + phdr->p_offset);
1378                         if (phdr->p_type == PT_LOAD)
1379                                 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
1380                 }
1381                 vdso.name = "";
1382                 vdso.shortname = "linux-gate.so.1";
1383                 vdso.global = 1;
1384                 vdso.relocated = 1;
1385                 decode_dyn(&vdso);
1386                 vdso.prev = &ldso;
1387                 ldso.next = &vdso;
1388         }
1389
1390         /* Initial dso chain consists only of the app. */
1391         head = tail = &app;
1392
1393         /* Donate unused parts of app and library mapping to malloc */
1394         reclaim_gaps(&app);
1395         reclaim_gaps(&ldso);
1396
1397         /* Load preload/needed libraries, add their symbols to the global
1398          * namespace, and perform all remaining relocations. */
1399         if (env_preload) load_preload(env_preload);
1400         load_deps(&app);
1401         make_global(&app);
1402
1403 #ifndef DYNAMIC_IS_RO
1404         for (i=0; app.dynv[i]; i+=2)
1405                 if (app.dynv[i]==DT_DEBUG)
1406                         app.dynv[i+1] = (size_t)&debug;
1407 #endif
1408
1409         /* The main program must be relocated LAST since it may contin
1410          * copy relocations which depend on libraries' relocations. */
1411         reloc_all(app.next);
1412         reloc_all(&app);
1413
1414         update_tls_size();
1415         if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) {
1416                 void *initial_tls = calloc(libc.tls_size, 1);
1417                 if (!initial_tls) {
1418                         dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
1419                                 argv[0], libc.tls_size);
1420                         _exit(127);
1421                 }
1422                 if (__init_tp(__copy_tls(initial_tls)) < 0) {
1423                         a_crash();
1424                 }
1425         } else {
1426                 size_t tmp_tls_size = libc.tls_size;
1427                 pthread_t self = __pthread_self();
1428                 /* Temporarily set the tls size to the full size of
1429                  * builtin_tls so that __copy_tls will use the same layout
1430                  * as it did for before. Then check, just to be safe. */
1431                 libc.tls_size = sizeof builtin_tls;
1432                 if (__copy_tls((void*)builtin_tls) != self) a_crash();
1433                 libc.tls_size = tmp_tls_size;
1434         }
1435         static_tls_cnt = tls_cnt;
1436
1437         if (ldso_fail) _exit(127);
1438         if (ldd_mode) _exit(0);
1439
1440         /* Switch to runtime mode: any further failures in the dynamic
1441          * linker are a reportable failure rather than a fatal startup
1442          * error. */
1443         runtime = 1;
1444
1445         debug.ver = 1;
1446         debug.bp = dl_debug_state;
1447         debug.head = head;
1448         debug.base = ldso.base;
1449         debug.state = 0;
1450         _dl_debug_state();
1451
1452         __init_libc(envp, argv[0]);
1453         atexit(do_fini);
1454         errno = 0;
1455         do_init_fini(tail);
1456
1457         CRTJMP((void *)aux[AT_ENTRY], argv-1);
1458         for(;;);
1459 }
1460
1461 void *dlopen(const char *file, int mode)
1462 {
1463         struct dso *volatile p, *orig_tail, *next;
1464         size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
1465         size_t i;
1466         int cs;
1467         jmp_buf jb;
1468
1469         if (!file) return head;
1470
1471         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
1472         pthread_rwlock_wrlock(&lock);
1473         __inhibit_ptc();
1474
1475         p = 0;
1476         orig_tls_cnt = tls_cnt;
1477         orig_tls_offset = tls_offset;
1478         orig_tls_align = tls_align;
1479         orig_tail = tail;
1480         noload = mode & RTLD_NOLOAD;
1481
1482         rtld_fail = &jb;
1483         if (setjmp(*rtld_fail)) {
1484                 /* Clean up anything new that was (partially) loaded */
1485                 if (p && p->deps) for (i=0; p->deps[i]; i++)
1486                         if (p->deps[i]->global < 0)
1487                                 p->deps[i]->global = 0;
1488                 for (p=orig_tail->next; p; p=next) {
1489                         next = p->next;
1490                         munmap(p->map, p->map_len);
1491                         while (p->td_index) {
1492                                 void *tmp = p->td_index->next;
1493                                 free(p->td_index);
1494                                 p->td_index = tmp;
1495                         }
1496                         if (p->rpath != p->rpath_orig)
1497                                 free(p->rpath);
1498                         free(p->deps);
1499                         free(p);
1500                 }
1501                 tls_cnt = orig_tls_cnt;
1502                 tls_offset = orig_tls_offset;
1503                 tls_align = orig_tls_align;
1504                 tail = orig_tail;
1505                 tail->next = 0;
1506                 p = 0;
1507                 goto end;
1508         } else p = load_library(file, head);
1509
1510         if (!p) {
1511                 error(noload ?
1512                         "Library %s is not already loaded" :
1513                         "Error loading shared library %s: %m",
1514                         file);
1515                 goto end;
1516         }
1517
1518         /* First load handling */
1519         if (!p->deps) {
1520                 load_deps(p);
1521                 if (p->deps) for (i=0; p->deps[i]; i++)
1522                         if (!p->deps[i]->global)
1523                                 p->deps[i]->global = -1;
1524                 if (!p->global) p->global = -1;
1525                 reloc_all(p);
1526                 if (p->deps) for (i=0; p->deps[i]; i++)
1527                         if (p->deps[i]->global < 0)
1528                                 p->deps[i]->global = 0;
1529                 if (p->global < 0) p->global = 0;
1530         }
1531
1532         if (mode & RTLD_GLOBAL) {
1533                 if (p->deps) for (i=0; p->deps[i]; i++)
1534                         p->deps[i]->global = 1;
1535                 p->global = 1;
1536         }
1537
1538         update_tls_size();
1539         _dl_debug_state();
1540         orig_tail = tail;
1541 end:
1542         __release_ptc();
1543         if (p) gencnt++;
1544         pthread_rwlock_unlock(&lock);
1545         if (p) do_init_fini(orig_tail);
1546         pthread_setcancelstate(cs, 0);
1547         return p;
1548 }
1549
1550 static int invalid_dso_handle(void *h)
1551 {
1552         struct dso *p;
1553         for (p=head; p; p=p->next) if (h==p) return 0;
1554         error("Invalid library handle %p", (void *)h);
1555         return 1;
1556 }
1557
1558 void *__tls_get_addr(size_t *);
1559
1560 static void *do_dlsym(struct dso *p, const char *s, void *ra)
1561 {
1562         size_t i;
1563         uint32_t h = 0, gh = 0, *ght;
1564         Sym *sym;
1565         if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
1566                 if (p == RTLD_DEFAULT) {
1567                         p = head;
1568                 } else if (p == RTLD_NEXT) {
1569                         for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
1570                         if (!p) p=head;
1571                         p = p->next;
1572                 }
1573                 struct symdef def = find_sym(p, s, 0);
1574                 if (!def.sym) goto failed;
1575                 if ((def.sym->st_info&0xf) == STT_TLS)
1576                         return __tls_get_addr((size_t []){def.dso->tls_id, def.sym->st_value});
1577                 return laddr(def.dso, def.sym->st_value);
1578         }
1579         if (invalid_dso_handle(p))
1580                 return 0;
1581         if ((ght = p->ghashtab)) {
1582                 gh = gnu_hash(s);
1583                 sym = gnu_lookup(gh, ght, p, s);
1584         } else {
1585                 h = sysv_hash(s);
1586                 sym = sysv_lookup(s, h, p);
1587         }
1588         if (sym && (sym->st_info&0xf) == STT_TLS)
1589                 return __tls_get_addr((size_t []){p->tls_id, sym->st_value});
1590         if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1591                 return laddr(p, sym->st_value);
1592         if (p->deps) for (i=0; p->deps[i]; i++) {
1593                 if ((ght = p->deps[i]->ghashtab)) {
1594                         if (!gh) gh = gnu_hash(s);
1595                         sym = gnu_lookup(gh, ght, p->deps[i], s);
1596                 } else {
1597                         if (!h) h = sysv_hash(s);
1598                         sym = sysv_lookup(s, h, p->deps[i]);
1599                 }
1600                 if (sym && (sym->st_info&0xf) == STT_TLS)
1601                         return __tls_get_addr((size_t []){p->deps[i]->tls_id, sym->st_value});
1602                 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1603                         return laddr(p->deps[i], sym->st_value);
1604         }
1605 failed:
1606         error("Symbol not found: %s", s);
1607         return 0;
1608 }
1609
1610 int __dladdr(const void *addr, Dl_info *info)
1611 {
1612         struct dso *p;
1613         Sym *sym;
1614         uint32_t nsym;
1615         char *strings;
1616         size_t i;
1617         void *best = 0;
1618         char *bestname;
1619
1620         pthread_rwlock_rdlock(&lock);
1621         for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
1622         pthread_rwlock_unlock(&lock);
1623
1624         if (!p) return 0;
1625
1626         sym = p->syms;
1627         strings = p->strings;
1628         if (p->hashtab) {
1629                 nsym = p->hashtab[1];
1630         } else {
1631                 uint32_t *buckets;
1632                 uint32_t *hashval;
1633                 buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
1634                 sym += p->ghashtab[1];
1635                 for (i = nsym = 0; i < p->ghashtab[0]; i++) {
1636                         if (buckets[i] > nsym)
1637                                 nsym = buckets[i];
1638                 }
1639                 if (nsym) {
1640                         nsym -= p->ghashtab[1];
1641                         hashval = buckets + p->ghashtab[0] + nsym;
1642                         do nsym++;
1643                         while (!(*hashval++ & 1));
1644                 }
1645         }
1646
1647         for (; nsym; nsym--, sym++) {
1648                 if (sym->st_value
1649                  && (1<<(sym->st_info&0xf) & OK_TYPES)
1650                  && (1<<(sym->st_info>>4) & OK_BINDS)) {
1651                         void *symaddr = laddr(p, sym->st_value);
1652                         if (symaddr > addr || symaddr < best)
1653                                 continue;
1654                         best = symaddr;
1655                         bestname = strings + sym->st_name;
1656                         if (addr == symaddr)
1657                                 break;
1658                 }
1659         }
1660
1661         if (!best) return 0;
1662
1663         info->dli_fname = p->name;
1664         info->dli_fbase = p->base;
1665         info->dli_sname = bestname;
1666         info->dli_saddr = best;
1667
1668         return 1;
1669 }
1670
1671 __attribute__((__visibility__("hidden")))
1672 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1673 {
1674         void *res;
1675         pthread_rwlock_rdlock(&lock);
1676         res = do_dlsym(p, s, ra);
1677         pthread_rwlock_unlock(&lock);
1678         return res;
1679 }
1680
1681 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
1682 {
1683         struct dso *current;
1684         struct dl_phdr_info info;
1685         int ret = 0;
1686         for(current = head; current;) {
1687                 info.dlpi_addr      = (uintptr_t)current->base;
1688                 info.dlpi_name      = current->name;
1689                 info.dlpi_phdr      = current->phdr;
1690                 info.dlpi_phnum     = current->phnum;
1691                 info.dlpi_adds      = gencnt;
1692                 info.dlpi_subs      = 0;
1693                 info.dlpi_tls_modid = current->tls_id;
1694                 info.dlpi_tls_data  = current->tls_image;
1695
1696                 ret = (callback)(&info, sizeof (info), data);
1697
1698                 if (ret != 0) break;
1699
1700                 pthread_rwlock_rdlock(&lock);
1701                 current = current->next;
1702                 pthread_rwlock_unlock(&lock);
1703         }
1704         return ret;
1705 }
1706 #else
1707 static int invalid_dso_handle(void *h)
1708 {
1709         error("Invalid library handle %p", (void *)h);
1710         return 1;
1711 }
1712 void *dlopen(const char *file, int mode)
1713 {
1714         error("Dynamic loading not supported");
1715         return 0;
1716 }
1717 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1718 {
1719         error("Symbol not found: %s", s);
1720         return 0;
1721 }
1722 int __dladdr (const void *addr, Dl_info *info)
1723 {
1724         return 0;
1725 }
1726 #endif
1727
1728 int __dlinfo(void *dso, int req, void *res)
1729 {
1730         if (invalid_dso_handle(dso)) return -1;
1731         if (req != RTLD_DI_LINKMAP) {
1732                 error("Unsupported request %d", req);
1733                 return -1;
1734         }
1735         *(struct link_map **)res = dso;
1736         return 0;
1737 }
1738
1739 char *dlerror()
1740 {
1741         pthread_t self = __pthread_self();
1742         if (!self->dlerror_flag) return 0;
1743         self->dlerror_flag = 0;
1744         char *s = self->dlerror_buf;
1745         if (s == (void *)-1)
1746                 return "Dynamic linker failed to allocate memory for error message";
1747         else
1748                 return s;
1749 }
1750
1751 int dlclose(void *p)
1752 {
1753         return invalid_dso_handle(p);
1754 }
1755
1756 void __dl_thread_cleanup(void)
1757 {
1758         pthread_t self = __pthread_self();
1759         if (self->dlerror_buf != (void *)-1)
1760                 free(self->dlerror_buf);
1761 }
1762
1763 static void error(const char *fmt, ...)
1764 {
1765         va_list ap;
1766         va_start(ap, fmt);
1767 #ifdef SHARED
1768         if (!runtime) {
1769                 vdprintf(2, fmt, ap);
1770                 dprintf(2, "\n");
1771                 ldso_fail = 1;
1772                 va_end(ap);
1773                 return;
1774         }
1775 #endif
1776         pthread_t self = __pthread_self();
1777         if (self->dlerror_buf != (void *)-1)
1778                 free(self->dlerror_buf);
1779         size_t len = vsnprintf(0, 0, fmt, ap);
1780         va_end(ap);
1781         char *buf = malloc(len+1);
1782         if (buf) {
1783                 va_start(ap, fmt);
1784                 vsnprintf(buf, len+1, fmt, ap);
1785                 va_end(ap);
1786         } else {
1787                 buf = (void *)-1;       
1788         }
1789         self->dlerror_buf = buf;
1790         self->dlerror_flag = 1;
1791 }