blob: 8627db24a7f6a25993bc3cb5a53157cab7c7df1a [file] [log] [blame]
Andy Lutomirskida861e12014-07-10 18:13:16 -07001/*
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
Andy Lutomirski6f121e52014-05-05 12:19:34 -070051#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
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070064#include <tools/le_byteshift.h>
65
Andy Lutomirski6f121e52014-05-05 12:19:34 -070066#include <linux/elf.h>
67#include <linux/types.h>
68
Andy Lutomirski01156182014-05-30 08:48:48 -070069const char *outfilename;
70
Andy Lutomirski6f121e52014-05-05 12:19:34 -070071/* Symbols that we need in vdso2c. */
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -070072enum {
Andy Lutomirskie6577a72014-07-10 18:13:15 -070073 sym_vvar_start,
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -070074 sym_vvar_page,
75 sym_hpet_page,
Andy Lutomirskibfad3812014-06-18 15:59:48 -070076 sym_VDSO_FAKE_SECTION_TABLE_START,
77 sym_VDSO_FAKE_SECTION_TABLE_END,
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -070078};
79
80const int special_pages[] = {
81 sym_vvar_page,
82 sym_hpet_page,
83};
84
Andy Lutomirskibfad3812014-06-18 15:59:48 -070085struct vdso_sym {
86 const char *name;
87 bool export;
88};
89
90struct vdso_sym required_syms[] = {
Andy Lutomirskie6577a72014-07-10 18:13:15 -070091 [sym_vvar_start] = {"vvar_start", true},
Andy Lutomirskibfad3812014-06-18 15:59:48 -070092 [sym_vvar_page] = {"vvar_page", true},
93 [sym_hpet_page] = {"hpet_page", true},
Andy Lutomirskibfad3812014-06-18 15:59:48 -070094 [sym_VDSO_FAKE_SECTION_TABLE_START] = {
95 "VDSO_FAKE_SECTION_TABLE_START", false
96 },
97 [sym_VDSO_FAKE_SECTION_TABLE_END] = {
98 "VDSO_FAKE_SECTION_TABLE_END", false
99 },
100 {"VDSO32_NOTE_MASK", true},
101 {"VDSO32_SYSENTER_RETURN", true},
102 {"__kernel_vsyscall", true},
103 {"__kernel_sigreturn", true},
104 {"__kernel_rt_sigreturn", true},
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700105};
106
107__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
108static void fail(const char *format, ...)
109{
110 va_list ap;
111 va_start(ap, format);
112 fprintf(stderr, "Error: ");
113 vfprintf(stderr, format, ap);
Andy Lutomirskida861e12014-07-10 18:13:16 -0700114 if (outfilename)
115 unlink(outfilename);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700116 exit(1);
117 va_end(ap);
118}
119
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700120/*
Andy Lutomirskib4b31f62014-06-12 17:53:11 -0700121 * Evil macros for little-endian reads and writes
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700122 */
H. Peter Anvinc1919202014-05-30 17:03:22 -0700123#define GLE(x, bits, ifnot) \
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700124 __builtin_choose_expr( \
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700125 (sizeof(*(x)) == bits/8), \
126 (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700127
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700128extern void bad_get_le(void);
Andy Lutomirskib4b31f62014-06-12 17:53:11 -0700129#define LAST_GLE(x) \
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700130 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700131
H. Peter Anvinc1919202014-05-30 17:03:22 -0700132#define GET_LE(x) \
Andy Lutomirskib4b31f62014-06-12 17:53:11 -0700133 GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
134
135#define PLE(x, val, bits, ifnot) \
136 __builtin_choose_expr( \
137 (sizeof(*(x)) == bits/8), \
138 put_unaligned_le##bits((val), (x)), ifnot)
139
140extern void bad_put_le(void);
141#define LAST_PLE(x, val) \
142 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
143
144#define PUT_LE(x, val) \
145 PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
146
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700147
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700148#define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
149
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700150#define BITSFUNC3(name, bits, suffix) name##bits##suffix
151#define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
152#define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
153
154#define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700155
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700156#define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
157#define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
158#define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
159
160#define ELF_BITS 64
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700161#include "vdso2c.h"
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700162#undef ELF_BITS
163
164#define ELF_BITS 32
165#include "vdso2c.h"
166#undef ELF_BITS
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700167
Andy Lutomirskida861e12014-07-10 18:13:16 -0700168static void go(void *raw_addr, size_t raw_len,
169 void *stripped_addr, size_t stripped_len,
170 FILE *outfile, const char *name)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700171{
Andy Lutomirskida861e12014-07-10 18:13:16 -0700172 Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700173
174 if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
Andy Lutomirskida861e12014-07-10 18:13:16 -0700175 go64(raw_addr, raw_len, stripped_addr, stripped_len,
176 outfile, name);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700177 } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
Andy Lutomirskida861e12014-07-10 18:13:16 -0700178 go32(raw_addr, raw_len, stripped_addr, stripped_len,
179 outfile, name);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700180 } else {
Andy Lutomirski01156182014-05-30 08:48:48 -0700181 fail("unknown ELF class\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700182 }
183}
184
Andy Lutomirskida861e12014-07-10 18:13:16 -0700185static void map_input(const char *name, void **addr, size_t *len, int prot)
186{
187 off_t tmp_len;
188
189 int fd = open(name, O_RDONLY);
190 if (fd == -1)
191 err(1, "%s", name);
192
193 tmp_len = lseek(fd, 0, SEEK_END);
194 if (tmp_len == (off_t)-1)
195 err(1, "lseek");
196 *len = (size_t)tmp_len;
197
198 *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
199 if (*addr == MAP_FAILED)
200 err(1, "mmap");
201
202 close(fd);
203}
204
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700205int main(int argc, char **argv)
206{
Andy Lutomirskida861e12014-07-10 18:13:16 -0700207 size_t raw_len, stripped_len;
208 void *raw_addr, *stripped_addr;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700209 FILE *outfile;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700210 char *name, *tmp;
211 int namelen;
212
Andy Lutomirskida861e12014-07-10 18:13:16 -0700213 if (argc != 4) {
214 printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700215 return 1;
216 }
217
218 /*
219 * Figure out the struct name. If we're writing to a .so file,
220 * generate raw output insted.
221 */
Andy Lutomirskida861e12014-07-10 18:13:16 -0700222 name = strdup(argv[3]);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700223 namelen = strlen(name);
224 if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
225 name = NULL;
226 } else {
227 tmp = strrchr(name, '/');
228 if (tmp)
229 name = tmp + 1;
230 tmp = strchr(name, '.');
231 if (tmp)
232 *tmp = '\0';
233 for (tmp = name; *tmp; tmp++)
234 if (*tmp == '-')
235 *tmp = '_';
236 }
237
Andy Lutomirskida861e12014-07-10 18:13:16 -0700238 map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
239 map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700240
Andy Lutomirskida861e12014-07-10 18:13:16 -0700241 outfilename = argv[3];
Andy Lutomirski01156182014-05-30 08:48:48 -0700242 outfile = fopen(outfilename, "w");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700243 if (!outfile)
244 err(1, "%s", argv[2]);
245
Andy Lutomirskida861e12014-07-10 18:13:16 -0700246 go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700247
Andy Lutomirskida861e12014-07-10 18:13:16 -0700248 munmap(raw_addr, raw_len);
249 munmap(stripped_addr, stripped_len);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700250 fclose(outfile);
251
Andy Lutomirski01156182014-05-30 08:48:48 -0700252 return 0;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700253}