vdso_test.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * vdso_test.c: Sample code to test parse_vdso.c
  3. * Copyright (c) 2014 Andy Lutomirski
  4. * Subject to the GNU General Public License, version 2
  5. *
  6. * Compile with:
  7. * gcc -std=gnu99 vdso_test.c parse_vdso.c
  8. *
  9. * Tested on x86, 32-bit and 64-bit. It may work on other architectures, too.
  10. */
  11. #include <stdint.h>
  12. #include <elf.h>
  13. #include <stdio.h>
  14. #include <sys/auxv.h>
  15. #include <sys/time.h>
  16. extern void *vdso_sym(const char *version, const char *name);
  17. extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
  18. extern void vdso_init_from_auxv(void *auxv);
  19. int main(int argc, char **argv)
  20. {
  21. unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
  22. if (!sysinfo_ehdr) {
  23. printf("AT_SYSINFO_EHDR is not present!\n");
  24. return 0;
  25. }
  26. vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
  27. /* Find gettimeofday. */
  28. typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
  29. gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
  30. if (!gtod) {
  31. printf("Could not find __vdso_gettimeofday\n");
  32. return 1;
  33. }
  34. struct timeval tv;
  35. long ret = gtod(&tv, 0);
  36. if (ret == 0) {
  37. printf("The time is %lld.%06lld\n",
  38. (long long)tv.tv_sec, (long long)tv.tv_usec);
  39. } else {
  40. printf("__vdso_gettimeofday failed\n");
  41. }
  42. return 0;
  43. }