vdso2c.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * vdso2c - A vdso image preparation tool
  3. * Copyright (c) 2014 Andy Lutomirski and others
  4. * Licensed under the GPL v2
  5. *
  6. * vdso2c requires stripped and unstripped input. It would be trivial
  7. * to fully strip the input in here, but, for reasons described below,
  8. * we need to write a section table. Doing this is more or less
  9. * equivalent to dropping all non-allocatable sections, but it's
  10. * easier to let objcopy handle that instead of doing it ourselves.
  11. * If we ever need to do something fancier than what objcopy provides,
  12. * it would be straightforward to add here.
  13. *
  14. * We keep a section table for a few reasons:
  15. *
  16. * Binutils has issues debugging the vDSO: it reads the section table to
  17. * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which
  18. * would break build-id if we removed the section table. Binutils
  19. * also requires that shstrndx != 0. See:
  20. * https://sourceware.org/bugzilla/show_bug.cgi?id=17064
  21. *
  22. * elfutils might not look for PT_NOTE if there is a section table at
  23. * all. I don't know whether this matters for any practical purpose.
  24. *
  25. * For simplicity, rather than hacking up a partial section table, we
  26. * just write a mostly complete one. We omit non-dynamic symbols,
  27. * though, since they're rather large.
  28. *
  29. * Once binutils gets fixed, we might be able to drop this for all but
  30. * the 64-bit vdso, since build-id only works in kernel RPMs, and
  31. * systems that update to new enough kernel RPMs will likely update
  32. * binutils in sync. build-id has never worked for home-built kernel
  33. * RPMs without manual symlinking, and I suspect that no one ever does
  34. * that.
  35. */
  36. /*
  37. * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
  38. */
  39. #include <inttypes.h>
  40. #include <stdint.h>
  41. #include <unistd.h>
  42. #include <stdarg.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <fcntl.h>
  47. #include <err.h>
  48. #include <sys/mman.h>
  49. #include <sys/types.h>
  50. #include <tools/be_byteshift.h>
  51. #include <linux/elf.h>
  52. #include <linux/types.h>
  53. #include <linux/kernel.h>
  54. const char *outfilename;
  55. /* Symbols that we need in vdso2c. */
  56. enum {
  57. sym_vvar_start,
  58. sym_VDSO_FAKE_SECTION_TABLE_START,
  59. sym_VDSO_FAKE_SECTION_TABLE_END,
  60. sym_vread_tick,
  61. sym_vread_tick_patch_start,
  62. sym_vread_tick_patch_end
  63. };
  64. struct vdso_sym {
  65. const char *name;
  66. int export;
  67. };
  68. struct vdso_sym required_syms[] = {
  69. [sym_vvar_start] = {"vvar_start", 1},
  70. [sym_VDSO_FAKE_SECTION_TABLE_START] = {
  71. "VDSO_FAKE_SECTION_TABLE_START", 0
  72. },
  73. [sym_VDSO_FAKE_SECTION_TABLE_END] = {
  74. "VDSO_FAKE_SECTION_TABLE_END", 0
  75. },
  76. [sym_vread_tick] = {"vread_tick", 1},
  77. [sym_vread_tick_patch_start] = {"vread_tick_patch_start", 1},
  78. [sym_vread_tick_patch_end] = {"vread_tick_patch_end", 1}
  79. };
  80. __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
  81. static void fail(const char *format, ...)
  82. {
  83. va_list ap;
  84. va_start(ap, format);
  85. fprintf(stderr, "Error: ");
  86. vfprintf(stderr, format, ap);
  87. if (outfilename)
  88. unlink(outfilename);
  89. exit(1);
  90. va_end(ap);
  91. }
  92. /*
  93. * Evil macros for big-endian reads and writes
  94. */
  95. #define GBE(x, bits, ifnot) \
  96. __builtin_choose_expr( \
  97. (sizeof(*(x)) == bits/8), \
  98. (__typeof__(*(x)))get_unaligned_be##bits(x), ifnot)
  99. #define LAST_GBE(x) \
  100. __builtin_choose_expr(sizeof(*(x)) == 1, *(x), (void)(0))
  101. #define GET_BE(x) \
  102. GBE(x, 64, GBE(x, 32, GBE(x, 16, LAST_GBE(x))))
  103. #define PBE(x, val, bits, ifnot) \
  104. __builtin_choose_expr( \
  105. (sizeof(*(x)) == bits/8), \
  106. put_unaligned_be##bits((val), (x)), ifnot)
  107. #define LAST_PBE(x, val) \
  108. __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), (void)(0))
  109. #define PUT_BE(x, val) \
  110. PBE(x, val, 64, PBE(x, val, 32, PBE(x, val, 16, LAST_PBE(x, val))))
  111. #define NSYMS ARRAY_SIZE(required_syms)
  112. #define BITSFUNC3(name, bits, suffix) name##bits##suffix
  113. #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
  114. #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
  115. #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
  116. #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
  117. #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
  118. #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
  119. #define ELF_BITS 64
  120. #include "vdso2c.h"
  121. #undef ELF_BITS
  122. #define ELF_BITS 32
  123. #include "vdso2c.h"
  124. #undef ELF_BITS
  125. static void go(void *raw_addr, size_t raw_len,
  126. void *stripped_addr, size_t stripped_len,
  127. FILE *outfile, const char *name)
  128. {
  129. Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
  130. if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
  131. go64(raw_addr, raw_len, stripped_addr, stripped_len,
  132. outfile, name);
  133. } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
  134. go32(raw_addr, raw_len, stripped_addr, stripped_len,
  135. outfile, name);
  136. } else {
  137. fail("unknown ELF class\n");
  138. }
  139. }
  140. static void map_input(const char *name, void **addr, size_t *len, int prot)
  141. {
  142. off_t tmp_len;
  143. int fd = open(name, O_RDONLY);
  144. if (fd == -1)
  145. err(1, "%s", name);
  146. tmp_len = lseek(fd, 0, SEEK_END);
  147. if (tmp_len == (off_t)-1)
  148. err(1, "lseek");
  149. *len = (size_t)tmp_len;
  150. *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
  151. if (*addr == MAP_FAILED)
  152. err(1, "mmap");
  153. close(fd);
  154. }
  155. int main(int argc, char **argv)
  156. {
  157. size_t raw_len, stripped_len;
  158. void *raw_addr, *stripped_addr;
  159. FILE *outfile;
  160. char *name, *tmp;
  161. int namelen;
  162. if (argc != 4) {
  163. printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
  164. return 1;
  165. }
  166. /*
  167. * Figure out the struct name. If we're writing to a .so file,
  168. * generate raw output insted.
  169. */
  170. name = strdup(argv[3]);
  171. namelen = strlen(name);
  172. if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
  173. name = NULL;
  174. } else {
  175. tmp = strrchr(name, '/');
  176. if (tmp)
  177. name = tmp + 1;
  178. tmp = strchr(name, '.');
  179. if (tmp)
  180. *tmp = '\0';
  181. for (tmp = name; *tmp; tmp++)
  182. if (*tmp == '-')
  183. *tmp = '_';
  184. }
  185. map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
  186. map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
  187. outfilename = argv[3];
  188. outfile = fopen(outfilename, "w");
  189. if (!outfile)
  190. err(1, "%s", argv[2]);
  191. go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
  192. munmap(raw_addr, raw_len);
  193. munmap(stripped_addr, stripped_len);
  194. fclose(outfile);
  195. return 0;
  196. }