+
+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;
+ }
+
+ _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;
+ 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;
+ }
+ h = hash(s);
+ sym = 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++) {
+ sym = 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;
+}
+
+void *__dlsym(void *p, const char *s, void *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 *p, const char *s, void *ra)
+{
+ return 0;
+}
+#endif
+
+char *dlerror()
+{
+ if (!errflag) return 0;
+ errflag = 0;
+ return errbuf;
+}
+
+int dlclose(void *p)
+{
+ return 0;
+}