xref: /netbsd/lib/csu/common/crt0-common.c (revision 6beeb802)
1 /* $NetBSD: crt0-common.c,v 1.27 2022/06/21 06:52:17 skrll 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.27 2022/06/21 06:52:17 skrll 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 #include "csu-common.h"
50 
51 extern int main(int, char **, char **);
52 
53 typedef void (*fptr_t)(void);
54 #ifndef HAVE_INITFINI_ARRAY
55 extern void	_init(void);
56 extern void	_fini(void);
57 #endif
58 extern void	_libc_init(void);
59 
60 /*
61  * Arrange for _DYNAMIC to be weak and undefined (and therefore to show up
62  * as being at address zero, unless something else defines it).  That way,
63  * if we happen to be compiling without -static but with without any
64  * shared libs present, things will still work.
65  */
66 
67 __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
68 
69 #ifdef MCRT0
70 extern void	monstartup(u_long, u_long);
71 extern void	_mcleanup(void);
72 extern unsigned char __etext, __eprol;
73 #endif /* MCRT0 */
74 
75 static char	 empty_string[] = "";
76 
77 char		**environ __common;
78 struct ps_strings *__ps_strings __common = 0;
79 char		*__progname __common = empty_string;
80 
81 __dead __dso_hidden void ___start(void (*)(void), struct ps_strings *);
82 
83 #define	write(fd, s, n)	__syscall(SYS_write, (fd), (s), (n))
84 
85 #define	_FATAL(str)				\
86 do {						\
87 	write(2, str, sizeof(str)-1);		\
88 	_exit(1);				\
89 } while (0)
90 
91 /*
92  * If we are using INIT_ARRAY/FINI_ARRAY and we are linked statically,
93  * we have to process these instead of relying on RTLD to do it for us.
94  *
95  * Since we don't need .init or .fini sections, just code them in C
96  * to make life easier.
97  */
98 extern const fptr_t __preinit_array_start[] __dso_hidden;
99 extern const fptr_t __preinit_array_end[] __dso_hidden __weak;
100 extern const fptr_t __init_array_start[] __dso_hidden;
101 extern const fptr_t __init_array_end[] __dso_hidden __weak;
102 extern const fptr_t __fini_array_start[] __dso_hidden;
103 extern const fptr_t __fini_array_end[] __dso_hidden __weak;
104 
105 static inline void
_preinit(void)106 _preinit(void)
107 {
108 	for (const fptr_t *f = __preinit_array_start; f < __preinit_array_end; f++) {
109 		(*f)();
110 	}
111 }
112 
113 static inline void
_initarray(void)114 _initarray(void)
115 {
116 	for (const fptr_t *f = __init_array_start; f < __init_array_end; f++) {
117 		(*f)();
118 	}
119 }
120 
121 static void
_finiarray(void)122 _finiarray(void)
123 {
124 	for (const fptr_t *f = __fini_array_start; f < __fini_array_end; f++) {
125 		(*f)();
126 	}
127 }
128 
129 #if \
130     defined(__aarch64__) || \
131     defined(__powerpc__) || \
132     defined(__sparc__) || \
133     defined(__x86_64__)
134 #define HAS_IPLTA
135 static void fix_iplta(void) __noinline;
136 #elif \
137     defined(__arm__) || \
138     defined(__i386__)
139 #define HAS_IPLT
140 static void fix_iplt(void) __noinline;
141 #endif
142 
143 
144 #ifdef HAS_IPLTA
145 #include <stdio.h>
146 extern const Elf_Rela __rela_iplt_start[] __dso_hidden __weak;
147 extern const Elf_Rela __rela_iplt_end[] __dso_hidden __weak;
148 #ifdef __sparc__
149 #define IFUNC_RELOCATION R_TYPE(JMP_IREL)
150 #include <machine/elf_support.h>
151 #define write_plt(where, value) sparc_write_branch((void *)where, (void *)value)
152 #else
153 #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
154 #define write_plt(where, value) *where = value
155 #endif
156 
157 static void
fix_iplta(void)158 fix_iplta(void)
159 {
160 	const Elf_Rela *rela, *relalim;
161 	uintptr_t relocbase = 0;
162 	Elf_Addr *where, target;
163 
164 	rela = __rela_iplt_start;
165 	relalim = __rela_iplt_end;
166 	for (; rela < relalim; ++rela) {
167 		if (ELF_R_TYPE(rela->r_info) != IFUNC_RELOCATION)
168 			abort();
169 		where = (Elf_Addr *)(relocbase + rela->r_offset);
170 		target = (Elf_Addr)(relocbase + rela->r_addend);
171 		target = ((Elf_Addr(*)(void))target)();
172 		write_plt(where, target);
173 	}
174 }
175 #endif
176 #ifdef HAS_IPLT
177 extern const Elf_Rel __rel_iplt_start[] __dso_hidden __weak;
178 extern const Elf_Rel __rel_iplt_end[] __dso_hidden __weak;
179 #define IFUNC_RELOCATION R_TYPE(IRELATIVE)
180 
181 static void
fix_iplt(void)182 fix_iplt(void)
183 {
184 	const Elf_Rel *rel, *rellim;
185 	uintptr_t relocbase = 0;
186 	Elf_Addr *where, target;
187 
188 	rel = __rel_iplt_start;
189 	rellim = __rel_iplt_end;
190 	for (; rel < rellim; ++rel) {
191 		if (ELF_R_TYPE(rel->r_info) != IFUNC_RELOCATION)
192 			abort();
193 		where = (Elf_Addr *)(relocbase + rel->r_offset);
194 		target = ((Elf_Addr(*)(void))*where)();
195 		*where = target;
196 	}
197 }
198 #endif
199 
200 #if defined(__x86_64__) || defined(__i386__)
201 #  define HAS_RELOCATE_SELF
202 #  if defined(__x86_64__)
203 #  define RELA
204 #  define REL_TAG DT_RELA
205 #  define RELSZ_TAG DT_RELASZ
206 #  define REL_TYPE Elf_Rela
207 #  else
208 #  define REL_TAG DT_REL
209 #  define RELSZ_TAG DT_RELSZ
210 #  define REL_TYPE Elf_Rel
211 #  endif
212 
213 #include <elf.h>
214 
215 static void relocate_self(struct ps_strings *) __noinline;
216 
217 static void
relocate_self(struct ps_strings * ps_strings)218 relocate_self(struct ps_strings *ps_strings)
219 {
220 	AuxInfo *aux = (AuxInfo *)(ps_strings->ps_argvstr + ps_strings->ps_nargvstr +
221 	    ps_strings->ps_nenvstr + 2);
222 	uintptr_t relocbase = (uintptr_t)~0U;
223 	const Elf_Phdr *phdr = NULL;
224 	Elf_Half phnum = (Elf_Half)~0;
225 
226 	for (; aux->a_type != AT_NULL; ++aux) {
227 		switch (aux->a_type) {
228 		case AT_BASE:
229 			if (aux->a_v)
230 				return;
231 			break;
232 		case AT_PHDR:
233 			phdr = (void *)aux->a_v;
234 			break;
235 		case AT_PHNUM:
236 			phnum = (Elf_Half)aux->a_v;
237 			break;
238 		}
239 	}
240 
241 	if (phdr == NULL || phnum == (Elf_Half)~0)
242 		return;
243 
244 	const Elf_Phdr *phlimit = phdr + phnum, *dynphdr = NULL;
245 
246 	for (; phdr < phlimit; ++phdr) {
247 		if (phdr->p_type == PT_DYNAMIC)
248 			dynphdr = phdr;
249 		if (phdr->p_type == PT_PHDR)
250 			relocbase = (uintptr_t)phdr - phdr->p_vaddr;
251 	}
252 	if (dynphdr == NULL || relocbase == (uintptr_t)~0U)
253 		return;
254 
255 	Elf_Dyn *dynp = (Elf_Dyn *)((uint8_t *)dynphdr->p_vaddr + relocbase);
256 
257 	const REL_TYPE *relocs = 0, *relocslim;
258 	Elf_Addr relocssz = 0;
259 
260 	for (; dynp->d_tag != DT_NULL; dynp++) {
261 		switch (dynp->d_tag) {
262 		case REL_TAG:
263 			relocs =
264 			    (const REL_TYPE *)(relocbase + dynp->d_un.d_ptr);
265 			break;
266 		case RELSZ_TAG:
267 			relocssz = dynp->d_un.d_val;
268 			break;
269 		}
270 	}
271 	relocslim = (const REL_TYPE *)((const uint8_t *)relocs + relocssz);
272 	for (; relocs < relocslim; ++relocs) {
273 		Elf_Addr *where;
274 
275 		where = (Elf_Addr *)(relocbase + relocs->r_offset);
276 
277 		switch (ELF_R_TYPE(relocs->r_info)) {
278 		case R_TYPE(RELATIVE):  /* word64 B + A */
279 #ifdef RELA
280 			*where = (Elf_Addr)(relocbase + relocs->r_addend);
281 #else
282 			*where += (Elf_Addr)relocbase;
283 #endif
284 			break;
285 #ifdef IFUNC_RELOCATION
286 		case IFUNC_RELOCATION:
287 			break;
288 #endif
289 		default:
290 			abort();
291 		}
292 	}
293 }
294 #endif
295 
296 void
___start(void (* cleanup)(void),struct ps_strings * ps_strings)297 ___start(void (*cleanup)(void),			/* from shared loader */
298     struct ps_strings *ps_strings)
299 {
300 #if defined(HAS_RELOCATE_SELF)
301 	relocate_self(ps_strings);
302 #endif
303 
304 	if (ps_strings == NULL)
305 		_FATAL("ps_strings missing\n");
306 	__ps_strings = ps_strings;
307 
308 	environ = ps_strings->ps_envstr;
309 
310 	if (ps_strings->ps_argvstr[0] != NULL) {
311 		char *c;
312 		__progname = ps_strings->ps_argvstr[0];
313 		for (c = ps_strings->ps_argvstr[0]; *c; ++c) {
314 			if (*c == '/')
315 				__progname = c + 1;
316 		}
317 	} else {
318 		__progname = empty_string;
319 	}
320 
321 	if (cleanup != NULL)
322 		atexit(cleanup);
323 
324 	_libc_init();
325 
326 	if (&rtld_DYNAMIC == NULL) {
327 #ifdef HAS_IPLTA
328 		fix_iplta();
329 #endif
330 #ifdef HAS_IPLT
331 		fix_iplt();
332 #endif
333 	}
334 
335 	_preinit();
336 
337 #ifdef MCRT0
338 	atexit(_mcleanup);
339 	monstartup((u_long)&__eprol, (u_long)&__etext);
340 #endif
341 
342 	atexit(_finiarray);
343 	_initarray();
344 
345 #ifndef HAVE_INITFINI_ARRAY
346 	atexit(_fini);
347 	_init();
348 #endif
349 
350 	exit(main(ps_strings->ps_nargvstr, ps_strings->ps_argvstr, environ));
351 }
352