2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2010, 2012 Konstantin Belousov <kib@FreeBSD.ORG>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "namespace.h"
39 #include "un-namespace.h"
40 #include "libc_private.h"
42 extern char **environ;
46 void *__elf_aux_vector;
47 static pthread_once_t aux_vector_once = PTHREAD_ONCE_INIT;
50 init_aux_vector_once(void)
54 sp = (Elf_Addr *)environ;
57 __elf_aux_vector = (Elf_Auxinfo *)sp;
61 __init_elf_aux_vector(void)
64 if (&_DYNAMIC != NULL)
66 _once(&aux_vector_once, init_aux_vector_once);
69 static pthread_once_t aux_once = PTHREAD_ONCE_INIT;
70 static int pagesize, osreldate, canary_len, ncpus, pagesizes_len, bsdflags;
71 static int hwcap_present, hwcap2_present;
72 static char *canary, *pagesizes, *execpath;
73 static void *ps_strings, *timekeep;
74 static u_long hwcap, hwcap2;
75 static void *fxrng_seed_version;
78 static int powerpc_new_auxv_format = 0;
79 static void _init_aux_powerpc_fixup(void);
80 int _powerpc_elf_aux_info(int, void *, int);
88 for (aux = __elf_aux_vector; aux->a_type != AT_NULL; aux++) {
89 switch (aux->a_type) {
91 bsdflags = aux->a_un.a_val;
95 canary = (char *)(aux->a_un.a_ptr);
99 canary_len = aux->a_un.a_val;
103 execpath = (char *)(aux->a_un.a_ptr);
108 hwcap = (u_long)(aux->a_un.a_val);
113 hwcap2 = (u_long)(aux->a_un.a_val);
117 pagesizes = (char *)(aux->a_un.a_ptr);
120 case AT_PAGESIZESLEN:
121 pagesizes_len = aux->a_un.a_val;
125 pagesize = aux->a_un.a_val;
129 osreldate = aux->a_un.a_val;
133 ncpus = aux->a_un.a_val;
137 timekeep = aux->a_un.a_ptr;
141 ps_strings = aux->a_un.a_ptr;
145 fxrng_seed_version = aux->a_un.a_ptr;
149 * Since AT_STACKPROT is always set, and the common
150 * value 23 is mutually exclusive with the legacy powerpc
151 * value 21, the existence of AT_STACKPROT proves we are
152 * on the common format.
154 case AT_STACKPROT: /* 23 */
155 powerpc_new_auxv_format = 1;
161 if (!powerpc_new_auxv_format)
162 _init_aux_powerpc_fixup();
168 _init_aux_powerpc_fixup(void)
173 * Before 1300070, PowerPC platforms had nonstandard numbering for
174 * the aux vector. When running old binaries, the kernel will pass
175 * the vector using the old numbering. Reload affected variables.
177 for (aux = __elf_aux_vector; aux->a_type != AT_NULL; aux++) {
178 switch (aux->a_type) {
180 canary = (char *)(aux->a_un.a_ptr);
182 case AT_OLD_CANARYLEN:
183 canary_len = aux->a_un.a_val;
185 case AT_OLD_EXECPATH:
186 execpath = (char *)(aux->a_un.a_ptr);
188 case AT_OLD_PAGESIZES:
189 pagesizes = (char *)(aux->a_un.a_ptr);
191 case AT_OLD_PAGESIZESLEN:
192 pagesizes_len = aux->a_un.a_val;
194 case AT_OLD_OSRELDATE:
195 osreldate = aux->a_un.a_val;
198 ncpus = aux->a_un.a_val;
205 _powerpc_elf_aux_info(int aux, void *buf, int buflen)
209 * If we are in the old auxv format, we need to translate the aux
210 * parameter of elf_aux_info() calls into the common auxv format.
211 * Internal libc calls always use the common format, and they
212 * directly call _elf_aux_info instead of using the weak symbol.
214 if (!powerpc_new_auxv_format) {
216 case AT_OLD_EXECPATH:
222 case AT_OLD_CANARYLEN:
225 case AT_OLD_OSRELDATE:
231 case AT_OLD_PAGESIZES:
234 case AT_OLD_PAGESIZESLEN:
235 aux = AT_PAGESIZESLEN;
237 case AT_OLD_STACKPROT:
242 return _elf_aux_info(aux, buf, buflen);
244 __weak_reference(_powerpc_elf_aux_info, elf_aux_info);
246 __weak_reference(_elf_aux_info, elf_aux_info);
250 _elf_aux_info(int aux, void *buf, int buflen)
254 __init_elf_aux_vector();
255 if (__elf_aux_vector == NULL)
257 _once(&aux_once, init_aux);
264 if (canary != NULL && canary_len >= buflen) {
265 memcpy(buf, canary, buflen);
266 memset(canary, 0, canary_len);
273 if (execpath == NULL)
275 else if (buf == NULL)
278 if (strlcpy(buf, execpath, buflen) >=
279 (unsigned int)buflen)
286 if (hwcap_present && buflen == sizeof(u_long)) {
287 *(u_long *)buf = hwcap;
293 if (hwcap2_present && buflen == sizeof(u_long)) {
294 *(u_long *)buf = hwcap2;
300 if (pagesizes != NULL && pagesizes_len >= buflen) {
301 memcpy(buf, pagesizes, buflen);
307 if (buflen == sizeof(int)) {
309 *(int *)buf = pagesize;
317 if (buflen == sizeof(int)) {
318 if (osreldate != 0) {
319 *(int *)buf = osreldate;
327 if (buflen == sizeof(int)) {
337 if (buflen == sizeof(void *)) {
338 if (timekeep != NULL) {
339 *(void **)buf = timekeep;
347 if (buflen == sizeof(int)) {
348 *(int *)buf = bsdflags;
354 if (buflen == sizeof(void *)) {
355 if (ps_strings != NULL) {
356 *(void **)buf = ps_strings;
364 if (buflen == sizeof(void *)) {
365 if (fxrng_seed_version != NULL) {
366 *(void **)buf = fxrng_seed_version;