Skip to content

Instantly share code, notes, and snippets.

@wangbj
Created October 17, 2019 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangbj/658da547437fe9b7eff2113cc9fc9741 to your computer and use it in GitHub Desktop.
Save wangbj/658da547437fe9b7eff2113cc9fc9741 to your computer and use it in GitHub Desktop.
#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