OSDN Git Service

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