crt0-common.c revision 1.14.14.5 1 1.14.14.5 pgoyette /* $NetBSD: crt0-common.c,v 1.14.14.5 2019/01/18 08:50:09 pgoyette Exp $ */
2 1.1 joerg
3 1.1 joerg /*
4 1.1 joerg * Copyright (c) 1998 Christos Zoulas
5 1.1 joerg * Copyright (c) 1995 Christopher G. Demetriou
6 1.1 joerg * All rights reserved.
7 1.1 joerg *
8 1.1 joerg * Redistribution and use in source and binary forms, with or without
9 1.1 joerg * modification, are permitted provided that the following conditions
10 1.1 joerg * are met:
11 1.1 joerg * 1. Redistributions of source code must retain the above copyright
12 1.1 joerg * notice, this list of conditions and the following disclaimer.
13 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 joerg * notice, this list of conditions and the following disclaimer in the
15 1.1 joerg * documentation and/or other materials provided with the distribution.
16 1.1 joerg * 3. All advertising materials mentioning features or use of this software
17 1.1 joerg * must display the following acknowledgement:
18 1.1 joerg * This product includes software developed for the
19 1.1 joerg * NetBSD Project. See http://www.NetBSD.org/ for
20 1.1 joerg * information about NetBSD.
21 1.1 joerg * 4. The name of the author may not be used to endorse or promote products
22 1.1 joerg * derived from this software without specific prior written permission.
23 1.1 joerg *
24 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 1.1 joerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.1 joerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 joerg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1 joerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1 joerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1 joerg * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1 joerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1 joerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1 joerg * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 joerg *
35 1.1 joerg * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
36 1.1 joerg */
37 1.1 joerg
38 1.1 joerg #include <sys/cdefs.h>
39 1.14.14.5 pgoyette __RCSID("$NetBSD: crt0-common.c,v 1.14.14.5 2019/01/18 08:50:09 pgoyette Exp $");
40 1.1 joerg
41 1.1 joerg #include <sys/types.h>
42 1.6 joerg #include <sys/exec.h>
43 1.14.14.4 pgoyette #include <sys/exec_elf.h>
44 1.1 joerg #include <sys/syscall.h>
45 1.1 joerg #include <machine/profile.h>
46 1.1 joerg #include <stdlib.h>
47 1.1 joerg #include <unistd.h>
48 1.1 joerg
49 1.1 joerg extern int main(int, char **, char **);
50 1.1 joerg
51 1.14.14.4 pgoyette typedef void (*fptr_t)(void);
52 1.14.14.5 pgoyette #ifndef HAVE_INITFINI_ARRAY
53 1.1 joerg extern void _init(void);
54 1.1 joerg extern void _fini(void);
55 1.9 matt #endif
56 1.5 joerg extern void _libc_init(void);
57 1.1 joerg
58 1.1 joerg /*
59 1.1 joerg * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
60 1.1 joerg * as being at address zero, unless something else defines it). That way,
61 1.1 joerg * if we happen to be compiling without -static but with without any
62 1.1 joerg * shared libs present, things will still work.
63 1.1 joerg */
64 1.3 joerg
65 1.4 joerg __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
66 1.1 joerg
67 1.1 joerg #ifdef MCRT0
68 1.1 joerg extern void monstartup(u_long, u_long);
69 1.1 joerg extern void _mcleanup(void);
70 1.1 joerg extern unsigned char __etext, __eprol;
71 1.1 joerg #endif /* MCRT0 */
72 1.1 joerg
73 1.1 joerg char **environ;
74 1.1 joerg struct ps_strings *__ps_strings = 0;
75 1.1 joerg
76 1.1 joerg static char empty_string[] = "";
77 1.1 joerg char *__progname = empty_string;
78 1.1 joerg
79 1.14.14.4 pgoyette __dead __dso_hidden void ___start(void (*)(void), struct ps_strings *);
80 1.1 joerg
81 1.1 joerg #define write(fd, s, n) __syscall(SYS_write, (fd), (s), (n))
82 1.1 joerg
83 1.1 joerg #define _FATAL(str) \
84 1.1 joerg do { \
85 1.1 joerg write(2, str, sizeof(str)-1); \
86 1.1 joerg _exit(1); \
87 1.1 joerg } while (0)
88 1.1 joerg
89 1.9 matt /*
90 1.9 matt * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
91 1.9 matt * we have to process these instead of relying on RTLD to do it for us.
92 1.9 matt *
93 1.9 matt * Since we don't need .init or .fini sections, just code them in C
94 1.9 matt * to make life easier.
95 1.9 matt */
96 1.14 joerg extern const fptr_t __preinit_array_start[] __dso_hidden;
97 1.14 joerg extern const fptr_t __preinit_array_end[] __dso_hidden __weak;
98 1.14 joerg extern const fptr_t __init_array_start[] __dso_hidden;
99 1.14 joerg extern const fptr_t __init_array_end[] __dso_hidden __weak;
100 1.14 joerg extern const fptr_t __fini_array_start[] __dso_hidden;
101 1.14 joerg extern const fptr_t __fini_array_end[] __dso_hidden __weak;
102 1.9 matt
103 1.9 matt static inline void
104 1.13 matt _preinit(void)
105 1.13 matt {
106 1.14 joerg for (const fptr_t *f = __preinit_array_start; f < __preinit_array_end; f++) {
107 1.13 matt (*f)();
108 1.13 matt }
109 1.13 matt }
110 1.13 matt
111 1.13 matt static inline void
112 1.14.14.5 pgoyette _initarray(void)
113 1.9 matt {
114 1.14 joerg for (const fptr_t *f = __init_array_start; f < __init_array_end; f++) {
115 1.9 matt (*f)();
116 1.9 matt }
117 1.9 matt }
118 1.9 matt
119 1.9 matt static void
120 1.14.14.5 pgoyette _finiarray(void)
121 1.9 matt {
122 1.14 joerg for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) {
123 1.9 matt (*f)();
124 1.9 matt }
125 1.9 matt }
126 1.9 matt
127 1.14.14.1 pgoyette #if defined(__x86_64__) || defined(__powerpc__) || defined(__sparc__)
128 1.14.14.1 pgoyette #define HAS_IPLTA
129 1.14.14.1 pgoyette static void fix_iplta(void) __noinline;
130 1.14.14.1 pgoyette #elif defined(__i386__) || defined(__arm__)
131 1.14.14.1 pgoyette #define HAS_IPLT
132 1.14.14.1 pgoyette static void fix_iplt(void) __noinline;
133 1.14.14.1 pgoyette #endif
134 1.14.14.1 pgoyette
135 1.14.14.1 pgoyette
136 1.14.14.1 pgoyette #ifdef HAS_IPLTA
137 1.14.14.1 pgoyette #include <stdio.h>
138 1.14.14.1 pgoyette extern const Elf_Rela __rela_iplt_start[] __dso_hidden __weak;
139 1.14.14.1 pgoyette extern const Elf_Rela __rela_iplt_end[] __dso_hidden __weak;
140 1.14.14.2 pgoyette #ifdef __sparc__
141 1.14.14.2 pgoyette #define IFUNC_RELOCATION R_TYPE(JMP_IREL)
142 1.14.14.2 pgoyette #include <machine/elf_support.h>
143 1.14.14.2 pgoyette #define write_plt(where, value) sparc_write_branch((void *)where, (void *)value)
144 1.14.14.2 pgoyette #else
145 1.14.14.2 pgoyette #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
146 1.14.14.2 pgoyette #define write_plt(where, value) *where = value
147 1.14.14.2 pgoyette #endif
148 1.14.14.1 pgoyette
149 1.14.14.1 pgoyette static void
150 1.14.14.1 pgoyette fix_iplta(void)
151 1.14.14.1 pgoyette {
152 1.14.14.1 pgoyette const Elf_Rela *rela, *relalim;
153 1.14.14.1 pgoyette uintptr_t relocbase = 0;
154 1.14.14.1 pgoyette Elf_Addr *where, target;
155 1.14.14.1 pgoyette
156 1.14.14.1 pgoyette rela = __rela_iplt_start;
157 1.14.14.1 pgoyette relalim = __rela_iplt_end;
158 1.14.14.1 pgoyette for (; rela < relalim; ++rela) {
159 1.14.14.2 pgoyette if (ELF_R_TYPE(rela->r_info) != IFUNC_RELOCATION)
160 1.14.14.1 pgoyette abort();
161 1.14.14.1 pgoyette where = (Elf_Addr *)(relocbase + rela->r_offset);
162 1.14.14.1 pgoyette target = (Elf_Addr)(relocbase + rela->r_addend);
163 1.14.14.1 pgoyette target = ((Elf_Addr(*)(void))target)();
164 1.14.14.2 pgoyette write_plt(where, target);
165 1.14.14.1 pgoyette }
166 1.14.14.1 pgoyette }
167 1.14.14.1 pgoyette #endif
168 1.14.14.1 pgoyette #ifdef HAS_IPLT
169 1.14.14.1 pgoyette extern const Elf_Rel __rel_iplt_start[] __dso_hidden __weak;
170 1.14.14.1 pgoyette extern const Elf_Rel __rel_iplt_end[] __dso_hidden __weak;
171 1.14.14.2 pgoyette #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
172 1.14.14.1 pgoyette
173 1.14.14.1 pgoyette static void
174 1.14.14.1 pgoyette fix_iplt(void)
175 1.14.14.1 pgoyette {
176 1.14.14.1 pgoyette const Elf_Rel *rel, *rellim;
177 1.14.14.1 pgoyette uintptr_t relocbase = 0;
178 1.14.14.1 pgoyette Elf_Addr *where, target;
179 1.14.14.1 pgoyette
180 1.14.14.1 pgoyette rel = __rel_iplt_start;
181 1.14.14.1 pgoyette rellim = __rel_iplt_end;
182 1.14.14.1 pgoyette for (; rel < rellim; ++rel) {
183 1.14.14.2 pgoyette if (ELF_R_TYPE(rel->r_info) != IFUNC_RELOCATION)
184 1.14.14.1 pgoyette abort();
185 1.14.14.1 pgoyette where = (Elf_Addr *)(relocbase + rel->r_offset);
186 1.14.14.1 pgoyette target = ((Elf_Addr(*)(void))*where)();
187 1.14.14.1 pgoyette *where = target;
188 1.14.14.1 pgoyette }
189 1.14.14.1 pgoyette }
190 1.14.14.1 pgoyette #endif
191 1.14.14.1 pgoyette
192 1.14.14.3 pgoyette #if defined(__x86_64__) || defined(__i386__)
193 1.14.14.3 pgoyette # define HAS_RELOCATE_SELF
194 1.14.14.3 pgoyette # if defined(__x86_64__)
195 1.14.14.3 pgoyette # define RELA
196 1.14.14.3 pgoyette # define REL_TAG DT_RELA
197 1.14.14.3 pgoyette # define RELSZ_TAG DT_RELASZ
198 1.14.14.3 pgoyette # define REL_TYPE Elf_Rela
199 1.14.14.3 pgoyette # else
200 1.14.14.3 pgoyette # define REL_TAG DT_REL
201 1.14.14.3 pgoyette # define RELSZ_TAG DT_RELSZ
202 1.14.14.3 pgoyette # define REL_TYPE Elf_Rel
203 1.14.14.3 pgoyette # endif
204 1.14.14.3 pgoyette
205 1.14.14.3 pgoyette #include <elf.h>
206 1.14.14.3 pgoyette
207 1.14.14.3 pgoyette static void relocate_self(struct ps_strings *) __noinline;
208 1.14.14.3 pgoyette
209 1.14.14.3 pgoyette static void
210 1.14.14.3 pgoyette relocate_self(struct ps_strings *ps_strings)
211 1.14.14.3 pgoyette {
212 1.14.14.3 pgoyette AuxInfo *aux = (AuxInfo *)(ps_strings->ps_argvstr + ps_strings->ps_nargvstr +
213 1.14.14.3 pgoyette ps_strings->ps_nenvstr + 2);
214 1.14.14.3 pgoyette uintptr_t relocbase = (uintptr_t)~0U;
215 1.14.14.3 pgoyette const Elf_Phdr *phdr = NULL;
216 1.14.14.3 pgoyette Elf_Half phnum = (Elf_Half)~0;
217 1.14.14.3 pgoyette
218 1.14.14.3 pgoyette for (; aux->a_type != AT_NULL; ++aux) {
219 1.14.14.3 pgoyette switch (aux->a_type) {
220 1.14.14.3 pgoyette case AT_BASE:
221 1.14.14.3 pgoyette if (aux->a_v)
222 1.14.14.3 pgoyette return;
223 1.14.14.3 pgoyette break;
224 1.14.14.3 pgoyette case AT_PHDR:
225 1.14.14.3 pgoyette phdr = (void *)aux->a_v;
226 1.14.14.3 pgoyette break;
227 1.14.14.3 pgoyette case AT_PHNUM:
228 1.14.14.3 pgoyette phnum = (Elf_Half)aux->a_v;
229 1.14.14.3 pgoyette break;
230 1.14.14.3 pgoyette }
231 1.14.14.3 pgoyette }
232 1.14.14.3 pgoyette
233 1.14.14.3 pgoyette if (phdr == NULL || phnum == (Elf_Half)~0)
234 1.14.14.3 pgoyette return;
235 1.14.14.3 pgoyette
236 1.14.14.3 pgoyette const Elf_Phdr *phlimit = phdr + phnum, *dynphdr = NULL;
237 1.14.14.3 pgoyette
238 1.14.14.3 pgoyette for (; phdr < phlimit; ++phdr) {
239 1.14.14.3 pgoyette if (phdr->p_type == PT_DYNAMIC)
240 1.14.14.3 pgoyette dynphdr = phdr;
241 1.14.14.3 pgoyette if (phdr->p_type == PT_PHDR)
242 1.14.14.3 pgoyette relocbase = (uintptr_t)phdr - phdr->p_vaddr;
243 1.14.14.3 pgoyette }
244 1.14.14.3 pgoyette if (dynphdr == NULL || relocbase == (uintptr_t)~0U)
245 1.14.14.3 pgoyette return;
246 1.14.14.3 pgoyette
247 1.14.14.3 pgoyette Elf_Dyn *dynp = (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase);
248 1.14.14.3 pgoyette
249 1.14.14.3 pgoyette const REL_TYPE *relocs = 0, *relocslim;
250 1.14.14.3 pgoyette Elf_Addr relocssz = 0;
251 1.14.14.3 pgoyette
252 1.14.14.3 pgoyette for (; dynp->d_tag != DT_NULL; dynp++) {
253 1.14.14.3 pgoyette switch (dynp->d_tag) {
254 1.14.14.3 pgoyette case REL_TAG:
255 1.14.14.3 pgoyette relocs =
256 1.14.14.3 pgoyette (const REL_TYPE *)(relocbase + dynp->d_un.d_ptr);
257 1.14.14.3 pgoyette break;
258 1.14.14.3 pgoyette case RELSZ_TAG:
259 1.14.14.3 pgoyette relocssz = dynp->d_un.d_val;
260 1.14.14.3 pgoyette break;
261 1.14.14.3 pgoyette }
262 1.14.14.3 pgoyette }
263 1.14.14.3 pgoyette relocslim = (const REL_TYPE *)((const uint8_t *)relocs + relocssz);
264 1.14.14.3 pgoyette for (; relocs < relocslim; ++relocs) {
265 1.14.14.3 pgoyette Elf_Addr *where;
266 1.14.14.3 pgoyette
267 1.14.14.3 pgoyette where = (Elf_Addr *)(relocbase + relocs->r_offset);
268 1.14.14.3 pgoyette
269 1.14.14.3 pgoyette switch (ELF_R_TYPE(relocs->r_info)) {
270 1.14.14.3 pgoyette case R_TYPE(RELATIVE): /* word64 B + A */
271 1.14.14.3 pgoyette #ifdef RELA
272 1.14.14.3 pgoyette *where = (Elf_Addr)(relocbase + relocs->r_addend);
273 1.14.14.3 pgoyette #else
274 1.14.14.3 pgoyette *where += (Elf_Addr)relocbase;
275 1.14.14.3 pgoyette #endif
276 1.14.14.3 pgoyette break;
277 1.14.14.3 pgoyette #ifdef IFUNC_RELOCATION
278 1.14.14.3 pgoyette case IFUNC_RELOCATION:
279 1.14.14.3 pgoyette break;
280 1.14.14.3 pgoyette #endif
281 1.14.14.3 pgoyette default:
282 1.14.14.3 pgoyette abort();
283 1.14.14.3 pgoyette }
284 1.14.14.3 pgoyette }
285 1.14.14.3 pgoyette }
286 1.14.14.3 pgoyette #endif
287 1.14.14.3 pgoyette
288 1.1 joerg void
289 1.6 joerg ___start(void (*cleanup)(void), /* from shared loader */
290 1.1 joerg struct ps_strings *ps_strings)
291 1.1 joerg {
292 1.14.14.3 pgoyette #if defined(HAS_RELOCATE_SELF)
293 1.14.14.3 pgoyette relocate_self(ps_strings);
294 1.14.14.3 pgoyette #endif
295 1.1 joerg
296 1.6 joerg if (ps_strings == NULL)
297 1.6 joerg _FATAL("ps_strings missing\n");
298 1.6 joerg __ps_strings = ps_strings;
299 1.6 joerg
300 1.6 joerg environ = ps_strings->ps_envstr;
301 1.6 joerg
302 1.6 joerg if (ps_strings->ps_argvstr[0] != NULL) {
303 1.1 joerg char *c;
304 1.6 joerg __progname = ps_strings->ps_argvstr[0];
305 1.6 joerg for (c = ps_strings->ps_argvstr[0]; *c; ++c) {
306 1.1 joerg if (*c == '/')
307 1.1 joerg __progname = c + 1;
308 1.1 joerg }
309 1.1 joerg } else {
310 1.1 joerg __progname = empty_string;
311 1.1 joerg }
312 1.1 joerg
313 1.14.14.4 pgoyette if (cleanup != NULL)
314 1.1 joerg atexit(cleanup);
315 1.1 joerg
316 1.5 joerg _libc_init();
317 1.5 joerg
318 1.14.14.1 pgoyette if (&rtld_DYNAMIC == NULL) {
319 1.14.14.1 pgoyette #ifdef HAS_IPLTA
320 1.14.14.1 pgoyette fix_iplta();
321 1.14.14.1 pgoyette #endif
322 1.14.14.1 pgoyette #ifdef HAS_IPLT
323 1.14.14.1 pgoyette fix_iplt();
324 1.14.14.1 pgoyette #endif
325 1.14.14.1 pgoyette }
326 1.14.14.1 pgoyette
327 1.13 matt _preinit();
328 1.13 matt
329 1.1 joerg #ifdef MCRT0
330 1.1 joerg atexit(_mcleanup);
331 1.1 joerg monstartup((u_long)&__eprol, (u_long)&__etext);
332 1.1 joerg #endif
333 1.1 joerg
334 1.14.14.5 pgoyette atexit(_finiarray);
335 1.14.14.5 pgoyette _initarray();
336 1.14.14.5 pgoyette
337 1.14.14.5 pgoyette #ifndef HAVE_INITFINI_ARRAY
338 1.1 joerg atexit(_fini);
339 1.1 joerg _init();
340 1.14.14.5 pgoyette #endif
341 1.1 joerg
342 1.6 joerg exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));
343 1.1 joerg }
344