sortextable.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * sortextable.c: Sort the kernel's exception table
  3. *
  4. * Copyright 2011 - 2012 Cavium, Inc.
  5. *
  6. * Based on code taken from recortmcount.c which is:
  7. *
  8. * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
  9. * Licensed under the GNU General Public License, version 2 (GPLv2).
  10. *
  11. * Restructured to fit Linux format, as well as other updates:
  12. * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  13. */
  14. /*
  15. * Strategy: alter the vmlinux file in-place.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/mman.h>
  19. #include <sys/stat.h>
  20. #include <getopt.h>
  21. #include <elf.h>
  22. #include <fcntl.h>
  23. #include <setjmp.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <tools/be_byteshift.h>
  29. #include <tools/le_byteshift.h>
  30. static int fd_map; /* File descriptor for file being modified. */
  31. static int mmap_failed; /* Boolean flag. */
  32. static void *ehdr_curr; /* current ElfXX_Ehdr * for resource cleanup */
  33. static struct stat sb; /* Remember .st_size, etc. */
  34. static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */
  35. /* setjmp() return values */
  36. enum {
  37. SJ_SETJMP = 0, /* hardwired first return */
  38. SJ_FAIL,
  39. SJ_SUCCEED
  40. };
  41. /* Per-file resource cleanup when multiple files. */
  42. static void
  43. cleanup(void)
  44. {
  45. if (!mmap_failed)
  46. munmap(ehdr_curr, sb.st_size);
  47. close(fd_map);
  48. }
  49. static void __attribute__((noreturn))
  50. fail_file(void)
  51. {
  52. cleanup();
  53. longjmp(jmpenv, SJ_FAIL);
  54. }
  55. static void __attribute__((noreturn))
  56. succeed_file(void)
  57. {
  58. cleanup();
  59. longjmp(jmpenv, SJ_SUCCEED);
  60. }
  61. /*
  62. * Get the whole file as a programming convenience in order to avoid
  63. * malloc+lseek+read+free of many pieces. If successful, then mmap
  64. * avoids copying unused pieces; else just read the whole file.
  65. * Open for both read and write.
  66. */
  67. static void *mmap_file(char const *fname)
  68. {
  69. void *addr;
  70. fd_map = open(fname, O_RDWR);
  71. if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
  72. perror(fname);
  73. fail_file();
  74. }
  75. if (!S_ISREG(sb.st_mode)) {
  76. fprintf(stderr, "not a regular file: %s\n", fname);
  77. fail_file();
  78. }
  79. addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED,
  80. fd_map, 0);
  81. if (addr == MAP_FAILED) {
  82. mmap_failed = 1;
  83. fprintf(stderr, "Could not mmap file: %s\n", fname);
  84. fail_file();
  85. }
  86. return addr;
  87. }
  88. static uint64_t r8be(const uint64_t *x)
  89. {
  90. return get_unaligned_be64(x);
  91. }
  92. static uint32_t rbe(const uint32_t *x)
  93. {
  94. return get_unaligned_be32(x);
  95. }
  96. static uint16_t r2be(const uint16_t *x)
  97. {
  98. return get_unaligned_be16(x);
  99. }
  100. static uint64_t r8le(const uint64_t *x)
  101. {
  102. return get_unaligned_le64(x);
  103. }
  104. static uint32_t rle(const uint32_t *x)
  105. {
  106. return get_unaligned_le32(x);
  107. }
  108. static uint16_t r2le(const uint16_t *x)
  109. {
  110. return get_unaligned_le16(x);
  111. }
  112. static void w8be(uint64_t val, uint64_t *x)
  113. {
  114. put_unaligned_be64(val, x);
  115. }
  116. static void wbe(uint32_t val, uint32_t *x)
  117. {
  118. put_unaligned_be32(val, x);
  119. }
  120. static void w2be(uint16_t val, uint16_t *x)
  121. {
  122. put_unaligned_be16(val, x);
  123. }
  124. static void w8le(uint64_t val, uint64_t *x)
  125. {
  126. put_unaligned_le64(val, x);
  127. }
  128. static void wle(uint32_t val, uint32_t *x)
  129. {
  130. put_unaligned_le32(val, x);
  131. }
  132. static void w2le(uint16_t val, uint16_t *x)
  133. {
  134. put_unaligned_le16(val, x);
  135. }
  136. static uint64_t (*r8)(const uint64_t *);
  137. static uint32_t (*r)(const uint32_t *);
  138. static uint16_t (*r2)(const uint16_t *);
  139. static void (*w8)(uint64_t, uint64_t *);
  140. static void (*w)(uint32_t, uint32_t *);
  141. static void (*w2)(uint16_t, uint16_t *);
  142. typedef void (*table_sort_t)(char *, int);
  143. /* 32 bit and 64 bit are very similar */
  144. #include "sortextable.h"
  145. #define SORTEXTABLE_64
  146. #include "sortextable.h"
  147. static int compare_x86_table(const void *a, const void *b)
  148. {
  149. int32_t av = (int32_t)r(a);
  150. int32_t bv = (int32_t)r(b);
  151. if (av < bv)
  152. return -1;
  153. if (av > bv)
  154. return 1;
  155. return 0;
  156. }
  157. static void sort_x86_table(char *extab_image, int image_size)
  158. {
  159. int i;
  160. /*
  161. * Do the same thing the runtime sort does, first normalize to
  162. * being relative to the start of the section.
  163. */
  164. i = 0;
  165. while (i < image_size) {
  166. uint32_t *loc = (uint32_t *)(extab_image + i);
  167. w(r(loc) + i, loc);
  168. i += 4;
  169. }
  170. qsort(extab_image, image_size / 8, 8, compare_x86_table);
  171. /* Now denormalize. */
  172. i = 0;
  173. while (i < image_size) {
  174. uint32_t *loc = (uint32_t *)(extab_image + i);
  175. w(r(loc) - i, loc);
  176. i += 4;
  177. }
  178. }
  179. static void
  180. do_file(char const *const fname)
  181. {
  182. table_sort_t custom_sort;
  183. Elf32_Ehdr *ehdr = mmap_file(fname);
  184. ehdr_curr = ehdr;
  185. switch (ehdr->e_ident[EI_DATA]) {
  186. default:
  187. fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
  188. ehdr->e_ident[EI_DATA], fname);
  189. fail_file();
  190. break;
  191. case ELFDATA2LSB:
  192. r = rle;
  193. r2 = r2le;
  194. r8 = r8le;
  195. w = wle;
  196. w2 = w2le;
  197. w8 = w8le;
  198. break;
  199. case ELFDATA2MSB:
  200. r = rbe;
  201. r2 = r2be;
  202. r8 = r8be;
  203. w = wbe;
  204. w2 = w2be;
  205. w8 = w8be;
  206. break;
  207. } /* end switch */
  208. if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
  209. || r2(&ehdr->e_type) != ET_EXEC
  210. || ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
  211. fprintf(stderr, "unrecognized ET_EXEC file %s\n", fname);
  212. fail_file();
  213. }
  214. custom_sort = NULL;
  215. switch (r2(&ehdr->e_machine)) {
  216. default:
  217. fprintf(stderr, "unrecognized e_machine %d %s\n",
  218. r2(&ehdr->e_machine), fname);
  219. fail_file();
  220. break;
  221. case EM_386:
  222. case EM_X86_64:
  223. custom_sort = sort_x86_table;
  224. break;
  225. case EM_S390:
  226. case EM_MIPS:
  227. break;
  228. } /* end switch */
  229. switch (ehdr->e_ident[EI_CLASS]) {
  230. default:
  231. fprintf(stderr, "unrecognized ELF class %d %s\n",
  232. ehdr->e_ident[EI_CLASS], fname);
  233. fail_file();
  234. break;
  235. case ELFCLASS32:
  236. if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
  237. || r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
  238. fprintf(stderr,
  239. "unrecognized ET_EXEC file: %s\n", fname);
  240. fail_file();
  241. }
  242. do32(ehdr, fname, custom_sort);
  243. break;
  244. case ELFCLASS64: {
  245. Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
  246. if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
  247. || r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
  248. fprintf(stderr,
  249. "unrecognized ET_EXEC file: %s\n", fname);
  250. fail_file();
  251. }
  252. do64(ghdr, fname, custom_sort);
  253. break;
  254. }
  255. } /* end switch */
  256. cleanup();
  257. }
  258. int
  259. main(int argc, char *argv[])
  260. {
  261. int n_error = 0; /* gcc-4.3.0 false positive complaint */
  262. int i;
  263. if (argc < 2) {
  264. fprintf(stderr, "usage: sortextable vmlinux...\n");
  265. return 0;
  266. }
  267. /* Process each file in turn, allowing deep failure. */
  268. for (i = 1; i < argc; i++) {
  269. char *file = argv[i];
  270. int const sjval = setjmp(jmpenv);
  271. switch (sjval) {
  272. default:
  273. fprintf(stderr, "internal error: %s\n", file);
  274. exit(1);
  275. break;
  276. case SJ_SETJMP: /* normal sequence */
  277. /* Avoid problems if early cleanup() */
  278. fd_map = -1;
  279. ehdr_curr = NULL;
  280. mmap_failed = 1;
  281. do_file(file);
  282. break;
  283. case SJ_FAIL: /* error in do_file or below */
  284. ++n_error;
  285. break;
  286. case SJ_SUCCEED: /* premature success */
  287. /* do nothing */
  288. break;
  289. } /* end switch */
  290. }
  291. return !!n_error;
  292. }