+
+void *dlopen(const char *file, int mode)
+{
+ struct dso *volatile p, *orig_tail = tail, *next;
+ size_t i;
+ int cs;
+
+ if (!file) return head;
+
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ pthread_rwlock_wrlock(&lock);
+
+ if (setjmp(rtld_fail)) {
+ /* Clean up anything new that was (partially) loaded */
+ if (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);
+ }
+ 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;
+ }
+
+ if (ssp_used) __init_ssp(auxv);
+
+ _dl_debug_state();
+
+ do_init_fini(tail);
+end:
+ pthread_rwlock_unlock(&lock);
+ 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 == RTLD_NEXT) {
+ for (p=head; p && (unsigned char *)ra-p->map>p->map_len; p=p->next);
+ if (!p) p=head;
+ void *res = find_sym(p->next, s, 0);
+ if (!res) goto failed;
+ return res;
+ }
+ if (p == head || p == RTLD_DEFAULT) {
+ void *res = find_sym(head, s, 0);
+ if (!res) goto failed;
+ return res;
+ }
+ 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_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_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;
+}
+#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;
+}