2 #define SYSCALL_NO_TLS 1
20 #include <semaphore.h>
21 #include <sys/membarrier.h>
22 #include "pthread_impl.h"
26 #define malloc __libc_malloc
27 #define calloc __libc_calloc
28 #define realloc __libc_realloc
29 #define free __libc_free
31 static void error(const char *, ...);
33 #define MAXP2(a,b) (-(-(a)&-(b)))
34 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
36 #define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
37 #define countof(a) ((sizeof (a))/(sizeof (a)[0]))
49 struct td_index *next;
54 struct fdpic_loadmap *loadmap;
60 struct dso *next, *prev;
70 struct dso *syms_next, *lazy_next;
71 size_t *lazy, lazy_cnt;
82 struct dso **deps, *needed_by;
85 pthread_t ctor_visitor;
86 char *rpath_orig, *rpath;
87 struct tls_module tls;
89 size_t relro_start, relro_end;
91 unsigned char *new_tls;
92 struct td_index *td_index;
93 struct dso *fini_next;
98 struct fdpic_loadmap *loadmap;
113 typedef void (*stage3_func)(size_t *, size_t *);
115 static struct builtin_tls {
120 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
122 #define ADDEND_LIMIT 4096
123 static size_t *saved_addends, *apply_addends_to;
125 static struct dso ldso;
126 static struct dso *head, *tail, *fini_head, *syms_tail, *lazy_head;
127 static char *env_path, *sys_path;
128 static unsigned long long gencnt;
131 static int ldso_fail;
133 static int shutting_down;
134 static jmp_buf *rtld_fail;
135 static pthread_rwlock_t lock;
136 static struct debug debug;
137 static struct tls_module *tls_tail;
138 static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN;
139 static size_t static_tls_cnt;
140 static pthread_mutex_t init_fini_lock;
141 static pthread_cond_t ctor_cond;
142 static struct dso *builtin_deps[2];
143 static struct dso *const no_deps[1];
144 static struct dso *builtin_ctor_queue[4];
145 static struct dso **main_ctor_queue;
146 static struct fdpic_loadmap *app_loadmap;
147 static struct fdpic_dummy_loadmap app_dummy_loadmap;
149 struct debug *_dl_debug_addr = &debug;
151 extern hidden int __malloc_replaced;
153 hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0;
155 extern hidden void (*const __init_array_end)(void), (*const __fini_array_end)(void);
157 weak_alias(__init_array_start, __init_array_end);
158 weak_alias(__fini_array_start, __fini_array_end);
160 static int dl_strcmp(const char *l, const char *r)
162 for (; *l==*r && *l; l++, r++);
163 return *(unsigned char *)l - *(unsigned char *)r;
165 #define strcmp(l,r) dl_strcmp(l,r)
167 /* Compute load address for a virtual address in a given dso. */
169 static void *laddr(const struct dso *p, size_t v)
172 if (!p->loadmap) return p->base + v;
173 for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++);
174 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
176 static void *laddr_pg(const struct dso *p, size_t v)
179 size_t pgsz = PAGE_SIZE;
180 if (!p->loadmap) return p->base + v;
182 size_t a = p->loadmap->segs[j].p_vaddr;
183 size_t b = a + p->loadmap->segs[j].p_memsz;
189 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
191 static void (*fdbarrier(void *p))()
194 __asm__("" : "=r"(fd) : "0"(p));
197 #define fpaddr(p, v) fdbarrier((&(struct funcdesc){ \
198 laddr(p, v), (p)->got }))
200 #define laddr(p, v) (void *)((p)->base + (v))
201 #define laddr_pg(p, v) laddr(p, v)
202 #define fpaddr(p, v) ((void (*)())laddr(p, v))
205 static void decode_vec(size_t *v, size_t *a, size_t cnt)
208 for (i=0; i<cnt; i++) a[i] = 0;
209 for (; v[0]; v+=2) if (v[0]-1<cnt-1) {
215 static int search_vec(size_t *v, size_t *r, size_t key)
217 for (; v[0]!=key; v+=2)
223 static uint32_t sysv_hash(const char *s0)
225 const unsigned char *s = (void *)s0;
231 return h & 0xfffffff;
234 static uint32_t gnu_hash(const char *s0)
236 const unsigned char *s = (void *)s0;
237 uint_fast32_t h = 5381;
243 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
246 Sym *syms = dso->syms;
247 Elf_Symndx *hashtab = dso->hashtab;
248 char *strings = dso->strings;
249 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
250 if ((!dso->versym || dso->versym[i] >= 0)
251 && (!strcmp(s, strings+syms[i].st_name)))
257 static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s)
259 uint32_t nbuckets = hashtab[0];
260 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
261 uint32_t i = buckets[h1 % nbuckets];
265 uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
267 for (h1 |= 1; ; i++) {
268 uint32_t h2 = *hashval++;
269 if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0)
270 && !strcmp(s, dso->strings + dso->syms[i].st_name))
278 static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask)
280 const size_t *bloomwords = (const void *)(hashtab+4);
281 size_t f = bloomwords[fofs & (hashtab[2]-1)];
282 if (!(f & fmask)) return 0;
284 f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
285 if (!(f & 1)) return 0;
287 return gnu_lookup(h1, hashtab, dso, s);
290 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
291 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
293 #ifndef ARCH_SYM_REJECT_UND
294 #define ARCH_SYM_REJECT_UND(s) 0
297 #if defined(__GNUC__)
298 __attribute__((always_inline))
300 static inline struct symdef find_sym2(struct dso *dso, const char *s, int need_def, int use_deps)
302 uint32_t h = 0, gh = gnu_hash(s), gho = gh / (8*sizeof(size_t)), *ght;
303 size_t ghm = 1ul << gh % (8*sizeof(size_t));
304 struct symdef def = {0};
305 struct dso **deps = use_deps ? dso->deps : 0;
306 for (; dso; dso=use_deps ? *deps++ : dso->syms_next) {
308 if ((ght = dso->ghashtab)) {
309 sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
311 if (!h) h = sysv_hash(s);
312 sym = sysv_lookup(s, h, dso);
316 if (need_def || (sym->st_info&0xf) == STT_TLS
317 || ARCH_SYM_REJECT_UND(sym))
320 if ((sym->st_info&0xf) != STT_TLS)
322 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
323 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
331 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
333 return find_sym2(dso, s, need_def, 0);
336 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
338 unsigned char *base = dso->base;
339 Sym *syms = dso->syms;
340 char *strings = dso->strings;
351 int skip_relative = 0, reuse_addends = 0, save_slot = 0;
354 /* Only ldso's REL table needs addend saving/reuse. */
355 if (rel == apply_addends_to)
360 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
361 if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue;
362 type = R_TYPE(rel[1]);
363 if (type == REL_NONE) continue;
364 reloc_addr = laddr(dso, rel[0]);
368 } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) {
370 } else if (reuse_addends) {
371 /* Save original addend in stage 2 where the dso
372 * chain consists of just ldso; otherwise read back
373 * saved addend since the inline one was clobbered. */
375 saved_addends[save_slot] = *reloc_addr;
376 addend = saved_addends[save_slot++];
378 addend = *reloc_addr;
381 sym_index = R_SYM(rel[1]);
383 sym = syms + sym_index;
384 name = strings + sym->st_name;
385 ctx = type==REL_COPY ? head->syms_next : head;
386 def = (sym->st_info>>4) == STB_LOCAL
387 ? (struct symdef){ .dso = dso, .sym = sym }
388 : find_sym(ctx, name, type==REL_PLT);
389 if (!def.sym && (sym->st_shndx != SHN_UNDEF
390 || sym->st_info>>4 != STB_WEAK)) {
391 if (dso->lazy && (type==REL_PLT || type==REL_GOT)) {
392 dso->lazy[3*dso->lazy_cnt+0] = rel[0];
393 dso->lazy[3*dso->lazy_cnt+1] = rel[1];
394 dso->lazy[3*dso->lazy_cnt+2] = addend;
398 error("Error relocating %s: %s: symbol not found",
400 if (runtime) longjmp(*rtld_fail, 1);
409 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0;
410 tls_val = def.sym ? def.sym->st_value : 0;
412 if ((type == REL_TPOFF || type == REL_TPOFF_NEG)
413 && def.dso->tls_id > static_tls_cnt) {
414 error("Error relocating %s: %s: initial-exec TLS "
415 "resolves to dynamic definition in %s",
416 dso->name, name, def.dso->name);
417 longjmp(*rtld_fail, 1);
422 addend -= (size_t)reloc_addr;
426 *reloc_addr = sym_val + addend;
429 memcpy(reloc_addr, &(size_t){sym_val + addend}, sizeof(size_t));
432 *reloc_addr = (size_t)base + addend;
435 if (sym) *reloc_addr = sym_val + addend;
436 else *reloc_addr = (size_t)base + addend;
439 memcpy(reloc_addr, (void *)sym_val, sym->st_size);
442 *(uint32_t *)reloc_addr = sym_val + addend
443 - (size_t)reloc_addr;
446 *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs
447 + (def.sym - def.dso->syms)) : 0;
449 case REL_FUNCDESC_VAL:
450 if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val;
451 else *reloc_addr = sym_val;
452 reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0;
455 *reloc_addr = def.dso->tls_id;
458 *reloc_addr = tls_val + addend - DTP_OFFSET;
462 *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend;
466 *reloc_addr = tls_val - def.dso->tls.offset + addend;
469 *reloc_addr = def.dso->tls.offset - tls_val + addend;
473 if (stride<3) addend = reloc_addr[1];
474 if (def.dso->tls_id > static_tls_cnt) {
475 struct td_index *new = malloc(sizeof *new);
478 "Error relocating %s: cannot allocate TLSDESC for %s",
479 dso->name, sym ? name : "(local)" );
480 longjmp(*rtld_fail, 1);
482 new->next = dso->td_index;
484 new->args[0] = def.dso->tls_id;
485 new->args[1] = tls_val + addend - DTP_OFFSET;
486 reloc_addr[0] = (size_t)__tlsdesc_dynamic;
487 reloc_addr[1] = (size_t)new;
489 reloc_addr[0] = (size_t)__tlsdesc_static;
491 reloc_addr[1] = tls_val + def.dso->tls.offset
494 reloc_addr[1] = tls_val - def.dso->tls.offset
498 #ifdef TLSDESC_BACKWARDS
499 /* Some archs (32-bit ARM at least) invert the order of
500 * the descriptor members. Fix them up here. */
501 size_t tmp = reloc_addr[0];
502 reloc_addr[0] = reloc_addr[1];
507 error("Error relocating %s: unsupported relocation type %d",
509 if (runtime) longjmp(*rtld_fail, 1);
515 static void redo_lazy_relocs()
517 struct dso *p = lazy_head, *next;
521 size_t size = p->lazy_cnt*3*sizeof(size_t);
523 do_relocs(p, p->lazy, size, 3);
525 p->lazy_next = lazy_head;
535 /* A huge hack: to make up for the wastefulness of shared libraries
536 * needing at least a page of dirty memory even if they have no global
537 * data, we reclaim the gaps at the beginning and end of writable maps
538 * and "donate" them to the heap. */
540 static void reclaim(struct dso *dso, size_t start, size_t end)
542 if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end;
543 if (end >= dso->relro_start && end < dso->relro_end) end = dso->relro_start;
544 if (start >= end) return;
545 char *base = laddr_pg(dso, start);
546 __malloc_donate(base, base+(end-start));
549 static void reclaim_gaps(struct dso *dso)
551 Phdr *ph = dso->phdr;
552 size_t phcnt = dso->phnum;
554 for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) {
555 if (ph->p_type!=PT_LOAD) continue;
556 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
557 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
558 reclaim(dso, ph->p_vaddr+ph->p_memsz,
559 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
563 static ssize_t read_loop(int fd, void *p, size_t n)
565 for (size_t i=0; i<n; ) {
566 ssize_t l = read(fd, (char *)p+i, n-i);
568 if (errno==EINTR) continue;
577 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
579 static int no_map_fixed;
582 q = mmap(p, n, prot, flags|MAP_FIXED, fd, off);
583 if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL)
587 /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
588 if (flags & MAP_ANONYMOUS) {
593 if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
594 for (q=p; n; q+=r, off+=r, n-=r) {
596 if (r < 0 && errno != EINTR) return MAP_FAILED;
605 static void unmap_library(struct dso *dso)
609 for (i=0; i<dso->loadmap->nsegs; i++) {
610 if (!dso->loadmap->segs[i].p_memsz)
612 munmap((void *)dso->loadmap->segs[i].addr,
613 dso->loadmap->segs[i].p_memsz);
616 } else if (dso->map && dso->map_len) {
617 munmap(dso->map, dso->map_len);
621 static void *map_library(int fd, struct dso *dso)
623 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
624 void *allocated_buf=0;
626 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
627 size_t this_min, this_max;
633 unsigned char *map=MAP_FAILED, *base;
638 ssize_t l = read(fd, buf, sizeof buf);
641 if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC))
643 phsize = eh->e_phentsize * eh->e_phnum;
644 if (phsize > sizeof buf - sizeof *eh) {
645 allocated_buf = malloc(phsize);
646 if (!allocated_buf) return 0;
647 l = pread(fd, allocated_buf, phsize, eh->e_phoff);
648 if (l < 0) goto error;
649 if (l != phsize) goto noexec;
650 ph = ph0 = allocated_buf;
651 } else if (eh->e_phoff + phsize > l) {
652 l = pread(fd, buf+1, phsize, eh->e_phoff);
653 if (l < 0) goto error;
654 if (l != phsize) goto noexec;
655 ph = ph0 = (void *)(buf + 1);
657 ph = ph0 = (void *)((char *)buf + eh->e_phoff);
659 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
660 if (ph->p_type == PT_DYNAMIC) {
662 } else if (ph->p_type == PT_TLS) {
663 tls_image = ph->p_vaddr;
664 dso->tls.align = ph->p_align;
665 dso->tls.len = ph->p_filesz;
666 dso->tls.size = ph->p_memsz;
667 } else if (ph->p_type == PT_GNU_RELRO) {
668 dso->relro_start = ph->p_vaddr & -PAGE_SIZE;
669 dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
670 } else if (ph->p_type == PT_GNU_STACK) {
671 if (!runtime && ph->p_memsz > __default_stacksize) {
672 __default_stacksize =
673 ph->p_memsz < DEFAULT_STACK_MAX ?
674 ph->p_memsz : DEFAULT_STACK_MAX;
677 if (ph->p_type != PT_LOAD) continue;
679 if (ph->p_vaddr < addr_min) {
680 addr_min = ph->p_vaddr;
681 off_start = ph->p_offset;
682 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
683 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
684 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
686 if (ph->p_vaddr+ph->p_memsz > addr_max) {
687 addr_max = ph->p_vaddr+ph->p_memsz;
690 if (!dyn) goto noexec;
691 if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
692 dso->loadmap = calloc(1, sizeof *dso->loadmap
693 + nsegs * sizeof *dso->loadmap->segs);
694 if (!dso->loadmap) goto error;
695 dso->loadmap->nsegs = nsegs;
696 for (ph=ph0, i=0; i<nsegs; ph=(void *)((char *)ph+eh->e_phentsize)) {
697 if (ph->p_type != PT_LOAD) continue;
698 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
699 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
700 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
701 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1),
703 fd, ph->p_offset & -PAGE_SIZE);
704 if (map == MAP_FAILED) {
708 dso->loadmap->segs[i].addr = (size_t)map +
709 (ph->p_vaddr & PAGE_SIZE-1);
710 dso->loadmap->segs[i].p_vaddr = ph->p_vaddr;
711 dso->loadmap->segs[i].p_memsz = ph->p_memsz;
713 if (prot & PROT_WRITE) {
714 size_t brk = (ph->p_vaddr & PAGE_SIZE-1)
716 size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE;
717 size_t pgend = brk + ph->p_memsz - ph->p_filesz
718 + PAGE_SIZE-1 & -PAGE_SIZE;
719 if (pgend > pgbrk && mmap_fixed(map+pgbrk,
721 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,
722 -1, off_start) == MAP_FAILED)
724 memset(map + brk, 0, pgbrk-brk);
727 map = (void *)dso->loadmap->segs[0].addr;
731 addr_max += PAGE_SIZE-1;
732 addr_max &= -PAGE_SIZE;
733 addr_min &= -PAGE_SIZE;
734 off_start &= -PAGE_SIZE;
735 map_len = addr_max - addr_min + off_start;
736 /* The first time, we map too much, possibly even more than
737 * the length of the file. This is okay because we will not
738 * use the invalid part; we just need to reserve the right
739 * amount of virtual address space to map over later. */
740 map = DL_NOMMU_SUPPORT
741 ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC,
742 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
743 : mmap((void *)addr_min, map_len, prot,
744 MAP_PRIVATE, fd, off_start);
745 if (map==MAP_FAILED) goto error;
747 dso->map_len = map_len;
748 /* If the loaded file is not relocatable and the requested address is
749 * not available, then the load operation must fail. */
750 if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) {
754 base = map - addr_min;
757 for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
758 if (ph->p_type != PT_LOAD) continue;
759 /* Check if the programs headers are in this load segment, and
760 * if so, record the address for use by dl_iterate_phdr. */
761 if (!dso->phdr && eh->e_phoff >= ph->p_offset
762 && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) {
763 dso->phdr = (void *)(base + ph->p_vaddr
764 + (eh->e_phoff-ph->p_offset));
765 dso->phnum = eh->e_phnum;
766 dso->phentsize = eh->e_phentsize;
768 this_min = ph->p_vaddr & -PAGE_SIZE;
769 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
770 off_start = ph->p_offset & -PAGE_SIZE;
771 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
772 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
773 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
774 /* Reuse the existing mapping for the lowest-address LOAD */
775 if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT)
776 if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
778 if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) {
779 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
780 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
781 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
782 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)
786 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
787 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
788 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
795 dso->dynv = laddr(dso, dyn);
796 if (dso->tls.size) dso->tls.image = laddr(dso, tls_image);
802 if (map!=MAP_FAILED) unmap_library(dso);
807 static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
812 s += strspn(s, ":\n");
813 l = strcspn(s, ":\n");
814 if (l-1 >= INT_MAX) return -1;
815 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) {
816 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
824 /* Any negative value but -1 will inhibit
825 * futher path search. */
833 static int fixup_rpath(struct dso *p, char *buf, size_t buf_size)
836 const char *s, *t, *origin;
838 if (p->rpath || !p->rpath_orig) return 0;
839 if (!strchr(p->rpath_orig, '$')) {
840 p->rpath = p->rpath_orig;
845 while ((t=strchr(s, '$'))) {
846 if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9))
851 if (n > SSIZE_MAX/PATH_MAX) return 0;
853 if (p->kernel_mapped) {
854 /* $ORIGIN searches cannot be performed for the main program
855 * when it is suid/sgid/AT_SECURE. This is because the
856 * pathname is under the control of the caller of execve.
857 * For libraries, however, $ORIGIN can be processed safely
858 * since the library's pathname came from a trusted source
859 * (either system paths or a call to dlopen). */
862 l = readlink("/proc/self/exe", buf, buf_size);
863 if (l == -1) switch (errno) {
878 t = strrchr(origin, '/');
882 /* Normally p->name will always be an absolute or relative
883 * pathname containing at least one '/' character, but in the
884 * case where ldso was invoked as a command to execute a
885 * program in the working directory, app.name may not. Fix. */
889 /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
890 if (libc.secure && *origin != '/')
892 p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1);
893 if (!p->rpath) return -1;
897 while ((t=strchr(s, '$'))) {
900 memcpy(d, origin, l);
902 /* It was determined previously that the '$' is followed
903 * either by "ORIGIN" or "{ORIGIN}". */
904 s = t + 7 + 2*(t[1]=='{');
910 static void decode_dyn(struct dso *p)
913 decode_vec(p->dynv, dyn, DYN_CNT);
914 p->syms = laddr(p, dyn[DT_SYMTAB]);
915 p->strings = laddr(p, dyn[DT_STRTAB]);
916 if (dyn[0]&(1<<DT_HASH))
917 p->hashtab = laddr(p, dyn[DT_HASH]);
918 if (dyn[0]&(1<<DT_RPATH))
919 p->rpath_orig = p->strings + dyn[DT_RPATH];
920 if (dyn[0]&(1<<DT_RUNPATH))
921 p->rpath_orig = p->strings + dyn[DT_RUNPATH];
922 if (dyn[0]&(1<<DT_PLTGOT))
923 p->got = laddr(p, dyn[DT_PLTGOT]);
924 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
925 p->ghashtab = laddr(p, *dyn);
926 if (search_vec(p->dynv, dyn, DT_VERSYM))
927 p->versym = laddr(p, *dyn);
930 static size_t count_syms(struct dso *p)
932 if (p->hashtab) return p->hashtab[1];
935 uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
937 for (i = nsym = 0; i < p->ghashtab[0]; i++) {
938 if (buckets[i] > nsym)
942 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]);
944 while (!(*hashval++ & 1));
949 static void *dl_mmap(size_t n)
952 int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE;
954 p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0);
956 p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0);
958 return (unsigned long)p > -4096UL ? 0 : p;
961 static void makefuncdescs(struct dso *p)
963 static int self_done;
964 size_t nsym = count_syms(p);
965 size_t i, size = nsym * sizeof(*p->funcdescs);
968 p->funcdescs = dl_mmap(size);
971 p->funcdescs = malloc(size);
974 if (!runtime) a_crash();
975 error("Error allocating function descriptors for %s", p->name);
976 longjmp(*rtld_fail, 1);
978 for (i=0; i<nsym; i++) {
979 if ((p->syms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) {
980 p->funcdescs[i].addr = laddr(p, p->syms[i].st_value);
981 p->funcdescs[i].got = p->got;
983 p->funcdescs[i].addr = 0;
984 p->funcdescs[i].got = 0;
989 static struct dso *load_library(const char *name, struct dso *needed_by)
991 char buf[2*NAME_MAX+2];
992 const char *pathname;
994 struct dso *p, temp_dso = {0};
1006 /* Catch and block attempts to reload the implementation itself */
1007 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
1008 static const char reserved[] =
1009 "c.pthread.rt.m.dl.util.xnet.";
1010 const char *rp, *next;
1011 for (rp=reserved; *rp; rp=next) {
1012 next = strchr(rp, '.') + 1;
1013 if (strncmp(name+3, rp, next-rp) == 0)
1018 /* Track which names have been resolved
1019 * and only report each one once. */
1020 static unsigned reported;
1021 unsigned mask = 1U<<(rp-reserved);
1022 if (!(reported & mask)) {
1024 dprintf(1, "\t%s => %s (%p)\n",
1032 if (!strcmp(name, ldso.name)) is_self = 1;
1041 if (strchr(name, '/')) {
1043 fd = open(name, O_RDONLY|O_CLOEXEC);
1045 /* Search for the name to see if it's already loaded */
1046 for (p=head->next; p; p=p->next) {
1047 if (p->shortname && !strcmp(p->shortname, name)) {
1051 if (strlen(name) > NAME_MAX) return 0;
1053 if (env_path) fd = path_open(name, env_path, buf, sizeof buf);
1054 for (p=needed_by; fd == -1 && p; p=p->needed_by) {
1055 if (fixup_rpath(p, buf, sizeof buf) < 0)
1056 fd = -2; /* Inhibit further search. */
1058 fd = path_open(name, p->rpath, buf, sizeof buf);
1064 if (ldso.name[0]=='/') {
1066 for (s=t=z=ldso.name; *s; s++)
1067 if (*s=='/') z=t, t=s;
1068 prefix_len = z-ldso.name;
1069 if (prefix_len < PATH_MAX)
1076 char etc_ldso_path[prefix_len + 1
1077 + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
1078 snprintf(etc_ldso_path, sizeof etc_ldso_path,
1079 "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
1080 (int)prefix_len, prefix);
1081 fd = open(etc_ldso_path, O_RDONLY|O_CLOEXEC);
1084 if (!fstat(fd, &st)) n = st.st_size;
1085 if ((sys_path = malloc(n+1)))
1087 if (!sys_path || read_loop(fd, sys_path, n)<0) {
1092 } else if (errno != ENOENT) {
1096 if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
1097 fd = path_open(name, sys_path, buf, sizeof buf);
1101 if (fd < 0) return 0;
1102 if (fstat(fd, &st) < 0) {
1106 for (p=head->next; p; p=p->next) {
1107 if (p->dev == st.st_dev && p->ino == st.st_ino) {
1108 /* If this library was previously loaded with a
1109 * pathname but a search found the same inode,
1110 * setup its shortname so it can be found by name. */
1111 if (!p->shortname && pathname != name)
1112 p->shortname = strrchr(p->name, '/')+1;
1117 map = noload ? 0 : map_library(fd, &temp_dso);
1121 /* Avoid the danger of getting two versions of libc mapped into the
1122 * same process when an absolute pathname was used. The symbols
1123 * checked are chosen to catch both musl and glibc, and to avoid
1124 * false positives from interposition-hack libraries. */
1125 decode_dyn(&temp_dso);
1126 if (find_sym(&temp_dso, "__libc_start_main", 1).sym &&
1127 find_sym(&temp_dso, "stdin", 1).sym) {
1128 unmap_library(&temp_dso);
1129 return load_library("libc.so", needed_by);
1131 /* Past this point, if we haven't reached runtime yet, ldso has
1132 * committed either to use the mapped library or to abort execution.
1133 * Unmapping is not possible, so we can safely reclaim gaps. */
1134 if (!runtime) reclaim_gaps(&temp_dso);
1136 /* Allocate storage for the new DSO. When there is TLS, this
1137 * storage must include a reservation for all pre-existing
1138 * threads to obtain copies of both the new TLS, and an
1139 * extended DTV capable of storing an additional slot for
1140 * the newly-loaded DSO. */
1141 alloc_size = sizeof *p + strlen(pathname) + 1;
1142 if (runtime && temp_dso.tls.image) {
1143 size_t per_th = temp_dso.tls.size + temp_dso.tls.align
1144 + sizeof(void *) * (tls_cnt+3);
1145 n_th = libc.threads_minus_1 + 1;
1146 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
1147 else alloc_size += n_th * per_th;
1149 p = calloc(1, alloc_size);
1151 unmap_library(&temp_dso);
1154 memcpy(p, &temp_dso, sizeof temp_dso);
1157 p->needed_by = needed_by;
1159 p->runtime_loaded = runtime;
1160 strcpy(p->name, pathname);
1161 /* Add a shortname only if name arg was not an explicit pathname. */
1162 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
1164 p->tls_id = ++tls_cnt;
1165 tls_align = MAXP2(tls_align, p->tls.align);
1167 p->tls.offset = tls_offset + ( (p->tls.align-1) &
1168 (-tls_offset + (uintptr_t)p->tls.image) );
1169 tls_offset = p->tls.offset + p->tls.size;
1171 tls_offset += p->tls.size + p->tls.align - 1;
1172 tls_offset -= (tls_offset + (uintptr_t)p->tls.image)
1174 p->tls.offset = tls_offset;
1176 p->new_dtv = (void *)(-sizeof(size_t) &
1177 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
1178 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
1179 if (tls_tail) tls_tail->next = &p->tls;
1180 else libc.tls_head = &p->tls;
1188 if (DL_FDPIC) makefuncdescs(p);
1190 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
1195 static void load_direct_deps(struct dso *p)
1199 if (p->deps) return;
1200 /* For head, all preloads are direct pseudo-dependencies.
1201 * Count and include them now to avoid realloc later. */
1202 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1204 for (i=0; p->dynv[i]; i+=2)
1205 if (p->dynv[i] == DT_NEEDED) cnt++;
1206 /* Use builtin buffer for apps with no external deps, to
1207 * preserve property of no runtime failure paths. */
1208 p->deps = (p==head && cnt<2) ? builtin_deps :
1209 calloc(cnt+1, sizeof *p->deps);
1211 error("Error loading dependencies for %s", p->name);
1212 if (runtime) longjmp(*rtld_fail, 1);
1215 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1217 for (i=0; p->dynv[i]; i+=2) {
1218 if (p->dynv[i] != DT_NEEDED) continue;
1219 struct dso *dep = load_library(p->strings + p->dynv[i+1], p);
1221 error("Error loading shared library %s: %m (needed by %s)",
1222 p->strings + p->dynv[i+1], p->name);
1223 if (runtime) longjmp(*rtld_fail, 1);
1226 p->deps[cnt++] = dep;
1229 p->ndeps_direct = cnt;
1232 static void load_deps(struct dso *p)
1234 if (p->deps) return;
1235 for (; p; p=p->next)
1236 load_direct_deps(p);
1239 static void extend_bfs_deps(struct dso *p)
1241 size_t i, j, cnt, ndeps_all;
1244 /* Can't use realloc if the original p->deps was allocated at
1245 * program entry and malloc has been replaced, or if it's
1246 * the builtin non-allocated trivial main program deps array. */
1247 int no_realloc = (__malloc_replaced && !p->runtime_loaded)
1248 || p->deps == builtin_deps;
1250 if (p->bfs_built) return;
1251 ndeps_all = p->ndeps_direct;
1253 /* Mark existing (direct) deps so they won't be duplicated. */
1254 for (i=0; p->deps[i]; i++)
1255 p->deps[i]->mark = 1;
1257 /* For each dependency already in the list, copy its list of direct
1258 * dependencies to the list, excluding any items already in the
1259 * list. Note that the list this loop iterates over will grow during
1260 * the loop, but since duplicates are excluded, growth is bounded. */
1261 for (i=0; p->deps[i]; i++) {
1262 struct dso *dep = p->deps[i];
1263 for (j=cnt=0; j<dep->ndeps_direct; j++)
1264 if (!dep->deps[j]->mark) cnt++;
1266 malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) :
1267 realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1));
1269 error("Error recording dependencies for %s", p->name);
1270 if (runtime) longjmp(*rtld_fail, 1);
1274 memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1));
1278 for (j=0; j<dep->ndeps_direct; j++) {
1279 if (dep->deps[j]->mark) continue;
1280 dep->deps[j]->mark = 1;
1281 p->deps[ndeps_all++] = dep->deps[j];
1283 p->deps[ndeps_all] = 0;
1286 for (p=head; p; p=p->next)
1290 static void load_preload(char *s)
1294 for (z=s; *z; s=z) {
1295 for ( ; *s && (isspace(*s) || *s==':'); s++);
1296 for (z=s; *z && !isspace(*z) && *z!=':'; z++);
1304 static void add_syms(struct dso *p)
1306 if (!p->syms_next && syms_tail != p) {
1307 syms_tail->syms_next = p;
1312 static void revert_syms(struct dso *old_tail)
1314 struct dso *p, *next;
1315 /* Chop off the tail of the list of dsos that participate in
1316 * the global symbol table, reverting them to RTLD_LOCAL. */
1317 for (p=old_tail; p; p=next) {
1318 next = p->syms_next;
1321 syms_tail = old_tail;
1324 static void do_mips_relocs(struct dso *p, size_t *got)
1326 size_t i, j, rel[2];
1327 unsigned char *base = p->base;
1328 i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO);
1332 while (i--) *got++ += (size_t)base;
1334 j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1335 i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1336 Sym *sym = p->syms + j;
1337 rel[0] = (unsigned char *)got - base;
1338 for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) {
1339 rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT);
1340 do_relocs(p, rel, sizeof rel, 2);
1344 static void reloc_all(struct dso *p)
1346 size_t dyn[DYN_CNT];
1347 for (; p; p=p->next) {
1348 if (p->relocated) continue;
1349 decode_vec(p->dynv, dyn, DYN_CNT);
1350 if (NEED_MIPS_GOT_RELOCS)
1351 do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT]));
1352 do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
1353 2+(dyn[DT_PLTREL]==DT_RELA));
1354 do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
1355 do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
1357 if (head != &ldso && p->relro_start != p->relro_end &&
1358 mprotect(laddr(p, p->relro_start), p->relro_end-p->relro_start, PROT_READ)
1359 && errno != ENOSYS) {
1360 error("Error relocating %s: RELRO protection failed: %m",
1362 if (runtime) longjmp(*rtld_fail, 1);
1369 static void kernel_mapped_dso(struct dso *p)
1371 size_t min_addr = -1, max_addr = 0, cnt;
1373 for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) {
1374 if (ph->p_type == PT_DYNAMIC) {
1375 p->dynv = laddr(p, ph->p_vaddr);
1376 } else if (ph->p_type == PT_GNU_RELRO) {
1377 p->relro_start = ph->p_vaddr & -PAGE_SIZE;
1378 p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
1379 } else if (ph->p_type == PT_GNU_STACK) {
1380 if (!runtime && ph->p_memsz > __default_stacksize) {
1381 __default_stacksize =
1382 ph->p_memsz < DEFAULT_STACK_MAX ?
1383 ph->p_memsz : DEFAULT_STACK_MAX;
1386 if (ph->p_type != PT_LOAD) continue;
1387 if (ph->p_vaddr < min_addr)
1388 min_addr = ph->p_vaddr;
1389 if (ph->p_vaddr+ph->p_memsz > max_addr)
1390 max_addr = ph->p_vaddr+ph->p_memsz;
1392 min_addr &= -PAGE_SIZE;
1393 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
1394 p->map = p->base + min_addr;
1395 p->map_len = max_addr - min_addr;
1396 p->kernel_mapped = 1;
1399 void __libc_exit_fini()
1402 size_t dyn[DYN_CNT];
1403 pthread_t self = __pthread_self();
1405 /* Take both locks before setting shutting_down, so that
1406 * either lock is sufficient to read its value. The lock
1407 * order matches that in dlopen to avoid deadlock. */
1408 pthread_rwlock_wrlock(&lock);
1409 pthread_mutex_lock(&init_fini_lock);
1411 pthread_rwlock_unlock(&lock);
1412 for (p=fini_head; p; p=p->fini_next) {
1413 while (p->ctor_visitor && p->ctor_visitor!=self)
1414 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1415 if (!p->constructed) continue;
1416 decode_vec(p->dynv, dyn, DYN_CNT);
1417 if (dyn[0] & (1<<DT_FINI_ARRAY)) {
1418 size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
1419 size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY])+n;
1420 while (n--) ((void (*)(void))*--fn)();
1422 #ifndef NO_LEGACY_INITFINI
1423 if ((dyn[0] & (1<<DT_FINI)) && dyn[DT_FINI])
1424 fpaddr(p, dyn[DT_FINI])();
1429 static struct dso **queue_ctors(struct dso *dso)
1431 size_t cnt, qpos, spos, i;
1432 struct dso *p, **queue, **stack;
1434 if (ldd_mode) return 0;
1436 /* Bound on queue size is the total number of indirect deps.
1437 * If a bfs deps list was built, we can use it. Otherwise,
1438 * bound by the total number of DSOs, which is always safe and
1439 * is reasonable we use it (for main app at startup). */
1440 if (dso->bfs_built) {
1441 for (cnt=0; dso->deps[cnt]; cnt++)
1442 dso->deps[cnt]->mark = 0;
1443 cnt++; /* self, not included in deps */
1445 for (cnt=0, p=head; p; cnt++, p=p->next)
1448 cnt++; /* termination slot */
1449 if (dso==head && cnt <= countof(builtin_ctor_queue))
1450 queue = builtin_ctor_queue;
1452 queue = calloc(cnt, sizeof *queue);
1455 error("Error allocating constructor queue: %m\n");
1456 if (runtime) longjmp(*rtld_fail, 1);
1460 /* Opposite ends of the allocated buffer serve as an output queue
1461 * and a working stack. Setup initial stack with just the argument
1462 * dso and initial queue empty... */
1466 stack[--spos] = dso;
1470 /* Then perform pseudo-DFS sort, but ignoring circular deps. */
1473 while (p->next_dep < p->ndeps_direct) {
1474 if (p->deps[p->next_dep]->mark) {
1478 p = p->deps[p->next_dep];
1486 for (i=0; i<qpos; i++) queue[i]->mark = 0;
1491 static void do_init_fini(struct dso **queue)
1494 size_t dyn[DYN_CNT], i;
1495 pthread_t self = __pthread_self();
1497 pthread_mutex_lock(&init_fini_lock);
1498 for (i=0; (p=queue[i]); i++) {
1499 while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down)
1500 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1501 if (p->ctor_visitor || p->constructed)
1503 p->ctor_visitor = self;
1505 decode_vec(p->dynv, dyn, DYN_CNT);
1506 if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
1507 p->fini_next = fini_head;
1511 pthread_mutex_unlock(&init_fini_lock);
1513 #ifndef NO_LEGACY_INITFINI
1514 if ((dyn[0] & (1<<DT_INIT)) && dyn[DT_INIT])
1515 fpaddr(p, dyn[DT_INIT])();
1517 if (dyn[0] & (1<<DT_INIT_ARRAY)) {
1518 size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
1519 size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]);
1520 while (n--) ((void (*)(void))*fn++)();
1523 pthread_mutex_lock(&init_fini_lock);
1524 p->ctor_visitor = 0;
1526 pthread_cond_broadcast(&ctor_cond);
1528 pthread_mutex_unlock(&init_fini_lock);
1531 void __libc_start_init(void)
1533 do_init_fini(main_ctor_queue);
1534 if (!__malloc_replaced && main_ctor_queue != builtin_ctor_queue)
1535 free(main_ctor_queue);
1536 main_ctor_queue = 0;
1539 static void dl_debug_state(void)
1543 weak_alias(dl_debug_state, _dl_debug_state);
1545 void __init_tls(size_t *auxv)
1549 static void update_tls_size()
1551 libc.tls_cnt = tls_cnt;
1552 libc.tls_align = tls_align;
1553 libc.tls_size = ALIGN(
1554 (1+tls_cnt) * sizeof(void *) +
1556 sizeof(struct pthread) +
1561 static void install_new_tls(void)
1564 pthread_t self = __pthread_self(), td;
1565 struct dso *dtv_provider = container_of(tls_tail, struct dso, tls);
1566 uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv;
1569 size_t old_cnt = self->dtv[0];
1571 __block_app_sigs(&set);
1573 /* Copy existing dtv contents from all existing threads. */
1574 for (i=0, td=self; !i || td!=self; i++, td=td->next) {
1575 memcpy(newdtv+i, td->dtv,
1576 (old_cnt+1)*sizeof(uintptr_t));
1577 newdtv[i][0] = tls_cnt;
1579 /* Install new dtls into the enlarged, uninstalled dtv copies. */
1580 for (p=head; ; p=p->next) {
1581 if (p->tls_id <= old_cnt) continue;
1582 unsigned char *mem = p->new_tls;
1583 for (j=0; j<i; j++) {
1584 unsigned char *new = mem;
1585 new += ((uintptr_t)p->tls.image - (uintptr_t)mem)
1587 memcpy(new, p->tls.image, p->tls.len);
1588 newdtv[j][p->tls_id] =
1589 (uintptr_t)new + DTP_OFFSET;
1590 mem += p->tls.size + p->tls.align;
1592 if (p->tls_id == tls_cnt) break;
1595 /* Broadcast barrier to ensure contents of new dtv is visible
1596 * if the new dtv pointer is. The __membarrier function has a
1597 * fallback emulation using signals for kernels that lack the
1598 * feature at the syscall level. */
1600 __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
1602 /* Install new dtv for each thread. */
1603 for (j=0, td=self; !j || td!=self; j++, td=td->next) {
1604 td->dtv = newdtv[j];
1608 __restore_sigs(&set);
1611 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1612 * following stage 2 and stage 3 functions via primitive symbolic lookup
1613 * since it does not have access to their addresses to begin with. */
1615 /* Stage 2 of the dynamic linker is called after relative relocations
1616 * have been processed. It can make function calls to static functions
1617 * and access string literals and static data, but cannot use extern
1618 * symbols. Its job is to perform symbolic relocations on the dynamic
1619 * linker itself, but some of the relocations performed may need to be
1620 * replaced later due to copy relocations in the main program. */
1622 hidden void __dls2(unsigned char *base, size_t *sp)
1625 for (auxv=sp+1+*sp+1; *auxv; auxv++);
1628 void *p1 = (void *)sp[-2];
1629 void *p2 = (void *)sp[-1];
1631 size_t aux[AUX_CNT];
1632 decode_vec(auxv, aux, AUX_CNT);
1633 if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE];
1634 else ldso.base = (void *)(aux[AT_PHDR] & -4096);
1636 app_loadmap = p2 ? p1 : 0;
1637 ldso.loadmap = p2 ? p2 : p1;
1638 ldso.base = laddr(&ldso, 0);
1642 Ehdr *ehdr = (void *)ldso.base;
1643 ldso.name = ldso.shortname = "libc.so";
1644 ldso.phnum = ehdr->e_phnum;
1645 ldso.phdr = laddr(&ldso, ehdr->e_phoff);
1646 ldso.phentsize = ehdr->e_phentsize;
1647 kernel_mapped_dso(&ldso);
1650 if (DL_FDPIC) makefuncdescs(&ldso);
1652 /* Prepare storage for to save clobbered REL addends so they
1653 * can be reused in stage 3. There should be very few. If
1654 * something goes wrong and there are a huge number, abort
1655 * instead of risking stack overflow. */
1656 size_t dyn[DYN_CNT];
1657 decode_vec(ldso.dynv, dyn, DYN_CNT);
1658 size_t *rel = laddr(&ldso, dyn[DT_REL]);
1659 size_t rel_size = dyn[DT_RELSZ];
1660 size_t symbolic_rel_cnt = 0;
1661 apply_addends_to = rel;
1662 for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t))
1663 if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++;
1664 if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash();
1665 size_t addends[symbolic_rel_cnt+1];
1666 saved_addends = addends;
1673 /* Call dynamic linker stage-2b, __dls2b, looking it up
1674 * symbolically as a barrier against moving the address
1675 * load across the above relocation processing. */
1676 struct symdef dls2b_def = find_sym(&ldso, "__dls2b", 0);
1677 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp, auxv);
1678 else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp, auxv);
1681 /* Stage 2b sets up a valid thread pointer, which requires relocations
1682 * completed in stage 2, and on which stage 3 is permitted to depend.
1683 * This is done as a separate stage, with symbolic lookup as a barrier,
1684 * so that loads of the thread pointer and &errno can be pure/const and
1685 * thereby hoistable. */
1687 void __dls2b(size_t *sp, size_t *auxv)
1689 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1690 * use during dynamic linking. If possible it will also serve as the
1691 * thread pointer at runtime. */
1692 search_vec(auxv, &__hwcap, AT_HWCAP);
1694 libc.tls_size = sizeof builtin_tls;
1695 libc.tls_align = tls_align;
1696 if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) {
1700 struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
1701 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp, auxv);
1702 else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp, auxv);
1705 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1706 * fully functional. Its job is to load (if not already loaded) and
1707 * process dependencies and relocations for the main application and
1708 * transfer control to its entry point. */
1710 void __dls3(size_t *sp, size_t *auxv)
1712 static struct dso app, vdso;
1713 size_t aux[AUX_CNT];
1715 char *env_preload=0;
1716 char *replace_argv0=0;
1719 char **argv = (void *)(sp+1);
1720 char **argv_orig = argv;
1721 char **envp = argv+argc+1;
1723 /* Find aux vector just past environ[] and use it to initialize
1724 * global data that may be needed before we can make syscalls. */
1726 decode_vec(auxv, aux, AUX_CNT);
1727 search_vec(auxv, &__sysinfo, AT_SYSINFO);
1728 __pthread_self()->sysinfo = __sysinfo;
1729 libc.page_size = aux[AT_PAGESZ];
1730 libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1731 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
1733 /* Only trust user/env if kernel says we're not suid/sgid */
1735 env_path = getenv("LD_LIBRARY_PATH");
1736 env_preload = getenv("LD_PRELOAD");
1739 /* If the main program was already loaded by the kernel,
1740 * AT_PHDR will point to some location other than the dynamic
1741 * linker's program headers. */
1742 if (aux[AT_PHDR] != (size_t)ldso.phdr) {
1743 size_t interp_off = 0;
1744 size_t tls_image = 0;
1745 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1746 Phdr *phdr = app.phdr = (void *)aux[AT_PHDR];
1747 app.phnum = aux[AT_PHNUM];
1748 app.phentsize = aux[AT_PHENT];
1749 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
1750 if (phdr->p_type == PT_PHDR)
1751 app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
1752 else if (phdr->p_type == PT_INTERP)
1753 interp_off = (size_t)phdr->p_vaddr;
1754 else if (phdr->p_type == PT_TLS) {
1755 tls_image = phdr->p_vaddr;
1756 app.tls.len = phdr->p_filesz;
1757 app.tls.size = phdr->p_memsz;
1758 app.tls.align = phdr->p_align;
1761 if (DL_FDPIC) app.loadmap = app_loadmap;
1762 if (app.tls.size) app.tls.image = laddr(&app, tls_image);
1763 if (interp_off) ldso.name = laddr(&app, interp_off);
1764 if ((aux[0] & (1UL<<AT_EXECFN))
1765 && strncmp((char *)aux[AT_EXECFN], "/proc/", 6))
1766 app.name = (char *)aux[AT_EXECFN];
1769 kernel_mapped_dso(&app);
1772 char *ldname = argv[0];
1773 size_t l = strlen(ldname);
1774 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
1776 while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') {
1777 char *opt = argv[0]+2;
1778 *argv++ = (void *)-1;
1781 } else if (!memcmp(opt, "list", 5)) {
1783 } else if (!memcmp(opt, "library-path", 12)) {
1784 if (opt[12]=='=') env_path = opt+13;
1785 else if (opt[12]) *argv = 0;
1786 else if (*argv) env_path = *argv++;
1787 } else if (!memcmp(opt, "preload", 7)) {
1788 if (opt[7]=='=') env_preload = opt+8;
1789 else if (opt[7]) *argv = 0;
1790 else if (*argv) env_preload = *argv++;
1791 } else if (!memcmp(opt, "argv0", 5)) {
1792 if (opt[5]=='=') replace_argv0 = opt+6;
1793 else if (opt[5]) *argv = 0;
1794 else if (*argv) replace_argv0 = *argv++;
1799 argv[-1] = (void *)(argc - (argv-argv_orig));
1801 dprintf(2, "musl libc (" LDSO_ARCH ")\n"
1803 "Dynamic Program Loader\n"
1804 "Usage: %s [options] [--] pathname%s\n",
1805 __libc_version, ldname,
1806 ldd_mode ? "" : " [args]");
1809 fd = open(argv[0], O_RDONLY);
1811 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
1814 Ehdr *ehdr = (void *)map_library(fd, &app);
1816 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
1822 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry);
1823 /* Find the name that would have been used for the dynamic
1824 * linker had ldd not taken its place. */
1826 for (i=0; i<app.phnum; i++) {
1827 if (app.phdr[i].p_type == PT_INTERP)
1828 ldso.name = laddr(&app, app.phdr[i].p_vaddr);
1830 dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base);
1834 libc.tls_head = tls_tail = &app.tls;
1835 app.tls_id = tls_cnt = 1;
1837 app.tls.offset = GAP_ABOVE_TP;
1838 app.tls.offset += (-GAP_ABOVE_TP + (uintptr_t)app.tls.image)
1839 & (app.tls.align-1);
1840 tls_offset = app.tls.offset + app.tls.size;
1842 tls_offset = app.tls.offset = app.tls.size
1843 + ( -((uintptr_t)app.tls.image + app.tls.size)
1844 & (app.tls.align-1) );
1846 tls_align = MAXP2(tls_align, app.tls.align);
1850 makefuncdescs(&app);
1852 app.loadmap = (void *)&app_dummy_loadmap;
1853 app.loadmap->nsegs = 1;
1854 app.loadmap->segs[0].addr = (size_t)app.map;
1855 app.loadmap->segs[0].p_vaddr = (size_t)app.map
1857 app.loadmap->segs[0].p_memsz = app.map_len;
1859 argv[-3] = (void *)app.loadmap;
1862 /* Initial dso chain consists only of the app. */
1863 head = tail = syms_tail = &app;
1865 /* Donate unused parts of app and library mapping to malloc */
1867 reclaim_gaps(&ldso);
1869 /* Load preload/needed libraries, add symbols to global namespace. */
1870 ldso.deps = (struct dso **)no_deps;
1871 if (env_preload) load_preload(env_preload);
1873 for (struct dso *p=head; p; p=p->next)
1876 /* Attach to vdso, if provided by the kernel, last so that it does
1877 * not become part of the global namespace. */
1878 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR) && vdso_base) {
1879 Ehdr *ehdr = (void *)vdso_base;
1880 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff);
1881 vdso.phnum = ehdr->e_phnum;
1882 vdso.phentsize = ehdr->e_phentsize;
1883 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
1884 if (phdr->p_type == PT_DYNAMIC)
1885 vdso.dynv = (void *)(vdso_base + phdr->p_offset);
1886 if (phdr->p_type == PT_LOAD)
1887 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
1890 vdso.shortname = "linux-gate.so.1";
1892 vdso.deps = (struct dso **)no_deps;
1899 for (i=0; app.dynv[i]; i+=2) {
1900 if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG)
1901 app.dynv[i+1] = (size_t)&debug;
1902 if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) {
1903 size_t *ptr = (size_t *) app.dynv[i+1];
1904 *ptr = (size_t)&debug;
1908 /* This must be done before final relocations, since it calls
1909 * malloc, which may be provided by the application. Calling any
1910 * application code prior to the jump to its entry point is not
1911 * valid in our model and does not work with FDPIC, where there
1912 * are additional relocation-like fixups that only the entry point
1913 * code can see to perform. */
1914 main_ctor_queue = queue_ctors(&app);
1916 /* Initial TLS must also be allocated before final relocations
1917 * might result in calloc being a call to application code. */
1919 void *initial_tls = builtin_tls;
1920 if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) {
1921 initial_tls = calloc(libc.tls_size, 1);
1923 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
1924 argv[0], libc.tls_size);
1928 static_tls_cnt = tls_cnt;
1930 /* The main program must be relocated LAST since it may contain
1931 * copy relocations which depend on libraries' relocations. */
1932 reloc_all(app.next);
1935 /* Actual copying to new TLS needs to happen after relocations,
1936 * since the TLS images might have contained relocated addresses. */
1937 if (initial_tls != builtin_tls) {
1938 if (__init_tp(__copy_tls(initial_tls)) < 0) {
1942 size_t tmp_tls_size = libc.tls_size;
1943 pthread_t self = __pthread_self();
1944 /* Temporarily set the tls size to the full size of
1945 * builtin_tls so that __copy_tls will use the same layout
1946 * as it did for before. Then check, just to be safe. */
1947 libc.tls_size = sizeof builtin_tls;
1948 if (__copy_tls((void*)builtin_tls) != self) a_crash();
1949 libc.tls_size = tmp_tls_size;
1952 if (ldso_fail) _exit(127);
1953 if (ldd_mode) _exit(0);
1955 /* Determine if malloc was interposed by a replacement implementation
1956 * so that calloc and the memalign family can harden against the
1957 * possibility of incomplete replacement. */
1958 if (find_sym(head, "malloc", 1).dso != &ldso)
1959 __malloc_replaced = 1;
1960 if (find_sym(head, "aligned_alloc", 1).dso != &ldso)
1961 __aligned_alloc_replaced = 1;
1963 /* Switch to runtime mode: any further failures in the dynamic
1964 * linker are a reportable failure rather than a fatal startup
1969 debug.bp = dl_debug_state;
1971 debug.base = ldso.base;
1972 debug.state = RT_CONSISTENT;
1975 if (replace_argv0) argv[0] = replace_argv0;
1979 CRTJMP((void *)aux[AT_ENTRY], argv-1);
1983 static void prepare_lazy(struct dso *p)
1985 size_t dyn[DYN_CNT], n, flags1=0;
1986 decode_vec(p->dynv, dyn, DYN_CNT);
1987 search_vec(p->dynv, &flags1, DT_FLAGS_1);
1988 if (dyn[DT_BIND_NOW] || (dyn[DT_FLAGS] & DF_BIND_NOW) || (flags1 & DF_1_NOW))
1990 n = dyn[DT_RELSZ]/2 + dyn[DT_RELASZ]/3 + dyn[DT_PLTRELSZ]/2 + 1;
1991 if (NEED_MIPS_GOT_RELOCS) {
1992 size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1993 size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1996 p->lazy = calloc(n, 3*sizeof(size_t));
1998 error("Error preparing lazy relocation for %s: %m", p->name);
1999 longjmp(*rtld_fail, 1);
2001 p->lazy_next = lazy_head;
2005 void *dlopen(const char *file, int mode)
2007 struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next;
2008 struct tls_module *orig_tls_tail;
2009 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
2013 struct dso **volatile ctor_queue = 0;
2015 if (!file) return head;
2017 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
2018 pthread_rwlock_wrlock(&lock);
2021 debug.state = RT_ADD;
2025 if (shutting_down) {
2026 error("Cannot dlopen while program is exiting.");
2029 orig_tls_tail = tls_tail;
2030 orig_tls_cnt = tls_cnt;
2031 orig_tls_offset = tls_offset;
2032 orig_tls_align = tls_align;
2033 orig_lazy_head = lazy_head;
2034 orig_syms_tail = syms_tail;
2036 noload = mode & RTLD_NOLOAD;
2039 if (setjmp(*rtld_fail)) {
2040 /* Clean up anything new that was (partially) loaded */
2041 revert_syms(orig_syms_tail);
2042 for (p=orig_tail->next; p; p=next) {
2044 while (p->td_index) {
2045 void *tmp = p->td_index->next;
2050 if (p->rpath != p->rpath_orig)
2058 if (!orig_tls_tail) libc.tls_head = 0;
2059 tls_tail = orig_tls_tail;
2060 if (tls_tail) tls_tail->next = 0;
2061 tls_cnt = orig_tls_cnt;
2062 tls_offset = orig_tls_offset;
2063 tls_align = orig_tls_align;
2064 lazy_head = orig_lazy_head;
2069 } else p = load_library(file, head);
2073 "Library %s is not already loaded" :
2074 "Error loading shared library %s: %m",
2079 /* First load handling */
2082 pthread_mutex_lock(&init_fini_lock);
2083 int constructed = p->constructed;
2084 pthread_mutex_unlock(&init_fini_lock);
2085 if (!constructed) ctor_queue = queue_ctors(p);
2086 if (!p->relocated && (mode & RTLD_LAZY)) {
2088 for (i=0; p->deps[i]; i++)
2089 if (!p->deps[i]->relocated)
2090 prepare_lazy(p->deps[i]);
2092 if (!p->relocated || (mode & RTLD_GLOBAL)) {
2093 /* Make new symbols global, at least temporarily, so we can do
2094 * relocations. If not RTLD_GLOBAL, this is reverted below. */
2096 for (i=0; p->deps[i]; i++)
2097 add_syms(p->deps[i]);
2099 if (!p->relocated) {
2103 /* If RTLD_GLOBAL was not specified, undo any new additions
2104 * to the global symbol table. This is a nop if the library was
2105 * previously loaded and already global. */
2106 if (!(mode & RTLD_GLOBAL))
2107 revert_syms(orig_syms_tail);
2109 /* Processing of deferred lazy relocations must not happen until
2110 * the new libraries are committed; otherwise we could end up with
2111 * relocations resolved to symbol definitions that get removed. */
2115 if (tls_cnt != orig_tls_cnt)
2119 debug.state = RT_CONSISTENT;
2123 pthread_rwlock_unlock(&lock);
2125 do_init_fini(ctor_queue);
2128 pthread_setcancelstate(cs, 0);
2132 hidden int __dl_invalid_handle(void *h)
2135 for (p=head; p; p=p->next) if (h==p) return 0;
2136 error("Invalid library handle %p", (void *)h);
2140 static void *addr2dso(size_t a)
2144 if (DL_FDPIC) for (p=head; p; p=p->next) {
2146 if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs))
2149 for (p=head; p; p=p->next) {
2150 if (DL_FDPIC && p->loadmap) {
2151 for (i=0; i<p->loadmap->nsegs; i++) {
2152 if (a-p->loadmap->segs[i].p_vaddr
2153 < p->loadmap->segs[i].p_memsz)
2158 size_t phcnt = p->phnum;
2159 size_t entsz = p->phentsize;
2160 size_t base = (size_t)p->base;
2161 for (; phcnt--; ph=(void *)((char *)ph+entsz)) {
2162 if (ph->p_type != PT_LOAD) continue;
2163 if (a-base-ph->p_vaddr < ph->p_memsz)
2166 if (a-(size_t)p->map < p->map_len)
2173 static void *do_dlsym(struct dso *p, const char *s, void *ra)
2176 if (p == head || p == RTLD_DEFAULT) {
2178 } else if (p == RTLD_NEXT) {
2179 p = addr2dso((size_t)ra);
2182 } else if (__dl_invalid_handle(p)) {
2186 struct symdef def = find_sym2(p, s, 0, use_deps);
2188 error("Symbol not found: %s", s);
2191 if ((def.sym->st_info&0xf) == STT_TLS)
2192 return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET});
2193 if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC)
2194 return def.dso->funcdescs + (def.sym - def.dso->syms);
2195 return laddr(def.dso, def.sym->st_value);
2198 int dladdr(const void *addr_arg, Dl_info *info)
2200 size_t addr = (size_t)addr_arg;
2206 size_t besterr = -1;
2208 pthread_rwlock_rdlock(&lock);
2210 pthread_rwlock_unlock(&lock);
2215 strings = p->strings;
2216 nsym = count_syms(p);
2219 size_t idx = (addr-(size_t)p->funcdescs)
2220 / sizeof(*p->funcdescs);
2221 if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) {
2222 best = (size_t)(p->funcdescs + idx);
2223 bestsym = sym + idx;
2228 if (!best) for (; nsym; nsym--, sym++) {
2230 && (1<<(sym->st_info&0xf) & OK_TYPES)
2231 && (1<<(sym->st_info>>4) & OK_BINDS)) {
2232 size_t symaddr = (size_t)laddr(p, sym->st_value);
2233 if (symaddr > addr || symaddr <= best)
2237 besterr = addr - symaddr;
2238 if (addr == symaddr)
2243 if (best && besterr > bestsym->st_size-1) {
2248 info->dli_fname = p->name;
2249 info->dli_fbase = p->map;
2252 info->dli_sname = 0;
2253 info->dli_saddr = 0;
2257 if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC)
2258 best = (size_t)(p->funcdescs + (bestsym - p->syms));
2259 info->dli_sname = strings + bestsym->st_name;
2260 info->dli_saddr = (void *)best;
2265 hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
2268 pthread_rwlock_rdlock(&lock);
2269 res = do_dlsym(p, s, ra);
2270 pthread_rwlock_unlock(&lock);
2274 hidden void *__dlsym_redir_time64(void *restrict p, const char *restrict s, void *restrict ra)
2277 const char *suffix, *suffix2 = "";
2280 /* Map the symbol name to a time64 version of itself according to the
2281 * pattern used for naming the redirected time64 symbols. */
2282 size_t l = strnlen(s, sizeof redir);
2283 if (l<4 || l==sizeof redir) goto no_redir;
2284 if (s[l-2]=='_' && s[l-1]=='r') {
2288 if (l<4) goto no_redir;
2289 if (!strcmp(s+l-4, "time")) suffix = "64";
2290 else suffix = "_time64";
2292 /* Use the presence of the remapped symbol name in libc to determine
2293 * whether it's one that requires time64 redirection; replace if so. */
2294 snprintf(redir, sizeof redir, "__%.*s%s%s", (int)l, s, suffix, suffix2);
2295 if (find_sym(&ldso, redir, 1).sym) s = redir;
2298 return __dlsym(p, s, ra);
2301 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
2303 struct dso *current;
2304 struct dl_phdr_info info;
2306 for(current = head; current;) {
2307 info.dlpi_addr = (uintptr_t)current->base;
2308 info.dlpi_name = current->name;
2309 info.dlpi_phdr = current->phdr;
2310 info.dlpi_phnum = current->phnum;
2311 info.dlpi_adds = gencnt;
2313 info.dlpi_tls_modid = current->tls_id;
2314 info.dlpi_tls_data = current->tls.image;
2316 ret = (callback)(&info, sizeof (info), data);
2318 if (ret != 0) break;
2320 pthread_rwlock_rdlock(&lock);
2321 current = current->next;
2322 pthread_rwlock_unlock(&lock);
2327 static void error(const char *fmt, ...)
2332 vdprintf(2, fmt, ap);
2338 __dl_vseterr(fmt, ap);