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
70const char *outfilename;
71
72
73enum {
74 sym_vvar_start,
75 sym_vvar_page,
76 sym_hpet_page,
77 sym_pvclock_page,
78 sym_hvclock_page,
79};
80
81const int special_pages[] = {
82 sym_vvar_page,
83 sym_hpet_page,
84 sym_pvclock_page,
85 sym_hvclock_page,
86};
87
88struct vdso_sym {
89 const char *name;
90 bool export;
91};
92
93struct vdso_sym required_syms[] = {
94 [sym_vvar_start] = {"vvar_start", true},
95 [sym_vvar_page] = {"vvar_page", true},
96 [sym_hpet_page] = {"hpet_page", true},
97 [sym_pvclock_page] = {"pvclock_page", true},
98 [sym_hvclock_page] = {"hvclock_page", true},
99 {"VDSO32_NOTE_MASK", true},
100 {"__kernel_vsyscall", true},
101 {"__kernel_sigreturn", true},
102 {"__kernel_rt_sigreturn", true},
103 {"int80_landing_pad", true},
104};
105
106__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
107static void fail(const char *format, ...)
108{
109 va_list ap;
110 va_start(ap, format);
111 fprintf(stderr, "Error: ");
112 vfprintf(stderr, format, ap);
113 if (outfilename)
114 unlink(outfilename);
115 exit(1);
116 va_end(ap);
117}
118
119
120
121
122#define GLE(x, bits, ifnot) \
123 __builtin_choose_expr( \
124 (sizeof(*(x)) == bits/8), \
125 (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
126
127extern void bad_get_le(void);
128#define LAST_GLE(x) \
129 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
130
131#define GET_LE(x) \
132 GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
133
134#define PLE(x, val, bits, ifnot) \
135 __builtin_choose_expr( \
136 (sizeof(*(x)) == bits/8), \
137 put_unaligned_le##bits((val), (x)), ifnot)
138
139extern void bad_put_le(void);
140#define LAST_PLE(x, val) \
141 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
142
143#define PUT_LE(x, val) \
144 PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
145
146
147#define NSYMS ARRAY_SIZE(required_syms)
148
149#define BITSFUNC3(name, bits, suffix) name##bits##suffix
150#define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
151#define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
152
153#define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
154
155#define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
156#define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
157#define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
158
159#define ELF_BITS 64
160#include "vdso2c.h"
161#undef ELF_BITS
162
163#define ELF_BITS 32
164#include "vdso2c.h"
165#undef ELF_BITS
166
167static void go(void *raw_addr, size_t raw_len,
168 void *stripped_addr, size_t stripped_len,
169 FILE *outfile, const char *name)
170{
171 Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
172
173 if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
174 go64(raw_addr, raw_len, stripped_addr, stripped_len,
175 outfile, name);
176 } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
177 go32(raw_addr, raw_len, stripped_addr, stripped_len,
178 outfile, name);
179 } else {
180 fail("unknown ELF class\n");
181 }
182}
183
184static void map_input(const char *name, void **addr, size_t *len, int prot)
185{
186 off_t tmp_len;
187
188 int fd = open(name, O_RDONLY);
189 if (fd == -1)
190 err(1, "%s", name);
191
192 tmp_len = lseek(fd, 0, SEEK_END);
193 if (tmp_len == (off_t)-1)
194 err(1, "lseek");
195 *len = (size_t)tmp_len;
196
197 *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
198 if (*addr == MAP_FAILED)
199 err(1, "mmap");
200
201 close(fd);
202}
203
204int main(int argc, char **argv)
205{
206 size_t raw_len, stripped_len;
207 void *raw_addr, *stripped_addr;
208 FILE *outfile;
209 char *name, *tmp;
210 int namelen;
211
212 if (argc != 4) {
213 printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
214 return 1;
215 }
216
217
218
219
220
221 name = strdup(argv[3]);
222 namelen = strlen(name);
223 if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
224 name = NULL;
225 } else {
226 tmp = strrchr(name, '/');
227 if (tmp)
228 name = tmp + 1;
229 tmp = strchr(name, '.');
230 if (tmp)
231 *tmp = '\0';
232 for (tmp = name; *tmp; tmp++)
233 if (*tmp == '-')
234 *tmp = '_';
235 }
236
237 map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
238 map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
239
240 outfilename = argv[3];
241 outfile = fopen(outfilename, "w");
242 if (!outfile)
243 err(1, "%s", argv[2]);
244
245 go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
246
247 munmap(raw_addr, raw_len);
248 munmap(stripped_addr, stripped_len);
249 fclose(outfile);
250
251 return 0;
252}
253