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