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