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