+
+ CRTJMP((void *)aux[AT_ENTRY], argv-1);
+ for(;;);
+}
+
+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;
+ jmp_buf jb;
+
+ 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;
+ noload = mode & RTLD_NOLOAD;
+
+ rtld_fail = &jb;
+ 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;
+ while (p->td_index) {
+ void *tmp = p->td_index->next;
+ free(p->td_index);
+ p->td_index = tmp;
+ }
+ free(p->funcdescs);
+ if (p->rpath != p->rpath_orig)
+ free(p->rpath);
+ free(p->deps);
+ unmap_library(p);
+ 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;
+ goto end;
+ } else p = load_library(file, head);
+
+ if (!p) {
+ error(noload ?
+ "Library %s is not already loaded" :
+ "Error loading shared library %s: %m",
+ file);
+ 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();
+ _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 int invalid_dso_handle(void *h)
+{
+ struct dso *p;
+ for (p=head; p; p=p->next) if (h==p) return 0;
+ error("Invalid library handle %p", (void *)h);
+ return 1;
+}
+
+static void *addr2dso(size_t a)
+{
+ struct dso *p;
+ for (p=head; p; p=p->next) {
+ if (DL_FDPIC && p->loadmap) {
+ size_t i;
+ for (i=0; i<p->loadmap->nsegs; i++) {
+ if (a-p->loadmap->segs[i].p_vaddr
+ < p->loadmap->segs[i].p_memsz)
+ return p;
+ }
+ i = count_syms(p);
+ if (a-(size_t)p->funcdescs < i*sizeof(*p->funcdescs))
+ return p;
+ } else {
+ if (a-(size_t)p->map < p->map_len)
+ return p;
+ }
+ }
+ return 0;
+}
+
+void *__tls_get_addr(size_t *);
+
+static void *do_dlsym(struct dso *p, const char *s, void *ra)
+{
+ size_t i;
+ uint32_t h = 0, gh = 0, *ght;
+ Sym *sym;
+ if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
+ if (p == RTLD_DEFAULT) {
+ p = head;
+ } else if (p == RTLD_NEXT) {
+ p = addr2dso((size_t)ra);
+ 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});
+ if (DL_FDPIC && (def.sym->st_info&0xf) == STT_FUNC)
+ return def.dso->funcdescs + (def.sym - def.dso->syms);
+ return laddr(def.dso, def.sym->st_value);
+ }
+ if (invalid_dso_handle(p))
+ return 0;
+ if ((ght = p->ghashtab)) {
+ gh = gnu_hash(s);
+ sym = gnu_lookup(gh, ght, p, s);
+ } 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 (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == STT_FUNC)
+ return p->funcdescs + (sym - p->syms);
+ if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
+ return laddr(p, sym->st_value);
+ if (p->deps) for (i=0; p->deps[i]; i++) {
+ if ((ght = p->deps[i]->ghashtab)) {
+ if (!gh) gh = gnu_hash(s);
+ sym = gnu_lookup(gh, ght, p->deps[i], s);
+ } 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 (DL_FDPIC && sym && sym->st_shndx && (sym->st_info&0xf) == STT_FUNC)
+ return p->deps[i]->funcdescs + (sym - p->deps[i]->syms);
+ if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
+ return laddr(p->deps[i], sym->st_value);
+ }
+failed:
+ error("Symbol not found: %s", s);
+ return 0;
+}
+
+int __dladdr(const void *addr, Dl_info *info)
+{
+ struct dso *p;
+ Sym *sym;
+ uint32_t nsym;
+ char *strings;
+ void *best = 0;
+ char *bestname;
+
+ pthread_rwlock_rdlock(&lock);
+ p = addr2dso((size_t)addr);
+ pthread_rwlock_unlock(&lock);
+
+ if (!p) return 0;
+
+ sym = p->syms;
+ strings = p->strings;
+ nsym = count_syms(p);
+
+ for (; nsym; nsym--, sym++) {
+ if (sym->st_value
+ && (1<<(sym->st_info&0xf) & OK_TYPES)
+ && (1<<(sym->st_info>>4) & OK_BINDS)) {
+ void *symaddr = laddr(p, 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;
+}
+
+__attribute__((__visibility__("hidden")))
+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
+static int invalid_dso_handle(void *h)
+{
+ error("Invalid library handle %p", (void *)h);
+ return 1;
+}
+void *dlopen(const char *file, int mode)
+{
+ error("Dynamic loading not supported");
+ return 0;
+}
+void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
+{
+ error("Symbol not found: %s", s);
+ return 0;
+}
+int __dladdr (const void *addr, Dl_info *info)
+{
+ return 0;
+}
+#endif
+
+int __dlinfo(void *dso, int req, void *res)
+{
+ if (invalid_dso_handle(dso)) return -1;
+ if (req != RTLD_DI_LINKMAP) {
+ error("Unsupported request %d", req);
+ return -1;
+ }
+ *(struct link_map **)res = dso;
+ return 0;
+}
+
+char *dlerror()
+{
+ pthread_t self = __pthread_self();
+ if (!self->dlerror_flag) return 0;
+ self->dlerror_flag = 0;
+ char *s = self->dlerror_buf;
+ if (s == (void *)-1)
+ return "Dynamic linker failed to allocate memory for error message";
+ else
+ return s;
+}
+
+int dlclose(void *p)
+{
+ return invalid_dso_handle(p);
+}
+
+void __dl_thread_cleanup(void)
+{
+ pthread_t self = __pthread_self();
+ if (self->dlerror_buf != (void *)-1)
+ free(self->dlerror_buf);
+}
+
+static void error(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+#ifdef SHARED
+ if (!runtime) {
+ vdprintf(2, fmt, ap);
+ dprintf(2, "\n");
+ ldso_fail = 1;
+ va_end(ap);
+ return;
+ }
+#endif
+ pthread_t self = __pthread_self();
+ if (self->dlerror_buf != (void *)-1)
+ free(self->dlerror_buf);
+ size_t len = vsnprintf(0, 0, fmt, ap);
+ va_end(ap);
+ char *buf = malloc(len+1);
+ if (buf) {
+ va_start(ap, fmt);
+ vsnprintf(buf, len+1, fmt, ap);
+ va_end(ap);
+ } else {
+ buf = (void *)-1;
+ }
+ self->dlerror_buf = buf;
+ self->dlerror_flag = 1;