size_t map_len;
dev_t dev;
ino_t ino;
- int global;
- int relocated;
+ char global;
+ char relocated;
+ char constructed;
struct dso **deps;
char *name;
char buf[];
return 0;
}
+static void do_init_fini(struct dso *p)
+{
+ size_t dyn[DYN_CNT] = {0};
+ for (; p; p=p->prev) {
+ if (p->constructed) return;
+ decode_vec(p->dynv, dyn, DYN_CNT);
+ if (dyn[0] & (1<<DT_FINI))
+ atexit((void (*)(void))(p->base + dyn[DT_FINI]));
+ if (dyn[0] & (1<<DT_INIT))
+ ((void (*)(void))(p->base + dyn[DT_INIT]))();
+ p->constructed = 1;
+ }
+}
+
void *__dynlink(int argc, char **argv)
{
size_t *auxv, aux[AUX_CNT] = {0};
ehdr->e_phnum, ehdr->e_phentsize));
decode_dyn(lib);
- /* Assume base address of 0 for the main program. This is not
- * valid for PIE code; we will have to search the PHDR to get
- * the correct load address in the PIE case (not yet supported). */
+ /* Find load address of the main program, via AT_PHDR vs PT_PHDR. */
app->base = 0;
+ phdr = (void *)aux[AT_PHDR];
+ for (i=aux[AT_PHNUM]; i; i--, phdr=(void *)((char *)phdr + aux[AT_PHENT])) {
+ if (phdr->p_type == PT_PHDR)
+ app->base = (void *)(aux[AT_PHDR] - phdr->p_vaddr);
+ }
app->name = argv[0];
app->global = 1;
+ app->constructed = 1;
app->dynv = (void *)(app->base + find_dyn(
(void *)aux[AT_PHDR], aux[AT_PHNUM], aux[AT_PHENT]));
decode_dyn(app);
ehdr->e_phentsize, ehdr->e_phnum);
/* Load preload/needed libraries, add their symbols to the global
- * namespace, and perform all remaining relocations. */
+ * namespace, and perform all remaining relocations. The main
+ * program must be relocated LAST since it may contain copy
+ * relocations which depend on libraries' relocations. */
if (env_preload) load_preload(env_preload);
load_deps(app);
make_global(app);
+ reloc_all(app->next);
reloc_all(app);
/* Switch to runtime mode: any further failures in the dynamic
* error. If the dynamic loader (dlopen) will not be used, free
* all memory used by the dynamic linker. */
runtime = 1;
+
+ do_init_fini(tail);
+
if (!rtld_used) {
free_all(head);
free(sys_path);
{
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)) {
}
tail = orig_tail;
tail->next = 0;
- pthread_rwlock_unlock(&lock);
- return 0;
+ p = 0;
+ goto end;
}
p = load_library(file);
p->global = 1;
}
+ do_init_fini(tail);
end:
pthread_rwlock_unlock(&lock);
+ pthread_setcancelstate(cs, 0);
return p;
}
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()
{
{
return 0;
}
-#endif