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