blob: 8e185ce39e69cd9309923347f2aa0bd56c2e0eca [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 Lutomirskic1979c32014-06-18 15:59:47 -07007static void BITSFUNC(go)(void *addr, size_t len,
8 FILE *outfile, const char *name)
Andy Lutomirski6f121e52014-05-05 12:19:34 -07009{
10 int found_load = 0;
11 unsigned long load_size = -1; /* Work around bogus warning */
12 unsigned long data_size;
Andy Lutomirskic1979c32014-06-18 15:59:47 -070013 ELF(Ehdr) *hdr = (ELF(Ehdr) *)addr;
Andy Lutomirski6f121e52014-05-05 12:19:34 -070014 int i;
15 unsigned long j;
Andy Lutomirskic1979c32014-06-18 15:59:47 -070016 ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
Andy Lutomirski6f121e52014-05-05 12:19:34 -070017 *alt_sec = NULL;
Andy Lutomirskic1979c32014-06-18 15:59:47 -070018 ELF(Dyn) *dyn = 0, *dyn_end = 0;
Andy Lutomirski6f121e52014-05-05 12:19:34 -070019 const char *secstrings;
20 uint64_t syms[NSYMS] = {};
21
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -070022 uint64_t fake_sections_value = 0, fake_sections_size = 0;
23
Andy Lutomirskic1979c32014-06-18 15:59:47 -070024 ELF(Phdr) *pt = (ELF(Phdr) *)(addr + GET_LE(&hdr->e_phoff));
Andy Lutomirski6f121e52014-05-05 12:19:34 -070025
26 /* Walk the segment table. */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070027 for (i = 0; i < GET_LE(&hdr->e_phnum); i++) {
28 if (GET_LE(&pt[i].p_type) == PT_LOAD) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -070029 if (found_load)
30 fail("multiple PT_LOAD segs\n");
31
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070032 if (GET_LE(&pt[i].p_offset) != 0 ||
33 GET_LE(&pt[i].p_vaddr) != 0)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070034 fail("PT_LOAD in wrong place\n");
35
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070036 if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz))
Andy Lutomirski6f121e52014-05-05 12:19:34 -070037 fail("cannot handle memsz != filesz\n");
38
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070039 load_size = GET_LE(&pt[i].p_memsz);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070040 found_load = 1;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070041 } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) {
42 dyn = addr + GET_LE(&pt[i].p_offset);
43 dyn_end = addr + GET_LE(&pt[i].p_offset) +
44 GET_LE(&pt[i].p_memsz);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070045 }
46 }
47 if (!found_load)
48 fail("no PT_LOAD seg\n");
49 data_size = (load_size + 4095) / 4096 * 4096;
50
51 /* Walk the dynamic table */
H. Peter Anvinc1919202014-05-30 17:03:22 -070052 for (i = 0; dyn + i < dyn_end &&
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070053 GET_LE(&dyn[i].d_tag) != DT_NULL; i++) {
54 typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag);
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -070055 if (tag == DT_REL || tag == DT_RELSZ ||
56 tag == DT_RELENT || tag == DT_TEXTREL)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070057 fail("vdso image contains dynamic relocations\n");
58 }
59
60 /* Walk the section table */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070061 secstrings_hdr = addr + GET_LE(&hdr->e_shoff) +
62 GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx);
63 secstrings = addr + GET_LE(&secstrings_hdr->sh_offset);
64 for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
Andy Lutomirskic1979c32014-06-18 15:59:47 -070065 ELF(Shdr) *sh = addr + GET_LE(&hdr->e_shoff) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070066 GET_LE(&hdr->e_shentsize) * i;
67 if (GET_LE(&sh->sh_type) == SHT_SYMTAB)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070068 symtab_hdr = sh;
69
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070070 if (!strcmp(secstrings + GET_LE(&sh->sh_name),
H. Peter Anvinc1919202014-05-30 17:03:22 -070071 ".altinstructions"))
Andy Lutomirski6f121e52014-05-05 12:19:34 -070072 alt_sec = sh;
73 }
74
Andy Lutomirski01156182014-05-30 08:48:48 -070075 if (!symtab_hdr)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070076 fail("no symbol table\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -070077
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070078 strtab_hdr = addr + GET_LE(&hdr->e_shoff) +
79 GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070080
81 /* Walk the symbol table */
H. Peter Anvinc1919202014-05-30 17:03:22 -070082 for (i = 0;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070083 i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -070084 i++) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -070085 int k;
Andy Lutomirskic1979c32014-06-18 15:59:47 -070086 ELF(Sym) *sym = addr + GET_LE(&symtab_hdr->sh_offset) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070087 GET_LE(&symtab_hdr->sh_entsize) * i;
88 const char *name = addr + GET_LE(&strtab_hdr->sh_offset) +
89 GET_LE(&sym->st_name);
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -070090
Andy Lutomirski6f121e52014-05-05 12:19:34 -070091 for (k = 0; k < NSYMS; k++) {
92 if (!strcmp(name, required_syms[k])) {
93 if (syms[k]) {
94 fail("duplicate symbol %s\n",
95 required_syms[k]);
96 }
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070097 syms[k] = GET_LE(&sym->st_value);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070098 }
99 }
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700100
101 if (!strcmp(name, "vdso_fake_sections")) {
102 if (fake_sections_value)
103 fail("duplicate vdso_fake_sections\n");
104 fake_sections_value = GET_LE(&sym->st_value);
105 fake_sections_size = GET_LE(&sym->st_size);
106 }
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700107 }
108
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700109 /* Validate mapping addresses. */
110 for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
111 if (!syms[i])
112 continue; /* The mapping isn't used; ignore it. */
113
114 if (syms[i] % 4096)
115 fail("%s must be a multiple of 4096\n",
116 required_syms[i]);
117 if (syms[i] < data_size)
118 fail("%s must be after the text mapping\n",
119 required_syms[i]);
120 if (syms[sym_end_mapping] < syms[i] + 4096)
121 fail("%s overruns end_mapping\n", required_syms[i]);
122 }
123 if (syms[sym_end_mapping] % 4096)
124 fail("end_mapping must be a multiple of 4096\n");
125
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700126 /* Remove sections or use fakes */
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700127 if (fake_sections_size % sizeof(ELF(Shdr)))
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700128 fail("vdso_fake_sections size is not a multiple of %ld\n",
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700129 (long)sizeof(ELF(Shdr)));
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700130 PUT_LE(&hdr->e_shoff, fake_sections_value);
Andy Lutomirskic1979c32014-06-18 15:59:47 -0700131 PUT_LE(&hdr->e_shentsize, fake_sections_value ? sizeof(ELF(Shdr)) : 0);
132 PUT_LE(&hdr->e_shnum, fake_sections_size / sizeof(ELF(Shdr)));
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -0700133 PUT_LE(&hdr->e_shstrndx, SHN_UNDEF);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700134
135 if (!name) {
136 fwrite(addr, load_size, 1, outfile);
Andy Lutomirski01156182014-05-30 08:48:48 -0700137 return;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700138 }
139
140 fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
141 fprintf(outfile, "#include <linux/linkage.h>\n");
142 fprintf(outfile, "#include <asm/page_types.h>\n");
143 fprintf(outfile, "#include <asm/vdso.h>\n");
144 fprintf(outfile, "\n");
145 fprintf(outfile,
146 "static unsigned char raw_data[%lu] __page_aligned_data = {",
147 data_size);
148 for (j = 0; j < load_size; j++) {
149 if (j % 10 == 0)
150 fprintf(outfile, "\n\t");
151 fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]);
152 }
153 fprintf(outfile, "\n};\n\n");
154
155 fprintf(outfile, "static struct page *pages[%lu];\n\n",
156 data_size / 4096);
157
158 fprintf(outfile, "const struct vdso_image %s = {\n", name);
159 fprintf(outfile, "\t.data = raw_data,\n");
160 fprintf(outfile, "\t.size = %lu,\n", data_size);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700161 fprintf(outfile, "\t.text_mapping = {\n");
162 fprintf(outfile, "\t\t.name = \"[vdso]\",\n");
163 fprintf(outfile, "\t\t.pages = pages,\n");
164 fprintf(outfile, "\t},\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700165 if (alt_sec) {
166 fprintf(outfile, "\t.alt = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700167 (unsigned long)GET_LE(&alt_sec->sh_offset));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700168 fprintf(outfile, "\t.alt_len = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700169 (unsigned long)GET_LE(&alt_sec->sh_size));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700170 }
171 for (i = 0; i < NSYMS; i++) {
172 if (syms[i])
173 fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n",
174 required_syms[i], syms[i]);
175 }
176 fprintf(outfile, "};\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700177}