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