vdso_test.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "../kselftest.h"
  17. extern void *vdso_sym(const char *version, const char *name);
  18. extern void vdso_init_from_sysinfo_ehdr(uintptr_t base);
  19. extern void vdso_init_from_auxv(void *auxv);
  20. /*
  21. * ARM64's vDSO exports its gettimeofday() implementation with a different
  22. * name and version from other architectures, so we need to handle it as
  23. * a special case.
  24. */
  25. #if defined(__aarch64__)
  26. const char *version = "LINUX_2.6.39";
  27. const char *name = "__kernel_gettimeofday";
  28. #else
  29. const char *version = "LINUX_2.6";
  30. const char *name = "__vdso_gettimeofday";
  31. #endif
  32. int main(int argc, char **argv)
  33. {
  34. unsigned long sysinfo_ehdr = getauxval(AT_SYSINFO_EHDR);
  35. if (!sysinfo_ehdr) {
  36. printf("AT_SYSINFO_EHDR is not present!\n");
  37. return KSFT_SKIP;
  38. }
  39. vdso_init_from_sysinfo_ehdr(getauxval(AT_SYSINFO_EHDR));
  40. /* Find gettimeofday. */
  41. typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
  42. gtod_t gtod = (gtod_t)vdso_sym(version, name);
  43. if (!gtod) {
  44. printf("Could not find %s\n", name);
  45. return KSFT_SKIP;
  46. }
  47. struct timeval tv;
  48. long ret = gtod(&tv, 0);
  49. if (ret == 0) {
  50. printf("The time is %lld.%06lld\n",
  51. (long long)tv.tv_sec, (long long)tv.tv_usec);
  52. } else {
  53. printf("%s failed\n", name);
  54. return KSFT_FAIL;
  55. }
  56. return 0;
  57. }