1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <sys/syscall.h>
15#include <sys/time.h>
16#include <unistd.h>
17#include <stdint.h>
18
19#include "parse_vdso.h"
20
21
22int strcmp(const char *a, const char *b)
23{
24
25 while (*a || *b) {
26 if (*a != *b)
27 return 1;
28 if (*a == 0 || *b == 0)
29 return 1;
30 a++;
31 b++;
32 }
33
34 return 0;
35}
36
37
38static inline long x86_syscall3(long nr, long a0, long a1, long a2)
39{
40 long ret;
41#ifdef __x86_64__
42 asm volatile ("syscall" : "=a" (ret) : "a" (nr),
43 "D" (a0), "S" (a1), "d" (a2) :
44 "cc", "memory", "rcx",
45 "r8", "r9", "r10", "r11" );
46#else
47 asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
48 "b" (a0), "c" (a1), "d" (a2) :
49 "cc", "memory" );
50#endif
51 return ret;
52}
53
54static inline long linux_write(int fd, const void *data, size_t len)
55{
56 return x86_syscall3(__NR_write, fd, (long)data, (long)len);
57}
58
59static inline void linux_exit(int code)
60{
61 x86_syscall3(__NR_exit, code, 0, 0);
62}
63
64void to_base10(char *lastdig, time_t n)
65{
66 while (n) {
67 *lastdig = (n % 10) + '0';
68 n /= 10;
69 lastdig--;
70 }
71}
72
73__attribute__((externally_visible)) void c_main(void **stack)
74{
75
76 long argc = (long)*stack;
77 stack += argc + 2;
78
79
80 while(*stack)
81 stack++;
82 stack++;
83
84
85 vdso_init_from_auxv((void *)stack);
86
87
88 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
89 gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
90
91 if (!gtod)
92 linux_exit(1);
93
94 struct timeval tv;
95 long ret = gtod(&tv, 0);
96
97 if (ret == 0) {
98 char buf[] = "The time is .000000\n";
99 to_base10(buf + 31, tv.tv_sec);
100 to_base10(buf + 38, tv.tv_usec);
101 linux_write(1, buf, sizeof(buf) - 1);
102 } else {
103 linux_exit(ret);
104 }
105
106 linux_exit(0);
107}
108
109
110
111
112
113asm (
114 ".text\n"
115 ".global _start\n"
116 ".type _start,@function\n"
117 "_start:\n\t"
118#ifdef __x86_64__
119 "mov %rsp,%rdi\n\t"
120 "jmp c_main"
121#else
122 "push %esp\n\t"
123 "call c_main\n\t"
124 "int $3"
125#endif
126 );
127