2 #define SYSCALL_NO_TLS 1
20 #include <semaphore.h>
21 #include <sys/membarrier.h>
22 #include "pthread_impl.h"
23 #include "fork_impl.h"
27 #define malloc __libc_malloc
28 #define calloc __libc_calloc
29 #define realloc __libc_realloc
30 #define free __libc_free
32 static void error_impl(const char *, ...);
33 static void error_noop(const char *, ...);
34 static void (*error)(const char *, ...) = error_noop;
36 #define MAXP2(a,b) (-(-(a)&-(b)))
37 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
39 #define container_of(p,t,m) ((t*)((char *)(p)-offsetof(t,m)))
40 #define countof(a) ((sizeof (a))/(sizeof (a)[0]))
52 struct td_index *next;
57 struct fdpic_loadmap *loadmap;
63 struct dso *next, *prev;
73 struct dso *syms_next, *lazy_next;
74 size_t *lazy, lazy_cnt;
85 struct dso **deps, *needed_by;
88 pthread_t ctor_visitor;
89 char *rpath_orig, *rpath;
90 struct tls_module tls;
92 size_t relro_start, relro_end;
94 unsigned char *new_tls;
95 struct td_index *td_index;
96 struct dso *fini_next;
101 struct fdpic_loadmap *loadmap;
116 typedef void (*stage3_func)(size_t *, size_t *);
118 static struct builtin_tls {
123 #define MIN_TLS_ALIGN offsetof(struct builtin_tls, pt)
125 #define ADDEND_LIMIT 4096
126 static size_t *saved_addends, *apply_addends_to;
128 static struct dso ldso;
129 static struct dso *head, *tail, *fini_head, *syms_tail, *lazy_head;
130 static char *env_path, *sys_path;
131 static unsigned long long gencnt;
134 static int ldso_fail;
136 static int shutting_down;
137 static jmp_buf *rtld_fail;
138 static pthread_rwlock_t lock;
139 static struct debug debug;
140 static struct tls_module *tls_tail;
141 static size_t tls_cnt, tls_offset, tls_align = MIN_TLS_ALIGN;
142 static size_t static_tls_cnt;
143 static pthread_mutex_t init_fini_lock;
144 static pthread_cond_t ctor_cond;
145 static struct dso *builtin_deps[2];
146 static struct dso *const no_deps[1];
147 static struct dso *builtin_ctor_queue[4];
148 static struct dso **main_ctor_queue;
149 static struct fdpic_loadmap *app_loadmap;
150 static struct fdpic_dummy_loadmap app_dummy_loadmap;
152 struct debug *_dl_debug_addr = &debug;
154 extern hidden int __malloc_replaced;
156 hidden void (*const __init_array_start)(void)=0, (*const __fini_array_start)(void)=0;
158 extern hidden void (*const __init_array_end)(void), (*const __fini_array_end)(void);
160 weak_alias(__init_array_start, __init_array_end);
161 weak_alias(__fini_array_start, __fini_array_end);
163 static int dl_strcmp(const char *l, const char *r)
165 for (; *l==*r && *l; l++, r++);
166 return *(unsigned char *)l - *(unsigned char *)r;
168 #define strcmp(l,r) dl_strcmp(l,r)
170 /* Compute load address for a virtual address in a given dso. */
172 static void *laddr(const struct dso *p, size_t v)
175 if (!p->loadmap) return p->base + v;
176 for (j=0; v-p->loadmap->segs[j].p_vaddr >= p->loadmap->segs[j].p_memsz; j++);
177 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
179 static void *laddr_pg(const struct dso *p, size_t v)
182 size_t pgsz = PAGE_SIZE;
183 if (!p->loadmap) return p->base + v;
185 size_t a = p->loadmap->segs[j].p_vaddr;
186 size_t b = a + p->loadmap->segs[j].p_memsz;
192 return (void *)(v - p->loadmap->segs[j].p_vaddr + p->loadmap->segs[j].addr);
194 static void (*fdbarrier(void *p))()
197 __asm__("" : "=r"(fd) : "0"(p));
200 #define fpaddr(p, v) fdbarrier((&(struct funcdesc){ \
201 laddr(p, v), (p)->got }))
203 #define laddr(p, v) (void *)((p)->base + (v))
204 #define laddr_pg(p, v) laddr(p, v)
205 #define fpaddr(p, v) ((void (*)())laddr(p, v))
208 static void decode_vec(size_t *v, size_t *a, size_t cnt)
211 for (i=0; i<cnt; i++) a[i] = 0;
212 for (; v[0]; v+=2) if (v[0]-1<cnt-1) {
218 static int search_vec(size_t *v, size_t *r, size_t key)
220 for (; v[0]!=key; v+=2)
226 static uint32_t sysv_hash(const char *s0)
228 const unsigned char *s = (void *)s0;
234 return h & 0xfffffff;
237 static uint32_t gnu_hash(const char *s0)
239 const unsigned char *s = (void *)s0;
240 uint_fast32_t h = 5381;
246 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
249 Sym *syms = dso->syms;
250 Elf_Symndx *hashtab = dso->hashtab;
251 char *strings = dso->strings;
252 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
253 if ((!dso->versym || dso->versym[i] >= 0)
254 && (!strcmp(s, strings+syms[i].st_name)))
260 static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s)
262 uint32_t nbuckets = hashtab[0];
263 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
264 uint32_t i = buckets[h1 % nbuckets];
268 uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
270 for (h1 |= 1; ; i++) {
271 uint32_t h2 = *hashval++;
272 if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0)
273 && !strcmp(s, dso->strings + dso->syms[i].st_name))
281 static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask)
283 const size_t *bloomwords = (const void *)(hashtab+4);
284 size_t f = bloomwords[fofs & (hashtab[2]-1)];
285 if (!(f & fmask)) return 0;
287 f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
288 if (!(f & 1)) return 0;
290 return gnu_lookup(h1, hashtab, dso, s);
293 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
294 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
296 #ifndef ARCH_SYM_REJECT_UND
297 #define ARCH_SYM_REJECT_UND(s) 0
300 #if defined(__GNUC__)
301 __attribute__((always_inline))
303 static inline struct symdef find_sym2(struct dso *dso, const char *s, int need_def, int use_deps)
305 uint32_t h = 0, gh = gnu_hash(s), gho = gh / (8*sizeof(size_t)), *ght;
306 size_t ghm = 1ul << gh % (8*sizeof(size_t));
307 struct symdef def = {0};
308 struct dso **deps = use_deps ? dso->deps : 0;
309 for (; dso; dso=use_deps ? *deps++ : dso->syms_next) {
311 if ((ght = dso->ghashtab)) {
312 sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
314 if (!h) h = sysv_hash(s);
315 sym = sysv_lookup(s, h, dso);
319 if (need_def || (sym->st_info&0xf) == STT_TLS
320 || ARCH_SYM_REJECT_UND(sym))
323 if ((sym->st_info&0xf) != STT_TLS)
325 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
326 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
334 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
336 return find_sym2(dso, s, need_def, 0);
339 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
341 unsigned char *base = dso->base;
342 Sym *syms = dso->syms;
343 char *strings = dso->strings;
354 int skip_relative = 0, reuse_addends = 0, save_slot = 0;
357 /* Only ldso's REL table needs addend saving/reuse. */
358 if (rel == apply_addends_to)
363 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
364 if (skip_relative && IS_RELATIVE(rel[1], dso->syms)) continue;
365 type = R_TYPE(rel[1]);
366 if (type == REL_NONE) continue;
367 reloc_addr = laddr(dso, rel[0]);
371 } else if (type==REL_GOT || type==REL_PLT|| type==REL_COPY) {
373 } else if (reuse_addends) {
374 /* Save original addend in stage 2 where the dso
375 * chain consists of just ldso; otherwise read back
376 * saved addend since the inline one was clobbered. */
378 saved_addends[save_slot] = *reloc_addr;
379 addend = saved_addends[save_slot++];
381 addend = *reloc_addr;
384 sym_index = R_SYM(rel[1]);
386 sym = syms + sym_index;
387 name = strings + sym->st_name;
388 ctx = type==REL_COPY ? head->syms_next : head;
389 def = (sym->st_info>>4) == STB_LOCAL
390 ? (struct symdef){ .dso = dso, .sym = sym }
391 : find_sym(ctx, name, type==REL_PLT);
392 if (!def.sym && (sym->st_shndx != SHN_UNDEF
393 || sym->st_info>>4 != STB_WEAK)) {
394 if (dso->lazy && (type==REL_PLT || type==REL_GOT)) {
395 dso->lazy[3*dso->lazy_cnt+0] = rel[0];
396 dso->lazy[3*dso->lazy_cnt+1] = rel[1];
397 dso->lazy[3*dso->lazy_cnt+2] = addend;
401 error("Error relocating %s: %s: symbol not found",
403 if (runtime) longjmp(*rtld_fail, 1);
412 sym_val = def.sym ? (size_t)laddr(def.dso, def.sym->st_value) : 0;
413 tls_val = def.sym ? def.sym->st_value : 0;
415 if ((type == REL_TPOFF || type == REL_TPOFF_NEG)
416 && def.dso->tls_id > static_tls_cnt) {
417 error("Error relocating %s: %s: initial-exec TLS "
418 "resolves to dynamic definition in %s",
419 dso->name, name, def.dso->name);
420 longjmp(*rtld_fail, 1);
425 addend -= (size_t)reloc_addr;
429 *reloc_addr = sym_val + addend;
432 memcpy(reloc_addr, &(size_t){sym_val + addend}, sizeof(size_t));
435 *reloc_addr = (size_t)base + addend;
438 if (sym) *reloc_addr = sym_val + addend;
439 else *reloc_addr = (size_t)base + addend;
442 memcpy(reloc_addr, (void *)sym_val, sym->st_size);
445 *(uint32_t *)reloc_addr = sym_val + addend
446 - (size_t)reloc_addr;
449 *reloc_addr = def.sym ? (size_t)(def.dso->funcdescs
450 + (def.sym - def.dso->syms)) : 0;
452 case REL_FUNCDESC_VAL:
453 if ((sym->st_info&0xf) == STT_SECTION) *reloc_addr += sym_val;
454 else *reloc_addr = sym_val;
455 reloc_addr[1] = def.sym ? (size_t)def.dso->got : 0;
458 *reloc_addr = def.dso->tls_id;
461 *reloc_addr = tls_val + addend - DTP_OFFSET;
465 *reloc_addr = tls_val + def.dso->tls.offset + TPOFF_K + addend;
469 *reloc_addr = tls_val - def.dso->tls.offset + addend;
472 *reloc_addr = def.dso->tls.offset - tls_val + addend;
476 if (stride<3) addend = reloc_addr[1];
477 if (def.dso->tls_id > static_tls_cnt) {
478 struct td_index *new = malloc(sizeof *new);
481 "Error relocating %s: cannot allocate TLSDESC for %s",
482 dso->name, sym ? name : "(local)" );
483 longjmp(*rtld_fail, 1);
485 new->next = dso->td_index;
487 new->args[0] = def.dso->tls_id;
488 new->args[1] = tls_val + addend - DTP_OFFSET;
489 reloc_addr[0] = (size_t)__tlsdesc_dynamic;
490 reloc_addr[1] = (size_t)new;
492 reloc_addr[0] = (size_t)__tlsdesc_static;
494 reloc_addr[1] = tls_val + def.dso->tls.offset
497 reloc_addr[1] = tls_val - def.dso->tls.offset
501 #ifdef TLSDESC_BACKWARDS
502 /* Some archs (32-bit ARM at least) invert the order of
503 * the descriptor members. Fix them up here. */
504 size_t tmp = reloc_addr[0];
505 reloc_addr[0] = reloc_addr[1];
510 error("Error relocating %s: unsupported relocation type %d",
512 if (runtime) longjmp(*rtld_fail, 1);
518 static void redo_lazy_relocs()
520 struct dso *p = lazy_head, *next;
524 size_t size = p->lazy_cnt*3*sizeof(size_t);
526 do_relocs(p, p->lazy, size, 3);
528 p->lazy_next = lazy_head;
538 /* A huge hack: to make up for the wastefulness of shared libraries
539 * needing at least a page of dirty memory even if they have no global
540 * data, we reclaim the gaps at the beginning and end of writable maps
541 * and "donate" them to the heap. */
543 static void reclaim(struct dso *dso, size_t start, size_t end)
545 if (start >= dso->relro_start && start < dso->relro_end) start = dso->relro_end;
546 if (end >= dso->relro_start && end < dso->relro_end) end = dso->relro_start;
547 if (start >= end) return;
548 char *base = laddr_pg(dso, start);
549 __malloc_donate(base, base+(end-start));
552 static void reclaim_gaps(struct dso *dso)
554 Phdr *ph = dso->phdr;
555 size_t phcnt = dso->phnum;
557 for (; phcnt--; ph=(void *)((char *)ph+dso->phentsize)) {
558 if (ph->p_type!=PT_LOAD) continue;
559 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
560 reclaim(dso, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
561 reclaim(dso, ph->p_vaddr+ph->p_memsz,
562 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
566 static ssize_t read_loop(int fd, void *p, size_t n)
568 for (size_t i=0; i<n; ) {
569 ssize_t l = read(fd, (char *)p+i, n-i);
571 if (errno==EINTR) continue;
580 static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
582 static int no_map_fixed;
586 q = mmap(p, n, prot, flags|MAP_FIXED, fd, off);
587 if (!DL_NOMMU_SUPPORT || q != MAP_FAILED || errno != EINVAL)
591 /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
592 if (flags & MAP_ANONYMOUS) {
597 if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
598 for (q=p; n; q+=r, off+=r, n-=r) {
600 if (r < 0 && errno != EINTR) return MAP_FAILED;
609 static void unmap_library(struct dso *dso)
613 for (i=0; i<dso->loadmap->nsegs; i++) {
614 if (!dso->loadmap->segs[i].p_memsz)
616 munmap((void *)dso->loadmap->segs[i].addr,
617 dso->loadmap->segs[i].p_memsz);
620 } else if (dso->map && dso->map_len) {
621 munmap(dso->map, dso->map_len);
625 static void *map_library(int fd, struct dso *dso)
627 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
628 void *allocated_buf=0;
630 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
631 size_t this_min, this_max;
637 unsigned char *map=MAP_FAILED, *base;
642 ssize_t l = read(fd, buf, sizeof buf);
645 if (l<sizeof *eh || (eh->e_type != ET_DYN && eh->e_type != ET_EXEC))
647 phsize = eh->e_phentsize * eh->e_phnum;
648 if (phsize > sizeof buf - sizeof *eh) {
649 allocated_buf = malloc(phsize);
650 if (!allocated_buf) return 0;
651 l = pread(fd, allocated_buf, phsize, eh->e_phoff);
652 if (l < 0) goto error;
653 if (l != phsize) goto noexec;
654 ph = ph0 = allocated_buf;
655 } else if (eh->e_phoff + phsize > l) {
656 l = pread(fd, buf+1, phsize, eh->e_phoff);
657 if (l < 0) goto error;
658 if (l != phsize) goto noexec;
659 ph = ph0 = (void *)(buf + 1);
661 ph = ph0 = (void *)((char *)buf + eh->e_phoff);
663 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
664 if (ph->p_type == PT_DYNAMIC) {
666 } else if (ph->p_type == PT_TLS) {
667 tls_image = ph->p_vaddr;
668 dso->tls.align = ph->p_align;
669 dso->tls.len = ph->p_filesz;
670 dso->tls.size = ph->p_memsz;
671 } else if (ph->p_type == PT_GNU_RELRO) {
672 dso->relro_start = ph->p_vaddr & -PAGE_SIZE;
673 dso->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
674 } else if (ph->p_type == PT_GNU_STACK) {
675 if (!runtime && ph->p_memsz > __default_stacksize) {
676 __default_stacksize =
677 ph->p_memsz < DEFAULT_STACK_MAX ?
678 ph->p_memsz : DEFAULT_STACK_MAX;
681 if (ph->p_type != PT_LOAD) continue;
683 if (ph->p_vaddr < addr_min) {
684 addr_min = ph->p_vaddr;
685 off_start = ph->p_offset;
686 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
687 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
688 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
690 if (ph->p_vaddr+ph->p_memsz > addr_max) {
691 addr_max = ph->p_vaddr+ph->p_memsz;
694 if (!dyn) goto noexec;
695 if (DL_FDPIC && !(eh->e_flags & FDPIC_CONSTDISP_FLAG)) {
696 dso->loadmap = calloc(1, sizeof *dso->loadmap
697 + nsegs * sizeof *dso->loadmap->segs);
698 if (!dso->loadmap) goto error;
699 dso->loadmap->nsegs = nsegs;
700 for (ph=ph0, i=0; i<nsegs; ph=(void *)((char *)ph+eh->e_phentsize)) {
701 if (ph->p_type != PT_LOAD) continue;
702 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
703 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
704 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
705 map = mmap(0, ph->p_memsz + (ph->p_vaddr & PAGE_SIZE-1),
707 fd, ph->p_offset & -PAGE_SIZE);
708 if (map == MAP_FAILED) {
712 dso->loadmap->segs[i].addr = (size_t)map +
713 (ph->p_vaddr & PAGE_SIZE-1);
714 dso->loadmap->segs[i].p_vaddr = ph->p_vaddr;
715 dso->loadmap->segs[i].p_memsz = ph->p_memsz;
717 if (prot & PROT_WRITE) {
718 size_t brk = (ph->p_vaddr & PAGE_SIZE-1)
720 size_t pgbrk = brk + PAGE_SIZE-1 & -PAGE_SIZE;
721 size_t pgend = brk + ph->p_memsz - ph->p_filesz
722 + PAGE_SIZE-1 & -PAGE_SIZE;
723 if (pgend > pgbrk && mmap_fixed(map+pgbrk,
725 MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS,
726 -1, off_start) == MAP_FAILED)
728 memset(map + brk, 0, pgbrk-brk);
731 map = (void *)dso->loadmap->segs[0].addr;
735 addr_max += PAGE_SIZE-1;
736 addr_max &= -PAGE_SIZE;
737 addr_min &= -PAGE_SIZE;
738 off_start &= -PAGE_SIZE;
739 map_len = addr_max - addr_min + off_start;
740 /* The first time, we map too much, possibly even more than
741 * the length of the file. This is okay because we will not
742 * use the invalid part; we just need to reserve the right
743 * amount of virtual address space to map over later. */
744 map = DL_NOMMU_SUPPORT
745 ? mmap((void *)addr_min, map_len, PROT_READ|PROT_WRITE|PROT_EXEC,
746 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
747 : mmap((void *)addr_min, map_len, prot,
748 MAP_PRIVATE, fd, off_start);
749 if (map==MAP_FAILED) goto error;
751 dso->map_len = map_len;
752 /* If the loaded file is not relocatable and the requested address is
753 * not available, then the load operation must fail. */
754 if (eh->e_type != ET_DYN && addr_min && map!=(void *)addr_min) {
758 base = map - addr_min;
761 for (ph=ph0, i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
762 if (ph->p_type != PT_LOAD) continue;
763 /* Check if the programs headers are in this load segment, and
764 * if so, record the address for use by dl_iterate_phdr. */
765 if (!dso->phdr && eh->e_phoff >= ph->p_offset
766 && eh->e_phoff+phsize <= ph->p_offset+ph->p_filesz) {
767 dso->phdr = (void *)(base + ph->p_vaddr
768 + (eh->e_phoff-ph->p_offset));
769 dso->phnum = eh->e_phnum;
770 dso->phentsize = eh->e_phentsize;
772 this_min = ph->p_vaddr & -PAGE_SIZE;
773 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
774 off_start = ph->p_offset & -PAGE_SIZE;
775 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
776 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
777 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
778 /* Reuse the existing mapping for the lowest-address LOAD */
779 if ((ph->p_vaddr & -PAGE_SIZE) != addr_min || DL_NOMMU_SUPPORT)
780 if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
782 if (ph->p_memsz > ph->p_filesz && (ph->p_flags&PF_W)) {
783 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
784 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
785 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
786 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)
790 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
791 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
792 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
799 dso->dynv = laddr(dso, dyn);
800 if (dso->tls.size) dso->tls.image = laddr(dso, tls_image);
806 if (map!=MAP_FAILED) unmap_library(dso);
811 static int path_open(const char *name, const char *s, char *buf, size_t buf_size)
816 s += strspn(s, ":\n");
817 l = strcspn(s, ":\n");
818 if (l-1 >= INT_MAX) return -1;
819 if (snprintf(buf, buf_size, "%.*s/%s", (int)l, s, name) < buf_size) {
820 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
828 /* Any negative value but -1 will inhibit
829 * futher path search. */
837 static int fixup_rpath(struct dso *p, char *buf, size_t buf_size)
840 const char *s, *t, *origin;
842 if (p->rpath || !p->rpath_orig) return 0;
843 if (!strchr(p->rpath_orig, '$')) {
844 p->rpath = p->rpath_orig;
849 while ((t=strchr(s, '$'))) {
850 if (strncmp(t, "$ORIGIN", 7) && strncmp(t, "${ORIGIN}", 9))
855 if (n > SSIZE_MAX/PATH_MAX) return 0;
857 if (p->kernel_mapped) {
858 /* $ORIGIN searches cannot be performed for the main program
859 * when it is suid/sgid/AT_SECURE. This is because the
860 * pathname is under the control of the caller of execve.
861 * For libraries, however, $ORIGIN can be processed safely
862 * since the library's pathname came from a trusted source
863 * (either system paths or a call to dlopen). */
866 l = readlink("/proc/self/exe", buf, buf_size);
867 if (l == -1) switch (errno) {
882 t = strrchr(origin, '/');
886 /* Normally p->name will always be an absolute or relative
887 * pathname containing at least one '/' character, but in the
888 * case where ldso was invoked as a command to execute a
889 * program in the working directory, app.name may not. Fix. */
893 /* Disallow non-absolute origins for suid/sgid/AT_SECURE. */
894 if (libc.secure && *origin != '/')
896 p->rpath = malloc(strlen(p->rpath_orig) + n*l + 1);
897 if (!p->rpath) return -1;
901 while ((t=strchr(s, '$'))) {
904 memcpy(d, origin, l);
906 /* It was determined previously that the '$' is followed
907 * either by "ORIGIN" or "{ORIGIN}". */
908 s = t + 7 + 2*(t[1]=='{');
914 static void decode_dyn(struct dso *p)
917 decode_vec(p->dynv, dyn, DYN_CNT);
918 p->syms = laddr(p, dyn[DT_SYMTAB]);
919 p->strings = laddr(p, dyn[DT_STRTAB]);
920 if (dyn[0]&(1<<DT_HASH))
921 p->hashtab = laddr(p, dyn[DT_HASH]);
922 if (dyn[0]&(1<<DT_RPATH))
923 p->rpath_orig = p->strings + dyn[DT_RPATH];
924 if (dyn[0]&(1<<DT_RUNPATH))
925 p->rpath_orig = p->strings + dyn[DT_RUNPATH];
926 if (dyn[0]&(1<<DT_PLTGOT))
927 p->got = laddr(p, dyn[DT_PLTGOT]);
928 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
929 p->ghashtab = laddr(p, *dyn);
930 if (search_vec(p->dynv, dyn, DT_VERSYM))
931 p->versym = laddr(p, *dyn);
934 static size_t count_syms(struct dso *p)
936 if (p->hashtab) return p->hashtab[1];
939 uint32_t *buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
941 for (i = nsym = 0; i < p->ghashtab[0]; i++) {
942 if (buckets[i] > nsym)
946 hashval = buckets + p->ghashtab[0] + (nsym - p->ghashtab[1]);
948 while (!(*hashval++ & 1));
953 static void *dl_mmap(size_t n)
956 int prot = PROT_READ|PROT_WRITE, flags = MAP_ANONYMOUS|MAP_PRIVATE;
958 p = (void *)__syscall(SYS_mmap2, 0, n, prot, flags, -1, 0);
960 p = (void *)__syscall(SYS_mmap, 0, n, prot, flags, -1, 0);
962 return (unsigned long)p > -4096UL ? 0 : p;
965 static void makefuncdescs(struct dso *p)
967 static int self_done;
968 size_t nsym = count_syms(p);
969 size_t i, size = nsym * sizeof(*p->funcdescs);
972 p->funcdescs = dl_mmap(size);
975 p->funcdescs = malloc(size);
978 if (!runtime) a_crash();
979 error("Error allocating function descriptors for %s", p->name);
980 longjmp(*rtld_fail, 1);
982 for (i=0; i<nsym; i++) {
983 if ((p->syms[i].st_info&0xf)==STT_FUNC && p->syms[i].st_shndx) {
984 p->funcdescs[i].addr = laddr(p, p->syms[i].st_value);
985 p->funcdescs[i].got = p->got;
987 p->funcdescs[i].addr = 0;
988 p->funcdescs[i].got = 0;
993 static struct dso *load_library(const char *name, struct dso *needed_by)
995 char buf[2*NAME_MAX+2];
996 const char *pathname;
998 struct dso *p, temp_dso = {0};
1010 /* Catch and block attempts to reload the implementation itself */
1011 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
1012 static const char reserved[] =
1013 "c.pthread.rt.m.dl.util.xnet.";
1014 const char *rp, *next;
1015 for (rp=reserved; *rp; rp=next) {
1016 next = strchr(rp, '.') + 1;
1017 if (strncmp(name+3, rp, next-rp) == 0)
1022 /* Track which names have been resolved
1023 * and only report each one once. */
1024 static unsigned reported;
1025 unsigned mask = 1U<<(rp-reserved);
1026 if (!(reported & mask)) {
1028 dprintf(1, "\t%s => %s (%p)\n",
1036 if (!strcmp(name, ldso.name)) is_self = 1;
1045 if (strchr(name, '/')) {
1047 fd = open(name, O_RDONLY|O_CLOEXEC);
1049 /* Search for the name to see if it's already loaded */
1050 for (p=head->next; p; p=p->next) {
1051 if (p->shortname && !strcmp(p->shortname, name)) {
1055 if (strlen(name) > NAME_MAX) return 0;
1057 if (env_path) fd = path_open(name, env_path, buf, sizeof buf);
1058 for (p=needed_by; fd == -1 && p; p=p->needed_by) {
1059 if (fixup_rpath(p, buf, sizeof buf) < 0)
1060 fd = -2; /* Inhibit further search. */
1062 fd = path_open(name, p->rpath, buf, sizeof buf);
1068 if (ldso.name[0]=='/') {
1070 for (s=t=z=ldso.name; *s; s++)
1071 if (*s=='/') z=t, t=s;
1072 prefix_len = z-ldso.name;
1073 if (prefix_len < PATH_MAX)
1080 char etc_ldso_path[prefix_len + 1
1081 + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
1082 snprintf(etc_ldso_path, sizeof etc_ldso_path,
1083 "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
1084 (int)prefix_len, prefix);
1085 fd = open(etc_ldso_path, O_RDONLY|O_CLOEXEC);
1088 if (!fstat(fd, &st)) n = st.st_size;
1089 if ((sys_path = malloc(n+1)))
1091 if (!sys_path || read_loop(fd, sys_path, n)<0) {
1096 } else if (errno != ENOENT) {
1100 if (!sys_path) sys_path = "/lib:/usr/local/lib:/usr/lib";
1101 fd = path_open(name, sys_path, buf, sizeof buf);
1105 if (fd < 0) return 0;
1106 if (fstat(fd, &st) < 0) {
1110 for (p=head->next; p; p=p->next) {
1111 if (p->dev == st.st_dev && p->ino == st.st_ino) {
1112 /* If this library was previously loaded with a
1113 * pathname but a search found the same inode,
1114 * setup its shortname so it can be found by name. */
1115 if (!p->shortname && pathname != name)
1116 p->shortname = strrchr(p->name, '/')+1;
1121 map = noload ? 0 : map_library(fd, &temp_dso);
1125 /* Avoid the danger of getting two versions of libc mapped into the
1126 * same process when an absolute pathname was used. The symbols
1127 * checked are chosen to catch both musl and glibc, and to avoid
1128 * false positives from interposition-hack libraries. */
1129 decode_dyn(&temp_dso);
1130 if (find_sym(&temp_dso, "__libc_start_main", 1).sym &&
1131 find_sym(&temp_dso, "stdin", 1).sym) {
1132 unmap_library(&temp_dso);
1133 return load_library("libc.so", needed_by);
1135 /* Past this point, if we haven't reached runtime yet, ldso has
1136 * committed either to use the mapped library or to abort execution.
1137 * Unmapping is not possible, so we can safely reclaim gaps. */
1138 if (!runtime) reclaim_gaps(&temp_dso);
1140 /* Allocate storage for the new DSO. When there is TLS, this
1141 * storage must include a reservation for all pre-existing
1142 * threads to obtain copies of both the new TLS, and an
1143 * extended DTV capable of storing an additional slot for
1144 * the newly-loaded DSO. */
1145 alloc_size = sizeof *p + strlen(pathname) + 1;
1146 if (runtime && temp_dso.tls.image) {
1147 size_t per_th = temp_dso.tls.size + temp_dso.tls.align
1148 + sizeof(void *) * (tls_cnt+3);
1149 n_th = libc.threads_minus_1 + 1;
1150 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
1151 else alloc_size += n_th * per_th;
1153 p = calloc(1, alloc_size);
1155 unmap_library(&temp_dso);
1158 memcpy(p, &temp_dso, sizeof temp_dso);
1161 p->needed_by = needed_by;
1163 p->runtime_loaded = runtime;
1164 strcpy(p->name, pathname);
1165 /* Add a shortname only if name arg was not an explicit pathname. */
1166 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
1168 p->tls_id = ++tls_cnt;
1169 tls_align = MAXP2(tls_align, p->tls.align);
1171 p->tls.offset = tls_offset + ( (p->tls.align-1) &
1172 (-tls_offset + (uintptr_t)p->tls.image) );
1173 tls_offset = p->tls.offset + p->tls.size;
1175 tls_offset += p->tls.size + p->tls.align - 1;
1176 tls_offset -= (tls_offset + (uintptr_t)p->tls.image)
1178 p->tls.offset = tls_offset;
1180 p->new_dtv = (void *)(-sizeof(size_t) &
1181 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
1182 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
1183 if (tls_tail) tls_tail->next = &p->tls;
1184 else libc.tls_head = &p->tls;
1192 if (DL_FDPIC) makefuncdescs(p);
1194 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, p->base);
1199 static void load_direct_deps(struct dso *p)
1203 if (p->deps) return;
1204 /* For head, all preloads are direct pseudo-dependencies.
1205 * Count and include them now to avoid realloc later. */
1206 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1208 for (i=0; p->dynv[i]; i+=2)
1209 if (p->dynv[i] == DT_NEEDED) cnt++;
1210 /* Use builtin buffer for apps with no external deps, to
1211 * preserve property of no runtime failure paths. */
1212 p->deps = (p==head && cnt<2) ? builtin_deps :
1213 calloc(cnt+1, sizeof *p->deps);
1215 error("Error loading dependencies for %s", p->name);
1216 if (runtime) longjmp(*rtld_fail, 1);
1219 if (p==head) for (struct dso *q=p->next; q; q=q->next)
1221 for (i=0; p->dynv[i]; i+=2) {
1222 if (p->dynv[i] != DT_NEEDED) continue;
1223 struct dso *dep = load_library(p->strings + p->dynv[i+1], p);
1225 error("Error loading shared library %s: %m (needed by %s)",
1226 p->strings + p->dynv[i+1], p->name);
1227 if (runtime) longjmp(*rtld_fail, 1);
1230 p->deps[cnt++] = dep;
1233 p->ndeps_direct = cnt;
1236 static void load_deps(struct dso *p)
1238 if (p->deps) return;
1239 for (; p; p=p->next)
1240 load_direct_deps(p);
1243 static void extend_bfs_deps(struct dso *p)
1245 size_t i, j, cnt, ndeps_all;
1248 /* Can't use realloc if the original p->deps was allocated at
1249 * program entry and malloc has been replaced, or if it's
1250 * the builtin non-allocated trivial main program deps array. */
1251 int no_realloc = (__malloc_replaced && !p->runtime_loaded)
1252 || p->deps == builtin_deps;
1254 if (p->bfs_built) return;
1255 ndeps_all = p->ndeps_direct;
1257 /* Mark existing (direct) deps so they won't be duplicated. */
1258 for (i=0; p->deps[i]; i++)
1259 p->deps[i]->mark = 1;
1261 /* For each dependency already in the list, copy its list of direct
1262 * dependencies to the list, excluding any items already in the
1263 * list. Note that the list this loop iterates over will grow during
1264 * the loop, but since duplicates are excluded, growth is bounded. */
1265 for (i=0; p->deps[i]; i++) {
1266 struct dso *dep = p->deps[i];
1267 for (j=cnt=0; j<dep->ndeps_direct; j++)
1268 if (!dep->deps[j]->mark) cnt++;
1270 malloc(sizeof(*tmp) * (ndeps_all+cnt+1)) :
1271 realloc(p->deps, sizeof(*tmp) * (ndeps_all+cnt+1));
1273 error("Error recording dependencies for %s", p->name);
1274 if (runtime) longjmp(*rtld_fail, 1);
1278 memcpy(tmp, p->deps, sizeof(*tmp) * (ndeps_all+1));
1282 for (j=0; j<dep->ndeps_direct; j++) {
1283 if (dep->deps[j]->mark) continue;
1284 dep->deps[j]->mark = 1;
1285 p->deps[ndeps_all++] = dep->deps[j];
1287 p->deps[ndeps_all] = 0;
1290 for (p=head; p; p=p->next)
1294 static void load_preload(char *s)
1298 for (z=s; *z; s=z) {
1299 for ( ; *s && (isspace(*s) || *s==':'); s++);
1300 for (z=s; *z && !isspace(*z) && *z!=':'; z++);
1308 static void add_syms(struct dso *p)
1310 if (!p->syms_next && syms_tail != p) {
1311 syms_tail->syms_next = p;
1316 static void revert_syms(struct dso *old_tail)
1318 struct dso *p, *next;
1319 /* Chop off the tail of the list of dsos that participate in
1320 * the global symbol table, reverting them to RTLD_LOCAL. */
1321 for (p=old_tail; p; p=next) {
1322 next = p->syms_next;
1325 syms_tail = old_tail;
1328 static void do_mips_relocs(struct dso *p, size_t *got)
1330 size_t i, j, rel[2];
1331 unsigned char *base = p->base;
1332 i=0; search_vec(p->dynv, &i, DT_MIPS_LOCAL_GOTNO);
1336 while (i--) *got++ += (size_t)base;
1338 j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
1339 i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
1340 Sym *sym = p->syms + j;
1341 rel[0] = (unsigned char *)got - base;
1342 for (i-=j; i; i--, sym++, rel[0]+=sizeof(size_t)) {
1343 rel[1] = R_INFO(sym-p->syms, R_MIPS_JUMP_SLOT);
1344 do_relocs(p, rel, sizeof rel, 2);
1348 static void reloc_all(struct dso *p)
1350 size_t dyn[DYN_CNT];
1351 for (; p; p=p->next) {
1352 if (p->relocated) continue;
1353 decode_vec(p->dynv, dyn, DYN_CNT);
1354 if (NEED_MIPS_GOT_RELOCS)
1355 do_mips_relocs(p, laddr(p, dyn[DT_PLTGOT]));
1356 do_relocs(p, laddr(p, dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
1357 2+(dyn[DT_PLTREL]==DT_RELA));
1358 do_relocs(p, laddr(p, dyn[DT_REL]), dyn[DT_RELSZ], 2);
1359 do_relocs(p, laddr(p, dyn[DT_RELA]), dyn[DT_RELASZ], 3);
1361 if (head != &ldso && p->relro_start != p->relro_end) {
1362 long ret = __syscall(SYS_mprotect, laddr(p, p->relro_start),
1363 p->relro_end-p->relro_start, PROT_READ);
1364 if (ret != 0 && ret != -ENOSYS) {
1365 error("Error relocating %s: RELRO protection failed: %m",
1367 if (runtime) longjmp(*rtld_fail, 1);
1375 static void kernel_mapped_dso(struct dso *p)
1377 size_t min_addr = -1, max_addr = 0, cnt;
1379 for (cnt = p->phnum; cnt--; ph = (void *)((char *)ph + p->phentsize)) {
1380 if (ph->p_type == PT_DYNAMIC) {
1381 p->dynv = laddr(p, ph->p_vaddr);
1382 } else if (ph->p_type == PT_GNU_RELRO) {
1383 p->relro_start = ph->p_vaddr & -PAGE_SIZE;
1384 p->relro_end = (ph->p_vaddr + ph->p_memsz) & -PAGE_SIZE;
1385 } else if (ph->p_type == PT_GNU_STACK) {
1386 if (!runtime && ph->p_memsz > __default_stacksize) {
1387 __default_stacksize =
1388 ph->p_memsz < DEFAULT_STACK_MAX ?
1389 ph->p_memsz : DEFAULT_STACK_MAX;
1392 if (ph->p_type != PT_LOAD) continue;
1393 if (ph->p_vaddr < min_addr)
1394 min_addr = ph->p_vaddr;
1395 if (ph->p_vaddr+ph->p_memsz > max_addr)
1396 max_addr = ph->p_vaddr+ph->p_memsz;
1398 min_addr &= -PAGE_SIZE;
1399 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
1400 p->map = p->base + min_addr;
1401 p->map_len = max_addr - min_addr;
1402 p->kernel_mapped = 1;
1405 void __libc_exit_fini()
1408 size_t dyn[DYN_CNT];
1409 pthread_t self = __pthread_self();
1411 /* Take both locks before setting shutting_down, so that
1412 * either lock is sufficient to read its value. The lock
1413 * order matches that in dlopen to avoid deadlock. */
1414 pthread_rwlock_wrlock(&lock);
1415 pthread_mutex_lock(&init_fini_lock);
1417 pthread_rwlock_unlock(&lock);
1418 for (p=fini_head; p; p=p->fini_next) {
1419 while (p->ctor_visitor && p->ctor_visitor!=self)
1420 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1421 if (!p->constructed) continue;
1422 decode_vec(p->dynv, dyn, DYN_CNT);
1423 if (dyn[0] & (1<<DT_FINI_ARRAY)) {
1424 size_t n = dyn[DT_FINI_ARRAYSZ]/sizeof(size_t);
1425 size_t *fn = (size_t *)laddr(p, dyn[DT_FINI_ARRAY])+n;
1426 while (n--) ((void (*)(void))*--fn)();
1428 #ifndef NO_LEGACY_INITFINI
1429 if ((dyn[0] & (1<<DT_FINI)) && dyn[DT_FINI])
1430 fpaddr(p, dyn[DT_FINI])();
1435 void __ldso_atfork(int who)
1438 pthread_rwlock_wrlock(&lock);
1439 pthread_mutex_lock(&init_fini_lock);
1441 pthread_mutex_unlock(&init_fini_lock);
1442 pthread_rwlock_unlock(&lock);
1446 static struct dso **queue_ctors(struct dso *dso)
1448 size_t cnt, qpos, spos, i;
1449 struct dso *p, **queue, **stack;
1451 if (ldd_mode) return 0;
1453 /* Bound on queue size is the total number of indirect deps.
1454 * If a bfs deps list was built, we can use it. Otherwise,
1455 * bound by the total number of DSOs, which is always safe and
1456 * is reasonable we use it (for main app at startup). */
1457 if (dso->bfs_built) {
1458 for (cnt=0; dso->deps[cnt]; cnt++)
1459 dso->deps[cnt]->mark = 0;
1460 cnt++; /* self, not included in deps */
1462 for (cnt=0, p=head; p; cnt++, p=p->next)
1465 cnt++; /* termination slot */
1466 if (dso==head && cnt <= countof(builtin_ctor_queue))
1467 queue = builtin_ctor_queue;
1469 queue = calloc(cnt, sizeof *queue);
1472 error("Error allocating constructor queue: %m\n");
1473 if (runtime) longjmp(*rtld_fail, 1);
1477 /* Opposite ends of the allocated buffer serve as an output queue
1478 * and a working stack. Setup initial stack with just the argument
1479 * dso and initial queue empty... */
1483 stack[--spos] = dso;
1487 /* Then perform pseudo-DFS sort, but ignoring circular deps. */
1490 while (p->next_dep < p->ndeps_direct) {
1491 if (p->deps[p->next_dep]->mark) {
1495 p = p->deps[p->next_dep];
1503 for (i=0; i<qpos; i++) queue[i]->mark = 0;
1504 for (i=0; i<qpos; i++)
1505 if (queue[i]->ctor_visitor && queue[i]->ctor_visitor->tid < 0) {
1506 error("State of %s is inconsistent due to multithreaded fork\n",
1509 if (runtime) longjmp(*rtld_fail, 1);
1515 static void do_init_fini(struct dso **queue)
1518 size_t dyn[DYN_CNT], i;
1519 pthread_t self = __pthread_self();
1521 pthread_mutex_lock(&init_fini_lock);
1522 for (i=0; (p=queue[i]); i++) {
1523 while ((p->ctor_visitor && p->ctor_visitor!=self) || shutting_down)
1524 pthread_cond_wait(&ctor_cond, &init_fini_lock);
1525 if (p->ctor_visitor || p->constructed)
1527 p->ctor_visitor = self;
1529 decode_vec(p->dynv, dyn, DYN_CNT);
1530 if (dyn[0] & ((1<<DT_FINI) | (1<<DT_FINI_ARRAY))) {
1531 p->fini_next = fini_head;
1535 pthread_mutex_unlock(&init_fini_lock);
1537 #ifndef NO_LEGACY_INITFINI
1538 if ((dyn[0] & (1<<DT_INIT)) && dyn[DT_INIT])
1539 fpaddr(p, dyn[DT_INIT])();
1541 if (dyn[0] & (1<<DT_INIT_ARRAY)) {
1542 size_t n = dyn[DT_INIT_ARRAYSZ]/sizeof(size_t);
1543 size_t *fn = laddr(p, dyn[DT_INIT_ARRAY]);
1544 while (n--) ((void (*)(void))*fn++)();
1547 pthread_mutex_lock(&init_fini_lock);
1548 p->ctor_visitor = 0;
1550 pthread_cond_broadcast(&ctor_cond);
1552 pthread_mutex_unlock(&init_fini_lock);
1555 void __libc_start_init(void)
1557 do_init_fini(main_ctor_queue);
1558 if (!__malloc_replaced && main_ctor_queue != builtin_ctor_queue)
1559 free(main_ctor_queue);
1560 main_ctor_queue = 0;
1563 static void dl_debug_state(void)
1567 weak_alias(dl_debug_state, _dl_debug_state);
1569 void __init_tls(size_t *auxv)
1573 static void update_tls_size()
1575 libc.tls_cnt = tls_cnt;
1576 libc.tls_align = tls_align;
1577 libc.tls_size = ALIGN(
1578 (1+tls_cnt) * sizeof(void *) +
1580 sizeof(struct pthread) +
1585 static void install_new_tls(void)
1588 pthread_t self = __pthread_self(), td;
1589 struct dso *dtv_provider = container_of(tls_tail, struct dso, tls);
1590 uintptr_t (*newdtv)[tls_cnt+1] = (void *)dtv_provider->new_dtv;
1593 size_t old_cnt = self->dtv[0];
1595 __block_app_sigs(&set);
1597 /* Copy existing dtv contents from all existing threads. */
1598 for (i=0, td=self; !i || td!=self; i++, td=td->next) {
1599 memcpy(newdtv+i, td->dtv,
1600 (old_cnt+1)*sizeof(uintptr_t));
1601 newdtv[i][0] = tls_cnt;
1603 /* Install new dtls into the enlarged, uninstalled dtv copies. */
1604 for (p=head; ; p=p->next) {
1605 if (p->tls_id <= old_cnt) continue;
1606 unsigned char *mem = p->new_tls;
1607 for (j=0; j<i; j++) {
1608 unsigned char *new = mem;
1609 new += ((uintptr_t)p->tls.image - (uintptr_t)mem)
1611 memcpy(new, p->tls.image, p->tls.len);
1612 newdtv[j][p->tls_id] =
1613 (uintptr_t)new + DTP_OFFSET;
1614 mem += p->tls.size + p->tls.align;
1616 if (p->tls_id == tls_cnt) break;
1619 /* Broadcast barrier to ensure contents of new dtv is visible
1620 * if the new dtv pointer is. The __membarrier function has a
1621 * fallback emulation using signals for kernels that lack the
1622 * feature at the syscall level. */
1624 __membarrier(MEMBARRIER_CMD_PRIVATE_EXPEDITED, 0);
1626 /* Install new dtv for each thread. */
1627 for (j=0, td=self; !j || td!=self; j++, td=td->next) {
1628 td->dtv = newdtv[j];
1632 __restore_sigs(&set);
1635 /* Stage 1 of the dynamic linker is defined in dlstart.c. It calls the
1636 * following stage 2 and stage 3 functions via primitive symbolic lookup
1637 * since it does not have access to their addresses to begin with. */
1639 /* Stage 2 of the dynamic linker is called after relative relocations
1640 * have been processed. It can make function calls to static functions
1641 * and access string literals and static data, but cannot use extern
1642 * symbols. Its job is to perform symbolic relocations on the dynamic
1643 * linker itself, but some of the relocations performed may need to be
1644 * replaced later due to copy relocations in the main program. */
1646 hidden void __dls2(unsigned char *base, size_t *sp)
1649 for (auxv=sp+1+*sp+1; *auxv; auxv++);
1652 void *p1 = (void *)sp[-2];
1653 void *p2 = (void *)sp[-1];
1655 size_t aux[AUX_CNT];
1656 decode_vec(auxv, aux, AUX_CNT);
1657 if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE];
1658 else ldso.base = (void *)(aux[AT_PHDR] & -4096);
1660 app_loadmap = p2 ? p1 : 0;
1661 ldso.loadmap = p2 ? p2 : p1;
1662 ldso.base = laddr(&ldso, 0);
1666 Ehdr *ehdr = (void *)ldso.base;
1667 ldso.name = ldso.shortname = "libc.so";
1668 ldso.phnum = ehdr->e_phnum;
1669 ldso.phdr = laddr(&ldso, ehdr->e_phoff);
1670 ldso.phentsize = ehdr->e_phentsize;
1671 kernel_mapped_dso(&ldso);
1674 if (DL_FDPIC) makefuncdescs(&ldso);
1676 /* Prepare storage for to save clobbered REL addends so they
1677 * can be reused in stage 3. There should be very few. If
1678 * something goes wrong and there are a huge number, abort
1679 * instead of risking stack overflow. */
1680 size_t dyn[DYN_CNT];
1681 decode_vec(ldso.dynv, dyn, DYN_CNT);
1682 size_t *rel = laddr(&ldso, dyn[DT_REL]);
1683 size_t rel_size = dyn[DT_RELSZ];
1684 size_t symbolic_rel_cnt = 0;
1685 apply_addends_to = rel;
1686 for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t))
1687 if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++;
1688 if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash();
1689 size_t addends[symbolic_rel_cnt+1];
1690 saved_addends = addends;
1697 /* Call dynamic linker stage-2b, __dls2b, looking it up
1698 * symbolically as a barrier against moving the address
1699 * load across the above relocation processing. */
1700 struct symdef dls2b_def = find_sym(&ldso, "__dls2b", 0);
1701 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls2b_def.sym-ldso.syms])(sp, auxv);
1702 else ((stage3_func)laddr(&ldso, dls2b_def.sym->st_value))(sp, auxv);
1705 /* Stage 2b sets up a valid thread pointer, which requires relocations
1706 * completed in stage 2, and on which stage 3 is permitted to depend.
1707 * This is done as a separate stage, with symbolic lookup as a barrier,
1708 * so that loads of the thread pointer and &errno can be pure/const and
1709 * thereby hoistable. */
1711 void __dls2b(size_t *sp, size_t *auxv)
1713 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1714 * use during dynamic linking. If possible it will also serve as the
1715 * thread pointer at runtime. */
1716 search_vec(auxv, &__hwcap, AT_HWCAP);
1718 libc.tls_size = sizeof builtin_tls;
1719 libc.tls_align = tls_align;
1720 if (__init_tp(__copy_tls((void *)builtin_tls)) < 0) {
1724 struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
1725 if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp, auxv);
1726 else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp, auxv);
1729 /* Stage 3 of the dynamic linker is called with the dynamic linker/libc
1730 * fully functional. Its job is to load (if not already loaded) and
1731 * process dependencies and relocations for the main application and
1732 * transfer control to its entry point. */
1734 void __dls3(size_t *sp, size_t *auxv)
1736 static struct dso app, vdso;
1737 size_t aux[AUX_CNT];
1739 char *env_preload=0;
1740 char *replace_argv0=0;
1743 char **argv = (void *)(sp+1);
1744 char **argv_orig = argv;
1745 char **envp = argv+argc+1;
1747 /* Find aux vector just past environ[] and use it to initialize
1748 * global data that may be needed before we can make syscalls. */
1750 decode_vec(auxv, aux, AUX_CNT);
1751 search_vec(auxv, &__sysinfo, AT_SYSINFO);
1752 __pthread_self()->sysinfo = __sysinfo;
1753 libc.page_size = aux[AT_PAGESZ];
1754 libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1755 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
1757 /* Only trust user/env if kernel says we're not suid/sgid */
1759 env_path = getenv("LD_LIBRARY_PATH");
1760 env_preload = getenv("LD_PRELOAD");
1763 /* Activate error handler function */
1766 /* If the main program was already loaded by the kernel,
1767 * AT_PHDR will point to some location other than the dynamic
1768 * linker's program headers. */
1769 if (aux[AT_PHDR] != (size_t)ldso.phdr) {
1770 size_t interp_off = 0;
1771 size_t tls_image = 0;
1772 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
1773 Phdr *phdr = app.phdr = (void *)aux[AT_PHDR];
1774 app.phnum = aux[AT_PHNUM];
1775 app.phentsize = aux[AT_PHENT];
1776 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
1777 if (phdr->p_type == PT_PHDR)
1778 app.base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
1779 else if (phdr->p_type == PT_INTERP)
1780 interp_off = (size_t)phdr->p_vaddr;
1781 else if (phdr->p_type == PT_TLS) {
1782 tls_image = phdr->p_vaddr;
1783 app.tls.len = phdr->p_filesz;
1784 app.tls.size = phdr->p_memsz;
1785 app.tls.align = phdr->p_align;
1788 if (DL_FDPIC) app.loadmap = app_loadmap;
1789 if (app.tls.size) app.tls.image = laddr(&app, tls_image);
1790 if (interp_off) ldso.name = laddr(&app, interp_off);
1791 if ((aux[0] & (1UL<<AT_EXECFN))
1792 && strncmp((char *)aux[AT_EXECFN], "/proc/", 6))
1793 app.name = (char *)aux[AT_EXECFN];
1796 kernel_mapped_dso(&app);
1799 char *ldname = argv[0];
1800 size_t l = strlen(ldname);
1801 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
1803 while (argv[0] && argv[0][0]=='-' && argv[0][1]=='-') {
1804 char *opt = argv[0]+2;
1805 *argv++ = (void *)-1;
1808 } else if (!memcmp(opt, "list", 5)) {
1810 } else if (!memcmp(opt, "library-path", 12)) {
1811 if (opt[12]=='=') env_path = opt+13;
1812 else if (opt[12]) *argv = 0;
1813 else if (*argv) env_path = *argv++;
1814 } else if (!memcmp(opt, "preload", 7)) {
1815 if (opt[7]=='=') env_preload = opt+8;
1816 else if (opt[7]) *argv = 0;
1817 else if (*argv) env_preload = *argv++;
1818 } else if (!memcmp(opt, "argv0", 5)) {
1819 if (opt[5]=='=') replace_argv0 = opt+6;
1820 else if (opt[5]) *argv = 0;
1821 else if (*argv) replace_argv0 = *argv++;
1826 argv[-1] = (void *)(argc - (argv-argv_orig));
1828 dprintf(2, "musl libc (" LDSO_ARCH ")\n"
1830 "Dynamic Program Loader\n"
1831 "Usage: %s [options] [--] pathname%s\n",
1832 __libc_version, ldname,
1833 ldd_mode ? "" : " [args]");
1836 fd = open(argv[0], O_RDONLY);
1838 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
1841 Ehdr *ehdr = map_library(fd, &app);
1843 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
1849 aux[AT_ENTRY] = (size_t)laddr(&app, ehdr->e_entry);
1850 /* Find the name that would have been used for the dynamic
1851 * linker had ldd not taken its place. */
1853 for (i=0; i<app.phnum; i++) {
1854 if (app.phdr[i].p_type == PT_INTERP)
1855 ldso.name = laddr(&app, app.phdr[i].p_vaddr);
1857 dprintf(1, "\t%s (%p)\n", ldso.name, ldso.base);
1861 libc.tls_head = tls_tail = &app.tls;
1862 app.tls_id = tls_cnt = 1;
1864 app.tls.offset = GAP_ABOVE_TP;
1865 app.tls.offset += (-GAP_ABOVE_TP + (uintptr_t)app.tls.image)
1866 & (app.tls.align-1);
1867 tls_offset = app.tls.offset + app.tls.size;
1869 tls_offset = app.tls.offset = app.tls.size
1870 + ( -((uintptr_t)app.tls.image + app.tls.size)
1871 & (app.tls.align-1) );
1873 tls_align = MAXP2(tls_align, app.tls.align);
1877 makefuncdescs(&app);
1879 app.loadmap = (void *)&app_dummy_loadmap;
1880 app.loadmap->nsegs = 1;
1881 app.loadmap->segs[0].addr = (size_t)app.map;
1882 app.loadmap->segs[0].p_vaddr = (size_t)app.map
1884 app.loadmap->segs[0].p_memsz = app.map_len;
1886 argv[-3] = (void *)app.loadmap;
1889 /* Initial dso chain consists only of the app. */
1890 head = tail = syms_tail = &app;
1892 /* Donate unused parts of app and library mapping to malloc */
1894 reclaim_gaps(&ldso);
1896 /* Load preload/needed libraries, add symbols to global namespace. */
1897 ldso.deps = (struct dso **)no_deps;
1898 if (env_preload) load_preload(env_preload);
1900 for (struct dso *p=head; p; p=p->next)
1903 /* Attach to vdso, if provided by the kernel, last so that it does
1904 * not become part of the global namespace. */
1905 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR) && vdso_base) {
1906 Ehdr *ehdr = (void *)vdso_base;
1907 Phdr *phdr = vdso.phdr = (void *)(vdso_base + ehdr->e_phoff);
1908 vdso.phnum = ehdr->e_phnum;
1909 vdso.phentsize = ehdr->e_phentsize;
1910 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
1911 if (phdr->p_type == PT_DYNAMIC)
1912 vdso.dynv = (void *)(vdso_base + phdr->p_offset);
1913 if (phdr->p_type == PT_LOAD)
1914 vdso.base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
1917 vdso.shortname = "linux-gate.so.1";
1919 vdso.deps = (struct dso **)no_deps;
1926 for (i=0; app.dynv[i]; i+=2) {
1927 if (!DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG)
1928 app.dynv[i+1] = (size_t)&debug;
1929 if (DT_DEBUG_INDIRECT && app.dynv[i]==DT_DEBUG_INDIRECT) {
1930 size_t *ptr = (size_t *) app.dynv[i+1];
1931 *ptr = (size_t)&debug;
1935 /* This must be done before final relocations, since it calls
1936 * malloc, which may be provided by the application. Calling any
1937 * application code prior to the jump to its entry point is not
1938 * valid in our model and does not work with FDPIC, where there
1939 * are additional relocation-like fixups that only the entry point
1940 * code can see to perform. */
1941 main_ctor_queue = queue_ctors(&app);
1943 /* Initial TLS must also be allocated before final relocations
1944 * might result in calloc being a call to application code. */
1946 void *initial_tls = builtin_tls;
1947 if (libc.tls_size > sizeof builtin_tls || tls_align > MIN_TLS_ALIGN) {
1948 initial_tls = calloc(libc.tls_size, 1);
1950 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
1951 argv[0], libc.tls_size);
1955 static_tls_cnt = tls_cnt;
1957 /* The main program must be relocated LAST since it may contain
1958 * copy relocations which depend on libraries' relocations. */
1959 reloc_all(app.next);
1962 /* Actual copying to new TLS needs to happen after relocations,
1963 * since the TLS images might have contained relocated addresses. */
1964 if (initial_tls != builtin_tls) {
1965 if (__init_tp(__copy_tls(initial_tls)) < 0) {
1969 size_t tmp_tls_size = libc.tls_size;
1970 pthread_t self = __pthread_self();
1971 /* Temporarily set the tls size to the full size of
1972 * builtin_tls so that __copy_tls will use the same layout
1973 * as it did for before. Then check, just to be safe. */
1974 libc.tls_size = sizeof builtin_tls;
1975 if (__copy_tls((void*)builtin_tls) != self) a_crash();
1976 libc.tls_size = tmp_tls_size;
1979 if (ldso_fail) _exit(127);
1980 if (ldd_mode) _exit(0);
1982 /* Determine if malloc was interposed by a replacement implementation
1983 * so that calloc and the memalign family can harden against the
1984 * possibility of incomplete replacement. */
1985 if (find_sym(head, "malloc", 1).dso != &ldso)
1986 __malloc_replaced = 1;
1987 if (find_sym(head, "aligned_alloc", 1).dso != &ldso)
1988 __aligned_alloc_replaced = 1;
1990 /* Switch to runtime mode: any further failures in the dynamic
1991 * linker are a reportable failure rather than a fatal startup
1996 debug.bp = dl_debug_state;
1998 debug.base = ldso.base;
1999 debug.state = RT_CONSISTENT;
2002 if (replace_argv0) argv[0] = replace_argv0;
2006 CRTJMP((void *)aux[AT_ENTRY], argv-1);
2010 static void prepare_lazy(struct dso *p)
2012 size_t dyn[DYN_CNT], n, flags1=0;
2013 decode_vec(p->dynv, dyn, DYN_CNT);
2014 search_vec(p->dynv, &flags1, DT_FLAGS_1);
2015 if (dyn[DT_BIND_NOW] || (dyn[DT_FLAGS] & DF_BIND_NOW) || (flags1 & DF_1_NOW))
2017 n = dyn[DT_RELSZ]/2 + dyn[DT_RELASZ]/3 + dyn[DT_PLTRELSZ]/2 + 1;
2018 if (NEED_MIPS_GOT_RELOCS) {
2019 size_t j=0; search_vec(p->dynv, &j, DT_MIPS_GOTSYM);
2020 size_t i=0; search_vec(p->dynv, &i, DT_MIPS_SYMTABNO);
2023 p->lazy = calloc(n, 3*sizeof(size_t));
2025 error("Error preparing lazy relocation for %s: %m", p->name);
2026 longjmp(*rtld_fail, 1);
2028 p->lazy_next = lazy_head;
2032 void *dlopen(const char *file, int mode)
2034 struct dso *volatile p, *orig_tail, *orig_syms_tail, *orig_lazy_head, *next;
2035 struct tls_module *orig_tls_tail;
2036 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
2040 struct dso **volatile ctor_queue = 0;
2042 if (!file) return head;
2044 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
2045 pthread_rwlock_wrlock(&lock);
2048 debug.state = RT_ADD;
2052 if (shutting_down) {
2053 error("Cannot dlopen while program is exiting.");
2056 orig_tls_tail = tls_tail;
2057 orig_tls_cnt = tls_cnt;
2058 orig_tls_offset = tls_offset;
2059 orig_tls_align = tls_align;
2060 orig_lazy_head = lazy_head;
2061 orig_syms_tail = syms_tail;
2063 noload = mode & RTLD_NOLOAD;
2066 if (setjmp(*rtld_fail)) {
2067 /* Clean up anything new that was (partially) loaded */
2068 revert_syms(orig_syms_tail);
2069 for (p=orig_tail->next; p; p=next) {
2071 while (p->td_index) {
2072 void *tmp = p->td_index->next;
2077 if (p->rpath != p->rpath_orig)
2085 if (!orig_tls_tail) libc.tls_head = 0;
2086 tls_tail = orig_tls_tail;
2087 if (tls_tail) tls_tail->next = 0;
2088 tls_cnt = orig_tls_cnt;
2089 tls_offset = orig_tls_offset;
2090 tls_align = orig_tls_align;
2091 lazy_head = orig_lazy_head;
2096 } else p = load_library(file, head);
2100 "Library %s is not already loaded" :
2101 "Error loading shared library %s: %m",
2106 /* First load handling */
2109 pthread_mutex_lock(&init_fini_lock);
2110 int constructed = p->constructed;
2111 pthread_mutex_unlock(&init_fini_lock);
2112 if (!constructed) ctor_queue = queue_ctors(p);
2113 if (!p->relocated && (mode & RTLD_LAZY)) {
2115 for (i=0; p->deps[i]; i++)
2116 if (!p->deps[i]->relocated)
2117 prepare_lazy(p->deps[i]);
2119 if (!p->relocated || (mode & RTLD_GLOBAL)) {
2120 /* Make new symbols global, at least temporarily, so we can do
2121 * relocations. If not RTLD_GLOBAL, this is reverted below. */
2123 for (i=0; p->deps[i]; i++)
2124 add_syms(p->deps[i]);
2126 if (!p->relocated) {
2130 /* If RTLD_GLOBAL was not specified, undo any new additions
2131 * to the global symbol table. This is a nop if the library was
2132 * previously loaded and already global. */
2133 if (!(mode & RTLD_GLOBAL))
2134 revert_syms(orig_syms_tail);
2136 /* Processing of deferred lazy relocations must not happen until
2137 * the new libraries are committed; otherwise we could end up with
2138 * relocations resolved to symbol definitions that get removed. */
2142 if (tls_cnt != orig_tls_cnt)
2146 debug.state = RT_CONSISTENT;
2150 pthread_rwlock_unlock(&lock);
2152 do_init_fini(ctor_queue);
2155 pthread_setcancelstate(cs, 0);
2159 hidden int __dl_invalid_handle(void *h)
2162 for (p=head; p; p=p->next) if (h==p) return 0;
2163 error("Invalid library handle %p", (void *)h);
2167 static void *addr2dso(size_t a)
2171 if (DL_FDPIC) for (p=head; p; p=p->next) {
2173 if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs))
2176 for (p=head; p; p=p->next) {
2177 if (DL_FDPIC && p->loadmap) {
2178 for (i=0; i<p->loadmap->nsegs; i++) {
2179 if (a-p->loadmap->segs[i].p_vaddr
2180 < p->loadmap->segs[i].p_memsz)
2185 size_t phcnt = p->phnum;
2186 size_t entsz = p->phentsize;
2187 size_t base = (size_t)p->base;
2188 for (; phcnt--; ph=(void *)((char *)ph+entsz)) {
2189 if (ph->p_type != PT_LOAD) continue;
2190 if (a-base-ph->p_vaddr < ph->p_memsz)
2193 if (a-(size_t)p->map < p->map_len)
2200 static void *do_dlsym(struct dso *p, const char *s, void *ra)
2203 if (p == head || p == RTLD_DEFAULT) {
2205 } else if (p == RTLD_NEXT) {
2206 p = addr2dso((size_t)ra);
2209 } else if (__dl_invalid_handle(p)) {
2213 struct symdef def = find_sym2(p, s, 0, use_deps);
2215 error("Symbol not found: %s", s);
2218 if ((def.sym->st_info&0xf) == STT_TLS)
2219 return __tls_get_addr((tls_mod_off_t []){def.dso->tls_id, def.sym->st_value-DTP_OFFSET});
2220 if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC)
2221 return def.dso->funcdescs + (def.sym - def.dso->syms);
2222 return laddr(def.dso, def.sym->st_value);
2225 int dladdr(const void *addr_arg, Dl_info *info)
2227 size_t addr = (size_t)addr_arg;
2233 size_t besterr = -1;
2235 pthread_rwlock_rdlock(&lock);
2237 pthread_rwlock_unlock(&lock);
2242 strings = p->strings;
2243 nsym = count_syms(p);
2246 size_t idx = (addr-(size_t)p->funcdescs)
2247 / sizeof(*p->funcdescs);
2248 if (idx < nsym && (sym[idx].st_info&0xf) == STT_FUNC) {
2249 best = (size_t)(p->funcdescs + idx);
2250 bestsym = sym + idx;
2255 if (!best) for (; nsym; nsym--, sym++) {
2257 && (1<<(sym->st_info&0xf) & OK_TYPES)
2258 && (1<<(sym->st_info>>4) & OK_BINDS)) {
2259 size_t symaddr = (size_t)laddr(p, sym->st_value);
2260 if (symaddr > addr || symaddr <= best)
2264 besterr = addr - symaddr;
2265 if (addr == symaddr)
2270 if (best && besterr > bestsym->st_size-1) {
2275 info->dli_fname = p->name;
2276 info->dli_fbase = p->map;
2279 info->dli_sname = 0;
2280 info->dli_saddr = 0;
2284 if (DL_FDPIC && (bestsym->st_info&0xf) == STT_FUNC)
2285 best = (size_t)(p->funcdescs + (bestsym - p->syms));
2286 info->dli_sname = strings + bestsym->st_name;
2287 info->dli_saddr = (void *)best;
2292 hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
2295 pthread_rwlock_rdlock(&lock);
2296 res = do_dlsym(p, s, ra);
2297 pthread_rwlock_unlock(&lock);
2301 hidden void *__dlsym_redir_time64(void *restrict p, const char *restrict s, void *restrict ra)
2304 const char *suffix, *suffix2 = "";
2307 /* Map the symbol name to a time64 version of itself according to the
2308 * pattern used for naming the redirected time64 symbols. */
2309 size_t l = strnlen(s, sizeof redir);
2310 if (l<4 || l==sizeof redir) goto no_redir;
2311 if (s[l-2]=='_' && s[l-1]=='r') {
2315 if (l<4) goto no_redir;
2316 if (!strcmp(s+l-4, "time")) suffix = "64";
2317 else suffix = "_time64";
2319 /* Use the presence of the remapped symbol name in libc to determine
2320 * whether it's one that requires time64 redirection; replace if so. */
2321 snprintf(redir, sizeof redir, "__%.*s%s%s", (int)l, s, suffix, suffix2);
2322 if (find_sym(&ldso, redir, 1).sym) s = redir;
2325 return __dlsym(p, s, ra);
2328 int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
2330 struct dso *current;
2331 struct dl_phdr_info info;
2333 for(current = head; current;) {
2334 info.dlpi_addr = (uintptr_t)current->base;
2335 info.dlpi_name = current->name;
2336 info.dlpi_phdr = current->phdr;
2337 info.dlpi_phnum = current->phnum;
2338 info.dlpi_adds = gencnt;
2340 info.dlpi_tls_modid = current->tls_id;
2341 info.dlpi_tls_data = !current->tls_id ? 0 :
2342 __tls_get_addr((tls_mod_off_t[]){current->tls_id,0});
2344 ret = (callback)(&info, sizeof (info), data);
2346 if (ret != 0) break;
2348 pthread_rwlock_rdlock(&lock);
2349 current = current->next;
2350 pthread_rwlock_unlock(&lock);
2355 static void error_impl(const char *fmt, ...)
2360 vdprintf(2, fmt, ap);
2366 __dl_vseterr(fmt, ap);
2370 static void error_noop(const char *fmt, ...)