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