From a35c9039b43f41e63cea5beba39fe752af9698c9 Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Mon, 10 Feb 2025 21:27:44 +0100 Subject: [PATCH] Init --- getauxval.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 getauxval.c diff --git a/getauxval.c b/getauxval.c new file mode 100644 index 0000000..4428571 --- /dev/null +++ b/getauxval.c @@ -0,0 +1,49 @@ +#include +#include + +#ifdef GETAUXVAL_BACKPORT_ELF64 +typedef Elf64_auxv_t __auxv_t; +#else +typedef Elf32_auxv_t __auxv_t; +#endif + +unsigned long getauxval(unsigned long type) { +#ifdef GETAUXVAL_BACKPORT_DEBUG + printf("getauxval: search started for type: %lu\n", type); +#endif + + FILE *f = fopen("/proc/self/auxv", "rb"); + if (f == NULL) + return 1; + + unsigned long val = AT_NULL; + __auxv_t aux; + +#ifdef GETAUXVAL_BACKPORT_DEBUG + unsigned int i = 0; +#endif + + while (fread(&aux, sizeof(aux), 1, f) == 1 && aux.a_type != AT_NULL) { +#ifdef GETAUXVAL_BACKPORT_DEBUG + printf("getauxval: current type: %u [%u]\n", aux.a_type, i); +#endif + + if (aux.a_type == type) { +#ifdef GETAUXVAL_BACKPORT_DEBUG + printf("getauxval: found %u with value: 0x%x\n", aux.a_type, + aux.a_un.a_val); +#endif + + val = aux.a_un.a_val; + break; + } + +#ifdef GETAUXVAL_BACKPORT_DEBUG + i++; +#endif + } + + fclose(f); + + return val; +}