46f4a5c6c2bd7c4a1c5e9342ee918af09dceb02e
[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 __attribute__((__visibility__("hidden")))
13 void _dlstart_c(size_t *sp, size_t *dynv)
14 {
15         size_t i, aux[AUX_CNT], dyn[DYN_CNT];
16
17         int argc = *sp;
18         char **argv = (void *)(sp+1);
19
20         for (i=argc+1; argv[i]; i++);
21         size_t *auxv = (void *)(argv+i+1);
22
23         for (i=0; i<AUX_CNT; i++) aux[i] = 0;
24         for (i=0; auxv[i]; i+=2) if (auxv[i]<AUX_CNT)
25                 aux[auxv[i]] = auxv[i+1];
26
27         for (i=0; i<DYN_CNT; i++) dyn[i] = 0;
28         for (i=0; dynv[i]; i+=2) if (dynv[i]<DYN_CNT)
29                 dyn[dynv[i]] = dynv[i+1];
30
31         /* If the dynamic linker is invoked as a command, its load
32          * address is not available in the aux vector. Instead, compute
33          * the load address as the difference between &_DYNAMIC and the
34          * virtual address in the PT_DYNAMIC program header. */
35         unsigned char *base = (void *)aux[AT_BASE];
36         if (!base) {
37                 size_t phnum = aux[AT_PHNUM];
38                 size_t phentsize = aux[AT_PHENT];
39                 Phdr *ph = (void *)aux[AT_PHDR];
40                 for (i=phnum; i--; ph = (void *)((char *)ph + phentsize)) {
41                         if (ph->p_type == PT_DYNAMIC) {
42                                 base = (void *)((size_t)dynv - ph->p_vaddr);
43                                 break;
44                         }
45                 }
46         }
47
48         /* MIPS uses an ugly packed form for GOT relocations. Since we
49          * can't make function calls yet and the code is tiny anyway,
50          * it's simply inlined here. */
51         if (NEED_MIPS_GOT_RELOCS) {
52                 size_t local_cnt = 0;
53                 size_t *got = (void *)(base + dyn[DT_PLTGOT]);
54                 for (i=0; dynv[i]; i+=2) if (dynv[i]==DT_MIPS_LOCAL_GOTNO)
55                         local_cnt = dynv[i+1];
56                 for (i=0; i<local_cnt; i++) got[i] += (size_t)base;
57         }
58
59         /* The use of the reloc_info structure and nested loops is a trick
60          * to work around the fact that we can't necessarily make function
61          * calls yet. Each struct in the array serves like the arguments
62          * to a function call. */
63         struct {
64                 void *rel;
65                 size_t size;
66                 size_t stride;
67         } reloc_info[] = {
68                 { base+dyn[DT_JMPREL], dyn[DT_PLTRELSZ], 2+(dyn[DT_PLTREL]==DT_RELA) },
69                 { base+dyn[DT_REL], dyn[DT_RELSZ], 2 },
70                 { base+dyn[DT_RELA], dyn[DT_RELASZ], 3 },
71                 { 0, 0, 0 }
72         };
73
74         for (i=0; reloc_info[i].stride; i++) {
75                 size_t *rel = reloc_info[i].rel;
76                 size_t rel_size = reloc_info[i].size;
77                 size_t stride = reloc_info[i].stride;
78                 for (; rel_size; rel+=stride, rel_size-=stride*sizeof(size_t)) {
79                         if (!IS_RELATIVE(rel[1])) continue;
80                         size_t *rel_addr = (void *)(base + rel[0]);
81                         size_t addend = stride==3 ? rel[2] : *rel_addr;
82                         *rel_addr = (size_t)base + addend;
83                 }
84         }
85
86         const char *strings = (void *)(base + dyn[DT_STRTAB]);
87         const Sym *syms = (void *)(base + dyn[DT_SYMTAB]);
88
89         /* Call dynamic linker stage-2, __dls2 */
90         for (i=0; ;i++) {
91                 const char *s = strings + syms[i].st_name;
92                 if (s[0]=='_' && s[1]=='_' && s[2]=='d'
93                  && s[3]=='l' && s[4]=='s' && s[5]=='2' && !s[6])
94                         break;
95         }
96         ((stage2_func)(base + syms[i].st_value))(base);
97
98         /* Call dynamic linker stage-3, __dls3 */
99         for (i=0; ;i++) {
100                 const char *s = strings + syms[i].st_name;
101                 if (s[0]=='_' && s[1]=='_' && s[2]=='d'
102                  && s[3]=='l' && s[4]=='s' && s[5]=='3' && !s[6])
103                         break;
104         }
105         ((stage3_func)(base + syms[i].st_value))(sp);
106 }
107
108 #endif