OSDN Git Service

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