vdso2.patch 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. diff -Nur linux-4.14.44.orig/arch/x86/entry/vdso/vdso2c.c linux-4.14.44/arch/x86/entry/vdso/vdso2c.c
  2. --- linux-4.14.44.orig/arch/x86/entry/vdso/vdso2c.c 2018-05-25 16:18:02.000000000 +0200
  3. +++ linux-4.14.44/arch/x86/entry/vdso/vdso2c.c 2018-05-31 02:09:00.000000000 +0200
  4. @@ -64,7 +64,15 @@
  5. #include <tools/le_byteshift.h>
  6. #include <linux/elf.h>
  7. +#ifdef __linux__
  8. #include <linux/types.h>
  9. +#else
  10. +typedef _Bool bool;
  11. +enum {
  12. + false = 0,
  13. + true = 1
  14. +};
  15. +#endif
  16. const char *outfilename;
  17. @@ -86,6 +94,7 @@
  18. sym_hvclock_page,
  19. };
  20. +
  21. struct vdso_sym {
  22. const char *name;
  23. bool export;
  24. diff -Nur linux-4.14.44.orig/arch/x86/entry/vdso/vdso2c.c.orig linux-4.14.44/arch/x86/entry/vdso/vdso2c.c.orig
  25. --- linux-4.14.44.orig/arch/x86/entry/vdso/vdso2c.c.orig 1970-01-01 01:00:00.000000000 +0100
  26. +++ linux-4.14.44/arch/x86/entry/vdso/vdso2c.c.orig 2018-05-25 16:18:02.000000000 +0200
  27. @@ -0,0 +1,259 @@
  28. +/*
  29. + * vdso2c - A vdso image preparation tool
  30. + * Copyright (c) 2014 Andy Lutomirski and others
  31. + * Licensed under the GPL v2
  32. + *
  33. + * vdso2c requires stripped and unstripped input. It would be trivial
  34. + * to fully strip the input in here, but, for reasons described below,
  35. + * we need to write a section table. Doing this is more or less
  36. + * equivalent to dropping all non-allocatable sections, but it's
  37. + * easier to let objcopy handle that instead of doing it ourselves.
  38. + * If we ever need to do something fancier than what objcopy provides,
  39. + * it would be straightforward to add here.
  40. + *
  41. + * We're keep a section table for a few reasons:
  42. + *
  43. + * The Go runtime had a couple of bugs: it would read the section
  44. + * table to try to figure out how many dynamic symbols there were (it
  45. + * shouldn't have looked at the section table at all) and, if there
  46. + * were no SHT_SYNDYM section table entry, it would use an
  47. + * uninitialized value for the number of symbols. An empty DYNSYM
  48. + * table would work, but I see no reason not to write a valid one (and
  49. + * keep full performance for old Go programs). This hack is only
  50. + * needed on x86_64.
  51. + *
  52. + * The bug was introduced on 2012-08-31 by:
  53. + * https://code.google.com/p/go/source/detail?r=56ea40aac72b
  54. + * and was fixed on 2014-06-13 by:
  55. + * https://code.google.com/p/go/source/detail?r=fc1cd5e12595
  56. + *
  57. + * Binutils has issues debugging the vDSO: it reads the section table to
  58. + * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which
  59. + * would break build-id if we removed the section table. Binutils
  60. + * also requires that shstrndx != 0. See:
  61. + * https://sourceware.org/bugzilla/show_bug.cgi?id=17064
  62. + *
  63. + * elfutils might not look for PT_NOTE if there is a section table at
  64. + * all. I don't know whether this matters for any practical purpose.
  65. + *
  66. + * For simplicity, rather than hacking up a partial section table, we
  67. + * just write a mostly complete one. We omit non-dynamic symbols,
  68. + * though, since they're rather large.
  69. + *
  70. + * Once binutils gets fixed, we might be able to drop this for all but
  71. + * the 64-bit vdso, since build-id only works in kernel RPMs, and
  72. + * systems that update to new enough kernel RPMs will likely update
  73. + * binutils in sync. build-id has never worked for home-built kernel
  74. + * RPMs without manual symlinking, and I suspect that no one ever does
  75. + * that.
  76. + */
  77. +
  78. +#include <inttypes.h>
  79. +#include <stdint.h>
  80. +#include <unistd.h>
  81. +#include <stdarg.h>
  82. +#include <stdlib.h>
  83. +#include <stdio.h>
  84. +#include <string.h>
  85. +#include <fcntl.h>
  86. +#include <err.h>
  87. +
  88. +#include <sys/mman.h>
  89. +#include <sys/types.h>
  90. +
  91. +#include <tools/le_byteshift.h>
  92. +
  93. +#include <linux/elf.h>
  94. +#include <linux/types.h>
  95. +
  96. +const char *outfilename;
  97. +
  98. +/* Symbols that we need in vdso2c. */
  99. +enum {
  100. + sym_vvar_start,
  101. + sym_vvar_page,
  102. + sym_hpet_page,
  103. + sym_pvclock_page,
  104. + sym_hvclock_page,
  105. + sym_VDSO_FAKE_SECTION_TABLE_START,
  106. + sym_VDSO_FAKE_SECTION_TABLE_END,
  107. +};
  108. +
  109. +const int special_pages[] = {
  110. + sym_vvar_page,
  111. + sym_hpet_page,
  112. + sym_pvclock_page,
  113. + sym_hvclock_page,
  114. +};
  115. +
  116. +struct vdso_sym {
  117. + const char *name;
  118. + bool export;
  119. +};
  120. +
  121. +struct vdso_sym required_syms[] = {
  122. + [sym_vvar_start] = {"vvar_start", true},
  123. + [sym_vvar_page] = {"vvar_page", true},
  124. + [sym_hpet_page] = {"hpet_page", true},
  125. + [sym_pvclock_page] = {"pvclock_page", true},
  126. + [sym_hvclock_page] = {"hvclock_page", true},
  127. + [sym_VDSO_FAKE_SECTION_TABLE_START] = {
  128. + "VDSO_FAKE_SECTION_TABLE_START", false
  129. + },
  130. + [sym_VDSO_FAKE_SECTION_TABLE_END] = {
  131. + "VDSO_FAKE_SECTION_TABLE_END", false
  132. + },
  133. + {"VDSO32_NOTE_MASK", true},
  134. + {"__kernel_vsyscall", true},
  135. + {"__kernel_sigreturn", true},
  136. + {"__kernel_rt_sigreturn", true},
  137. + {"int80_landing_pad", true},
  138. +};
  139. +
  140. +__attribute__((format(printf, 1, 2))) __attribute__((noreturn))
  141. +static void fail(const char *format, ...)
  142. +{
  143. + va_list ap;
  144. + va_start(ap, format);
  145. + fprintf(stderr, "Error: ");
  146. + vfprintf(stderr, format, ap);
  147. + if (outfilename)
  148. + unlink(outfilename);
  149. + exit(1);
  150. + va_end(ap);
  151. +}
  152. +
  153. +/*
  154. + * Evil macros for little-endian reads and writes
  155. + */
  156. +#define GLE(x, bits, ifnot) \
  157. + __builtin_choose_expr( \
  158. + (sizeof(*(x)) == bits/8), \
  159. + (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
  160. +
  161. +extern void bad_get_le(void);
  162. +#define LAST_GLE(x) \
  163. + __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
  164. +
  165. +#define GET_LE(x) \
  166. + GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
  167. +
  168. +#define PLE(x, val, bits, ifnot) \
  169. + __builtin_choose_expr( \
  170. + (sizeof(*(x)) == bits/8), \
  171. + put_unaligned_le##bits((val), (x)), ifnot)
  172. +
  173. +extern void bad_put_le(void);
  174. +#define LAST_PLE(x, val) \
  175. + __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
  176. +
  177. +#define PUT_LE(x, val) \
  178. + PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
  179. +
  180. +
  181. +#define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
  182. +
  183. +#define BITSFUNC3(name, bits, suffix) name##bits##suffix
  184. +#define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
  185. +#define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
  186. +
  187. +#define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
  188. +
  189. +#define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
  190. +#define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
  191. +#define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
  192. +
  193. +#define ELF_BITS 64
  194. +#include "vdso2c.h"
  195. +#undef ELF_BITS
  196. +
  197. +#define ELF_BITS 32
  198. +#include "vdso2c.h"
  199. +#undef ELF_BITS
  200. +
  201. +static void go(void *raw_addr, size_t raw_len,
  202. + void *stripped_addr, size_t stripped_len,
  203. + FILE *outfile, const char *name)
  204. +{
  205. + Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
  206. +
  207. + if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
  208. + go64(raw_addr, raw_len, stripped_addr, stripped_len,
  209. + outfile, name);
  210. + } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
  211. + go32(raw_addr, raw_len, stripped_addr, stripped_len,
  212. + outfile, name);
  213. + } else {
  214. + fail("unknown ELF class\n");
  215. + }
  216. +}
  217. +
  218. +static void map_input(const char *name, void **addr, size_t *len, int prot)
  219. +{
  220. + off_t tmp_len;
  221. +
  222. + int fd = open(name, O_RDONLY);
  223. + if (fd == -1)
  224. + err(1, "%s", name);
  225. +
  226. + tmp_len = lseek(fd, 0, SEEK_END);
  227. + if (tmp_len == (off_t)-1)
  228. + err(1, "lseek");
  229. + *len = (size_t)tmp_len;
  230. +
  231. + *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
  232. + if (*addr == MAP_FAILED)
  233. + err(1, "mmap");
  234. +
  235. + close(fd);
  236. +}
  237. +
  238. +int main(int argc, char **argv)
  239. +{
  240. + size_t raw_len, stripped_len;
  241. + void *raw_addr, *stripped_addr;
  242. + FILE *outfile;
  243. + char *name, *tmp;
  244. + int namelen;
  245. +
  246. + if (argc != 4) {
  247. + printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
  248. + return 1;
  249. + }
  250. +
  251. + /*
  252. + * Figure out the struct name. If we're writing to a .so file,
  253. + * generate raw output insted.
  254. + */
  255. + name = strdup(argv[3]);
  256. + namelen = strlen(name);
  257. + if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
  258. + name = NULL;
  259. + } else {
  260. + tmp = strrchr(name, '/');
  261. + if (tmp)
  262. + name = tmp + 1;
  263. + tmp = strchr(name, '.');
  264. + if (tmp)
  265. + *tmp = '\0';
  266. + for (tmp = name; *tmp; tmp++)
  267. + if (*tmp == '-')
  268. + *tmp = '_';
  269. + }
  270. +
  271. + map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
  272. + map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
  273. +
  274. + outfilename = argv[3];
  275. + outfile = fopen(outfilename, "w");
  276. + if (!outfile)
  277. + err(1, "%s", argv[2]);
  278. +
  279. + go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
  280. +
  281. + munmap(raw_addr, raw_len);
  282. + munmap(stripped_addr, stripped_len);
  283. + fclose(outfile);
  284. +
  285. + return 0;
  286. +}