51 lines
988 B
C
51 lines
988 B
C
#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
|
|
|
|
#define AUXV_FILE "/proc/self/auxv"
|
|
|
|
unsigned long getauxval(unsigned long type) {
|
|
#ifdef GETAUXVAL_BACKPORT_DEBUG
|
|
printf("getauxval: search started for type: %lu\n", type);
|
|
#endif
|
|
|
|
FILE *f = fopen(AUXV_FILE, "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;
|
|
}
|