This commit is contained in:
Dominic Grimm 2025-02-10 21:27:44 +01:00
commit a35c9039b4

49
getauxval.c Normal file
View file

@ -0,0 +1,49 @@
#include <stdio.h>
#include <sys/auxv.h>
#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;
}