Commit | Line | Data |
---|---|---|
da861e18 AL |
1 | /* |
2 | * vdso2c - A vdso image preparation tool | |
3 | * Copyright (c) 2014 Andy Lutomirski and others | |
4 | * Licensed under the GPL v2 | |
5 | * | |
6 | * vdso2c requires stripped and unstripped input. It would be trivial | |
7 | * to fully strip the input in here, but, for reasons described below, | |
8 | * we need to write a section table. Doing this is more or less | |
9 | * equivalent to dropping all non-allocatable sections, but it's | |
10 | * easier to let objcopy handle that instead of doing it ourselves. | |
11 | * If we ever need to do something fancier than what objcopy provides, | |
12 | * it would be straightforward to add here. | |
13 | * | |
14 | * We're keep a section table for a few reasons: | |
15 | * | |
16 | * The Go runtime had a couple of bugs: it would read the section | |
17 | * table to try to figure out how many dynamic symbols there were (it | |
18 | * shouldn't have looked at the section table at all) and, if there | |
19 | * were no SHT_SYNDYM section table entry, it would use an | |
20 | * uninitialized value for the number of symbols. An empty DYNSYM | |
21 | * table would work, but I see no reason not to write a valid one (and | |
22 | * keep full performance for old Go programs). This hack is only | |
23 | * needed on x86_64. | |
24 | * | |
25 | * The bug was introduced on 2012-08-31 by: | |
26 | * https://code.google.com/p/go/source/detail?r=56ea40aac72b | |
27 | * and was fixed on 2014-06-13 by: | |
28 | * https://code.google.com/p/go/source/detail?r=fc1cd5e12595 | |
29 | * | |
30 | * Binutils has issues debugging the vDSO: it reads the section table to | |
31 | * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which | |
32 | * would break build-id if we removed the section table. Binutils | |
33 | * also requires that shstrndx != 0. See: | |
34 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17064 | |
35 | * | |
36 | * elfutils might not look for PT_NOTE if there is a section table at | |
37 | * all. I don't know whether this matters for any practical purpose. | |
38 | * | |
39 | * For simplicity, rather than hacking up a partial section table, we | |
40 | * just write a mostly complete one. We omit non-dynamic symbols, | |
41 | * though, since they're rather large. | |
42 | * | |
43 | * Once binutils gets fixed, we might be able to drop this for all but | |
44 | * the 64-bit vdso, since build-id only works in kernel RPMs, and | |
45 | * systems that update to new enough kernel RPMs will likely update | |
46 | * binutils in sync. build-id has never worked for home-built kernel | |
47 | * RPMs without manual symlinking, and I suspect that no one ever does | |
48 | * that. | |
49 | */ | |
50 | ||
6f121e54 AL |
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 | ||
bdfb9bcc PA |
64 | #include <tools/le_byteshift.h> |
65 | ||
6f121e54 AL |
66 | #include <linux/elf.h> |
67 | #include <linux/types.h> | |
0cfe5b5f | 68 | #include <linux/kernel.h> |
6f121e54 | 69 | |
01156183 AL |
70 | const char *outfilename; |
71 | ||
6f121e54 | 72 | /* Symbols that we need in vdso2c. */ |
18d0a6fd | 73 | enum { |
e6577a7c | 74 | sym_vvar_start, |
18d0a6fd | 75 | sym_vvar_page, |
dac16fba | 76 | sym_pvclock_page, |
90b20432 | 77 | sym_hvclock_page, |
550a77a7 | 78 | sym_timens_page, |
18d0a6fd AL |
79 | }; |
80 | ||
81 | const int special_pages[] = { | |
82 | sym_vvar_page, | |
dac16fba | 83 | sym_pvclock_page, |
90b20432 | 84 | sym_hvclock_page, |
550a77a7 | 85 | sym_timens_page, |
18d0a6fd AL |
86 | }; |
87 | ||
bfad381c AL |
88 | struct vdso_sym { |
89 | const char *name; | |
90 | bool export; | |
91 | }; | |
92 | ||
93 | struct vdso_sym required_syms[] = { | |
e6577a7c | 94 | [sym_vvar_start] = {"vvar_start", true}, |
bfad381c | 95 | [sym_vvar_page] = {"vvar_page", true}, |
dac16fba | 96 | [sym_pvclock_page] = {"pvclock_page", true}, |
90b20432 | 97 | [sym_hvclock_page] = {"hvclock_page", true}, |
550a77a7 | 98 | [sym_timens_page] = {"timens_page", true}, |
bfad381c | 99 | {"VDSO32_NOTE_MASK", true}, |
bfad381c AL |
100 | {"__kernel_vsyscall", true}, |
101 | {"__kernel_sigreturn", true}, | |
102 | {"__kernel_rt_sigreturn", true}, | |
8242c6c8 | 103 | {"int80_landing_pad", true}, |
6f121e54 AL |
104 | }; |
105 | ||
106 | __attribute__((format(printf, 1, 2))) __attribute__((noreturn)) | |
107 | static void fail(const char *format, ...) | |
108 | { | |
109 | va_list ap; | |
110 | va_start(ap, format); | |
111 | fprintf(stderr, "Error: "); | |
112 | vfprintf(stderr, format, ap); | |
da861e18 AL |
113 | if (outfilename) |
114 | unlink(outfilename); | |
6f121e54 AL |
115 | exit(1); |
116 | va_end(ap); | |
117 | } | |
118 | ||
add4eed0 | 119 | /* |
b4b31f61 | 120 | * Evil macros for little-endian reads and writes |
add4eed0 | 121 | */ |
c191920f | 122 | #define GLE(x, bits, ifnot) \ |
add4eed0 | 123 | __builtin_choose_expr( \ |
bdfb9bcc PA |
124 | (sizeof(*(x)) == bits/8), \ |
125 | (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot) | |
add4eed0 | 126 | |
bdfb9bcc | 127 | extern void bad_get_le(void); |
b4b31f61 | 128 | #define LAST_GLE(x) \ |
bdfb9bcc | 129 | __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le()) |
add4eed0 | 130 | |
c191920f | 131 | #define GET_LE(x) \ |
b4b31f61 AL |
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 | ||
139 | extern 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 | ||
add4eed0 | 146 | |
0cfe5b5f | 147 | #define NSYMS ARRAY_SIZE(required_syms) |
6f121e54 | 148 | |
e6577a7c AL |
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) | |
c1979c37 AL |
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 | |
6f121e54 | 160 | #include "vdso2c.h" |
c1979c37 AL |
161 | #undef ELF_BITS |
162 | ||
163 | #define ELF_BITS 32 | |
6f121e54 | 164 | #include "vdso2c.h" |
c1979c37 | 165 | #undef ELF_BITS |
6f121e54 | 166 | |
da861e18 AL |
167 | static void go(void *raw_addr, size_t raw_len, |
168 | void *stripped_addr, size_t stripped_len, | |
169 | FILE *outfile, const char *name) | |
6f121e54 | 170 | { |
da861e18 | 171 | Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr; |
6f121e54 AL |
172 | |
173 | if (hdr->e_ident[EI_CLASS] == ELFCLASS64) { | |
da861e18 AL |
174 | go64(raw_addr, raw_len, stripped_addr, stripped_len, |
175 | outfile, name); | |
6f121e54 | 176 | } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) { |
da861e18 AL |
177 | go32(raw_addr, raw_len, stripped_addr, stripped_len, |
178 | outfile, name); | |
6f121e54 | 179 | } else { |
01156183 | 180 | fail("unknown ELF class\n"); |
6f121e54 AL |
181 | } |
182 | } | |
183 | ||
da861e18 AL |
184 | static 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 | ||
6f121e54 AL |
204 | int main(int argc, char **argv) |
205 | { | |
da861e18 AL |
206 | size_t raw_len, stripped_len; |
207 | void *raw_addr, *stripped_addr; | |
6f121e54 | 208 | FILE *outfile; |
6f121e54 AL |
209 | char *name, *tmp; |
210 | int namelen; | |
211 | ||
da861e18 AL |
212 | if (argc != 4) { |
213 | printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n"); | |
6f121e54 AL |
214 | return 1; |
215 | } | |
216 | ||
217 | /* | |
218 | * Figure out the struct name. If we're writing to a .so file, | |
219 | * generate raw output insted. | |
220 | */ | |
da861e18 | 221 | name = strdup(argv[3]); |
6f121e54 AL |
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 | ||
da861e18 AL |
237 | map_input(argv[1], &raw_addr, &raw_len, PROT_READ); |
238 | map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ); | |
6f121e54 | 239 | |
da861e18 | 240 | outfilename = argv[3]; |
01156183 | 241 | outfile = fopen(outfilename, "w"); |
6f121e54 AL |
242 | if (!outfile) |
243 | err(1, "%s", argv[2]); | |
244 | ||
da861e18 | 245 | go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name); |
6f121e54 | 246 | |
da861e18 AL |
247 | munmap(raw_addr, raw_len); |
248 | munmap(stripped_addr, stripped_len); | |
6f121e54 AL |
249 | fclose(outfile); |
250 | ||
01156183 | 251 | return 0; |
6f121e54 | 252 | } |