2 * Post-process a vdso elf image for inclusion into qemu.
4 * Copyright 2023 Linaro, Ltd.
6 * SPDX-License-Identifier: GPL-2.0-or-later
20 #define bswap_(p) _Generic(*(p), \
21 uint16_t: __builtin_bswap16, \
22 uint32_t: __builtin_bswap32, \
23 uint64_t: __builtin_bswap64, \
24 int16_t: __builtin_bswap16, \
25 int32_t: __builtin_bswap32, \
26 int64_t: __builtin_bswap64)
27 #define bswaps(p) (*(p) = bswap_(p)(*(p)))
29 static void output_reloc(FILE *outf
, void *buf
, void *loc
)
31 fprintf(outf
, " 0x%08tx,\n", loc
- buf
);
34 static const char *sigreturn_sym
;
35 static const char *rt_sigreturn_sym
;
37 static unsigned sigreturn_addr
;
38 static unsigned rt_sigreturn_addr
;
41 #define elfN(x) elf32_##x
42 #define ElfN(x) Elf32_##x
43 #include "gen-vdso-elfn.c.inc"
49 #define elfN(x) elf64_##x
50 #define ElfN(x) Elf64_##x
51 #include "gen-vdso-elfn.c.inc"
57 int main(int argc
, char **argv
)
61 const char *prefix
= "vdso";
63 const char *outf_name
= NULL
;
68 int opt
= getopt(argc
, argv
, "o:p:r:s:");
80 rt_sigreturn_sym
= optarg
;
83 sigreturn_sym
= optarg
;
87 fprintf(stderr
, "usage: [-p prefix] [-r rt-sigreturn-name] "
88 "[-s sigreturn-name] -o output-file input-file\n");
93 if (optind
>= argc
|| outf_name
== NULL
) {
96 inf_name
= argv
[optind
];
99 * Open the input and output files.
101 inf
= fopen(inf_name
, "rb");
105 outf
= fopen(outf_name
, "w");
111 * Read the input file into a buffer.
112 * We expect the vdso to be small, on the order of one page,
113 * therefore we do not expect a partial read.
115 fseek(inf
, 0, SEEK_END
);
116 total_len
= ftell(inf
);
117 fseek(inf
, 0, SEEK_SET
);
119 buf
= malloc(total_len
);
125 if (fread(buf
, 1, total_len
, inf
) != total_len
) {
129 fprintf(stderr
, "%s: incomplete read\n", inf_name
);
135 * Write out the vdso image now, before we make local changes.
139 "/* Automatically generated from linux-user/gen-vdso.c. */\n"
141 "static const uint8_t %s_image[] = {",
143 for (long i
= 0; i
< total_len
; ++i
) {
147 fprintf(outf
, " 0x%02x,", buf
[i
]);
149 fprintf(outf
, "\n};\n\n");
152 * Identify which elf flavor we're processing.
153 * The first 16 bytes of the file are e_ident.
156 if (buf
[EI_MAG0
] != ELFMAG0
|| buf
[EI_MAG1
] != ELFMAG1
||
157 buf
[EI_MAG2
] != ELFMAG2
|| buf
[EI_MAG3
] != ELFMAG3
) {
158 fprintf(stderr
, "%s: not an elf file\n", inf_name
);
161 switch (buf
[EI_DATA
]) {
163 need_bswap
= BYTE_ORDER
!= LITTLE_ENDIAN
;
166 need_bswap
= BYTE_ORDER
!= BIG_ENDIAN
;
169 fprintf(stderr
, "%s: invalid elf EI_DATA (%u)\n",
170 inf_name
, buf
[EI_DATA
]);
175 * We need to relocate the VDSO image. The one built into the kernel
176 * is built for a fixed address. The one we built for QEMU is not,
177 * since that requires close control of the guest address space.
179 * Output relocation addresses as we go.
182 fprintf(outf
, "static const unsigned %s_relocs[] = {\n", prefix
);
184 switch (buf
[EI_CLASS
]) {
186 elf32_process(outf
, buf
, need_bswap
);
189 elf64_process(outf
, buf
, need_bswap
);
192 fprintf(stderr
, "%s: invalid elf EI_CLASS (%u)\n",
193 inf_name
, buf
[EI_CLASS
]);
197 fprintf(outf
, "};\n\n"); /* end vdso_relocs. */
199 fprintf(outf
, "static const VdsoImageInfo %s_image_info = {\n", prefix
);
200 fprintf(outf
, " .image = %s_image,\n", prefix
);
201 fprintf(outf
, " .relocs = %s_relocs,\n", prefix
);
202 fprintf(outf
, " .image_size = sizeof(%s_image),\n", prefix
);
203 fprintf(outf
, " .reloc_count = ARRAY_SIZE(%s_relocs),\n", prefix
);
204 fprintf(outf
, " .sigreturn_ofs = 0x%x,\n", sigreturn_addr
);
205 fprintf(outf
, " .rt_sigreturn_ofs = 0x%x,\n", rt_sigreturn_addr
);
206 fprintf(outf
, "};\n");
209 * Everything should have gone well.