Skip to content

Instantly share code, notes, and snippets.

@chergert
Created September 17, 2015 08:48
Show Gist options
  • Save chergert/eb6149916b10d3bf094c to your computer and use it in GitHub Desktop.
Save chergert/eb6149916b10d3bf094c to your computer and use it in GitHub Desktop.
example of fetching getcpu from the vsdo
#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