master
c 246 lines 6.29 KB
Raw
1 /*
2 * Post-process a vdso elf image for inclusion into qemu.
3 *
4 * Copyright 2023 Linaro, Ltd.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <endian.h>
16 #include <unistd.h>
17 #include "elf.h"
18
19
20 #define bswap_(p) _Generic(*(p), \
21 uint16_t: __builtin_bswap16, \
22 uint32_t: __builtin_bswap32, \
23 uint64_t: __builtin_bswap64, \
24 int16_t: __builtin_bswap16, \
25 int32_t: __builtin_bswap32, \
26 int64_t: __builtin_bswap64)
27 #define bswaps(p) (*(p) = bswap_(p)(*(p)))
28
29 static void output_reloc(FILE *outf, void *buf, void *loc)
30 {
31 fprintf(outf, " 0x%08tx,\n", loc - buf);
32 }
33
34 static const char *sigreturn_sym;
35 static const char *rt_sigreturn_sym;
36
37 static unsigned sigreturn_addr;
38 static unsigned rt_sigreturn_addr;
39 static unsigned sigreturn_region_start_addr;
40 static unsigned sigreturn_region_end_addr;
41
42 #define N 32
43 #define elfN(x) elf32_##x
44 #define ElfN(x) Elf32_##x
45 #include "gen-vdso-elfn.c.inc"
46 #undef N
47 #undef elfN
48 #undef ElfN
49
50 #define N 64
51 #define elfN(x) elf64_##x
52 #define ElfN(x) Elf64_##x
53 #include "gen-vdso-elfn.c.inc"
54 #undef N
55 #undef elfN
56 #undef ElfN
57
58
59 int main(int argc, char **argv)
60 {
61 FILE *inf = NULL, *outf = NULL;
62 long total_len;
63 const char *prefix = "vdso";
64 const char *inf_name;
65 const char *outf_name = NULL;
66 unsigned char *buf = NULL;
67 bool need_bswap;
68 int ret = EXIT_FAILURE;
69
70 while (1) {
71 int opt = getopt(argc, argv, "o:p:r:s:");
72 if (opt < 0) {
73 break;
74 }
75 switch (opt) {
76 case 'o':
77 outf_name = optarg;
78 break;
79 case 'p':
80 prefix = optarg;
81 break;
82 case 'r':
83 rt_sigreturn_sym = optarg;
84 break;
85 case 's':
86 sigreturn_sym = optarg;
87 break;
88 default:
89 usage:
90 fprintf(stderr, "usage: [-p prefix] [-r rt-sigreturn-name] "
91 "[-s sigreturn-name] -o output-file input-file\n");
92 return EXIT_FAILURE;
93 }
94 }
95
96 if (optind >= argc || outf_name == NULL) {
97 goto usage;
98 }
99 inf_name = argv[optind];
100
101 /*
102 * Open the input and output files.
103 */
104 inf = fopen(inf_name, "rb");
105 if (inf == NULL) {
106 goto perror_inf;
107 }
108 outf = fopen(outf_name, "w");
109 if (outf == NULL) {
110 goto perror_outf;
111 }
112
113 /*
114 * Read the input file into a buffer.
115 * We expect the vdso to be small, on the order of one page,
116 * therefore we do not expect a partial read.
117 */
118 if (fseek(inf, 0, SEEK_END) < 0) {
119 goto perror_inf;
120 }
121 total_len = ftell(inf);
122 if (total_len < 0) {
123 goto perror_inf;
124 }
125 if (fseek(inf, 0, SEEK_SET) < 0) {
126 goto perror_inf;
127 }
128
129 if (total_len < EI_NIDENT) {
130 fprintf(stderr, "%s: file too small (truncated?)\n", inf_name);
131 return EXIT_FAILURE;
132 }
133
134 buf = malloc(total_len);
135 if (buf == NULL) {
136 goto perror_inf;
137 }
138
139 errno = 0;
140 if (fread(buf, 1, total_len, inf) != total_len) {
141 if (errno) {
142 goto perror_inf;
143 }
144 fprintf(stderr, "%s: incomplete read\n", inf_name);
145 return EXIT_FAILURE;
146 }
147
148 /*
149 * Identify which elf flavor we're processing.
150 * The first 16 bytes of the file are e_ident.
151 */
152
153 if (buf[EI_MAG0] != ELFMAG0 || buf[EI_MAG1] != ELFMAG1 ||
154 buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3) {
155 fprintf(stderr, "%s: not an elf file\n", inf_name);
156 return EXIT_FAILURE;
157 }
158 switch (buf[EI_DATA]) {
159 case ELFDATA2LSB:
160 need_bswap = BYTE_ORDER != LITTLE_ENDIAN;
161 break;
162 case ELFDATA2MSB:
163 need_bswap = BYTE_ORDER != BIG_ENDIAN;
164 break;
165 default:
166 fprintf(stderr, "%s: invalid elf EI_DATA (%u)\n",
167 inf_name, buf[EI_DATA]);
168 return EXIT_FAILURE;
169 }
170
171 /*
172 * We need to relocate the VDSO image. The one built into the kernel
173 * is built for a fixed address. The one we built for QEMU is not,
174 * since that requires close control of the guest address space.
175 *
176 * Output relocation addresses as we go.
177 */
178
179 fprintf(outf,
180 "/* Automatically generated by linux-user/gen-vdso.c. */\n"
181 "\n"
182 "static const unsigned %s_relocs[] = {\n", prefix);
183
184 switch (buf[EI_CLASS]) {
185 case ELFCLASS32:
186 elf32_process(outf, buf, total_len, need_bswap);
187 break;
188 case ELFCLASS64:
189 elf64_process(outf, buf, total_len, need_bswap);
190 break;
191 default:
192 fprintf(stderr, "%s: invalid elf EI_CLASS (%u)\n",
193 inf_name, buf[EI_CLASS]);
194 return EXIT_FAILURE;
195 }
196
197 fprintf(outf, "};\n\n"); /* end vdso_relocs. */
198
199 /*
200 * Write out the vdso image now, after we made local changes.
201 */
202 fprintf(outf,
203 "static const uint8_t %s_image[] = {",
204 prefix);
205 for (long i = 0; i < total_len; ++i) {
206 if (i % 12 == 0) {
207 fputs("\n ", outf);
208 }
209 fprintf(outf, " 0x%02x,", buf[i]);
210 }
211 fprintf(outf, "\n};\n\n");
212
213 fprintf(outf, "static const VdsoImageInfo %s_image_info = {\n", prefix);
214 fprintf(outf, " .image = %s_image,\n", prefix);
215 fprintf(outf, " .relocs = %s_relocs,\n", prefix);
216 fprintf(outf, " .image_size = sizeof(%s_image),\n", prefix);
217 fprintf(outf, " .reloc_count = ARRAY_SIZE(%s_relocs),\n", prefix);
218 fprintf(outf, " .sigreturn_ofs = 0x%x,\n", sigreturn_addr);
219 fprintf(outf, " .rt_sigreturn_ofs = 0x%x,\n", rt_sigreturn_addr);
220 fprintf(outf, " .sigreturn_region_start_ofs = 0x%x,\n",
221 sigreturn_region_start_addr);
222 fprintf(outf, " .sigreturn_region_end_ofs = 0x%x,\n",
223 sigreturn_region_end_addr);
224 fprintf(outf, "};\n");
225
226 ret = EXIT_SUCCESS;
227
228 cleanup:
229 free(buf);
230
231 if (outf && fclose(outf) != 0) {
232 ret = EXIT_FAILURE;
233 }
234 if (inf && fclose(inf) != 0) {
235 ret = EXIT_FAILURE;
236 }
237 return ret;
238
239 perror_inf:
240 perror(inf_name);
241 goto cleanup;
242
243 perror_outf:
244 perror(outf_name);
245 goto cleanup;
246 }