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
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.
13 #include <sys/syscall.h>
18 extern void *vdso_sym(const char *version
, const char *name
);
19 extern void vdso_init_from_sysinfo_ehdr(uintptr_t base
);
20 extern void vdso_init_from_auxv(void *auxv
);
22 /* We need a libc functions... */
23 int strcmp(const char *a
, const char *b
)
25 /* This implementation is buggy: it never returns -1. */
29 if (*a
== 0 || *b
== 0)
38 /* ...and two syscalls. This is x86_64-specific. */
39 static inline long linux_write(int fd
, const void *data
, size_t len
)
43 asm volatile ("syscall" : "=a" (ret
) : "a" (__NR_write
),
44 "D" (fd
), "S" (data
), "d" (len
) :
45 "cc", "memory", "rcx",
46 "r8", "r9", "r10", "r11" );
50 static inline void linux_exit(int code
)
52 asm volatile ("syscall" : : "a" (__NR_exit
), "D" (code
));
55 void to_base10(char *lastdig
, uint64_t n
)
58 *lastdig
= (n
% 10) + '0';
64 __attribute__((externally_visible
)) void c_main(void **stack
)
67 long argc
= (long)*stack
;
70 /* Now we're pointing at the environment. Skip it. */
75 /* Now we're pointing at auxv. Initialize the vDSO parser. */
76 vdso_init_from_auxv((void *)stack
);
78 /* Find gettimeofday. */
79 typedef long (*gtod_t
)(struct timeval
*tv
, struct timezone
*tz
);
80 gtod_t gtod
= (gtod_t
)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
86 long ret
= gtod(&tv
, 0);
89 char buf
[] = "The time is .000000\n";
90 to_base10(buf
+ 31, tv
.tv_sec
);
91 to_base10(buf
+ 38, tv
.tv_usec
);
92 linux_write(1, buf
, sizeof(buf
) - 1);
101 * This is the real entry point. It passes the initial stack into
107 ".type _start,@function\n"