Created
October 17, 2019 14:51
-
-
Save wangbj/658da547437fe9b7eff2113cc9fc9741 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/auxv.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <assert.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
// for simplicity, no optimizations in mind.. | |
static unsigned long proc_getauxval(unsigned long type) { | |
unsigned long auxv[4096 / sizeof(unsigned long)] = {0,}; | |
int fd = open("/proc/self/auxv", O_RDONLY); | |
assert(fd >= 0); | |
long nb = read(fd, auxv, sizeof(auxv)); | |
assert(nb >= 0); | |
close(fd); | |
for (int i = 0; i < nb / sizeof(long); i+=2) { | |
if (auxv[i] == type) { | |
return auxv[i+1]; | |
} | |
} | |
return 0; | |
} | |
static struct auxval { | |
unsigned long type; | |
const char* desc; | |
} auxvals[] = | |
{ | |
{ AT_BASE, "AT_BASE", }, | |
{ AT_ENTRY, "AT_ENTRY", }, | |
{ AT_HWCAP, "AT_HWCAP", }, | |
}; | |
int main(int argc, char** argv) { | |
unsigned long vals[2]; | |
for (int i = 0; i < sizeof(auxvals) / sizeof(auxvals[0]); i++) { | |
vals[0] = getauxval(auxvals[i].type); | |
vals[1] = proc_getauxval(auxvals[i].type); | |
printf("%16s: getauxval = 0x%-16lx, proc_getauxval = 0x%-16lx\n", | |
auxvals[i].desc, vals[0], vals[1]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment