a968baab9ca3134eb4fa792cc4e60216d2422f12
[musl] / src / ldso / dlstart.c
1 #include <stddef.h>
2 #include "dynlink.h"
3
4 #ifdef SHARED
5
6 #ifndef START
7 #define START "_dlstart"
8 #endif
9
10 #include "crt_arch.h"
11
12 #ifndef GETFUNCSYM
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)
18 #endif
19
20 __attribute__((__visibility__("hidden")))
21 void _dlstart_c(size_t *sp, size_t *dynv)
22 {
23         size_t i, aux[AUX_CNT], dyn[DYN_CNT];
24
25         int argc = *sp;
26         char **argv = (void *)(sp+1);
27
28         for (i=argc+1; argv[i]; i++);
29         size_t *auxv = (void *)(argv+i+1);
30
31         for (i=0; i<AUX_CNT; i++) aux[i] = 0;
32         for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT)
33                 aux[auxv[i]] = auxv[i+1];
34
35         for (i=0; i<DYN_CNT; i++) dyn[i] = 0;
36         for (i=0; dynv[i]; i+=2) if (dynv[i]<DYN_CNT)
37                 dyn[dynv[i]] = dynv[i+1];
38
39         /* If the dynamic linker is invoked as a command, its load
40          * address is not available in the aux vector. Instead, compute
41          * the load address as the difference between &_DYNAMIC and the
42          * virtual address in the PT_DYNAMIC program header. */
43         unsigned char *base = (void *)aux[AT_BASE];
44         if (!base) {
45                 size_t phnum = aux[AT_PHNUM];
46                 size_t phentsize = aux[AT_PHENT];
47                 Phdr *ph = (void *)aux[AT_PHDR];
48                 for (i=phnum; i--; ph = (void *)((char *)ph + phentsize)) {
49                         if (ph->p_type == PT_DYNAMIC) {
50                                 base = (void *)((size_t)dynv - ph->p_vaddr);
51                                 break;
52                         }
53                 }
54         }
55
56         /* MIPS uses an ugly packed form for GOT relocations. Since we
57          * can't make function calls yet and the code is tiny anyway,
58          * it's simply inlined here. */
59         if (NEED_MIPS_GOT_RELOCS) {
60                 size_t local_cnt = 0;
61                 size_t *got = (void *)(base + dyn[DT_PLTGOT]);
62                 for (i=0; dynv[i]; i+=2) if (dynv[i]==DT_MIPS_LOCAL_GOTNO)
63                         local_cnt = dynv[i+1];
64                 for (i=0; i<local_cnt; i++) got[i] += (size_t)base;
65         }
66
67         size_t *rel, rel_size;
68
69         rel = (void *)(base+dyn[DT_REL]);
70         rel_size = dyn[DT_RELSZ];
71         for (; rel_size; rel+=2, rel_size-=2*sizeof(size_t)) {
72                 if (!IS_RELATIVE(rel[1])) continue;
73                 size_t *rel_addr = (void *)(base + rel[0]);
74                 *rel_addr += (size_t)base;
75         }
76
77         rel = (void *)(base+dyn[DT_RELA]);
78         rel_size = dyn[DT_RELASZ];
79         for (; rel_size; rel+=3, rel_size-=3*sizeof(size_t)) {
80                 if (!IS_RELATIVE(rel[1])) continue;
81                 size_t *rel_addr = (void *)(base + rel[0]);
82                 *rel_addr = (size_t)base + rel[2];
83         }
84
85         stage2_func dls2;
86         GETFUNCSYM(&dls2, __dls2, base+dyn[DT_PLTGOT]);
87         dls2(base, sp);
88 }
89
90 #endif