nolibc/stdlib: Implement `getauxval(3)` function
authorAmmar Faizi <ammarfaizi2@gnuweeb.org>
Tue, 10 Jan 2023 07:24:32 +0000 (08:24 +0100)
committerPaul E. McKenney <paulmck@kernel.org>
Tue, 10 Jan 2023 21:33:56 +0000 (13:33 -0800)
Previous commits save the address of the auxiliary vector into a global
variable @_auxv. This commit creates a new function 'getauxval()' as a
helper function to get the auxv value based on the given key.

The behavior of this function is identic with the function documented
in 'man 3 getauxval'. This function is also needed to implement
'getpagesize()' function that we will wire up in the next patches.

Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
tools/include/nolibc/stdlib.h

index a24000d1e8222c2b23511529af32de14a66206e3..894c955d027e48510fc2e1eb4f33199e345d0b35 100644 (file)
@@ -12,6 +12,7 @@
 #include "types.h"
 #include "sys.h"
 #include "string.h"
+#include <linux/auxvec.h>
 
 struct nolibc_heap {
        size_t  len;
@@ -108,6 +109,32 @@ char *getenv(const char *name)
        return _getenv(name, environ);
 }
 
+static __attribute__((unused))
+unsigned long getauxval(unsigned long type)
+{
+       const unsigned long *auxv = _auxv;
+       unsigned long ret;
+
+       if (!auxv)
+               return 0;
+
+       while (1) {
+               if (!auxv[0] && !auxv[1]) {
+                       ret = 0;
+                       break;
+               }
+
+               if (auxv[0] == type) {
+                       ret = auxv[1];
+                       break;
+               }
+
+               auxv += 2;
+       }
+
+       return ret;
+}
+
 static __attribute__((unused))
 void *malloc(size_t len)
 {