blob: c6eefaf389b95e6a18bef6a58943afb4c6d597bf [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 Lutomirski01156182014-05-30 08:48:48 -07007static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
Andy Lutomirski6f121e52014-05-05 12:19:34 -07008{
9 int found_load = 0;
10 unsigned long load_size = -1; /* Work around bogus warning */
11 unsigned long data_size;
12 Elf_Ehdr *hdr = (Elf_Ehdr *)addr;
13 int i;
14 unsigned long j;
15 Elf_Shdr *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
16 *alt_sec = NULL;
17 Elf_Dyn *dyn = 0, *dyn_end = 0;
18 const char *secstrings;
19 uint64_t syms[NSYMS] = {};
20
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -070021 uint64_t fake_sections_value = 0, fake_sections_size = 0;
22
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070023 Elf_Phdr *pt = (Elf_Phdr *)(addr + GET_LE(&hdr->e_phoff));
Andy Lutomirski6f121e52014-05-05 12:19:34 -070024
25 /* Walk the segment table. */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070026 for (i = 0; i < GET_LE(&hdr->e_phnum); i++) {
27 if (GET_LE(&pt[i].p_type) == PT_LOAD) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -070028 if (found_load)
29 fail("multiple PT_LOAD segs\n");
30
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070031 if (GET_LE(&pt[i].p_offset) != 0 ||
32 GET_LE(&pt[i].p_vaddr) != 0)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070033 fail("PT_LOAD in wrong place\n");
34
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070035 if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz))
Andy Lutomirski6f121e52014-05-05 12:19:34 -070036 fail("cannot handle memsz != filesz\n");
37
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070038 load_size = GET_LE(&pt[i].p_memsz);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070039 found_load = 1;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070040 } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) {
41 dyn = addr + GET_LE(&pt[i].p_offset);
42 dyn_end = addr + GET_LE(&pt[i].p_offset) +
43 GET_LE(&pt[i].p_memsz);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070044 }
45 }
46 if (!found_load)
47 fail("no PT_LOAD seg\n");
48 data_size = (load_size + 4095) / 4096 * 4096;
49
50 /* Walk the dynamic table */
H. Peter Anvinc1919202014-05-30 17:03:22 -070051 for (i = 0; dyn + i < dyn_end &&
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070052 GET_LE(&dyn[i].d_tag) != DT_NULL; i++) {
53 typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag);
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -070054 if (tag == DT_REL || tag == DT_RELSZ ||
55 tag == DT_RELENT || tag == DT_TEXTREL)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070056 fail("vdso image contains dynamic relocations\n");
57 }
58
59 /* Walk the section table */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070060 secstrings_hdr = addr + GET_LE(&hdr->e_shoff) +
61 GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx);
62 secstrings = addr + GET_LE(&secstrings_hdr->sh_offset);
63 for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
64 Elf_Shdr *sh = addr + GET_LE(&hdr->e_shoff) +
65 GET_LE(&hdr->e_shentsize) * i;
66 if (GET_LE(&sh->sh_type) == SHT_SYMTAB)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070067 symtab_hdr = sh;
68
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070069 if (!strcmp(secstrings + GET_LE(&sh->sh_name),
H. Peter Anvinc1919202014-05-30 17:03:22 -070070 ".altinstructions"))
Andy Lutomirski6f121e52014-05-05 12:19:34 -070071 alt_sec = sh;
72 }
73
Andy Lutomirski01156182014-05-30 08:48:48 -070074 if (!symtab_hdr)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070075 fail("no symbol table\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -070076
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070077 strtab_hdr = addr + GET_LE(&hdr->e_shoff) +
78 GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070079
80 /* Walk the symbol table */
H. Peter Anvinc1919202014-05-30 17:03:22 -070081 for (i = 0;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070082 i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -070083 i++) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -070084 int k;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070085 Elf_Sym *sym = addr + GET_LE(&symtab_hdr->sh_offset) +
86 GET_LE(&symtab_hdr->sh_entsize) * i;
87 const char *name = addr + GET_LE(&strtab_hdr->sh_offset) +
88 GET_LE(&sym->st_name);
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -070089
Andy Lutomirski6f121e52014-05-05 12:19:34 -070090 for (k = 0; k < NSYMS; k++) {
91 if (!strcmp(name, required_syms[k])) {
92 if (syms[k]) {
93 fail("duplicate symbol %s\n",
94 required_syms[k]);
95 }
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070096 syms[k] = GET_LE(&sym->st_value);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070097 }
98 }
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -070099
100 if (!strcmp(name, "vdso_fake_sections")) {
101 if (fake_sections_value)
102 fail("duplicate vdso_fake_sections\n");
103 fake_sections_value = GET_LE(&sym->st_value);
104 fake_sections_size = GET_LE(&sym->st_size);
105 }
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700106 }
107
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700108 /* Validate mapping addresses. */
109 for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
110 if (!syms[i])
111 continue; /* The mapping isn't used; ignore it. */
112
113 if (syms[i] % 4096)
114 fail("%s must be a multiple of 4096\n",
115 required_syms[i]);
116 if (syms[i] < data_size)
117 fail("%s must be after the text mapping\n",
118 required_syms[i]);
119 if (syms[sym_end_mapping] < syms[i] + 4096)
120 fail("%s overruns end_mapping\n", required_syms[i]);
121 }
122 if (syms[sym_end_mapping] % 4096)
123 fail("end_mapping must be a multiple of 4096\n");
124
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700125 /* Remove sections or use fakes */
126 if (fake_sections_size % sizeof(Elf_Shdr))
127 fail("vdso_fake_sections size is not a multiple of %ld\n",
128 (long)sizeof(Elf_Shdr));
129 PUT_LE(&hdr->e_shoff, fake_sections_value);
130 PUT_LE(&hdr->e_shentsize, fake_sections_value ? sizeof(Elf_Shdr) : 0);
131 PUT_LE(&hdr->e_shnum, fake_sections_size / sizeof(Elf_Shdr));
132 PUT_LE(&hdr->e_shstrndx, SHN_UNDEF);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700133
134 if (!name) {
135 fwrite(addr, load_size, 1, outfile);
Andy Lutomirski01156182014-05-30 08:48:48 -0700136 return;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700137 }
138
139 fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
140 fprintf(outfile, "#include <linux/linkage.h>\n");
141 fprintf(outfile, "#include <asm/page_types.h>\n");
142 fprintf(outfile, "#include <asm/vdso.h>\n");
143 fprintf(outfile, "\n");
144 fprintf(outfile,
145 "static unsigned char raw_data[%lu] __page_aligned_data = {",
146 data_size);
147 for (j = 0; j < load_size; j++) {
148 if (j % 10 == 0)
149 fprintf(outfile, "\n\t");
150 fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]);
151 }
152 fprintf(outfile, "\n};\n\n");
153
154 fprintf(outfile, "static struct page *pages[%lu];\n\n",
155 data_size / 4096);
156
157 fprintf(outfile, "const struct vdso_image %s = {\n", name);
158 fprintf(outfile, "\t.data = raw_data,\n");
159 fprintf(outfile, "\t.size = %lu,\n", data_size);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700160 fprintf(outfile, "\t.text_mapping = {\n");
161 fprintf(outfile, "\t\t.name = \"[vdso]\",\n");
162 fprintf(outfile, "\t\t.pages = pages,\n");
163 fprintf(outfile, "\t},\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700164 if (alt_sec) {
165 fprintf(outfile, "\t.alt = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700166 (unsigned long)GET_LE(&alt_sec->sh_offset));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700167 fprintf(outfile, "\t.alt_len = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700168 (unsigned long)GET_LE(&alt_sec->sh_size));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700169 }
170 for (i = 0; i < NSYMS; i++) {
171 if (syms[i])
172 fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n",
173 required_syms[i], syms[i]);
174 }
175 fprintf(outfile, "};\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700176}