blob: 2da32fbc46daff5b211af6e826d184ec291c7811 [file] [log] [blame]
Andy Lutomirski6f121e52014-05-05 12:19:34 -07001/*
2 * This file is included twice from vdso2c.c. It generates code for 32-bit
3 * and 64-bit vDSOs. We need both for 64-bit builds, since 32-bit vDSOs
4 * are built for 32-bit userspace.
5 */
6
Andy Lutomirskibfad3812014-06-18 15:59:48 -07007/*
8 * We're writing a section table for a few reasons:
9 *
10 * The Go runtime had a couple of bugs: it would read the section
11 * table to try to figure out how many dynamic symbols there were (it
12 * shouldn't have looked at the section table at all) and, if there
13 * were no SHT_SYNDYM section table entry, it would use an
14 * uninitialized value for the number of symbols. An empty DYNSYM
15 * table would work, but I see no reason not to write a valid one (and
16 * keep full performance for old Go programs). This hack is only
17 * needed on x86_64.
18 *
19 * The bug was introduced on 2012-08-31 by:
20 * https://code.google.com/p/go/source/detail?r=56ea40aac72b
21 * and was fixed on 2014-06-13 by:
22 * https://code.google.com/p/go/source/detail?r=fc1cd5e12595
23 *
24 * Binutils has issues debugging the vDSO: it reads the section table to
25 * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which
26 * would break build-id if we removed the section table. Binutils
27 * also requires that shstrndx != 0. See:
28 * https://sourceware.org/bugzilla/show_bug.cgi?id=17064
29 *
30 * elfutils might not look for PT_NOTE if there is a section table at
31 * all. I don't know whether this matters for any practical purpose.
32 *
33 * For simplicity, rather than hacking up a partial section table, we
34 * just write a mostly complete one. We omit non-dynamic symbols,
35 * though, since they're rather large.
36 *
37 * Once binutils gets fixed, we might be able to drop this for all but
38 * the 64-bit vdso, since build-id only works in kernel RPMs, and
39 * systems that update to new enough kernel RPMs will likely update
40 * binutils in sync. build-id has never worked for home-built kernel
41 * RPMs without manual symlinking, and I suspect that no one ever does
42 * that.
43 */
44struct BITSFUNC(fake_sections)
45{
46 ELF(Shdr) *table;
47 unsigned long table_offset;
48 int count, max_count;
49
50 int in_shstrndx;
51 unsigned long shstr_offset;
52 const char *shstrtab;
53 size_t shstrtab_len;
54
55 int out_shstrndx;
56};
57
58static unsigned int BITSFUNC(find_shname)(struct BITSFUNC(fake_sections) *out,
59 const char *name)
60{
61 const char *outname = out->shstrtab;
62 while (outname - out->shstrtab < out->shstrtab_len) {
63 if (!strcmp(name, outname))
64 return (outname - out->shstrtab) + out->shstr_offset;
65 outname += strlen(outname) + 1;
66 }
67
68 if (*name)
69 printf("Warning: could not find output name \"%s\"\n", name);
70 return out->shstr_offset + out->shstrtab_len - 1; /* Use a null. */
71}
72
73static void BITSFUNC(init_sections)(struct BITSFUNC(fake_sections) *out)
74{
75 if (!out->in_shstrndx)
76 fail("didn't find the fake shstrndx\n");
77
78 memset(out->table, 0, out->max_count * sizeof(ELF(Shdr)));
79
80 if (out->max_count < 1)
81 fail("we need at least two fake output sections\n");
82
83 PUT_LE(&out->table[0].sh_type, SHT_NULL);
84 PUT_LE(&out->table[0].sh_name, BITSFUNC(find_shname)(out, ""));
85
86 out->count = 1;
87}
88
89static void BITSFUNC(copy_section)(struct BITSFUNC(fake_sections) *out,
90 int in_idx, const ELF(Shdr) *in,
91 const char *name)
92{
93 uint64_t flags = GET_LE(&in->sh_flags);
94
Andy Lutomirski0e3727a2014-06-18 15:59:49 -070095 bool copy = flags & SHF_ALLOC &&
Jan Beulich9f88b902014-07-03 15:34:38 +010096 (GET_LE(&in->sh_size) ||
97 (GET_LE(&in->sh_type) != SHT_RELA &&
98 GET_LE(&in->sh_type) != SHT_REL)) &&
Andy Lutomirski0e3727a2014-06-18 15:59:49 -070099 strcmp(name, ".altinstructions") &&
100 strcmp(name, ".altinstr_replacement");
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700101
102 if (!copy)
103 return;
104
105 if (out->count >= out->max_count)
106 fail("too many copied sections (max = %d)\n", out->max_count);
107
108 if (in_idx == out->in_shstrndx)
109 out->out_shstrndx = out->count;
110
111 out->table[out->count] = *in;
112 PUT_LE(&out->table[out->count].sh_name,
113 BITSFUNC(find_shname)(out, name));
114
115 /* elfutils requires that a strtab have the correct type. */
116 if (!strcmp(name, ".fake_shstrtab"))
117 PUT_LE(&out->table[out->count].sh_type, SHT_STRTAB);
118
119 out->count++;
120}
121
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700122static void BITSFUNC(go)(void *addr, size_t len,
123 FILE *outfile, const char *name)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700124{
125 int found_load = 0;
126 unsigned long load_size = -1; /* Work around bogus warning */
127 unsigned long data_size;
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700128 ELF(Ehdr) *hdr = (ELF(Ehdr) *)addr;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700129 int i;
130 unsigned long j;
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700131 ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700132 *alt_sec = NULL;
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700133 ELF(Dyn) *dyn = 0, *dyn_end = 0;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700134 const char *secstrings;
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700135 INT_BITS syms[NSYMS] = {};
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700136
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700137 struct BITSFUNC(fake_sections) fake_sections = {};
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700138
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700139 ELF(Phdr) *pt = (ELF(Phdr) *)(addr + GET_LE(&hdr->e_phoff));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700140
141 /* Walk the segment table. */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700142 for (i = 0; i < GET_LE(&hdr->e_phnum); i++) {
143 if (GET_LE(&pt[i].p_type) == PT_LOAD) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700144 if (found_load)
145 fail("multiple PT_LOAD segs\n");
146
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700147 if (GET_LE(&pt[i].p_offset) != 0 ||
148 GET_LE(&pt[i].p_vaddr) != 0)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700149 fail("PT_LOAD in wrong place\n");
150
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700151 if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz))
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700152 fail("cannot handle memsz != filesz\n");
153
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700154 load_size = GET_LE(&pt[i].p_memsz);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700155 found_load = 1;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700156 } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) {
157 dyn = addr + GET_LE(&pt[i].p_offset);
158 dyn_end = addr + GET_LE(&pt[i].p_offset) +
159 GET_LE(&pt[i].p_memsz);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700160 }
161 }
162 if (!found_load)
163 fail("no PT_LOAD seg\n");
164 data_size = (load_size + 4095) / 4096 * 4096;
165
166 /* Walk the dynamic table */
H. Peter Anvinc1919202014-05-30 17:03:22 -0700167 for (i = 0; dyn + i < dyn_end &&
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700168 GET_LE(&dyn[i].d_tag) != DT_NULL; i++) {
169 typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag);
Andy Lutomirski6a89d712014-06-24 13:46:53 -0700170 if (tag == DT_REL || tag == DT_RELSZ || tag == DT_RELA ||
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700171 tag == DT_RELENT || tag == DT_TEXTREL)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700172 fail("vdso image contains dynamic relocations\n");
173 }
174
175 /* Walk the section table */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700176 secstrings_hdr = addr + GET_LE(&hdr->e_shoff) +
177 GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx);
178 secstrings = addr + GET_LE(&secstrings_hdr->sh_offset);
179 for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700180 ELF(Shdr) *sh = addr + GET_LE(&hdr->e_shoff) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700181 GET_LE(&hdr->e_shentsize) * i;
182 if (GET_LE(&sh->sh_type) == SHT_SYMTAB)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700183 symtab_hdr = sh;
184
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700185 if (!strcmp(secstrings + GET_LE(&sh->sh_name),
H. Peter Anvinc1919202014-05-30 17:03:22 -0700186 ".altinstructions"))
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700187 alt_sec = sh;
188 }
189
Andy Lutomirski01156182014-05-30 08:48:48 -0700190 if (!symtab_hdr)
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700191 fail("no symbol table\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700192
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700193 strtab_hdr = addr + GET_LE(&hdr->e_shoff) +
194 GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700195
196 /* Walk the symbol table */
H. Peter Anvinc1919202014-05-30 17:03:22 -0700197 for (i = 0;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700198 i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -0700199 i++) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700200 int k;
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700201 ELF(Sym) *sym = addr + GET_LE(&symtab_hdr->sh_offset) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700202 GET_LE(&symtab_hdr->sh_entsize) * i;
203 const char *name = addr + GET_LE(&strtab_hdr->sh_offset) +
204 GET_LE(&sym->st_name);
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700205
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700206 for (k = 0; k < NSYMS; k++) {
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700207 if (!strcmp(name, required_syms[k].name)) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700208 if (syms[k]) {
209 fail("duplicate symbol %s\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700210 required_syms[k].name);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700211 }
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700212
213 /*
214 * Careful: we use negative addresses, but
215 * st_value is unsigned, so we rely
216 * on syms[k] being a signed type of the
217 * correct width.
218 */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700219 syms[k] = GET_LE(&sym->st_value);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700220 }
221 }
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700222
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700223 if (!strcmp(name, "fake_shstrtab")) {
224 ELF(Shdr) *sh;
225
226 fake_sections.in_shstrndx = GET_LE(&sym->st_shndx);
227 fake_sections.shstrtab = addr + GET_LE(&sym->st_value);
228 fake_sections.shstrtab_len = GET_LE(&sym->st_size);
229 sh = addr + GET_LE(&hdr->e_shoff) +
230 GET_LE(&hdr->e_shentsize) *
231 fake_sections.in_shstrndx;
232 fake_sections.shstr_offset = GET_LE(&sym->st_value) -
233 GET_LE(&sh->sh_addr);
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700234 }
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700235 }
236
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700237 /* Build the output section table. */
238 if (!syms[sym_VDSO_FAKE_SECTION_TABLE_START] ||
239 !syms[sym_VDSO_FAKE_SECTION_TABLE_END])
240 fail("couldn't find fake section table\n");
241 if ((syms[sym_VDSO_FAKE_SECTION_TABLE_END] -
242 syms[sym_VDSO_FAKE_SECTION_TABLE_START]) % sizeof(ELF(Shdr)))
243 fail("fake section table size isn't a multiple of sizeof(Shdr)\n");
244 fake_sections.table = addr + syms[sym_VDSO_FAKE_SECTION_TABLE_START];
245 fake_sections.table_offset = syms[sym_VDSO_FAKE_SECTION_TABLE_START];
246 fake_sections.max_count = (syms[sym_VDSO_FAKE_SECTION_TABLE_END] -
247 syms[sym_VDSO_FAKE_SECTION_TABLE_START]) /
248 sizeof(ELF(Shdr));
249
250 BITSFUNC(init_sections)(&fake_sections);
251 for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
252 ELF(Shdr) *sh = addr + GET_LE(&hdr->e_shoff) +
253 GET_LE(&hdr->e_shentsize) * i;
254 BITSFUNC(copy_section)(&fake_sections, i, sh,
255 secstrings + GET_LE(&sh->sh_name));
256 }
257 if (!fake_sections.out_shstrndx)
258 fail("didn't generate shstrndx?!?\n");
259
260 PUT_LE(&hdr->e_shoff, fake_sections.table_offset);
261 PUT_LE(&hdr->e_shentsize, sizeof(ELF(Shdr)));
262 PUT_LE(&hdr->e_shnum, fake_sections.count);
263 PUT_LE(&hdr->e_shstrndx, fake_sections.out_shstrndx);
264
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700265 /* Validate mapping addresses. */
266 for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
267 if (!syms[i])
268 continue; /* The mapping isn't used; ignore it. */
269
270 if (syms[i] % 4096)
271 fail("%s must be a multiple of 4096\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700272 required_syms[i].name);
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700273 if (syms[sym_vvar_start] > syms[i] + 4096)
274 fail("%s underruns begin_vvar\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700275 required_syms[i].name);
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700276 if (syms[i] + 4096 > 0)
277 fail("%s is on the wrong side of the vdso text\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700278 required_syms[i].name);
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700279 }
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700280 if (syms[sym_vvar_start] % 4096)
281 fail("vvar_begin must be a multiple of 4096\n");
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700282
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700283 if (!name) {
284 fwrite(addr, load_size, 1, outfile);
Andy Lutomirski01156182014-05-30 08:48:48 -0700285 return;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700286 }
287
288 fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
289 fprintf(outfile, "#include <linux/linkage.h>\n");
290 fprintf(outfile, "#include <asm/page_types.h>\n");
291 fprintf(outfile, "#include <asm/vdso.h>\n");
292 fprintf(outfile, "\n");
293 fprintf(outfile,
294 "static unsigned char raw_data[%lu] __page_aligned_data = {",
295 data_size);
296 for (j = 0; j < load_size; j++) {
297 if (j % 10 == 0)
298 fprintf(outfile, "\n\t");
299 fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]);
300 }
301 fprintf(outfile, "\n};\n\n");
302
303 fprintf(outfile, "static struct page *pages[%lu];\n\n",
304 data_size / 4096);
305
306 fprintf(outfile, "const struct vdso_image %s = {\n", name);
307 fprintf(outfile, "\t.data = raw_data,\n");
308 fprintf(outfile, "\t.size = %lu,\n", data_size);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700309 fprintf(outfile, "\t.text_mapping = {\n");
310 fprintf(outfile, "\t\t.name = \"[vdso]\",\n");
311 fprintf(outfile, "\t\t.pages = pages,\n");
312 fprintf(outfile, "\t},\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700313 if (alt_sec) {
314 fprintf(outfile, "\t.alt = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700315 (unsigned long)GET_LE(&alt_sec->sh_offset));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700316 fprintf(outfile, "\t.alt_len = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700317 (unsigned long)GET_LE(&alt_sec->sh_size));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700318 }
319 for (i = 0; i < NSYMS; i++) {
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700320 if (required_syms[i].export && syms[i])
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700321 fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
322 required_syms[i].name, (int64_t)syms[i]);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700323 }
324 fprintf(outfile, "};\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700325}