vdso2c.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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're keep a section table for a few reasons:
  15. *
  16. * The Go runtime had a couple of bugs: it would read the section
  17. * table to try to figure out how many dynamic symbols there were (it
  18. * shouldn't have looked at the section table at all) and, if there
  19. * were no SHT_SYNDYM section table entry, it would use an
  20. * uninitialized value for the number of symbols. An empty DYNSYM
  21. * table would work, but I see no reason not to write a valid one (and
  22. * keep full performance for old Go programs). This hack is only
  23. * needed on x86_64.
  24. *
  25. * The bug was introduced on 2012-08-31 by:
  26. * https://code.google.com/p/go/source/detail?r=56ea40aac72b
  27. * and was fixed on 2014-06-13 by:
  28. * https://code.google.com/p/go/source/detail?r=fc1cd5e12595
  29. *
  30. * Binutils has issues debugging the vDSO: it reads the section table to
  31. * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which
  32. * would break build-id if we removed the section table. Binutils
  33. * also requires that shstrndx != 0. See:
  34. * https://sourceware.org/bugzilla/show_bug.cgi?id=17064
  35. *
  36. * elfutils might not look for PT_NOTE if there is a section table at
  37. * all. I don't know whether this matters for any practical purpose.
  38. *
  39. * For simplicity, rather than hacking up a partial section table, we
  40. * just write a mostly complete one. We omit non-dynamic symbols,
  41. * though, since they're rather large.
  42. *
  43. * Once binutils gets fixed, we might be able to drop this for all but
  44. * the 64-bit vdso, since build-id only works in kernel RPMs, and
  45. * systems that update to new enough kernel RPMs will likely update
  46. * binutils in sync. build-id has never worked for home-built kernel
  47. * RPMs without manual symlinking, and I suspect that no one ever does
  48. * that.
  49. */
  50. #include <inttypes.h>
  51. #include <stdint.h>
  52. #include <unistd.h>
  53. #include <stdarg.h>
  54. #include <stdlib.h>
  55. #include <stdio.h>
  56. #include <string.h>
  57. #include <fcntl.h>
  58. #include <err.h>
  59. #include <sys/mman.h>
  60. #include <sys/types.h>
  61. #include <tools/le_byteshift.h>
  62. #include <linux/elf.h>
  63. #include <linux/types.h>
  64. const char *outfilename;
  65. /* Symbols that we need in vdso2c. */
  66. enum {
  67. sym_vvar_start,
  68. sym_vvar_page,
  69. sym_hpet_page,
  70. sym_VDSO_FAKE_SECTION_TABLE_START,
  71. sym_VDSO_FAKE_SECTION_TABLE_END,
  72. };
  73. const int special_pages[] = {
  74. sym_vvar_page,
  75. sym_hpet_page,
  76. };
  77. struct vdso_sym {
  78. const char *name;
  79. bool export;
  80. };
  81. struct vdso_sym required_syms[] = {
  82. [sym_vvar_start] = {"vvar_start", true},
  83. [sym_vvar_page] = {"vvar_page", true},
  84. [sym_hpet_page] = {"hpet_page", true},
  85. [sym_VDSO_FAKE_SECTION_TABLE_START] = {
  86. "VDSO_FAKE_SECTION_TABLE_START", false
  87. },
  88. [sym_VDSO_FAKE_SECTION_TABLE_END] = {
  89. "VDSO_FAKE_SECTION_TABLE_END", false
  90. },
  91. {"VDSO32_NOTE_MASK", true},
  92. {"VDSO32_SYSENTER_RETURN", true},
  93. {"__kernel_vsyscall", true},
  94. {"__kernel_sigreturn", true},
  95. {"__kernel_rt_sigreturn", true},
  96. };
  97. __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
  98. static void fail(const char *format, ...)
  99. {
  100. va_list ap;
  101. va_start(ap, format);
  102. fprintf(stderr, "Error: ");
  103. vfprintf(stderr, format, ap);
  104. if (outfilename)
  105. unlink(outfilename);
  106. exit(1);
  107. va_end(ap);
  108. }
  109. /*
  110. * Evil macros for little-endian reads and writes
  111. */
  112. #define GLE(x, bits, ifnot) \
  113. __builtin_choose_expr( \
  114. (sizeof(*(x)) == bits/8), \
  115. (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
  116. extern void bad_get_le(void);
  117. #define LAST_GLE(x) \
  118. __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
  119. #define GET_LE(x) \
  120. GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
  121. #define PLE(x, val, bits, ifnot) \
  122. __builtin_choose_expr( \
  123. (sizeof(*(x)) == bits/8), \
  124. put_unaligned_le##bits((val), (x)), ifnot)
  125. extern void bad_put_le(void);
  126. #define LAST_PLE(x, val) \
  127. __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
  128. #define PUT_LE(x, val) \
  129. PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
  130. #define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
  131. #define BITSFUNC3(name, bits, suffix) name##bits##suffix
  132. #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
  133. #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
  134. #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
  135. #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
  136. #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
  137. #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
  138. #define ELF_BITS 64
  139. #include "vdso2c.h"
  140. #undef ELF_BITS
  141. #define ELF_BITS 32
  142. #include "vdso2c.h"
  143. #undef ELF_BITS
  144. static void go(void *raw_addr, size_t raw_len,
  145. void *stripped_addr, size_t stripped_len,
  146. FILE *outfile, const char *name)
  147. {
  148. Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
  149. if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
  150. go64(raw_addr, raw_len, stripped_addr, stripped_len,
  151. outfile, name);
  152. } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
  153. go32(raw_addr, raw_len, stripped_addr, stripped_len,
  154. outfile, name);
  155. } else {
  156. fail("unknown ELF class\n");
  157. }
  158. }
  159. static void map_input(const char *name, void **addr, size_t *len, int prot)
  160. {
  161. off_t tmp_len;
  162. int fd = open(name, O_RDONLY);
  163. if (fd == -1)
  164. err(1, "%s", name);
  165. tmp_len = lseek(fd, 0, SEEK_END);
  166. if (tmp_len == (off_t)-1)
  167. err(1, "lseek");
  168. *len = (size_t)tmp_len;
  169. *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
  170. if (*addr == MAP_FAILED)
  171. err(1, "mmap");
  172. close(fd);
  173. }
  174. int main(int argc, char **argv)
  175. {
  176. size_t raw_len, stripped_len;
  177. void *raw_addr, *stripped_addr;
  178. FILE *outfile;
  179. char *name, *tmp;
  180. int namelen;
  181. if (argc != 4) {
  182. printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
  183. return 1;
  184. }
  185. /*
  186. * Figure out the struct name. If we're writing to a .so file,
  187. * generate raw output insted.
  188. */
  189. name = strdup(argv[3]);
  190. namelen = strlen(name);
  191. if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
  192. name = NULL;
  193. } else {
  194. tmp = strrchr(name, '/');
  195. if (tmp)
  196. name = tmp + 1;
  197. tmp = strchr(name, '.');
  198. if (tmp)
  199. *tmp = '\0';
  200. for (tmp = name; *tmp; tmp++)
  201. if (*tmp == '-')
  202. *tmp = '_';
  203. }
  204. map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
  205. map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
  206. outfilename = argv[3];
  207. outfile = fopen(outfilename, "w");
  208. if (!outfile)
  209. err(1, "%s", argv[2]);
  210. go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
  211. munmap(raw_addr, raw_len);
  212. munmap(stripped_addr, stripped_len);
  213. fclose(outfile);
  214. return 0;
  215. }