Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 1 | /* |
| 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 Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 7 | static void BITSFUNC(go)(void *raw_addr, size_t raw_len, |
| 8 | void *stripped_addr, size_t stripped_len, |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame] | 9 | FILE *outfile, const char *name) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 10 | { |
| 11 | int found_load = 0; |
| 12 | unsigned long load_size = -1; /* Work around bogus warning */ |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 13 | unsigned long mapping_size; |
| 14 | ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 15 | int i; |
| 16 | unsigned long j; |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame] | 17 | ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr, |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 18 | *alt_sec = NULL; |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame] | 19 | ELF(Dyn) *dyn = 0, *dyn_end = 0; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 20 | const char *secstrings; |
Andy Lutomirski | e6577a7 | 2014-07-10 18:13:15 -0700 | [diff] [blame] | 21 | INT_BITS syms[NSYMS] = {}; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 22 | |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 23 | ELF(Phdr) *pt = (ELF(Phdr) *)(raw_addr + GET_LE(&hdr->e_phoff)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 24 | |
| 25 | /* Walk the segment table. */ |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 26 | for (i = 0; i < GET_LE(&hdr->e_phnum); i++) { |
| 27 | if (GET_LE(&pt[i].p_type) == PT_LOAD) { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 28 | if (found_load) |
| 29 | fail("multiple PT_LOAD segs\n"); |
| 30 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 31 | if (GET_LE(&pt[i].p_offset) != 0 || |
| 32 | GET_LE(&pt[i].p_vaddr) != 0) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 33 | fail("PT_LOAD in wrong place\n"); |
| 34 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 35 | if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz)) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 36 | fail("cannot handle memsz != filesz\n"); |
| 37 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 38 | load_size = GET_LE(&pt[i].p_memsz); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 39 | found_load = 1; |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 40 | } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) { |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 41 | dyn = raw_addr + GET_LE(&pt[i].p_offset); |
| 42 | dyn_end = raw_addr + GET_LE(&pt[i].p_offset) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 43 | GET_LE(&pt[i].p_memsz); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | if (!found_load) |
| 47 | fail("no PT_LOAD seg\n"); |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 48 | |
| 49 | if (stripped_len < load_size) |
| 50 | fail("stripped input is too short\n"); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 51 | |
| 52 | /* Walk the dynamic table */ |
H. Peter Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 53 | for (i = 0; dyn + i < dyn_end && |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 54 | GET_LE(&dyn[i].d_tag) != DT_NULL; i++) { |
| 55 | typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag); |
Andy Lutomirski | 6a89d71 | 2014-06-24 13:46:53 -0700 | [diff] [blame] | 56 | if (tag == DT_REL || tag == DT_RELSZ || tag == DT_RELA || |
Andy Lutomirski | add4eed | 2014-05-30 08:48:49 -0700 | [diff] [blame] | 57 | tag == DT_RELENT || tag == DT_TEXTREL) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 58 | fail("vdso image contains dynamic relocations\n"); |
| 59 | } |
| 60 | |
| 61 | /* Walk the section table */ |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 62 | secstrings_hdr = raw_addr + GET_LE(&hdr->e_shoff) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 63 | GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx); |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 64 | secstrings = raw_addr + GET_LE(&secstrings_hdr->sh_offset); |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 65 | for (i = 0; i < GET_LE(&hdr->e_shnum); i++) { |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 66 | ELF(Shdr) *sh = raw_addr + GET_LE(&hdr->e_shoff) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 67 | GET_LE(&hdr->e_shentsize) * i; |
| 68 | if (GET_LE(&sh->sh_type) == SHT_SYMTAB) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 69 | symtab_hdr = sh; |
| 70 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 71 | if (!strcmp(secstrings + GET_LE(&sh->sh_name), |
H. Peter Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 72 | ".altinstructions")) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 73 | alt_sec = sh; |
| 74 | } |
| 75 | |
Andy Lutomirski | 0115618 | 2014-05-30 08:48:48 -0700 | [diff] [blame] | 76 | if (!symtab_hdr) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 77 | fail("no symbol table\n"); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 78 | |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 79 | strtab_hdr = raw_addr + GET_LE(&hdr->e_shoff) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 80 | GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 81 | |
| 82 | /* Walk the symbol table */ |
H. Peter Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 83 | for (i = 0; |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 84 | i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize); |
Andy Lutomirski | add4eed | 2014-05-30 08:48:49 -0700 | [diff] [blame] | 85 | i++) { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 86 | int k; |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 87 | ELF(Sym) *sym = raw_addr + GET_LE(&symtab_hdr->sh_offset) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 88 | GET_LE(&symtab_hdr->sh_entsize) * i; |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 89 | const char *name = raw_addr + GET_LE(&strtab_hdr->sh_offset) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 90 | GET_LE(&sym->st_name); |
Andy Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 91 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 92 | for (k = 0; k < NSYMS; k++) { |
Andy Lutomirski | bfad381 | 2014-06-18 15:59:48 -0700 | [diff] [blame] | 93 | if (!strcmp(name, required_syms[k].name)) { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 94 | if (syms[k]) { |
| 95 | fail("duplicate symbol %s\n", |
Andy Lutomirski | bfad381 | 2014-06-18 15:59:48 -0700 | [diff] [blame] | 96 | required_syms[k].name); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 97 | } |
Andy Lutomirski | e6577a7 | 2014-07-10 18:13:15 -0700 | [diff] [blame] | 98 | |
| 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 Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 105 | syms[k] = GET_LE(&sym->st_value); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 110 | /* Validate mapping addresses. */ |
| 111 | for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) { |
Andy Lutomirski | f12c1f9 | 2014-07-30 14:59:49 -0700 | [diff] [blame] | 112 | INT_BITS symval = syms[special_pages[i]]; |
| 113 | |
| 114 | if (!symval) |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 115 | continue; /* The mapping isn't used; ignore it. */ |
| 116 | |
Andy Lutomirski | f12c1f9 | 2014-07-30 14:59:49 -0700 | [diff] [blame] | 117 | if (symval % 4096) |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 118 | fail("%s must be a multiple of 4096\n", |
Andy Lutomirski | bfad381 | 2014-06-18 15:59:48 -0700 | [diff] [blame] | 119 | required_syms[i].name); |
Andy Lutomirski | f12c1f9 | 2014-07-30 14:59:49 -0700 | [diff] [blame] | 120 | if (symval + 4096 < syms[sym_vvar_start]) |
| 121 | fail("%s underruns vvar_start\n", |
Andy Lutomirski | bfad381 | 2014-06-18 15:59:48 -0700 | [diff] [blame] | 122 | required_syms[i].name); |
Andy Lutomirski | f12c1f9 | 2014-07-30 14:59:49 -0700 | [diff] [blame] | 123 | if (symval + 4096 > 0) |
Andy Lutomirski | e6577a7 | 2014-07-10 18:13:15 -0700 | [diff] [blame] | 124 | fail("%s is on the wrong side of the vdso text\n", |
Andy Lutomirski | bfad381 | 2014-06-18 15:59:48 -0700 | [diff] [blame] | 125 | required_syms[i].name); |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 126 | } |
Andy Lutomirski | e6577a7 | 2014-07-10 18:13:15 -0700 | [diff] [blame] | 127 | if (syms[sym_vvar_start] % 4096) |
| 128 | fail("vvar_begin must be a multiple of 4096\n"); |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 129 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 130 | if (!name) { |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 131 | fwrite(stripped_addr, stripped_len, 1, outfile); |
Andy Lutomirski | 0115618 | 2014-05-30 08:48:48 -0700 | [diff] [blame] | 132 | return; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 135 | mapping_size = (stripped_len + 4095) / 4096 * 4096; |
| 136 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 137 | 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 Cook | 018ef8d | 2016-02-17 14:41:17 -0800 | [diff] [blame] | 143 | "static unsigned char raw_data[%lu] __ro_after_init __aligned(PAGE_SIZE) = {", |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 144 | mapping_size); |
| 145 | for (j = 0; j < stripped_len; j++) { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 146 | if (j % 10 == 0) |
| 147 | fprintf(outfile, "\n\t"); |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 148 | fprintf(outfile, "0x%02X, ", |
| 149 | (int)((unsigned char *)stripped_addr)[j]); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 150 | } |
| 151 | fprintf(outfile, "\n};\n\n"); |
| 152 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 153 | fprintf(outfile, "const struct vdso_image %s = {\n", name); |
| 154 | fprintf(outfile, "\t.data = raw_data,\n"); |
Andy Lutomirski | da861e1 | 2014-07-10 18:13:16 -0700 | [diff] [blame] | 155 | fprintf(outfile, "\t.size = %lu,\n", mapping_size); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 156 | if (alt_sec) { |
| 157 | fprintf(outfile, "\t.alt = %lu,\n", |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 158 | (unsigned long)GET_LE(&alt_sec->sh_offset)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 159 | fprintf(outfile, "\t.alt_len = %lu,\n", |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 160 | (unsigned long)GET_LE(&alt_sec->sh_size)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 161 | } |
| 162 | for (i = 0; i < NSYMS; i++) { |
Andy Lutomirski | bfad381 | 2014-06-18 15:59:48 -0700 | [diff] [blame] | 163 | if (required_syms[i].export && syms[i]) |
Andy Lutomirski | e6577a7 | 2014-07-10 18:13:15 -0700 | [diff] [blame] | 164 | fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n", |
| 165 | required_syms[i].name, (int64_t)syms[i]); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 166 | } |
| 167 | fprintf(outfile, "};\n"); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 168 | } |