4df005cf98c0e5f4c0e84c68058b8a829e3cdc0b
[muen/linux.git] / arch / sparc / vdso / vdso2c.h
1 /*
2  * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
3  */
4
5 /*
6  * This file is included up to twice from vdso2c.c.  It generates code for
7  * 32-bit and 64-bit vDSOs.  We will eventually need both for 64-bit builds,
8  * since 32-bit vDSOs will then be built for 32-bit userspace.
9  */
10
11 static void BITSFUNC(go)(void *raw_addr, size_t raw_len,
12                          void *stripped_addr, size_t stripped_len,
13                          FILE *outfile, const char *name)
14 {
15         int found_load = 0;
16         unsigned long load_size = -1;  /* Work around bogus warning */
17         unsigned long mapping_size;
18         int i;
19         unsigned long j;
20         ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
21                 *patch_sec = NULL;
22         ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr;
23         ELF(Dyn) *dyn = 0, *dyn_end = 0;
24         const char *secstrings;
25         INT_BITS syms[NSYMS] = {};
26
27         ELF(Phdr) *pt = (ELF(Phdr) *)(raw_addr + GET_BE(&hdr->e_phoff));
28
29         /* Walk the segment table. */
30         for (i = 0; i < GET_BE(&hdr->e_phnum); i++) {
31                 if (GET_BE(&pt[i].p_type) == PT_LOAD) {
32                         if (found_load)
33                                 fail("multiple PT_LOAD segs\n");
34
35                         if (GET_BE(&pt[i].p_offset) != 0 ||
36                             GET_BE(&pt[i].p_vaddr) != 0)
37                                 fail("PT_LOAD in wrong place\n");
38
39                         if (GET_BE(&pt[i].p_memsz) != GET_BE(&pt[i].p_filesz))
40                                 fail("cannot handle memsz != filesz\n");
41
42                         load_size = GET_BE(&pt[i].p_memsz);
43                         found_load = 1;
44                 } else if (GET_BE(&pt[i].p_type) == PT_DYNAMIC) {
45                         dyn = raw_addr + GET_BE(&pt[i].p_offset);
46                         dyn_end = raw_addr + GET_BE(&pt[i].p_offset) +
47                                 GET_BE(&pt[i].p_memsz);
48                 }
49         }
50         if (!found_load)
51                 fail("no PT_LOAD seg\n");
52
53         if (stripped_len < load_size)
54                 fail("stripped input is too short\n");
55
56         /* Walk the dynamic table */
57         for (i = 0; dyn + i < dyn_end &&
58                      GET_BE(&dyn[i].d_tag) != DT_NULL; i++) {
59                 typeof(dyn[i].d_tag) tag = GET_BE(&dyn[i].d_tag);
60                 typeof(dyn[i].d_un.d_val) val = GET_BE(&dyn[i].d_un.d_val);
61
62                 if ((tag == DT_RELSZ || tag == DT_RELASZ) && (val != 0))
63                         fail("vdso image contains dynamic relocations\n");
64         }
65
66         /* Walk the section table */
67         secstrings_hdr = raw_addr + GET_BE(&hdr->e_shoff) +
68                 GET_BE(&hdr->e_shentsize)*GET_BE(&hdr->e_shstrndx);
69         secstrings = raw_addr + GET_BE(&secstrings_hdr->sh_offset);
70         for (i = 0; i < GET_BE(&hdr->e_shnum); i++) {
71                 ELF(Shdr) *sh = raw_addr + GET_BE(&hdr->e_shoff) +
72                         GET_BE(&hdr->e_shentsize) * i;
73                 if (GET_BE(&sh->sh_type) == SHT_SYMTAB)
74                         symtab_hdr = sh;
75
76                 if (!strcmp(secstrings + GET_BE(&sh->sh_name),
77                             ".tick_patch"))
78                         patch_sec = sh;
79         }
80
81         if (!symtab_hdr)
82                 fail("no symbol table\n");
83
84         strtab_hdr = raw_addr + GET_BE(&hdr->e_shoff) +
85                 GET_BE(&hdr->e_shentsize) * GET_BE(&symtab_hdr->sh_link);
86
87         /* Walk the symbol table */
88         for (i = 0;
89              i < GET_BE(&symtab_hdr->sh_size) / GET_BE(&symtab_hdr->sh_entsize);
90              i++) {
91                 int k;
92
93                 ELF(Sym) *sym = raw_addr + GET_BE(&symtab_hdr->sh_offset) +
94                         GET_BE(&symtab_hdr->sh_entsize) * i;
95                 const char *name = raw_addr + GET_BE(&strtab_hdr->sh_offset) +
96                         GET_BE(&sym->st_name);
97
98                 for (k = 0; k < NSYMS; k++) {
99                         if (!strcmp(name, required_syms[k].name)) {
100                                 if (syms[k]) {
101                                         fail("duplicate symbol %s\n",
102                                              required_syms[k].name);
103                                 }
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                                  */
111                                 syms[k] = GET_BE(&sym->st_value);
112                         }
113                 }
114         }
115
116         /* Validate mapping addresses. */
117         if (syms[sym_vvar_start] % 8192)
118                 fail("vvar_begin must be a multiple of 8192\n");
119
120         if (!name) {
121                 fwrite(stripped_addr, stripped_len, 1, outfile);
122                 return;
123         }
124
125         mapping_size = (stripped_len + 8191) / 8192 * 8192;
126
127         fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
128         fprintf(outfile, "#include <linux/cache.h>\n");
129         fprintf(outfile, "#include <asm/vdso.h>\n");
130         fprintf(outfile, "\n");
131         fprintf(outfile,
132                 "static unsigned char raw_data[%lu] __ro_after_init __aligned(8192)= {",
133                 mapping_size);
134         for (j = 0; j < stripped_len; j++) {
135                 if (j % 10 == 0)
136                         fprintf(outfile, "\n\t");
137                 fprintf(outfile, "0x%02X, ",
138                         (int)((unsigned char *)stripped_addr)[j]);
139         }
140         fprintf(outfile, "\n};\n\n");
141
142         fprintf(outfile, "const struct vdso_image %s_builtin = {\n", name);
143         fprintf(outfile, "\t.data = raw_data,\n");
144         fprintf(outfile, "\t.size = %lu,\n", mapping_size);
145         if (patch_sec) {
146                 fprintf(outfile, "\t.tick_patch = %lu,\n",
147                         (unsigned long)GET_BE(&patch_sec->sh_offset));
148                 fprintf(outfile, "\t.tick_patch_len = %lu,\n",
149                         (unsigned long)GET_BE(&patch_sec->sh_size));
150         }
151         for (i = 0; i < NSYMS; i++) {
152                 if (required_syms[i].export && syms[i])
153                         fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
154                                 required_syms[i].name, (int64_t)syms[i]);
155         }
156         fprintf(outfile, "};\n");
157 }