vdso_standalone_test_x86.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * vdso_test.c: Sample code to test parse_vdso.c on x86
  3. * Copyright (c) 2011-2014 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 -lgcc_s
  9. * vdso_standalone_test_x86.c parse_vdso.c
  10. * to generate a small binary. On x86_64, you can omit -lgcc_s
  11. * if you want the binary to be completely standalone.
  12. */
  13. #include <sys/syscall.h>
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #include <stdint.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. /* We need a libc functions... */
  21. int strcmp(const char *a, const char *b)
  22. {
  23. /* This implementation is buggy: it never returns -1. */
  24. while (*a || *b) {
  25. if (*a != *b)
  26. return 1;
  27. if (*a == 0 || *b == 0)
  28. return 1;
  29. a++;
  30. b++;
  31. }
  32. return 0;
  33. }
  34. /* ...and two syscalls. This is x86-specific. */
  35. static inline long x86_syscall3(long nr, long a0, long a1, long a2)
  36. {
  37. long ret;
  38. #ifdef __x86_64__
  39. asm volatile ("syscall" : "=a" (ret) : "a" (nr),
  40. "D" (a0), "S" (a1), "d" (a2) :
  41. "cc", "memory", "rcx",
  42. "r8", "r9", "r10", "r11" );
  43. #else
  44. asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
  45. "b" (a0), "c" (a1), "d" (a2) :
  46. "cc", "memory" );
  47. #endif
  48. return ret;
  49. }
  50. static inline long linux_write(int fd, const void *data, size_t len)
  51. {
  52. return x86_syscall3(__NR_write, fd, (long)data, (long)len);
  53. }
  54. static inline void linux_exit(int code)
  55. {
  56. x86_syscall3(__NR_exit, code, 0, 0);
  57. }
  58. void to_base10(char *lastdig, time_t n)
  59. {
  60. while (n) {
  61. *lastdig = (n % 10) + '0';
  62. n /= 10;
  63. lastdig--;
  64. }
  65. }
  66. __attribute__((externally_visible)) void c_main(void **stack)
  67. {
  68. /* Parse the stack */
  69. long argc = (long)*stack;
  70. stack += argc + 2;
  71. /* Now we're pointing at the environment. Skip it. */
  72. while(*stack)
  73. stack++;
  74. stack++;
  75. /* Now we're pointing at auxv. Initialize the vDSO parser. */
  76. vdso_init_from_auxv((void *)stack);
  77. /* Find gettimeofday. */
  78. typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
  79. gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
  80. if (!gtod)
  81. linux_exit(1);
  82. struct timeval tv;
  83. long ret = gtod(&tv, 0);
  84. if (ret == 0) {
  85. char buf[] = "The time is .000000\n";
  86. to_base10(buf + 31, tv.tv_sec);
  87. to_base10(buf + 38, tv.tv_usec);
  88. linux_write(1, buf, sizeof(buf) - 1);
  89. } else {
  90. linux_exit(ret);
  91. }
  92. linux_exit(0);
  93. }
  94. /*
  95. * This is the real entry point. It passes the initial stack into
  96. * the C entry point.
  97. */
  98. asm (
  99. ".text\n"
  100. ".global _start\n"
  101. ".type _start,@function\n"
  102. "_start:\n\t"
  103. #ifdef __x86_64__
  104. "mov %rsp,%rdi\n\t"
  105. "jmp c_main"
  106. #else
  107. "push %esp\n\t"
  108. "call c_main\n\t"
  109. "int $3"
  110. #endif
  111. );