genvdso.c 7.3 KB

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