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 | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 7 | static void BITSFUNC(go)(void *addr, size_t len, |
| 8 | FILE *outfile, const char *name) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 9 | { |
| 10 | int found_load = 0; |
| 11 | unsigned long load_size = -1; /* Work around bogus warning */ |
| 12 | unsigned long data_size; |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 13 | ELF(Ehdr) *hdr = (ELF(Ehdr) *)addr; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 14 | int i; |
| 15 | unsigned long j; |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 16 | ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr, |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 17 | *alt_sec = NULL; |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 18 | ELF(Dyn) *dyn = 0, *dyn_end = 0; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 19 | const char *secstrings; |
| 20 | uint64_t syms[NSYMS] = {}; |
| 21 | |
Andy Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 22 | uint64_t fake_sections_value = 0, fake_sections_size = 0; |
| 23 | |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 24 | ELF(Phdr) *pt = (ELF(Phdr) *)(addr + GET_LE(&hdr->e_phoff)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 25 | |
| 26 | /* Walk the segment table. */ |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 27 | for (i = 0; i < GET_LE(&hdr->e_phnum); i++) { |
| 28 | if (GET_LE(&pt[i].p_type) == PT_LOAD) { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 29 | if (found_load) |
| 30 | fail("multiple PT_LOAD segs\n"); |
| 31 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 32 | if (GET_LE(&pt[i].p_offset) != 0 || |
| 33 | GET_LE(&pt[i].p_vaddr) != 0) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 34 | fail("PT_LOAD in wrong place\n"); |
| 35 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 36 | 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] | 37 | fail("cannot handle memsz != filesz\n"); |
| 38 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 39 | load_size = GET_LE(&pt[i].p_memsz); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 40 | found_load = 1; |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 41 | } 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 45 | } |
| 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 Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 52 | for (i = 0; dyn + i < dyn_end && |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 53 | GET_LE(&dyn[i].d_tag) != DT_NULL; i++) { |
| 54 | typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag); |
Andy Lutomirski | add4eed | 2014-05-30 08:48:49 -0700 | [diff] [blame] | 55 | if (tag == DT_REL || tag == DT_RELSZ || |
| 56 | tag == DT_RELENT || tag == DT_TEXTREL) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 57 | fail("vdso image contains dynamic relocations\n"); |
| 58 | } |
| 59 | |
| 60 | /* Walk the section table */ |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 61 | 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 Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 65 | ELF(Shdr) *sh = addr + GET_LE(&hdr->e_shoff) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 66 | GET_LE(&hdr->e_shentsize) * i; |
| 67 | if (GET_LE(&sh->sh_type) == SHT_SYMTAB) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 68 | symtab_hdr = sh; |
| 69 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 70 | if (!strcmp(secstrings + GET_LE(&sh->sh_name), |
H. Peter Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 71 | ".altinstructions")) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 72 | alt_sec = sh; |
| 73 | } |
| 74 | |
Andy Lutomirski | 0115618 | 2014-05-30 08:48:48 -0700 | [diff] [blame] | 75 | if (!symtab_hdr) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 76 | fail("no symbol table\n"); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 77 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 78 | strtab_hdr = addr + GET_LE(&hdr->e_shoff) + |
| 79 | GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 80 | |
| 81 | /* Walk the symbol table */ |
H. Peter Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 82 | for (i = 0; |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 83 | 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] | 84 | i++) { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 85 | int k; |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 86 | ELF(Sym) *sym = addr + GET_LE(&symtab_hdr->sh_offset) + |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 87 | 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 Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 90 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 91 | 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 Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 97 | syms[k] = GET_LE(&sym->st_value); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
Andy Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 100 | |
| 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 109 | /* 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 Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 126 | /* Remove sections or use fakes */ |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 127 | if (fake_sections_size % sizeof(ELF(Shdr))) |
Andy Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 128 | fail("vdso_fake_sections size is not a multiple of %ld\n", |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 129 | (long)sizeof(ELF(Shdr))); |
Andy Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 130 | PUT_LE(&hdr->e_shoff, fake_sections_value); |
Andy Lutomirski | c1979c3 | 2014-06-18 15:59:47 -0700 | [diff] [blame^] | 131 | 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 Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 133 | PUT_LE(&hdr->e_shstrndx, SHN_UNDEF); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 134 | |
| 135 | if (!name) { |
| 136 | fwrite(addr, load_size, 1, outfile); |
Andy Lutomirski | 0115618 | 2014-05-30 08:48:48 -0700 | [diff] [blame] | 137 | return; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 138 | } |
| 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 Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 161 | 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 165 | if (alt_sec) { |
| 166 | fprintf(outfile, "\t.alt = %lu,\n", |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 167 | (unsigned long)GET_LE(&alt_sec->sh_offset)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 168 | fprintf(outfile, "\t.alt_len = %lu,\n", |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 169 | (unsigned long)GET_LE(&alt_sec->sh_size)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 170 | } |
| 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 177 | } |