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 | |
| 7 | static int GOFUNC(void *addr, size_t len, FILE *outfile, const char *name) |
| 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 | |
| 21 | Elf_Phdr *pt = (Elf_Phdr *)(addr + hdr->e_phoff); |
| 22 | |
| 23 | /* Walk the segment table. */ |
| 24 | for (i = 0; i < hdr->e_phnum; i++) { |
| 25 | if (pt[i].p_type == PT_LOAD) { |
| 26 | if (found_load) |
| 27 | fail("multiple PT_LOAD segs\n"); |
| 28 | |
| 29 | if (pt[i].p_offset != 0 || pt[i].p_vaddr != 0) |
| 30 | fail("PT_LOAD in wrong place\n"); |
| 31 | |
| 32 | if (pt[i].p_memsz != pt[i].p_filesz) |
| 33 | fail("cannot handle memsz != filesz\n"); |
| 34 | |
| 35 | load_size = pt[i].p_memsz; |
| 36 | found_load = 1; |
| 37 | } else if (pt[i].p_type == PT_DYNAMIC) { |
| 38 | dyn = addr + pt[i].p_offset; |
| 39 | dyn_end = addr + pt[i].p_offset + pt[i].p_memsz; |
| 40 | } |
| 41 | } |
| 42 | if (!found_load) |
| 43 | fail("no PT_LOAD seg\n"); |
| 44 | data_size = (load_size + 4095) / 4096 * 4096; |
| 45 | |
| 46 | /* Walk the dynamic table */ |
| 47 | for (i = 0; dyn + i < dyn_end && dyn[i].d_tag != DT_NULL; i++) { |
| 48 | if (dyn[i].d_tag == DT_REL || dyn[i].d_tag == DT_RELSZ || |
| 49 | dyn[i].d_tag == DT_RELENT || dyn[i].d_tag == DT_TEXTREL) |
| 50 | fail("vdso image contains dynamic relocations\n"); |
| 51 | } |
| 52 | |
| 53 | /* Walk the section table */ |
| 54 | secstrings_hdr = addr + hdr->e_shoff + hdr->e_shentsize*hdr->e_shstrndx; |
| 55 | secstrings = addr + secstrings_hdr->sh_offset; |
| 56 | for (i = 0; i < hdr->e_shnum; i++) { |
| 57 | Elf_Shdr *sh = addr + hdr->e_shoff + hdr->e_shentsize * i; |
| 58 | if (sh->sh_type == SHT_SYMTAB) |
| 59 | symtab_hdr = sh; |
| 60 | |
| 61 | if (!strcmp(secstrings + sh->sh_name, ".altinstructions")) |
| 62 | alt_sec = sh; |
| 63 | } |
| 64 | |
| 65 | if (!symtab_hdr) { |
| 66 | fail("no symbol table\n"); |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | strtab_hdr = addr + hdr->e_shoff + |
| 71 | hdr->e_shentsize * symtab_hdr->sh_link; |
| 72 | |
| 73 | /* Walk the symbol table */ |
| 74 | for (i = 0; i < symtab_hdr->sh_size / symtab_hdr->sh_entsize; i++) { |
| 75 | int k; |
| 76 | Elf_Sym *sym = addr + symtab_hdr->sh_offset + |
| 77 | symtab_hdr->sh_entsize * i; |
| 78 | const char *name = addr + strtab_hdr->sh_offset + sym->st_name; |
| 79 | for (k = 0; k < NSYMS; k++) { |
| 80 | if (!strcmp(name, required_syms[k])) { |
| 81 | if (syms[k]) { |
| 82 | fail("duplicate symbol %s\n", |
| 83 | required_syms[k]); |
| 84 | } |
| 85 | syms[k] = sym->st_value; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 90 | /* Validate mapping addresses. */ |
| 91 | for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) { |
| 92 | if (!syms[i]) |
| 93 | continue; /* The mapping isn't used; ignore it. */ |
| 94 | |
| 95 | if (syms[i] % 4096) |
| 96 | fail("%s must be a multiple of 4096\n", |
| 97 | required_syms[i]); |
| 98 | if (syms[i] < data_size) |
| 99 | fail("%s must be after the text mapping\n", |
| 100 | required_syms[i]); |
| 101 | if (syms[sym_end_mapping] < syms[i] + 4096) |
| 102 | fail("%s overruns end_mapping\n", required_syms[i]); |
| 103 | } |
| 104 | if (syms[sym_end_mapping] % 4096) |
| 105 | fail("end_mapping must be a multiple of 4096\n"); |
| 106 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 107 | /* Remove sections. */ |
| 108 | hdr->e_shoff = 0; |
| 109 | hdr->e_shentsize = 0; |
| 110 | hdr->e_shnum = 0; |
| 111 | hdr->e_shstrndx = SHN_UNDEF; |
| 112 | |
| 113 | if (!name) { |
| 114 | fwrite(addr, load_size, 1, outfile); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n"); |
| 119 | fprintf(outfile, "#include <linux/linkage.h>\n"); |
| 120 | fprintf(outfile, "#include <asm/page_types.h>\n"); |
| 121 | fprintf(outfile, "#include <asm/vdso.h>\n"); |
| 122 | fprintf(outfile, "\n"); |
| 123 | fprintf(outfile, |
| 124 | "static unsigned char raw_data[%lu] __page_aligned_data = {", |
| 125 | data_size); |
| 126 | for (j = 0; j < load_size; j++) { |
| 127 | if (j % 10 == 0) |
| 128 | fprintf(outfile, "\n\t"); |
| 129 | fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]); |
| 130 | } |
| 131 | fprintf(outfile, "\n};\n\n"); |
| 132 | |
| 133 | fprintf(outfile, "static struct page *pages[%lu];\n\n", |
| 134 | data_size / 4096); |
| 135 | |
| 136 | fprintf(outfile, "const struct vdso_image %s = {\n", name); |
| 137 | fprintf(outfile, "\t.data = raw_data,\n"); |
| 138 | fprintf(outfile, "\t.size = %lu,\n", data_size); |
Andy Lutomirski | a62c34b | 2014-05-19 15:58:33 -0700 | [diff] [blame^] | 139 | fprintf(outfile, "\t.text_mapping = {\n"); |
| 140 | fprintf(outfile, "\t\t.name = \"[vdso]\",\n"); |
| 141 | fprintf(outfile, "\t\t.pages = pages,\n"); |
| 142 | fprintf(outfile, "\t},\n"); |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 143 | if (alt_sec) { |
| 144 | fprintf(outfile, "\t.alt = %lu,\n", |
| 145 | (unsigned long)alt_sec->sh_offset); |
| 146 | fprintf(outfile, "\t.alt_len = %lu,\n", |
| 147 | (unsigned long)alt_sec->sh_size); |
| 148 | } |
| 149 | for (i = 0; i < NSYMS; i++) { |
| 150 | if (syms[i]) |
| 151 | fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n", |
| 152 | required_syms[i], syms[i]); |
| 153 | } |
| 154 | fprintf(outfile, "};\n"); |
| 155 | |
| 156 | return 0; |
| 157 | } |