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 | 0115618 | 2014-05-30 08:48:48 -0700 | [diff] [blame] | 7 | static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 8 | { |
| 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 Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 21 | uint64_t fake_sections_value = 0, fake_sections_size = 0; |
| 22 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 23 | Elf_Phdr *pt = (Elf_Phdr *)(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) { |
| 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 44 | } |
| 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 Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 51 | for (i = 0; dyn + i < dyn_end && |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 52 | GET_LE(&dyn[i].d_tag) != DT_NULL; i++) { |
| 53 | typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag); |
Andy Lutomirski | add4eed | 2014-05-30 08:48:49 -0700 | [diff] [blame] | 54 | if (tag == DT_REL || tag == DT_RELSZ || |
| 55 | tag == DT_RELENT || tag == DT_TEXTREL) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 56 | fail("vdso image contains dynamic relocations\n"); |
| 57 | } |
| 58 | |
| 59 | /* Walk the section table */ |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 60 | 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 67 | symtab_hdr = sh; |
| 68 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 69 | if (!strcmp(secstrings + GET_LE(&sh->sh_name), |
H. Peter Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 70 | ".altinstructions")) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 71 | alt_sec = sh; |
| 72 | } |
| 73 | |
Andy Lutomirski | 0115618 | 2014-05-30 08:48:48 -0700 | [diff] [blame] | 74 | if (!symtab_hdr) |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 75 | fail("no symbol table\n"); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 76 | |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 77 | strtab_hdr = addr + GET_LE(&hdr->e_shoff) + |
| 78 | GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 79 | |
| 80 | /* Walk the symbol table */ |
H. Peter Anvin | c191920 | 2014-05-30 17:03:22 -0700 | [diff] [blame] | 81 | for (i = 0; |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 82 | 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] | 83 | i++) { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 84 | int k; |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 85 | 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 Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 89 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 90 | 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 Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 96 | syms[k] = GET_LE(&sym->st_value); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
Andy Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 99 | |
| 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 108 | /* 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 Lutomirski | e0bf7b8 | 2014-06-12 17:53:12 -0700 | [diff] [blame] | 125 | /* 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 133 | |
| 134 | if (!name) { |
| 135 | fwrite(addr, load_size, 1, outfile); |
Andy Lutomirski | 0115618 | 2014-05-30 08:48:48 -0700 | [diff] [blame] | 136 | return; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 137 | } |
| 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 Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame] | 160 | 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 164 | if (alt_sec) { |
| 165 | fprintf(outfile, "\t.alt = %lu,\n", |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 166 | (unsigned long)GET_LE(&alt_sec->sh_offset)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 167 | fprintf(outfile, "\t.alt_len = %lu,\n", |
H. Peter Anvin | bdfb9bc | 2014-06-06 14:30:37 -0700 | [diff] [blame] | 168 | (unsigned long)GET_LE(&alt_sec->sh_size)); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 169 | } |
| 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 Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 176 | } |