2 * vdso2c - A vdso image preparation tool
3 * Copyright (c) 2014 Andy Lutomirski and others
4 * Licensed under the GPL v2
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.
14 * We're keep a section table for a few reasons:
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
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
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
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.
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.
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
62 #include <sys/types.h>
64 #include <tools/le_byteshift.h>
66 #include <linux/elf.h>
67 #include <linux/types.h>
68 #include <linux/kernel.h>
70 const char *outfilename
;
72 /* Symbols that we need in vdso2c. */
79 sym_VDSO_FAKE_SECTION_TABLE_START
,
80 sym_VDSO_FAKE_SECTION_TABLE_END
,
83 const int special_pages
[] = {
95 struct vdso_sym required_syms
[] = {
96 [sym_vvar_start
] = {"vvar_start", true},
97 [sym_vvar_page
] = {"vvar_page", true},
98 [sym_hpet_page
] = {"hpet_page", true},
99 [sym_pvclock_page
] = {"pvclock_page", true},
100 [sym_hvclock_page
] = {"hvclock_page", true},
101 [sym_VDSO_FAKE_SECTION_TABLE_START
] = {
102 "VDSO_FAKE_SECTION_TABLE_START", false
104 [sym_VDSO_FAKE_SECTION_TABLE_END
] = {
105 "VDSO_FAKE_SECTION_TABLE_END", false
107 {"VDSO32_NOTE_MASK", true},
108 {"__kernel_vsyscall", true},
109 {"__kernel_sigreturn", true},
110 {"__kernel_rt_sigreturn", true},
111 {"int80_landing_pad", true},
114 __attribute__((format(printf
, 1, 2))) __attribute__((noreturn
))
115 static void fail(const char *format
, ...)
118 va_start(ap
, format
);
119 fprintf(stderr
, "Error: ");
120 vfprintf(stderr
, format
, ap
);
128 * Evil macros for little-endian reads and writes
130 #define GLE(x, bits, ifnot) \
131 __builtin_choose_expr( \
132 (sizeof(*(x)) == bits/8), \
133 (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
135 extern void bad_get_le(void);
136 #define LAST_GLE(x) \
137 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
140 GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
142 #define PLE(x, val, bits, ifnot) \
143 __builtin_choose_expr( \
144 (sizeof(*(x)) == bits/8), \
145 put_unaligned_le##bits((val), (x)), ifnot)
147 extern void bad_put_le(void);
148 #define LAST_PLE(x, val) \
149 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
151 #define PUT_LE(x, val) \
152 PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
155 #define NSYMS ARRAY_SIZE(required_syms)
157 #define BITSFUNC3(name, bits, suffix) name##bits##suffix
158 #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
159 #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
161 #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
163 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
164 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
165 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
175 static void go(void *raw_addr
, size_t raw_len
,
176 void *stripped_addr
, size_t stripped_len
,
177 FILE *outfile
, const char *name
)
179 Elf64_Ehdr
*hdr
= (Elf64_Ehdr
*)raw_addr
;
181 if (hdr
->e_ident
[EI_CLASS
] == ELFCLASS64
) {
182 go64(raw_addr
, raw_len
, stripped_addr
, stripped_len
,
184 } else if (hdr
->e_ident
[EI_CLASS
] == ELFCLASS32
) {
185 go32(raw_addr
, raw_len
, stripped_addr
, stripped_len
,
188 fail("unknown ELF class\n");
192 static void map_input(const char *name
, void **addr
, size_t *len
, int prot
)
196 int fd
= open(name
, O_RDONLY
);
200 tmp_len
= lseek(fd
, 0, SEEK_END
);
201 if (tmp_len
== (off_t
)-1)
203 *len
= (size_t)tmp_len
;
205 *addr
= mmap(NULL
, tmp_len
, prot
, MAP_PRIVATE
, fd
, 0);
206 if (*addr
== MAP_FAILED
)
212 int main(int argc
, char **argv
)
214 size_t raw_len
, stripped_len
;
215 void *raw_addr
, *stripped_addr
;
221 printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
226 * Figure out the struct name. If we're writing to a .so file,
227 * generate raw output insted.
229 name
= strdup(argv
[3]);
230 namelen
= strlen(name
);
231 if (namelen
>= 3 && !strcmp(name
+ namelen
- 3, ".so")) {
234 tmp
= strrchr(name
, '/');
237 tmp
= strchr(name
, '.');
240 for (tmp
= name
; *tmp
; tmp
++)
245 map_input(argv
[1], &raw_addr
, &raw_len
, PROT_READ
);
246 map_input(argv
[2], &stripped_addr
, &stripped_len
, PROT_READ
);
248 outfilename
= argv
[3];
249 outfile
= fopen(outfilename
, "w");
251 err(1, "%s", argv
[2]);
253 go(raw_addr
, raw_len
, stripped_addr
, stripped_len
, outfile
, name
);
255 munmap(raw_addr
, raw_len
);
256 munmap(stripped_addr
, stripped_len
);