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