diff options
author | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2024-07-14 09:48:50 +0000 |
---|---|---|
committer | Jeremie Courreges-Anglas <jca@cvs.openbsd.org> | 2024-07-14 09:48:50 +0000 |
commit | 5d97d781354fd9a672facbf67e35e6f5413c055b (patch) | |
tree | 2471113964071307bad212f25e1383074cb898ea /lib/libc | |
parent | ca37f40c3a8a098c1b9c69645a39c675aeb5cac1 (diff) |
Add elf_aux_info(3)
Designed to let userland peek at AT_HWCAP and AT_HWCAP2 using an already
existing interface coming from FreeBSD. Headers bits were snatched from
there. Input & ok kettenis@
libc bump and sets sync will follow soon
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/Symbols.list | 1 | ||||
-rw-r--r-- | lib/libc/dlfcn/init.c | 12 | ||||
-rw-r--r-- | lib/libc/gen/Makefile.inc | 8 | ||||
-rw-r--r-- | lib/libc/gen/elf_aux_info.3 | 74 | ||||
-rw-r--r-- | lib/libc/gen/elf_aux_info.c | 70 | ||||
-rw-r--r-- | lib/libc/hidden/sys/auxv.h | 26 |
6 files changed, 186 insertions, 5 deletions
diff --git a/lib/libc/Symbols.list b/lib/libc/Symbols.list index eb4d78cc947..48c4965a6d9 100644 --- a/lib/libc/Symbols.list +++ b/lib/libc/Symbols.list @@ -608,6 +608,7 @@ daemon devname dirfd dirname +elf_aux_info endfsent endgrent endnetgrent diff --git a/lib/libc/dlfcn/init.c b/lib/libc/dlfcn/init.c index 9fc63c97f98..c7b8fd2bc37 100644 --- a/lib/libc/dlfcn/init.c +++ b/lib/libc/dlfcn/init.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init.c,v 1.22 2024/01/21 17:18:13 kettenis Exp $ */ +/* $OpenBSD: init.c,v 1.23 2024/07/14 09:48:48 jca Exp $ */ /* * Copyright (c) 2014,2015 Philip Guenther <guenther@openbsd.org> * @@ -49,6 +49,8 @@ char ***_csu_finish(char **_argv, char **_envp, void (*_cleanup)(void)); /* provide definitions for these */ int _pagesize = 0; struct timekeep *_timekeep; +unsigned long _hwcap, _hwcap2; +int _hwcap_avail, _hwcap2_avail; /* * In dynamically linked binaries environ and __progname are overridden by @@ -96,6 +98,14 @@ _libc_preinit(int argc, char **argv, char **envp, dl_cb_cb *cb) ; for (aux = (void *)envp; aux->au_id != AUX_null; aux++) { switch (aux->au_id) { + case AUX_hwcap: + _hwcap = aux->au_v; + _hwcap_avail = 1; + break; + case AUX_hwcap2: + _hwcap2 = aux->au_v; + _hwcap2_avail = 1; + break; case AUX_pagesz: _pagesize = aux->au_v; break; diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index cf36ead40e9..f87cc7a89e5 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.82 2019/09/02 21:18:40 deraadt Exp $ +# $OpenBSD: Makefile.inc,v 1.83 2024/07/14 09:48:48 jca Exp $ # gen sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/gen ${LIBCSRCDIR}/gen @@ -6,8 +6,8 @@ SRCS+= alarm.c assert.c auth_subr.c authenticate.c \ basename.c clock.c clock_getcpuclockid.c \ closedir.c confstr.c ctermid.c ctype_.c \ - daemon.c devname.c dirfd.c dirname.c disklabel.c err.c \ - errc.c errx.c errlist.c errno.c exec.c \ + daemon.c devname.c dirfd.c dirname.c disklabel.c elf_aux_info.c \ + err.c errc.c errx.c errlist.c errno.c exec.c \ fdatasync.c fnmatch.c fpclassify.c frexp.c \ fstab.c ftok.c fts.c ftw.c getbsize.c getcap.c getcwd.c \ getdomainname.c getgrent.c getgrouplist.c gethostname.c \ @@ -36,7 +36,7 @@ SRCS+= alarm.c assert.c auth_subr.c authenticate.c \ MAN+= __tfork_thread.3 alarm.3 auth_subr.3 authenticate.3 basename.3 clock.3 \ clock_getcpuclockid.3 confstr.3 \ - ctermid.3 daemon.3 devname.3 opendir.3 dirname.3 err.3 \ + ctermid.3 daemon.3 devname.3 opendir.3 dirname.3 elf_aux_info.3 err.3 \ execv.3 fabs.3 fnmatch.3 fpclassify.3 fpgetmask.3 frexp.3 ftok.3 fts_open.3 \ ftw.3 getbsize.3 cgetent.3 getcwd.3 getdomainname.3 getdiskbyname.3 \ getfsent.3 getgrent.3 getgrouplist.3 gethostname.3 getloadavg.3 \ diff --git a/lib/libc/gen/elf_aux_info.3 b/lib/libc/gen/elf_aux_info.3 new file mode 100644 index 00000000000..1bd1a0923e0 --- /dev/null +++ b/lib/libc/gen/elf_aux_info.3 @@ -0,0 +1,74 @@ +.\" $OpenBSD: elf_aux_info.3,v 1.1 2024/07/14 09:48:48 jca Exp $ +.\" +.\" Origin: FreeBSD auxv.3 +.\" +.\" Copyright (c) 2019 Ian Lepore <ian@freebsd.org> +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +.\" +.Dd $Mdocdate: July 14 2024 $ +.Dt ELF_AUX_INFO 3 +.Os +.Sh NAME +.Nm elf_aux_info +.Nd extract data from the elf auxiliary vector of the current process +.Sh SYNOPSIS +.In sys/auxv.h +.Ft int +.Fn elf_aux_info "int aux" "void *buf" "int buflen" +.Sh DESCRIPTION +The +.Fn elf_aux_info +function retrieves the auxiliary info vector requested in +.Va aux . +The information is stored into the provided buffer if it will fit. +The following values can be requested (corresponding buffer sizes are +specified in parenthesis): +.Bl -tag -width AT_HWCAP2 +.It AT_HWCAP +CPU / hardware feature flags +.Dv (sizeof(unsigned long)) . +.It AT_HWCAP2 +CPU / hardware feature flags +.Dv (sizeof(unsigned long)) . +.It AT_PAGESZ +Page size in bytes +.Dv (sizeof(int)) . +.El +.Sh RETURN VALUES +Returns zero on success, or an error number on failure. +.Sh ERRORS +.Bl -tag -width Er +.It Bq Er EINVAL +An unknown item was requested. +.It Bq Er EINVAL +The provided buffer was not the right size for the requested item. +.It Bq Er ENOENT +The requested item is not available. +.El +.Sh HISTORY +The +.Fn elf_aux_info +function appeared in +.Fx 12.0 +and was first available in +.Ox 7.6 . diff --git a/lib/libc/gen/elf_aux_info.c b/lib/libc/gen/elf_aux_info.c new file mode 100644 index 00000000000..a4b7667ba6f --- /dev/null +++ b/lib/libc/gen/elf_aux_info.c @@ -0,0 +1,70 @@ +/* $OpenBSD: elf_aux_info.c,v 1.1 2024/07/14 09:48:48 jca Exp $ */ + +/* + * Copyright (c) 2024 Jeremie Courreges-Anglas <jca@wxcvbn.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <sys/auxv.h> + +#include <errno.h> + +extern int _pagesize; +extern unsigned long _hwcap, _hwcap2; +extern int _hwcap_avail, _hwcap2_avail; + +int +elf_aux_info(int request, void *buf, int buflen) +{ + int ret = 0; + + if (buflen < 0) + return EINVAL; + + switch (request) { + case AT_HWCAP: + if (buflen != sizeof(unsigned long)) + ret = EINVAL; + else if (!_hwcap_avail) + ret = ENOENT; + else + *(unsigned long *)buf = _hwcap; + break; + case AT_HWCAP2: + if (buflen != sizeof(unsigned long)) + ret = EINVAL; + else if (!_hwcap2_avail) + ret = ENOENT; + else + *(unsigned long *)buf = _hwcap2; + break; + case AT_PAGESZ: + if (buflen != sizeof(int)) + ret = EINVAL; + else if (!_pagesize) + ret = ENOENT; + else + *(int *)buf = _pagesize; + break; + default: + if (request < 0 || request >= AT_COUNT) + ret = EINVAL; + else + ret = ENOENT; + break; + } + + return ret; +} diff --git a/lib/libc/hidden/sys/auxv.h b/lib/libc/hidden/sys/auxv.h new file mode 100644 index 00000000000..cd566b7b2a2 --- /dev/null +++ b/lib/libc/hidden/sys/auxv.h @@ -0,0 +1,26 @@ +/* $OpenBSD: auxv.h,v 1.1 2024/07/14 09:48:48 jca Exp $ */ + +/* + * Copyright (c) 2024 Jeremie Courreges-Anglas <jca@wxcvbn.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _LIBC_SYS_AUXV_H_ +#define _LIBC_SYS_AUXV_H_ + +#include_next <sys/auxv.h> + +PROTO_DEPRECATED(elf_aux_info); + +#endif /* !_LIBC_SYS_AUXV_H_ */ |