vdso_test.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * vdso_test.c: Sample code to test parse_vdso.c on x86_64
  3. * Copyright (c) 2011 Andy Lutomirski
  4. * Subject to the GNU General Public License, version 2
  5. *
  6. * You can amuse yourself by compiling with:
  7. * gcc -std=gnu99 -nostdlib
  8. * -Os -fno-asynchronous-unwind-tables -flto
  9. * vdso_test.c parse_vdso.c -o vdso_test
  10. * to generate a small binary with no dependencies at all.
  11. */
  12. #include <sys/syscall.h>
  13. #include <sys/time.h>
  14. #include <unistd.h>
  15. #include <stdint.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. /* We need a libc functions... */
  20. int strcmp(const char *a, const char *b)
  21. {
  22. /* This implementation is buggy: it never returns -1. */
  23. while (*a || *b) {
  24. if (*a != *b)
  25. return 1;
  26. if (*a == 0 || *b == 0)
  27. return 1;
  28. a++;
  29. b++;
  30. }
  31. return 0;
  32. }
  33. /* ...and two syscalls. This is x86_64-specific. */
  34. static inline long linux_write(int fd, const void *data, size_t len)
  35. {
  36. long ret;
  37. asm volatile ("syscall" : "=a" (ret) : "a" (__NR_write),
  38. "D" (fd), "S" (data), "d" (len) :
  39. "cc", "memory", "rcx",
  40. "r8", "r9", "r10", "r11" );
  41. return ret;
  42. }
  43. static inline void linux_exit(int code)
  44. {
  45. asm volatile ("syscall" : : "a" (__NR_exit), "D" (code));
  46. }
  47. void to_base10(char *lastdig, uint64_t n)
  48. {
  49. while (n) {
  50. *lastdig = (n % 10) + '0';
  51. n /= 10;
  52. lastdig--;
  53. }
  54. }
  55. __attribute__((externally_visible)) void c_main(void **stack)
  56. {
  57. /* Parse the stack */
  58. long argc = (long)*stack;
  59. stack += argc + 2;
  60. /* Now we're pointing at the environment. Skip it. */
  61. while(*stack)
  62. stack++;
  63. stack++;
  64. /* Now we're pointing at auxv. Initialize the vDSO parser. */
  65. vdso_init_from_auxv((void *)stack);
  66. /* Find gettimeofday. */
  67. typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
  68. gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
  69. if (!gtod)
  70. linux_exit(1);
  71. struct timeval tv;
  72. long ret = gtod(&tv, 0);
  73. if (ret == 0) {
  74. char buf[] = "The time is .000000\n";
  75. to_base10(buf + 31, tv.tv_sec);
  76. to_base10(buf + 38, tv.tv_usec);
  77. linux_write(1, buf, sizeof(buf) - 1);
  78. } else {
  79. linux_exit(ret);
  80. }
  81. linux_exit(0);
  82. }
  83. /*
  84. * This is the real entry point. It passes the initial stack into
  85. * the C entry point.
  86. */
  87. asm (
  88. ".text\n"
  89. ".global _start\n"
  90. ".type _start,@function\n"
  91. "_start:\n\t"
  92. "mov %rsp,%rdi\n\t"
  93. "jmp c_main"
  94. );