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 keep a section table for a few reasons:
16 * Binutils has issues debugging the vDSO: it reads the section table to
17 * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which
18 * would break build-id if we removed the section table. Binutils
19 * also requires that shstrndx != 0. See:
20 * https://sourceware.org/bugzilla/show_bug.cgi?id=17064
22 * elfutils might not look for PT_NOTE if there is a section table at
23 * all. I don't know whether this matters for any practical purpose.
25 * For simplicity, rather than hacking up a partial section table, we
26 * just write a mostly complete one. We omit non-dynamic symbols,
27 * though, since they're rather large.
29 * Once binutils gets fixed, we might be able to drop this for all but
30 * the 64-bit vdso, since build-id only works in kernel RPMs, and
31 * systems that update to new enough kernel RPMs will likely update
32 * binutils in sync. build-id has never worked for home-built kernel
33 * RPMs without manual symlinking, and I suspect that no one ever does
38 * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
52 #include <sys/types.h>
53 #include <tools/be_byteshift.h>
55 #include <linux/elf.h>
56 #include <linux/types.h>
57 #include <linux/kernel.h>
59 const char *outfilename
;
61 /* Symbols that we need in vdso2c. */
64 sym_VDSO_FAKE_SECTION_TABLE_START
,
65 sym_VDSO_FAKE_SECTION_TABLE_END
,
73 struct 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
78 [sym_VDSO_FAKE_SECTION_TABLE_END
] = {
79 "VDSO_FAKE_SECTION_TABLE_END", 0
83 __attribute__((format(printf
, 1, 2))) __attribute__((noreturn
))
84 static void fail(const char *format
, ...)
89 fprintf(stderr
, "Error: ");
90 vfprintf(stderr
, format
, ap
);
98 * Evil macros for big-endian reads and writes
100 #define GBE(x, bits, ifnot) \
101 __builtin_choose_expr( \
102 (sizeof(*(x)) == bits/8), \
103 (__typeof__(*(x)))get_unaligned_be##bits(x), ifnot)
105 #define LAST_GBE(x) \
106 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), (void)(0))
109 GBE(x, 64, GBE(x, 32, GBE(x, 16, LAST_GBE(x))))
111 #define PBE(x, val, bits, ifnot) \
112 __builtin_choose_expr( \
113 (sizeof(*(x)) == bits/8), \
114 put_unaligned_be##bits((val), (x)), ifnot)
116 #define LAST_PBE(x, val) \
117 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), (void)(0))
119 #define PUT_BE(x, val) \
120 PBE(x, val, 64, PBE(x, val, 32, PBE(x, val, 16, LAST_PBE(x, val))))
122 #define NSYMS ARRAY_SIZE(required_syms)
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, )
128 #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
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)
142 static void go(void *raw_addr
, size_t raw_len
,
143 void *stripped_addr
, size_t stripped_len
,
144 FILE *outfile
, const char *name
)
146 Elf64_Ehdr
*hdr
= (Elf64_Ehdr
*)raw_addr
;
148 if (hdr
->e_ident
[EI_CLASS
] == ELFCLASS64
) {
149 go64(raw_addr
, raw_len
, stripped_addr
, stripped_len
,
151 } else if (hdr
->e_ident
[EI_CLASS
] == ELFCLASS32
) {
152 go32(raw_addr
, raw_len
, stripped_addr
, stripped_len
,
155 fail("unknown ELF class\n");
159 static void map_input(const char *name
, void **addr
, size_t *len
, int prot
)
163 int fd
= open(name
, O_RDONLY
);
168 tmp_len
= lseek(fd
, 0, SEEK_END
);
169 if (tmp_len
== (off_t
)-1)
171 *len
= (size_t)tmp_len
;
173 *addr
= mmap(NULL
, tmp_len
, prot
, MAP_PRIVATE
, fd
, 0);
174 if (*addr
== MAP_FAILED
)
180 int main(int argc
, char **argv
)
182 size_t raw_len
, stripped_len
;
183 void *raw_addr
, *stripped_addr
;
189 printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
194 * Figure out the struct name. If we're writing to a .so file,
195 * generate raw output insted.
197 name
= strdup(argv
[3]);
198 namelen
= strlen(name
);
199 if (namelen
>= 3 && !strcmp(name
+ namelen
- 3, ".so")) {
202 tmp
= strrchr(name
, '/');
205 tmp
= strchr(name
, '.');
208 for (tmp
= name
; *tmp
; tmp
++)
213 map_input(argv
[1], &raw_addr
, &raw_len
, PROT_READ
);
214 map_input(argv
[2], &stripped_addr
, &stripped_len
, PROT_READ
);
216 outfilename
= argv
[3];
217 outfile
= fopen(outfilename
, "w");
219 err(1, "%s", argv
[2]);
221 go(raw_addr
, raw_len
, stripped_addr
, stripped_len
, outfile
, name
);
223 munmap(raw_addr
, raw_len
);
224 munmap(stripped_addr
, stripped_len
);