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