sortextable.c 7.4 KB

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