• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdint.h>
2 #include <features.h>
3 
__fdpic_fixup(void * map,uintptr_t * a,uintptr_t * z)4 hidden void *__fdpic_fixup(void *map, uintptr_t *a, uintptr_t *z)
5 {
6 	/* If map is a null pointer, the program was loaded by a
7 	 * non-FDPIC-aware ELF loader, and fixups are not needed,
8 	 * but the value for the GOT pointer is. */
9 	if (!map) return (void *)z[-1];
10 
11 	struct {
12 		unsigned short version, nsegs;
13 		struct fdpic_loadseg {
14 			uintptr_t addr, p_vaddr, p_memsz;
15 		} segs[];
16 	} *lm = map;
17 	int nsegs = lm->nsegs, rseg = 0, vseg = 0;
18 	for (;;) {
19 		while (*a-lm->segs[rseg].p_vaddr >= lm->segs[rseg].p_memsz)
20 			if (++rseg == nsegs) rseg = 0;
21 		uintptr_t *r = (uintptr_t *)
22 			(*a + lm->segs[rseg].addr - lm->segs[rseg].p_vaddr);
23 		if (++a == z) return r;
24 		while (*r-lm->segs[vseg].p_vaddr >= lm->segs[vseg].p_memsz)
25 			if (++vseg == nsegs) vseg = 0;
26 		*r += lm->segs[vseg].addr - lm->segs[vseg].p_vaddr;
27 	}
28 }
29