sortextable.c 6.8 KB

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