xref: /openbmc/linux/arch/sparc/vdso/vdso2c.c (revision c4c3c32d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vdso2c - A vdso image preparation tool
4  * Copyright (c) 2014 Andy Lutomirski and others
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 };
67 
68 struct vdso_sym {
69 	const char *name;
70 	int export;
71 };
72 
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
77 	},
78 	[sym_VDSO_FAKE_SECTION_TABLE_END] = {
79 		"VDSO_FAKE_SECTION_TABLE_END", 0
80 	},
81 };
82 
83 __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
84 static void fail(const char *format, ...)
85 {
86 	va_list ap;
87 
88 	va_start(ap, format);
89 	fprintf(stderr, "Error: ");
90 	vfprintf(stderr, format, ap);
91 	if (outfilename)
92 		unlink(outfilename);
93 	exit(1);
94 	va_end(ap);
95 }
96 
97 /*
98  * Evil macros for big-endian reads and writes
99  */
100 #define GBE(x, bits, ifnot)						\
101 	__builtin_choose_expr(						\
102 		(sizeof(*(x)) == bits/8),				\
103 		(__typeof__(*(x)))get_unaligned_be##bits(x), ifnot)
104 
105 #define LAST_GBE(x)							\
106 	__builtin_choose_expr(sizeof(*(x)) == 1, *(x), (void)(0))
107 
108 #define GET_BE(x)							\
109 	GBE(x, 64, GBE(x, 32, GBE(x, 16, LAST_GBE(x))))
110 
111 #define PBE(x, val, bits, ifnot)					\
112 	__builtin_choose_expr(						\
113 		(sizeof(*(x)) == bits/8),				\
114 		put_unaligned_be##bits((val), (x)), ifnot)
115 
116 #define LAST_PBE(x, val)						\
117 	__builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), (void)(0))
118 
119 #define PUT_BE(x, val)					\
120 	PBE(x, val, 64, PBE(x, val, 32, PBE(x, val, 16, LAST_PBE(x, val))))
121 
122 #define NSYMS ARRAY_SIZE(required_syms)
123 
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, )
127 
128 #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
129 
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)
133 
134 #define ELF_BITS 64
135 #include "vdso2c.h"
136 #undef ELF_BITS
137 
138 #define ELF_BITS 32
139 #include "vdso2c.h"
140 #undef ELF_BITS
141 
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)
145 {
146 	Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
147 
148 	if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
149 		go64(raw_addr, raw_len, stripped_addr, stripped_len,
150 		     outfile, name);
151 	} else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
152 		go32(raw_addr, raw_len, stripped_addr, stripped_len,
153 		     outfile, name);
154 	} else {
155 		fail("unknown ELF class\n");
156 	}
157 }
158 
159 static void map_input(const char *name, void **addr, size_t *len, int prot)
160 {
161 	off_t tmp_len;
162 
163 	int fd = open(name, O_RDONLY);
164 
165 	if (fd == -1)
166 		err(1, "%s", name);
167 
168 	tmp_len = lseek(fd, 0, SEEK_END);
169 	if (tmp_len == (off_t)-1)
170 		err(1, "lseek");
171 	*len = (size_t)tmp_len;
172 
173 	*addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
174 	if (*addr == MAP_FAILED)
175 		err(1, "mmap");
176 
177 	close(fd);
178 }
179 
180 int main(int argc, char **argv)
181 {
182 	size_t raw_len, stripped_len;
183 	void *raw_addr, *stripped_addr;
184 	FILE *outfile;
185 	char *name, *tmp;
186 	int namelen;
187 
188 	if (argc != 4) {
189 		printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
190 		return 1;
191 	}
192 
193 	/*
194 	 * Figure out the struct name.  If we're writing to a .so file,
195 	 * generate raw output insted.
196 	 */
197 	name = strdup(argv[3]);
198 	namelen = strlen(name);
199 	if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
200 		name = NULL;
201 	} else {
202 		tmp = strrchr(name, '/');
203 		if (tmp)
204 			name = tmp + 1;
205 		tmp = strchr(name, '.');
206 		if (tmp)
207 			*tmp = '\0';
208 		for (tmp = name; *tmp; tmp++)
209 			if (*tmp == '-')
210 				*tmp = '_';
211 	}
212 
213 	map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
214 	map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
215 
216 	outfilename = argv[3];
217 	outfile = fopen(outfilename, "w");
218 	if (!outfile)
219 		err(1, "%s", argv[2]);
220 
221 	go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
222 
223 	munmap(raw_addr, raw_len);
224 	munmap(stripped_addr, stripped_len);
225 	fclose(outfile);
226 
227 	return 0;
228 }
229