]>
Commit | Line | Data |
---|---|---|
6f121e54 AL |
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 | ||
da861e18 AL |
7 | static void BITSFUNC(go)(void *raw_addr, size_t raw_len, |
8 | void *stripped_addr, size_t stripped_len, | |
c1979c37 | 9 | FILE *outfile, const char *name) |
6f121e54 AL |
10 | { |
11 | int found_load = 0; | |
12 | unsigned long load_size = -1; /* Work around bogus warning */ | |
da861e18 AL |
13 | unsigned long mapping_size; |
14 | ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr; | |
6f121e54 AL |
15 | int i; |
16 | unsigned long j; | |
c1979c37 | 17 | ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr, |
6f121e54 | 18 | *alt_sec = NULL; |
c1979c37 | 19 | ELF(Dyn) *dyn = 0, *dyn_end = 0; |
6f121e54 | 20 | const char *secstrings; |
e6577a7c | 21 | INT_BITS syms[NSYMS] = {}; |
6f121e54 | 22 | |
da861e18 | 23 | ELF(Phdr) *pt = (ELF(Phdr) *)(raw_addr + GET_LE(&hdr->e_phoff)); |
6f121e54 | 24 | |
e4aad645 | 25 | if (GET_LE(&hdr->e_type) != ET_DYN) |
57f90c3d AL |
26 | fail("input is not a shared object\n"); |
27 | ||
6f121e54 | 28 | /* Walk the segment table. */ |
bdfb9bcc PA |
29 | for (i = 0; i < GET_LE(&hdr->e_phnum); i++) { |
30 | if (GET_LE(&pt[i].p_type) == PT_LOAD) { | |
6f121e54 AL |
31 | if (found_load) |
32 | fail("multiple PT_LOAD segs\n"); | |
33 | ||
bdfb9bcc PA |
34 | if (GET_LE(&pt[i].p_offset) != 0 || |
35 | GET_LE(&pt[i].p_vaddr) != 0) | |
6f121e54 AL |
36 | fail("PT_LOAD in wrong place\n"); |
37 | ||
bdfb9bcc | 38 | if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz)) |
6f121e54 AL |
39 | fail("cannot handle memsz != filesz\n"); |
40 | ||
bdfb9bcc | 41 | load_size = GET_LE(&pt[i].p_memsz); |
6f121e54 | 42 | found_load = 1; |
bdfb9bcc | 43 | } else if (GET_LE(&pt[i].p_type) == PT_DYNAMIC) { |
da861e18 AL |
44 | dyn = raw_addr + GET_LE(&pt[i].p_offset); |
45 | dyn_end = raw_addr + GET_LE(&pt[i].p_offset) + | |
bdfb9bcc | 46 | GET_LE(&pt[i].p_memsz); |
6f121e54 AL |
47 | } |
48 | } | |
49 | if (!found_load) | |
50 | fail("no PT_LOAD seg\n"); | |
da861e18 AL |
51 | |
52 | if (stripped_len < load_size) | |
53 | fail("stripped input is too short\n"); | |
6f121e54 | 54 | |
57f90c3d AL |
55 | if (!dyn) |
56 | fail("input has no PT_DYNAMIC section -- your toolchain is buggy\n"); | |
57 | ||
6f121e54 | 58 | /* Walk the dynamic table */ |
c191920f | 59 | for (i = 0; dyn + i < dyn_end && |
bdfb9bcc PA |
60 | GET_LE(&dyn[i].d_tag) != DT_NULL; i++) { |
61 | typeof(dyn[i].d_tag) tag = GET_LE(&dyn[i].d_tag); | |
6a89d710 | 62 | if (tag == DT_REL || tag == DT_RELSZ || tag == DT_RELA || |
add4eed0 | 63 | tag == DT_RELENT || tag == DT_TEXTREL) |
6f121e54 AL |
64 | fail("vdso image contains dynamic relocations\n"); |
65 | } | |
66 | ||
67 | /* Walk the section table */ | |
da861e18 | 68 | secstrings_hdr = raw_addr + GET_LE(&hdr->e_shoff) + |
bdfb9bcc | 69 | GET_LE(&hdr->e_shentsize)*GET_LE(&hdr->e_shstrndx); |
da861e18 | 70 | secstrings = raw_addr + GET_LE(&secstrings_hdr->sh_offset); |
bdfb9bcc | 71 | for (i = 0; i < GET_LE(&hdr->e_shnum); i++) { |
da861e18 | 72 | ELF(Shdr) *sh = raw_addr + GET_LE(&hdr->e_shoff) + |
bdfb9bcc PA |
73 | GET_LE(&hdr->e_shentsize) * i; |
74 | if (GET_LE(&sh->sh_type) == SHT_SYMTAB) | |
6f121e54 AL |
75 | symtab_hdr = sh; |
76 | ||
bdfb9bcc | 77 | if (!strcmp(secstrings + GET_LE(&sh->sh_name), |
c191920f | 78 | ".altinstructions")) |
6f121e54 AL |
79 | alt_sec = sh; |
80 | } | |
81 | ||
01156183 | 82 | if (!symtab_hdr) |
6f121e54 | 83 | fail("no symbol table\n"); |
6f121e54 | 84 | |
da861e18 | 85 | strtab_hdr = raw_addr + GET_LE(&hdr->e_shoff) + |
bdfb9bcc | 86 | GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link); |
6f121e54 AL |
87 | |
88 | /* Walk the symbol table */ | |
c191920f | 89 | for (i = 0; |
bdfb9bcc | 90 | i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize); |
add4eed0 | 91 | i++) { |
6f121e54 | 92 | int k; |
da861e18 | 93 | ELF(Sym) *sym = raw_addr + GET_LE(&symtab_hdr->sh_offset) + |
bdfb9bcc | 94 | GET_LE(&symtab_hdr->sh_entsize) * i; |
da861e18 | 95 | const char *name = raw_addr + GET_LE(&strtab_hdr->sh_offset) + |
bdfb9bcc | 96 | GET_LE(&sym->st_name); |
e0bf7b86 | 97 | |
6f121e54 | 98 | for (k = 0; k < NSYMS; k++) { |
bfad381c | 99 | if (!strcmp(name, required_syms[k].name)) { |
6f121e54 AL |
100 | if (syms[k]) { |
101 | fail("duplicate symbol %s\n", | |
bfad381c | 102 | required_syms[k].name); |
6f121e54 | 103 | } |
e6577a7c AL |
104 | |
105 | /* | |
106 | * Careful: we use negative addresses, but | |
107 | * st_value is unsigned, so we rely | |
108 | * on syms[k] being a signed type of the | |
109 | * correct width. | |
110 | */ | |
bdfb9bcc | 111 | syms[k] = GET_LE(&sym->st_value); |
6f121e54 AL |
112 | } |
113 | } | |
bfad381c | 114 | } |
bfad381c | 115 | |
18d0a6fd AL |
116 | /* Validate mapping addresses. */ |
117 | for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) { | |
f12c1f90 AL |
118 | INT_BITS symval = syms[special_pages[i]]; |
119 | ||
120 | if (!symval) | |
18d0a6fd AL |
121 | continue; /* The mapping isn't used; ignore it. */ |
122 | ||
f12c1f90 | 123 | if (symval % 4096) |
18d0a6fd | 124 | fail("%s must be a multiple of 4096\n", |
bfad381c | 125 | required_syms[i].name); |
f12c1f90 AL |
126 | if (symval + 4096 < syms[sym_vvar_start]) |
127 | fail("%s underruns vvar_start\n", | |
bfad381c | 128 | required_syms[i].name); |
f12c1f90 | 129 | if (symval + 4096 > 0) |
e6577a7c | 130 | fail("%s is on the wrong side of the vdso text\n", |
bfad381c | 131 | required_syms[i].name); |
18d0a6fd | 132 | } |
e6577a7c AL |
133 | if (syms[sym_vvar_start] % 4096) |
134 | fail("vvar_begin must be a multiple of 4096\n"); | |
18d0a6fd | 135 | |
6f121e54 | 136 | if (!name) { |
da861e18 | 137 | fwrite(stripped_addr, stripped_len, 1, outfile); |
01156183 | 138 | return; |
6f121e54 AL |
139 | } |
140 | ||
da861e18 AL |
141 | mapping_size = (stripped_len + 4095) / 4096 * 4096; |
142 | ||
6f121e54 AL |
143 | fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n"); |
144 | fprintf(outfile, "#include <linux/linkage.h>\n"); | |
145 | fprintf(outfile, "#include <asm/page_types.h>\n"); | |
146 | fprintf(outfile, "#include <asm/vdso.h>\n"); | |
147 | fprintf(outfile, "\n"); | |
148 | fprintf(outfile, | |
018ef8dc | 149 | "static unsigned char raw_data[%lu] __ro_after_init __aligned(PAGE_SIZE) = {", |
da861e18 AL |
150 | mapping_size); |
151 | for (j = 0; j < stripped_len; j++) { | |
6f121e54 AL |
152 | if (j % 10 == 0) |
153 | fprintf(outfile, "\n\t"); | |
da861e18 AL |
154 | fprintf(outfile, "0x%02X, ", |
155 | (int)((unsigned char *)stripped_addr)[j]); | |
6f121e54 AL |
156 | } |
157 | fprintf(outfile, "\n};\n\n"); | |
158 | ||
6f121e54 AL |
159 | fprintf(outfile, "const struct vdso_image %s = {\n", name); |
160 | fprintf(outfile, "\t.data = raw_data,\n"); | |
da861e18 | 161 | fprintf(outfile, "\t.size = %lu,\n", mapping_size); |
6f121e54 AL |
162 | if (alt_sec) { |
163 | fprintf(outfile, "\t.alt = %lu,\n", | |
bdfb9bcc | 164 | (unsigned long)GET_LE(&alt_sec->sh_offset)); |
6f121e54 | 165 | fprintf(outfile, "\t.alt_len = %lu,\n", |
bdfb9bcc | 166 | (unsigned long)GET_LE(&alt_sec->sh_size)); |
6f121e54 AL |
167 | } |
168 | for (i = 0; i < NSYMS; i++) { | |
bfad381c | 169 | if (required_syms[i].export && syms[i]) |
e6577a7c AL |
170 | fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n", |
171 | required_syms[i].name, (int64_t)syms[i]); | |
6f121e54 AL |
172 | } |
173 | fprintf(outfile, "};\n"); | |
6f121e54 | 174 | } |