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.
7 static void GOFUNC(void *addr, size_t len, FILE *outfile, const char *name)
10 unsigned long load_size = -1; /* Work around bogus warning */
11 unsigned long data_size;
12 Elf_Ehdr *hdr = (Elf_Ehdr *)addr;
15 Elf_Shdr *symtab_hdr = NULL, *strtab_hdr, *secstrings_hdr,
17 Elf_Dyn *dyn = 0, *dyn_end = 0;
18 const char *secstrings;
19 uint64_t syms[NSYMS] = {};
21 Elf_Phdr *pt = (Elf_Phdr *)(addr + GET_LE(&hdr->e_phoff));
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) {
27 fail("multiple PT_LOAD segs\n");
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");
33 if (GET_LE(&pt[i].p_memsz) != GET_LE(&pt[i].p_filesz))
34 fail("cannot handle memsz != filesz\n");
36 load_size = GET_LE(&pt[i].p_memsz);
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);
45 fail("no PT_LOAD seg\n");
46 data_size = (load_size + 4095) / 4096 * 4096;
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");
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)
67 if (!strcmp(secstrings + GET_LE(&sh->sh_name),
73 fail("no symbol table\n");
75 strtab_hdr = addr + GET_LE(&hdr->e_shoff) +
76 GET_LE(&hdr->e_shentsize) * GET_LE(&symtab_hdr->sh_link);
78 /* Walk the symbol table */
80 i < GET_LE(&symtab_hdr->sh_size) / GET_LE(&symtab_hdr->sh_entsize);
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])) {
90 fail("duplicate symbol %s\n",
93 syms[k] = GET_LE(&sym->st_value);
98 /* Validate mapping addresses. */
99 for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
101 continue; /* The mapping isn't used; ignore it. */
104 fail("%s must be a multiple of 4096\n",
106 if (syms[i] < data_size)
107 fail("%s must be after the text mapping\n",
109 if (syms[sym_end_mapping] < syms[i] + 4096)
110 fail("%s overruns end_mapping\n", required_syms[i]);
112 if (syms[sym_end_mapping] % 4096)
113 fail("end_mapping must be a multiple of 4096\n");
115 /* Remove sections. */
117 hdr->e_shentsize = 0;
119 hdr->e_shstrndx = SHN_UNDEF; /* SHN_UNDEF == 0 */
122 fwrite(addr, load_size, 1, outfile);
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");
132 "static unsigned char raw_data[%lu] __page_aligned_data = {",
134 for (j = 0; j < load_size; j++) {
136 fprintf(outfile, "\n\t");
137 fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]);
139 fprintf(outfile, "\n};\n\n");
141 fprintf(outfile, "static struct page *pages[%lu];\n\n",
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");
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));
157 for (i = 0; i < NSYMS; i++) {
159 fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n",
160 required_syms[i], syms[i]);
162 fprintf(outfile, "};\n");