genvdso.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 Imagination Technologies
  4. * Author: Alex Smith <alex.smith@imgtec.com>
  5. */
  6. /*
  7. * This tool is used to generate the real VDSO images from the raw image. It
  8. * first patches up the MIPS ABI flags and GNU attributes sections defined in
  9. * elf.S to have the correct name and type. It then generates a C source file
  10. * to be compiled into the kernel containing the VDSO image data and a
  11. * mips_vdso_image struct for it, including symbol offsets extracted from the
  12. * image.
  13. *
  14. * We need to be passed both a stripped and unstripped VDSO image. The stripped
  15. * image is compiled into the kernel, but we must also patch up the unstripped
  16. * image's ABI flags sections so that it can be installed and used for
  17. * debugging.
  18. */
  19. #include <sys/mman.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <byteswap.h>
  23. #include <elf.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #include <inttypes.h>
  27. #include <stdarg.h>
  28. #include <stdbool.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. /* Define these in case the system elf.h is not new enough to have them. */
  34. #ifndef SHT_GNU_ATTRIBUTES
  35. # define SHT_GNU_ATTRIBUTES 0x6ffffff5
  36. #endif
  37. #ifndef SHT_MIPS_ABIFLAGS
  38. # define SHT_MIPS_ABIFLAGS 0x7000002a
  39. #endif
  40. enum {
  41. ABI_O32 = (1 << 0),
  42. ABI_N32 = (1 << 1),
  43. ABI_N64 = (1 << 2),
  44. ABI_ALL = ABI_O32 | ABI_N32 | ABI_N64,
  45. };
  46. /* Symbols the kernel requires offsets for. */
  47. static struct {
  48. const char *name;
  49. const char *offset_name;
  50. unsigned int abis;
  51. } vdso_symbols[] = {
  52. { "__vdso_sigreturn", "off_sigreturn", ABI_O32 },
  53. { "__vdso_rt_sigreturn", "off_rt_sigreturn", ABI_ALL },
  54. {}
  55. };
  56. static const char *program_name;
  57. static const char *vdso_name;
  58. static unsigned char elf_class;
  59. static unsigned int elf_abi;
  60. static bool need_swap;
  61. static FILE *out_file;
  62. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  63. # define HOST_ORDER ELFDATA2LSB
  64. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  65. # define HOST_ORDER ELFDATA2MSB
  66. #endif
  67. #define BUILD_SWAP(bits) \
  68. static uint##bits##_t swap_uint##bits(uint##bits##_t val) \
  69. { \
  70. return need_swap ? bswap_##bits(val) : val; \
  71. }
  72. BUILD_SWAP(16)
  73. BUILD_SWAP(32)
  74. BUILD_SWAP(64)
  75. #define __FUNC(name, bits) name##bits
  76. #define _FUNC(name, bits) __FUNC(name, bits)
  77. #define FUNC(name) _FUNC(name, ELF_BITS)
  78. #define __ELF(x, bits) Elf##bits##_##x
  79. #define _ELF(x, bits) __ELF(x, bits)
  80. #define ELF(x) _ELF(x, ELF_BITS)
  81. /*
  82. * Include genvdso.h twice with ELF_BITS defined differently to get functions
  83. * for both ELF32 and ELF64.
  84. */
  85. #define ELF_BITS 64
  86. #include "genvdso.h"
  87. #undef ELF_BITS
  88. #define ELF_BITS 32
  89. #include "genvdso.h"
  90. #undef ELF_BITS
  91. static void *map_vdso(const char *path, size_t *_size)
  92. {
  93. int fd;
  94. struct stat stat;
  95. void *addr;
  96. const Elf32_Ehdr *ehdr;
  97. fd = open(path, O_RDWR);
  98. if (fd < 0) {
  99. fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name,
  100. path, strerror(errno));
  101. return NULL;
  102. }
  103. if (fstat(fd, &stat) != 0) {
  104. fprintf(stderr, "%s: Failed to stat '%s': %s\n", program_name,
  105. path, strerror(errno));
  106. close(fd);
  107. return NULL;
  108. }
  109. addr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
  110. 0);
  111. if (addr == MAP_FAILED) {
  112. fprintf(stderr, "%s: Failed to map '%s': %s\n", program_name,
  113. path, strerror(errno));
  114. close(fd);
  115. return NULL;
  116. }
  117. /* ELF32/64 header formats are the same for the bits we're checking. */
  118. ehdr = addr;
  119. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
  120. fprintf(stderr, "%s: '%s' is not an ELF file\n", program_name,
  121. path);
  122. close(fd);
  123. return NULL;
  124. }
  125. elf_class = ehdr->e_ident[EI_CLASS];
  126. switch (elf_class) {
  127. case ELFCLASS32:
  128. case ELFCLASS64:
  129. break;
  130. default:
  131. fprintf(stderr, "%s: '%s' has invalid ELF class\n",
  132. program_name, path);
  133. close(fd);
  134. return NULL;
  135. }
  136. switch (ehdr->e_ident[EI_DATA]) {
  137. case ELFDATA2LSB:
  138. case ELFDATA2MSB:
  139. need_swap = ehdr->e_ident[EI_DATA] != HOST_ORDER;
  140. break;
  141. default:
  142. fprintf(stderr, "%s: '%s' has invalid ELF data order\n",
  143. program_name, path);
  144. close(fd);
  145. return NULL;
  146. }
  147. if (swap_uint16(ehdr->e_machine) != EM_MIPS) {
  148. fprintf(stderr,
  149. "%s: '%s' has invalid ELF machine (expected EM_MIPS)\n",
  150. program_name, path);
  151. close(fd);
  152. return NULL;
  153. } else if (swap_uint16(ehdr->e_type) != ET_DYN) {
  154. fprintf(stderr,
  155. "%s: '%s' has invalid ELF type (expected ET_DYN)\n",
  156. program_name, path);
  157. close(fd);
  158. return NULL;
  159. }
  160. *_size = stat.st_size;
  161. close(fd);
  162. return addr;
  163. }
  164. static bool patch_vdso(const char *path, void *vdso)
  165. {
  166. if (elf_class == ELFCLASS64)
  167. return patch_vdso64(path, vdso);
  168. else
  169. return patch_vdso32(path, vdso);
  170. }
  171. static bool get_symbols(const char *path, void *vdso)
  172. {
  173. if (elf_class == ELFCLASS64)
  174. return get_symbols64(path, vdso);
  175. else
  176. return get_symbols32(path, vdso);
  177. }
  178. int main(int argc, char **argv)
  179. {
  180. const char *dbg_vdso_path, *vdso_path, *out_path;
  181. void *dbg_vdso, *vdso;
  182. size_t dbg_vdso_size, vdso_size, i;
  183. program_name = argv[0];
  184. if (argc < 4 || argc > 5) {
  185. fprintf(stderr,
  186. "Usage: %s <debug VDSO> <stripped VDSO> <output file> [<name>]\n",
  187. program_name);
  188. return EXIT_FAILURE;
  189. }
  190. dbg_vdso_path = argv[1];
  191. vdso_path = argv[2];
  192. out_path = argv[3];
  193. vdso_name = (argc > 4) ? argv[4] : "";
  194. dbg_vdso = map_vdso(dbg_vdso_path, &dbg_vdso_size);
  195. if (!dbg_vdso)
  196. return EXIT_FAILURE;
  197. vdso = map_vdso(vdso_path, &vdso_size);
  198. if (!vdso)
  199. return EXIT_FAILURE;
  200. /* Patch both the VDSOs' ABI flags sections. */
  201. if (!patch_vdso(dbg_vdso_path, dbg_vdso))
  202. return EXIT_FAILURE;
  203. if (!patch_vdso(vdso_path, vdso))
  204. return EXIT_FAILURE;
  205. if (msync(dbg_vdso, dbg_vdso_size, MS_SYNC) != 0) {
  206. fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
  207. dbg_vdso_path, strerror(errno));
  208. return EXIT_FAILURE;
  209. } else if (msync(vdso, vdso_size, MS_SYNC) != 0) {
  210. fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
  211. vdso_path, strerror(errno));
  212. return EXIT_FAILURE;
  213. }
  214. out_file = fopen(out_path, "w");
  215. if (!out_file) {
  216. fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name,
  217. out_path, strerror(errno));
  218. return EXIT_FAILURE;
  219. }
  220. fprintf(out_file, "/* Automatically generated - do not edit */\n");
  221. fprintf(out_file, "#include <linux/linkage.h>\n");
  222. fprintf(out_file, "#include <linux/mm.h>\n");
  223. fprintf(out_file, "#include <asm/vdso.h>\n");
  224. fprintf(out_file, "static int vdso_mremap(\n");
  225. fprintf(out_file, " const struct vm_special_mapping *sm,\n");
  226. fprintf(out_file, " struct vm_area_struct *new_vma)\n");
  227. fprintf(out_file, "{\n");
  228. fprintf(out_file, " unsigned long new_size =\n");
  229. fprintf(out_file, " new_vma->vm_end - new_vma->vm_start;\n");
  230. fprintf(out_file, " if (vdso_image.size != new_size)\n");
  231. fprintf(out_file, " return -EINVAL;\n");
  232. fprintf(out_file, " current->mm->context.vdso =\n");
  233. fprintf(out_file, " (void *)(new_vma->vm_start);\n");
  234. fprintf(out_file, " return 0;\n");
  235. fprintf(out_file, "}\n");
  236. /* Write out the stripped VDSO data. */
  237. fprintf(out_file,
  238. "static unsigned char vdso_data[PAGE_ALIGN(%zu)] __page_aligned_data = {\n\t",
  239. vdso_size);
  240. for (i = 0; i < vdso_size; i++) {
  241. if (!(i % 10))
  242. fprintf(out_file, "\n\t");
  243. fprintf(out_file, "0x%02x, ", ((unsigned char *)vdso)[i]);
  244. }
  245. fprintf(out_file, "\n};\n");
  246. /* Preallocate a page array. */
  247. fprintf(out_file,
  248. "static struct page *vdso_pages[PAGE_ALIGN(%zu) / PAGE_SIZE];\n",
  249. vdso_size);
  250. fprintf(out_file, "struct mips_vdso_image vdso_image%s%s = {\n",
  251. (vdso_name[0]) ? "_" : "", vdso_name);
  252. fprintf(out_file, "\t.data = vdso_data,\n");
  253. fprintf(out_file, "\t.size = PAGE_ALIGN(%zu),\n", vdso_size);
  254. fprintf(out_file, "\t.mapping = {\n");
  255. fprintf(out_file, "\t\t.name = \"[vdso]\",\n");
  256. fprintf(out_file, "\t\t.pages = vdso_pages,\n");
  257. fprintf(out_file, "\t\t.mremap = vdso_mremap,\n");
  258. fprintf(out_file, "\t},\n");
  259. /* Calculate and write symbol offsets to <output file> */
  260. if (!get_symbols(dbg_vdso_path, dbg_vdso)) {
  261. unlink(out_path);
  262. fclose(out_file);
  263. return EXIT_FAILURE;
  264. }
  265. fprintf(out_file, "};\n");
  266. fclose(out_file);
  267. return EXIT_SUCCESS;
  268. }