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