This source file includes following definitions.
- fail
- go
- map_input
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 #include <inttypes.h>
52 #include <stdint.h>
53 #include <unistd.h>
54 #include <stdarg.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <fcntl.h>
59 #include <err.h>
60
61 #include <sys/mman.h>
62 #include <sys/types.h>
63
64 #include <tools/le_byteshift.h>
65
66 #include <linux/elf.h>
67 #include <linux/types.h>
68 #include <linux/kernel.h>
69
70 const char *outfilename;
71
72
73 enum {
74 sym_vvar_start,
75 sym_vvar_page,
76 sym_pvclock_page,
77 sym_hvclock_page,
78 };
79
80 const int special_pages[] = {
81 sym_vvar_page,
82 sym_pvclock_page,
83 sym_hvclock_page,
84 };
85
86 struct vdso_sym {
87 const char *name;
88 bool export;
89 };
90
91 struct vdso_sym required_syms[] = {
92 [sym_vvar_start] = {"vvar_start", true},
93 [sym_vvar_page] = {"vvar_page", true},
94 [sym_pvclock_page] = {"pvclock_page", true},
95 [sym_hvclock_page] = {"hvclock_page", true},
96 {"VDSO32_NOTE_MASK", true},
97 {"__kernel_vsyscall", true},
98 {"__kernel_sigreturn", true},
99 {"__kernel_rt_sigreturn", true},
100 {"int80_landing_pad", true},
101 };
102
103 __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
104 static void fail(const char *format, ...)
105 {
106 va_list ap;
107 va_start(ap, format);
108 fprintf(stderr, "Error: ");
109 vfprintf(stderr, format, ap);
110 if (outfilename)
111 unlink(outfilename);
112 exit(1);
113 va_end(ap);
114 }
115
116
117
118
119 #define GLE(x, bits, ifnot) \
120 __builtin_choose_expr( \
121 (sizeof(*(x)) == bits/8), \
122 (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
123
124 extern void bad_get_le(void);
125 #define LAST_GLE(x) \
126 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
127
128 #define GET_LE(x) \
129 GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
130
131 #define PLE(x, val, bits, ifnot) \
132 __builtin_choose_expr( \
133 (sizeof(*(x)) == bits/8), \
134 put_unaligned_le##bits((val), (x)), ifnot)
135
136 extern void bad_put_le(void);
137 #define LAST_PLE(x, val) \
138 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
139
140 #define PUT_LE(x, val) \
141 PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
142
143
144 #define NSYMS ARRAY_SIZE(required_syms)
145
146 #define BITSFUNC3(name, bits, suffix) name##bits##suffix
147 #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
148 #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
149
150 #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
151
152 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
153 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
154 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
155
156 #define ELF_BITS 64
157 #include "vdso2c.h"
158 #undef ELF_BITS
159
160 #define ELF_BITS 32
161 #include "vdso2c.h"
162 #undef ELF_BITS
163
164 static void go(void *raw_addr, size_t raw_len,
165 void *stripped_addr, size_t stripped_len,
166 FILE *outfile, const char *name)
167 {
168 Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
169
170 if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
171 go64(raw_addr, raw_len, stripped_addr, stripped_len,
172 outfile, name);
173 } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
174 go32(raw_addr, raw_len, stripped_addr, stripped_len,
175 outfile, name);
176 } else {
177 fail("unknown ELF class\n");
178 }
179 }
180
181 static void map_input(const char *name, void **addr, size_t *len, int prot)
182 {
183 off_t tmp_len;
184
185 int fd = open(name, O_RDONLY);
186 if (fd == -1)
187 err(1, "%s", name);
188
189 tmp_len = lseek(fd, 0, SEEK_END);
190 if (tmp_len == (off_t)-1)
191 err(1, "lseek");
192 *len = (size_t)tmp_len;
193
194 *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
195 if (*addr == MAP_FAILED)
196 err(1, "mmap");
197
198 close(fd);
199 }
200
201 int main(int argc, char **argv)
202 {
203 size_t raw_len, stripped_len;
204 void *raw_addr, *stripped_addr;
205 FILE *outfile;
206 char *name, *tmp;
207 int namelen;
208
209 if (argc != 4) {
210 printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
211 return 1;
212 }
213
214
215
216
217
218 name = strdup(argv[3]);
219 namelen = strlen(name);
220 if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
221 name = NULL;
222 } else {
223 tmp = strrchr(name, '/');
224 if (tmp)
225 name = tmp + 1;
226 tmp = strchr(name, '.');
227 if (tmp)
228 *tmp = '\0';
229 for (tmp = name; *tmp; tmp++)
230 if (*tmp == '-')
231 *tmp = '_';
232 }
233
234 map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
235 map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
236
237 outfilename = argv[3];
238 outfile = fopen(outfilename, "w");
239 if (!outfile)
240 err(1, "%s", argv[2]);
241
242 go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
243
244 munmap(raw_addr, raw_len);
245 munmap(stripped_addr, stripped_len);
246 fclose(outfile);
247
248 return 0;
249 }