7 #define START "_dlstart"
13 #define GETFUNCSYM(fp, sym, got) do { \
14 __attribute__((__visibility__("hidden"))) void sym(); \
15 static void (*static_func_ptr)() = sym; \
16 __asm__ __volatile__ ( "" : "+m"(static_func_ptr) : : "memory"); \
17 *(fp) = static_func_ptr; } while(0)
20 __attribute__((__visibility__("hidden")))
21 void _dlstart_c(size_t *sp, size_t *dynv)
23 size_t i, aux[AUX_CNT], dyn[DYN_CNT];
24 size_t *rel, rel_size, base;
27 char **argv = (void *)(sp+1);
29 for (i=argc+1; argv[i]; i++);
30 size_t *auxv = (void *)(argv+i+1);
32 for (i=0; i<AUX_CNT; i++) aux[i] = 0;
33 for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT)
34 aux[auxv[i]] = auxv[i+1];
36 for (i=0; i<DYN_CNT; i++) dyn[i] = 0;
37 for (i=0; dynv[i]; i+=2) if (dynv[i]<DYN_CNT)
38 dyn[dynv[i]] = dynv[i+1];
40 /* If the dynamic linker is invoked as a command, its load
41 * address is not available in the aux vector. Instead, compute
42 * the load address as the difference between &_DYNAMIC and the
43 * virtual address in the PT_DYNAMIC program header. */
46 size_t phnum = aux[AT_PHNUM];
47 size_t phentsize = aux[AT_PHENT];
48 Phdr *ph = (void *)aux[AT_PHDR];
49 for (i=phnum; i--; ph = (void *)((char *)ph + phentsize)) {
50 if (ph->p_type == PT_DYNAMIC) {
51 base = (size_t)dynv - ph->p_vaddr;
57 /* MIPS uses an ugly packed form for GOT relocations. Since we
58 * can't make function calls yet and the code is tiny anyway,
59 * it's simply inlined here. */
60 if (NEED_MIPS_GOT_RELOCS) {
62 size_t *got = (void *)(base + dyn[DT_PLTGOT]);
63 for (i=0; dynv[i]; i+=2) if (dynv[i]==DT_MIPS_LOCAL_GOTNO)
64 local_cnt = dynv[i+1];
65 for (i=0; i<local_cnt; i++) got[i] += base;
68 rel = (void *)(base+dyn[DT_REL]);
69 rel_size = dyn[DT_RELSZ];
70 for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) {
71 if (!IS_RELATIVE(rel[1])) continue;
72 size_t *rel_addr = (void *)(base + rel[0]);
76 rel = (void *)(base+dyn[DT_RELA]);
77 rel_size = dyn[DT_RELASZ];
78 for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
79 if (!IS_RELATIVE(rel[1])) continue;
80 size_t *rel_addr = (void *)(base + rel[0]);
81 *rel_addr = base + rel[2];
85 GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]);
86 dls2((void *)base, sp);