+
+void *dlopen(const char *file, int mode)
+{
+ struct dso *volatile p, *orig_tail, *next;
+ size_t orig_tls_cnt, orig_tls_offset, orig_tls_align;
+ size_t i;
+ int cs;
+
+ if (!file) return head;
+
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ pthread_rwlock_wrlock(&lock);
+ __inhibit_ptc();
+
+ p = 0;
+ orig_tls_cnt = tls_cnt;
+ orig_tls_offset = tls_offset;
+ orig_tls_align = tls_align;
+ orig_tail = tail;
+
+ if (setjmp(rtld_fail)) {
+ /* Clean up anything new that was (partially) loaded */
+ if (p && p->deps) for (i=0; p->deps[i]; i++)
+ if (p->deps[i]->global < 0)
+ p->deps[i]->global = 0;
+ for (p=orig_tail->next; p; p=next) {
+ next = p->next;
+ munmap(p->map, p->map_len);
+ free(p->deps);
+ free(p);
+ }
+ tls_cnt = orig_tls_cnt;
+ tls_offset = orig_tls_offset;
+ tls_align = orig_tls_align;
+ tail = orig_tail;
+ tail->next = 0;
+ p = 0;
+ errflag = 1;
+ goto end;
+ } else p = load_library(file);
+
+ if (!p) {
+ snprintf(errbuf, sizeof errbuf,
+ "Error loading shared library %s: %m", file);
+ errflag = 1;
+ goto end;
+ }
+
+ /* First load handling */
+ if (!p->deps) {
+ load_deps(p);
+ if (p->deps) for (i=0; p->deps[i]; i++)
+ if (!p->deps[i]->global)
+ p->deps[i]->global = -1;
+ if (!p->global) p->global = -1;
+ reloc_all(p);
+ if (p->deps) for (i=0; p->deps[i]; i++)
+ if (p->deps[i]->global < 0)
+ p->deps[i]->global = 0;
+ if (p->global < 0) p->global = 0;
+ }
+
+ if (mode & RTLD_GLOBAL) {
+ if (p->deps) for (i=0; p->deps[i]; i++)
+ p->deps[i]->global = 1;
+ p->global = 1;
+ }
+
+ update_tls_size();
+
+ if (ssp_used) __init_ssp(libc.auxv);
+
+ _dl_debug_state();
+ orig_tail = tail;
+end:
+ __release_ptc();
+ if (p) gencnt++;
+ pthread_rwlock_unlock(&lock);
+ if (p) do_init_fini(orig_tail);
+ pthread_setcancelstate(cs, 0);
+ return p;
+}
+
+static void *do_dlsym(struct dso *p, const char *s, void *ra)
+{
+ size_t i;
+ uint32_t h = 0, gh = 0;
+ Sym *sym;
+ if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
+ if (p == RTLD_DEFAULT) {
+ p = head;
+ } else if (p == RTLD_NEXT) {
+ for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
+ if (!p) p=head;
+ p = p->next;
+ }
+ struct symdef def = find_sym(p, s, 0);
+ if (!def.sym) goto failed;
+ if ((def.sym->st_info&0xf) == STT_TLS)
+ return __tls_get_addr((size_t []){def.dso->tls_id, def.sym->st_value});
+ return def.dso->base + def.sym->st_value;
+ }
+ if (p->ghashtab) {
+ gh = gnu_hash(s);
+ sym = gnu_lookup(s, gh, p);
+ } else {
+ h = sysv_hash(s);
+ sym = sysv_lookup(s, h, p);
+ }
+ if (sym && (sym->st_info&0xf) == STT_TLS)
+ return __tls_get_addr((size_t []){p->tls_id, sym->st_value});
+ if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
+ return p->base + sym->st_value;
+ if (p->deps) for (i=0; p->deps[i]; i++) {
+ if (p->deps[i]->ghashtab) {
+ if (!gh) gh = gnu_hash(s);
+ sym = gnu_lookup(s, gh, p->deps[i]);
+ } else {
+ if (!h) h = sysv_hash(s);
+ sym = sysv_lookup(s, h, p->deps[i]);
+ }
+ if (sym && (sym->st_info&0xf) == STT_TLS)
+ return __tls_get_addr((size_t []){p->deps[i]->tls_id, sym->st_value});
+ if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
+ return p->deps[i]->base + sym->st_value;
+ }
+failed:
+ errflag = 1;
+ snprintf(errbuf, sizeof errbuf, "Symbol not found: %s", s);
+ return 0;
+}
+
+int __dladdr(void *addr, Dl_info *info)
+{
+ struct dso *p;
+ Sym *sym;
+ uint32_t nsym;
+ char *strings;
+ size_t i;
+ void *best = 0;
+ char *bestname;
+
+ pthread_rwlock_rdlock(&lock);
+ for (p=head; p && (unsigned char *)addr-p->map>p->map_len; p=p->next);
+ pthread_rwlock_unlock(&lock);
+
+ if (!p) return 0;
+
+ sym = p->syms;
+ strings = p->strings;
+ if (p->hashtab) {
+ nsym = p->hashtab[1];
+ } else {
+ uint32_t *buckets;
+ uint32_t *hashval;
+ buckets = p->ghashtab + 4 + (p->ghashtab[2]*sizeof(size_t)/4);
+ sym += p->ghashtab[1];
+ for (i = 0; i < p->ghashtab[0]; i++) {
+ if (buckets[i] > nsym)
+ nsym = buckets[i];
+ }
+ if (nsym) {
+ nsym -= p->ghashtab[1];
+ hashval = buckets + p->ghashtab[0] + nsym;
+ do nsym++;
+ while (!(*hashval++ & 1));
+ }
+ }
+
+ for (; nsym; nsym--, sym++) {
+ if (sym->st_shndx && sym->st_value
+ && (1<<(sym->st_info&0xf) & OK_TYPES)
+ && (1<<(sym->st_info>>4) & OK_BINDS)) {
+ void *symaddr = p->base + sym->st_value;
+ if (symaddr > addr || symaddr < best)
+ continue;
+ best = symaddr;
+ bestname = strings + sym->st_name;
+ if (addr == symaddr)
+ break;
+ }
+ }
+
+ if (!best) return 0;
+
+ info->dli_fname = p->name;
+ info->dli_fbase = p->base;
+ info->dli_sname = bestname;
+ info->dli_saddr = best;
+
+ return 1;
+}
+
+void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
+{
+ void *res;
+ pthread_rwlock_rdlock(&lock);
+ res = do_dlsym(p, s, ra);
+ pthread_rwlock_unlock(&lock);
+ return res;
+}
+
+int dl_iterate_phdr(int(*callback)(struct dl_phdr_info *info, size_t size, void *data), void *data)
+{
+ struct dso *current;
+ struct dl_phdr_info info;
+ int ret = 0;
+ for(current = head; current;) {
+ info.dlpi_addr = (uintptr_t)current->base;
+ info.dlpi_name = current->name;
+ info.dlpi_phdr = current->phdr;
+ info.dlpi_phnum = current->phnum;
+ info.dlpi_adds = gencnt;
+ info.dlpi_subs = 0;
+ info.dlpi_tls_modid = current->tls_id;
+ info.dlpi_tls_data = current->tls_image;
+
+ ret = (callback)(&info, sizeof (info), data);
+
+ if (ret != 0) break;
+
+ pthread_rwlock_rdlock(&lock);
+ current = current->next;
+ pthread_rwlock_unlock(&lock);
+ }
+ return ret;
+}
+#else
+void *dlopen(const char *file, int mode)
+{
+ return 0;
+}
+void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
+{
+ return 0;
+}
+int __dladdr (void *addr, Dl_info *info)
+{
+ return 0;
+}
+#endif
+
+char *dlerror()
+{
+ if (!errflag) return 0;
+ errflag = 0;
+ return errbuf;
+}
+
+int dlclose(void *p)
+{
+ return 0;
+}