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#include <inttypes.h>
42#include <stdint.h>
43#include <unistd.h>
44#include <stdarg.h>
45#include <stdlib.h>
46#include <stdio.h>
47#include <string.h>
48#include <fcntl.h>
49#include <err.h>
50
51#include <sys/mman.h>
52#include <sys/types.h>
53#include <tools/be_byteshift.h>
54
55#include <linux/elf.h>
56#include <linux/types.h>
57#include <linux/kernel.h>
58
59const char *outfilename;
60
61
62enum {
63 sym_vvar_start,
64 sym_VDSO_FAKE_SECTION_TABLE_START,
65 sym_VDSO_FAKE_SECTION_TABLE_END,
66};
67
68struct vdso_sym {
69 const char *name;
70 int export;
71};
72
73struct vdso_sym required_syms[] = {
74 [sym_vvar_start] = {"vvar_start", 1},
75 [sym_VDSO_FAKE_SECTION_TABLE_START] = {
76 "VDSO_FAKE_SECTION_TABLE_START", 0
77 },
78 [sym_VDSO_FAKE_SECTION_TABLE_END] = {
79 "VDSO_FAKE_SECTION_TABLE_END", 0
80 },
81};
82
83__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
84static void fail(const char *format, ...)
85{
86 va_list ap;
87
88 va_start(ap, format);
89 fprintf(stderr, "Error: ");
90 vfprintf(stderr, format, ap);
91 if (outfilename)
92 unlink(outfilename);
93 exit(1);
94 va_end(ap);
95}
96
97
98
99
100#define GBE(x, bits, ifnot) \
101 __builtin_choose_expr( \
102 (sizeof(*(x)) == bits/8), \
103 (__typeof__(*(x)))get_unaligned_be##bits(x), ifnot)
104
105#define LAST_GBE(x) \
106 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), (void)(0))
107
108#define GET_BE(x) \
109 GBE(x, 64, GBE(x, 32, GBE(x, 16, LAST_GBE(x))))
110
111#define PBE(x, val, bits, ifnot) \
112 __builtin_choose_expr( \
113 (sizeof(*(x)) == bits/8), \
114 put_unaligned_be##bits((val), (x)), ifnot)
115
116#define LAST_PBE(x, val) \
117 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), (void)(0))
118
119#define PUT_BE(x, val) \
120 PBE(x, val, 64, PBE(x, val, 32, PBE(x, val, 16, LAST_PBE(x, val))))
121
122#define NSYMS ARRAY_SIZE(required_syms)
123
124#define BITSFUNC3(name, bits, suffix) name##bits##suffix
125#define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
126#define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
127
128#define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
129
130#define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
131#define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
132#define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
133
134#define ELF_BITS 64
135#include "vdso2c.h"
136#undef ELF_BITS
137
138#define ELF_BITS 32
139#include "vdso2c.h"
140#undef ELF_BITS
141
142static void go(void *raw_addr, size_t raw_len,
143 void *stripped_addr, size_t stripped_len,
144 FILE *outfile, const char *name)
145{
146 Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
147
148 if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
149 go64(raw_addr, raw_len, stripped_addr, stripped_len,
150 outfile, name);
151 } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
152 go32(raw_addr, raw_len, stripped_addr, stripped_len,
153 outfile, name);
154 } else {
155 fail("unknown ELF class\n");
156 }
157}
158
159static void map_input(const char *name, void **addr, size_t *len, int prot)
160{
161 off_t tmp_len;
162
163 int fd = open(name, O_RDONLY);
164
165 if (fd == -1)
166 err(1, "%s", name);
167
168 tmp_len = lseek(fd, 0, SEEK_END);
169 if (tmp_len == (off_t)-1)
170 err(1, "lseek");
171 *len = (size_t)tmp_len;
172
173 *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
174 if (*addr == MAP_FAILED)
175 err(1, "mmap");
176
177 close(fd);
178}
179
180int main(int argc, char **argv)
181{
182 size_t raw_len, stripped_len;
183 void *raw_addr, *stripped_addr;
184 FILE *outfile;
185 char *name, *tmp;
186 int namelen;
187
188 if (argc != 4) {
189 printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
190 return 1;
191 }
192
193
194
195
196
197 name = strdup(argv[3]);
198 namelen = strlen(name);
199 if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
200 name = NULL;
201 } else {
202 tmp = strrchr(name, '/');
203 if (tmp)
204 name = tmp + 1;
205 tmp = strchr(name, '.');
206 if (tmp)
207 *tmp = '\0';
208 for (tmp = name; *tmp; tmp++)
209 if (*tmp == '-')
210 *tmp = '_';
211 }
212
213 map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
214 map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
215
216 outfilename = argv[3];
217 outfile = fopen(outfilename, "w");
218 if (!outfile)
219 err(1, "%s", argv[2]);
220
221 go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
222
223 munmap(raw_addr, raw_len);
224 munmap(stripped_addr, stripped_len);
225 fclose(outfile);
226
227 return 0;
228}
229