gen-vdso.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Post-process a vdso elf image for inclusion into qemu.
  3. *
  4. * Copyright 2023 Linaro, Ltd.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0-or-later
  7. */
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <endian.h>
  15. #include <unistd.h>
  16. #include "elf.h"
  17. #define bswap_(p) _Generic(*(p), \
  18. uint16_t: __builtin_bswap16, \
  19. uint32_t: __builtin_bswap32, \
  20. uint64_t: __builtin_bswap64, \
  21. int16_t: __builtin_bswap16, \
  22. int32_t: __builtin_bswap32, \
  23. int64_t: __builtin_bswap64)
  24. #define bswaps(p) (*(p) = bswap_(p)(*(p)))
  25. static void output_reloc(FILE *outf, void *buf, void *loc)
  26. {
  27. fprintf(outf, " 0x%08tx,\n", loc - buf);
  28. }
  29. static const char *sigreturn_sym;
  30. static const char *rt_sigreturn_sym;
  31. static unsigned sigreturn_addr;
  32. static unsigned rt_sigreturn_addr;
  33. #define N 32
  34. #define elfN(x) elf32_##x
  35. #define ElfN(x) Elf32_##x
  36. #include "gen-vdso-elfn.c.inc"
  37. #undef N
  38. #undef elfN
  39. #undef ElfN
  40. #define N 64
  41. #define elfN(x) elf64_##x
  42. #define ElfN(x) Elf64_##x
  43. #include "gen-vdso-elfn.c.inc"
  44. #undef N
  45. #undef elfN
  46. #undef ElfN
  47. int main(int argc, char **argv)
  48. {
  49. FILE *inf, *outf;
  50. long total_len;
  51. const char *prefix = "vdso";
  52. const char *inf_name;
  53. const char *outf_name = NULL;
  54. unsigned char *buf;
  55. bool need_bswap;
  56. while (1) {
  57. int opt = getopt(argc, argv, "o:p:r:s:");
  58. if (opt < 0) {
  59. break;
  60. }
  61. switch (opt) {
  62. case 'o':
  63. outf_name = optarg;
  64. break;
  65. case 'p':
  66. prefix = optarg;
  67. break;
  68. case 'r':
  69. rt_sigreturn_sym = optarg;
  70. break;
  71. case 's':
  72. sigreturn_sym = optarg;
  73. break;
  74. default:
  75. usage:
  76. fprintf(stderr, "usage: [-p prefix] [-r rt-sigreturn-name] "
  77. "[-s sigreturn-name] -o output-file input-file\n");
  78. return EXIT_FAILURE;
  79. }
  80. }
  81. if (optind >= argc || outf_name == NULL) {
  82. goto usage;
  83. }
  84. inf_name = argv[optind];
  85. /*
  86. * Open the input and output files.
  87. */
  88. inf = fopen(inf_name, "rb");
  89. if (inf == NULL) {
  90. goto perror_inf;
  91. }
  92. outf = fopen(outf_name, "w");
  93. if (outf == NULL) {
  94. goto perror_outf;
  95. }
  96. /*
  97. * Read the input file into a buffer.
  98. * We expect the vdso to be small, on the order of one page,
  99. * therefore we do not expect a partial read.
  100. */
  101. fseek(inf, 0, SEEK_END);
  102. total_len = ftell(inf);
  103. fseek(inf, 0, SEEK_SET);
  104. buf = malloc(total_len);
  105. if (buf == NULL) {
  106. goto perror_inf;
  107. }
  108. errno = 0;
  109. if (fread(buf, 1, total_len, inf) != total_len) {
  110. if (errno) {
  111. goto perror_inf;
  112. }
  113. fprintf(stderr, "%s: incomplete read\n", inf_name);
  114. return EXIT_FAILURE;
  115. }
  116. fclose(inf);
  117. /*
  118. * Identify which elf flavor we're processing.
  119. * The first 16 bytes of the file are e_ident.
  120. */
  121. if (buf[EI_MAG0] != ELFMAG0 || buf[EI_MAG1] != ELFMAG1 ||
  122. buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3) {
  123. fprintf(stderr, "%s: not an elf file\n", inf_name);
  124. return EXIT_FAILURE;
  125. }
  126. switch (buf[EI_DATA]) {
  127. case ELFDATA2LSB:
  128. need_bswap = BYTE_ORDER != LITTLE_ENDIAN;
  129. break;
  130. case ELFDATA2MSB:
  131. need_bswap = BYTE_ORDER != BIG_ENDIAN;
  132. break;
  133. default:
  134. fprintf(stderr, "%s: invalid elf EI_DATA (%u)\n",
  135. inf_name, buf[EI_DATA]);
  136. return EXIT_FAILURE;
  137. }
  138. /*
  139. * We need to relocate the VDSO image. The one built into the kernel
  140. * is built for a fixed address. The one we built for QEMU is not,
  141. * since that requires close control of the guest address space.
  142. *
  143. * Output relocation addresses as we go.
  144. */
  145. fprintf(outf,
  146. "/* Automatically generated by linux-user/gen-vdso.c. */\n"
  147. "\n"
  148. "static const unsigned %s_relocs[] = {\n", prefix);
  149. switch (buf[EI_CLASS]) {
  150. case ELFCLASS32:
  151. elf32_process(outf, buf, total_len, need_bswap);
  152. break;
  153. case ELFCLASS64:
  154. elf64_process(outf, buf, total_len, need_bswap);
  155. break;
  156. default:
  157. fprintf(stderr, "%s: invalid elf EI_CLASS (%u)\n",
  158. inf_name, buf[EI_CLASS]);
  159. return EXIT_FAILURE;
  160. }
  161. fprintf(outf, "};\n\n"); /* end vdso_relocs. */
  162. /*
  163. * Write out the vdso image now, after we made local changes.
  164. */
  165. fprintf(outf,
  166. "static const uint8_t %s_image[] = {",
  167. prefix);
  168. for (long i = 0; i < total_len; ++i) {
  169. if (i % 12 == 0) {
  170. fputs("\n ", outf);
  171. }
  172. fprintf(outf, " 0x%02x,", buf[i]);
  173. }
  174. fprintf(outf, "\n};\n\n");
  175. fprintf(outf, "static const VdsoImageInfo %s_image_info = {\n", prefix);
  176. fprintf(outf, " .image = %s_image,\n", prefix);
  177. fprintf(outf, " .relocs = %s_relocs,\n", prefix);
  178. fprintf(outf, " .image_size = sizeof(%s_image),\n", prefix);
  179. fprintf(outf, " .reloc_count = ARRAY_SIZE(%s_relocs),\n", prefix);
  180. fprintf(outf, " .sigreturn_ofs = 0x%x,\n", sigreturn_addr);
  181. fprintf(outf, " .rt_sigreturn_ofs = 0x%x,\n", rt_sigreturn_addr);
  182. fprintf(outf, "};\n");
  183. /*
  184. * Everything should have gone well.
  185. */
  186. if (fclose(outf)) {
  187. goto perror_outf;
  188. }
  189. return EXIT_SUCCESS;
  190. perror_inf:
  191. perror(inf_name);
  192. return EXIT_FAILURE;
  193. perror_outf:
  194. perror(outf_name);
  195. return EXIT_FAILURE;
  196. }