+ if (DL_FDPIC) {
+ void *p1 = (void *)sp[-2];
+ void *p2 = (void *)sp[-1];
+ if (!p1) {
+ size_t *auxv, aux[AUX_CNT];
+ for (auxv=sp+1+*sp+1; *auxv; auxv++); auxv++;
+ decode_vec(auxv, aux, AUX_CNT);
+ if (aux[AT_BASE]) ldso.base = (void *)aux[AT_BASE];
+ else ldso.base = (void *)(aux[AT_PHDR] & -4096);
+ }
+ app_loadmap = p2 ? p1 : 0;
+ ldso.loadmap = p2 ? p2 : p1;
+ ldso.base = laddr(&ldso, 0);
+ } else {
+ ldso.base = base;
+ }
+ Ehdr *ehdr = (void *)ldso.base;
+ ldso.name = ldso.shortname = "libc.so";
+ ldso.global = 1;
+ ldso.phnum = ehdr->e_phnum;
+ ldso.phdr = laddr(&ldso, ehdr->e_phoff);
+ ldso.phentsize = ehdr->e_phentsize;
+ kernel_mapped_dso(&ldso);
+ decode_dyn(&ldso);
+
+ if (DL_FDPIC) makefuncdescs(&ldso);
+
+ /* Prepare storage for to save clobbered REL addends so they
+ * can be reused in stage 3. There should be very few. If
+ * something goes wrong and there are a huge number, abort
+ * instead of risking stack overflow. */
+ size_t dyn[DYN_CNT];
+ decode_vec(ldso.dynv, dyn, DYN_CNT);
+ size_t *rel = laddr(&ldso, dyn[DT_REL]);
+ size_t rel_size = dyn[DT_RELSZ];
+ size_t symbolic_rel_cnt = 0;
+ apply_addends_to = rel;
+ for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t))
+ if (!IS_RELATIVE(rel[1], ldso.syms)) symbolic_rel_cnt++;
+ if (symbolic_rel_cnt >= ADDEND_LIMIT) a_crash();
+ size_t addends[symbolic_rel_cnt+1];
+ saved_addends = addends;
+
+ head = &ldso;
+ reloc_all(&ldso);
+
+ ldso.relocated = 0;
+
+ /* Call dynamic linker stage-3, __dls3, looking it up
+ * symbolically as a barrier against moving the address
+ * load across the above relocation processing. */
+ struct symdef dls3_def = find_sym(&ldso, "__dls3", 0);
+ if (DL_FDPIC) ((stage3_func)&ldso.funcdescs[dls3_def.sym-ldso.syms])(sp);
+ else ((stage3_func)laddr(&ldso, dls3_def.sym->st_value))(sp);
+}
+
+/* Stage 3 of the dynamic linker is called with the dynamic linker/libc
+ * fully functional. Its job is to load (if not already loaded) and
+ * process dependencies and relocations for the main application and
+ * transfer control to its entry point. */
+
+_Noreturn void __dls3(size_t *sp)
+{
+ static struct dso app, vdso;
+ size_t aux[AUX_CNT], *auxv;