genvdso.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. close(fd);
  111. return NULL;
  112. }
  113. addr = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
  114. 0);
  115. if (addr == MAP_FAILED) {
  116. fprintf(stderr, "%s: Failed to map '%s': %s\n", program_name,
  117. path, strerror(errno));
  118. close(fd);
  119. return NULL;
  120. }
  121. /* ELF32/64 header formats are the same for the bits we're checking. */
  122. ehdr = addr;
  123. if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0) {
  124. fprintf(stderr, "%s: '%s' is not an ELF file\n", program_name,
  125. path);
  126. close(fd);
  127. return NULL;
  128. }
  129. elf_class = ehdr->e_ident[EI_CLASS];
  130. switch (elf_class) {
  131. case ELFCLASS32:
  132. case ELFCLASS64:
  133. break;
  134. default:
  135. fprintf(stderr, "%s: '%s' has invalid ELF class\n",
  136. program_name, path);
  137. close(fd);
  138. return NULL;
  139. }
  140. switch (ehdr->e_ident[EI_DATA]) {
  141. case ELFDATA2LSB:
  142. case ELFDATA2MSB:
  143. need_swap = ehdr->e_ident[EI_DATA] != HOST_ORDER;
  144. break;
  145. default:
  146. fprintf(stderr, "%s: '%s' has invalid ELF data order\n",
  147. program_name, path);
  148. close(fd);
  149. return NULL;
  150. }
  151. if (swap_uint16(ehdr->e_machine) != EM_MIPS) {
  152. fprintf(stderr,
  153. "%s: '%s' has invalid ELF machine (expected EM_MIPS)\n",
  154. program_name, path);
  155. close(fd);
  156. return NULL;
  157. } else if (swap_uint16(ehdr->e_type) != ET_DYN) {
  158. fprintf(stderr,
  159. "%s: '%s' has invalid ELF type (expected ET_DYN)\n",
  160. program_name, path);
  161. close(fd);
  162. return NULL;
  163. }
  164. *_size = stat.st_size;
  165. close(fd);
  166. return addr;
  167. }
  168. static bool patch_vdso(const char *path, void *vdso)
  169. {
  170. if (elf_class == ELFCLASS64)
  171. return patch_vdso64(path, vdso);
  172. else
  173. return patch_vdso32(path, vdso);
  174. }
  175. static bool get_symbols(const char *path, void *vdso)
  176. {
  177. if (elf_class == ELFCLASS64)
  178. return get_symbols64(path, vdso);
  179. else
  180. return get_symbols32(path, vdso);
  181. }
  182. int main(int argc, char **argv)
  183. {
  184. const char *dbg_vdso_path, *vdso_path, *out_path;
  185. void *dbg_vdso, *vdso;
  186. size_t dbg_vdso_size, vdso_size, i;
  187. program_name = argv[0];
  188. if (argc < 4 || argc > 5) {
  189. fprintf(stderr,
  190. "Usage: %s <debug VDSO> <stripped VDSO> <output file> [<name>]\n",
  191. program_name);
  192. return EXIT_FAILURE;
  193. }
  194. dbg_vdso_path = argv[1];
  195. vdso_path = argv[2];
  196. out_path = argv[3];
  197. vdso_name = (argc > 4) ? argv[4] : "";
  198. dbg_vdso = map_vdso(dbg_vdso_path, &dbg_vdso_size);
  199. if (!dbg_vdso)
  200. return EXIT_FAILURE;
  201. vdso = map_vdso(vdso_path, &vdso_size);
  202. if (!vdso)
  203. return EXIT_FAILURE;
  204. /* Patch both the VDSOs' ABI flags sections. */
  205. if (!patch_vdso(dbg_vdso_path, dbg_vdso))
  206. return EXIT_FAILURE;
  207. if (!patch_vdso(vdso_path, vdso))
  208. return EXIT_FAILURE;
  209. if (msync(dbg_vdso, dbg_vdso_size, MS_SYNC) != 0) {
  210. fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
  211. dbg_vdso_path, strerror(errno));
  212. return EXIT_FAILURE;
  213. } else if (msync(vdso, vdso_size, MS_SYNC) != 0) {
  214. fprintf(stderr, "%s: Failed to sync '%s': %s\n", program_name,
  215. vdso_path, strerror(errno));
  216. return EXIT_FAILURE;
  217. }
  218. out_file = fopen(out_path, "w");
  219. if (!out_file) {
  220. fprintf(stderr, "%s: Failed to open '%s': %s\n", program_name,
  221. out_path, strerror(errno));
  222. return EXIT_FAILURE;
  223. }
  224. fprintf(out_file, "/* Automatically generated - do not edit */\n");
  225. fprintf(out_file, "#include <linux/linkage.h>\n");
  226. fprintf(out_file, "#include <linux/mm.h>\n");
  227. fprintf(out_file, "#include <asm/vdso.h>\n");
  228. /* Write out the stripped VDSO data. */
  229. fprintf(out_file,
  230. "static unsigned char vdso_data[PAGE_ALIGN(%zu)] __page_aligned_data = {\n\t",
  231. vdso_size);
  232. for (i = 0; i < vdso_size; i++) {
  233. if (!(i % 10))
  234. fprintf(out_file, "\n\t");
  235. fprintf(out_file, "0x%02x, ", ((unsigned char *)vdso)[i]);
  236. }
  237. fprintf(out_file, "\n};\n");
  238. /* Preallocate a page array. */
  239. fprintf(out_file,
  240. "static struct page *vdso_pages[PAGE_ALIGN(%zu) / PAGE_SIZE];\n",
  241. vdso_size);
  242. fprintf(out_file, "struct mips_vdso_image vdso_image%s%s = {\n",
  243. (vdso_name[0]) ? "_" : "", vdso_name);
  244. fprintf(out_file, "\t.data = vdso_data,\n");
  245. fprintf(out_file, "\t.size = PAGE_ALIGN(%zu),\n", vdso_size);
  246. fprintf(out_file, "\t.mapping = {\n");
  247. fprintf(out_file, "\t\t.name = \"[vdso]\",\n");
  248. fprintf(out_file, "\t\t.pages = vdso_pages,\n");
  249. fprintf(out_file, "\t},\n");
  250. /* Calculate and write symbol offsets to <output file> */
  251. if (!get_symbols(dbg_vdso_path, dbg_vdso)) {
  252. unlink(out_path);
  253. fclose(out_file);
  254. return EXIT_FAILURE;
  255. }
  256. fprintf(out_file, "};\n");
  257. fclose(out_file);
  258. return EXIT_SUCCESS;
  259. }