sorttable.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sorttable.c: Sort the kernel's table
  4. *
  5. * Added ORC unwind tables sort support and other updates:
  6. * Copyright (C) 1999-2019 Alibaba Group Holding Limited. by:
  7. * Shile Zhang <shile.zhang@linux.alibaba.com>
  8. *
  9. * Copyright 2011 - 2012 Cavium, Inc.
  10. *
  11. * Based on code taken from recortmcount.c which is:
  12. *
  13. * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
  14. *
  15. * Restructured to fit Linux format, as well as other updates:
  16. * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  17. */
  18. /*
  19. * Strategy: alter the vmlinux file in-place.
  20. */
  21. #include <sys/types.h>
  22. #include <sys/mman.h>
  23. #include <sys/stat.h>
  24. #include <getopt.h>
  25. #include <elf.h>
  26. #include <fcntl.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <unistd.h>
  31. #include <tools/be_byteshift.h>
  32. #include <tools/le_byteshift.h>
  33. #ifndef EM_ARCOMPACT
  34. #define EM_ARCOMPACT 93
  35. #endif
  36. #ifndef EM_XTENSA
  37. #define EM_XTENSA 94
  38. #endif
  39. #ifndef EM_AARCH64
  40. #define EM_AARCH64 183
  41. #endif
  42. #ifndef EM_MICROBLAZE
  43. #define EM_MICROBLAZE 189
  44. #endif
  45. #ifndef EM_ARCV2
  46. #define EM_ARCV2 195
  47. #endif
  48. static uint32_t (*r)(const uint32_t *);
  49. static uint16_t (*r2)(const uint16_t *);
  50. static uint64_t (*r8)(const uint64_t *);
  51. static void (*w)(uint32_t, uint32_t *);
  52. static void (*w2)(uint16_t, uint16_t *);
  53. static void (*w8)(uint64_t, uint64_t *);
  54. typedef void (*table_sort_t)(char *, int);
  55. /*
  56. * Get the whole file as a programming convenience in order to avoid
  57. * malloc+lseek+read+free of many pieces. If successful, then mmap
  58. * avoids copying unused pieces; else just read the whole file.
  59. * Open for both read and write.
  60. */
  61. static void *mmap_file(char const *fname, size_t *size)
  62. {
  63. int fd;
  64. struct stat sb;
  65. void *addr = NULL;
  66. fd = open(fname, O_RDWR);
  67. if (fd < 0) {
  68. perror(fname);
  69. return NULL;
  70. }
  71. if (fstat(fd, &sb) < 0) {
  72. perror(fname);
  73. goto out;
  74. }
  75. if (!S_ISREG(sb.st_mode)) {
  76. fprintf(stderr, "not a regular file: %s\n", fname);
  77. goto out;
  78. }
  79. addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  80. if (addr == MAP_FAILED) {
  81. fprintf(stderr, "Could not mmap file: %s\n", fname);
  82. goto out;
  83. }
  84. *size = sb.st_size;
  85. out:
  86. close(fd);
  87. return addr;
  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 r8be(const uint64_t *x)
  98. {
  99. return get_unaligned_be64(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 uint64_t r8le(const uint64_t *x)
  110. {
  111. return get_unaligned_le64(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 w8be(uint64_t val, uint64_t *x)
  122. {
  123. put_unaligned_be64(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 void w8le(uint64_t val, uint64_t *x)
  134. {
  135. put_unaligned_le64(val, x);
  136. }
  137. /*
  138. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  139. * the way to -256..-1, to avoid conflicting with real section
  140. * indices.
  141. */
  142. #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
  143. static inline int is_shndx_special(unsigned int i)
  144. {
  145. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  146. }
  147. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  148. static inline unsigned int get_secindex(unsigned int shndx,
  149. unsigned int sym_offs,
  150. const Elf32_Word *symtab_shndx_start)
  151. {
  152. if (is_shndx_special(shndx))
  153. return SPECIAL(shndx);
  154. if (shndx != SHN_XINDEX)
  155. return shndx;
  156. return r(&symtab_shndx_start[sym_offs]);
  157. }
  158. /* 32 bit and 64 bit are very similar */
  159. #include "sorttable.h"
  160. #define SORTTABLE_64
  161. #include "sorttable.h"
  162. static int compare_relative_table(const void *a, const void *b)
  163. {
  164. int32_t av = (int32_t)r(a);
  165. int32_t bv = (int32_t)r(b);
  166. if (av < bv)
  167. return -1;
  168. if (av > bv)
  169. return 1;
  170. return 0;
  171. }
  172. static void sort_relative_table(char *extab_image, int image_size)
  173. {
  174. int i = 0;
  175. /*
  176. * Do the same thing the runtime sort does, first normalize to
  177. * being relative to the start of the section.
  178. */
  179. while (i < image_size) {
  180. uint32_t *loc = (uint32_t *)(extab_image + i);
  181. w(r(loc) + i, loc);
  182. i += 4;
  183. }
  184. qsort(extab_image, image_size / 8, 8, compare_relative_table);
  185. /* Now denormalize. */
  186. i = 0;
  187. while (i < image_size) {
  188. uint32_t *loc = (uint32_t *)(extab_image + i);
  189. w(r(loc) - i, loc);
  190. i += 4;
  191. }
  192. }
  193. static void x86_sort_relative_table(char *extab_image, int image_size)
  194. {
  195. int i = 0;
  196. while (i < image_size) {
  197. uint32_t *loc = (uint32_t *)(extab_image + i);
  198. w(r(loc) + i, loc);
  199. w(r(loc + 1) + i + 4, loc + 1);
  200. w(r(loc + 2) + i + 8, loc + 2);
  201. i += sizeof(uint32_t) * 3;
  202. }
  203. qsort(extab_image, image_size / 12, 12, compare_relative_table);
  204. i = 0;
  205. while (i < image_size) {
  206. uint32_t *loc = (uint32_t *)(extab_image + i);
  207. w(r(loc) - i, loc);
  208. w(r(loc + 1) - (i + 4), loc + 1);
  209. w(r(loc + 2) - (i + 8), loc + 2);
  210. i += sizeof(uint32_t) * 3;
  211. }
  212. }
  213. static void s390_sort_relative_table(char *extab_image, int image_size)
  214. {
  215. int i;
  216. for (i = 0; i < image_size; i += 16) {
  217. char *loc = extab_image + i;
  218. uint64_t handler;
  219. w(r((uint32_t *)loc) + i, (uint32_t *)loc);
  220. w(r((uint32_t *)(loc + 4)) + (i + 4), (uint32_t *)(loc + 4));
  221. /*
  222. * 0 is a special self-relative handler value, which means that
  223. * handler should be ignored. It is safe, because it means that
  224. * handler field points to itself, which should never happen.
  225. * When creating extable-relative values, keep it as 0, since
  226. * this should never occur either: it would mean that handler
  227. * field points to the first extable entry.
  228. */
  229. handler = r8((uint64_t *)(loc + 8));
  230. if (handler)
  231. handler += i + 8;
  232. w8(handler, (uint64_t *)(loc + 8));
  233. }
  234. qsort(extab_image, image_size / 16, 16, compare_relative_table);
  235. for (i = 0; i < image_size; i += 16) {
  236. char *loc = extab_image + i;
  237. uint64_t handler;
  238. w(r((uint32_t *)loc) - i, (uint32_t *)loc);
  239. w(r((uint32_t *)(loc + 4)) - (i + 4), (uint32_t *)(loc + 4));
  240. handler = r8((uint64_t *)(loc + 8));
  241. if (handler)
  242. handler -= i + 8;
  243. w8(handler, (uint64_t *)(loc + 8));
  244. }
  245. }
  246. static int do_file(char const *const fname, void *addr)
  247. {
  248. int rc = -1;
  249. Elf32_Ehdr *ehdr = addr;
  250. table_sort_t custom_sort = NULL;
  251. switch (ehdr->e_ident[EI_DATA]) {
  252. case ELFDATA2LSB:
  253. r = rle;
  254. r2 = r2le;
  255. r8 = r8le;
  256. w = wle;
  257. w2 = w2le;
  258. w8 = w8le;
  259. break;
  260. case ELFDATA2MSB:
  261. r = rbe;
  262. r2 = r2be;
  263. r8 = r8be;
  264. w = wbe;
  265. w2 = w2be;
  266. w8 = w8be;
  267. break;
  268. default:
  269. fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
  270. ehdr->e_ident[EI_DATA], fname);
  271. return -1;
  272. }
  273. if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
  274. (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
  275. ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
  276. fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
  277. return -1;
  278. }
  279. switch (r2(&ehdr->e_machine)) {
  280. case EM_386:
  281. case EM_X86_64:
  282. custom_sort = x86_sort_relative_table;
  283. break;
  284. case EM_S390:
  285. custom_sort = s390_sort_relative_table;
  286. break;
  287. case EM_AARCH64:
  288. case EM_PARISC:
  289. case EM_PPC:
  290. case EM_PPC64:
  291. custom_sort = sort_relative_table;
  292. break;
  293. case EM_ARCOMPACT:
  294. case EM_ARCV2:
  295. case EM_ARM:
  296. case EM_MICROBLAZE:
  297. case EM_MIPS:
  298. case EM_XTENSA:
  299. break;
  300. default:
  301. fprintf(stderr, "unrecognized e_machine %d %s\n",
  302. r2(&ehdr->e_machine), fname);
  303. return -1;
  304. }
  305. switch (ehdr->e_ident[EI_CLASS]) {
  306. case ELFCLASS32:
  307. if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
  308. r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
  309. fprintf(stderr,
  310. "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
  311. break;
  312. }
  313. rc = do_sort_32(ehdr, fname, custom_sort);
  314. break;
  315. case ELFCLASS64:
  316. {
  317. Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
  318. if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
  319. r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
  320. fprintf(stderr,
  321. "unrecognized ET_EXEC/ET_DYN file: %s\n",
  322. fname);
  323. break;
  324. }
  325. rc = do_sort_64(ghdr, fname, custom_sort);
  326. }
  327. break;
  328. default:
  329. fprintf(stderr, "unrecognized ELF class %d %s\n",
  330. ehdr->e_ident[EI_CLASS], fname);
  331. break;
  332. }
  333. return rc;
  334. }
  335. int main(int argc, char *argv[])
  336. {
  337. int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
  338. size_t size = 0;
  339. void *addr = NULL;
  340. if (argc < 2) {
  341. fprintf(stderr, "usage: sorttable vmlinux...\n");
  342. return 0;
  343. }
  344. /* Process each file in turn, allowing deep failure. */
  345. for (i = 1; i < argc; i++) {
  346. addr = mmap_file(argv[i], &size);
  347. if (!addr) {
  348. ++n_error;
  349. continue;
  350. }
  351. if (do_file(argv[i], addr))
  352. ++n_error;
  353. munmap(addr, size);
  354. }
  355. return !!n_error;
  356. }