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 int 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 + hdr->e_phoff);
23 /* Walk the segment table. */
24 for (i = 0; i < hdr->e_phnum; i++) {
25 if (pt[i].p_type == PT_LOAD) {
27 fail("multiple PT_LOAD segs\n");
29 if (pt[i].p_offset != 0 || pt[i].p_vaddr != 0)
30 fail("PT_LOAD in wrong place\n");
32 if (pt[i].p_memsz != pt[i].p_filesz)
33 fail("cannot handle memsz != filesz\n");
35 load_size = pt[i].p_memsz;
37 } else if (pt[i].p_type == PT_DYNAMIC) {
38 dyn = addr + pt[i].p_offset;
39 dyn_end = addr + pt[i].p_offset + pt[i].p_memsz;
43 fail("no PT_LOAD seg\n");
44 data_size = (load_size + 4095) / 4096 * 4096;
46 /* Walk the dynamic table */
47 for (i = 0; dyn + i < dyn_end && dyn[i].d_tag != DT_NULL; i++) {
48 if (dyn[i].d_tag == DT_REL || dyn[i].d_tag == DT_RELSZ ||
49 dyn[i].d_tag == DT_RELENT || dyn[i].d_tag == DT_TEXTREL)
50 fail("vdso image contains dynamic relocations\n");
53 /* Walk the section table */
54 secstrings_hdr = addr + hdr->e_shoff + hdr->e_shentsize*hdr->e_shstrndx;
55 secstrings = addr + secstrings_hdr->sh_offset;
56 for (i = 0; i < hdr->e_shnum; i++) {
57 Elf_Shdr *sh = addr + hdr->e_shoff + hdr->e_shentsize * i;
58 if (sh->sh_type == SHT_SYMTAB)
61 if (!strcmp(secstrings + sh->sh_name, ".altinstructions"))
66 fail("no symbol table\n");
70 strtab_hdr = addr + hdr->e_shoff +
71 hdr->e_shentsize * symtab_hdr->sh_link;
73 /* Walk the symbol table */
74 for (i = 0; i < symtab_hdr->sh_size / symtab_hdr->sh_entsize; i++) {
76 Elf_Sym *sym = addr + symtab_hdr->sh_offset +
77 symtab_hdr->sh_entsize * i;
78 const char *name = addr + strtab_hdr->sh_offset + sym->st_name;
79 for (k = 0; k < NSYMS; k++) {
80 if (!strcmp(name, required_syms[k])) {
82 fail("duplicate symbol %s\n",
85 syms[k] = sym->st_value;
90 /* Validate mapping addresses. */
91 for (i = 0; i < sizeof(special_pages) / sizeof(special_pages[0]); i++) {
93 continue; /* The mapping isn't used; ignore it. */
96 fail("%s must be a multiple of 4096\n",
98 if (syms[i] < data_size)
99 fail("%s must be after the text mapping\n",
101 if (syms[sym_end_mapping] < syms[i] + 4096)
102 fail("%s overruns end_mapping\n", required_syms[i]);
104 if (syms[sym_end_mapping] % 4096)
105 fail("end_mapping must be a multiple of 4096\n");
107 /* Remove sections. */
109 hdr->e_shentsize = 0;
111 hdr->e_shstrndx = SHN_UNDEF;
114 fwrite(addr, load_size, 1, outfile);
118 fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
119 fprintf(outfile, "#include <linux/linkage.h>\n");
120 fprintf(outfile, "#include <asm/page_types.h>\n");
121 fprintf(outfile, "#include <asm/vdso.h>\n");
122 fprintf(outfile, "\n");
124 "static unsigned char raw_data[%lu] __page_aligned_data = {",
126 for (j = 0; j < load_size; j++) {
128 fprintf(outfile, "\n\t");
129 fprintf(outfile, "0x%02X, ", (int)((unsigned char *)addr)[j]);
131 fprintf(outfile, "\n};\n\n");
133 fprintf(outfile, "static struct page *pages[%lu];\n\n",
136 fprintf(outfile, "const struct vdso_image %s = {\n", name);
137 fprintf(outfile, "\t.data = raw_data,\n");
138 fprintf(outfile, "\t.size = %lu,\n", data_size);
139 fprintf(outfile, "\t.text_mapping = {\n");
140 fprintf(outfile, "\t\t.name = \"[vdso]\",\n");
141 fprintf(outfile, "\t\t.pages = pages,\n");
142 fprintf(outfile, "\t},\n");
144 fprintf(outfile, "\t.alt = %lu,\n",
145 (unsigned long)alt_sec->sh_offset);
146 fprintf(outfile, "\t.alt_len = %lu,\n",
147 (unsigned long)alt_sec->sh_size);
149 for (i = 0; i < NSYMS; i++) {
151 fprintf(outfile, "\t.sym_%s = 0x%" PRIx64 ",\n",
152 required_syms[i], syms[i]);
154 fprintf(outfile, "};\n");