- app = (struct dso){
- .base = 0,
- .strings = (void *)(app_dyn[DT_STRTAB]),
- .hashtab = (void *)(app_dyn[DT_HASH]),
- .syms = (void *)(app_dyn[DT_SYMTAB]),
- .dynv = (void *)(phdr->p_vaddr),
- .name = argv[0],
- .global = 1,
- .next = &lib
- };
-
- lib = (struct dso){
- .base = (void *)aux[AT_BASE],
- .strings = (void *)(aux[AT_BASE]+lib_dyn[DT_STRTAB]),
- .hashtab = (void *)(aux[AT_BASE]+lib_dyn[DT_HASH]),
- .syms = (void *)(aux[AT_BASE]+lib_dyn[DT_SYMTAB]),
- .dynv = (void *)(got[0]),
- .name = "libc.so",
- .global = 1,
- .relocated = 1
- };
-
- /* Relocate the dynamic linker/libc */
- do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_REL]),
- lib_dyn[DT_RELSZ], 2, lib.syms, lib.strings, &app);
- do_relocs((void *)aux[AT_BASE], (void *)(aux[AT_BASE]+lib_dyn[DT_RELA]),
- lib_dyn[DT_RELASZ], 3, lib.syms, lib.strings, &app);
-
- /* At this point the standard library is fully functional */
-
- reclaim_gaps(app.base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
- ehdr = (void *)lib.base;
- reclaim_gaps(lib.base, (void *)(lib.base+ehdr->e_phoff),
+ /* Initial dso chain consists only of the app. We temporarily
+ * append the dynamic linker/libc so we can relocate it, then
+ * restore the initial chain in preparation for loading third
+ * party libraries (preload/needed). */
+ head = tail = app;
+ libc = lib;
+ app->next = lib;
+ reloc_all(lib);
+ app->next = 0;
+
+ /* PAST THIS POINT, ALL LIBC INTERFACES ARE FULLY USABLE. */
+
+ /* Donate unused parts of app and library mapping to malloc */
+ reclaim_gaps(app->base, (void *)aux[AT_PHDR], aux[AT_PHENT], aux[AT_PHNUM]);
+ ehdr = (void *)lib->base;
+ reclaim_gaps(lib->base, (void *)(lib->base+ehdr->e_phoff),