xref: /linux/arch/sparc/vdso/vdso2c.c (revision 791d3ef2e11100449837dc0b6fe884e60ca3a484)
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 keep a section table for a few reasons:
15  *
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
21  *
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.
24  *
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.
28  *
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
34  * that.
35  */
36 
37 /*
38  * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
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 
59 const char *outfilename;
60 
61 /* Symbols that we need in vdso2c. */
62 enum {
63 	sym_vvar_start,
64 	sym_VDSO_FAKE_SECTION_TABLE_START,
65 	sym_VDSO_FAKE_SECTION_TABLE_END,
66 	sym_vread_tick,
67 	sym_vread_tick_patch_start,
68 	sym_vread_tick_patch_end
69 };
70 
71 struct vdso_sym {
72 	const char *name;
73 	int export;
74 };
75 
76 struct vdso_sym required_syms[] = {
77 	[sym_vvar_start] = {"vvar_start", 1},
78 	[sym_VDSO_FAKE_SECTION_TABLE_START] = {
79 		"VDSO_FAKE_SECTION_TABLE_START", 0
80 	},
81 	[sym_VDSO_FAKE_SECTION_TABLE_END] = {
82 		"VDSO_FAKE_SECTION_TABLE_END", 0
83 	},
84 	[sym_vread_tick] = {"vread_tick", 1},
85 	[sym_vread_tick_patch_start] = {"vread_tick_patch_start", 1},
86 	[sym_vread_tick_patch_end] = {"vread_tick_patch_end", 1}
87 };
88 
89 __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
90 static void fail(const char *format, ...)
91 {
92 	va_list ap;
93 
94 	va_start(ap, format);
95 	fprintf(stderr, "Error: ");
96 	vfprintf(stderr, format, ap);
97 	if (outfilename)
98 		unlink(outfilename);
99 	exit(1);
100 	va_end(ap);
101 }
102 
103 /*
104  * Evil macros for big-endian reads and writes
105  */
106 #define GBE(x, bits, ifnot)						\
107 	__builtin_choose_expr(						\
108 		(sizeof(*(x)) == bits/8),				\
109 		(__typeof__(*(x)))get_unaligned_be##bits(x), ifnot)
110 
111 #define LAST_GBE(x)							\
112 	__builtin_choose_expr(sizeof(*(x)) == 1, *(x), (void)(0))
113 
114 #define GET_BE(x)							\
115 	GBE(x, 64, GBE(x, 32, GBE(x, 16, LAST_GBE(x))))
116 
117 #define PBE(x, val, bits, ifnot)					\
118 	__builtin_choose_expr(						\
119 		(sizeof(*(x)) == bits/8),				\
120 		put_unaligned_be##bits((val), (x)), ifnot)
121 
122 #define LAST_PBE(x, val)						\
123 	__builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), (void)(0))
124 
125 #define PUT_BE(x, val)					\
126 	PBE(x, val, 64, PBE(x, val, 32, PBE(x, val, 16, LAST_PBE(x, val))))
127 
128 #define NSYMS ARRAY_SIZE(required_syms)
129 
130 #define BITSFUNC3(name, bits, suffix) name##bits##suffix
131 #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
132 #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
133 
134 #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
135 
136 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
137 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
138 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
139 
140 #define ELF_BITS 64
141 #include "vdso2c.h"
142 #undef ELF_BITS
143 
144 #define ELF_BITS 32
145 #include "vdso2c.h"
146 #undef ELF_BITS
147 
148 static void go(void *raw_addr, size_t raw_len,
149 	       void *stripped_addr, size_t stripped_len,
150 	       FILE *outfile, const char *name)
151 {
152 	Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
153 
154 	if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
155 		go64(raw_addr, raw_len, stripped_addr, stripped_len,
156 		     outfile, name);
157 	} else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
158 		go32(raw_addr, raw_len, stripped_addr, stripped_len,
159 		     outfile, name);
160 	} else {
161 		fail("unknown ELF class\n");
162 	}
163 }
164 
165 static void map_input(const char *name, void **addr, size_t *len, int prot)
166 {
167 	off_t tmp_len;
168 
169 	int fd = open(name, O_RDONLY);
170 
171 	if (fd == -1)
172 		err(1, "%s", name);
173 
174 	tmp_len = lseek(fd, 0, SEEK_END);
175 	if (tmp_len == (off_t)-1)
176 		err(1, "lseek");
177 	*len = (size_t)tmp_len;
178 
179 	*addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
180 	if (*addr == MAP_FAILED)
181 		err(1, "mmap");
182 
183 	close(fd);
184 }
185 
186 int main(int argc, char **argv)
187 {
188 	size_t raw_len, stripped_len;
189 	void *raw_addr, *stripped_addr;
190 	FILE *outfile;
191 	char *name, *tmp;
192 	int namelen;
193 
194 	if (argc != 4) {
195 		printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
196 		return 1;
197 	}
198 
199 	/*
200 	 * Figure out the struct name.  If we're writing to a .so file,
201 	 * generate raw output insted.
202 	 */
203 	name = strdup(argv[3]);
204 	namelen = strlen(name);
205 	if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
206 		name = NULL;
207 	} else {
208 		tmp = strrchr(name, '/');
209 		if (tmp)
210 			name = tmp + 1;
211 		tmp = strchr(name, '.');
212 		if (tmp)
213 			*tmp = '\0';
214 		for (tmp = name; *tmp; tmp++)
215 			if (*tmp == '-')
216 				*tmp = '_';
217 	}
218 
219 	map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
220 	map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
221 
222 	outfilename = argv[3];
223 	outfile = fopen(outfilename, "w");
224 	if (!outfile)
225 		err(1, "%s", argv[2]);
226 
227 	go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
228 
229 	munmap(raw_addr, raw_len);
230 	munmap(stripped_addr, stripped_len);
231 	fclose(outfile);
232 
233 	return 0;
234 }
235