1 /* SPDX-License-Identifier: GPL-2.0 */
3 * This file is included twice from vdso2c.c. It generates code for 32-bit
4 * and 64-bit vDSOs. We need both for 64-bit builds, since 32-bit vDSOs
5 * are built for 32-bit userspace.
8 static void BITSFUNC(go
)(void *raw_addr
, size_t raw_len
,
9 void *stripped_addr
, size_t stripped_len
,
10 FILE *outfile
, const char *name
)
13 unsigned long load_size
= -1; /* Work around bogus warning */
14 unsigned long mapping_size
;
15 ELF(Ehdr
) *hdr
= (ELF(Ehdr
) *)raw_addr
;
18 ELF(Shdr
) *symtab_hdr
= NULL
, *strtab_hdr
, *secstrings_hdr
,
20 ELF(Dyn
) *dyn
= 0, *dyn_end
= 0;
21 const char *secstrings
;
22 INT_BITS syms
[NSYMS
] = {};
24 ELF(Phdr
) *pt
= (ELF(Phdr
) *)(raw_addr
+ GET_LE(&hdr
->e_phoff
));
26 if (GET_LE(&hdr
->e_type
) != ET_DYN
)
27 fail("input is not a shared object\n");
29 /* Walk the segment table. */
30 for (i
= 0; i
< GET_LE(&hdr
->e_phnum
); i
++) {
31 if (GET_LE(&pt
[i
].p_type
) == PT_LOAD
) {
33 fail("multiple PT_LOAD segs\n");
35 if (GET_LE(&pt
[i
].p_offset
) != 0 ||
36 GET_LE(&pt
[i
].p_vaddr
) != 0)
37 fail("PT_LOAD in wrong place\n");
39 if (GET_LE(&pt
[i
].p_memsz
) != GET_LE(&pt
[i
].p_filesz
))
40 fail("cannot handle memsz != filesz\n");
42 load_size
= GET_LE(&pt
[i
].p_memsz
);
44 } else if (GET_LE(&pt
[i
].p_type
) == PT_DYNAMIC
) {
45 dyn
= raw_addr
+ GET_LE(&pt
[i
].p_offset
);
46 dyn_end
= raw_addr
+ GET_LE(&pt
[i
].p_offset
) +
47 GET_LE(&pt
[i
].p_memsz
);
51 fail("no PT_LOAD seg\n");
53 if (stripped_len
< load_size
)
54 fail("stripped input is too short\n");
57 fail("input has no PT_DYNAMIC section -- your toolchain is buggy\n");
59 /* Walk the dynamic table */
60 for (i
= 0; dyn
+ i
< dyn_end
&&
61 GET_LE(&dyn
[i
].d_tag
) != DT_NULL
; i
++) {
62 typeof(dyn
[i
].d_tag
) tag
= GET_LE(&dyn
[i
].d_tag
);
63 if (tag
== DT_REL
|| tag
== DT_RELSZ
|| tag
== DT_RELA
||
64 tag
== DT_RELENT
|| tag
== DT_TEXTREL
)
65 fail("vdso image contains dynamic relocations\n");
68 /* Walk the section table */
69 secstrings_hdr
= raw_addr
+ GET_LE(&hdr
->e_shoff
) +
70 GET_LE(&hdr
->e_shentsize
)*GET_LE(&hdr
->e_shstrndx
);
71 secstrings
= raw_addr
+ GET_LE(&secstrings_hdr
->sh_offset
);
72 for (i
= 0; i
< GET_LE(&hdr
->e_shnum
); i
++) {
73 ELF(Shdr
) *sh
= raw_addr
+ GET_LE(&hdr
->e_shoff
) +
74 GET_LE(&hdr
->e_shentsize
) * i
;
75 if (GET_LE(&sh
->sh_type
) == SHT_SYMTAB
)
78 if (!strcmp(secstrings
+ GET_LE(&sh
->sh_name
),
84 fail("no symbol table\n");
86 strtab_hdr
= raw_addr
+ GET_LE(&hdr
->e_shoff
) +
87 GET_LE(&hdr
->e_shentsize
) * GET_LE(&symtab_hdr
->sh_link
);
89 /* Walk the symbol table */
91 i
< GET_LE(&symtab_hdr
->sh_size
) / GET_LE(&symtab_hdr
->sh_entsize
);
94 ELF(Sym
) *sym
= raw_addr
+ GET_LE(&symtab_hdr
->sh_offset
) +
95 GET_LE(&symtab_hdr
->sh_entsize
) * i
;
96 const char *name
= raw_addr
+ GET_LE(&strtab_hdr
->sh_offset
) +
97 GET_LE(&sym
->st_name
);
99 for (k
= 0; k
< NSYMS
; k
++) {
100 if (!strcmp(name
, required_syms
[k
].name
)) {
102 fail("duplicate symbol %s\n",
103 required_syms
[k
].name
);
107 * Careful: we use negative addresses, but
108 * st_value is unsigned, so we rely
109 * on syms[k] being a signed type of the
112 syms
[k
] = GET_LE(&sym
->st_value
);
117 /* Validate mapping addresses. */
118 for (i
= 0; i
< sizeof(special_pages
) / sizeof(special_pages
[0]); i
++) {
119 INT_BITS symval
= syms
[special_pages
[i
]];
122 continue; /* The mapping isn't used; ignore it. */
125 fail("%s must be a multiple of 4096\n",
126 required_syms
[i
].name
);
127 if (symval
+ 4096 < syms
[sym_vvar_start
])
128 fail("%s underruns vvar_start\n",
129 required_syms
[i
].name
);
130 if (symval
+ 4096 > 0)
131 fail("%s is on the wrong side of the vdso text\n",
132 required_syms
[i
].name
);
134 if (syms
[sym_vvar_start
] % 4096)
135 fail("vvar_begin must be a multiple of 4096\n");
138 fwrite(stripped_addr
, stripped_len
, 1, outfile
);
142 mapping_size
= (stripped_len
+ 4095) / 4096 * 4096;
144 fprintf(outfile
, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
145 fprintf(outfile
, "#include <linux/linkage.h>\n");
146 fprintf(outfile
, "#include <asm/page_types.h>\n");
147 fprintf(outfile
, "#include <asm/vdso.h>\n");
148 fprintf(outfile
, "\n");
150 "static unsigned char raw_data[%lu] __ro_after_init __aligned(PAGE_SIZE) = {",
152 for (j
= 0; j
< stripped_len
; j
++) {
154 fprintf(outfile
, "\n\t");
155 fprintf(outfile
, "0x%02X, ",
156 (int)((unsigned char *)stripped_addr
)[j
]);
158 fprintf(outfile
, "\n};\n\n");
160 fprintf(outfile
, "const struct vdso_image %s = {\n", name
);
161 fprintf(outfile
, "\t.data = raw_data,\n");
162 fprintf(outfile
, "\t.size = %lu,\n", mapping_size
);
164 fprintf(outfile
, "\t.alt = %lu,\n",
165 (unsigned long)GET_LE(&alt_sec
->sh_offset
));
166 fprintf(outfile
, "\t.alt_len = %lu,\n",
167 (unsigned long)GET_LE(&alt_sec
->sh_size
));
169 for (i
= 0; i
< NSYMS
; i
++) {
170 if (required_syms
[i
].export
&& syms
[i
])
171 fprintf(outfile
, "\t.sym_%s = %" PRIi64
",\n",
172 required_syms
[i
].name
, (int64_t)syms
[i
]);
174 fprintf(outfile
, "};\n");