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, tls_offset;
83 void __init_ssp(size_t *);
84 void *__install_initial_tls(void *);
86 static struct dso *head, *tail, *libc;
87 static char *env_path, *sys_path, *r_path;
92 static jmp_buf rtld_fail;
93 static pthread_rwlock_t lock;
94 static struct debug debug;
96 static size_t tls_cnt, tls_size;
98 struct debug *_dl_debug_addr = &debug;
103 static void decode_vec(size_t *v, size_t *a, size_t cnt)
105 memset(a, 0, cnt*sizeof(size_t));
106 for (; v[0]; v+=2) if (v[0]<cnt) {
112 static int search_vec(size_t *v, size_t *r, size_t key)
114 for (; v[0]!=key; v+=2)
120 static uint32_t sysv_hash(const char *s0)
122 const unsigned char *s = (void *)s0;
128 return h & 0xfffffff;
131 static uint32_t gnu_hash(const char *s0)
133 const unsigned char *s = (void *)s0;
134 uint_fast32_t h = 5381;
140 static Sym *sysv_lookup(const char *s, uint32_t h, struct dso *dso)
143 Sym *syms = dso->syms;
144 uint32_t *hashtab = dso->hashtab;
145 char *strings = dso->strings;
146 for (i=hashtab[2+h%hashtab[0]]; i; i=hashtab[2+hashtab[0]+i]) {
147 if (!strcmp(s, strings+syms[i].st_name))
153 static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
157 uint32_t *hashtab = dso->ghashtab;
158 uint32_t nbuckets = hashtab[0];
159 uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
162 uint32_t n = buckets[h1 % nbuckets];
166 strings = dso->strings;
168 hashval = buckets + nbuckets + (n - hashtab[1]);
170 for (h1 |= 1; ; sym++) {
172 if ((h1 == (h2|1)) && !strcmp(s, strings + sym->st_name))
180 #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
181 #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK)
183 static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
185 uint32_t h = 0, gh = 0;
186 struct symdef def = {0};
189 if (gh == 0x1f4039c9 && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
192 if (h == 0x595a4cc && !strcmp(s, "__stack_chk_fail")) ssp_used = 1;
194 for (; dso; dso=dso->next) {
196 if (!dso->global) continue;
198 if (!gh) gh = gnu_hash(s);
199 sym = gnu_lookup(s, gh, dso);
201 if (!h) h = sysv_hash(s);
202 sym = sysv_lookup(s, h, dso);
204 if (sym && (!need_def || sym->st_shndx) && sym->st_value
205 && (1<<(sym->st_info&0xf) & OK_TYPES)
206 && (1<<(sym->st_info>>4) & OK_BINDS)) {
207 if (def.sym && sym->st_info>>4 == STB_WEAK) continue;
210 if (sym->st_info>>4 == STB_GLOBAL) break;
216 static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stride)
218 unsigned char *base = dso->base;
219 Sym *syms = dso->syms;
220 char *strings = dso->strings;
228 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
229 type = R_TYPE(rel[1]);
230 sym_index = R_SYM(rel[1]);
232 sym = syms + sym_index;
233 name = strings + sym->st_name;
234 ctx = IS_COPY(type) ? head->next : head;
235 def = find_sym(ctx, name, IS_PLT(type));
236 if (!def.sym && sym->st_info>>4 != STB_WEAK) {
237 snprintf(errbuf, sizeof errbuf,
238 "Error relocating %s: %s: symbol not found",
240 if (runtime) longjmp(rtld_fail, 1);
241 dprintf(2, "%s\n", errbuf);
250 do_single_reloc(dso, base, (void *)(base + rel[0]), type,
251 stride>2 ? rel[2] : 0, sym, sym?sym->st_size:0, def,
252 def.sym?(size_t)(def.dso->base+def.sym->st_value):0);
256 /* A huge hack: to make up for the wastefulness of shared libraries
257 * needing at least a page of dirty memory even if they have no global
258 * data, we reclaim the gaps at the beginning and end of writable maps
259 * and "donate" them to the heap by setting up minimal malloc
260 * structures and then freeing them. */
262 static void reclaim(unsigned char *base, size_t start, size_t end)
265 start = start + 6*sizeof(size_t)-1 & -4*sizeof(size_t);
266 end = (end & -4*sizeof(size_t)) - 2*sizeof(size_t);
267 if (start>end || end-start < 4*sizeof(size_t)) return;
268 a = (size_t *)(base + start);
269 z = (size_t *)(base + end);
271 a[-1] = z[0] = end-start + 2*sizeof(size_t) | 1;
276 static void reclaim_gaps(unsigned char *base, Phdr *ph, size_t phent, size_t phcnt)
278 for (; phcnt--; ph=(void *)((char *)ph+phent)) {
279 if (ph->p_type!=PT_LOAD) continue;
280 if ((ph->p_flags&(PF_R|PF_W))!=(PF_R|PF_W)) continue;
281 reclaim(base, ph->p_vaddr & -PAGE_SIZE, ph->p_vaddr);
282 reclaim(base, ph->p_vaddr+ph->p_memsz,
283 ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE);
287 static void *map_library(int fd, struct dso *dso)
289 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
291 size_t addr_min=SIZE_MAX, addr_max=0, map_len;
292 size_t this_min, this_max;
297 unsigned char *map, *base;
302 ssize_t l = read(fd, buf, sizeof buf);
303 if (l<sizeof *eh) return 0;
305 phsize = eh->e_phentsize * eh->e_phnum;
306 if (phsize + sizeof *eh > l) return 0;
307 if (eh->e_phoff + phsize > l) {
308 l = pread(fd, buf+1, phsize, eh->e_phoff);
309 if (l != phsize) return 0;
310 eh->e_phoff = sizeof *eh;
312 ph = (void *)((char *)buf + eh->e_phoff);
313 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
314 if (ph->p_type == PT_DYNAMIC)
316 if (ph->p_type == PT_TLS) {
317 tls_image = ph->p_vaddr;
318 dso->tls_align = ph->p_align;
319 dso->tls_len = ph->p_filesz;
320 dso->tls_size = ph->p_memsz;
322 if (ph->p_type != PT_LOAD) continue;
323 if (ph->p_vaddr < addr_min) {
324 addr_min = ph->p_vaddr;
325 off_start = ph->p_offset;
326 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
327 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
328 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
330 if (ph->p_vaddr+ph->p_memsz > addr_max) {
331 addr_max = ph->p_vaddr+ph->p_memsz;
335 addr_max += PAGE_SIZE-1;
336 addr_max &= -PAGE_SIZE;
337 addr_min &= -PAGE_SIZE;
338 off_start &= -PAGE_SIZE;
339 map_len = addr_max - addr_min + off_start;
340 /* The first time, we map too much, possibly even more than
341 * the length of the file. This is okay because we will not
342 * use the invalid part; we just need to reserve the right
343 * amount of virtual address space to map over later. */
344 map = mmap((void *)addr_min, map_len, prot, MAP_PRIVATE, fd, off_start);
345 if (map==MAP_FAILED) return 0;
346 base = map - addr_min;
347 ph = (void *)((char *)buf + eh->e_phoff);
348 for (i=eh->e_phnum; i; i--, ph=(void *)((char *)ph+eh->e_phentsize)) {
349 if (ph->p_type != PT_LOAD) continue;
350 /* Reuse the existing mapping for the lowest-address LOAD */
351 if ((ph->p_vaddr & -PAGE_SIZE) == addr_min) continue;
352 this_min = ph->p_vaddr & -PAGE_SIZE;
353 this_max = ph->p_vaddr+ph->p_memsz+PAGE_SIZE-1 & -PAGE_SIZE;
354 off_start = ph->p_offset & -PAGE_SIZE;
355 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
356 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
357 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
358 if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
360 if (ph->p_memsz > ph->p_filesz) {
361 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
362 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
363 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
364 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)
368 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
369 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
370 if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
374 if (!runtime) reclaim_gaps(base, (void *)((char *)buf + eh->e_phoff),
375 eh->e_phentsize, eh->e_phnum);
377 dso->map_len = map_len;
379 dso->dynv = (void *)(base+dyn);
380 if (dso->tls_size) dso->tls_image = (void *)(base+tls_image);
383 munmap(map, map_len);
387 static int path_open(const char *name, const char *search, char *buf, size_t buf_size)
389 const char *s=search, *z;
395 l = z ? z-s : strlen(s);
396 snprintf(buf, buf_size, "%.*s/%s", l, s, name);
397 if ((fd = open(buf, O_RDONLY|O_CLOEXEC))>=0) return fd;
402 static void decode_dyn(struct dso *p)
404 size_t dyn[DYN_CNT] = {0};
405 decode_vec(p->dynv, dyn, DYN_CNT);
406 p->syms = (void *)(p->base + dyn[DT_SYMTAB]);
407 p->strings = (void *)(p->base + dyn[DT_STRTAB]);
408 if (dyn[0]&(1<<DT_HASH))
409 p->hashtab = (void *)(p->base + dyn[DT_HASH]);
410 if (search_vec(p->dynv, dyn, DT_GNU_HASH))
411 p->ghashtab = (void *)(p->base + *dyn);
414 static struct dso *load_library(const char *name)
416 char buf[2*NAME_MAX+2];
417 const char *pathname;
418 unsigned char *base, *map;
419 size_t dyno, map_len;
420 struct dso *p, temp_dso = {0};
424 /* Catch and block attempts to reload the implementation itself */
425 if (name[0]=='l' && name[1]=='i' && name[2]=='b') {
426 static const char *rp, reserved[] =
427 "c\0pthread\0rt\0m\0dl\0util\0xnet\0";
428 char *z = strchr(name, '.');
431 for (rp=reserved; *rp && memcmp(name+3, rp, l-3); rp+=strlen(rp)+1);
436 tail = libc->next ? libc->next : libc;
442 if (strchr(name, '/')) {
444 fd = open(name, O_RDONLY|O_CLOEXEC);
446 /* Search for the name to see if it's already loaded */
447 for (p=head->next; p; p=p->next) {
448 if (p->shortname && !strcmp(p->shortname, name)) {
453 if (strlen(name) > NAME_MAX) return 0;
455 if (r_path) fd = path_open(name, r_path, buf, sizeof buf);
456 if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
459 FILE *f = fopen(ETC_LDSO_PATH, "rbe");
461 if (getline(&sys_path, (size_t[1]){0}, f) > 0)
462 sys_path[strlen(sys_path)-1]=0;
466 if (sys_path) fd = path_open(name, sys_path, buf, sizeof buf);
467 else fd = path_open(name, "/lib:/usr/local/lib:/usr/lib", buf, sizeof buf);
471 if (fd < 0) return 0;
472 if (fstat(fd, &st) < 0) {
476 for (p=head->next; p; p=p->next) {
477 if (p->dev == st.st_dev && p->ino == st.st_ino) {
478 /* If this library was previously loaded with a
479 * pathname but a search found the same inode,
480 * setup its shortname so it can be found by name. */
481 if (!p->shortname) p->shortname = strrchr(p->name, '/')+1;
487 map = map_library(fd, &temp_dso);
490 p = malloc(sizeof *p + strlen(pathname) + 1);
492 munmap(map, map_len);
495 memcpy(p, &temp_dso, sizeof temp_dso);
498 p->tls_id = ++tls_cnt;
499 tls_size += p->tls_size + p->tls_align + 8*sizeof(size_t) - 1
506 strcpy(p->name, pathname);
507 /* Add a shortname only if name arg was not an explicit pathname. */
508 if (pathname != name) p->shortname = strrchr(p->name, '/')+1;
514 if (ldd_mode) dprintf(1, "\t%s => %s (%p)\n", name, pathname, base);
519 static void load_deps(struct dso *p)
522 struct dso ***deps = &p->deps, **tmp, *dep;
523 for (; p; p=p->next) {
524 for (i=0; p->dynv[i]; i+=2) {
525 if (p->dynv[i] != DT_RPATH) continue;
526 r_path = (void *)(p->strings + p->dynv[i+1]);
528 for (i=0; p->dynv[i]; i+=2) {
529 if (p->dynv[i] != DT_NEEDED) continue;
530 dep = load_library(p->strings + p->dynv[i+1]);
532 snprintf(errbuf, sizeof errbuf,
533 "Error loading shared library %s: %m (needed by %s)",
534 p->strings + p->dynv[i+1], p->name);
535 if (runtime) longjmp(rtld_fail, 1);
536 dprintf(2, "%s\n", errbuf);
541 tmp = realloc(*deps, sizeof(*tmp)*(ndeps+2));
542 if (!tmp) longjmp(rtld_fail, 1);
552 static void load_preload(char *s)
557 for ( ; *s && isspace(*s); s++);
558 for (z=s; *z && !isspace(*z); z++);
566 static void make_global(struct dso *p)
568 for (; p; p=p->next) p->global = 1;
571 static void reloc_all(struct dso *p)
573 size_t dyn[DYN_CNT] = {0};
574 for (; p; p=p->next) {
575 if (p->relocated) continue;
576 decode_vec(p->dynv, dyn, DYN_CNT);
577 #ifdef NEED_ARCH_RELOCS
578 do_arch_relocs(p, head);
580 do_relocs(p, (void *)(p->base+dyn[DT_JMPREL]), dyn[DT_PLTRELSZ],
581 2+(dyn[DT_PLTREL]==DT_RELA));
582 do_relocs(p, (void *)(p->base+dyn[DT_REL]), dyn[DT_RELSZ], 2);
583 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
588 static void free_all(struct dso *p)
593 if (p->map && p!=libc && p!=head) free(p);
598 static size_t find_dyn(Phdr *ph, size_t cnt, size_t stride)
600 for (; cnt--; ph = (void *)((char *)ph + stride))
601 if (ph->p_type == PT_DYNAMIC)
606 static void find_map_range(Phdr *ph, size_t cnt, size_t stride, struct dso *p)
608 size_t min_addr = -1, max_addr = 0;
609 for (; cnt--; ph = (void *)((char *)ph + stride)) {
610 if (ph->p_type != PT_LOAD) continue;
611 if (ph->p_vaddr < min_addr)
612 min_addr = ph->p_vaddr;
613 if (ph->p_vaddr+ph->p_memsz > max_addr)
614 max_addr = ph->p_vaddr+ph->p_memsz;
616 min_addr &= -PAGE_SIZE;
617 max_addr = (max_addr + PAGE_SIZE-1) & -PAGE_SIZE;
618 p->map = p->base + min_addr;
619 p->map_len = max_addr - min_addr;
622 static void do_init_fini(struct dso *p)
624 size_t dyn[DYN_CNT] = {0};
625 for (; p; p=p->prev) {
626 if (p->constructed) return;
627 decode_vec(p->dynv, dyn, DYN_CNT);
628 if (dyn[0] & (1<<DT_FINI))
629 atexit((void (*)(void))(p->base + dyn[DT_FINI]));
630 if (dyn[0] & (1<<DT_INIT))
631 ((void (*)(void))(p->base + dyn[DT_INIT]))();
636 void _dl_debug_state(void)
640 void *__copy_tls(unsigned char *mem, size_t cnt)
643 void **dtv = (void *)mem;
644 dtv[0] = (void *)cnt;
645 mem = (void *)(dtv + cnt + 1);
646 for (p=tail; p; p=p->prev) {
647 if (p->tls_id-1 >= cnt) continue;
648 mem += -p->tls_len & (4*sizeof(size_t)-1);
649 mem += ((uintptr_t)p->tls_image - (uintptr_t)mem)
651 dtv[p->tls_id] = mem;
652 memcpy(mem, p->tls_image, p->tls_len);
655 ((pthread_t)mem)->dtv = dtv;
659 void *__tls_get_addr(size_t *p)
661 pthread_t self = __pthread_self();
662 if ((size_t)self->dtv[0] < p[0]) {
663 // FIXME: obtain new DTV and TLS from the DSO
666 return (char *)self->dtv[p[0]] + p[1];
669 void *__dynlink(int argc, char **argv)
671 size_t aux[AUX_CNT] = {0};
675 static struct dso builtin_dsos[3];
676 struct dso *const app = builtin_dsos+0;
677 struct dso *const lib = builtin_dsos+1;
678 struct dso *const vdso = builtin_dsos+2;
682 /* Find aux vector just past environ[] */
683 for (i=argc+1; argv[i]; i++)
684 if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
685 env_path = argv[i]+16;
686 else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
687 env_preload = argv[i]+11;
688 auxv = (void *)(argv+i+1);
690 decode_vec(auxv, aux, AUX_CNT);
692 /* Only trust user/env if kernel says we're not suid/sgid */
693 if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
694 || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
699 /* If the dynamic linker was invoked as a program itself, AT_BASE
700 * will not be set. In that case, we assume the base address is
701 * the start of the page containing the PHDRs; I don't know any
702 * better approach... */
704 aux[AT_BASE] = aux[AT_PHDR] & -PAGE_SIZE;
705 aux[AT_PHDR] = aux[AT_PHENT] = aux[AT_PHNUM] = 0;
708 /* The dynamic linker load address is passed by the kernel
709 * in the AUX vector, so this is easy. */
710 lib->base = (void *)aux[AT_BASE];
711 lib->name = lib->shortname = "libc.so";
713 ehdr = (void *)lib->base;
714 find_map_range((void *)(aux[AT_BASE]+ehdr->e_phoff),
715 ehdr->e_phnum, ehdr->e_phentsize, lib);
716 lib->dynv = (void *)(lib->base + find_dyn(
717 (void *)(aux[AT_BASE]+ehdr->e_phoff),
718 ehdr->e_phnum, ehdr->e_phentsize));
722 size_t interp_off = 0;
723 size_t tls_image = 0;
724 /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
725 phdr = (void *)aux[AT_PHDR];
726 for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
727 if (phdr->p_type == PT_PHDR)
728 app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
729 else if (phdr->p_type == PT_INTERP)
730 interp_off = (size_t)phdr->p_vaddr;
731 else if (phdr->p_type == PT_TLS) {
732 tls_image = phdr->p_vaddr;
733 app->tls_len = phdr->p_filesz;
734 app->tls_size = phdr->p_memsz;
735 app->tls_align = phdr->p_align;
738 if (app->tls_size) app->tls_image = (char *)app->base + tls_image;
739 if (interp_off) lib->name = (char *)app->base + interp_off;
741 app->dynv = (void *)(app->base + find_dyn(
742 (void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
743 find_map_range((void *)aux[AT_PHDR],
744 aux[AT_PHNUM], aux[AT_PHENT], app);
747 char *ldname = argv[0];
748 size_t dyno, l = strlen(ldname);
749 if (l >= 3 && !strcmp(ldname+l-3, "ldd")) ldd_mode = 1;
750 *argv++ = (void *)-1;
751 if (argv[0] && !strcmp(argv[0], "--")) *argv++ = (void *)-1;
753 dprintf(2, "musl libc/dynamic program loader\n");
754 dprintf(2, "usage: %s pathname%s\n", ldname,
755 ldd_mode ? "" : " [args]");
758 fd = open(argv[0], O_RDONLY);
760 dprintf(2, "%s: cannot load %s: %s\n", ldname, argv[0], strerror(errno));
764 ehdr = (void *)map_library(fd, app);
766 dprintf(2, "%s: %s: Not a valid dynamic program\n", ldname, argv[0]);
773 aux[AT_ENTRY] = ehdr->e_entry;
776 app->tls_id = ++tls_cnt;
777 tls_size += app->tls_size+app->tls_align + 8*sizeof(size_t)-1
781 app->constructed = 1;
784 /* Attach to vdso, if provided by the kernel */
785 if (search_vec(auxv, &vdso_base, AT_SYSINFO_EHDR)) {
786 ehdr = (void *)vdso_base;
787 phdr = (void *)(vdso_base + ehdr->e_phoff);
788 for (i=ehdr->e_phnum; i; i--, phdr=(void *)((char *)phdr + ehdr->e_phentsize)) {
789 if (phdr->p_type == PT_DYNAMIC)
790 vdso->dynv = (void *)(vdso_base + phdr->p_offset);
791 if (phdr->p_type == PT_LOAD)
792 vdso->base = (void *)(vdso_base - phdr->p_vaddr + phdr->p_offset);
794 vdso->name = vdso->shortname = "linux-gate.so.1";
801 /* Initial dso chain consists only of the app. We temporarily
802 * append the dynamic linker/libc so we can relocate it, then
803 * restore the initial chain in preparation for loading third
804 * party libraries (preload/needed). */
811 /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
813 /* Donate unused parts of app and library mapping to malloc */
814 reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
815 ehdr = (void *)lib->base;
816 reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),
817 ehdr->e_phentsize, ehdr->e_phnum);
819 /* Load preload/needed libraries, add their symbols to the global
820 * namespace, and perform all remaining relocations. The main
821 * program must be relocated LAST since it may contain copy
822 * relocations which depend on libraries' relocations. */
823 if (env_preload) load_preload(env_preload);
827 /* Make an initial pass setting up TLS before performing relocs.
828 * This provides the TP-based offset of each DSO's TLS for
829 * use in TP-relative relocations. After relocations, we need
830 * to copy the TLS images again in case they had relocs. */
831 tls_size += sizeof(struct pthread) + 4*sizeof(size_t);
832 __libc.tls_size = tls_size;
833 __libc.tls_cnt = tls_cnt;
836 void *mem = mmap(0, __libc.tls_size, PROT_READ|PROT_WRITE,
837 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
838 if (mem==MAP_FAILED ||
839 !__install_initial_tls(__copy_tls(mem, tls_cnt))) {
840 dprintf(2, "%s: Error getting %zu bytes thread-local storage: %m\n",
844 for (p=head; p; p=p->next) {
845 if (!p->tls_id) continue;
846 p->tls_offset = (char *)__pthread_self()
847 - (char *)__pthread_self()->dtv[p->tls_id];
851 reloc_all(app->next);
854 /* The initial DTV is located at the base of the memory
855 * allocated for TLS. Repeat copying TLS to pick up relocs. */
856 if (tls_cnt) __copy_tls((void *)__pthread_self()->dtv, tls_cnt);
858 if (ldso_fail) _exit(127);
859 if (ldd_mode) _exit(0);
861 /* Switch to runtime mode: any further failures in the dynamic
862 * linker are a reportable failure rather than a fatal startup
863 * error. If the dynamic loader (dlopen) will not be used, free
864 * all memory used by the dynamic linker. */
867 #ifndef DYNAMIC_IS_RO
868 for (i=0; app->dynv[i]; i+=2)
869 if (app->dynv[i]==DT_DEBUG)
870 app->dynv[i+1] = (size_t)&debug;
873 debug.bp = _dl_debug_state;
875 debug.base = lib->base;
879 if (ssp_used) __init_ssp(auxv);
884 return (void *)aux[AT_ENTRY];
887 void *dlopen(const char *file, int mode)
889 struct dso *volatile p, *orig_tail = tail, *next;
893 if (!file) return head;
895 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
896 pthread_rwlock_wrlock(&lock);
898 if (setjmp(rtld_fail)) {
899 /* Clean up anything new that was (partially) loaded */
900 if (p->deps) for (i=0; p->deps[i]; i++)
901 if (p->deps[i]->global < 0)
902 p->deps[i]->global = 0;
903 for (p=orig_tail->next; p; p=next) {
905 munmap(p->map, p->map_len);
914 } else p = load_library(file);
917 snprintf(errbuf, sizeof errbuf,
918 "Error loading shared library %s: %m", file);
923 /* First load handling */
926 if (p->deps) for (i=0; p->deps[i]; i++)
927 if (!p->deps[i]->global)
928 p->deps[i]->global = -1;
929 if (!p->global) p->global = -1;
931 if (p->deps) for (i=0; p->deps[i]; i++)
932 if (p->deps[i]->global < 0)
933 p->deps[i]->global = 0;
934 if (p->global < 0) p->global = 0;
937 if (mode & RTLD_GLOBAL) {
938 if (p->deps) for (i=0; p->deps[i]; i++)
939 p->deps[i]->global = 1;
943 if (ssp_used) __init_ssp(auxv);
949 pthread_rwlock_unlock(&lock);
950 pthread_setcancelstate(cs, 0);
954 static void *do_dlsym(struct dso *p, const char *s, void *ra)
957 uint32_t h = 0, gh = 0;
959 if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
960 if (p == RTLD_NEXT) {
961 for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
964 struct symdef def = find_sym(p->next, s, 0);
965 if (!def.sym) goto failed;
966 return def.dso->base + def.sym->st_value;
970 sym = gnu_lookup(s, gh, p);
973 sym = sysv_lookup(s, h, p);
975 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
976 return p->base + sym->st_value;
977 if (p->deps) for (i=0; p->deps[i]; i++) {
978 if (p->deps[i]->ghashtab) {
979 if (!gh) gh = gnu_hash(s);
980 sym = gnu_lookup(s, gh, p->deps[i]);
982 if (!h) h = sysv_hash(s);
983 sym = sysv_lookup(s, h, p->deps[i]);
985 if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
986 return p->deps[i]->base + sym->st_value;
990 snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
994 int __dladdr(void *addr, Dl_info *info)
1004 pthread_rwlock_rdlock(&lock);
1005 for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
1006 pthread_rwlock_unlock(&lock);
1011 strings = p->strings;
1013 nsym = p->hashtab[1];
1017 buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
1018 sym += p->ghashtab[1];
1019 for (i = 0; i < p->ghashtab[0]; i++) {
1020 if (buckets[i] > nsym)
1024 nsym -= p->ghashtab[1];
1025 hashval = buckets + p->ghashtab[0] + nsym;
1027 while (!(*hashval++ & 1));
1031 for (; nsym; nsym--, sym++) {
1032 if (sym->st_shndx && sym->st_value
1033 && (1<<(sym->st_info&0xf) & OK_TYPES)
1034 && (1<<(sym->st_info>>4) & OK_BINDS)) {
1035 void *symaddr = p->base + sym->st_value;
1036 if (symaddr > addr || symaddr < best)
1039 bestname = strings + sym->st_name;
1040 if (addr == symaddr)
1045 if (!best) return 0;
1047 info->dli_fname = p->name;
1048 info->dli_fbase = p->base;
1049 info->dli_sname = bestname;
1050 info->dli_saddr = best;
1055 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1058 pthread_rwlock_rdlock(&lock);
1059 res = do_dlsym(p, s, ra);
1060 pthread_rwlock_unlock(&lock);
1064 void *dlopen(const char *file, int mode)
1068 void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
1072 int __dladdr (void *addr, Dl_info *info)
1080 if (!errflag) return 0;
1085 int dlclose(void *p)