sortextable.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_ARCOMPACT
  31. #define EM_ARCOMPACT 93
  32. #endif
  33. #ifndef EM_AARCH64
  34. #define EM_AARCH64 183
  35. #endif
  36. #ifndef EM_MICROBLAZE
  37. #define EM_MICROBLAZE 189
  38. #endif
  39. static int fd_map; /* File descriptor for file being modified. */
  40. static int mmap_failed; /* Boolean flag. */
  41. static void *ehdr_curr; /* current ElfXX_Ehdr * for resource cleanup */
  42. static struct stat sb; /* Remember .st_size, etc. */
  43. static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */
  44. /* setjmp() return values */
  45. enum {
  46. SJ_SETJMP = 0, /* hardwired first return */
  47. SJ_FAIL,
  48. SJ_SUCCEED
  49. };
  50. /* Per-file resource cleanup when multiple files. */
  51. static void
  52. cleanup(void)
  53. {
  54. if (!mmap_failed)
  55. munmap(ehdr_curr, sb.st_size);
  56. close(fd_map);
  57. }
  58. static void __attribute__((noreturn))
  59. fail_file(void)
  60. {
  61. cleanup();
  62. longjmp(jmpenv, SJ_FAIL);
  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. /*
  147. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  148. * the way to -256..-1, to avoid conflicting with real section
  149. * indices.
  150. */
  151. #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
  152. static inline int is_shndx_special(unsigned int i)
  153. {
  154. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  155. }
  156. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  157. static inline unsigned int get_secindex(unsigned int shndx,
  158. unsigned int sym_offs,
  159. const Elf32_Word *symtab_shndx_start)
  160. {
  161. if (is_shndx_special(shndx))
  162. return SPECIAL(shndx);
  163. if (shndx != SHN_XINDEX)
  164. return shndx;
  165. return r(&symtab_shndx_start[sym_offs]);
  166. }
  167. /* 32 bit and 64 bit are very similar */
  168. #include "sortextable.h"
  169. #define SORTEXTABLE_64
  170. #include "sortextable.h"
  171. static int compare_relative_table(const void *a, const void *b)
  172. {
  173. int32_t av = (int32_t)r(a);
  174. int32_t bv = (int32_t)r(b);
  175. if (av < bv)
  176. return -1;
  177. if (av > bv)
  178. return 1;
  179. return 0;
  180. }
  181. static void sort_relative_table(char *extab_image, int image_size)
  182. {
  183. int i;
  184. /*
  185. * Do the same thing the runtime sort does, first normalize to
  186. * being relative to the start of the section.
  187. */
  188. i = 0;
  189. while (i < image_size) {
  190. uint32_t *loc = (uint32_t *)(extab_image + i);
  191. w(r(loc) + i, loc);
  192. i += 4;
  193. }
  194. qsort(extab_image, image_size / 8, 8, compare_relative_table);
  195. /* Now denormalize. */
  196. i = 0;
  197. while (i < image_size) {
  198. uint32_t *loc = (uint32_t *)(extab_image + i);
  199. w(r(loc) - i, loc);
  200. i += 4;
  201. }
  202. }
  203. static void
  204. do_file(char const *const fname)
  205. {
  206. table_sort_t custom_sort;
  207. Elf32_Ehdr *ehdr = mmap_file(fname);
  208. ehdr_curr = ehdr;
  209. switch (ehdr->e_ident[EI_DATA]) {
  210. default:
  211. fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
  212. ehdr->e_ident[EI_DATA], fname);
  213. fail_file();
  214. break;
  215. case ELFDATA2LSB:
  216. r = rle;
  217. r2 = r2le;
  218. r8 = r8le;
  219. w = wle;
  220. w2 = w2le;
  221. w8 = w8le;
  222. break;
  223. case ELFDATA2MSB:
  224. r = rbe;
  225. r2 = r2be;
  226. r8 = r8be;
  227. w = wbe;
  228. w2 = w2be;
  229. w8 = w8be;
  230. break;
  231. } /* end switch */
  232. if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
  233. || r2(&ehdr->e_type) != ET_EXEC
  234. || ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
  235. fprintf(stderr, "unrecognized ET_EXEC file %s\n", fname);
  236. fail_file();
  237. }
  238. custom_sort = NULL;
  239. switch (r2(&ehdr->e_machine)) {
  240. default:
  241. fprintf(stderr, "unrecognized e_machine %d %s\n",
  242. r2(&ehdr->e_machine), fname);
  243. fail_file();
  244. break;
  245. case EM_386:
  246. case EM_X86_64:
  247. case EM_S390:
  248. custom_sort = sort_relative_table;
  249. break;
  250. case EM_ARCOMPACT:
  251. case EM_ARM:
  252. case EM_AARCH64:
  253. case EM_MICROBLAZE:
  254. case EM_MIPS:
  255. break;
  256. } /* end switch */
  257. switch (ehdr->e_ident[EI_CLASS]) {
  258. default:
  259. fprintf(stderr, "unrecognized ELF class %d %s\n",
  260. ehdr->e_ident[EI_CLASS], fname);
  261. fail_file();
  262. break;
  263. case ELFCLASS32:
  264. if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
  265. || r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
  266. fprintf(stderr,
  267. "unrecognized ET_EXEC file: %s\n", fname);
  268. fail_file();
  269. }
  270. do32(ehdr, fname, custom_sort);
  271. break;
  272. case ELFCLASS64: {
  273. Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
  274. if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
  275. || r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
  276. fprintf(stderr,
  277. "unrecognized ET_EXEC file: %s\n", fname);
  278. fail_file();
  279. }
  280. do64(ghdr, fname, custom_sort);
  281. break;
  282. }
  283. } /* end switch */
  284. cleanup();
  285. }
  286. int
  287. main(int argc, char *argv[])
  288. {
  289. int n_error = 0; /* gcc-4.3.0 false positive complaint */
  290. int i;
  291. if (argc < 2) {
  292. fprintf(stderr, "usage: sortextable vmlinux...\n");
  293. return 0;
  294. }
  295. /* Process each file in turn, allowing deep failure. */
  296. for (i = 1; i < argc; i++) {
  297. char *file = argv[i];
  298. int const sjval = setjmp(jmpenv);
  299. switch (sjval) {
  300. default:
  301. fprintf(stderr, "internal error: %s\n", file);
  302. exit(1);
  303. break;
  304. case SJ_SETJMP: /* normal sequence */
  305. /* Avoid problems if early cleanup() */
  306. fd_map = -1;
  307. ehdr_curr = NULL;
  308. mmap_failed = 1;
  309. do_file(file);
  310. break;
  311. case SJ_FAIL: /* error in do_file or below */
  312. ++n_error;
  313. break;
  314. case SJ_SUCCEED: /* premature success */
  315. /* do nothing */
  316. break;
  317. } /* end switch */
  318. }
  319. return !!n_error;
  320. }