Created
September 17, 2015 08:48
-
-
Save chergert/eb6149916b10d3bf094c to your computer and use it in GitHub Desktop.
example of fetching getcpu from the vsdo
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 <dlfcn.h> | |
#include <stdio.h> | |
#include <asm/vsyscall.h> | |
#include <sys/auxv.h> | |
int (*test_getcpu) (unsigned *cpu, | |
unsigned *node, | |
void *cache); | |
void * | |
get_vdso_sym (const char *name) | |
{ | |
static const char *vdso_names[] = { | |
"linux-vdso.so.1", | |
"linux-vdso32.so.1", | |
"linux-vdso64.so.1", | |
NULL | |
}; | |
int i; | |
for (i = 0; vdso_names [i]; i++) | |
{ | |
void *lib; | |
void *symbol; | |
lib = dlopen (vdso_names [i], RTLD_NOW | RTLD_GLOBAL); | |
if (lib == NULL) | |
continue; | |
symbol = dlsym (lib, name); | |
if (symbol == NULL) | |
goto cleanup; | |
if (*(void **)symbol == NULL) | |
goto cleanup; | |
return symbol; | |
cleanup: | |
dlclose (lib); | |
} | |
} | |
int | |
main (int argc, | |
char *argv[]) | |
{ | |
int ret; | |
int cpu = -1; | |
test_getcpu = get_vdso_sym ("__kernel_getcpu"); | |
if (test_getcpu == NULL) | |
test_getcpu = get_vdso_sym ("__vdso_getcpu"); | |
ret = test_getcpu (&cpu, NULL, NULL); | |
printf ("ret = %d cpu = %d\n", ret, cpu); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment