20 #include "pthread_impl.h"
25 static char errbuf[128];
29 #if ULONG_MAX == 0xffffffff
30 typedef Elf32_Ehdr Ehdr;
31 typedef Elf32_Phdr Phdr;
32 typedef Elf32_Sym Sym;
33 #define R_TYPE(x) ((x)&255)
34 #define R_SYM(x) ((x)>>8)
36 typedef Elf64_Ehdr Ehdr;
37 typedef Elf64_Phdr Phdr;
38 typedef Elf64_Sym Sym;
39 #define R_TYPE(x) ((x)&0xffffffff)
40 #define R_SYM(x) ((x)>>32)
55 struct dso *next, *prev;
71 size_t tls_len, tls_size, tls_align, tls_id;
78 void __init_ssp(size_t *);
79 void *__install_initial_tls(void *);
81 static struct dso *head, *tail, *libc;
82 static char *env_path, *sys_path, *r_path;
87 static jmp_buf rtld_fail;
88 static pthread_rwlock_t lock;
89 static struct debug debug;
91 static size_t tls_cnt, tls_size;
93 struct debug *_dl_debug_addr = &debug;
98 static void decode_vec(size_t *v, size_t *a, size_t cnt)
100 memset(a, 0, cnt*sizeof(size_t));
101 for (; v[0]; v+=2) if (v[0]<cnt) {
107 static int search_vec(size_t *v, size_t *r, size_t key)
109 for (; v[0]!=key; v+=2)
115 static uint32_t sysv_hash(const char *s0)
117 const unsigned char *s = (void *)s0;
123 return h & 0xfffffff;
126 static uint32_t gnu_hash(const char *s0)
128 const unsigned char *s = (void *)s0;
129 uint_fast32_t h = 5381;
135 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
138 Sym *syms = dso->syms;
139 uint32_t *hashtab = dso->hashtab;
140 char *strings = dso->strings;
141 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
142 if (!strcmp(s, strings+syms[i].st_name))
148 static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
152 uint32_t *hashtab = dso->ghashtab;
153 uint32_t nbuckets = hashtab[0];
154 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
157 uint32_t n = buckets[h1 % nbuckets];
161 strings = dso->strings;
163 hashval = buckets + nbuckets + (n - hashtab[1]);
165 for (h1 |= 1; ; sym++) {
167 if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name))
175 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON)
176 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
178 static void *find_sym(struct dso *dso, const char *s, int need_def)
180 uint32_t h = 0, gh = 0;
184 if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
187 if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
189 for (; dso; dso=dso->next) {
191 if (!dso->global) continue;
193 if (!gh) gh = gnu_hash(s);
194 sym = gnu_lookup(s, gh, dso);
196 if (!h) h = sysv_hash(s);
197 sym = sysv_lookup(s, h, dso);
199 if (sym && (!need_def || sym->st_shndx) && sym->st_value
200 && (1<<(sym->st_info&0xf) & OK_TYPES)
201 && (1<<(sym->st_info>>4) & OK_BINDS)) {
202 if (def && sym->st_info>>4 == STB_WEAK) continue;
203 def = dso->base + sym->st_value;
204 if (sym->st_info>>4 == STB_GLOBAL) break;
210 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
212 unsigned char *base = dso->base;
213 Sym *syms = dso->syms;
214 char *strings = dso->strings;
217 size_t sym_val, sym_size;
223 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
224 reloc_addr = (void *)(base + rel[0]);
225 type = R_TYPE(rel[1]);
226 sym_index = R_SYM(rel[1]);
228 sym = syms + sym_index;
229 name = strings + sym->st_name;
230 ctx = IS_COPY(type) ? head->next : head;
231 sym_val = (size_t)find_sym(ctx, name, IS_PLT(type));
232 if (!sym_val && sym->st_info>>4 != STB_WEAK) {
233 snprintf(errbuf, sizeof errbuf,
234 "Error relocating %s: %s: symbol not found",
236 if (runtime) longjmp(rtld_fail, 1);
237 dprintf(2, "%s\n", errbuf);
241 sym_size = sym->st_size;
243 sym_val = sym_size = 0;
245 do_single_reloc(reloc_addr, type, sym_val, sym_size, base, rel[2]);
249 /* A huge hack: to make up for the wastefulness of shared libraries
250 * needing at least a page of dirty memory even if they have no global
251 * data, we reclaim the gaps at the beginning and end of writable maps
252 * and "donate" them to the heap by setting up minimal malloc
253 * structures and then freeing them. */
255 static void reclaim(unsigned char *base, size_t start, size_t end)
258 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
259 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
260 if (start>end || end-start < 4*sizeof(size_t)) return;
261 a = (size_t *)(base + start);
262 z = (size_t *)(base + end);
264 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
269 static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
271 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
272 if (ph->p_type!=PT_LOAD) continue;
273 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
274 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
275 reclaim(base, ph->p_vaddr+ph->p_memsz,
276 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
280 static void *map_library(int fd, struct dso *dso)
282 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
284 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
285 size_t this_min, this_max;
290 unsigned char *map, *base;
295 ssize_t l = read(fd, buf, sizeof buf);
296 if (l<sizeof *eh) return 0;
298 phsize = eh->e_phentsize * eh->e_phnum;
299 if (phsize + sizeof *eh > l) return 0;
300 if (eh->e_phoff + phsize > l) {
301 l = pread(fd, buf+1, phsize, eh->e_phoff);
302 if (l != phsize) return 0;
303 eh->e_phoff = sizeof *eh;
305 ph = (void *)((char *)buf + eh->e_phoff);
306 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
307 if (ph->p_type == PT_DYNAMIC)
309 if (ph->p_type == PT_TLS) {
310 tls_image = ph->p_vaddr;
311 dso->tls_align = ph->p_align;
312 dso->tls_len = ph->p_filesz;
313 dso->tls_size = ph->p_memsz;
315 if (ph->p_type != PT_LOAD) continue;
316 if (ph->p_vaddr < addr_min) {
317 addr_min = ph->p_vaddr;
318 off_start = ph->p_offset;
319 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
320 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
321 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
323 if (ph->p_vaddr+ph->p_memsz > addr_max) {
324 addr_max = ph->p_vaddr+ph->p_memsz;
328 addr_max += PAGE_SIZE-1;
329 addr_max &= -PAGE_SIZE;
330 addr_min &= -PAGE_SIZE;
331 off_start &= -PAGE_SIZE;
332 map_len = addr_max - addr_min + off_start;
333 /* The first time, we map too much, possibly even more than
334 * the length of the file. This is okay because we will not
335 * use the invalid part; we just need to reserve the right
336 * amount of virtual address space to map over later. */
337 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
338 if (map==MAP_FAILED) return 0;
339 base = map - addr_min;
340 ph = (void *)((char *)buf + eh->e_phoff);
341 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
342 if (ph->p_type != PT_LOAD) continue;
343 /* Reuse the existing mapping for the lowest-address LOAD */
344 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
345 this_min = ph->p_vaddr & -PAGE_SIZE;
346 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
347 off_start = ph->p_offset & -PAGE_SIZE;
348 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
349 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
350 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
351 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
353 if (ph->p_memsz > ph->p_filesz) {
354 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
355 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
356 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
357 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)
361 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
362 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
363 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
367 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
368 eh->e_phentsize, eh->e_phnum);
370 dso->map_len = map_len;
372 dso->dynv = (void *)(base+dyn);
373 if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
376 munmap(map, map_len);
380 static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
382 const char *s=search, *z;
388 l = z ? z-s : strlen(s);
389 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
390 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
395 static void decode_dyn(struct dso *p)
397 size_t dyn[DYN_CNT] = {0};
398 decode_vec(p->dynv, dyn, DYN_CNT);
399 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
400 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
401 if (dyn[0]&(1<<DT_HASH))
402 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
403 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
404 p->ghashtab = (void *)(p->base + *dyn);
407 static struct dso *load_library(const char *name)
409 char buf[2*NAME_MAX+2];
410 const char *pathname;
411 unsigned char *base, *map;
412 size_t dyno, map_len;
413 struct dso *p, temp_dso = {0};
417 /* Catch and block attempts to reload the implementation itself */
418 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
419 static const char *rp, reserved[] =
420 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
421 char *z = strchr(name, '.');
424 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
429 tail = libc->next ? libc->next : libc;
435 if (strchr(name, '/')) {
437 fd = open(name, O_RDONLY|O_CLOEXEC);
439 /* Search for the name to see if it's already loaded */
440 for (p=head->next; p; p=p->next) {
441 if (p->shortname && !strcmp(p->shortname, name)) {
446 if (strlen(name) > NAME_MAX) return 0;
448 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
449 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
452 FILE *f = fopen(ETC_LDSO_PATH, "rbe");
454 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
455 sys_path[strlen(sys_path)-1]=0;
459 if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf);
460 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf);
464 if (fd < 0) return 0;
465 if (fstat(fd, &st) < 0) {
469 for (p=head->next; p; p=p->next) {
470 if (p->dev == st.st_dev && p->ino == st.st_ino) {
471 /* If this library was previously loaded with a
472 * pathname but a search found the same inode,
473 * setup its shortname so it can be found by name. */
474 if (!p->shortname) p->shortname = strrchr(p->name, '/')+1;
480 map = map_library(fd, &temp_dso);
483 p = malloc(sizeof *p + strlen(pathname) + 1);
485 munmap(map, map_len);
488 memcpy(p, &temp_dso, sizeof temp_dso);
491 p->tls_id = ++tls_cnt;
492 tls_size += p->tls_size + p->tls_align + 8*sizeof(size_t) - 1
499 strcpy(p->name, pathname);
500 /* Add a shortname only if name arg was not an explicit pathname. */
501 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
507 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base);
512 static void load_deps(struct dso *p)
515 struct dso ***deps = &p->deps, **tmp, *dep;
516 for (; p; p=p->next) {
517 for (i=0; p->dynv[i]; i+=2) {
518 if (p->dynv[i] != DT_RPATH) continue;
519 r_path = (void *)(p->strings + p->dynv[i+1]);
521 for (i=0; p->dynv[i]; i+=2) {
522 if (p->dynv[i] != DT_NEEDED) continue;
523 dep = load_library(p->strings + p->dynv[i+1]);
525 snprintf(errbuf, sizeof errbuf,
526 "Error loading shared library %s: %m (needed by %s)",
527 p->strings + p->dynv[i+1], p->name);
528 if (runtime) longjmp(rtld_fail, 1);
529 dprintf(2, "%s\n", errbuf);
534 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
535 if (!tmp) longjmp(rtld_fail, 1);
545 static void load_preload(char *s)
550 for ( ; *s && isspace(*s); s++);
551 for (z=s; *z && !isspace(*z); z++);
559 static void make_global(struct dso *p)
561 for (; p; p=p->next) p->global = 1;
564 static void reloc_all(struct dso *p)
566 size_t dyn[DYN_CNT] = {0};
567 for (; p; p=p->next) {
568 if (p->relocated) continue;
569 decode_vec(p->dynv, dyn, DYN_CNT);
570 #ifdef NEED_ARCH_RELOCS
571 do_arch_relocs(p, head);
573 do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
574 2+(dyn[DT_PLTREL]==DT_RELA));
575 do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2);
576 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
581 static void free_all(struct dso *p)
586 if (p->map && p!=libc && p!=head) free(p);
591 static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
593 for (; cnt--; ph = (void *)((char *)ph + stride))
594 if (ph->p_type == PT_DYNAMIC)
599 static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p)
601 size_t min_addr = -1, max_addr = 0;
602 for (; cnt--; ph = (void *)((char *)ph + stride)) {
603 if (ph->p_type != PT_LOAD) continue;
604 if (ph->p_vaddr < min_addr)
605 min_addr = ph->p_vaddr;
606 if (ph->p_vaddr+ph->p_memsz > max_addr)
607 max_addr = ph->p_vaddr+ph->p_memsz;
609 min_addr &= -PAGE_SIZE;
610 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
611 p->map = p->base + min_addr;
612 p->map_len = max_addr - min_addr;
615 static void do_init_fini(struct dso *p)
617 size_t dyn[DYN_CNT] = {0};
618 for (; p; p=p->prev) {
619 if (p->constructed) return;
620 decode_vec(p->dynv, dyn, DYN_CNT);
621 if (dyn[0] & (1<<DT_FINI))
622 atexit((void (*)(void))(p->base + dyn[DT_FINI]));
623 if (dyn[0] & (1<<DT_INIT))
624 ((void (*)(void))(p->base + dyn[DT_INIT]))();
629 void _dl_debug_state(void)
633 void *__copy_tls(unsigned char *mem, size_t cnt)
636 void **dtv = (void *)mem;
637 dtv[0] = (void *)cnt;
638 mem = (void *)(dtv + cnt + 1);
639 for (p=tail; p; p=p->prev) {
640 if (p->tls_id-1 >= cnt) continue;
641 mem += -p->tls_len & (4*sizeof(size_t)-1);
642 mem += ((uintptr_t)p->tls_image - (uintptr_t)mem)
644 dtv[p->tls_id] = mem;
645 memcpy(mem, p->tls_image, p->tls_len);
648 ((pthread_t)mem)->dtv = dtv;
652 void *__tls_get_addr(size_t *p)
654 pthread_t self = __pthread_self();
655 if ((size_t)self->dtv[0] < p[0]) {
656 // FIXME: obtain new DTV and TLS from the DSO
659 return (char *)self->dtv[p[0]] + p[1];
662 void *__dynlink(int argc, char **argv)
664 size_t aux[AUX_CNT] = {0};
668 static struct dso builtin_dsos[3];
669 struct dso *const app = builtin_dsos+0;
670 struct dso *const lib = builtin_dsos+1;
671 struct dso *const vdso = builtin_dsos+2;
675 /* Find aux vector just past environ[] */
676 for (i=argc+1; argv[i]; i++)
677 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
678 env_path = argv[i]+16;
679 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
680 env_preload = argv[i]+11;
681 auxv = (void *)(argv+i+1);
683 decode_vec(auxv, aux, AUX_CNT);
685 /* Only trust user/env if kernel says we're not suid/sgid */
686 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
687 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
692 /* If the dynamic linker was invoked as a program itself, AT_BASE
693 * will not be set. In that case, we assume the base address is
694 * the start of the page containing the PHDRs; I don't know any
695 * better approach... */
697 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
698 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
701 /* The dynamic linker load address is passed by the kernel
702 * in the AUX vector, so this is easy. */
703 lib->base = (void *)aux[AT_BASE];
704 lib->name = lib->shortname = "libc.so";
706 ehdr = (void *)lib->base;
707 find_map_range((void *)(aux[AT_BASE]+ehdr->e_phoff),
708 ehdr->e_phnum, ehdr->e_phentsize, lib);
709 lib->dynv = (void *)(lib->base + find_dyn(
710 (void *)(aux[AT_BASE]+ehdr->e_phoff),
711 ehdr->e_phnum, ehdr->e_phentsize));
715 size_t interp_off = 0;
716 size_t tls_image = 0;
717 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
718 phdr = (void *)aux[AT_PHDR];
719 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
720 if (phdr->p_type == PT_PHDR)
721 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
722 else if (phdr->p_type == PT_INTERP)
723 interp_off = (size_t)phdr->p_vaddr;
724 else if (phdr->p_type == PT_TLS) {
725 tls_image = phdr->p_vaddr;
726 app->tls_len = phdr->p_filesz;
727 app->tls_size = phdr->p_memsz;
728 app->tls_align = phdr->p_align;
731 if (app->tls_size) app->tls_image = (char *)app->base + tls_image;
732 if (interp_off) lib->name = (char *)app->base + interp_off;
734 app->dynv = (void *)(app->base + find_dyn(
735 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
736 find_map_range((void *)aux[AT_PHDR],
737 aux[AT_PHNUM], aux[AT_PHENT], app);
740 char *ldname = argv[0];
741 size_t dyno, l = strlen(ldname);
742 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
743 *argv++ = (void *)-1;
744 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
746 dprintf(2, "musl libc/dynamic program loader\n");
747 dprintf(2, "usage: %s pathname%s\n", ldname,
748 ldd_mode ? "" : " [args]");
751 fd = open(argv[0], O_RDONLY);
753 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
757 ehdr = (void *)map_library(fd, app);
759 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
766 aux[AT_ENTRY] = ehdr->e_entry;
769 app->tls_id = ++tls_cnt;
770 tls_size += app->tls_size+app->tls_align + 8*sizeof(size_t)-1
774 app->constructed = 1;
777 /* Attach to vdso, if provided by the kernel */
778 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
779 ehdr = (void *)vdso_base;
780 phdr = (void *)(vdso_base + ehdr->e_phoff);
781 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
782 if (phdr->p_type == PT_DYNAMIC)
783 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
784 if (phdr->p_type == PT_LOAD)
785 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
787 vdso->name = vdso->shortname = "linux-gate.so.1";
794 /* Initial dso chain consists only of the app. We temporarily
795 * append the dynamic linker/libc so we can relocate it, then
796 * restore the initial chain in preparation for loading third
797 * party libraries (preload/needed). */
804 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
806 /* Donate unused parts of app and library mapping to malloc */
807 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
808 ehdr = (void *)lib->base;
809 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
810 ehdr->e_phentsize, ehdr->e_phnum);
812 /* Load preload/needed libraries, add their symbols to the global
813 * namespace, and perform all remaining relocations. The main
814 * program must be relocated LAST since it may contain copy
815 * relocations which depend on libraries' relocations. */
816 if (env_preload) load_preload(env_preload);
819 reloc_all(app->next);
822 if (ldso_fail) _exit(127);
823 if (ldd_mode) _exit(0);
825 /* Switch to runtime mode: any further failures in the dynamic
826 * linker are a reportable failure rather than a fatal startup
827 * error. If the dynamic loader (dlopen) will not be used, free
828 * all memory used by the dynamic linker. */
831 #ifndef DYNAMIC_IS_RO
832 for (i=0; app->dynv[i]; i+=2)
833 if (app->dynv[i]==DT_DEBUG)
834 app->dynv[i+1] = (size_t)&debug;
837 debug.bp = _dl_debug_state;
839 debug.base = lib->base;
843 tls_size += sizeof(struct pthread) + 4*sizeof(size_t);
844 __libc.tls_size = tls_size;
845 __libc.tls_cnt = tls_cnt;
847 void *mem = mmap(0, __libc.tls_size, PROT_READ|PROT_WRITE,
848 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
849 if (mem==MAP_FAILED ||
850 !__install_initial_tls(__copy_tls(mem, tls_cnt))) {
851 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
856 if (ssp_used) __init_ssp(auxv);
861 return (void *)aux[AT_ENTRY];
864 void *dlopen(const char *file, int mode)
866 struct dso *volatile p, *orig_tail = tail, *next;
870 if (!file) return head;
872 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
873 pthread_rwlock_wrlock(&lock);
875 if (setjmp(rtld_fail)) {
876 /* Clean up anything new that was (partially) loaded */
877 if (p->deps) for (i=0; p->deps[i]; i++)
878 if (p->deps[i]->global < 0)
879 p->deps[i]->global = 0;
880 for (p=orig_tail->next; p; p=next) {
882 munmap(p->map, p->map_len);
891 } else p = load_library(file);
894 snprintf(errbuf, sizeof errbuf,
895 "Error loading shared library %s: %m", file);
900 /* First load handling */
903 if (p->deps) for (i=0; p->deps[i]; i++)
904 if (!p->deps[i]->global)
905 p->deps[i]->global = -1;
906 if (!p->global) p->global = -1;
908 if (p->deps) for (i=0; p->deps[i]; i++)
909 if (p->deps[i]->global < 0)
910 p->deps[i]->global = 0;
911 if (p->global < 0) p->global = 0;
914 if (mode & RTLD_GLOBAL) {
915 if (p->deps) for (i=0; p->deps[i]; i++)
916 p->deps[i]->global = 1;
920 if (ssp_used) __init_ssp(auxv);
926 pthread_rwlock_unlock(&lock);
927 pthread_setcancelstate(cs, 0);
931 static void *do_dlsym(struct dso *p, const char *s, void *ra)
934 uint32_t h = 0, gh = 0;
936 if (p == RTLD_NEXT) {
937 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
939 void *res = find_sym(p->next, s, 0);
940 if (!res) goto failed;
943 if (p == head || p == RTLD_DEFAULT) {
944 void *res = find_sym(head, s, 0);
945 if (!res) goto failed;
950 sym = gnu_lookup(s, gh, p);
953 sym = sysv_lookup(s, h, p);
955 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
956 return p->base + sym->st_value;
957 if (p->deps) for (i=0; p->deps[i]; i++) {
958 if (p->deps[i]->ghashtab) {
959 if (!gh) gh = gnu_hash(s);
960 sym = gnu_lookup(s, gh, p->deps[i]);
962 if (!h) h = sysv_hash(s);
963 sym = sysv_lookup(s, h, p->deps[i]);
965 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
966 return p->deps[i]->base + sym->st_value;
970 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
974 int __dladdr(void *addr, Dl_info *info)
984 pthread_rwlock_rdlock(&lock);
985 for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
986 pthread_rwlock_unlock(&lock);
991 strings = p->strings;
993 nsym = p->hashtab[1];
997 buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
998 sym += p->ghashtab[1];
999 for (i = 0; i < p->ghashtab[0]; i++) {
1000 if (buckets[i] > nsym)
1004 nsym -= p->ghashtab[1];
1005 hashval = buckets + p->ghashtab[0] + nsym;
1007 while (!(*hashval++ & 1));
1011 for (; nsym; nsym--, sym++) {
1012 if (sym->st_shndx && sym->st_value
1013 && (1<<(sym->st_info&0xf) & OK_TYPES)
1014 && (1<<(sym->st_info>>4) & OK_BINDS)) {
1015 void *symaddr = p->base + sym->st_value;
1016 if (symaddr > addr || symaddr < best)
1019 bestname = strings + sym->st_name;
1020 if (addr == symaddr)
1025 if (!best) return 0;
1027 info->dli_fname = p->name;
1028 info->dli_fbase = p->base;
1029 info->dli_sname = bestname;
1030 info->dli_saddr = best;
1035 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1038 pthread_rwlock_rdlock(&lock);
1039 res = do_dlsym(p, s, ra);
1040 pthread_rwlock_unlock(&lock);
1044 void *dlopen(const char *file, int mode)
1048 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1052 int __dladdr (void *addr, Dl_info *info)
1060 if (!errflag) return 0;
1065 int dlclose(void *p)