• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Imagination Technologies
3  * Author: Alex Smith <alex.smith@imgtec.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation;  either version 2 of the  License, or (at your
8  * option) any later version.
9  */
10 
11 /*
12  * This tool is used to generate the real VDSO images from the raw image. It
13  * first patches up the MIPS ABI flags and GNU attributes sections defined in
14  * elf.S to have the correct name and type. It then generates a C source file
15  * to be compiled into the kernel containing the VDSO image data and a
16  * mips_vdso_image struct for it, including symbol offsets extracted from the
17  * image.
18  *
19  * We need to be passed both a stripped and unstripped VDSO image. The stripped
20  * image is compiled into the kernel, but we must also patch up the unstripped
21  * image's ABI flags sections so that it can be installed and used for
22  * debugging.
23  */
24 
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 
29 #include <byteswap.h>
30 #include <elf.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <stdarg.h>
35 #include <stdbool.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 /* Define these in case the system elf.h is not new enough to have them. */
42 #ifndef SHT_GNU_ATTRIBUTES
43 # define SHT_GNU_ATTRIBUTES	0x6ffffff5
44 #endif
45 #ifndef SHT_MIPS_ABIFLAGS
46 # define SHT_MIPS_ABIFLAGS	0x7000002a
47 #endif
48 
49 enum {
50 	ABI_O32 = (1 << 0),
51 	ABI_N32 = (1 << 1),
52 	ABI_N64 = (1 << 2),
53 
54 	ABI_ALL = ABI_O32 | ABI_N32 | ABI_N64,
55 };
56 
57 /* Symbols the kernel requires offsets for. */
58 static struct {
59 	const char *name;
60 	const char *offset_name;
61 	unsigned int abis;
62 } vdso_symbols[] = {
63 	{ "__vdso_sigreturn", "off_sigreturn", ABI_O32 },
64 	{ "__vdso_rt_sigreturn", "off_rt_sigreturn", ABI_ALL },
65 	{}
66 };
67 
68 static const char *program_name;
69 static const char *vdso_name;
70 static unsigned char elf_class;
71 static unsigned int elf_abi;
72 static bool need_swap;
73 static FILE *out_file;
74 
75 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
76 # define HOST_ORDER		ELFDATA2LSB
77 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
78 # define HOST_ORDER		ELFDATA2MSB
79 #endif
80 
81 #define BUILD_SWAP(bits)						\
82 	static uint##bits##_t swap_uint##bits(uint##bits##_t val)	\
83 	{								\
84 		return need_swap ? bswap_##bits(val) : val;		\
85 	}
86 
87 BUILD_SWAP(16)
88 BUILD_SWAP(32)
89 BUILD_SWAP(64)
90 
91 #define __FUNC(name, bits) name##bits
92 #define _FUNC(name, bits) __FUNC(name, bits)
93 #define FUNC(name) _FUNC(name, ELF_BITS)
94 
95 #define __ELF(x, bits) Elf##bits##_##x
96 #define _ELF(x, bits) __ELF(x, bits)
97 #define ELF(x) _ELF(x, ELF_BITS)
98 
99 /*
100  * Include genvdso.h twice with ELF_BITS defined differently to get functions
101  * for both ELF32 and ELF64.
102  */
103 
104 #define ELF_BITS 64
105 #include "genvdso.h"
106 #undef ELF_BITS
107 
108 #define ELF_BITS 32
109 #include "genvdso.h"
110 #undef ELF_BITS
111 
map_vdso(const char * path,size_t * _size)112 static void *map_vdso(const char *path, size_t *_size)
113 {
114 	int fd;
115 	struct stat stat;
116 	void *addr;
117 	const Elf32_Ehdr *ehdr;
118 
119 	fd = open(path, O_RDWR);
120 	if (fd < 0) {
121 		fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name,
122 			path, strerror(errno));
123 		return NULL;
124 	}
125 
126 	if (fstat(fd, &stat) != 0) {
127 		fprintf(stderr, "%s: Failed to stat '%s': %s\n", program_name,
128 			path, strerror(errno));
129 		close(fd);
130 		return NULL;
131 	}
132 
133 	addr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
134 		    0);
135 	if (addr == MAP_FAILED) {
136 		fprintf(stderr, "%s: Failed to map '%s': %s\n", program_name,
137 			path, strerror(errno));
138 		close(fd);
139 		return NULL;
140 	}
141 
142 	/* ELF32/64 header formats are the same for the bits we're checking. */
143 	ehdr = addr;
144 
145 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
146 		fprintf(stderr, "%s: '%s' is not an ELF file\n", program_name,
147 			path);
148 		close(fd);
149 		return NULL;
150 	}
151 
152 	elf_class = ehdr->e_ident[EI_CLASS];
153 	switch (elf_class) {
154 	case ELFCLASS32:
155 	case ELFCLASS64:
156 		break;
157 	default:
158 		fprintf(stderr, "%s: '%s' has invalid ELF class\n",
159 			program_name, path);
160 		close(fd);
161 		return NULL;
162 	}
163 
164 	switch (ehdr->e_ident[EI_DATA]) {
165 	case ELFDATA2LSB:
166 	case ELFDATA2MSB:
167 		need_swap = ehdr->e_ident[EI_DATA] != HOST_ORDER;
168 		break;
169 	default:
170 		fprintf(stderr, "%s: '%s' has invalid ELF data order\n",
171 			program_name, path);
172 		close(fd);
173 		return NULL;
174 	}
175 
176 	if (swap_uint16(ehdr->e_machine) != EM_MIPS) {
177 		fprintf(stderr,
178 			"%s: '%s' has invalid ELF machine (expected EM_MIPS)\n",
179 			program_name, path);
180 		close(fd);
181 		return NULL;
182 	} else if (swap_uint16(ehdr->e_type) != ET_DYN) {
183 		fprintf(stderr,
184 			"%s: '%s' has invalid ELF type (expected ET_DYN)\n",
185 			program_name, path);
186 		close(fd);
187 		return NULL;
188 	}
189 
190 	*_size = stat.st_size;
191 	close(fd);
192 	return addr;
193 }
194 
patch_vdso(const char * path,void * vdso)195 static bool patch_vdso(const char *path, void *vdso)
196 {
197 	if (elf_class == ELFCLASS64)
198 		return patch_vdso64(path, vdso);
199 	else
200 		return patch_vdso32(path, vdso);
201 }
202 
get_symbols(const char * path,void * vdso)203 static bool get_symbols(const char *path, void *vdso)
204 {
205 	if (elf_class == ELFCLASS64)
206 		return get_symbols64(path, vdso);
207 	else
208 		return get_symbols32(path, vdso);
209 }
210 
main(int argc,char ** argv)211 int main(int argc, char **argv)
212 {
213 	const char *dbg_vdso_path, *vdso_path, *out_path;
214 	void *dbg_vdso, *vdso;
215 	size_t dbg_vdso_size, vdso_size, i;
216 
217 	program_name = argv[0];
218 
219 	if (argc < 4 || argc > 5) {
220 		fprintf(stderr,
221 			"Usage: %s <debug VDSO> <stripped VDSO> <output file> [<name>]\n",
222 			program_name);
223 		return EXIT_FAILURE;
224 	}
225 
226 	dbg_vdso_path = argv[1];
227 	vdso_path = argv[2];
228 	out_path = argv[3];
229 	vdso_name = (argc > 4) ? argv[4] : "";
230 
231 	dbg_vdso = map_vdso(dbg_vdso_path, &dbg_vdso_size);
232 	if (!dbg_vdso)
233 		return EXIT_FAILURE;
234 
235 	vdso = map_vdso(vdso_path, &vdso_size);
236 	if (!vdso)
237 		return EXIT_FAILURE;
238 
239 	/* Patch both the VDSOs' ABI flags sections. */
240 	if (!patch_vdso(dbg_vdso_path, dbg_vdso))
241 		return EXIT_FAILURE;
242 	if (!patch_vdso(vdso_path, vdso))
243 		return EXIT_FAILURE;
244 
245 	if (msync(dbg_vdso, dbg_vdso_size, MS_SYNC) != 0) {
246 		fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
247 			dbg_vdso_path, strerror(errno));
248 		return EXIT_FAILURE;
249 	} else if (msync(vdso, vdso_size, MS_SYNC) != 0) {
250 		fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
251 			vdso_path, strerror(errno));
252 		return EXIT_FAILURE;
253 	}
254 
255 	out_file = fopen(out_path, "w");
256 	if (!out_file) {
257 		fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name,
258 			out_path, strerror(errno));
259 		return EXIT_FAILURE;
260 	}
261 
262 	fprintf(out_file, "/* Automatically generated - do not edit */\n");
263 	fprintf(out_file, "#include <linux/linkage.h>\n");
264 	fprintf(out_file, "#include <linux/mm.h>\n");
265 	fprintf(out_file, "#include <asm/vdso.h>\n");
266 
267 	/* Write out the stripped VDSO data. */
268 	fprintf(out_file,
269 		"static unsigned char vdso_data[PAGE_ALIGN(%zu)] __page_aligned_data = {\n\t",
270 		vdso_size);
271 	for (i = 0; i < vdso_size; i++) {
272 		if (!(i % 10))
273 			fprintf(out_file, "\n\t");
274 		fprintf(out_file, "0x%02x, ", ((unsigned char *)vdso)[i]);
275 	}
276 	fprintf(out_file, "\n};\n");
277 
278 	/* Preallocate a page array. */
279 	fprintf(out_file,
280 		"static struct page *vdso_pages[PAGE_ALIGN(%zu) / PAGE_SIZE];\n",
281 		vdso_size);
282 
283 	fprintf(out_file, "struct mips_vdso_image vdso_image%s%s = {\n",
284 		(vdso_name[0]) ? "_" : "", vdso_name);
285 	fprintf(out_file, "\t.data = vdso_data,\n");
286 	fprintf(out_file, "\t.size = PAGE_ALIGN(%zu),\n", vdso_size);
287 	fprintf(out_file, "\t.mapping = {\n");
288 	fprintf(out_file, "\t\t.name = \"[vdso]\",\n");
289 	fprintf(out_file, "\t\t.pages = vdso_pages,\n");
290 	fprintf(out_file, "\t},\n");
291 
292 	/* Calculate and write symbol offsets to <output file> */
293 	if (!get_symbols(dbg_vdso_path, dbg_vdso)) {
294 		unlink(out_path);
295 		fclose(out_file);
296 		return EXIT_FAILURE;
297 	}
298 
299 	fprintf(out_file, "};\n");
300 	fclose(out_file);
301 
302 	return EXIT_SUCCESS;
303 }
304