public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] misc: Add internal __getauxval2 function
@ 2020-10-27  9:42 Florian Weimer
  2020-10-27 15:20 ` Alistair Francis
  0 siblings, 1 reply; 2+ messages in thread
From: Florian Weimer @ 2020-10-27  9:42 UTC (permalink / raw)
  To: libc-alpha

The explicit error return value (without in-band signaling) avoids
complicated steps to detect errors based on whether errno has been
updated.

Tested on x86_64-linux-gnu.

---
 include/sys/auxv.h |  5 +++++
 misc/getauxval.c   | 33 +++++++++++++++++++++++++++------
 2 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/include/sys/auxv.h b/include/sys/auxv.h
index 3bab6d05d4..dd0602b08d 100644
--- a/include/sys/auxv.h
+++ b/include/sys/auxv.h
@@ -5,4 +5,9 @@
 extern __typeof (getauxval) __getauxval;
 libc_hidden_proto (__getauxval)
 
+/* Like getauxval, but writes the value to *RESULT and returns true if
+   found, or returns false.  Does not set errno.  */
+_Bool __getauxval2 (unsigned long int type, unsigned long int *result);
+libc_hidden_proto (__getauxval2)
+
 #endif  /* !_ISOMAC */
diff --git a/misc/getauxval.c b/misc/getauxval.c
index d7f9f957d0..e96d4dfa20 100644
--- a/misc/getauxval.c
+++ b/misc/getauxval.c
@@ -18,26 +18,47 @@
 #include <sys/auxv.h>
 #include <errno.h>
 #include <ldsodefs.h>
+#include <stdbool.h>
 
-
-unsigned long int
-__getauxval (unsigned long int type)
+bool
+__getauxval2 (unsigned long int type, unsigned long int *result)
 {
 #ifdef HAVE_AUX_VECTOR
   ElfW(auxv_t) *p;
 #endif
 
   if (type == AT_HWCAP)
-    return GLRO(dl_hwcap);
+    {
+      *result = GLRO(dl_hwcap);
+      return true;
+    }
   else if (type == AT_HWCAP2)
-    return GLRO(dl_hwcap2);
+    {
+      *result = GLRO(dl_hwcap2);
+      return true;
+    }
 
 #ifdef HAVE_AUX_VECTOR
   for (p = GLRO(dl_auxv); p->a_type != AT_NULL; p++)
     if (p->a_type == type)
-      return p->a_un.a_val;
+      {
+        *result = p->a_un.a_val;
+        return true;
+      }
 #endif
 
+  return false;
+}
+libc_hidden_def (__getauxval2)
+
+unsigned long int
+__getauxval (unsigned long int type)
+{
+  unsigned long int result;
+
+  if (__getauxval2 (type, &result))
+    return result;
+
   __set_errno (ENOENT);
   return 0;
 }

-- 
Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
Commercial register: Amtsgericht Muenchen, HRB 153243,
Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] misc: Add internal __getauxval2 function
  2020-10-27  9:42 [PATCH] misc: Add internal __getauxval2 function Florian Weimer
@ 2020-10-27 15:20 ` Alistair Francis
  0 siblings, 0 replies; 2+ messages in thread
From: Alistair Francis @ 2020-10-27 15:20 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Tue, Oct 27, 2020 at 2:43 AM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> The explicit error return value (without in-band signaling) avoids
> complicated steps to detect errors based on whether errno has been
> updated.
>
> Tested on x86_64-linux-gnu.

Reviewed-by: Alistair Francis <alistair.francis@wdc.com>

Alistair

>
> ---
>  include/sys/auxv.h |  5 +++++
>  misc/getauxval.c   | 33 +++++++++++++++++++++++++++------
>  2 files changed, 32 insertions(+), 6 deletions(-)
>
> diff --git a/include/sys/auxv.h b/include/sys/auxv.h
> index 3bab6d05d4..dd0602b08d 100644
> --- a/include/sys/auxv.h
> +++ b/include/sys/auxv.h
> @@ -5,4 +5,9 @@
>  extern __typeof (getauxval) __getauxval;
>  libc_hidden_proto (__getauxval)
>
> +/* Like getauxval, but writes the value to *RESULT and returns true if
> +   found, or returns false.  Does not set errno.  */
> +_Bool __getauxval2 (unsigned long int type, unsigned long int *result);
> +libc_hidden_proto (__getauxval2)
> +
>  #endif  /* !_ISOMAC */
> diff --git a/misc/getauxval.c b/misc/getauxval.c
> index d7f9f957d0..e96d4dfa20 100644
> --- a/misc/getauxval.c
> +++ b/misc/getauxval.c
> @@ -18,26 +18,47 @@
>  #include <sys/auxv.h>
>  #include <errno.h>
>  #include <ldsodefs.h>
> +#include <stdbool.h>
>
> -
> -unsigned long int
> -__getauxval (unsigned long int type)
> +bool
> +__getauxval2 (unsigned long int type, unsigned long int *result)
>  {
>  #ifdef HAVE_AUX_VECTOR
>    ElfW(auxv_t) *p;
>  #endif
>
>    if (type == AT_HWCAP)
> -    return GLRO(dl_hwcap);
> +    {
> +      *result = GLRO(dl_hwcap);
> +      return true;
> +    }
>    else if (type == AT_HWCAP2)
> -    return GLRO(dl_hwcap2);
> +    {
> +      *result = GLRO(dl_hwcap2);
> +      return true;
> +    }
>
>  #ifdef HAVE_AUX_VECTOR
>    for (p = GLRO(dl_auxv); p->a_type != AT_NULL; p++)
>      if (p->a_type == type)
> -      return p->a_un.a_val;
> +      {
> +        *result = p->a_un.a_val;
> +        return true;
> +      }
>  #endif
>
> +  return false;
> +}
> +libc_hidden_def (__getauxval2)
> +
> +unsigned long int
> +__getauxval (unsigned long int type)
> +{
> +  unsigned long int result;
> +
> +  if (__getauxval2 (type, &result))
> +    return result;
> +
>    __set_errno (ENOENT);
>    return 0;
>  }
>
> --
> Red Hat GmbH, https://de.redhat.com/ , Registered seat: Grasbrunn,
> Commercial register: Amtsgericht Muenchen, HRB 153243,
> Managing Directors: Charles Cachera, Brian Klemm, Laurie Krebs, Michael O'Neill
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-10-27 15:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-27  9:42 [PATCH] misc: Add internal __getauxval2 function Florian Weimer
2020-10-27 15:20 ` Alistair Francis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).