sorttable.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 <[email protected]>
  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 <[email protected]>. All rights reserved.
  14. *
  15. * Restructured to fit Linux format, as well as other updates:
  16. * Copyright 2010 Steven Rostedt <[email protected]>, 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 <errno.h>
  32. #include <pthread.h>
  33. #include <tools/be_byteshift.h>
  34. #include <tools/le_byteshift.h>
  35. #ifndef EM_ARCOMPACT
  36. #define EM_ARCOMPACT 93
  37. #endif
  38. #ifndef EM_XTENSA
  39. #define EM_XTENSA 94
  40. #endif
  41. #ifndef EM_AARCH64
  42. #define EM_AARCH64 183
  43. #endif
  44. #ifndef EM_MICROBLAZE
  45. #define EM_MICROBLAZE 189
  46. #endif
  47. #ifndef EM_ARCV2
  48. #define EM_ARCV2 195
  49. #endif
  50. #ifndef EM_RISCV
  51. #define EM_RISCV 243
  52. #endif
  53. #ifndef EM_LOONGARCH
  54. #define EM_LOONGARCH 258
  55. #endif
  56. static uint32_t (*r)(const uint32_t *);
  57. static uint16_t (*r2)(const uint16_t *);
  58. static uint64_t (*r8)(const uint64_t *);
  59. static void (*w)(uint32_t, uint32_t *);
  60. static void (*w2)(uint16_t, uint16_t *);
  61. static void (*w8)(uint64_t, uint64_t *);
  62. typedef void (*table_sort_t)(char *, int);
  63. /*
  64. * Get the whole file as a programming convenience in order to avoid
  65. * malloc+lseek+read+free of many pieces. If successful, then mmap
  66. * avoids copying unused pieces; else just read the whole file.
  67. * Open for both read and write.
  68. */
  69. static void *mmap_file(char const *fname, size_t *size)
  70. {
  71. int fd;
  72. struct stat sb;
  73. void *addr = NULL;
  74. fd = open(fname, O_RDWR);
  75. if (fd < 0) {
  76. perror(fname);
  77. return NULL;
  78. }
  79. if (fstat(fd, &sb) < 0) {
  80. perror(fname);
  81. goto out;
  82. }
  83. if (!S_ISREG(sb.st_mode)) {
  84. fprintf(stderr, "not a regular file: %s\n", fname);
  85. goto out;
  86. }
  87. addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  88. if (addr == MAP_FAILED) {
  89. fprintf(stderr, "Could not mmap file: %s\n", fname);
  90. goto out;
  91. }
  92. *size = sb.st_size;
  93. out:
  94. close(fd);
  95. return addr;
  96. }
  97. static uint32_t rbe(const uint32_t *x)
  98. {
  99. return get_unaligned_be32(x);
  100. }
  101. static uint16_t r2be(const uint16_t *x)
  102. {
  103. return get_unaligned_be16(x);
  104. }
  105. static uint64_t r8be(const uint64_t *x)
  106. {
  107. return get_unaligned_be64(x);
  108. }
  109. static uint32_t rle(const uint32_t *x)
  110. {
  111. return get_unaligned_le32(x);
  112. }
  113. static uint16_t r2le(const uint16_t *x)
  114. {
  115. return get_unaligned_le16(x);
  116. }
  117. static uint64_t r8le(const uint64_t *x)
  118. {
  119. return get_unaligned_le64(x);
  120. }
  121. static void wbe(uint32_t val, uint32_t *x)
  122. {
  123. put_unaligned_be32(val, x);
  124. }
  125. static void w2be(uint16_t val, uint16_t *x)
  126. {
  127. put_unaligned_be16(val, x);
  128. }
  129. static void w8be(uint64_t val, uint64_t *x)
  130. {
  131. put_unaligned_be64(val, x);
  132. }
  133. static void wle(uint32_t val, uint32_t *x)
  134. {
  135. put_unaligned_le32(val, x);
  136. }
  137. static void w2le(uint16_t val, uint16_t *x)
  138. {
  139. put_unaligned_le16(val, x);
  140. }
  141. static void w8le(uint64_t val, uint64_t *x)
  142. {
  143. put_unaligned_le64(val, x);
  144. }
  145. /*
  146. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  147. * the way to -256..-1, to avoid conflicting with real section
  148. * indices.
  149. */
  150. #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
  151. static inline int is_shndx_special(unsigned int i)
  152. {
  153. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  154. }
  155. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  156. static inline unsigned int get_secindex(unsigned int shndx,
  157. unsigned int sym_offs,
  158. const Elf32_Word *symtab_shndx_start)
  159. {
  160. if (is_shndx_special(shndx))
  161. return SPECIAL(shndx);
  162. if (shndx != SHN_XINDEX)
  163. return shndx;
  164. return r(&symtab_shndx_start[sym_offs]);
  165. }
  166. /* 32 bit and 64 bit are very similar */
  167. #include "sorttable.h"
  168. #define SORTTABLE_64
  169. #include "sorttable.h"
  170. static int compare_relative_table(const void *a, const void *b)
  171. {
  172. int32_t av = (int32_t)r(a);
  173. int32_t bv = (int32_t)r(b);
  174. if (av < bv)
  175. return -1;
  176. if (av > bv)
  177. return 1;
  178. return 0;
  179. }
  180. static void sort_relative_table(char *extab_image, int image_size)
  181. {
  182. int i = 0;
  183. /*
  184. * Do the same thing the runtime sort does, first normalize to
  185. * being relative to the start of the section.
  186. */
  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. qsort(extab_image, image_size / 8, 8, compare_relative_table);
  193. /* Now denormalize. */
  194. i = 0;
  195. while (i < image_size) {
  196. uint32_t *loc = (uint32_t *)(extab_image + i);
  197. w(r(loc) - i, loc);
  198. i += 4;
  199. }
  200. }
  201. static void sort_relative_table_with_data(char *extab_image, int image_size)
  202. {
  203. int i = 0;
  204. while (i < image_size) {
  205. uint32_t *loc = (uint32_t *)(extab_image + i);
  206. w(r(loc) + i, loc);
  207. w(r(loc + 1) + i + 4, loc + 1);
  208. /* Don't touch the fixup type or data */
  209. i += sizeof(uint32_t) * 3;
  210. }
  211. qsort(extab_image, image_size / 12, 12, compare_relative_table);
  212. i = 0;
  213. while (i < image_size) {
  214. uint32_t *loc = (uint32_t *)(extab_image + i);
  215. w(r(loc) - i, loc);
  216. w(r(loc + 1) - (i + 4), loc + 1);
  217. /* Don't touch the fixup type or data */
  218. i += sizeof(uint32_t) * 3;
  219. }
  220. }
  221. static int do_file(char const *const fname, void *addr)
  222. {
  223. int rc = -1;
  224. Elf32_Ehdr *ehdr = addr;
  225. table_sort_t custom_sort = NULL;
  226. switch (ehdr->e_ident[EI_DATA]) {
  227. case ELFDATA2LSB:
  228. r = rle;
  229. r2 = r2le;
  230. r8 = r8le;
  231. w = wle;
  232. w2 = w2le;
  233. w8 = w8le;
  234. break;
  235. case ELFDATA2MSB:
  236. r = rbe;
  237. r2 = r2be;
  238. r8 = r8be;
  239. w = wbe;
  240. w2 = w2be;
  241. w8 = w8be;
  242. break;
  243. default:
  244. fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
  245. ehdr->e_ident[EI_DATA], fname);
  246. return -1;
  247. }
  248. if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
  249. (r2(&ehdr->e_type) != ET_EXEC && r2(&ehdr->e_type) != ET_DYN) ||
  250. ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
  251. fprintf(stderr, "unrecognized ET_EXEC/ET_DYN file %s\n", fname);
  252. return -1;
  253. }
  254. switch (r2(&ehdr->e_machine)) {
  255. case EM_386:
  256. case EM_AARCH64:
  257. case EM_RISCV:
  258. case EM_S390:
  259. case EM_X86_64:
  260. custom_sort = sort_relative_table_with_data;
  261. break;
  262. case EM_PARISC:
  263. case EM_PPC:
  264. case EM_PPC64:
  265. custom_sort = sort_relative_table;
  266. break;
  267. case EM_ARCOMPACT:
  268. case EM_ARCV2:
  269. case EM_ARM:
  270. case EM_LOONGARCH:
  271. case EM_MICROBLAZE:
  272. case EM_MIPS:
  273. case EM_XTENSA:
  274. break;
  275. default:
  276. fprintf(stderr, "unrecognized e_machine %d %s\n",
  277. r2(&ehdr->e_machine), fname);
  278. return -1;
  279. }
  280. switch (ehdr->e_ident[EI_CLASS]) {
  281. case ELFCLASS32:
  282. if (r2(&ehdr->e_ehsize) != sizeof(Elf32_Ehdr) ||
  283. r2(&ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
  284. fprintf(stderr,
  285. "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
  286. break;
  287. }
  288. rc = do_sort_32(ehdr, fname, custom_sort);
  289. break;
  290. case ELFCLASS64:
  291. {
  292. Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
  293. if (r2(&ghdr->e_ehsize) != sizeof(Elf64_Ehdr) ||
  294. r2(&ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
  295. fprintf(stderr,
  296. "unrecognized ET_EXEC/ET_DYN file: %s\n",
  297. fname);
  298. break;
  299. }
  300. rc = do_sort_64(ghdr, fname, custom_sort);
  301. }
  302. break;
  303. default:
  304. fprintf(stderr, "unrecognized ELF class %d %s\n",
  305. ehdr->e_ident[EI_CLASS], fname);
  306. break;
  307. }
  308. return rc;
  309. }
  310. int main(int argc, char *argv[])
  311. {
  312. int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
  313. size_t size = 0;
  314. void *addr = NULL;
  315. if (argc < 2) {
  316. fprintf(stderr, "usage: sorttable vmlinux...\n");
  317. return 0;
  318. }
  319. /* Process each file in turn, allowing deep failure. */
  320. for (i = 1; i < argc; i++) {
  321. addr = mmap_file(argv[i], &size);
  322. if (!addr) {
  323. ++n_error;
  324. continue;
  325. }
  326. if (do_file(argv[i], addr))
  327. ++n_error;
  328. munmap(addr, size);
  329. }
  330. return !!n_error;
  331. }