203282858c66f48361f52407556d344f51cd332d
[musl] / 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 <semaphore.h>
21 #include <sys/membarrier.h>
22 #include "pthread_impl.h"
23 #include "libc.h"
24 #include "dynlink.h"
25 #include "malloc_impl.h"
26
27 static void error(const char *, ...);
28
29 #define MAXP2(a,b) (-(-(a)&-(b)))
30 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
31
32 #define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
33
34 struct debug {
35         int ver;
36         void *head;
37         void (*bp)(void);
38         int state;
39         void *base;
40 };
41
42 struct td_index {
43         size_t args[2];
44         struct td_index *next;
45 };
46
47 struct dso {
48 #if DL_FDPIC
49         struct fdpic_loadmap *loadmap;
50 #else
51         unsigned char *base;
52 #endif
53         char *name;
54         size_t *dynv;
55         struct dso *next, *prev;
56
57         Phdr *phdr;
58         int phnum;
59         size_t phentsize;
60         Sym *syms;
61         Elf_Symndx *hashtab;
62         uint32_t *ghashtab;
63         int16_t *versym;
64         char *strings;
65         struct dso *syms_next, *lazy_next;
66         size_t *lazy, lazy_cnt;
67         unsigned char *map;
68         size_t map_len;
69         dev_t dev;
70         ino_t ino;
71         char relocated;
72         char constructed;
73         char kernel_mapped;
74         char mark;
75         char bfs_built;
76         char runtime_loaded;
77         struct dso **deps, *needed_by;
78         size_t ndeps_direct;
79         size_t next_dep;
80         int ctor_visitor;
81         char *rpath_orig, *rpath;
82         struct tls_module tls;
83         size_t tls_id;
84         size_t relro_start, relro_end;
85         uintptr_t *new_dtv;
86         unsigned char *new_tls;
87         volatile int new_dtv_idx, new_tls_idx;
88         struct td_index *td_index;
89         struct dso *fini_next;
90         char *shortname;
91 #if DL_FDPIC
92         unsigned char *base;
93 #else
94         struct fdpic_loadmap *loadmap;
95 #endif
96         struct funcdesc {
97                 void *addr;
98                 size_t *got;
99         } *funcdescs;
100         size_t *got;
101         char buf[];
102 };
103
104 struct symdef {
105         Sym *sym;
106         struct dso *dso;
107 };
108
109 static struct builtin_tls {
110         char c;
111         struct pthread pt;
112         void *space[16];
113 } builtin_tls[1];
114 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
115
116 #define ADDEND_LIMIT 4096
117 static size_t *saved_addends, *apply_addends_to;
118
119 static struct dso ldso;
120 static struct dso *head, *tail, *fini_head, *syms_tail, *lazy_head;
121 static char *env_path, *sys_path;
122 static unsigned long long gencnt;
123 static int runtime;
124 static int ldd_mode;
125 static int ldso_fail;
126 static int noload;
127 static int shutting_down;
128 static jmp_buf *rtld_fail;
129 static pthread_rwlock_t lock;
130 static struct debug debug;
131 static struct tls_module *tls_tail;
132 static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN;
133 static size_t static_tls_cnt;
134 static pthread_mutex_t init_fini_lock;
135 static pthread_cond_t ctor_cond;
136 static struct dso **main_ctor_queue;
137 static struct fdpic_loadmap *app_loadmap;
138 static struct fdpic_dummy_loadmap app_dummy_loadmap;
139
140 struct debug *_dl_debug_addr = &debug;
141
142 extern hidden int __malloc_replaced;
143
144 hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0;
145
146 extern hidden void (*const __init_array_end)(void), (*const __fini_array_end)(void);
147
148 weak_alias(__init_array_start, __init_array_end);
149 weak_alias(__fini_array_start, __fini_array_end);
150
151 static int dl_strcmp(const char *l, const char *r)
152 {
153         for (; *l==*r && *l; l++, r++);
154         return *(unsigned char *)l - *(unsigned char *)r;
155 }
156 #define strcmp(l,r) dl_strcmp(l,r)
157
158 /* Compute load address for a virtual address in a given dso. */
159 #if DL_FDPIC
160 static void *laddr(const struct dso *p, size_t v)
161 {
162         size_t j=0;
163         if (!p->loadmap) return p->base + v;
164         for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++);
165         return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
166 }
167 static void *laddr_pg(const struct dso *p, size_t v)
168 {
169         size_t j=0;
170         size_t pgsz = PAGE_SIZE;
171         if (!p->loadmap) return p->base + v;
172         for (j=0; ; j++) {
173                 size_t a = p->loadmap->segs[j].p_vaddr;
174                 size_t b = a + p->loadmap->segs[j].p_memsz;
175                 a &= -pgsz;
176                 b += pgsz-1;
177                 b &= -pgsz;
178                 if (v-a<b-a) break;
179         }
180         return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
181 }
182 #define fpaddr(p, v) ((void (*)())&(struct funcdesc){ \
183         laddr(p, v), (p)->got })
184 #else
185 #define laddr(p, v) (void *)((p)->base + (v))
186 #define laddr_pg(p, v) laddr(p, v)
187 #define fpaddr(p, v) ((void (*)())laddr(p, v))
188 #endif
189
190 static void decode_vec(size_t *v, size_t *a, size_t cnt)
191 {
192         size_t i;
193         for (i=0; i<cnt; i++) a[i] = 0;
194         for (; v[0]; v+=2) if (v[0]-1<cnt-1) {
195                 a[0] |= 1UL<<v[0];
196                 a[v[0]] = v[1];
197         }
198 }
199
200 static int search_vec(size_t *v, size_t *r, size_t key)
201 {
202         for (; v[0]!=key; v+=2)
203                 if (!v[0]) return 0;
204         *r = v[1];
205         return 1;
206 }
207
208 static uint32_t sysv_hash(const char *s0)
209 {
210         const unsigned char *s = (void *)s0;
211         uint_fast32_t h = 0;
212         while (*s) {
213                 h = 16*h + *s++;
214                 h ^= h>>24 & 0xf0;
215         }
216         return h & 0xfffffff;
217 }
218
219 static uint32_t gnu_hash(const char *s0)
220 {
221         const unsigned char *s = (void *)s0;
222         uint_fast32_t h = 5381;
223         for (; *s; s++)
224                 h += h*32 + *s;
225         return h;
226 }
227
228 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
229 {
230         size_t i;
231         Sym *syms = dso->syms;
232         Elf_Symndx *hashtab = dso->hashtab;
233         char *strings = dso->strings;
234         for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
235                 if ((!dso->versym || dso->versym[i] >= 0)
236                     && (!strcmp(s, strings+syms[i].st_name)))
237                         return syms+i;
238         }
239         return 0;
240 }
241
242 static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s)
243 {
244         uint32_t nbuckets = hashtab[0];
245         uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
246         uint32_t i = buckets[h1 % nbuckets];
247
248         if (!i) return 0;
249
250         uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
251
252         for (h1 |= 1; ; i++) {
253                 uint32_t h2 = *hashval++;
254                 if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0)
255                     && !strcmp(s, dso->strings + dso->syms[i].st_name))
256                         return dso->syms+i;
257                 if (h2 & 1) break;
258         }
259
260         return 0;
261 }
262
263 static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask)
264 {
265         const size_t *bloomwords = (const void *)(hashtab+4);
266         size_t f = bloomwords[fofs & (hashtab[2]-1)];
267         if (!(f & fmask)) return 0;
268
269         f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
270         if (!(f & 1)) return 0;
271
272         return gnu_lookup(h1, hashtab, dso, s);
273 }
274
275 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
276 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
277
278 #ifndef ARCH_SYM_REJECT_UND
279 #define ARCH_SYM_REJECT_UND(s) 0
280 #endif
281
282 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
283 {
284         uint32_t h = 0, gh = gnu_hash(s), gho = gh / (8*sizeof(size_t)), *ght;
285         size_t ghm = 1ul << gh % (8*sizeof(size_t));
286         struct symdef def = {0};
287         for (; dso; dso=dso->syms_next) {
288                 Sym *sym;
289                 if ((ght = dso->ghashtab)) {
290                         sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
291                 } else {
292                         if (!h) h = sysv_hash(s);
293                         sym = sysv_lookup(s, h, dso);
294                 }
295                 if (!sym) continue;
296                 if (!sym->st_shndx)
297                         if (need_def || (sym->st_info&0xf) == STT_TLS
298                             || ARCH_SYM_REJECT_UND(sym))
299                                 continue;
300                 if (!sym->st_value)
301                         if ((sym->st_info&0xf) != STT_TLS)
302                                 continue;
303                 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
304                 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
305                 def.sym = sym;
306                 def.dso = dso;
307                 break;
308         }
309         return def;
310 }
311
312 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
313 {
314         unsigned char *base = dso->base;
315         Sym *syms = dso->syms;
316         char *strings = dso->strings;
317         Sym *sym;
318         const char *name;
319         void *ctx;
320         int type;
321         int sym_index;
322         struct symdef def;
323         size_t *reloc_addr;
324         size_t sym_val;
325         size_t tls_val;
326         size_t addend;
327         int skip_relative = 0, reuse_addends = 0, save_slot = 0;
328
329         if (dso == &ldso) {
330                 /* Only ldso's REL table needs addend saving/reuse. */
331                 if (rel == apply_addends_to)
332                         reuse_addends = 1;
333                 skip_relative = 1;
334         }
335
336         for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
337                 if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue;
338                 type = R_TYPE(rel[1]);
339                 if (type == REL_NONE) continue;
340                 reloc_addr = laddr(dso, rel[0]);
341
342                 if (stride > 2) {
343                         addend = rel[2];
344                 } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) {
345                         addend = 0;
346                 } else if (reuse_addends) {
347                         /* Save original addend in stage 2 where the dso
348                          * chain consists of just ldso; otherwise read back
349                          * saved addend since the inline one was clobbered. */
350                         if (head==&ldso)
351                                 saved_addends[save_slot] = *reloc_addr;
352                         addend = saved_addends[save_slot++];
353                 } else {
354                         addend = *reloc_addr;
355                 }
356
357                 sym_index = R_SYM(rel[1]);
358                 if (sym_index) {
359                         sym = syms + sym_index;
360                         name = strings + sym->st_name;
361                         ctx = type==REL_COPY ? head->syms_next : head;
362                         def = (sym->st_info&0xf) == STT_SECTION
363                                 ? (struct symdef){ .dso = dso, .sym = sym }
364                                 : find_sym(ctx, name, type==REL_PLT);
365                         if (!def.sym && (sym->st_shndx != SHN_UNDEF
366                             || sym->st_info>>4 != STB_WEAK)) {
367                                 if (dso->lazy && (type==REL_PLT || type==REL_GOT)) {
368                                         dso->lazy[3*dso->lazy_cnt+0] = rel[0];
369                                         dso->lazy[3*dso->lazy_cnt+1] = rel[1];
370                                         dso->lazy[3*dso->lazy_cnt+2] = addend;
371                                         dso->lazy_cnt++;
372                                         continue;
373                                 }
374                                 error("Error relocating %s: %s: symbol not found",
375                                         dso->name, name);
376                                 if (runtime) longjmp(*rtld_fail, 1);
377                                 continue;
378                         }
379                 } else {
380                         sym = 0;
381                         def.sym = 0;
382                         def.dso = dso;
383                 }
384
385                 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0;
386                 tls_val = def.sym ? def.sym->st_value : 0;
387
388                 if ((type == REL_TPOFF || type == REL_TPOFF_NEG)
389                     && runtime && def.dso->tls_id > static_tls_cnt) {
390                         error("Error relocating %s: %s: initial-exec TLS "
391                                 "resolves to dynamic definition in %s",
392                                 dso->name, name, def.dso->name);
393                         longjmp(*rtld_fail, 1);
394                 }
395
396                 switch(type) {
397                 case REL_NONE:
398                         break;
399                 case REL_OFFSET:
400                         addend -= (size_t)reloc_addr;
401                 case REL_SYMBOLIC:
402                 case REL_GOT:
403                 case REL_PLT:
404                         *reloc_addr = sym_val + addend;
405                         break;
406                 case REL_RELATIVE:
407                         *reloc_addr = (size_t)base + addend;
408                         break;
409                 case REL_SYM_OR_REL:
410                         if (sym) *reloc_addr = sym_val + addend;
411                         else *reloc_addr = (size_t)base + addend;
412                         break;
413                 case REL_COPY:
414                         memcpy(reloc_addr, (void *)sym_val, sym->st_size);
415                         break;
416                 case REL_OFFSET32:
417                         *(uint32_t *)reloc_addr = sym_val + addend
418                                 - (size_t)reloc_addr;
419                         break;
420                 case REL_FUNCDESC:
421                         *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs
422                                 + (def.sym - def.dso->syms)) : 0;
423                         break;
424                 case REL_FUNCDESC_VAL:
425                         if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val;
426                         else *reloc_addr = sym_val;
427                         reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0;
428                         break;
429                 case REL_DTPMOD:
430                         *reloc_addr = def.dso->tls_id;
431                         break;
432                 case REL_DTPOFF:
433                         *reloc_addr = tls_val + addend - DTP_OFFSET;
434                         break;
435 #ifdef TLS_ABOVE_TP
436                 case REL_TPOFF:
437                         *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend;
438                         break;
439 #else
440                 case REL_TPOFF:
441                         *reloc_addr = tls_val - def.dso->tls.offset + addend;
442                         break;
443                 case REL_TPOFF_NEG:
444                         *reloc_addr = def.dso->tls.offset - tls_val + addend;
445                         break;
446 #endif
447                 case REL_TLSDESC:
448                         if (stride<3) addend = reloc_addr[1];
449                         if (runtime && def.dso->tls_id > static_tls_cnt) {
450                                 struct td_index *new = malloc(sizeof *new);
451                                 if (!new) {
452                                         error(
453                                         "Error relocating %s: cannot allocate TLSDESC for %s",
454                                         dso->name, sym ? name : "(local)" );
455                                         longjmp(*rtld_fail, 1);
456                                 }
457                                 new->next = dso->td_index;
458                                 dso->td_index = new;
459                                 new->args[0] = def.dso->tls_id;
460                                 new->args[1] = tls_val + addend - DTP_OFFSET;
461                                 reloc_addr[0] = (size_t)__tlsdesc_dynamic;
462                                 reloc_addr[1] = (size_t)new;
463                         } else {
464                                 reloc_addr[0] = (size_t)__tlsdesc_static;
465 #ifdef TLS_ABOVE_TP
466                                 reloc_addr[1] = tls_val + def.dso->tls.offset
467                                         + TPOFF_K + addend;
468 #else
469                                 reloc_addr[1] = tls_val - def.dso->tls.offset
470                                         + addend;
471 #endif
472                         }
473 #ifdef TLSDESC_BACKWARDS
474                         /* Some archs (32-bit ARM at least) invert the order of
475                          * the descriptor members. Fix them up here. */
476                         size_t tmp = reloc_addr[0];
477                         reloc_addr[0] = reloc_addr[1];
478                         reloc_addr[1] = tmp;
479 #endif
480                         break;
481                 default:
482                         error("Error relocating %s: unsupported relocation type %d",
483                                 dso->name, type);
484                         if (runtime) longjmp(*rtld_fail, 1);
485                         continue;
486                 }
487         }
488 }
489
490 static void redo_lazy_relocs()
491 {
492         struct dso *p = lazy_head, *next;
493         lazy_head = 0;
494         for (; p; p=next) {
495                 next = p->lazy_next;
496                 size_t size = p->lazy_cnt*3*sizeof(size_t);
497                 p->lazy_cnt = 0;
498                 do_relocs(p, p->lazy, size, 3);
499                 if (p->lazy_cnt) {
500                         p->lazy_next = lazy_head;
501                         lazy_head = p;
502                 } else {
503                         free(p->lazy);
504                         p->lazy = 0;
505                         p->lazy_next = 0;
506                 }
507         }
508 }
509
510 /* A huge hack: to make up for the wastefulness of shared libraries
511  * needing at least a page of dirty memory even if they have no global
512  * data, we reclaim the gaps at the beginning and end of writable maps
513  * and "donate" them to the heap. */
514
515 static void reclaim(struct dso *dso, size_t start, size_t end)
516 {
517         if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end;
518         if (end   >= dso->relro_start && end   < dso->relro_end) end = dso->relro_start;
519         if (start >= end) return;
520         char *base = laddr_pg(dso, start);
521         __malloc_donate(base, base+(end-start));
522 }
523
524 static void reclaim_gaps(struct dso *dso)
525 {
526         Phdr *ph = dso->phdr;
527         size_t phcnt = dso->phnum;
528
529         for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) {
530                 if (ph->p_type!=PT_LOAD) continue;
531                 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
532                 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
533                 reclaim(dso, ph->p_vaddr+ph->p_memsz,
534                         ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
535         }
536 }
537
538 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
539 {
540         static int no_map_fixed;
541         char *q;
542         if (!no_map_fixed) {
543                 q = mmap(p, n, prot, flags|MAP_FIXED, fd, off);
544                 if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL)
545                         return q;
546                 no_map_fixed = 1;
547         }
548         /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
549         if (flags & MAP_ANONYMOUS) {
550                 memset(p, 0, n);
551                 return p;
552         }
553         ssize_t r;
554         if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
555         for (q=p; n; q+=r, off+=r, n-=r) {
556                 r = read(fd, q, n);
557                 if (r < 0 && errno != EINTR) return MAP_FAILED;
558                 if (!r) {
559                         memset(q, 0, n);
560                         break;
561                 }
562         }
563         return p;
564 }
565
566 static void unmap_library(struct dso *dso)
567 {
568         if (dso->loadmap) {
569                 size_t i;
570                 for (i=0; i<dso->loadmap->nsegs; i++) {
571                         if (!dso->loadmap->segs[i].p_memsz)
572                                 continue;
573                         munmap((void *)dso->loadmap->segs[i].addr,
574                                 dso->loadmap->segs[i].p_memsz);
575                 }
576                 free(dso->loadmap);
577         } else if (dso->map && dso->map_len) {
578                 munmap(dso->map, dso->map_len);
579         }
580 }
581
582 static void *map_library(int fd, struct dso *dso)
583 {
584         Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
585         void *allocated_buf=0;
586         size_t phsize;
587         size_t addr_min=SIZE_MAX, addr_max=0, map_len;
588         size_t this_min, this_max;
589         size_t nsegs = 0;
590         off_t off_start;
591         Ehdr *eh;
592         Phdr *ph, *ph0;
593         unsigned prot;
594         unsigned char *map=MAP_FAILED, *base;
595         size_t dyn=0;
596         size_t tls_image=0;
597         size_t i;
598
599         ssize_t l = read(fd, buf, sizeof buf);
600         eh = buf;
601         if (l<0) return 0;
602         if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC))
603                 goto noexec;
604         phsize = eh->e_phentsize * eh->e_phnum;
605         if (phsize > sizeof buf - sizeof *eh) {
606                 allocated_buf = malloc(phsize);
607                 if (!allocated_buf) return 0;
608                 l = pread(fd, allocated_buf, phsize, eh->e_phoff);
609                 if (l < 0) goto error;
610                 if (l != phsize) goto noexec;
611                 ph = ph0 = allocated_buf;
612         } else if (eh->e_phoff + phsize > l) {
613                 l = pread(fd, buf+1, phsize, eh->e_phoff);
614                 if (l < 0) goto error;
615                 if (l != phsize) goto noexec;
616                 ph = ph0 = (void *)(buf + 1);
617         } else {
618                 ph = ph0 = (void *)((char *)buf + eh->e_phoff);
619         }
620         for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
621                 if (ph->p_type == PT_DYNAMIC) {
622                         dyn = ph->p_vaddr;
623                 } else if (ph->p_type == PT_TLS) {
624                         tls_image = ph->p_vaddr;
625                         dso->tls.align = ph->p_align;
626                         dso->tls.len = ph->p_filesz;
627                         dso->tls.size = ph->p_memsz;
628                 } else if (ph->p_type == PT_GNU_RELRO) {
629                         dso->relro_start = ph->p_vaddr & -PAGE_SIZE;
630                         dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
631                 } else if (ph->p_type == PT_GNU_STACK) {
632                         if (!runtime && ph->p_memsz > __default_stacksize) {
633                                 __default_stacksize =
634                                         ph->p_memsz < DEFAULT_STACK_MAX ?
635                                         ph->p_memsz : DEFAULT_STACK_MAX;
636                         }
637                 }
638                 if (ph->p_type != PT_LOAD) continue;
639                 nsegs++;
640                 if (ph->p_vaddr < addr_min) {
641                         addr_min = ph->p_vaddr;
642                         off_start = ph->p_offset;
643                         prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
644                                 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
645                                 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
646                 }
647                 if (ph->p_vaddr+ph->p_memsz > addr_max) {
648                         addr_max = ph->p_vaddr+ph->p_memsz;
649                 }
650         }
651         if (!dyn) goto noexec;
652         if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
653                 dso->loadmap = calloc(1, sizeof *dso->loadmap
654                         + nsegs * sizeof *dso->loadmap->segs);
655                 if (!dso->loadmap) goto error;
656                 dso->loadmap->nsegs = nsegs;
657                 for (ph=ph0, i=0; i<nsegs; ph=(void *)((char *)ph+eh->e_phentsize)) {
658                         if (ph->p_type != PT_LOAD) continue;
659                         prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
660                                 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
661                                 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
662                         map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1),
663                                 prot, MAP_PRIVATE,
664                                 fd, ph->p_offset & -PAGE_SIZE);
665                         if (map == MAP_FAILED) {
666                                 unmap_library(dso);
667                                 goto error;
668                         }
669                         dso->loadmap->segs[i].addr = (size_t)map +
670                                 (ph->p_vaddr & PAGE_SIZE-1);
671                         dso->loadmap->segs[i].p_vaddr = ph->p_vaddr;
672                         dso->loadmap->segs[i].p_memsz = ph->p_memsz;
673                         i++;
674                         if (prot & PROT_WRITE) {
675                                 size_t brk = (ph->p_vaddr & PAGE_SIZE-1)
676                                         + ph->p_filesz;
677                                 size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE;
678                                 size_t pgend = brk + ph->p_memsz - ph->p_filesz
679                                         + PAGE_SIZE-1 & -PAGE_SIZE;
680                                 if (pgend > pgbrk && mmap_fixed(map+pgbrk,
681                                         pgend-pgbrk, prot,
682                                         MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,
683                                         -1, off_start) == MAP_FAILED)
684                                         goto error;
685                                 memset(map + brk, 0, pgbrk-brk);
686                         }
687                 }
688                 map = (void *)dso->loadmap->segs[0].addr;
689                 map_len = 0;
690                 goto done_mapping;
691         }
692         addr_max += PAGE_SIZE-1;
693         addr_max &= -PAGE_SIZE;
694         addr_min &= -PAGE_SIZE;
695         off_start &= -PAGE_SIZE;
696         map_len = addr_max - addr_min + off_start;
697         /* The first time, we map too much, possibly even more than
698          * the length of the file. This is okay because we will not
699          * use the invalid part; we just need to reserve the right
700          * amount of virtual address space to map over later. */
701         map = DL_NOMMU_SUPPORT
702                 ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC,
703                         MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
704                 : mmap((void *)addr_min, map_len, prot,
705                         MAP_PRIVATE, fd, off_start);
706         if (map==MAP_FAILED) goto error;
707         dso->map = map;
708         dso->map_len = map_len;
709         /* If the loaded file is not relocatable and the requested address is
710          * not available, then the load operation must fail. */
711         if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) {
712                 errno = EBUSY;
713                 goto error;
714         }
715         base = map - addr_min;
716         dso->phdr = 0;
717         dso->phnum = 0;
718         for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
719                 if (ph->p_type != PT_LOAD) continue;
720                 /* Check if the programs headers are in this load segment, and
721                  * if so, record the address for use by dl_iterate_phdr. */
722                 if (!dso->phdr && eh->e_phoff >= ph->p_offset
723                     && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) {
724                         dso->phdr = (void *)(base + ph->p_vaddr
725                                 + (eh->e_phoff-ph->p_offset));
726                         dso->phnum = eh->e_phnum;
727                         dso->phentsize = eh->e_phentsize;
728                 }
729                 this_min = ph->p_vaddr & -PAGE_SIZE;
730                 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
731                 off_start = ph->p_offset & -PAGE_SIZE;
732                 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
733                         ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
734                         ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
735                 /* Reuse the existing mapping for the lowest-address LOAD */
736                 if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT)
737                         if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
738                                 goto error;
739                 if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) {
740                         size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
741                         size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
742                         memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
743                         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)
744                                 goto error;
745                 }
746         }
747         for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
748                 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
749                         if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
750                             && errno != ENOSYS)
751                                 goto error;
752                         break;
753                 }
754 done_mapping:
755         dso->base = base;
756         dso->dynv = laddr(dso, dyn);
757         if (dso->tls.size) dso->tls.image = laddr(dso, tls_image);
758         free(allocated_buf);
759         return map;
760 noexec:
761         errno = ENOEXEC;
762 error:
763         if (map!=MAP_FAILED) unmap_library(dso);
764         free(allocated_buf);
765         return 0;
766 }
767
768 static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
769 {
770         size_t l;
771         int fd;
772         for (;;) {
773                 s += strspn(s, ":\n");
774                 l = strcspn(s, ":\n");
775                 if (l-1 >= INT_MAX) return -1;
776                 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) {
777                         if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
778                         switch (errno) {
779                         case ENOENT:
780                         case ENOTDIR:
781                         case EACCES:
782                         case ENAMETOOLONG:
783                                 break;
784                         default:
785                                 /* Any negative value but -1 will inhibit
786                                  * futher path search. */
787                                 return -2;
788                         }
789                 }
790                 s += l;
791         }
792 }
793
794 static int fixup_rpath(struct dso *p, char *buf, size_t buf_size)
795 {
796         size_t n, l;
797         const char *s, *t, *origin;
798         char *d;
799         if (p->rpath || !p->rpath_orig) return 0;
800         if (!strchr(p->rpath_orig, '$')) {
801                 p->rpath = p->rpath_orig;
802                 return 0;
803         }
804         n = 0;
805         s = p->rpath_orig;
806         while ((t=strchr(s, '$'))) {
807                 if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9))
808                         return 0;
809                 s = t+1;
810                 n++;
811         }
812         if (n > SSIZE_MAX/PATH_MAX) return 0;
813
814         if (p->kernel_mapped) {
815                 /* $ORIGIN searches cannot be performed for the main program
816                  * when it is suid/sgid/AT_SECURE. This is because the
817                  * pathname is under the control of the caller of execve.
818                  * For libraries, however, $ORIGIN can be processed safely
819                  * since the library's pathname came from a trusted source
820                  * (either system paths or a call to dlopen). */
821                 if (libc.secure)
822                         return 0;
823                 l = readlink("/proc/self/exe", buf, buf_size);
824                 if (l == -1) switch (errno) {
825                 case ENOENT:
826                 case ENOTDIR:
827                 case EACCES:
828                         break;
829                 default:
830                         return -1;
831                 }
832                 if (l >= buf_size)
833                         return 0;
834                 buf[l] = 0;
835                 origin = buf;
836         } else {
837                 origin = p->name;
838         }
839         t = strrchr(origin, '/');
840         if (t) {
841                 l = t-origin;
842         } else {
843                 /* Normally p->name will always be an absolute or relative
844                  * pathname containing at least one '/' character, but in the
845                  * case where ldso was invoked as a command to execute a
846                  * program in the working directory, app.name may not. Fix. */
847                 origin = ".";
848                 l = 1;
849         }
850         /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
851         if (libc.secure && *origin != '/')
852                 return 0;
853         p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1);
854         if (!p->rpath) return -1;
855
856         d = p->rpath;
857         s = p->rpath_orig;
858         while ((t=strchr(s, '$'))) {
859                 memcpy(d, s, t-s);
860                 d += t-s;
861                 memcpy(d, origin, l);
862                 d += l;
863                 /* It was determined previously that the '$' is followed
864                  * either by "ORIGIN" or "{ORIGIN}". */
865                 s = t + 7 + 2*(t[1]=='{');
866         }
867         strcpy(d, s);
868         return 0;
869 }
870
871 static void decode_dyn(struct dso *p)
872 {
873         size_t dyn[DYN_CNT];
874         decode_vec(p->dynv, dyn, DYN_CNT);
875         p->syms = laddr(p, dyn[DT_SYMTAB]);
876         p->strings = laddr(p, dyn[DT_STRTAB]);
877         if (dyn[0]&(1<<DT_HASH))
878                 p->hashtab = laddr(p, dyn[DT_HASH]);
879         if (dyn[0]&(1<<DT_RPATH))
880                 p->rpath_orig = p->strings + dyn[DT_RPATH];
881         if (dyn[0]&(1<<DT_RUNPATH))
882                 p->rpath_orig = p->strings + dyn[DT_RUNPATH];
883         if (dyn[0]&(1<<DT_PLTGOT))
884                 p->got = laddr(p, dyn[DT_PLTGOT]);
885         if (search_vec(p->dynv, dyn, DT_GNU_HASH))
886                 p->ghashtab = laddr(p, *dyn);
887         if (search_vec(p->dynv, dyn, DT_VERSYM))
888                 p->versym = laddr(p, *dyn);
889 }
890
891 static size_t count_syms(struct dso *p)
892 {
893         if (p->hashtab) return p->hashtab[1];
894
895         size_t nsym, i;
896         uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
897         uint32_t *hashval;
898         for (i = nsym = 0; i < p->ghashtab[0]; i++) {
899                 if (buckets[i] > nsym)
900                         nsym = buckets[i];
901         }
902         if (nsym) {
903                 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]);
904                 do nsym++;
905                 while (!(*hashval++ & 1));
906         }
907         return nsym;
908 }
909
910 static void *dl_mmap(size_t n)
911 {
912         void *p;
913         int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE;
914 #ifdef SYS_mmap2
915         p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0);
916 #else
917         p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0);
918 #endif
919         return p == MAP_FAILED ? 0 : p;
920 }
921
922 static void makefuncdescs(struct dso *p)
923 {
924         static int self_done;
925         size_t nsym = count_syms(p);
926         size_t i, size = nsym * sizeof(*p->funcdescs);
927
928         if (!self_done) {
929                 p->funcdescs = dl_mmap(size);
930                 self_done = 1;
931         } else {
932                 p->funcdescs = malloc(size);
933         }
934         if (!p->funcdescs) {
935                 if (!runtime) a_crash();
936                 error("Error allocating function descriptors for %s", p->name);
937                 longjmp(*rtld_fail, 1);
938         }
939         for (i=0; i<nsym; i++) {
940                 if ((p->syms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) {
941                         p->funcdescs[i].addr = laddr(p, p->syms[i].st_value);
942                         p->funcdescs[i].got = p->got;
943                 } else {
944                         p->funcdescs[i].addr = 0;
945                         p->funcdescs[i].got = 0;
946                 }
947         }
948 }
949
950 static struct dso *load_library(const char *name, struct dso *needed_by)
951 {
952         char buf[2*NAME_MAX+2];
953         const char *pathname;
954         unsigned char *map;
955         struct dso *p, temp_dso = {0};
956         int fd;
957         struct stat st;
958         size_t alloc_size;
959         int n_th = 0;
960         int is_self = 0;
961
962         if (!*name) {
963                 errno = EINVAL;
964                 return 0;
965         }
966
967         /* Catch and block attempts to reload the implementation itself */
968         if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
969                 static const char reserved[] =
970                         "c.pthread.rt.m.dl.util.xnet.";
971                 const char *rp, *next;
972                 for (rp=reserved; *rp; rp=next) {
973                         next = strchr(rp, '.') + 1;
974                         if (strncmp(name+3, rp, next-rp) == 0)
975                                 break;
976                 }
977                 if (*rp) {
978                         if (ldd_mode) {
979                                 /* Track which names have been resolved
980                                  * and only report each one once. */
981                                 static unsigned reported;
982                                 unsigned mask = 1U<<(rp-reserved);
983                                 if (!(reported & mask)) {
984                                         reported |= mask;
985                                         dprintf(1, "\t%s => %s (%p)\n",
986                                                 name, ldso.name,
987                                                 ldso.base);
988                                 }
989                         }
990                         is_self = 1;
991                 }
992         }
993         if (!strcmp(name, ldso.name)) is_self = 1;
994         if (is_self) {
995                 if (!ldso.prev) {
996                         tail->next = &ldso;
997                         ldso.prev = tail;
998                         tail = &ldso;
999                 }
1000                 return &ldso;
1001         }
1002         if (strchr(name, '/')) {
1003                 pathname = name;
1004                 fd = open(name, O_RDONLY|O_CLOEXEC);
1005         } else {
1006                 /* Search for the name to see if it's already loaded */
1007                 for (p=head->next; p; p=p->next) {
1008                         if (p->shortname && !strcmp(p->shortname, name)) {
1009                                 return p;
1010                         }
1011                 }
1012                 if (strlen(name) > NAME_MAX) return 0;
1013                 fd = -1;
1014                 if (env_path) fd = path_open(name, env_path, buf, sizeof buf);
1015                 for (p=needed_by; fd == -1 && p; p=p->needed_by) {
1016                         if (fixup_rpath(p, buf, sizeof buf) < 0)
1017                                 fd = -2; /* Inhibit further search. */
1018                         if (p->rpath)
1019                                 fd = path_open(name, p->rpath, buf, sizeof buf);
1020                 }
1021                 if (fd == -1) {
1022                         if (!sys_path) {
1023                                 char *prefix = 0;
1024                                 size_t prefix_len;
1025                                 if (ldso.name[0]=='/') {
1026                                         char *s, *t, *z;
1027                                         for (s=t=z=ldso.name; *s; s++)
1028                                                 if (*s=='/') z=t, t=s;
1029                                         prefix_len = z-ldso.name;
1030                                         if (prefix_len < PATH_MAX)
1031                                                 prefix = ldso.name;
1032                                 }
1033                                 if (!prefix) {
1034                                         prefix = "";
1035                                         prefix_len = 0;
1036                                 }
1037                                 char etc_ldso_path[prefix_len + 1
1038                                         + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
1039                                 snprintf(etc_ldso_path, sizeof etc_ldso_path,
1040                                         "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
1041                                         (int)prefix_len, prefix);
1042                                 FILE *f = fopen(etc_ldso_path, "rbe");
1043                                 if (f) {
1044                                         if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
1045                                                 free(sys_path);
1046                                                 sys_path = "";
1047                                         }
1048                                         fclose(f);
1049                                 } else if (errno != ENOENT) {
1050                                         sys_path = "";
1051                                 }
1052                         }
1053                         if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
1054                         fd = path_open(name, sys_path, buf, sizeof buf);
1055                 }
1056                 pathname = buf;
1057         }
1058         if (fd < 0) return 0;
1059         if (fstat(fd, &st) < 0) {
1060                 close(fd);
1061                 return 0;
1062         }
1063         for (p=head->next; p; p=p->next) {
1064                 if (p->dev == st.st_dev && p->ino == st.st_ino) {
1065                         /* If this library was previously loaded with a
1066                          * pathname but a search found the same inode,
1067                          * setup its shortname so it can be found by name. */
1068                         if (!p->shortname && pathname != name)
1069                                 p->shortname = strrchr(p->name, '/')+1;
1070                         close(fd);
1071                         return p;
1072                 }
1073         }
1074         map = noload ? 0 : map_library(fd, &temp_dso);
1075         close(fd);
1076         if (!map) return 0;
1077
1078         /* Avoid the danger of getting two versions of libc mapped into the
1079          * same process when an absolute pathname was used. The symbols
1080          * checked are chosen to catch both musl and glibc, and to avoid
1081          * false positives from interposition-hack libraries. */
1082         decode_dyn(&temp_dso);
1083         if (find_sym(&temp_dso, "__libc_start_main", 1).sym &&
1084             find_sym(&temp_dso, "stdin", 1).sym) {
1085                 unmap_library(&temp_dso);
1086                 return load_library("libc.so", needed_by);
1087         }
1088         /* Past this point, if we haven't reached runtime yet, ldso has
1089          * committed either to use the mapped library or to abort execution.
1090          * Unmapping is not possible, so we can safely reclaim gaps. */
1091         if (!runtime) reclaim_gaps(&temp_dso);
1092
1093         /* Allocate storage for the new DSO. When there is TLS, this
1094          * storage must include a reservation for all pre-existing
1095          * threads to obtain copies of both the new TLS, and an
1096          * extended DTV capable of storing an additional slot for
1097          * the newly-loaded DSO. */
1098         alloc_size = sizeof *p + strlen(pathname) + 1;
1099         if (runtime && temp_dso.tls.image) {
1100                 size_t per_th = temp_dso.tls.size + temp_dso.tls.align
1101                         + sizeof(void *) * (tls_cnt+3);
1102                 n_th = libc.threads_minus_1 + 1;
1103                 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
1104                 else alloc_size += n_th * per_th;
1105         }
1106         p = calloc(1, alloc_size);
1107         if (!p) {
1108                 unmap_library(&temp_dso);
1109                 return 0;
1110         }
1111         memcpy(p, &temp_dso, sizeof temp_dso);
1112         p->dev = st.st_dev;
1113         p->ino = st.st_ino;
1114         p->needed_by = needed_by;
1115         p->name = p->buf;
1116         p->runtime_loaded = runtime;
1117         strcpy(p->name, pathname);
1118         /* Add a shortname only if name arg was not an explicit pathname. */
1119         if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
1120         if (p->tls.image) {
1121                 p->tls_id = ++tls_cnt;
1122                 tls_align = MAXP2(tls_align, p->tls.align);
1123 #ifdef TLS_ABOVE_TP
1124                 p->tls.offset = tls_offset + ( (tls_align-1) &
1125                         -(tls_offset + (uintptr_t)p->tls.image) );
1126                 tls_offset += p->tls.size;
1127 #else
1128                 tls_offset += p->tls.size + p->tls.align - 1;
1129                 tls_offset -= (tls_offset + (uintptr_t)p->tls.image)
1130                         & (p->tls.align-1);
1131                 p->tls.offset = tls_offset;
1132 #endif
1133                 p->new_dtv = (void *)(-sizeof(size_t) &
1134                         (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
1135                 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
1136                 if (tls_tail) tls_tail->next = &p->tls;
1137                 else libc.tls_head = &p->tls;
1138                 tls_tail = &p->tls;
1139         }
1140
1141         tail->next = p;
1142         p->prev = tail;
1143         tail = p;
1144
1145         if (DL_FDPIC) makefuncdescs(p);
1146
1147         if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
1148
1149         return p;
1150 }
1151
1152 static void load_direct_deps(struct dso *p)
1153 {
1154         size_t i, cnt=0;
1155         if (p->deps) return;
1156         /* For head, all preloads are direct pseudo-dependencies.
1157          * Count and include them now to avoid realloc later. */
1158         if (p==head) for (struct dso *q=p->next; q; q=q->next)
1159                 cnt++;
1160         for (i=0; p->dynv[i]; i+=2)
1161                 if (p->dynv[i] == DT_NEEDED) cnt++;
1162         p->deps = calloc(cnt+1, sizeof *p->deps);
1163         if (!p->deps) {
1164                 error("Error loading dependencies for %s", p->name);
1165                 if (runtime) longjmp(*rtld_fail, 1);
1166         }
1167         cnt=0;
1168         if (p==head) for (struct dso *q=p->next; q; q=q->next)
1169                 p->deps[cnt++] = q;
1170         for (i=0; p->dynv[i]; i+=2) {
1171                 if (p->dynv[i] != DT_NEEDED) continue;
1172                 struct dso *dep = load_library(p->strings + p->dynv[i+1], p);
1173                 if (!dep) {
1174                         error("Error loading shared library %s: %m (needed by %s)",
1175                                 p->strings + p->dynv[i+1], p->name);
1176                         if (runtime) longjmp(*rtld_fail, 1);
1177                         continue;
1178                 }
1179                 p->deps[cnt++] = dep;
1180         }
1181         p->deps[cnt] = 0;
1182         p->ndeps_direct = cnt;
1183 }
1184
1185 static void load_deps(struct dso *p)
1186 {
1187         if (p->deps) return;
1188         for (; p; p=p->next)
1189                 load_direct_deps(p);
1190 }
1191
1192 static void extend_bfs_deps(struct dso *p)
1193 {
1194         size_t i, j, cnt, ndeps_all;
1195         struct dso **tmp;
1196
1197         /* Can't use realloc if the original p->deps was allocated at
1198          * program entry and malloc has been replaced. */
1199         int no_realloc = __malloc_replaced && !p->runtime_loaded;
1200
1201         if (p->bfs_built) return;
1202         ndeps_all = p->ndeps_direct;
1203
1204         /* Mark existing (direct) deps so they won't be duplicated. */
1205         for (i=0; p->deps[i]; i++)
1206                 p->deps[i]->mark = 1;
1207
1208         /* For each dependency already in the list, copy its list of direct
1209          * dependencies to the list, excluding any items already in the
1210          * list. Note that the list this loop iterates over will grow during
1211          * the loop, but since duplicates are excluded, growth is bounded. */
1212         for (i=0; p->deps[i]; i++) {
1213                 struct dso *dep = p->deps[i];
1214                 for (j=cnt=0; j<dep->ndeps_direct; j++)
1215                         if (!dep->deps[j]->mark) cnt++;
1216                 tmp = no_realloc ? 
1217                         malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) :
1218                         realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1));
1219                 if (!tmp) {
1220                         error("Error recording dependencies for %s", p->name);
1221                         if (runtime) longjmp(*rtld_fail, 1);
1222                         continue;
1223                 }
1224                 if (no_realloc) {
1225                         memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1));
1226                         no_realloc = 0;
1227                 }
1228                 p->deps = tmp;
1229                 for (j=0; j<dep->ndeps_direct; j++) {
1230                         if (dep->deps[j]->mark) continue;
1231                         dep->deps[j]->mark = 1;
1232                         p->deps[ndeps_all++] = dep->deps[j];
1233                 }
1234                 p->deps[ndeps_all] = 0;
1235         }
1236         p->bfs_built = 1;
1237         for (p=head; p; p=p->next)
1238                 p->mark = 0;
1239 }
1240
1241 static void load_preload(char *s)
1242 {
1243         int tmp;
1244         char *z;
1245         for (z=s; *z; s=z) {
1246                 for (   ; *s && (isspace(*s) || *s==':'); s++);
1247                 for (z=s; *z && !isspace(*z) && *z!=':'; z++);
1248                 tmp = *z;
1249                 *z = 0;
1250                 load_library(s, 0);
1251                 *z = tmp;
1252         }
1253 }
1254
1255 static void add_syms(struct dso *p)
1256 {
1257         if (!p->syms_next && syms_tail != p) {
1258                 syms_tail->syms_next = p;
1259                 syms_tail = p;
1260         }
1261 }
1262
1263 static void revert_syms(struct dso *old_tail)
1264 {
1265         struct dso *p, *next;
1266         /* Chop off the tail of the list of dsos that participate in
1267          * the global symbol table, reverting them to RTLD_LOCAL. */
1268         for (p=old_tail; p; p=next) {
1269                 next = p->syms_next;
1270                 p->syms_next = 0;
1271         }
1272         syms_tail = old_tail;
1273 }
1274
1275 static void do_mips_relocs(struct dso *p, size_t *got)
1276 {
1277         size_t i, j, rel[2];
1278         unsigned char *base = p->base;
1279         i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO);
1280         if (p==&ldso) {
1281                 got += i;
1282         } else {
1283                 while (i--) *got++ += (size_t)base;
1284         }
1285         j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1286         i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1287         Sym *sym = p->syms + j;
1288         rel[0] = (unsigned char *)got - base;
1289         for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) {
1290                 rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT);
1291                 do_relocs(p, rel, sizeof rel, 2);
1292         }
1293 }
1294
1295 static void reloc_all(struct dso *p)
1296 {
1297         size_t dyn[DYN_CNT];
1298         for (; p; p=p->next) {
1299                 if (p->relocated) continue;
1300                 decode_vec(p->dynv, dyn, DYN_CNT);
1301                 if (NEED_MIPS_GOT_RELOCS)
1302                         do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT]));
1303                 do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
1304                         2+(dyn[DT_PLTREL]==DT_RELA));
1305                 do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
1306                 do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
1307
1308                 if (head != &ldso && p->relro_start != p->relro_end &&
1309                     mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
1310                     && errno != ENOSYS) {
1311                         error("Error relocating %s: RELRO protection failed: %m",
1312                                 p->name);
1313                         if (runtime) longjmp(*rtld_fail, 1);
1314                 }
1315
1316                 p->relocated = 1;
1317         }
1318 }
1319
1320 static void kernel_mapped_dso(struct dso *p)
1321 {
1322         size_t min_addr = -1, max_addr = 0, cnt;
1323         Phdr *ph = p->phdr;
1324         for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) {
1325                 if (ph->p_type == PT_DYNAMIC) {
1326                         p->dynv = laddr(p, ph->p_vaddr);
1327                 } else if (ph->p_type == PT_GNU_RELRO) {
1328                         p->relro_start = ph->p_vaddr & -PAGE_SIZE;
1329                         p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
1330                 } else if (ph->p_type == PT_GNU_STACK) {
1331                         if (!runtime && ph->p_memsz > __default_stacksize) {
1332                                 __default_stacksize =
1333                                         ph->p_memsz < DEFAULT_STACK_MAX ?
1334                                         ph->p_memsz : DEFAULT_STACK_MAX;
1335                         }
1336                 }
1337                 if (ph->p_type != PT_LOAD) continue;
1338                 if (ph->p_vaddr < min_addr)
1339                         min_addr = ph->p_vaddr;
1340                 if (ph->p_vaddr+ph->p_memsz > max_addr)
1341                         max_addr = ph->p_vaddr+ph->p_memsz;
1342         }
1343         min_addr &= -PAGE_SIZE;
1344         max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
1345         p->map = p->base + min_addr;
1346         p->map_len = max_addr - min_addr;
1347         p->kernel_mapped = 1;
1348 }
1349
1350 void __libc_exit_fini()
1351 {
1352         struct dso *p;
1353         size_t dyn[DYN_CNT];
1354         int self = __pthread_self()->tid;
1355
1356         /* Take both locks before setting shutting_down, so that
1357          * either lock is sufficient to read its value. The lock
1358          * order matches that in dlopen to avoid deadlock. */
1359         pthread_rwlock_wrlock(&lock);
1360         pthread_mutex_lock(&init_fini_lock);
1361         shutting_down = 1;
1362         pthread_rwlock_unlock(&lock);
1363         for (p=fini_head; p; p=p->fini_next) {
1364                 while (p->ctor_visitor && p->ctor_visitor!=self)
1365                         pthread_cond_wait(&ctor_cond, &init_fini_lock);
1366                 if (!p->constructed) continue;
1367                 decode_vec(p->dynv, dyn, DYN_CNT);
1368                 if (dyn[0] & (1<<DT_FINI_ARRAY)) {
1369                         size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
1370                         size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY])+n;
1371                         while (n--) ((void (*)(void))*--fn)();
1372                 }
1373 #ifndef NO_LEGACY_INITFINI
1374                 if ((dyn[0] & (1<<DT_FINI)) && dyn[DT_FINI])
1375                         fpaddr(p, dyn[DT_FINI])();
1376 #endif
1377         }
1378 }
1379
1380 static struct dso **queue_ctors(struct dso *dso)
1381 {
1382         size_t cnt, qpos, spos, i;
1383         struct dso *p, **queue, **stack;
1384
1385         if (ldd_mode) return 0;
1386
1387         /* Bound on queue size is the total number of indirect deps.
1388          * If a bfs deps list was built, we can use it. Otherwise,
1389          * bound by the total number of DSOs, which is always safe and
1390          * is reasonable we use it (for main app at startup). */
1391         if (dso->bfs_built) {
1392                 for (cnt=0; dso->deps[cnt]; cnt++)
1393                         dso->deps[cnt]->mark = 0;
1394                 cnt++; /* self, not included in deps */
1395         } else {
1396                 for (cnt=0, p=head; p; cnt++, p=p->next)
1397                         p->mark = 0;
1398         }
1399         cnt++; /* termination slot */
1400         stack = queue = calloc(cnt, sizeof *queue);
1401
1402         if (!queue) {
1403                 error("Error allocating constructor queue: %m\n");
1404                 if (runtime) longjmp(*rtld_fail, 1);
1405                 return 0;
1406         }
1407
1408         /* Opposite ends of the allocated buffer serve as an output queue
1409          * and a working stack. Setup initial stack with just the argument
1410          * dso and initial queue empty... */
1411         qpos = 0;
1412         spos = cnt;
1413         stack[--spos] = dso;
1414         dso->next_dep = 0;
1415         dso->mark = 1;
1416
1417         /* Then perform pseudo-DFS sort, but ignoring circular deps. */
1418         while (spos<cnt) {
1419                 p = stack[spos++];
1420                 while (p->next_dep < p->ndeps_direct) {
1421                         if (p->deps[p->next_dep]->mark) {
1422                                 p->next_dep++;
1423                         } else {
1424                                 stack[--spos] = p;
1425                                 p = p->deps[p->next_dep];
1426                                 p->next_dep = 0;
1427                                 p->mark = 1;
1428                         }
1429                 }
1430                 queue[qpos++] = p;
1431         }
1432         queue[qpos] = 0;
1433         for (i=0; i<qpos; i++) queue[i]->mark = 0;
1434
1435         return queue;
1436 }
1437
1438 static void do_init_fini(struct dso **queue)
1439 {
1440         struct dso *p;
1441         size_t dyn[DYN_CNT], i;
1442         int self = __pthread_self()->tid;
1443
1444         pthread_mutex_lock(&init_fini_lock);
1445         for (i=0; (p=queue[i]); i++) {
1446                 while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down)
1447                         pthread_cond_wait(&ctor_cond, &init_fini_lock);
1448                 if (p->ctor_visitor || p->constructed)
1449                         continue;
1450                 if (p->constructed) continue;
1451                 p->ctor_visitor = self;
1452                 
1453                 decode_vec(p->dynv, dyn, DYN_CNT);
1454                 if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
1455                         p->fini_next = fini_head;
1456                         fini_head = p;
1457                 }
1458
1459                 pthread_mutex_unlock(&init_fini_lock);
1460
1461 #ifndef NO_LEGACY_INITFINI
1462                 if ((dyn[0] & (1<<DT_INIT)) && dyn[DT_INIT])
1463                         fpaddr(p, dyn[DT_INIT])();
1464 #endif
1465                 if (dyn[0] & (1<<DT_INIT_ARRAY)) {
1466                         size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
1467                         size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]);
1468                         while (n--) ((void (*)(void))*fn++)();
1469                 }
1470
1471                 pthread_mutex_lock(&init_fini_lock);
1472                 p->ctor_visitor = 0;
1473                 p->constructed = 1;
1474                 pthread_cond_broadcast(&ctor_cond);
1475         }
1476         pthread_mutex_unlock(&init_fini_lock);
1477 }
1478
1479 void __libc_start_init(void)
1480 {
1481         do_init_fini(main_ctor_queue);
1482         /* This is valid because the queue was allocated after redoing
1483          * relocations with any interposed malloc having taken effect. */
1484         free(main_ctor_queue);
1485         main_ctor_queue = 0;
1486 }
1487
1488 static void dl_debug_state(void)
1489 {
1490 }
1491
1492 weak_alias(dl_debug_state, _dl_debug_state);
1493
1494 void __init_tls(size_t *auxv)
1495 {
1496 }
1497
1498 static void update_tls_size()
1499 {
1500         libc.tls_cnt = tls_cnt;
1501         libc.tls_align = tls_align;
1502         libc.tls_size = ALIGN(
1503                 (1+tls_cnt) * sizeof(void *) +
1504                 tls_offset +
1505                 sizeof(struct pthread) +
1506                 tls_align * 2,
1507         tls_align);
1508 }
1509
1510 static void install_new_tls(void)
1511 {
1512         sigset_t set;
1513         pthread_t self = __pthread_self(), td;
1514         struct dso *dtv_provider = container_of(tls_tail, struct dso, tls);
1515         uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv;
1516         struct dso *p;
1517         size_t i, j;
1518         size_t old_cnt = self->dtv[0];
1519
1520         __block_app_sigs(&set);
1521         __tl_lock();
1522         /* Copy existing dtv contents from all existing threads. */
1523         for (i=0, td=self; !i || td!=self; i++, td=td->next) {
1524                 memcpy(newdtv+i, td->dtv,
1525                         (old_cnt+1)*sizeof(uintptr_t));
1526                 newdtv[i][0] = tls_cnt;
1527         }
1528         /* Install new dtls into the enlarged, uninstalled dtv copies. */
1529         for (p=head; ; p=p->next) {
1530                 if (p->tls_id <= old_cnt) continue;
1531                 unsigned char *mem = p->new_tls;
1532                 for (j=0; j<i; j++) {
1533                         unsigned char *new = mem;
1534                         new += ((uintptr_t)p->tls.image - (uintptr_t)mem)
1535                                 & (p->tls.align-1);
1536                         memcpy(new, p->tls.image, p->tls.len);
1537                         newdtv[j][p->tls_id] =
1538                                 (uintptr_t)new + DTP_OFFSET;
1539                         mem += p->tls.size + p->tls.align;
1540                 }
1541                 if (p->tls_id == tls_cnt) break;
1542         }
1543
1544         /* Broadcast barrier to ensure contents of new dtv is visible
1545          * if the new dtv pointer is. The __membarrier function has a
1546          * fallback emulation using signals for kernels that lack the
1547          * feature at the syscall level. */
1548
1549         __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
1550
1551         /* Install new dtv for each thread. */
1552         for (j=0, td=self; !j || td!=self; j++, td=td->next) {
1553                 td->dtv = td->dtv_copy = newdtv[j];
1554         }
1555
1556         __tl_unlock();
1557         __restore_sigs(&set);
1558 }
1559
1560 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1561  * following stage 2 and stage 3 functions via primitive symbolic lookup
1562  * since it does not have access to their addresses to begin with. */
1563
1564 /* Stage 2 of the dynamic linker is called after relative relocations 
1565  * have been processed. It can make function calls to static functions
1566  * and access string literals and static data, but cannot use extern
1567  * symbols. Its job is to perform symbolic relocations on the dynamic
1568  * linker itself, but some of the relocations performed may need to be
1569  * replaced later due to copy relocations in the main program. */
1570
1571 hidden void __dls2(unsigned char *base, size_t *sp)
1572 {
1573         if (DL_FDPIC) {
1574                 void *p1 = (void *)sp[-2];
1575                 void *p2 = (void *)sp[-1];
1576                 if (!p1) {
1577                         size_t *auxv, aux[AUX_CNT];
1578                         for (auxv=sp+1+*sp+1; *auxv; auxv++);
1579                         auxv++;
1580                         decode_vec(auxv, aux, AUX_CNT);
1581                         if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE];
1582                         else ldso.base = (void *)(aux[AT_PHDR] & -4096);
1583                 }
1584                 app_loadmap = p2 ? p1 : 0;
1585                 ldso.loadmap = p2 ? p2 : p1;
1586                 ldso.base = laddr(&ldso, 0);
1587         } else {
1588                 ldso.base = base;
1589         }
1590         Ehdr *ehdr = (void *)ldso.base;
1591         ldso.name = ldso.shortname = "libc.so";
1592         ldso.phnum = ehdr->e_phnum;
1593         ldso.phdr = laddr(&ldso, ehdr->e_phoff);
1594         ldso.phentsize = ehdr->e_phentsize;
1595         kernel_mapped_dso(&ldso);
1596         decode_dyn(&ldso);
1597
1598         if (DL_FDPIC) makefuncdescs(&ldso);
1599
1600         /* Prepare storage for to save clobbered REL addends so they
1601          * can be reused in stage 3. There should be very few. If
1602          * something goes wrong and there are a huge number, abort
1603          * instead of risking stack overflow. */
1604         size_t dyn[DYN_CNT];
1605         decode_vec(ldso.dynv, dyn, DYN_CNT);
1606         size_t *rel = laddr(&ldso, dyn[DT_REL]);
1607         size_t rel_size = dyn[DT_RELSZ];
1608         size_t symbolic_rel_cnt = 0;
1609         apply_addends_to = rel;
1610         for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t))
1611                 if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++;
1612         if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash();
1613         size_t addends[symbolic_rel_cnt+1];
1614         saved_addends = addends;
1615
1616         head = &ldso;
1617         reloc_all(&ldso);
1618
1619         ldso.relocated = 0;
1620
1621         /* Call dynamic linker stage-2b, __dls2b, looking it up
1622          * symbolically as a barrier against moving the address
1623          * load across the above relocation processing. */
1624         struct symdef dls2b_def = find_sym(&ldso, "__dls2b", 0);
1625         if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp);
1626         else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp);
1627 }
1628
1629 /* Stage 2b sets up a valid thread pointer, which requires relocations
1630  * completed in stage 2, and on which stage 3 is permitted to depend.
1631  * This is done as a separate stage, with symbolic lookup as a barrier,
1632  * so that loads of the thread pointer and &errno can be pure/const and
1633  * thereby hoistable. */
1634
1635 _Noreturn void __dls2b(size_t *sp)
1636 {
1637         /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1638          * use during dynamic linking. If possible it will also serve as the
1639          * thread pointer at runtime. */
1640         libc.tls_size = sizeof builtin_tls;
1641         libc.tls_align = tls_align;
1642         if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) {
1643                 a_crash();
1644         }
1645
1646         struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
1647         if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp);
1648         else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp);
1649 }
1650
1651 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1652  * fully functional. Its job is to load (if not already loaded) and
1653  * process dependencies and relocations for the main application and
1654  * transfer control to its entry point. */
1655
1656 _Noreturn void __dls3(size_t *sp)
1657 {
1658         static struct dso app, vdso;
1659         size_t aux[AUX_CNT], *auxv;
1660         size_t i;
1661         char *env_preload=0;
1662         char *replace_argv0=0;
1663         size_t vdso_base;
1664         int argc = *sp;
1665         char **argv = (void *)(sp+1);
1666         char **argv_orig = argv;
1667         char **envp = argv+argc+1;
1668
1669         /* Find aux vector just past environ[] and use it to initialize
1670          * global data that may be needed before we can make syscalls. */
1671         __environ = envp;
1672         for (i=argc+1; argv[i]; i++);
1673         libc.auxv = auxv = (void *)(argv+i+1);
1674         decode_vec(auxv, aux, AUX_CNT);
1675         __hwcap = aux[AT_HWCAP];
1676         libc.page_size = aux[AT_PAGESZ];
1677         libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1678                 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
1679
1680         /* Only trust user/env if kernel says we're not suid/sgid */
1681         if (!libc.secure) {
1682                 env_path = getenv("LD_LIBRARY_PATH");
1683                 env_preload = getenv("LD_PRELOAD");
1684         }
1685
1686         /* If the main program was already loaded by the kernel,
1687          * AT_PHDR will point to some location other than the dynamic
1688          * linker's program headers. */
1689         if (aux[AT_PHDR] != (size_t)ldso.phdr) {
1690                 size_t interp_off = 0;
1691                 size_t tls_image = 0;
1692                 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1693                 Phdr *phdr = app.phdr = (void *)aux[AT_PHDR];
1694                 app.phnum = aux[AT_PHNUM];
1695                 app.phentsize = aux[AT_PHENT];
1696                 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
1697                         if (phdr->p_type == PT_PHDR)
1698                                 app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
1699                         else if (phdr->p_type == PT_INTERP)
1700                                 interp_off = (size_t)phdr->p_vaddr;
1701                         else if (phdr->p_type == PT_TLS) {
1702                                 tls_image = phdr->p_vaddr;
1703                                 app.tls.len = phdr->p_filesz;
1704                                 app.tls.size = phdr->p_memsz;
1705                                 app.tls.align = phdr->p_align;
1706                         }
1707                 }
1708                 if (DL_FDPIC) app.loadmap = app_loadmap;
1709                 if (app.tls.size) app.tls.image = laddr(&app, tls_image);
1710                 if (interp_off) ldso.name = laddr(&app, interp_off);
1711                 if ((aux[0] & (1UL<<AT_EXECFN))
1712                     && strncmp((char *)aux[AT_EXECFN], "/proc/", 6))
1713                         app.name = (char *)aux[AT_EXECFN];
1714                 else
1715                         app.name = argv[0];
1716                 kernel_mapped_dso(&app);
1717         } else {
1718                 int fd;
1719                 char *ldname = argv[0];
1720                 size_t l = strlen(ldname);
1721                 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
1722                 argv++;
1723                 while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') {
1724                         char *opt = argv[0]+2;
1725                         *argv++ = (void *)-1;
1726                         if (!*opt) {
1727                                 break;
1728                         } else if (!memcmp(opt, "list", 5)) {
1729                                 ldd_mode = 1;
1730                         } else if (!memcmp(opt, "library-path", 12)) {
1731                                 if (opt[12]=='=') env_path = opt+13;
1732                                 else if (opt[12]) *argv = 0;
1733                                 else if (*argv) env_path = *argv++;
1734                         } else if (!memcmp(opt, "preload", 7)) {
1735                                 if (opt[7]=='=') env_preload = opt+8;
1736                                 else if (opt[7]) *argv = 0;
1737                                 else if (*argv) env_preload = *argv++;
1738                         } else if (!memcmp(opt, "argv0", 5)) {
1739                                 if (opt[5]=='=') replace_argv0 = opt+6;
1740                                 else if (opt[5]) *argv = 0;
1741                                 else if (*argv) replace_argv0 = *argv++;
1742                         } else {
1743                                 argv[0] = 0;
1744                         }
1745                 }
1746                 argv[-1] = (void *)(argc - (argv-argv_orig));
1747                 if (!argv[0]) {
1748                         dprintf(2, "musl libc (" LDSO_ARCH ")\n"
1749                                 "Version %s\n"
1750                                 "Dynamic Program Loader\n"
1751                                 "Usage: %s [options] [--] pathname%s\n",
1752                                 __libc_version, ldname,
1753                                 ldd_mode ? "" : " [args]");
1754                         _exit(1);
1755                 }
1756                 fd = open(argv[0], O_RDONLY);
1757                 if (fd < 0) {
1758                         dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
1759                         _exit(1);
1760                 }
1761                 Ehdr *ehdr = (void *)map_library(fd, &app);
1762                 if (!ehdr) {
1763                         dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
1764                         _exit(1);
1765                 }
1766                 close(fd);
1767                 ldso.name = ldname;
1768                 app.name = argv[0];
1769                 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry);
1770                 /* Find the name that would have been used for the dynamic
1771                  * linker had ldd not taken its place. */
1772                 if (ldd_mode) {
1773                         for (i=0; i<app.phnum; i++) {
1774                                 if (app.phdr[i].p_type == PT_INTERP)
1775                                         ldso.name = laddr(&app, app.phdr[i].p_vaddr);
1776                         }
1777                         dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base);
1778                 }
1779         }
1780         if (app.tls.size) {
1781                 libc.tls_head = tls_tail = &app.tls;
1782                 app.tls_id = tls_cnt = 1;
1783 #ifdef TLS_ABOVE_TP
1784                 app.tls.offset = GAP_ABOVE_TP;
1785                 app.tls.offset += -GAP_ABOVE_TP & (app.tls.align-1);
1786                 tls_offset = app.tls.offset + app.tls.size
1787                         + ( -((uintptr_t)app.tls.image + app.tls.size)
1788                         & (app.tls.align-1) );
1789 #else
1790                 tls_offset = app.tls.offset = app.tls.size
1791                         + ( -((uintptr_t)app.tls.image + app.tls.size)
1792                         & (app.tls.align-1) );
1793 #endif
1794                 tls_align = MAXP2(tls_align, app.tls.align);
1795         }
1796         decode_dyn(&app);
1797         if (DL_FDPIC) {
1798                 makefuncdescs(&app);
1799                 if (!app.loadmap) {
1800                         app.loadmap = (void *)&app_dummy_loadmap;
1801                         app.loadmap->nsegs = 1;
1802                         app.loadmap->segs[0].addr = (size_t)app.map;
1803                         app.loadmap->segs[0].p_vaddr = (size_t)app.map
1804                                 - (size_t)app.base;
1805                         app.loadmap->segs[0].p_memsz = app.map_len;
1806                 }
1807                 argv[-3] = (void *)app.loadmap;
1808         }
1809
1810         /* Initial dso chain consists only of the app. */
1811         head = tail = syms_tail = &app;
1812
1813         /* Donate unused parts of app and library mapping to malloc */
1814         reclaim_gaps(&app);
1815         reclaim_gaps(&ldso);
1816
1817         /* Load preload/needed libraries, add symbols to global namespace. */
1818         if (env_preload) load_preload(env_preload);
1819         load_deps(&app);
1820         for (struct dso *p=head; p; p=p->next)
1821                 add_syms(p);
1822
1823         /* Attach to vdso, if provided by the kernel, last so that it does
1824          * not become part of the global namespace.  */
1825         if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR) && vdso_base) {
1826                 Ehdr *ehdr = (void *)vdso_base;
1827                 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff);
1828                 vdso.phnum = ehdr->e_phnum;
1829                 vdso.phentsize = ehdr->e_phentsize;
1830                 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
1831                         if (phdr->p_type == PT_DYNAMIC)
1832                                 vdso.dynv = (void *)(vdso_base + phdr->p_offset);
1833                         if (phdr->p_type == PT_LOAD)
1834                                 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
1835                 }
1836                 vdso.name = "";
1837                 vdso.shortname = "linux-gate.so.1";
1838                 vdso.relocated = 1;
1839                 decode_dyn(&vdso);
1840                 vdso.prev = tail;
1841                 tail->next = &vdso;
1842                 tail = &vdso;
1843         }
1844
1845         for (i=0; app.dynv[i]; i+=2) {
1846                 if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG)
1847                         app.dynv[i+1] = (size_t)&debug;
1848                 if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) {
1849                         size_t *ptr = (size_t *) app.dynv[i+1];
1850                         *ptr = (size_t)&debug;
1851                 }
1852         }
1853
1854         /* The main program must be relocated LAST since it may contin
1855          * copy relocations which depend on libraries' relocations. */
1856         reloc_all(app.next);
1857         reloc_all(&app);
1858
1859         update_tls_size();
1860         if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) {
1861                 void *initial_tls = calloc(libc.tls_size, 1);
1862                 if (!initial_tls) {
1863                         dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
1864                                 argv[0], libc.tls_size);
1865                         _exit(127);
1866                 }
1867                 if (__init_tp(__copy_tls(initial_tls)) < 0) {
1868                         a_crash();
1869                 }
1870         } else {
1871                 size_t tmp_tls_size = libc.tls_size;
1872                 pthread_t self = __pthread_self();
1873                 /* Temporarily set the tls size to the full size of
1874                  * builtin_tls so that __copy_tls will use the same layout
1875                  * as it did for before. Then check, just to be safe. */
1876                 libc.tls_size = sizeof builtin_tls;
1877                 if (__copy_tls((void*)builtin_tls) != self) a_crash();
1878                 libc.tls_size = tmp_tls_size;
1879         }
1880         static_tls_cnt = tls_cnt;
1881
1882         main_ctor_queue = queue_ctors(&app);
1883
1884         if (ldso_fail) _exit(127);
1885         if (ldd_mode) _exit(0);
1886
1887         /* Determine if malloc was interposed by a replacement implementation
1888          * so that calloc and the memalign family can harden against the
1889          * possibility of incomplete replacement. */
1890         if (find_sym(head, "malloc", 1).dso != &ldso)
1891                 __malloc_replaced = 1;
1892
1893         /* Switch to runtime mode: any further failures in the dynamic
1894          * linker are a reportable failure rather than a fatal startup
1895          * error. */
1896         runtime = 1;
1897
1898         debug.ver = 1;
1899         debug.bp = dl_debug_state;
1900         debug.head = head;
1901         debug.base = ldso.base;
1902         debug.state = 0;
1903         _dl_debug_state();
1904
1905         if (replace_argv0) argv[0] = replace_argv0;
1906
1907         errno = 0;
1908
1909         CRTJMP((void *)aux[AT_ENTRY], argv-1);
1910         for(;;);
1911 }
1912
1913 static void prepare_lazy(struct dso *p)
1914 {
1915         size_t dyn[DYN_CNT], n, flags1=0;
1916         decode_vec(p->dynv, dyn, DYN_CNT);
1917         search_vec(p->dynv, &flags1, DT_FLAGS_1);
1918         if (dyn[DT_BIND_NOW] || (dyn[DT_FLAGS] & DF_BIND_NOW) || (flags1 & DF_1_NOW))
1919                 return;
1920         n = dyn[DT_RELSZ]/2 + dyn[DT_RELASZ]/3 + dyn[DT_PLTRELSZ]/2 + 1;
1921         if (NEED_MIPS_GOT_RELOCS) {
1922                 size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1923                 size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1924                 n += i-j;
1925         }
1926         p->lazy = calloc(n, 3*sizeof(size_t));
1927         if (!p->lazy) {
1928                 error("Error preparing lazy relocation for %s: %m", p->name);
1929                 longjmp(*rtld_fail, 1);
1930         }
1931         p->lazy_next = lazy_head;
1932         lazy_head = p;
1933 }
1934
1935 void *dlopen(const char *file, int mode)
1936 {
1937         struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next;
1938         struct tls_module *orig_tls_tail;
1939         size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
1940         size_t i;
1941         int cs;
1942         jmp_buf jb;
1943         struct dso **volatile ctor_queue = 0;
1944
1945         if (!file) return head;
1946
1947         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
1948         pthread_rwlock_wrlock(&lock);
1949         __inhibit_ptc();
1950
1951         p = 0;
1952         if (shutting_down) {
1953                 error("Cannot dlopen while program is exiting.");
1954                 goto end;
1955         }
1956         orig_tls_tail = tls_tail;
1957         orig_tls_cnt = tls_cnt;
1958         orig_tls_offset = tls_offset;
1959         orig_tls_align = tls_align;
1960         orig_lazy_head = lazy_head;
1961         orig_syms_tail = syms_tail;
1962         orig_tail = tail;
1963         noload = mode & RTLD_NOLOAD;
1964
1965         rtld_fail = &jb;
1966         if (setjmp(*rtld_fail)) {
1967                 /* Clean up anything new that was (partially) loaded */
1968                 revert_syms(orig_syms_tail);
1969                 for (p=orig_tail->next; p; p=next) {
1970                         next = p->next;
1971                         while (p->td_index) {
1972                                 void *tmp = p->td_index->next;
1973                                 free(p->td_index);
1974                                 p->td_index = tmp;
1975                         }
1976                         free(p->funcdescs);
1977                         if (p->rpath != p->rpath_orig)
1978                                 free(p->rpath);
1979                         free(p->deps);
1980                         unmap_library(p);
1981                         free(p);
1982                         free(ctor_queue);
1983                 }
1984                 if (!orig_tls_tail) libc.tls_head = 0;
1985                 tls_tail = orig_tls_tail;
1986                 if (tls_tail) tls_tail->next = 0;
1987                 tls_cnt = orig_tls_cnt;
1988                 tls_offset = orig_tls_offset;
1989                 tls_align = orig_tls_align;
1990                 lazy_head = orig_lazy_head;
1991                 tail = orig_tail;
1992                 tail->next = 0;
1993                 p = 0;
1994                 goto end;
1995         } else p = load_library(file, head);
1996
1997         if (!p) {
1998                 error(noload ?
1999                         "Library %s is not already loaded" :
2000                         "Error loading shared library %s: %m",
2001                         file);
2002                 goto end;
2003         }
2004
2005         /* First load handling */
2006         load_deps(p);
2007         extend_bfs_deps(p);
2008         pthread_mutex_lock(&init_fini_lock);
2009         if (!p->constructed) ctor_queue = queue_ctors(p);
2010         pthread_mutex_unlock(&init_fini_lock);
2011         if (!p->relocated && (mode & RTLD_LAZY)) {
2012                 prepare_lazy(p);
2013                 for (i=0; p->deps[i]; i++)
2014                         if (!p->deps[i]->relocated)
2015                                 prepare_lazy(p->deps[i]);
2016         }
2017         if (!p->relocated || (mode & RTLD_GLOBAL)) {
2018                 /* Make new symbols global, at least temporarily, so we can do
2019                  * relocations. If not RTLD_GLOBAL, this is reverted below. */
2020                 add_syms(p);
2021                 for (i=0; p->deps[i]; i++)
2022                         add_syms(p->deps[i]);
2023         }
2024         if (!p->relocated) {
2025                 reloc_all(p);
2026         }
2027
2028         /* If RTLD_GLOBAL was not specified, undo any new additions
2029          * to the global symbol table. This is a nop if the library was
2030          * previously loaded and already global. */
2031         if (!(mode & RTLD_GLOBAL))
2032                 revert_syms(orig_syms_tail);
2033
2034         /* Processing of deferred lazy relocations must not happen until
2035          * the new libraries are committed; otherwise we could end up with
2036          * relocations resolved to symbol definitions that get removed. */
2037         redo_lazy_relocs();
2038
2039         update_tls_size();
2040         if (tls_cnt != orig_tls_cnt)
2041                 install_new_tls();
2042         _dl_debug_state();
2043         orig_tail = tail;
2044 end:
2045         __release_ptc();
2046         if (p) gencnt++;
2047         pthread_rwlock_unlock(&lock);
2048         if (ctor_queue) {
2049                 do_init_fini(ctor_queue);
2050                 free(ctor_queue);
2051         }
2052         pthread_setcancelstate(cs, 0);
2053         return p;
2054 }
2055
2056 hidden int __dl_invalid_handle(void *h)
2057 {
2058         struct dso *p;
2059         for (p=head; p; p=p->next) if (h==p) return 0;
2060         error("Invalid library handle %p", (void *)h);
2061         return 1;
2062 }
2063
2064 static void *addr2dso(size_t a)
2065 {
2066         struct dso *p;
2067         size_t i;
2068         if (DL_FDPIC) for (p=head; p; p=p->next) {
2069                 i = count_syms(p);
2070                 if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs))
2071                         return p;
2072         }
2073         for (p=head; p; p=p->next) {
2074                 if (DL_FDPIC && p->loadmap) {
2075                         for (i=0; i<p->loadmap->nsegs; i++) {
2076                                 if (a-p->loadmap->segs[i].p_vaddr
2077                                     < p->loadmap->segs[i].p_memsz)
2078                                         return p;
2079                         }
2080                 } else {
2081                         Phdr *ph = p->phdr;
2082                         size_t phcnt = p->phnum;
2083                         size_t entsz = p->phentsize;
2084                         size_t base = (size_t)p->base;
2085                         for (; phcnt--; ph=(void *)((char *)ph+entsz)) {
2086                                 if (ph->p_type != PT_LOAD) continue;
2087                                 if (a-base-ph->p_vaddr < ph->p_memsz)
2088                                         return p;
2089                         }
2090                         if (a-(size_t)p->map < p->map_len)
2091                                 return 0;
2092                 }
2093         }
2094         return 0;
2095 }
2096
2097 static void *do_dlsym(struct dso *p, const char *s, void *ra)
2098 {
2099         size_t i;
2100         uint32_t h = 0, gh = 0, *ght;
2101         Sym *sym;
2102         if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
2103                 if (p == RTLD_DEFAULT) {
2104                         p = head;
2105                 } else if (p == RTLD_NEXT) {
2106                         p = addr2dso((size_t)ra);
2107                         if (!p) p=head;
2108                         p = p->next;
2109                 }
2110                 struct symdef def = find_sym(p, s, 0);
2111                 if (!def.sym) goto failed;
2112                 if ((def.sym->st_info&0xf) == STT_TLS)
2113                         return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET});
2114                 if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC)
2115                         return def.dso->funcdescs + (def.sym - def.dso->syms);
2116                 return laddr(def.dso, def.sym->st_value);
2117         }
2118         if (__dl_invalid_handle(p))
2119                 return 0;
2120         if ((ght = p->ghashtab)) {
2121                 gh = gnu_hash(s);
2122                 sym = gnu_lookup(gh, ght, p, s);
2123         } else {
2124                 h = sysv_hash(s);
2125                 sym = sysv_lookup(s, h, p);
2126         }
2127         if (sym && (sym->st_info&0xf) == STT_TLS)
2128                 return __tls_get_addr((tls_mod_off_t []){p->tls_id, sym->st_value-DTP_OFFSET});
2129         if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == STT_FUNC)
2130                 return p->funcdescs + (sym - p->syms);
2131         if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
2132                 return laddr(p, sym->st_value);
2133         for (i=0; p->deps[i]; i++) {
2134                 if ((ght = p->deps[i]->ghashtab)) {
2135                         if (!gh) gh = gnu_hash(s);
2136                         sym = gnu_lookup(gh, ght, p->deps[i], s);
2137                 } else {
2138                         if (!h) h = sysv_hash(s);
2139                         sym = sysv_lookup(s, h, p->deps[i]);
2140                 }
2141                 if (sym && (sym->st_info&0xf) == STT_TLS)
2142                         return __tls_get_addr((tls_mod_off_t []){p->deps[i]->tls_id, sym->st_value-DTP_OFFSET});
2143                 if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == STT_FUNC)
2144                         return p->deps[i]->funcdescs + (sym - p->deps[i]->syms);
2145                 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
2146                         return laddr(p->deps[i], sym->st_value);
2147         }
2148 failed:
2149         error("Symbol not found: %s", s);
2150         return 0;
2151 }
2152
2153 int dladdr(const void *addr_arg, Dl_info *info)
2154 {
2155         size_t addr = (size_t)addr_arg;
2156         struct dso *p;
2157         Sym *sym, *bestsym;
2158         uint32_t nsym;
2159         char *strings;
2160         size_t best = 0;
2161         size_t besterr = -1;
2162
2163         pthread_rwlock_rdlock(&lock);
2164         p = addr2dso(addr);
2165         pthread_rwlock_unlock(&lock);
2166
2167         if (!p) return 0;
2168
2169         sym = p->syms;
2170         strings = p->strings;
2171         nsym = count_syms(p);
2172
2173         if (DL_FDPIC) {
2174                 size_t idx = (addr-(size_t)p->funcdescs)
2175                         / sizeof(*p->funcdescs);
2176                 if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) {
2177                         best = (size_t)(p->funcdescs + idx);
2178                         bestsym = sym + idx;
2179                         besterr = 0;
2180                 }
2181         }
2182
2183         if (!best) for (; nsym; nsym--, sym++) {
2184                 if (sym->st_value
2185                  && (1<<(sym->st_info&0xf) & OK_TYPES)
2186                  && (1<<(sym->st_info>>4) & OK_BINDS)) {
2187                         size_t symaddr = (size_t)laddr(p, sym->st_value);
2188                         if (symaddr > addr || symaddr <= best)
2189                                 continue;
2190                         best = symaddr;
2191                         bestsym = sym;
2192                         besterr = addr - symaddr;
2193                         if (addr == symaddr)
2194                                 break;
2195                 }
2196         }
2197
2198         if (bestsym && besterr > bestsym->st_size-1) {
2199                 best = 0;
2200                 bestsym = 0;
2201         }
2202
2203         info->dli_fname = p->name;
2204         info->dli_fbase = p->map;
2205
2206         if (!best) {
2207                 info->dli_sname = 0;
2208                 info->dli_saddr = 0;
2209                 return 1;
2210         }
2211
2212         if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC)
2213                 best = (size_t)(p->funcdescs + (bestsym - p->syms));
2214         info->dli_sname = strings + bestsym->st_name;
2215         info->dli_saddr = (void *)best;
2216
2217         return 1;
2218 }
2219
2220 hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
2221 {
2222         void *res;
2223         pthread_rwlock_rdlock(&lock);
2224         res = do_dlsym(p, s, ra);
2225         pthread_rwlock_unlock(&lock);
2226         return res;
2227 }
2228
2229 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
2230 {
2231         struct dso *current;
2232         struct dl_phdr_info info;
2233         int ret = 0;
2234         for(current = head; current;) {
2235                 info.dlpi_addr      = (uintptr_t)current->base;
2236                 info.dlpi_name      = current->name;
2237                 info.dlpi_phdr      = current->phdr;
2238                 info.dlpi_phnum     = current->phnum;
2239                 info.dlpi_adds      = gencnt;
2240                 info.dlpi_subs      = 0;
2241                 info.dlpi_tls_modid = current->tls_id;
2242                 info.dlpi_tls_data  = current->tls.image;
2243
2244                 ret = (callback)(&info, sizeof (info), data);
2245
2246                 if (ret != 0) break;
2247
2248                 pthread_rwlock_rdlock(&lock);
2249                 current = current->next;
2250                 pthread_rwlock_unlock(&lock);
2251         }
2252         return ret;
2253 }
2254
2255 static void error(const char *fmt, ...)
2256 {
2257         va_list ap;
2258         va_start(ap, fmt);
2259         if (!runtime) {
2260                 vdprintf(2, fmt, ap);
2261                 dprintf(2, "\n");
2262                 ldso_fail = 1;
2263                 va_end(ap);
2264                 return;
2265         }
2266         __dl_vseterr(fmt, ap);
2267         va_end(ap);
2268 }