20 #include <semaphore.h>
21 #include <sys/membarrier.h>
22 #include "pthread_impl.h"
25 #include "malloc_impl.h"
27 static void error(const char *, ...);
29 #define MAXP2(a,b) (-(-(a)&-(b)))
30 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
32 #define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
44 struct td_index *next;
49 struct fdpic_loadmap *loadmap;
55 struct dso *next, *prev;
65 struct dso *syms_next, *lazy_next;
66 size_t *lazy, lazy_cnt;
77 struct dso **deps, *needed_by;
81 char *rpath_orig, *rpath;
82 struct tls_module tls;
84 size_t relro_start, relro_end;
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;
94 struct fdpic_loadmap *loadmap;
109 static struct builtin_tls {
114 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
116 #define ADDEND_LIMIT 4096
117 static size_t *saved_addends, *apply_addends_to;
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;
125 static int ldso_fail;
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 **main_ctor_queue;
137 static struct fdpic_loadmap *app_loadmap;
138 static struct fdpic_dummy_loadmap app_dummy_loadmap;
140 struct debug *_dl_debug_addr = &debug;
142 extern hidden int __malloc_replaced;
144 hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0;
146 extern hidden void (*const __init_array_end)(void), (*const __fini_array_end)(void);
148 weak_alias(__init_array_start, __init_array_end);
149 weak_alias(__fini_array_start, __fini_array_end);
151 static int dl_strcmp(const char *l, const char *r)
153 for (; *l==*r && *l; l++, r++);
154 return *(unsigned char *)l - *(unsigned char *)r;
156 #define strcmp(l,r) dl_strcmp(l,r)
158 /* Compute load address for a virtual address in a given dso. */
160 static void *laddr(const struct dso *p, size_t v)
163 if (!p->loadmap) return p->base + v;
164 for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++);
165 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
167 static void *laddr_pg(const struct dso *p, size_t v)
170 size_t pgsz = PAGE_SIZE;
171 if (!p->loadmap) return p->base + v;
173 size_t a = p->loadmap->segs[j].p_vaddr;
174 size_t b = a + p->loadmap->segs[j].p_memsz;
180 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
182 #define fpaddr(p, v) ((void (*)())&(struct funcdesc){ \
183 laddr(p, v), (p)->got })
185 #define laddr(p, v) (void *)((p)->base + (v))
186 #define laddr_pg(p, v) laddr(p, v)
187 #define fpaddr(p, v) ((void (*)())laddr(p, v))
190 static void decode_vec(size_t *v, size_t *a, size_t cnt)
193 for (i=0; i<cnt; i++) a[i] = 0;
194 for (; v[0]; v+=2) if (v[0]-1<cnt-1) {
200 static int search_vec(size_t *v, size_t *r, size_t key)
202 for (; v[0]!=key; v+=2)
208 static uint32_t sysv_hash(const char *s0)
210 const unsigned char *s = (void *)s0;
216 return h & 0xfffffff;
219 static uint32_t gnu_hash(const char *s0)
221 const unsigned char *s = (void *)s0;
222 uint_fast32_t h = 5381;
228 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
231 Sym *syms = dso->syms;
232 Elf_Symndx *hashtab = dso->hashtab;
233 char *strings = dso->strings;
234 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
235 if ((!dso->versym || dso->versym[i] >= 0)
236 && (!strcmp(s, strings+syms[i].st_name)))
242 static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s)
244 uint32_t nbuckets = hashtab[0];
245 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
246 uint32_t i = buckets[h1 % nbuckets];
250 uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
252 for (h1 |= 1; ; i++) {
253 uint32_t h2 = *hashval++;
254 if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0)
255 && !strcmp(s, dso->strings + dso->syms[i].st_name))
263 static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask)
265 const size_t *bloomwords = (const void *)(hashtab+4);
266 size_t f = bloomwords[fofs & (hashtab[2]-1)];
267 if (!(f & fmask)) return 0;
269 f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
270 if (!(f & 1)) return 0;
272 return gnu_lookup(h1, hashtab, dso, s);
275 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
276 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
278 #ifndef ARCH_SYM_REJECT_UND
279 #define ARCH_SYM_REJECT_UND(s) 0
282 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
284 uint32_t h = 0, gh = gnu_hash(s), gho = gh / (8*sizeof(size_t)), *ght;
285 size_t ghm = 1ul << gh % (8*sizeof(size_t));
286 struct symdef def = {0};
287 for (; dso; dso=dso->syms_next) {
289 if ((ght = dso->ghashtab)) {
290 sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
292 if (!h) h = sysv_hash(s);
293 sym = sysv_lookup(s, h, dso);
297 if (need_def || (sym->st_info&0xf) == STT_TLS
298 || ARCH_SYM_REJECT_UND(sym))
301 if ((sym->st_info&0xf) != STT_TLS)
303 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
304 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
312 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
314 unsigned char *base = dso->base;
315 Sym *syms = dso->syms;
316 char *strings = dso->strings;
327 int skip_relative = 0, reuse_addends = 0, save_slot = 0;
330 /* Only ldso's REL table needs addend saving/reuse. */
331 if (rel == apply_addends_to)
336 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
337 if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue;
338 type = R_TYPE(rel[1]);
339 if (type == REL_NONE) continue;
340 reloc_addr = laddr(dso, rel[0]);
344 } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) {
346 } else if (reuse_addends) {
347 /* Save original addend in stage 2 where the dso
348 * chain consists of just ldso; otherwise read back
349 * saved addend since the inline one was clobbered. */
351 saved_addends[save_slot] = *reloc_addr;
352 addend = saved_addends[save_slot++];
354 addend = *reloc_addr;
357 sym_index = R_SYM(rel[1]);
359 sym = syms + sym_index;
360 name = strings + sym->st_name;
361 ctx = type==REL_COPY ? head->syms_next : head;
362 def = (sym->st_info&0xf) == STT_SECTION
363 ? (struct symdef){ .dso = dso, .sym = sym }
364 : find_sym(ctx, name, type==REL_PLT);
365 if (!def.sym && (sym->st_shndx != SHN_UNDEF
366 || sym->st_info>>4 != STB_WEAK)) {
367 if (dso->lazy && (type==REL_PLT || type==REL_GOT)) {
368 dso->lazy[3*dso->lazy_cnt+0] = rel[0];
369 dso->lazy[3*dso->lazy_cnt+1] = rel[1];
370 dso->lazy[3*dso->lazy_cnt+2] = addend;
374 error("Error relocating %s: %s: symbol not found",
376 if (runtime) longjmp(*rtld_fail, 1);
385 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0;
386 tls_val = def.sym ? def.sym->st_value : 0;
388 if ((type == REL_TPOFF || type == REL_TPOFF_NEG)
389 && runtime && def.dso->tls_id > static_tls_cnt) {
390 error("Error relocating %s: %s: initial-exec TLS "
391 "resolves to dynamic definition in %s",
392 dso->name, name, def.dso->name);
393 longjmp(*rtld_fail, 1);
400 addend -= (size_t)reloc_addr;
404 *reloc_addr = sym_val + addend;
407 *reloc_addr = (size_t)base + addend;
410 if (sym) *reloc_addr = sym_val + addend;
411 else *reloc_addr = (size_t)base + addend;
414 memcpy(reloc_addr, (void *)sym_val, sym->st_size);
417 *(uint32_t *)reloc_addr = sym_val + addend
418 - (size_t)reloc_addr;
421 *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs
422 + (def.sym - def.dso->syms)) : 0;
424 case REL_FUNCDESC_VAL:
425 if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val;
426 else *reloc_addr = sym_val;
427 reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0;
430 *reloc_addr = def.dso->tls_id;
433 *reloc_addr = tls_val + addend - DTP_OFFSET;
437 *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend;
441 *reloc_addr = tls_val - def.dso->tls.offset + addend;
444 *reloc_addr = def.dso->tls.offset - tls_val + addend;
448 if (stride<3) addend = reloc_addr[1];
449 if (runtime && def.dso->tls_id > static_tls_cnt) {
450 struct td_index *new = malloc(sizeof *new);
453 "Error relocating %s: cannot allocate TLSDESC for %s",
454 dso->name, sym ? name : "(local)" );
455 longjmp(*rtld_fail, 1);
457 new->next = dso->td_index;
459 new->args[0] = def.dso->tls_id;
460 new->args[1] = tls_val + addend - DTP_OFFSET;
461 reloc_addr[0] = (size_t)__tlsdesc_dynamic;
462 reloc_addr[1] = (size_t)new;
464 reloc_addr[0] = (size_t)__tlsdesc_static;
466 reloc_addr[1] = tls_val + def.dso->tls.offset
469 reloc_addr[1] = tls_val - def.dso->tls.offset
473 #ifdef TLSDESC_BACKWARDS
474 /* Some archs (32-bit ARM at least) invert the order of
475 * the descriptor members. Fix them up here. */
476 size_t tmp = reloc_addr[0];
477 reloc_addr[0] = reloc_addr[1];
482 error("Error relocating %s: unsupported relocation type %d",
484 if (runtime) longjmp(*rtld_fail, 1);
490 static void redo_lazy_relocs()
492 struct dso *p = lazy_head, *next;
496 size_t size = p->lazy_cnt*3*sizeof(size_t);
498 do_relocs(p, p->lazy, size, 3);
500 p->lazy_next = lazy_head;
510 /* A huge hack: to make up for the wastefulness of shared libraries
511 * needing at least a page of dirty memory even if they have no global
512 * data, we reclaim the gaps at the beginning and end of writable maps
513 * and "donate" them to the heap. */
515 static void reclaim(struct dso *dso, size_t start, size_t end)
517 if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end;
518 if (end >= dso->relro_start && end < dso->relro_end) end = dso->relro_start;
519 if (start >= end) return;
520 char *base = laddr_pg(dso, start);
521 __malloc_donate(base, base+(end-start));
524 static void reclaim_gaps(struct dso *dso)
526 Phdr *ph = dso->phdr;
527 size_t phcnt = dso->phnum;
529 for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) {
530 if (ph->p_type!=PT_LOAD) continue;
531 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
532 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
533 reclaim(dso, ph->p_vaddr+ph->p_memsz,
534 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
538 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
540 static int no_map_fixed;
543 q = mmap(p, n, prot, flags|MAP_FIXED, fd, off);
544 if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL)
548 /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
549 if (flags & MAP_ANONYMOUS) {
554 if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
555 for (q=p; n; q+=r, off+=r, n-=r) {
557 if (r < 0 && errno != EINTR) return MAP_FAILED;
566 static void unmap_library(struct dso *dso)
570 for (i=0; i<dso->loadmap->nsegs; i++) {
571 if (!dso->loadmap->segs[i].p_memsz)
573 munmap((void *)dso->loadmap->segs[i].addr,
574 dso->loadmap->segs[i].p_memsz);
577 } else if (dso->map && dso->map_len) {
578 munmap(dso->map, dso->map_len);
582 static void *map_library(int fd, struct dso *dso)
584 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
585 void *allocated_buf=0;
587 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
588 size_t this_min, this_max;
594 unsigned char *map=MAP_FAILED, *base;
599 ssize_t l = read(fd, buf, sizeof buf);
602 if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC))
604 phsize = eh->e_phentsize * eh->e_phnum;
605 if (phsize > sizeof buf - sizeof *eh) {
606 allocated_buf = malloc(phsize);
607 if (!allocated_buf) return 0;
608 l = pread(fd, allocated_buf, phsize, eh->e_phoff);
609 if (l < 0) goto error;
610 if (l != phsize) goto noexec;
611 ph = ph0 = allocated_buf;
612 } else if (eh->e_phoff + phsize > l) {
613 l = pread(fd, buf+1, phsize, eh->e_phoff);
614 if (l < 0) goto error;
615 if (l != phsize) goto noexec;
616 ph = ph0 = (void *)(buf + 1);
618 ph = ph0 = (void *)((char *)buf + eh->e_phoff);
620 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
621 if (ph->p_type == PT_DYNAMIC) {
623 } else if (ph->p_type == PT_TLS) {
624 tls_image = ph->p_vaddr;
625 dso->tls.align = ph->p_align;
626 dso->tls.len = ph->p_filesz;
627 dso->tls.size = ph->p_memsz;
628 } else if (ph->p_type == PT_GNU_RELRO) {
629 dso->relro_start = ph->p_vaddr & -PAGE_SIZE;
630 dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
631 } else if (ph->p_type == PT_GNU_STACK) {
632 if (!runtime && ph->p_memsz > __default_stacksize) {
633 __default_stacksize =
634 ph->p_memsz < DEFAULT_STACK_MAX ?
635 ph->p_memsz : DEFAULT_STACK_MAX;
638 if (ph->p_type != PT_LOAD) continue;
640 if (ph->p_vaddr < addr_min) {
641 addr_min = ph->p_vaddr;
642 off_start = ph->p_offset;
643 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
644 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
645 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
647 if (ph->p_vaddr+ph->p_memsz > addr_max) {
648 addr_max = ph->p_vaddr+ph->p_memsz;
651 if (!dyn) goto noexec;
652 if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
653 dso->loadmap = calloc(1, sizeof *dso->loadmap
654 + nsegs * sizeof *dso->loadmap->segs);
655 if (!dso->loadmap) goto error;
656 dso->loadmap->nsegs = nsegs;
657 for (ph=ph0, i=0; i<nsegs; ph=(void *)((char *)ph+eh->e_phentsize)) {
658 if (ph->p_type != PT_LOAD) continue;
659 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
660 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
661 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
662 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1),
664 fd, ph->p_offset & -PAGE_SIZE);
665 if (map == MAP_FAILED) {
669 dso->loadmap->segs[i].addr = (size_t)map +
670 (ph->p_vaddr & PAGE_SIZE-1);
671 dso->loadmap->segs[i].p_vaddr = ph->p_vaddr;
672 dso->loadmap->segs[i].p_memsz = ph->p_memsz;
674 if (prot & PROT_WRITE) {
675 size_t brk = (ph->p_vaddr & PAGE_SIZE-1)
677 size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE;
678 size_t pgend = brk + ph->p_memsz - ph->p_filesz
679 + PAGE_SIZE-1 & -PAGE_SIZE;
680 if (pgend > pgbrk && mmap_fixed(map+pgbrk,
682 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,
683 -1, off_start) == MAP_FAILED)
685 memset(map + brk, 0, pgbrk-brk);
688 map = (void *)dso->loadmap->segs[0].addr;
692 addr_max += PAGE_SIZE-1;
693 addr_max &= -PAGE_SIZE;
694 addr_min &= -PAGE_SIZE;
695 off_start &= -PAGE_SIZE;
696 map_len = addr_max - addr_min + off_start;
697 /* The first time, we map too much, possibly even more than
698 * the length of the file. This is okay because we will not
699 * use the invalid part; we just need to reserve the right
700 * amount of virtual address space to map over later. */
701 map = DL_NOMMU_SUPPORT
702 ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC,
703 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
704 : mmap((void *)addr_min, map_len, prot,
705 MAP_PRIVATE, fd, off_start);
706 if (map==MAP_FAILED) goto error;
708 dso->map_len = map_len;
709 /* If the loaded file is not relocatable and the requested address is
710 * not available, then the load operation must fail. */
711 if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) {
715 base = map - addr_min;
718 for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
719 if (ph->p_type != PT_LOAD) continue;
720 /* Check if the programs headers are in this load segment, and
721 * if so, record the address for use by dl_iterate_phdr. */
722 if (!dso->phdr && eh->e_phoff >= ph->p_offset
723 && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) {
724 dso->phdr = (void *)(base + ph->p_vaddr
725 + (eh->e_phoff-ph->p_offset));
726 dso->phnum = eh->e_phnum;
727 dso->phentsize = eh->e_phentsize;
729 this_min = ph->p_vaddr & -PAGE_SIZE;
730 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
731 off_start = ph->p_offset & -PAGE_SIZE;
732 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
733 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
734 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
735 /* Reuse the existing mapping for the lowest-address LOAD */
736 if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT)
737 if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
739 if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) {
740 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
741 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
742 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
743 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)
747 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
748 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
749 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
756 dso->dynv = laddr(dso, dyn);
757 if (dso->tls.size) dso->tls.image = laddr(dso, tls_image);
763 if (map!=MAP_FAILED) unmap_library(dso);
768 static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
773 s += strspn(s, ":\n");
774 l = strcspn(s, ":\n");
775 if (l-1 >= INT_MAX) return -1;
776 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) {
777 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
785 /* Any negative value but -1 will inhibit
786 * futher path search. */
794 static int fixup_rpath(struct dso *p, char *buf, size_t buf_size)
797 const char *s, *t, *origin;
799 if (p->rpath || !p->rpath_orig) return 0;
800 if (!strchr(p->rpath_orig, '$')) {
801 p->rpath = p->rpath_orig;
806 while ((t=strchr(s, '$'))) {
807 if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9))
812 if (n > SSIZE_MAX/PATH_MAX) return 0;
814 if (p->kernel_mapped) {
815 /* $ORIGIN searches cannot be performed for the main program
816 * when it is suid/sgid/AT_SECURE. This is because the
817 * pathname is under the control of the caller of execve.
818 * For libraries, however, $ORIGIN can be processed safely
819 * since the library's pathname came from a trusted source
820 * (either system paths or a call to dlopen). */
823 l = readlink("/proc/self/exe", buf, buf_size);
824 if (l == -1) switch (errno) {
839 t = strrchr(origin, '/');
843 /* Normally p->name will always be an absolute or relative
844 * pathname containing at least one '/' character, but in the
845 * case where ldso was invoked as a command to execute a
846 * program in the working directory, app.name may not. Fix. */
850 /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
851 if (libc.secure && *origin != '/')
853 p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1);
854 if (!p->rpath) return -1;
858 while ((t=strchr(s, '$'))) {
861 memcpy(d, origin, l);
863 /* It was determined previously that the '$' is followed
864 * either by "ORIGIN" or "{ORIGIN}". */
865 s = t + 7 + 2*(t[1]=='{');
871 static void decode_dyn(struct dso *p)
874 decode_vec(p->dynv, dyn, DYN_CNT);
875 p->syms = laddr(p, dyn[DT_SYMTAB]);
876 p->strings = laddr(p, dyn[DT_STRTAB]);
877 if (dyn[0]&(1<<DT_HASH))
878 p->hashtab = laddr(p, dyn[DT_HASH]);
879 if (dyn[0]&(1<<DT_RPATH))
880 p->rpath_orig = p->strings + dyn[DT_RPATH];
881 if (dyn[0]&(1<<DT_RUNPATH))
882 p->rpath_orig = p->strings + dyn[DT_RUNPATH];
883 if (dyn[0]&(1<<DT_PLTGOT))
884 p->got = laddr(p, dyn[DT_PLTGOT]);
885 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
886 p->ghashtab = laddr(p, *dyn);
887 if (search_vec(p->dynv, dyn, DT_VERSYM))
888 p->versym = laddr(p, *dyn);
891 static size_t count_syms(struct dso *p)
893 if (p->hashtab) return p->hashtab[1];
896 uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
898 for (i = nsym = 0; i < p->ghashtab[0]; i++) {
899 if (buckets[i] > nsym)
903 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]);
905 while (!(*hashval++ & 1));
910 static void *dl_mmap(size_t n)
913 int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE;
915 p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0);
917 p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0);
919 return p == MAP_FAILED ? 0 : p;
922 static void makefuncdescs(struct dso *p)
924 static int self_done;
925 size_t nsym = count_syms(p);
926 size_t i, size = nsym * sizeof(*p->funcdescs);
929 p->funcdescs = dl_mmap(size);
932 p->funcdescs = malloc(size);
935 if (!runtime) a_crash();
936 error("Error allocating function descriptors for %s", p->name);
937 longjmp(*rtld_fail, 1);
939 for (i=0; i<nsym; i++) {
940 if ((p->syms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) {
941 p->funcdescs[i].addr = laddr(p, p->syms[i].st_value);
942 p->funcdescs[i].got = p->got;
944 p->funcdescs[i].addr = 0;
945 p->funcdescs[i].got = 0;
950 static struct dso *load_library(const char *name, struct dso *needed_by)
952 char buf[2*NAME_MAX+2];
953 const char *pathname;
955 struct dso *p, temp_dso = {0};
967 /* Catch and block attempts to reload the implementation itself */
968 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
969 static const char reserved[] =
970 "c.pthread.rt.m.dl.util.xnet.";
971 const char *rp, *next;
972 for (rp=reserved; *rp; rp=next) {
973 next = strchr(rp, '.') + 1;
974 if (strncmp(name+3, rp, next-rp) == 0)
979 /* Track which names have been resolved
980 * and only report each one once. */
981 static unsigned reported;
982 unsigned mask = 1U<<(rp-reserved);
983 if (!(reported & mask)) {
985 dprintf(1, "\t%s => %s (%p)\n",
993 if (!strcmp(name, ldso.name)) is_self = 1;
1002 if (strchr(name, '/')) {
1004 fd = open(name, O_RDONLY|O_CLOEXEC);
1006 /* Search for the name to see if it's already loaded */
1007 for (p=head->next; p; p=p->next) {
1008 if (p->shortname && !strcmp(p->shortname, name)) {
1012 if (strlen(name) > NAME_MAX) return 0;
1014 if (env_path) fd = path_open(name, env_path, buf, sizeof buf);
1015 for (p=needed_by; fd == -1 && p; p=p->needed_by) {
1016 if (fixup_rpath(p, buf, sizeof buf) < 0)
1017 fd = -2; /* Inhibit further search. */
1019 fd = path_open(name, p->rpath, buf, sizeof buf);
1025 if (ldso.name[0]=='/') {
1027 for (s=t=z=ldso.name; *s; s++)
1028 if (*s=='/') z=t, t=s;
1029 prefix_len = z-ldso.name;
1030 if (prefix_len < PATH_MAX)
1037 char etc_ldso_path[prefix_len + 1
1038 + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
1039 snprintf(etc_ldso_path, sizeof etc_ldso_path,
1040 "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
1041 (int)prefix_len, prefix);
1042 FILE *f = fopen(etc_ldso_path, "rbe");
1044 if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
1049 } else if (errno != ENOENT) {
1053 if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
1054 fd = path_open(name, sys_path, buf, sizeof buf);
1058 if (fd < 0) return 0;
1059 if (fstat(fd, &st) < 0) {
1063 for (p=head->next; p; p=p->next) {
1064 if (p->dev == st.st_dev && p->ino == st.st_ino) {
1065 /* If this library was previously loaded with a
1066 * pathname but a search found the same inode,
1067 * setup its shortname so it can be found by name. */
1068 if (!p->shortname && pathname != name)
1069 p->shortname = strrchr(p->name, '/')+1;
1074 map = noload ? 0 : map_library(fd, &temp_dso);
1078 /* Avoid the danger of getting two versions of libc mapped into the
1079 * same process when an absolute pathname was used. The symbols
1080 * checked are chosen to catch both musl and glibc, and to avoid
1081 * false positives from interposition-hack libraries. */
1082 decode_dyn(&temp_dso);
1083 if (find_sym(&temp_dso, "__libc_start_main", 1).sym &&
1084 find_sym(&temp_dso, "stdin", 1).sym) {
1085 unmap_library(&temp_dso);
1086 return load_library("libc.so", needed_by);
1088 /* Past this point, if we haven't reached runtime yet, ldso has
1089 * committed either to use the mapped library or to abort execution.
1090 * Unmapping is not possible, so we can safely reclaim gaps. */
1091 if (!runtime) reclaim_gaps(&temp_dso);
1093 /* Allocate storage for the new DSO. When there is TLS, this
1094 * storage must include a reservation for all pre-existing
1095 * threads to obtain copies of both the new TLS, and an
1096 * extended DTV capable of storing an additional slot for
1097 * the newly-loaded DSO. */
1098 alloc_size = sizeof *p + strlen(pathname) + 1;
1099 if (runtime && temp_dso.tls.image) {
1100 size_t per_th = temp_dso.tls.size + temp_dso.tls.align
1101 + sizeof(void *) * (tls_cnt+3);
1102 n_th = libc.threads_minus_1 + 1;
1103 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
1104 else alloc_size += n_th * per_th;
1106 p = calloc(1, alloc_size);
1108 unmap_library(&temp_dso);
1111 memcpy(p, &temp_dso, sizeof temp_dso);
1114 p->needed_by = needed_by;
1116 p->runtime_loaded = runtime;
1117 strcpy(p->name, pathname);
1118 /* Add a shortname only if name arg was not an explicit pathname. */
1119 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
1121 p->tls_id = ++tls_cnt;
1122 tls_align = MAXP2(tls_align, p->tls.align);
1124 p->tls.offset = tls_offset + ( (tls_align-1) &
1125 -(tls_offset + (uintptr_t)p->tls.image) );
1126 tls_offset += p->tls.size;
1128 tls_offset += p->tls.size + p->tls.align - 1;
1129 tls_offset -= (tls_offset + (uintptr_t)p->tls.image)
1131 p->tls.offset = tls_offset;
1133 p->new_dtv = (void *)(-sizeof(size_t) &
1134 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
1135 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
1136 if (tls_tail) tls_tail->next = &p->tls;
1137 else libc.tls_head = &p->tls;
1145 if (DL_FDPIC) makefuncdescs(p);
1147 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
1152 static void load_direct_deps(struct dso *p)
1155 if (p->deps) return;
1156 /* For head, all preloads are direct pseudo-dependencies.
1157 * Count and include them now to avoid realloc later. */
1158 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1160 for (i=0; p->dynv[i]; i+=2)
1161 if (p->dynv[i] == DT_NEEDED) cnt++;
1162 p->deps = calloc(cnt+1, sizeof *p->deps);
1164 error("Error loading dependencies for %s", p->name);
1165 if (runtime) longjmp(*rtld_fail, 1);
1168 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1170 for (i=0; p->dynv[i]; i+=2) {
1171 if (p->dynv[i] != DT_NEEDED) continue;
1172 struct dso *dep = load_library(p->strings + p->dynv[i+1], p);
1174 error("Error loading shared library %s: %m (needed by %s)",
1175 p->strings + p->dynv[i+1], p->name);
1176 if (runtime) longjmp(*rtld_fail, 1);
1179 p->deps[cnt++] = dep;
1182 p->ndeps_direct = cnt;
1185 static void load_deps(struct dso *p)
1187 if (p->deps) return;
1188 for (; p; p=p->next)
1189 load_direct_deps(p);
1192 static void extend_bfs_deps(struct dso *p)
1194 size_t i, j, cnt, ndeps_all;
1197 /* Can't use realloc if the original p->deps was allocated at
1198 * program entry and malloc has been replaced. */
1199 int no_realloc = __malloc_replaced && !p->runtime_loaded;
1201 if (p->bfs_built) return;
1202 ndeps_all = p->ndeps_direct;
1204 /* Mark existing (direct) deps so they won't be duplicated. */
1205 for (i=0; p->deps[i]; i++)
1206 p->deps[i]->mark = 1;
1208 /* For each dependency already in the list, copy its list of direct
1209 * dependencies to the list, excluding any items already in the
1210 * list. Note that the list this loop iterates over will grow during
1211 * the loop, but since duplicates are excluded, growth is bounded. */
1212 for (i=0; p->deps[i]; i++) {
1213 struct dso *dep = p->deps[i];
1214 for (j=cnt=0; j<dep->ndeps_direct; j++)
1215 if (!dep->deps[j]->mark) cnt++;
1217 malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) :
1218 realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1));
1220 error("Error recording dependencies for %s", p->name);
1221 if (runtime) longjmp(*rtld_fail, 1);
1225 memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1));
1229 for (j=0; j<dep->ndeps_direct; j++) {
1230 if (dep->deps[j]->mark) continue;
1231 dep->deps[j]->mark = 1;
1232 p->deps[ndeps_all++] = dep->deps[j];
1234 p->deps[ndeps_all] = 0;
1237 for (p=head; p; p=p->next)
1241 static void load_preload(char *s)
1245 for (z=s; *z; s=z) {
1246 for ( ; *s && (isspace(*s) || *s==':'); s++);
1247 for (z=s; *z && !isspace(*z) && *z!=':'; z++);
1255 static void add_syms(struct dso *p)
1257 if (!p->syms_next && syms_tail != p) {
1258 syms_tail->syms_next = p;
1263 static void revert_syms(struct dso *old_tail)
1265 struct dso *p, *next;
1266 /* Chop off the tail of the list of dsos that participate in
1267 * the global symbol table, reverting them to RTLD_LOCAL. */
1268 for (p=old_tail; p; p=next) {
1269 next = p->syms_next;
1272 syms_tail = old_tail;
1275 static void do_mips_relocs(struct dso *p, size_t *got)
1277 size_t i, j, rel[2];
1278 unsigned char *base = p->base;
1279 i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO);
1283 while (i--) *got++ += (size_t)base;
1285 j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1286 i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1287 Sym *sym = p->syms + j;
1288 rel[0] = (unsigned char *)got - base;
1289 for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) {
1290 rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT);
1291 do_relocs(p, rel, sizeof rel, 2);
1295 static void reloc_all(struct dso *p)
1297 size_t dyn[DYN_CNT];
1298 for (; p; p=p->next) {
1299 if (p->relocated) continue;
1300 decode_vec(p->dynv, dyn, DYN_CNT);
1301 if (NEED_MIPS_GOT_RELOCS)
1302 do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT]));
1303 do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
1304 2+(dyn[DT_PLTREL]==DT_RELA));
1305 do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
1306 do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
1308 if (head != &ldso && p->relro_start != p->relro_end &&
1309 mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
1310 && errno != ENOSYS) {
1311 error("Error relocating %s: RELRO protection failed: %m",
1313 if (runtime) longjmp(*rtld_fail, 1);
1320 static void kernel_mapped_dso(struct dso *p)
1322 size_t min_addr = -1, max_addr = 0, cnt;
1324 for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) {
1325 if (ph->p_type == PT_DYNAMIC) {
1326 p->dynv = laddr(p, ph->p_vaddr);
1327 } else if (ph->p_type == PT_GNU_RELRO) {
1328 p->relro_start = ph->p_vaddr & -PAGE_SIZE;
1329 p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
1330 } else if (ph->p_type == PT_GNU_STACK) {
1331 if (!runtime && ph->p_memsz > __default_stacksize) {
1332 __default_stacksize =
1333 ph->p_memsz < DEFAULT_STACK_MAX ?
1334 ph->p_memsz : DEFAULT_STACK_MAX;
1337 if (ph->p_type != PT_LOAD) continue;
1338 if (ph->p_vaddr < min_addr)
1339 min_addr = ph->p_vaddr;
1340 if (ph->p_vaddr+ph->p_memsz > max_addr)
1341 max_addr = ph->p_vaddr+ph->p_memsz;
1343 min_addr &= -PAGE_SIZE;
1344 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
1345 p->map = p->base + min_addr;
1346 p->map_len = max_addr - min_addr;
1347 p->kernel_mapped = 1;
1350 void __libc_exit_fini()
1353 size_t dyn[DYN_CNT];
1354 int self = __pthread_self()->tid;
1356 /* Take both locks before setting shutting_down, so that
1357 * either lock is sufficient to read its value. The lock
1358 * order matches that in dlopen to avoid deadlock. */
1359 pthread_rwlock_wrlock(&lock);
1360 pthread_mutex_lock(&init_fini_lock);
1362 pthread_rwlock_unlock(&lock);
1363 for (p=fini_head; p; p=p->fini_next) {
1364 while (p->ctor_visitor && p->ctor_visitor!=self)
1365 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1366 if (!p->constructed) continue;
1367 decode_vec(p->dynv, dyn, DYN_CNT);
1368 if (dyn[0] & (1<<DT_FINI_ARRAY)) {
1369 size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
1370 size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY])+n;
1371 while (n--) ((void (*)(void))*--fn)();
1373 #ifndef NO_LEGACY_INITFINI
1374 if ((dyn[0] & (1<<DT_FINI)) && dyn[DT_FINI])
1375 fpaddr(p, dyn[DT_FINI])();
1380 static struct dso **queue_ctors(struct dso *dso)
1382 size_t cnt, qpos, spos, i;
1383 struct dso *p, **queue, **stack;
1385 if (ldd_mode) return 0;
1387 /* Bound on queue size is the total number of indirect deps.
1388 * If a bfs deps list was built, we can use it. Otherwise,
1389 * bound by the total number of DSOs, which is always safe and
1390 * is reasonable we use it (for main app at startup). */
1391 if (dso->bfs_built) {
1392 for (cnt=0; dso->deps[cnt]; cnt++)
1393 dso->deps[cnt]->mark = 0;
1394 cnt++; /* self, not included in deps */
1396 for (cnt=0, p=head; p; cnt++, p=p->next)
1399 cnt++; /* termination slot */
1400 stack = queue = calloc(cnt, sizeof *queue);
1403 error("Error allocating constructor queue: %m\n");
1404 if (runtime) longjmp(*rtld_fail, 1);
1408 /* Opposite ends of the allocated buffer serve as an output queue
1409 * and a working stack. Setup initial stack with just the argument
1410 * dso and initial queue empty... */
1413 stack[--spos] = dso;
1417 /* Then perform pseudo-DFS sort, but ignoring circular deps. */
1420 while (p->next_dep < p->ndeps_direct) {
1421 if (p->deps[p->next_dep]->mark) {
1425 p = p->deps[p->next_dep];
1433 for (i=0; i<qpos; i++) queue[i]->mark = 0;
1438 static void do_init_fini(struct dso **queue)
1441 size_t dyn[DYN_CNT], i;
1442 int self = __pthread_self()->tid;
1444 pthread_mutex_lock(&init_fini_lock);
1445 for (i=0; (p=queue[i]); i++) {
1446 while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down)
1447 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1448 if (p->ctor_visitor || p->constructed)
1450 if (p->constructed) continue;
1451 p->ctor_visitor = self;
1453 decode_vec(p->dynv, dyn, DYN_CNT);
1454 if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
1455 p->fini_next = fini_head;
1459 pthread_mutex_unlock(&init_fini_lock);
1461 #ifndef NO_LEGACY_INITFINI
1462 if ((dyn[0] & (1<<DT_INIT)) && dyn[DT_INIT])
1463 fpaddr(p, dyn[DT_INIT])();
1465 if (dyn[0] & (1<<DT_INIT_ARRAY)) {
1466 size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
1467 size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]);
1468 while (n--) ((void (*)(void))*fn++)();
1471 pthread_mutex_lock(&init_fini_lock);
1472 p->ctor_visitor = 0;
1474 pthread_cond_broadcast(&ctor_cond);
1476 pthread_mutex_unlock(&init_fini_lock);
1479 void __libc_start_init(void)
1481 do_init_fini(main_ctor_queue);
1482 /* This is valid because the queue was allocated after redoing
1483 * relocations with any interposed malloc having taken effect. */
1484 free(main_ctor_queue);
1485 main_ctor_queue = 0;
1488 static void dl_debug_state(void)
1492 weak_alias(dl_debug_state, _dl_debug_state);
1494 void __init_tls(size_t *auxv)
1498 static void update_tls_size()
1500 libc.tls_cnt = tls_cnt;
1501 libc.tls_align = tls_align;
1502 libc.tls_size = ALIGN(
1503 (1+tls_cnt) * sizeof(void *) +
1505 sizeof(struct pthread) +
1510 static void install_new_tls(void)
1513 pthread_t self = __pthread_self(), td;
1514 struct dso *dtv_provider = container_of(tls_tail, struct dso, tls);
1515 uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv;
1518 size_t old_cnt = self->dtv[0];
1520 __block_app_sigs(&set);
1522 /* Copy existing dtv contents from all existing threads. */
1523 for (i=0, td=self; !i || td!=self; i++, td=td->next) {
1524 memcpy(newdtv+i, td->dtv,
1525 (old_cnt+1)*sizeof(uintptr_t));
1526 newdtv[i][0] = tls_cnt;
1528 /* Install new dtls into the enlarged, uninstalled dtv copies. */
1529 for (p=head; ; p=p->next) {
1530 if (p->tls_id <= old_cnt) continue;
1531 unsigned char *mem = p->new_tls;
1532 for (j=0; j<i; j++) {
1533 unsigned char *new = mem;
1534 new += ((uintptr_t)p->tls.image - (uintptr_t)mem)
1536 memcpy(new, p->tls.image, p->tls.len);
1537 newdtv[j][p->tls_id] =
1538 (uintptr_t)new + DTP_OFFSET;
1539 mem += p->tls.size + p->tls.align;
1541 if (p->tls_id == tls_cnt) break;
1544 /* Broadcast barrier to ensure contents of new dtv is visible
1545 * if the new dtv pointer is. The __membarrier function has a
1546 * fallback emulation using signals for kernels that lack the
1547 * feature at the syscall level. */
1549 __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
1551 /* Install new dtv for each thread. */
1552 for (j=0, td=self; !j || td!=self; j++, td=td->next) {
1553 td->dtv = td->dtv_copy = newdtv[j];
1557 __restore_sigs(&set);
1560 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1561 * following stage 2 and stage 3 functions via primitive symbolic lookup
1562 * since it does not have access to their addresses to begin with. */
1564 /* Stage 2 of the dynamic linker is called after relative relocations
1565 * have been processed. It can make function calls to static functions
1566 * and access string literals and static data, but cannot use extern
1567 * symbols. Its job is to perform symbolic relocations on the dynamic
1568 * linker itself, but some of the relocations performed may need to be
1569 * replaced later due to copy relocations in the main program. */
1571 hidden void __dls2(unsigned char *base, size_t *sp)
1574 void *p1 = (void *)sp[-2];
1575 void *p2 = (void *)sp[-1];
1577 size_t *auxv, aux[AUX_CNT];
1578 for (auxv=sp+1+*sp+1; *auxv; auxv++);
1580 decode_vec(auxv, aux, AUX_CNT);
1581 if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE];
1582 else ldso.base = (void *)(aux[AT_PHDR] & -4096);
1584 app_loadmap = p2 ? p1 : 0;
1585 ldso.loadmap = p2 ? p2 : p1;
1586 ldso.base = laddr(&ldso, 0);
1590 Ehdr *ehdr = (void *)ldso.base;
1591 ldso.name = ldso.shortname = "libc.so";
1592 ldso.phnum = ehdr->e_phnum;
1593 ldso.phdr = laddr(&ldso, ehdr->e_phoff);
1594 ldso.phentsize = ehdr->e_phentsize;
1595 kernel_mapped_dso(&ldso);
1598 if (DL_FDPIC) makefuncdescs(&ldso);
1600 /* Prepare storage for to save clobbered REL addends so they
1601 * can be reused in stage 3. There should be very few. If
1602 * something goes wrong and there are a huge number, abort
1603 * instead of risking stack overflow. */
1604 size_t dyn[DYN_CNT];
1605 decode_vec(ldso.dynv, dyn, DYN_CNT);
1606 size_t *rel = laddr(&ldso, dyn[DT_REL]);
1607 size_t rel_size = dyn[DT_RELSZ];
1608 size_t symbolic_rel_cnt = 0;
1609 apply_addends_to = rel;
1610 for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t))
1611 if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++;
1612 if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash();
1613 size_t addends[symbolic_rel_cnt+1];
1614 saved_addends = addends;
1621 /* Call dynamic linker stage-2b, __dls2b, looking it up
1622 * symbolically as a barrier against moving the address
1623 * load across the above relocation processing. */
1624 struct symdef dls2b_def = find_sym(&ldso, "__dls2b", 0);
1625 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp);
1626 else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp);
1629 /* Stage 2b sets up a valid thread pointer, which requires relocations
1630 * completed in stage 2, and on which stage 3 is permitted to depend.
1631 * This is done as a separate stage, with symbolic lookup as a barrier,
1632 * so that loads of the thread pointer and &errno can be pure/const and
1633 * thereby hoistable. */
1635 _Noreturn void __dls2b(size_t *sp)
1637 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1638 * use during dynamic linking. If possible it will also serve as the
1639 * thread pointer at runtime. */
1640 libc.tls_size = sizeof builtin_tls;
1641 libc.tls_align = tls_align;
1642 if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) {
1646 struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
1647 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp);
1648 else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp);
1651 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1652 * fully functional. Its job is to load (if not already loaded) and
1653 * process dependencies and relocations for the main application and
1654 * transfer control to its entry point. */
1656 _Noreturn void __dls3(size_t *sp)
1658 static struct dso app, vdso;
1659 size_t aux[AUX_CNT], *auxv;
1661 char *env_preload=0;
1662 char *replace_argv0=0;
1665 char **argv = (void *)(sp+1);
1666 char **argv_orig = argv;
1667 char **envp = argv+argc+1;
1669 /* Find aux vector just past environ[] and use it to initialize
1670 * global data that may be needed before we can make syscalls. */
1672 for (i=argc+1; argv[i]; i++);
1673 libc.auxv = auxv = (void *)(argv+i+1);
1674 decode_vec(auxv, aux, AUX_CNT);
1675 __hwcap = aux[AT_HWCAP];
1676 libc.page_size = aux[AT_PAGESZ];
1677 libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1678 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
1680 /* Only trust user/env if kernel says we're not suid/sgid */
1682 env_path = getenv("LD_LIBRARY_PATH");
1683 env_preload = getenv("LD_PRELOAD");
1686 /* If the main program was already loaded by the kernel,
1687 * AT_PHDR will point to some location other than the dynamic
1688 * linker's program headers. */
1689 if (aux[AT_PHDR] != (size_t)ldso.phdr) {
1690 size_t interp_off = 0;
1691 size_t tls_image = 0;
1692 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1693 Phdr *phdr = app.phdr = (void *)aux[AT_PHDR];
1694 app.phnum = aux[AT_PHNUM];
1695 app.phentsize = aux[AT_PHENT];
1696 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
1697 if (phdr->p_type == PT_PHDR)
1698 app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
1699 else if (phdr->p_type == PT_INTERP)
1700 interp_off = (size_t)phdr->p_vaddr;
1701 else if (phdr->p_type == PT_TLS) {
1702 tls_image = phdr->p_vaddr;
1703 app.tls.len = phdr->p_filesz;
1704 app.tls.size = phdr->p_memsz;
1705 app.tls.align = phdr->p_align;
1708 if (DL_FDPIC) app.loadmap = app_loadmap;
1709 if (app.tls.size) app.tls.image = laddr(&app, tls_image);
1710 if (interp_off) ldso.name = laddr(&app, interp_off);
1711 if ((aux[0] & (1UL<<AT_EXECFN))
1712 && strncmp((char *)aux[AT_EXECFN], "/proc/", 6))
1713 app.name = (char *)aux[AT_EXECFN];
1716 kernel_mapped_dso(&app);
1719 char *ldname = argv[0];
1720 size_t l = strlen(ldname);
1721 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
1723 while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') {
1724 char *opt = argv[0]+2;
1725 *argv++ = (void *)-1;
1728 } else if (!memcmp(opt, "list", 5)) {
1730 } else if (!memcmp(opt, "library-path", 12)) {
1731 if (opt[12]=='=') env_path = opt+13;
1732 else if (opt[12]) *argv = 0;
1733 else if (*argv) env_path = *argv++;
1734 } else if (!memcmp(opt, "preload", 7)) {
1735 if (opt[7]=='=') env_preload = opt+8;
1736 else if (opt[7]) *argv = 0;
1737 else if (*argv) env_preload = *argv++;
1738 } else if (!memcmp(opt, "argv0", 5)) {
1739 if (opt[5]=='=') replace_argv0 = opt+6;
1740 else if (opt[5]) *argv = 0;
1741 else if (*argv) replace_argv0 = *argv++;
1746 argv[-1] = (void *)(argc - (argv-argv_orig));
1748 dprintf(2, "musl libc (" LDSO_ARCH ")\n"
1750 "Dynamic Program Loader\n"
1751 "Usage: %s [options] [--] pathname%s\n",
1752 __libc_version, ldname,
1753 ldd_mode ? "" : " [args]");
1756 fd = open(argv[0], O_RDONLY);
1758 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
1761 Ehdr *ehdr = (void *)map_library(fd, &app);
1763 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
1769 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry);
1770 /* Find the name that would have been used for the dynamic
1771 * linker had ldd not taken its place. */
1773 for (i=0; i<app.phnum; i++) {
1774 if (app.phdr[i].p_type == PT_INTERP)
1775 ldso.name = laddr(&app, app.phdr[i].p_vaddr);
1777 dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base);
1781 libc.tls_head = tls_tail = &app.tls;
1782 app.tls_id = tls_cnt = 1;
1784 app.tls.offset = GAP_ABOVE_TP;
1785 app.tls.offset += -GAP_ABOVE_TP & (app.tls.align-1);
1786 tls_offset = app.tls.offset + app.tls.size
1787 + ( -((uintptr_t)app.tls.image + app.tls.size)
1788 & (app.tls.align-1) );
1790 tls_offset = app.tls.offset = app.tls.size
1791 + ( -((uintptr_t)app.tls.image + app.tls.size)
1792 & (app.tls.align-1) );
1794 tls_align = MAXP2(tls_align, app.tls.align);
1798 makefuncdescs(&app);
1800 app.loadmap = (void *)&app_dummy_loadmap;
1801 app.loadmap->nsegs = 1;
1802 app.loadmap->segs[0].addr = (size_t)app.map;
1803 app.loadmap->segs[0].p_vaddr = (size_t)app.map
1805 app.loadmap->segs[0].p_memsz = app.map_len;
1807 argv[-3] = (void *)app.loadmap;
1810 /* Initial dso chain consists only of the app. */
1811 head = tail = syms_tail = &app;
1813 /* Donate unused parts of app and library mapping to malloc */
1815 reclaim_gaps(&ldso);
1817 /* Load preload/needed libraries, add symbols to global namespace. */
1818 if (env_preload) load_preload(env_preload);
1820 for (struct dso *p=head; p; p=p->next)
1823 /* Attach to vdso, if provided by the kernel, last so that it does
1824 * not become part of the global namespace. */
1825 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR) && vdso_base) {
1826 Ehdr *ehdr = (void *)vdso_base;
1827 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff);
1828 vdso.phnum = ehdr->e_phnum;
1829 vdso.phentsize = ehdr->e_phentsize;
1830 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
1831 if (phdr->p_type == PT_DYNAMIC)
1832 vdso.dynv = (void *)(vdso_base + phdr->p_offset);
1833 if (phdr->p_type == PT_LOAD)
1834 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
1837 vdso.shortname = "linux-gate.so.1";
1845 for (i=0; app.dynv[i]; i+=2) {
1846 if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG)
1847 app.dynv[i+1] = (size_t)&debug;
1848 if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) {
1849 size_t *ptr = (size_t *) app.dynv[i+1];
1850 *ptr = (size_t)&debug;
1854 /* The main program must be relocated LAST since it may contin
1855 * copy relocations which depend on libraries' relocations. */
1856 reloc_all(app.next);
1860 if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) {
1861 void *initial_tls = calloc(libc.tls_size, 1);
1863 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
1864 argv[0], libc.tls_size);
1867 if (__init_tp(__copy_tls(initial_tls)) < 0) {
1871 size_t tmp_tls_size = libc.tls_size;
1872 pthread_t self = __pthread_self();
1873 /* Temporarily set the tls size to the full size of
1874 * builtin_tls so that __copy_tls will use the same layout
1875 * as it did for before. Then check, just to be safe. */
1876 libc.tls_size = sizeof builtin_tls;
1877 if (__copy_tls((void*)builtin_tls) != self) a_crash();
1878 libc.tls_size = tmp_tls_size;
1880 static_tls_cnt = tls_cnt;
1882 main_ctor_queue = queue_ctors(&app);
1884 if (ldso_fail) _exit(127);
1885 if (ldd_mode) _exit(0);
1887 /* Determine if malloc was interposed by a replacement implementation
1888 * so that calloc and the memalign family can harden against the
1889 * possibility of incomplete replacement. */
1890 if (find_sym(head, "malloc", 1).dso != &ldso)
1891 __malloc_replaced = 1;
1893 /* Switch to runtime mode: any further failures in the dynamic
1894 * linker are a reportable failure rather than a fatal startup
1899 debug.bp = dl_debug_state;
1901 debug.base = ldso.base;
1905 if (replace_argv0) argv[0] = replace_argv0;
1909 CRTJMP((void *)aux[AT_ENTRY], argv-1);
1913 static void prepare_lazy(struct dso *p)
1915 size_t dyn[DYN_CNT], n, flags1=0;
1916 decode_vec(p->dynv, dyn, DYN_CNT);
1917 search_vec(p->dynv, &flags1, DT_FLAGS_1);
1918 if (dyn[DT_BIND_NOW] || (dyn[DT_FLAGS] & DF_BIND_NOW) || (flags1 & DF_1_NOW))
1920 n = dyn[DT_RELSZ]/2 + dyn[DT_RELASZ]/3 + dyn[DT_PLTRELSZ]/2 + 1;
1921 if (NEED_MIPS_GOT_RELOCS) {
1922 size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1923 size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1926 p->lazy = calloc(n, 3*sizeof(size_t));
1928 error("Error preparing lazy relocation for %s: %m", p->name);
1929 longjmp(*rtld_fail, 1);
1931 p->lazy_next = lazy_head;
1935 void *dlopen(const char *file, int mode)
1937 struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next;
1938 struct tls_module *orig_tls_tail;
1939 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
1943 struct dso **volatile ctor_queue = 0;
1945 if (!file) return head;
1947 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
1948 pthread_rwlock_wrlock(&lock);
1952 if (shutting_down) {
1953 error("Cannot dlopen while program is exiting.");
1956 orig_tls_tail = tls_tail;
1957 orig_tls_cnt = tls_cnt;
1958 orig_tls_offset = tls_offset;
1959 orig_tls_align = tls_align;
1960 orig_lazy_head = lazy_head;
1961 orig_syms_tail = syms_tail;
1963 noload = mode & RTLD_NOLOAD;
1966 if (setjmp(*rtld_fail)) {
1967 /* Clean up anything new that was (partially) loaded */
1968 revert_syms(orig_syms_tail);
1969 for (p=orig_tail->next; p; p=next) {
1971 while (p->td_index) {
1972 void *tmp = p->td_index->next;
1977 if (p->rpath != p->rpath_orig)
1984 if (!orig_tls_tail) libc.tls_head = 0;
1985 tls_tail = orig_tls_tail;
1986 if (tls_tail) tls_tail->next = 0;
1987 tls_cnt = orig_tls_cnt;
1988 tls_offset = orig_tls_offset;
1989 tls_align = orig_tls_align;
1990 lazy_head = orig_lazy_head;
1995 } else p = load_library(file, head);
1999 "Library %s is not already loaded" :
2000 "Error loading shared library %s: %m",
2005 /* First load handling */
2008 pthread_mutex_lock(&init_fini_lock);
2009 if (!p->constructed) ctor_queue = queue_ctors(p);
2010 pthread_mutex_unlock(&init_fini_lock);
2011 if (!p->relocated && (mode & RTLD_LAZY)) {
2013 for (i=0; p->deps[i]; i++)
2014 if (!p->deps[i]->relocated)
2015 prepare_lazy(p->deps[i]);
2017 if (!p->relocated || (mode & RTLD_GLOBAL)) {
2018 /* Make new symbols global, at least temporarily, so we can do
2019 * relocations. If not RTLD_GLOBAL, this is reverted below. */
2021 for (i=0; p->deps[i]; i++)
2022 add_syms(p->deps[i]);
2024 if (!p->relocated) {
2028 /* If RTLD_GLOBAL was not specified, undo any new additions
2029 * to the global symbol table. This is a nop if the library was
2030 * previously loaded and already global. */
2031 if (!(mode & RTLD_GLOBAL))
2032 revert_syms(orig_syms_tail);
2034 /* Processing of deferred lazy relocations must not happen until
2035 * the new libraries are committed; otherwise we could end up with
2036 * relocations resolved to symbol definitions that get removed. */
2040 if (tls_cnt != orig_tls_cnt)
2047 pthread_rwlock_unlock(&lock);
2049 do_init_fini(ctor_queue);
2052 pthread_setcancelstate(cs, 0);
2056 hidden int __dl_invalid_handle(void *h)
2059 for (p=head; p; p=p->next) if (h==p) return 0;
2060 error("Invalid library handle %p", (void *)h);
2064 static void *addr2dso(size_t a)
2068 if (DL_FDPIC) for (p=head; p; p=p->next) {
2070 if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs))
2073 for (p=head; p; p=p->next) {
2074 if (DL_FDPIC && p->loadmap) {
2075 for (i=0; i<p->loadmap->nsegs; i++) {
2076 if (a-p->loadmap->segs[i].p_vaddr
2077 < p->loadmap->segs[i].p_memsz)
2082 size_t phcnt = p->phnum;
2083 size_t entsz = p->phentsize;
2084 size_t base = (size_t)p->base;
2085 for (; phcnt--; ph=(void *)((char *)ph+entsz)) {
2086 if (ph->p_type != PT_LOAD) continue;
2087 if (a-base-ph->p_vaddr < ph->p_memsz)
2090 if (a-(size_t)p->map < p->map_len)
2097 static void *do_dlsym(struct dso *p, const char *s, void *ra)
2100 uint32_t h = 0, gh = 0, *ght;
2102 if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
2103 if (p == RTLD_DEFAULT) {
2105 } else if (p == RTLD_NEXT) {
2106 p = addr2dso((size_t)ra);
2110 struct symdef def = find_sym(p, s, 0);
2111 if (!def.sym) goto failed;
2112 if ((def.sym->st_info&0xf) == STT_TLS)
2113 return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET});
2114 if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC)
2115 return def.dso->funcdescs + (def.sym - def.dso->syms);
2116 return laddr(def.dso, def.sym->st_value);
2118 if (__dl_invalid_handle(p))
2120 if ((ght = p->ghashtab)) {
2122 sym = gnu_lookup(gh, ght, p, s);
2125 sym = sysv_lookup(s, h, p);
2127 if (sym && (sym->st_info&0xf) == STT_TLS)
2128 return __tls_get_addr((tls_mod_off_t []){p->tls_id, sym->st_value-DTP_OFFSET});
2129 if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == STT_FUNC)
2130 return p->funcdescs + (sym - p->syms);
2131 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
2132 return laddr(p, sym->st_value);
2133 for (i=0; p->deps[i]; i++) {
2134 if ((ght = p->deps[i]->ghashtab)) {
2135 if (!gh) gh = gnu_hash(s);
2136 sym = gnu_lookup(gh, ght, p->deps[i], s);
2138 if (!h) h = sysv_hash(s);
2139 sym = sysv_lookup(s, h, p->deps[i]);
2141 if (sym && (sym->st_info&0xf) == STT_TLS)
2142 return __tls_get_addr((tls_mod_off_t []){p->deps[i]->tls_id, sym->st_value-DTP_OFFSET});
2143 if (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == STT_FUNC)
2144 return p->deps[i]->funcdescs + (sym - p->deps[i]->syms);
2145 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
2146 return laddr(p->deps[i], sym->st_value);
2149 error("Symbol not found: %s", s);
2153 int dladdr(const void *addr_arg, Dl_info *info)
2155 size_t addr = (size_t)addr_arg;
2161 size_t besterr = -1;
2163 pthread_rwlock_rdlock(&lock);
2165 pthread_rwlock_unlock(&lock);
2170 strings = p->strings;
2171 nsym = count_syms(p);
2174 size_t idx = (addr-(size_t)p->funcdescs)
2175 / sizeof(*p->funcdescs);
2176 if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) {
2177 best = (size_t)(p->funcdescs + idx);
2178 bestsym = sym + idx;
2183 if (!best) for (; nsym; nsym--, sym++) {
2185 && (1<<(sym->st_info&0xf) & OK_TYPES)
2186 && (1<<(sym->st_info>>4) & OK_BINDS)) {
2187 size_t symaddr = (size_t)laddr(p, sym->st_value);
2188 if (symaddr > addr || symaddr <= best)
2192 besterr = addr - symaddr;
2193 if (addr == symaddr)
2198 if (bestsym && besterr > bestsym->st_size-1) {
2203 info->dli_fname = p->name;
2204 info->dli_fbase = p->map;
2207 info->dli_sname = 0;
2208 info->dli_saddr = 0;
2212 if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC)
2213 best = (size_t)(p->funcdescs + (bestsym - p->syms));
2214 info->dli_sname = strings + bestsym->st_name;
2215 info->dli_saddr = (void *)best;
2220 hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
2223 pthread_rwlock_rdlock(&lock);
2224 res = do_dlsym(p, s, ra);
2225 pthread_rwlock_unlock(&lock);
2229 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
2231 struct dso *current;
2232 struct dl_phdr_info info;
2234 for(current = head; current;) {
2235 info.dlpi_addr = (uintptr_t)current->base;
2236 info.dlpi_name = current->name;
2237 info.dlpi_phdr = current->phdr;
2238 info.dlpi_phnum = current->phnum;
2239 info.dlpi_adds = gencnt;
2241 info.dlpi_tls_modid = current->tls_id;
2242 info.dlpi_tls_data = current->tls.image;
2244 ret = (callback)(&info, sizeof (info), data);
2246 if (ret != 0) break;
2248 pthread_rwlock_rdlock(&lock);
2249 current = current->next;
2250 pthread_rwlock_unlock(&lock);
2255 static void error(const char *fmt, ...)
2260 vdprintf(2, fmt, ap);
2266 __dl_vseterr(fmt, ap);