vdso2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vdso2c - A vdso image preparation tool
  4. * Copyright (c) 2014 Andy Lutomirski and others
  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. };
  61. struct vdso_sym {
  62. const char *name;
  63. int export;
  64. };
  65. struct vdso_sym required_syms[] = {
  66. [sym_vvar_start] = {"vvar_start", 1},
  67. [sym_VDSO_FAKE_SECTION_TABLE_START] = {
  68. "VDSO_FAKE_SECTION_TABLE_START", 0
  69. },
  70. [sym_VDSO_FAKE_SECTION_TABLE_END] = {
  71. "VDSO_FAKE_SECTION_TABLE_END", 0
  72. },
  73. };
  74. __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
  75. static void fail(const char *format, ...)
  76. {
  77. va_list ap;
  78. va_start(ap, format);
  79. fprintf(stderr, "Error: ");
  80. vfprintf(stderr, format, ap);
  81. if (outfilename)
  82. unlink(outfilename);
  83. exit(1);
  84. va_end(ap);
  85. }
  86. /*
  87. * Evil macros for big-endian reads and writes
  88. */
  89. #define GBE(x, bits, ifnot) \
  90. __builtin_choose_expr( \
  91. (sizeof(*(x)) == bits/8), \
  92. (__typeof__(*(x)))get_unaligned_be##bits(x), ifnot)
  93. #define LAST_GBE(x) \
  94. __builtin_choose_expr(sizeof(*(x)) == 1, *(x), (void)(0))
  95. #define GET_BE(x) \
  96. GBE(x, 64, GBE(x, 32, GBE(x, 16, LAST_GBE(x))))
  97. #define PBE(x, val, bits, ifnot) \
  98. __builtin_choose_expr( \
  99. (sizeof(*(x)) == bits/8), \
  100. put_unaligned_be##bits((val), (x)), ifnot)
  101. #define LAST_PBE(x, val) \
  102. __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), (void)(0))
  103. #define PUT_BE(x, val) \
  104. PBE(x, val, 64, PBE(x, val, 32, PBE(x, val, 16, LAST_PBE(x, val))))
  105. #define NSYMS ARRAY_SIZE(required_syms)
  106. #define BITSFUNC3(name, bits, suffix) name##bits##suffix
  107. #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix)
  108. #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, )
  109. #define INT_BITS BITSFUNC2(int, ELF_BITS, _t)
  110. #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x
  111. #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x)
  112. #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x)
  113. #define ELF_BITS 64
  114. #include "vdso2c.h"
  115. #undef ELF_BITS
  116. #define ELF_BITS 32
  117. #include "vdso2c.h"
  118. #undef ELF_BITS
  119. static void go(void *raw_addr, size_t raw_len,
  120. void *stripped_addr, size_t stripped_len,
  121. FILE *outfile, const char *name)
  122. {
  123. Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr;
  124. if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
  125. go64(raw_addr, raw_len, stripped_addr, stripped_len,
  126. outfile, name);
  127. } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
  128. go32(raw_addr, raw_len, stripped_addr, stripped_len,
  129. outfile, name);
  130. } else {
  131. fail("unknown ELF class\n");
  132. }
  133. }
  134. static void map_input(const char *name, void **addr, size_t *len, int prot)
  135. {
  136. off_t tmp_len;
  137. int fd = open(name, O_RDONLY);
  138. if (fd == -1)
  139. err(1, "%s", name);
  140. tmp_len = lseek(fd, 0, SEEK_END);
  141. if (tmp_len == (off_t)-1)
  142. err(1, "lseek");
  143. *len = (size_t)tmp_len;
  144. *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0);
  145. if (*addr == MAP_FAILED)
  146. err(1, "mmap");
  147. close(fd);
  148. }
  149. int main(int argc, char **argv)
  150. {
  151. size_t raw_len, stripped_len;
  152. void *raw_addr, *stripped_addr;
  153. FILE *outfile;
  154. char *name, *tmp;
  155. int namelen;
  156. if (argc != 4) {
  157. printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n");
  158. return 1;
  159. }
  160. /*
  161. * Figure out the struct name. If we're writing to a .so file,
  162. * generate raw output insted.
  163. */
  164. name = strdup(argv[3]);
  165. namelen = strlen(name);
  166. if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
  167. name = NULL;
  168. } else {
  169. tmp = strrchr(name, '/');
  170. if (tmp)
  171. name = tmp + 1;
  172. tmp = strchr(name, '.');
  173. if (tmp)
  174. *tmp = '\0';
  175. for (tmp = name; *tmp; tmp++)
  176. if (*tmp == '-')
  177. *tmp = '_';
  178. }
  179. map_input(argv[1], &raw_addr, &raw_len, PROT_READ);
  180. map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ);
  181. outfilename = argv[3];
  182. outfile = fopen(outfilename, "w");
  183. if (!outfile)
  184. err(1, "%s", argv[2]);
  185. go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name);
  186. munmap(raw_addr, raw_len);
  187. munmap(stripped_addr, stripped_len);
  188. fclose(outfile);
  189. return 0;
  190. }