0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <sys/syscall.h>
0015 #include <sys/time.h>
0016 #include <unistd.h>
0017 #include <stdint.h>
0018
0019 #include "parse_vdso.h"
0020
0021
0022 int strcmp(const char *a, const char *b)
0023 {
0024
0025 while (*a || *b) {
0026 if (*a != *b)
0027 return 1;
0028 if (*a == 0 || *b == 0)
0029 return 1;
0030 a++;
0031 b++;
0032 }
0033
0034 return 0;
0035 }
0036
0037
0038 static inline long x86_syscall3(long nr, long a0, long a1, long a2)
0039 {
0040 long ret;
0041 #ifdef __x86_64__
0042 asm volatile ("syscall" : "=a" (ret) : "a" (nr),
0043 "D" (a0), "S" (a1), "d" (a2) :
0044 "cc", "memory", "rcx",
0045 "r8", "r9", "r10", "r11" );
0046 #else
0047 asm volatile ("int $0x80" : "=a" (ret) : "a" (nr),
0048 "b" (a0), "c" (a1), "d" (a2) :
0049 "cc", "memory" );
0050 #endif
0051 return ret;
0052 }
0053
0054 static inline long linux_write(int fd, const void *data, size_t len)
0055 {
0056 return x86_syscall3(__NR_write, fd, (long)data, (long)len);
0057 }
0058
0059 static inline void linux_exit(int code)
0060 {
0061 x86_syscall3(__NR_exit, code, 0, 0);
0062 }
0063
0064 void to_base10(char *lastdig, time_t n)
0065 {
0066 while (n) {
0067 *lastdig = (n % 10) + '0';
0068 n /= 10;
0069 lastdig--;
0070 }
0071 }
0072
0073 __attribute__((externally_visible)) void c_main(void **stack)
0074 {
0075
0076 long argc = (long)*stack;
0077 stack += argc + 2;
0078
0079
0080 while(*stack)
0081 stack++;
0082 stack++;
0083
0084
0085 vdso_init_from_auxv((void *)stack);
0086
0087
0088 typedef long (*gtod_t)(struct timeval *tv, struct timezone *tz);
0089 gtod_t gtod = (gtod_t)vdso_sym("LINUX_2.6", "__vdso_gettimeofday");
0090
0091 if (!gtod)
0092 linux_exit(1);
0093
0094 struct timeval tv;
0095 long ret = gtod(&tv, 0);
0096
0097 if (ret == 0) {
0098 char buf[] = "The time is .000000\n";
0099 to_base10(buf + 31, tv.tv_sec);
0100 to_base10(buf + 38, tv.tv_usec);
0101 linux_write(1, buf, sizeof(buf) - 1);
0102 } else {
0103 linux_exit(ret);
0104 }
0105
0106 linux_exit(0);
0107 }
0108
0109
0110
0111
0112
0113 asm (
0114 ".text\n"
0115 ".global _start\n"
0116 ".type _start,@function\n"
0117 "_start:\n\t"
0118 #ifdef __x86_64__
0119 "mov %rsp,%rdi\n\t"
0120 "jmp c_main"
0121 #else
0122 "push %esp\n\t"
0123 "call c_main\n\t"
0124 "int $3"
0125 #endif
0126 );