]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/x86/vdso/vdso2c.c
Merge branch 'next' (accumulated 3.16 merge window patches) into master
[karo-tx-linux.git] / arch / x86 / vdso / vdso2c.c
1 #include <inttypes.h>
2 #include <stdint.h>
3 #include <unistd.h>
4 #include <stdarg.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <fcntl.h>
9 #include <err.h>
10
11 #include <sys/mman.h>
12 #include <sys/types.h>
13
14 #include <linux/elf.h>
15 #include <linux/types.h>
16
17 const char *outfilename;
18
19 /* Symbols that we need in vdso2c. */
20 enum {
21         sym_vvar_page,
22         sym_hpet_page,
23         sym_end_mapping,
24 };
25
26 const int special_pages[] = {
27         sym_vvar_page,
28         sym_hpet_page,
29 };
30
31 char const * const required_syms[] = {
32         [sym_vvar_page] = "vvar_page",
33         [sym_hpet_page] = "hpet_page",
34         [sym_end_mapping] = "end_mapping",
35         "VDSO32_NOTE_MASK",
36         "VDSO32_SYSENTER_RETURN",
37         "__kernel_vsyscall",
38         "__kernel_sigreturn",
39         "__kernel_rt_sigreturn",
40 };
41
42 __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
43 static void fail(const char *format, ...)
44 {
45         va_list ap;
46         va_start(ap, format);
47         fprintf(stderr, "Error: ");
48         vfprintf(stderr, format, ap);
49         unlink(outfilename);
50         exit(1);
51         va_end(ap);
52 }
53
54 /*
55  * Evil macros to do a little-endian read.
56  */
57 #define GLE(x, bits, ifnot)                                             \
58         __builtin_choose_expr(                                          \
59                 (sizeof(x) == bits/8),                                  \
60                 (__typeof__(x))le##bits##toh(x), ifnot)
61
62 extern void bad_get_le(uint64_t);
63 #define LAST_LE(x)                                                      \
64         __builtin_choose_expr(sizeof(x) == 1, (x), bad_get_le(x))
65
66 #define GET_LE(x)                                                       \
67         GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_LE(x))))
68
69 #define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
70
71 #define BITS 64
72 #define GOFUNC go64
73 #define Elf_Ehdr Elf64_Ehdr
74 #define Elf_Shdr Elf64_Shdr
75 #define Elf_Phdr Elf64_Phdr
76 #define Elf_Sym Elf64_Sym
77 #define Elf_Dyn Elf64_Dyn
78 #include "vdso2c.h"
79 #undef BITS
80 #undef GOFUNC
81 #undef Elf_Ehdr
82 #undef Elf_Shdr
83 #undef Elf_Phdr
84 #undef Elf_Sym
85 #undef Elf_Dyn
86
87 #define BITS 32
88 #define GOFUNC go32
89 #define Elf_Ehdr Elf32_Ehdr
90 #define Elf_Shdr Elf32_Shdr
91 #define Elf_Phdr Elf32_Phdr
92 #define Elf_Sym Elf32_Sym
93 #define Elf_Dyn Elf32_Dyn
94 #include "vdso2c.h"
95 #undef BITS
96 #undef GOFUNC
97 #undef Elf_Ehdr
98 #undef Elf_Shdr
99 #undef Elf_Phdr
100 #undef Elf_Sym
101 #undef Elf_Dyn
102
103 static void go(void *addr, size_t len, FILE *outfile, const char *name)
104 {
105         Elf64_Ehdr *hdr = (Elf64_Ehdr *)addr;
106
107         if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
108                 go64(addr, len, outfile, name);
109         } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
110                 go32(addr, len, outfile, name);
111         } else {
112                 fail("unknown ELF class\n");
113         }
114 }
115
116 int main(int argc, char **argv)
117 {
118         int fd;
119         off_t len;
120         void *addr;
121         FILE *outfile;
122         char *name, *tmp;
123         int namelen;
124
125         if (argc != 3) {
126                 printf("Usage: vdso2c INPUT OUTPUT\n");
127                 return 1;
128         }
129
130         /*
131          * Figure out the struct name.  If we're writing to a .so file,
132          * generate raw output insted.
133          */
134         name = strdup(argv[2]);
135         namelen = strlen(name);
136         if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
137                 name = NULL;
138         } else {
139                 tmp = strrchr(name, '/');
140                 if (tmp)
141                         name = tmp + 1;
142                 tmp = strchr(name, '.');
143                 if (tmp)
144                         *tmp = '\0';
145                 for (tmp = name; *tmp; tmp++)
146                         if (*tmp == '-')
147                                 *tmp = '_';
148         }
149
150         fd = open(argv[1], O_RDONLY);
151         if (fd == -1)
152                 err(1, "%s", argv[1]);
153
154         len = lseek(fd, 0, SEEK_END);
155         if (len == (off_t)-1)
156                 err(1, "lseek");
157
158         addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
159         if (addr == MAP_FAILED)
160                 err(1, "mmap");
161
162         outfilename = argv[2];
163         outfile = fopen(outfilename, "w");
164         if (!outfile)
165                 err(1, "%s", argv[2]);
166
167         go(addr, (size_t)len, outfile, name);
168
169         munmap(addr, len);
170         fclose(outfile);
171
172         return 0;
173 }