1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * This file is included twice from vdso2c.c. It generates code for 32-bit
4 * and 64-bit vDSOs. We need both for 64-bit builds, since 32-bit vDSOs
5 * are built for 32-bit userspace.
6 */
7
8 /*
9 * For x86_64 16kB page size emulation
10 *
11 * The redefinition is needed here since, vdso2c is a program that runs
12 * on the host.
13 *
14 * It converts the vdso shared lib to a C array.
15 */
16 #define __MAX_PAGE_SIZE 16384
17
BITSFUNC(copy)18 static void BITSFUNC(copy)(FILE *outfile, const unsigned char *data, size_t len)
19 {
20 size_t i;
21
22 for (i = 0; i < len; i++) {
23 if (i % 10 == 0)
24 fprintf(outfile, "\n\t");
25 fprintf(outfile, "0x%02X, ", (int)(data)[i]);
26 }
27 }
28
29
30 /*
31 * Extract a section from the input data into a standalone blob. Used to
32 * capture kernel-only data that needs to persist indefinitely, e.g. the
33 * exception fixup tables, but only in the kernel, i.e. the section can
34 * be stripped from the final vDSO image.
35 */
BITSFUNC(extract)36 static void BITSFUNC(extract)(const unsigned char *data, size_t data_len,
37 FILE *outfile, ELF(Shdr) *sec, const char *name)
38 {
39 unsigned long offset;
40 size_t len;
41
42 offset = (unsigned long)GET_LE(&sec->sh_offset);
43 len = (size_t)GET_LE(&sec->sh_size);
44
45 if (offset + len > data_len)
46 fail("section to extract overruns input data");
47
48 fprintf(outfile, "static const unsigned char %s[%zu] = {", name, len);
49 BITSFUNC(copy)(outfile, data + offset, len);
50 fprintf(outfile, "\n};\n\n");
51 }
52
BITSFUNC(go)53 static void BITSFUNC(go)(void *raw_addr, size_t raw_len,
54 void *stripped_addr, size_t stripped_len,
55 FILE *outfile, const char *image_name)
56 {
57 int found_load = 0;
58 unsigned long load_size = -1; /* Work around bogus warning */
59 unsigned long mapping_size;
60 ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr;
61 unsigned long i, syms_nr;
62 ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
63 *alt_sec = NULL, *extable_sec = NULL;
64 ELF(Dyn) *dyn = 0, *dyn_end = 0;
65 const char *secstrings;
66 INT_BITS syms[NSYMS] = {};
67
68 ELF(Phdr) *pt = (ELF(Phdr) *)(raw_addr + GET_LE(&hdr->e_phoff));
69
70 if (GET_LE(&hdr->e_type) != ET_DYN)
71 fail("input is not a shared object\n");
72
73 /* Walk the segment table. */
74 for (i = 0; i < GET_LE(&hdr->e_phnum); i++) {
75 if (GET_LE(&pt[i].p_type) == PT_LOAD) {
76 if (found_load)
77 fail("multiple PT_LOAD segs\n");
78
79 if (GET_LE(&pt[i].p_offset) != 0 ||
80 GET_LE(&pt[i].p_vaddr) != 0)
81 fail("PT_LOAD in wrong place\n");
82
83 if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz))
84 fail("cannot handle memsz != filesz\n");
85
86 load_size = GET_LE(&pt[i].p_memsz);
87 found_load = 1;
88 } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) {
89 dyn = raw_addr + GET_LE(&pt[i].p_offset);
90 dyn_end = raw_addr + GET_LE(&pt[i].p_offset) +
91 GET_LE(&pt[i].p_memsz);
92 }
93 }
94 if (!found_load)
95 fail("no PT_LOAD seg\n");
96
97 if (stripped_len < load_size)
98 fail("stripped input is too short\n");
99
100 if (!dyn)
101 fail("input has no PT_DYNAMIC section -- your toolchain is buggy\n");
102
103 /* Walk the dynamic table */
104 for (i = 0; dyn + i < dyn_end &&
105 GET_LE(&dyn[i].d_tag) != DT_NULL; i++) {
106 typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag);
107 if (tag == DT_REL || tag == DT_RELSZ || tag == DT_RELA ||
108 tag == DT_RELENT || tag == DT_TEXTREL)
109 fail("vdso image contains dynamic relocations\n");
110 }
111
112 /* Walk the section table */
113 secstrings_hdr = raw_addr + GET_LE(&hdr->e_shoff) +
114 GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx);
115 secstrings = raw_addr + GET_LE(&secstrings_hdr->sh_offset);
116 for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
117 ELF(Shdr) *sh = raw_addr + GET_LE(&hdr->e_shoff) +
118 GET_LE(&hdr->e_shentsize) * i;
119 if (GET_LE(&sh->sh_type) == SHT_SYMTAB)
120 symtab_hdr = sh;
121
122 if (!strcmp(secstrings + GET_LE(&sh->sh_name),
123 ".altinstructions"))
124 alt_sec = sh;
125 if (!strcmp(secstrings + GET_LE(&sh->sh_name), "__ex_table"))
126 extable_sec = sh;
127 }
128
129 if (!symtab_hdr)
130 fail("no symbol table\n");
131
132 strtab_hdr = raw_addr + GET_LE(&hdr->e_shoff) +
133 GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
134
135 syms_nr = GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
136 /* Walk the symbol table */
137 for (i = 0; i < syms_nr; i++) {
138 unsigned int k;
139 ELF(Sym) *sym = raw_addr + GET_LE(&symtab_hdr->sh_offset) +
140 GET_LE(&symtab_hdr->sh_entsize) * i;
141 const char *sym_name = raw_addr +
142 GET_LE(&strtab_hdr->sh_offset) +
143 GET_LE(&sym->st_name);
144
145 for (k = 0; k < NSYMS; k++) {
146 if (!strcmp(sym_name, required_syms[k].name)) {
147 if (syms[k]) {
148 fail("duplicate symbol %s\n",
149 required_syms[k].name);
150 }
151
152 /*
153 * Careful: we use negative addresses, but
154 * st_value is unsigned, so we rely
155 * on syms[k] being a signed type of the
156 * correct width.
157 */
158 syms[k] = GET_LE(&sym->st_value);
159 }
160 }
161 }
162
163 /* Validate mapping addresses. */
164 for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
165 INT_BITS symval = syms[special_pages[i]];
166
167 if (!symval)
168 continue; /* The mapping isn't used; ignore it. */
169
170 if (symval % 4096)
171 fail("%s must be a multiple of 4096\n",
172 required_syms[i].name);
173 if (symval + 4096 < syms[sym_vvar_start])
174 fail("%s underruns vvar_start\n",
175 required_syms[i].name);
176 if (symval + 4096 > 0)
177 fail("%s is on the wrong side of the vdso text\n",
178 required_syms[i].name);
179 }
180 if (syms[sym_vvar_start] % 4096)
181 fail("vvar_begin must be a multiple of 4096\n");
182
183 if (!image_name) {
184 fwrite(stripped_addr, stripped_len, 1, outfile);
185 return;
186 }
187
188 mapping_size = (stripped_len + __MAX_PAGE_SIZE-1) / __MAX_PAGE_SIZE * __MAX_PAGE_SIZE;
189
190 fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
191 fprintf(outfile, "#include <linux/linkage.h>\n");
192 fprintf(outfile, "#include <linux/init.h>\n");
193 fprintf(outfile, "#include <asm/page_types.h>\n");
194 fprintf(outfile, "#include <asm/vdso.h>\n");
195 fprintf(outfile, "\n");
196 fprintf(outfile,
197 "static unsigned char raw_data[%lu] __ro_after_init __aligned(%d) = {",
198 mapping_size, __MAX_PAGE_SIZE);
199 for (i = 0; i < stripped_len; i++) {
200 if (i % 10 == 0)
201 fprintf(outfile, "\n\t");
202 fprintf(outfile, "0x%02X, ",
203 (int)((unsigned char *)stripped_addr)[i]);
204 }
205 fprintf(outfile, "\n};\n\n");
206 if (extable_sec)
207 BITSFUNC(extract)(raw_addr, raw_len, outfile,
208 extable_sec, "extable");
209
210 fprintf(outfile, "const struct vdso_image %s = {\n", image_name);
211 fprintf(outfile, "\t.data = raw_data,\n");
212 fprintf(outfile, "\t.size = %lu,\n", mapping_size);
213 if (alt_sec) {
214 fprintf(outfile, "\t.alt = %lu,\n",
215 (unsigned long)GET_LE(&alt_sec->sh_offset));
216 fprintf(outfile, "\t.alt_len = %lu,\n",
217 (unsigned long)GET_LE(&alt_sec->sh_size));
218 }
219 if (extable_sec) {
220 fprintf(outfile, "\t.extable_base = %lu,\n",
221 (unsigned long)GET_LE(&extable_sec->sh_offset));
222 fprintf(outfile, "\t.extable_len = %lu,\n",
223 (unsigned long)GET_LE(&extable_sec->sh_size));
224 fprintf(outfile, "\t.extable = extable,\n");
225 }
226
227 for (i = 0; i < NSYMS; i++) {
228 if (required_syms[i].export && syms[i])
229 fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
230 required_syms[i].name, (int64_t)syms[i]);
231 }
232 fprintf(outfile, "};\n\n");
233 fprintf(outfile, "static __init int init_%s(void) {\n", image_name);
234 fprintf(outfile, "\treturn init_vdso_image(&%s);\n", image_name);
235 fprintf(outfile, "};\n");
236 fprintf(outfile, "subsys_initcall(init_%s);\n", image_name);
237
238 }
239