20 #include "pthread_impl.h"
24 static char errbuf[128];
28 #if ULONG_MAX == 0xffffffff
29 typedef Elf32_Ehdr Ehdr;
30 typedef Elf32_Phdr Phdr;
31 typedef Elf32_Sym Sym;
32 #define R_TYPE(x) ((x)&255)
33 #define R_SYM(x) ((x)>>8)
35 typedef Elf64_Ehdr Ehdr;
36 typedef Elf64_Phdr Phdr;
37 typedef Elf64_Sym Sym;
38 #define R_TYPE(x) ((x)&0xffffffff)
39 #define R_SYM(x) ((x)>>32)
42 #define MAXP2(a,b) (-(-(a)&-(b)))
43 #define ALIGN(x,y) ((x)+(y)-1 & -(y))
57 struct dso *next, *prev;
73 size_t tls_len, tls_size, tls_align, tls_id, tls_offset;
75 unsigned char *new_tls;
76 int new_dtv_idx, new_tls_idx;
77 struct dso *fini_next;
89 void __init_ssp(size_t *);
90 void *__install_initial_tls(void *);
92 static struct dso *head, *tail, *ldso, *fini_head;
93 static char *env_path, *sys_path, *r_path;
98 static jmp_buf rtld_fail;
99 static pthread_rwlock_t lock;
100 static struct debug debug;
102 static size_t tls_cnt, tls_offset, tls_align = 4*sizeof(size_t);
103 static pthread_mutex_t init_fini_lock = { ._m_type = PTHREAD_MUTEX_RECURSIVE };
105 struct debug *_dl_debug_addr = &debug;
110 static void decode_vec(size_t *v, size_t *a, size_t cnt)
112 memset(a, 0, cnt*sizeof(size_t));
113 for (; v[0]; v+=2) if (v[0]<cnt) {
119 static int search_vec(size_t *v, size_t *r, size_t key)
121 for (; v[0]!=key; v+=2)
127 static uint32_t sysv_hash(const char *s0)
129 const unsigned char *s = (void *)s0;
135 return h & 0xfffffff;
138 static uint32_t gnu_hash(const char *s0)
140 const unsigned char *s = (void *)s0;
141 uint_fast32_t h = 5381;
147 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
150 Sym *syms = dso->syms;
151 uint32_t *hashtab = dso->hashtab;
152 char *strings = dso->strings;
153 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
154 if (!strcmp(s, strings+syms[i].st_name))
160 static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
164 uint32_t *hashtab = dso->ghashtab;
165 uint32_t nbuckets = hashtab[0];
166 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
169 uint32_t n = buckets[h1 % nbuckets];
173 strings = dso->strings;
175 hashval = buckets + nbuckets + (n - hashtab[1]);
177 for (h1 |= 1; ; sym++) {
179 if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name))
187 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
188 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
190 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
192 uint32_t h = 0, gh = 0;
193 struct symdef def = {0};
196 if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
199 if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
201 for (; dso; dso=dso->next) {
203 if (!dso->global) continue;
205 if (!gh) gh = gnu_hash(s);
206 sym = gnu_lookup(s, gh, dso);
208 if (!h) h = sysv_hash(s);
209 sym = sysv_lookup(s, h, dso);
213 if (need_def || (sym->st_info&0xf) == STT_TLS)
216 if ((sym->st_info&0xf) != STT_TLS)
218 if (!(1<<(sym->st_info&0xf) & OK_TYPES)) continue;
219 if (!(1<<(sym->st_info>>4) & OK_BINDS)) continue;
221 if (def.sym && sym->st_info>>4 == STB_WEAK) continue;
224 if (sym->st_info>>4 == STB_GLOBAL) break;
229 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
231 unsigned char *base = dso->base;
232 Sym *syms = dso->syms;
233 char *strings = dso->strings;
241 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
242 type = R_TYPE(rel[1]);
243 sym_index = R_SYM(rel[1]);
245 sym = syms + sym_index;
246 name = strings + sym->st_name;
247 ctx = IS_COPY(type) ? head->next : head;
248 def = find_sym(ctx, name, IS_PLT(type));
249 if (!def.sym && sym->st_info>>4 != STB_WEAK) {
250 snprintf(errbuf, sizeof errbuf,
251 "Error relocating %s: %s: symbol not found",
253 if (runtime) longjmp(rtld_fail, 1);
254 dprintf(2, "%s\n", errbuf);
263 do_single_reloc(dso, base, (void *)(base + rel[0]), type,
264 stride>2 ? rel[2] : 0, sym, sym?sym->st_size:0, def,
265 def.sym?(size_t)(def.dso->base+def.sym->st_value):0);
269 /* A huge hack: to make up for the wastefulness of shared libraries
270 * needing at least a page of dirty memory even if they have no global
271 * data, we reclaim the gaps at the beginning and end of writable maps
272 * and "donate" them to the heap by setting up minimal malloc
273 * structures and then freeing them. */
275 static void reclaim(unsigned char *base, size_t start, size_t end)
278 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
279 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
280 if (start>end || end-start < 4*sizeof(size_t)) return;
281 a = (size_t *)(base + start);
282 z = (size_t *)(base + end);
284 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
289 static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
291 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
292 if (ph->p_type!=PT_LOAD) continue;
293 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
294 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
295 reclaim(base, ph->p_vaddr+ph->p_memsz,
296 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
300 static void *map_library(int fd, struct dso *dso)
302 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
304 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
305 size_t this_min, this_max;
310 unsigned char *map, *base;
315 ssize_t l = read(fd, buf, sizeof buf);
316 if (l<sizeof *eh) return 0;
318 phsize = eh->e_phentsize * eh->e_phnum;
319 if (phsize + sizeof *eh > l) return 0;
320 if (eh->e_phoff + phsize > l) {
321 l = pread(fd, buf+1, phsize, eh->e_phoff);
322 if (l != phsize) return 0;
323 eh->e_phoff = sizeof *eh;
325 ph = (void *)((char *)buf + eh->e_phoff);
326 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
327 if (ph->p_type == PT_DYNAMIC)
329 if (ph->p_type == PT_TLS) {
330 tls_image = ph->p_vaddr;
331 dso->tls_align = ph->p_align;
332 dso->tls_len = ph->p_filesz;
333 dso->tls_size = ph->p_memsz;
335 if (ph->p_type != PT_LOAD) continue;
336 if (ph->p_vaddr < addr_min) {
337 addr_min = ph->p_vaddr;
338 off_start = ph->p_offset;
339 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
340 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
341 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
343 if (ph->p_vaddr+ph->p_memsz > addr_max) {
344 addr_max = ph->p_vaddr+ph->p_memsz;
348 addr_max += PAGE_SIZE-1;
349 addr_max &= -PAGE_SIZE;
350 addr_min &= -PAGE_SIZE;
351 off_start &= -PAGE_SIZE;
352 map_len = addr_max - addr_min + off_start;
353 /* The first time, we map too much, possibly even more than
354 * the length of the file. This is okay because we will not
355 * use the invalid part; we just need to reserve the right
356 * amount of virtual address space to map over later. */
357 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
358 if (map==MAP_FAILED) return 0;
359 base = map - addr_min;
360 ph = (void *)((char *)buf + eh->e_phoff);
361 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
362 if (ph->p_type != PT_LOAD) continue;
363 /* Reuse the existing mapping for the lowest-address LOAD */
364 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
365 this_min = ph->p_vaddr & -PAGE_SIZE;
366 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
367 off_start = ph->p_offset & -PAGE_SIZE;
368 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
369 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
370 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
371 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
373 if (ph->p_memsz > ph->p_filesz) {
374 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
375 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
376 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
377 if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
381 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
382 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
383 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
387 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
388 eh->e_phentsize, eh->e_phnum);
390 dso->map_len = map_len;
392 dso->dynv = (void *)(base+dyn);
393 if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
396 munmap(map, map_len);
400 static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
402 const char *s=search, *z;
408 l = z ? z-s : strlen(s);
409 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
410 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
415 static void decode_dyn(struct dso *p)
417 size_t dyn[DYN_CNT] = {0};
418 decode_vec(p->dynv, dyn, DYN_CNT);
419 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
420 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
421 if (dyn[0]&(1<<DT_HASH))
422 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
423 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
424 p->ghashtab = (void *)(p->base + *dyn);
427 static struct dso *load_library(const char *name)
429 char buf[2*NAME_MAX+2];
430 const char *pathname;
431 unsigned char *base, *map;
432 size_t dyno, map_len;
433 struct dso *p, temp_dso = {0};
439 /* Catch and block attempts to reload the implementation itself */
440 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
441 static const char *rp, reserved[] =
442 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
443 char *z = strchr(name, '.');
446 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
451 tail = ldso->next ? ldso->next : ldso;
457 if (strchr(name, '/')) {
459 fd = open(name, O_RDONLY|O_CLOEXEC);
461 /* Search for the name to see if it's already loaded */
462 for (p=head->next; p; p=p->next) {
463 if (p->shortname && !strcmp(p->shortname, name)) {
468 if (strlen(name) > NAME_MAX) return 0;
470 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
471 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
474 FILE *f = fopen(ETC_LDSO_PATH, "rbe");
476 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
477 sys_path[strlen(sys_path)-1]=0;
481 if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf);
482 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf);
486 if (fd < 0) return 0;
487 if (fstat(fd, &st) < 0) {
491 for (p=head->next; p; p=p->next) {
492 if (p->dev == st.st_dev && p->ino == st.st_ino) {
493 /* If this library was previously loaded with a
494 * pathname but a search found the same inode,
495 * setup its shortname so it can be found by name. */
496 if (!p->shortname && pathname != name)
497 p->shortname = strrchr(p->name, '/')+1;
503 map = map_library(fd, &temp_dso);
507 /* Allocate storage for the new DSO. When there is TLS, this
508 * storage must include a reservation for all pre-existing
509 * threads to obtain copies of both the new TLS, and an
510 * extended DTV capable of storing an additional slot for
511 * the newly-loaded DSO. */
512 alloc_size = sizeof *p + strlen(pathname) + 1;
513 if (runtime && temp_dso.tls_image) {
514 size_t per_th = temp_dso.tls_size + temp_dso.tls_align
515 + sizeof(void *) * (tls_cnt+3);
516 n_th = libc.threads_minus_1 + 1;
517 if (n_th > SSIZE_MAX / per_th) alloc_size = SIZE_MAX;
518 else alloc_size += n_th * per_th;
520 p = calloc(1, alloc_size);
522 munmap(map, map_len);
525 memcpy(p, &temp_dso, sizeof temp_dso);
531 strcpy(p->name, pathname);
532 /* Add a shortname only if name arg was not an explicit pathname. */
533 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
535 if (runtime && !__pthread_self_init()) {
537 munmap(map, map_len);
540 p->tls_id = ++tls_cnt;
541 tls_align = MAXP2(tls_align, p->tls_align);
542 tls_offset += p->tls_size + p->tls_align - 1;
543 tls_offset -= (tls_offset + (uintptr_t)p->tls_image)
545 p->tls_offset = tls_offset;
546 p->new_dtv = (void *)(-sizeof(size_t) &
547 (uintptr_t)(p->name+strlen(p->name)+sizeof(size_t)));
548 p->new_tls = (void *)(p->new_dtv + n_th*(tls_cnt+1));
555 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base);
560 static void load_deps(struct dso *p)
563 struct dso ***deps = &p->deps, **tmp, *dep;
564 for (; p; p=p->next) {
565 for (i=0; p->dynv[i]; i+=2) {
566 if (p->dynv[i] != DT_RPATH) continue;
567 r_path = (void *)(p->strings + p->dynv[i+1]);
569 for (i=0; p->dynv[i]; i+=2) {
570 if (p->dynv[i] != DT_NEEDED) continue;
571 dep = load_library(p->strings + p->dynv[i+1]);
573 snprintf(errbuf, sizeof errbuf,
574 "Error loading shared library %s: %m (needed by %s)",
575 p->strings + p->dynv[i+1], p->name);
576 if (runtime) longjmp(rtld_fail, 1);
577 dprintf(2, "%s\n", errbuf);
582 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
583 if (!tmp) longjmp(rtld_fail, 1);
593 static void load_preload(char *s)
598 for ( ; *s && isspace(*s); s++);
599 for (z=s; *z && !isspace(*z); z++);
607 static void make_global(struct dso *p)
609 for (; p; p=p->next) p->global = 1;
612 static void reloc_all(struct dso *p)
614 size_t dyn[DYN_CNT] = {0};
615 for (; p; p=p->next) {
616 if (p->relocated) continue;
617 decode_vec(p->dynv, dyn, DYN_CNT);
618 #ifdef NEED_ARCH_RELOCS
619 do_arch_relocs(p, head);
621 do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
622 2+(dyn[DT_PLTREL]==DT_RELA));
623 do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2);
624 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
629 static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
631 for (; cnt--; ph = (void *)((char *)ph + stride))
632 if (ph->p_type == PT_DYNAMIC)
637 static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p)
639 size_t min_addr = -1, max_addr = 0;
640 for (; cnt--; ph = (void *)((char *)ph + stride)) {
641 if (ph->p_type != PT_LOAD) continue;
642 if (ph->p_vaddr < min_addr)
643 min_addr = ph->p_vaddr;
644 if (ph->p_vaddr+ph->p_memsz > max_addr)
645 max_addr = ph->p_vaddr+ph->p_memsz;
647 min_addr &= -PAGE_SIZE;
648 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
649 p->map = p->base + min_addr;
650 p->map_len = max_addr - min_addr;
653 static void do_fini()
656 size_t dyn[DYN_CNT] = {0};
657 for (p=fini_head; p; p=p->fini_next) {
658 if (!p->constructed) continue;
659 decode_vec(p->dynv, dyn, DYN_CNT);
660 ((void (*)(void))(p->base + dyn[DT_FINI]))();
664 static void do_init_fini(struct dso *p)
666 size_t dyn[DYN_CNT] = {0};
667 int need_locking = libc.threads_minus_1;
668 /* Allow recursive calls that arise when a library calls
669 * dlopen from one of its constructors, but block any
670 * other threads until all ctors have finished. */
671 if (need_locking) pthread_mutex_lock(&init_fini_lock);
672 for (; p; p=p->prev) {
673 if (p->constructed) continue;
675 decode_vec(p->dynv, dyn, DYN_CNT);
676 if (dyn[0] & (1<<DT_FINI)) {
677 p->fini_next = fini_head;
680 if (dyn[0] & (1<<DT_INIT))
681 ((void (*)(void))(p->base + dyn[DT_INIT]))();
683 if (need_locking) pthread_mutex_unlock(&init_fini_lock);
686 void _dl_debug_state(void)
690 void *__copy_tls(unsigned char *mem)
695 if (!tls_cnt) return mem;
697 void **dtv = (void *)mem;
698 dtv[0] = (void *)tls_cnt;
700 mem += libc.tls_size - sizeof(struct pthread);
701 mem -= (uintptr_t)mem & (tls_align-1);
704 for (p=head; p; p=p->next) {
705 if (!p->tls_id) continue;
706 dtv[p->tls_id] = mem - p->tls_offset;
707 memcpy(dtv[p->tls_id], p->tls_image, p->tls_len);
713 void *__tls_get_addr(size_t *v)
715 pthread_t self = __pthread_self();
716 if (self->dtv && v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]])
717 return (char *)self->dtv[v[0]]+v[1];
719 /* Block signals to make accessing new TLS async-signal-safe */
721 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &set);
722 if (self->dtv && v[0]<=(size_t)self->dtv[0] && self->dtv[v[0]]) {
723 pthread_sigmask(SIG_SETMASK, &set, 0);
724 return (char *)self->dtv[v[0]]+v[1];
727 /* This is safe without any locks held because, if the caller
728 * is able to request the Nth entry of the DTV, the DSO list
729 * must be valid at least that far out and it was synchronized
730 * at program startup or by an already-completed call to dlopen. */
732 for (p=head; p->tls_id != v[0]; p=p->next);
734 /* Get new DTV space from new DSO if needed */
735 if (!self->dtv || v[0] > (size_t)self->dtv[0]) {
736 void **newdtv = p->new_dtv +
737 (v[0]+1)*sizeof(void *)*a_fetch_add(&p->new_dtv_idx,1);
738 if (self->dtv) memcpy(newdtv, self->dtv,
739 ((size_t)self->dtv[0]+1) * sizeof(void *));
740 newdtv[0] = (void *)v[0];
744 /* Get new TLS memory from new DSO */
745 unsigned char *mem = p->new_tls +
746 (p->tls_size + p->tls_align) * a_fetch_add(&p->new_tls_idx,1);
747 mem += ((uintptr_t)p->tls_image - (uintptr_t)mem) & (p->tls_align-1);
748 self->dtv[v[0]] = mem;
749 memcpy(mem, p->tls_image, p->tls_len);
750 pthread_sigmask(SIG_SETMASK, &set, 0);
754 static void update_tls_size()
756 size_t below_tp = (1+tls_cnt) * sizeof(void *) + tls_offset;
757 size_t above_tp = sizeof(struct pthread) + tls_align;
758 libc.tls_size = ALIGN(below_tp + above_tp, tls_align);
761 void *__dynlink(int argc, char **argv)
763 size_t aux[AUX_CNT] = {0};
767 static struct dso builtin_dsos[3];
768 struct dso *const app = builtin_dsos+0;
769 struct dso *const lib = builtin_dsos+1;
770 struct dso *const vdso = builtin_dsos+2;
774 /* Find aux vector just past environ[] */
775 for (i=argc+1; argv[i]; i++)
776 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
777 env_path = argv[i]+16;
778 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
779 env_preload = argv[i]+11;
780 auxv = (void *)(argv+i+1);
782 decode_vec(auxv, aux, AUX_CNT);
784 /* Only trust user/env if kernel says we're not suid/sgid */
785 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
786 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
791 /* If the dynamic linker was invoked as a program itself, AT_BASE
792 * will not be set. In that case, we assume the base address is
793 * the start of the page containing the PHDRs; I don't know any
794 * better approach... */
796 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
797 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
800 /* The dynamic linker load address is passed by the kernel
801 * in the AUX vector, so this is easy. */
802 lib->base = (void *)aux[AT_BASE];
803 lib->name = lib->shortname = "libc.so";
805 ehdr = (void *)lib->base;
806 find_map_range((void *)(aux[AT_BASE]+ehdr->e_phoff),
807 ehdr->e_phnum, ehdr->e_phentsize, lib);
808 lib->dynv = (void *)(lib->base + find_dyn(
809 (void *)(aux[AT_BASE]+ehdr->e_phoff),
810 ehdr->e_phnum, ehdr->e_phentsize));
814 size_t interp_off = 0;
815 size_t tls_image = 0;
816 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
817 phdr = (void *)aux[AT_PHDR];
818 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
819 if (phdr->p_type == PT_PHDR)
820 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
821 else if (phdr->p_type == PT_INTERP)
822 interp_off = (size_t)phdr->p_vaddr;
823 else if (phdr->p_type == PT_TLS) {
824 tls_image = phdr->p_vaddr;
825 app->tls_len = phdr->p_filesz;
826 app->tls_size = phdr->p_memsz;
827 app->tls_align = phdr->p_align;
830 if (app->tls_size) app->tls_image = (char *)app->base + tls_image;
831 if (interp_off) lib->name = (char *)app->base + interp_off;
833 app->dynv = (void *)(app->base + find_dyn(
834 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
835 find_map_range((void *)aux[AT_PHDR],
836 aux[AT_PHNUM], aux[AT_PHENT], app);
839 char *ldname = argv[0];
840 size_t dyno, l = strlen(ldname);
841 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
842 *argv++ = (void *)-1;
843 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
845 dprintf(2, "musl libc/dynamic program loader\n");
846 dprintf(2, "usage: %s pathname%s\n", ldname,
847 ldd_mode ? "" : " [args]");
850 fd = open(argv[0], O_RDONLY);
852 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
856 ehdr = (void *)map_library(fd, app);
858 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
865 aux[AT_ENTRY] = ehdr->e_entry;
868 app->tls_id = tls_cnt = 1;
869 tls_offset = app->tls_offset = app->tls_size
870 + ( -((uintptr_t)app->tls_image + app->tls_size)
871 & (app->tls_align-1) );
872 tls_align = MAXP2(tls_align, app->tls_align);
875 app->constructed = 1;
878 /* Attach to vdso, if provided by the kernel */
879 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
880 ehdr = (void *)vdso_base;
881 phdr = (void *)(vdso_base + ehdr->e_phoff);
882 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
883 if (phdr->p_type == PT_DYNAMIC)
884 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
885 if (phdr->p_type == PT_LOAD)
886 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
888 vdso->name = vdso->shortname = "linux-gate.so.1";
895 /* Initial dso chain consists only of the app. We temporarily
896 * append the dynamic linker/libc so we can relocate it, then
897 * restore the initial chain in preparation for loading third
898 * party libraries (preload/needed). */
905 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
907 /* Donate unused parts of app and library mapping to malloc */
908 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
909 ehdr = (void *)lib->base;
910 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
911 ehdr->e_phentsize, ehdr->e_phnum);
913 /* Load preload/needed libraries, add their symbols to the global
914 * namespace, and perform all remaining relocations. The main
915 * program must be relocated LAST since it may contain copy
916 * relocations which depend on libraries' relocations. */
917 if (env_preload) load_preload(env_preload);
921 reloc_all(app->next);
927 void *mem = mmap(0, libc.tls_size, PROT_READ|PROT_WRITE,
928 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
929 if (mem==MAP_FAILED ||
930 !__install_initial_tls(__copy_tls(mem))) {
931 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
932 argv[0], libc.tls_size);
937 if (ldso_fail) _exit(127);
938 if (ldd_mode) _exit(0);
940 /* Switch to runtime mode: any further failures in the dynamic
941 * linker are a reportable failure rather than a fatal startup
942 * error. If the dynamic loader (dlopen) will not be used, free
943 * all memory used by the dynamic linker. */
946 #ifndef DYNAMIC_IS_RO
947 for (i=0; app->dynv[i]; i+=2)
948 if (app->dynv[i]==DT_DEBUG)
949 app->dynv[i+1] = (size_t)&debug;
952 debug.bp = _dl_debug_state;
954 debug.base = lib->base;
958 if (ssp_used) __init_ssp((void *)aux[AT_RANDOM]);
964 return (void *)aux[AT_ENTRY];
967 void *dlopen(const char *file, int mode)
969 struct dso *volatile p, *orig_tail, *next;
970 size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
974 if (!file) return head;
976 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
977 pthread_rwlock_wrlock(&lock);
981 orig_tls_cnt = tls_cnt;
982 orig_tls_offset = tls_offset;
983 orig_tls_align = tls_align;
986 if (setjmp(rtld_fail)) {
987 /* Clean up anything new that was (partially) loaded */
988 if (p && p->deps) for (i=0; p->deps[i]; i++)
989 if (p->deps[i]->global < 0)
990 p->deps[i]->global = 0;
991 for (p=orig_tail->next; p; p=next) {
993 munmap(p->map, p->map_len);
997 tls_cnt = orig_tls_cnt;
998 tls_offset = orig_tls_offset;
999 tls_align = orig_tls_align;
1005 } else p = load_library(file);
1008 snprintf(errbuf, sizeof errbuf,
1009 "Error loading shared library %s: %m", file);
1014 /* First load handling */
1017 if (p->deps) for (i=0; p->deps[i]; i++)
1018 if (!p->deps[i]->global)
1019 p->deps[i]->global = -1;
1020 if (!p->global) p->global = -1;
1022 if (p->deps) for (i=0; p->deps[i]; i++)
1023 if (p->deps[i]->global < 0)
1024 p->deps[i]->global = 0;
1025 if (p->global < 0) p->global = 0;
1028 if (mode & RTLD_GLOBAL) {
1029 if (p->deps) for (i=0; p->deps[i]; i++)
1030 p->deps[i]->global = 1;
1036 if (ssp_used) __init_ssp(auxv);
1042 pthread_rwlock_unlock(&lock);
1043 if (p) do_init_fini(orig_tail);
1044 pthread_setcancelstate(cs, 0);
1048 static void *do_dlsym(struct dso *p, const char *s, void *ra)
1051 uint32_t h = 0, gh = 0;
1053 if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
1054 if (p == RTLD_NEXT) {
1055 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
1058 struct symdef def = find_sym(p->next, s, 0);
1059 if (!def.sym) goto failed;
1060 return def.dso->base + def.sym->st_value;
1064 sym = gnu_lookup(s, gh, p);
1067 sym = sysv_lookup(s, h, p);
1069 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1070 return p->base + sym->st_value;
1071 if (p->deps) for (i=0; p->deps[i]; i++) {
1072 if (p->deps[i]->ghashtab) {
1073 if (!gh) gh = gnu_hash(s);
1074 sym = gnu_lookup(s, gh, p->deps[i]);
1076 if (!h) h = sysv_hash(s);
1077 sym = sysv_lookup(s, h, p->deps[i]);
1079 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
1080 return p->deps[i]->base + sym->st_value;
1084 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
1088 int __dladdr(void *addr, Dl_info *info)
1098 pthread_rwlock_rdlock(&lock);
1099 for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
1100 pthread_rwlock_unlock(&lock);
1105 strings = p->strings;
1107 nsym = p->hashtab[1];
1111 buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
1112 sym += p->ghashtab[1];
1113 for (i = 0; i < p->ghashtab[0]; i++) {
1114 if (buckets[i] > nsym)
1118 nsym -= p->ghashtab[1];
1119 hashval = buckets + p->ghashtab[0] + nsym;
1121 while (!(*hashval++ & 1));
1125 for (; nsym; nsym--, sym++) {
1126 if (sym->st_shndx && sym->st_value
1127 && (1<<(sym->st_info&0xf) & OK_TYPES)
1128 && (1<<(sym->st_info>>4) & OK_BINDS)) {
1129 void *symaddr = p->base + sym->st_value;
1130 if (symaddr > addr || symaddr < best)
1133 bestname = strings + sym->st_name;
1134 if (addr == symaddr)
1139 if (!best) return 0;
1141 info->dli_fname = p->name;
1142 info->dli_fbase = p->base;
1143 info->dli_sname = bestname;
1144 info->dli_saddr = best;
1149 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1152 pthread_rwlock_rdlock(&lock);
1153 res = do_dlsym(p, s, ra);
1154 pthread_rwlock_unlock(&lock);
1158 void *dlopen(const char *file, int mode)
1162 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1166 int __dladdr (void *addr, Dl_info *info)
1174 if (!errflag) return 0;
1179 int dlclose(void *p)