blob: 3f69326ed545719aedd9e11b1be0fdea104136bc [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 Lutomirskida861e12014-07-10 18:13:16 -07007static void BITSFUNC(go)(void *raw_addr, size_t raw_len,
8 void *stripped_addr, size_t stripped_len,
Andy Lutomirskic1979c32014-06-18 15:59:47 -07009 FILE *outfile, const char *name)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070010{
11 int found_load = 0;
12 unsigned long load_size = -1; /* Work around bogus warning */
Andy Lutomirskida861e12014-07-10 18:13:16 -070013 unsigned long mapping_size;
14 ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr;
Andy Lutomirski6f121e52014-05-05 12:19:34 -070015 int i;
16 unsigned long j;
Andy Lutomirskic1979c32014-06-18 15:59:47 -070017 ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
Andy Lutomirski6f121e52014-05-05 12:19:34 -070018 *alt_sec = NULL;
Andy Lutomirskic1979c32014-06-18 15:59:47 -070019 ELF(Dyn) *dyn = 0, *dyn_end = 0;
Andy Lutomirski6f121e52014-05-05 12:19:34 -070020 const char *secstrings;
Andy Lutomirskie6577a72014-07-10 18:13:15 -070021 INT_BITS syms[NSYMS] = {};
Andy Lutomirski6f121e52014-05-05 12:19:34 -070022
Andy Lutomirskida861e12014-07-10 18:13:16 -070023 ELF(Phdr) *pt = (ELF(Phdr) *)(raw_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) {
Andy Lutomirskida861e12014-07-10 18:13:16 -070041 dyn = raw_addr + GET_LE(&pt[i].p_offset);
42 dyn_end = raw_addr + GET_LE(&pt[i].p_offset) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070043 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");
Andy Lutomirskida861e12014-07-10 18:13:16 -070048
49 if (stripped_len < load_size)
50 fail("stripped input is too short\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -070051
52 /* Walk the dynamic table */
H. Peter Anvinc1919202014-05-30 17:03:22 -070053 for (i = 0; dyn + i < dyn_end &&
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070054 GET_LE(&dyn[i].d_tag) != DT_NULL; i++) {
55 typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag);
Andy Lutomirski6a89d712014-06-24 13:46:53 -070056 if (tag == DT_REL || tag == DT_RELSZ || tag == DT_RELA ||
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -070057 tag == DT_RELENT || tag == DT_TEXTREL)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070058 fail("vdso image contains dynamic relocations\n");
59 }
60
61 /* Walk the section table */
Andy Lutomirskida861e12014-07-10 18:13:16 -070062 secstrings_hdr = raw_addr + GET_LE(&hdr->e_shoff) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070063 GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx);
Andy Lutomirskida861e12014-07-10 18:13:16 -070064 secstrings = raw_addr + GET_LE(&secstrings_hdr->sh_offset);
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070065 for (i = 0; i < GET_LE(&hdr->e_shnum); i++) {
Andy Lutomirskida861e12014-07-10 18:13:16 -070066 ELF(Shdr) *sh = raw_addr + GET_LE(&hdr->e_shoff) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070067 GET_LE(&hdr->e_shentsize) * i;
68 if (GET_LE(&sh->sh_type) == SHT_SYMTAB)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070069 symtab_hdr = sh;
70
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070071 if (!strcmp(secstrings + GET_LE(&sh->sh_name),
H. Peter Anvinc1919202014-05-30 17:03:22 -070072 ".altinstructions"))
Andy Lutomirski6f121e52014-05-05 12:19:34 -070073 alt_sec = sh;
74 }
75
Andy Lutomirski01156182014-05-30 08:48:48 -070076 if (!symtab_hdr)
Andy Lutomirski6f121e52014-05-05 12:19:34 -070077 fail("no symbol table\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -070078
Andy Lutomirskida861e12014-07-10 18:13:16 -070079 strtab_hdr = raw_addr + GET_LE(&hdr->e_shoff) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070080 GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070081
82 /* Walk the symbol table */
H. Peter Anvinc1919202014-05-30 17:03:22 -070083 for (i = 0;
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070084 i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
Andy Lutomirskiadd4eed2014-05-30 08:48:49 -070085 i++) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -070086 int k;
Andy Lutomirskida861e12014-07-10 18:13:16 -070087 ELF(Sym) *sym = raw_addr + GET_LE(&symtab_hdr->sh_offset) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070088 GET_LE(&symtab_hdr->sh_entsize) * i;
Andy Lutomirskida861e12014-07-10 18:13:16 -070089 const char *name = raw_addr + GET_LE(&strtab_hdr->sh_offset) +
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -070090 GET_LE(&sym->st_name);
Andy Lutomirskie0bf7b82014-06-12 17:53:12 -070091
Andy Lutomirski6f121e52014-05-05 12:19:34 -070092 for (k = 0; k < NSYMS; k++) {
Andy Lutomirskibfad3812014-06-18 15:59:48 -070093 if (!strcmp(name, required_syms[k].name)) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -070094 if (syms[k]) {
95 fail("duplicate symbol %s\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -070096 required_syms[k].name);
Andy Lutomirski6f121e52014-05-05 12:19:34 -070097 }
Andy Lutomirskie6577a72014-07-10 18:13:15 -070098
99 /*
100 * Careful: we use negative addresses, but
101 * st_value is unsigned, so we rely
102 * on syms[k] being a signed type of the
103 * correct width.
104 */
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700105 syms[k] = GET_LE(&sym->st_value);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700106 }
107 }
108 }
109
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700110 /* Validate mapping addresses. */
111 for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
Andy Lutomirskif12c1f92014-07-30 14:59:49 -0700112 INT_BITS symval = syms[special_pages[i]];
113
114 if (!symval)
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700115 continue; /* The mapping isn't used; ignore it. */
116
Andy Lutomirskif12c1f92014-07-30 14:59:49 -0700117 if (symval % 4096)
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700118 fail("%s must be a multiple of 4096\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700119 required_syms[i].name);
Andy Lutomirskif12c1f92014-07-30 14:59:49 -0700120 if (symval + 4096 < syms[sym_vvar_start])
121 fail("%s underruns vvar_start\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700122 required_syms[i].name);
Andy Lutomirskif12c1f92014-07-30 14:59:49 -0700123 if (symval + 4096 > 0)
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700124 fail("%s is on the wrong side of the vdso text\n",
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700125 required_syms[i].name);
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700126 }
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700127 if (syms[sym_vvar_start] % 4096)
128 fail("vvar_begin must be a multiple of 4096\n");
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700129
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700130 if (!name) {
Andy Lutomirskida861e12014-07-10 18:13:16 -0700131 fwrite(stripped_addr, stripped_len, 1, outfile);
Andy Lutomirski01156182014-05-30 08:48:48 -0700132 return;
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700133 }
134
Andy Lutomirskida861e12014-07-10 18:13:16 -0700135 mapping_size = (stripped_len + 4095) / 4096 * 4096;
136
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700137 fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
138 fprintf(outfile, "#include <linux/linkage.h>\n");
139 fprintf(outfile, "#include <asm/page_types.h>\n");
140 fprintf(outfile, "#include <asm/vdso.h>\n");
141 fprintf(outfile, "\n");
142 fprintf(outfile,
Kees Cook018ef8d2016-02-17 14:41:17 -0800143 "static unsigned char raw_data[%lu] __ro_after_init __aligned(PAGE_SIZE) = {",
Andy Lutomirskida861e12014-07-10 18:13:16 -0700144 mapping_size);
145 for (j = 0; j < stripped_len; j++) {
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700146 if (j % 10 == 0)
147 fprintf(outfile, "\n\t");
Andy Lutomirskida861e12014-07-10 18:13:16 -0700148 fprintf(outfile, "0x%02X, ",
149 (int)((unsigned char *)stripped_addr)[j]);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700150 }
151 fprintf(outfile, "\n};\n\n");
152
153 fprintf(outfile, "static struct page *pages[%lu];\n\n",
Andy Lutomirskida861e12014-07-10 18:13:16 -0700154 mapping_size / 4096);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700155
156 fprintf(outfile, "const struct vdso_image %s = {\n", name);
157 fprintf(outfile, "\t.data = raw_data,\n");
Andy Lutomirskida861e12014-07-10 18:13:16 -0700158 fprintf(outfile, "\t.size = %lu,\n", mapping_size);
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700159 fprintf(outfile, "\t.text_mapping = {\n");
160 fprintf(outfile, "\t\t.name = \"[vdso]\",\n");
161 fprintf(outfile, "\t\t.pages = pages,\n");
162 fprintf(outfile, "\t},\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700163 if (alt_sec) {
164 fprintf(outfile, "\t.alt = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700165 (unsigned long)GET_LE(&alt_sec->sh_offset));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700166 fprintf(outfile, "\t.alt_len = %lu,\n",
H. Peter Anvinbdfb9bc2014-06-06 14:30:37 -0700167 (unsigned long)GET_LE(&alt_sec->sh_size));
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700168 }
169 for (i = 0; i < NSYMS; i++) {
Andy Lutomirskibfad3812014-06-18 15:59:48 -0700170 if (required_syms[i].export && syms[i])
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700171 fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
172 required_syms[i].name, (int64_t)syms[i]);
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700173 }
174 fprintf(outfile, "};\n");
Andy Lutomirski6f121e52014-05-05 12:19:34 -0700175}