xref: /openbsd/lib/libc/dlfcn/init.c (revision 4cfece93)
1 /*	$OpenBSD: init.c,v 1.8 2020/07/06 13:33:05 pirofti Exp $ */
2 /*
3  * Copyright (c) 2014,2015 Philip Guenther <guenther@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 
19 #define _DYN_LOADER
20 
21 #include <sys/types.h>
22 #include <sys/syscall.h>
23 #include <sys/timetc.h>		/* timekeep */
24 
25 #ifndef PIC
26 #include <sys/mman.h>
27 #endif
28 
29 #include <tib.h>
30 #include <limits.h>		/* NAME_MAX */
31 #include <link.h>
32 #include <stdlib.h>		/* atexit */
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include "init.h"
37 
38 #define MAX(a,b)	(((a)>(b))?(a):(b))
39 
40 #ifdef TIB_EXTRA_ALIGN
41 # define TIB_ALIGN	MAX(__alignof__(struct tib), TIB_EXTRA_ALIGN)
42 #else
43 # define TIB_ALIGN	__alignof__(struct tib)
44 #endif
45 
46 /* XXX should be in an include file shared with csu */
47 char	***_csu_finish(char **_argv, char **_envp, void (*_cleanup)(void));
48 
49 /* provide definitions for these */
50 int	_pagesize = 0;
51 struct timekeep	*_timekeep;
52 
53 /*
54  * In dynamicly linked binaries environ and __progname are overriden by
55  * the definitions in ld.so.
56  */
57 char	**environ __attribute__((weak)) = NULL;
58 char	*__progname __attribute__((weak)) = NULL;
59 
60 
61 #ifndef PIC
62 struct dl_phdr_info	_static_phdr_info __relro = { .dlpi_name = "a.out" };
63 
64 static inline void early_static_init(char **_argv, char **_envp);
65 static inline void setup_static_tib(Elf_Phdr *_phdr, int _phnum);
66 
67 /* provided by the linker */
68 extern Elf_Ehdr __executable_start[] __attribute__((weak));
69 #endif /* PIC */
70 
71 /* provide definitions for these */
72 const dl_cb *_dl_cb __relro = NULL;
73 
74 void _libc_preinit(int, char **, char **, dl_cb_cb *) __dso_hidden;
75 void
76 _libc_preinit(int argc, char **argv, char **envp, dl_cb_cb *cb)
77 {
78 	AuxInfo	*aux;
79 #ifndef PIC
80 	Elf_Phdr *phdr = NULL;
81 	int phnum = 0;
82 
83 	/* static libc in a static link? */
84 	if (cb == NULL)
85 		early_static_init(argv, envp);
86 #endif /* !PIC */
87 
88 	if (cb != NULL)
89 		_dl_cb = cb(DL_CB_CUR);
90 
91 	/* Extract useful bits from the auxiliary vector */
92 	while (*envp++ != NULL)
93 		;
94 	for (aux = (void *)envp; aux->au_id != AUX_null; aux++) {
95 		switch (aux->au_id) {
96 		case AUX_pagesz:
97 			_pagesize = aux->au_v;
98 			break;
99 #ifndef PIC
100 		case AUX_base:
101 			_static_phdr_info.dlpi_addr = aux->au_v;
102 			break;
103 		case AUX_phdr:
104 			phdr = (void *)aux->au_v;
105 			break;
106 		case AUX_phnum:
107 			phnum = aux->au_v;
108 			break;
109 #endif /* !PIC */
110 		case AUX_openbsd_timekeep:
111 			if (_tc_get_timecount) {
112 				_timekeep = (void *)aux->au_v;
113 				if (_timekeep &&
114 				    _timekeep->tk_version != TK_VERSION)
115 					_timekeep = NULL;
116 			}
117 			break;
118 		}
119 	}
120 
121 #ifndef PIC
122 	if (cb == NULL && phdr == NULL && __executable_start != NULL) {
123 		/*
124 		 * Static non-PIE processes don't get an AUX vector,
125 		 * so find the phdrs through the ELF header
126 		 */
127 		_static_phdr_info.dlpi_addr = (Elf_Addr)__executable_start;
128 		phdr = (void *)((char *)__executable_start +
129 		    __executable_start->e_phoff);
130 		phnum = __executable_start->e_phnum;
131 	}
132 	_static_phdr_info.dlpi_phdr = phdr;
133 	_static_phdr_info.dlpi_phnum = phnum;
134 
135 	/* static libc in a static link? */
136 	if (cb == NULL)
137 		setup_static_tib(phdr, phnum);
138 #endif /* !PIC */
139 }
140 
141 /* ARM just had to be different... */
142 #ifndef __arm__
143 # define TYPE	"@"
144 #else
145 # define TYPE	"%"
146 #endif
147 
148 #ifdef __LP64__
149 # define VALUE_ALIGN		".balign 8"
150 # define VALUE_DIRECTIVE	".quad"
151 #else
152 # define VALUE_ALIGN		".balign 4"
153 # ifdef __hppa__
154    /* hppa just had to be different: func pointers prefix with 'P%' */
155 #  define VALUE_DIRECTIVE	".int P%"
156 # else
157 #  define VALUE_DIRECTIVE	".int"
158 # endif
159 #endif
160 
161 #define ADD_TO_ARRAY(func, which) \
162 	__asm(	" .section ."#which",\"a\","TYPE#which"\n " \
163 	VALUE_ALIGN"\n "VALUE_DIRECTIVE" "#func"\n .previous")
164 
165 #ifdef PIC
166 ADD_TO_ARRAY(_libc_preinit, init_array);
167 #else
168 ADD_TO_ARRAY(_libc_preinit, preinit_array);
169 #endif
170 
171 /*
172  * In dynamic links, invoke ld.so's dl_clean_boot() callback, if any,
173  * and register its cleanup.
174  */
175 char ***
176 _csu_finish(char **argv, char **envp, void (*cleanup)(void))
177 {
178 	if (_dl_cb != NULL && _dl_cb->dl_clean_boot != NULL)
179 		_dl_cb->dl_clean_boot();
180 
181 	if (cleanup != NULL)
182 		atexit(cleanup);
183 
184 	return &environ;
185 }
186 
187 #ifndef PIC
188 /*
189  * static libc in a static link?  Then disable kbind and set up
190  * __progname and environ
191  */
192 static inline void
193 early_static_init(char **argv, char **envp)
194 {
195 	static char progname_storage[NAME_MAX+1];
196 
197 	/* disable kbind */
198 	syscall(SYS_kbind, (void *)NULL, (size_t)0, (long long)0);
199 
200 	environ = envp;
201 
202 	/* set up __progname */
203 	if (*argv != NULL) {		/* NULL ptr if argc = 0 */
204 		const char *p = strrchr(*argv, '/');
205 
206 		if (p == NULL)
207 			p = *argv;
208 		else
209 			p++;
210 		strlcpy(progname_storage, p, sizeof(progname_storage));
211 	}
212 	__progname = progname_storage;
213 }
214 
215 /*
216  * static TLS handling
217  */
218 #define ELF_ROUND(x,malign)	(((x) + (malign)-1) & ~((malign)-1))
219 
220 /* for static binaries, the location and size of the TLS image */
221 static void		*static_tls __relro;
222 static size_t		static_tls_fsize __relro;
223 
224 size_t			_static_tls_size __relro = 0;
225 int			_static_tls_align __relro;
226 int			_static_tls_align_offset __relro;
227 
228 static inline void
229 setup_static_tib(Elf_Phdr *phdr, int phnum)
230 {
231 	struct tib *tib;
232 	char *base;
233 	int i;
234 
235 	_static_tls_align = TIB_ALIGN;
236 	if (phdr != NULL) {
237 		for (i = 0; i < phnum; i++) {
238 			if (phdr[i].p_type != PT_TLS)
239 				continue;
240 			if (phdr[i].p_memsz == 0)
241 				break;
242 			if (phdr[i].p_memsz < phdr[i].p_filesz)
243 				break;		/* invalid */
244 			if (phdr[i].p_align > getpagesize())
245 				break;		/* nope */
246 			_static_tls_align = MAX(phdr[i].p_align, TIB_ALIGN);
247 #if TLS_VARIANT == 1
248 			/*
249 			 * Variant 1 places the data after the TIB.  If the
250 			 * TLS alignment is larger than the TIB alignment
251 			 * then we may need to pad in front of the TIB to
252 			 * place the TLS data on the proper alignment.
253 			 * Example: p_align=16 sizeof(TIB)=52 align(TIB)=4
254 			 * - need to offset the TIB 12 bytes from the start
255 			 * - to place ths TLS data at offset 64
256 			 */
257 			_static_tls_size = phdr[i].p_memsz;
258 			_static_tls_align_offset =
259 			    ELF_ROUND(sizeof(struct tib), _static_tls_align) -
260 			    sizeof(struct tib);
261 #elif TLS_VARIANT == 2
262 			/*
263 			 * Variant 2 places the data before the TIB
264 			 * so we need to round up the size to the
265 			 * TLS data alignment TIB's alignment.
266 			 * Example A: p_memsz=24 p_align=16 align(TIB)=8
267 			 * - need to allocate 32 bytes for TLS as compiler
268 			 * - will give the first TLS symbol an offset of -32
269 			 * Example B: p_memsz=4 p_align=4 align(TIB)=8
270 			 * - need to allocate 8 bytes so that the TIB is
271 			 * - properly aligned
272 			 */
273 			_static_tls_size = ELF_ROUND(phdr[i].p_memsz,
274 			    phdr[i].p_align);
275 			_static_tls_align_offset = ELF_ROUND(_static_tls_size,
276 			    _static_tls_align) - _static_tls_size;
277 #endif
278 			if (phdr[i].p_vaddr != 0 && phdr[i].p_filesz != 0) {
279 				static_tls = (void *)phdr[i].p_vaddr +
280 				    _static_phdr_info.dlpi_addr;
281 				static_tls_fsize = phdr[i].p_filesz;
282 			}
283 			break;
284 		}
285 	}
286 
287 	base = mmap(NULL, _static_tls_size + _static_tls_align_offset
288 	    + sizeof *tib, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
289 
290 	tib = _static_tls_init(base, NULL);
291 	tib->tib_tid = getthrid();
292 	TCB_SET(TIB_TO_TCB(tib));
293 #if ! TCB_HAVE_MD_GET
294 	_libc_single_tcb = TIB_TO_TCB(tib);
295 #endif
296 }
297 
298 struct tib *
299 _static_tls_init(char *base, void *thread)
300 {
301 	struct tib *tib;
302 
303 	base += _static_tls_align_offset;
304 # if TLS_VARIANT == 1
305 	tib = (struct tib *)base;
306 	base += sizeof(struct tib);
307 # elif TLS_VARIANT == 2
308 	tib = (struct tib *)(base + _static_tls_size);
309 # endif
310 
311 	if (_static_tls_size) {
312 		if (static_tls != NULL)
313 			memcpy(base, static_tls, static_tls_fsize);
314 		memset(base + static_tls_fsize, 0,
315 		    _static_tls_size - static_tls_fsize);
316 	}
317 
318 	TIB_INIT(tib, NULL, thread);
319 	return tib;
320 }
321 #endif /* !PIC */
322