sorttable.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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/stat.h>
  23. #include <getopt.h>
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <stdbool.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <pthread.h>
  32. #include "elf-parse.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. #ifndef EM_RISCV
  49. #define EM_RISCV 243
  50. #endif
  51. #ifndef EM_LOONGARCH
  52. #define EM_LOONGARCH 258
  53. #endif
  54. typedef void (*table_sort_t)(char *, int);
  55. /*
  56. * Move reserved section indices SHN_LORESERVE..SHN_HIRESERVE out of
  57. * the way to -256..-1, to avoid conflicting with real section
  58. * indices.
  59. */
  60. #define SPECIAL(i) ((i) - (SHN_HIRESERVE + 1))
  61. static inline int is_shndx_special(unsigned int i)
  62. {
  63. return i != SHN_XINDEX && i >= SHN_LORESERVE && i <= SHN_HIRESERVE;
  64. }
  65. /* Accessor for sym->st_shndx, hides ugliness of "64k sections" */
  66. static inline unsigned int get_secindex(unsigned int shndx,
  67. unsigned int sym_offs,
  68. const Elf32_Word *symtab_shndx_start)
  69. {
  70. if (is_shndx_special(shndx))
  71. return SPECIAL(shndx);
  72. if (shndx != SHN_XINDEX)
  73. return shndx;
  74. return elf_parser.r(&symtab_shndx_start[sym_offs]);
  75. }
  76. static int compare_extable_32(const void *a, const void *b)
  77. {
  78. Elf32_Addr av = elf_parser.r(a);
  79. Elf32_Addr bv = elf_parser.r(b);
  80. if (av < bv)
  81. return -1;
  82. return av > bv;
  83. }
  84. static int compare_extable_64(const void *a, const void *b)
  85. {
  86. Elf64_Addr av = elf_parser.r8(a);
  87. Elf64_Addr bv = elf_parser.r8(b);
  88. if (av < bv)
  89. return -1;
  90. return av > bv;
  91. }
  92. static int (*compare_extable)(const void *a, const void *b);
  93. static inline void *get_index(void *start, int entsize, int index)
  94. {
  95. return start + (entsize * index);
  96. }
  97. static int extable_ent_size;
  98. static int long_size;
  99. #define ERRSTR_MAXSZ 256
  100. #ifdef UNWINDER_ORC_ENABLED
  101. /* ORC unwinder only support X86_64 */
  102. #include <asm/orc_types.h>
  103. static char g_err[ERRSTR_MAXSZ];
  104. static int *g_orc_ip_table;
  105. static struct orc_entry *g_orc_table;
  106. static pthread_t orc_sort_thread;
  107. static inline unsigned long orc_ip(const int *ip)
  108. {
  109. return (unsigned long)ip + *ip;
  110. }
  111. static int orc_sort_cmp(const void *_a, const void *_b)
  112. {
  113. struct orc_entry *orc_a, *orc_b;
  114. const int *a = g_orc_ip_table + *(int *)_a;
  115. const int *b = g_orc_ip_table + *(int *)_b;
  116. unsigned long a_val = orc_ip(a);
  117. unsigned long b_val = orc_ip(b);
  118. if (a_val > b_val)
  119. return 1;
  120. if (a_val < b_val)
  121. return -1;
  122. /*
  123. * The "weak" section terminator entries need to always be on the left
  124. * to ensure the lookup code skips them in favor of real entries.
  125. * These terminator entries exist to handle any gaps created by
  126. * whitelisted .o files which didn't get objtool generation.
  127. */
  128. orc_a = g_orc_table + (a - g_orc_ip_table);
  129. orc_b = g_orc_table + (b - g_orc_ip_table);
  130. if (orc_a->type == ORC_TYPE_UNDEFINED && orc_b->type == ORC_TYPE_UNDEFINED)
  131. return 0;
  132. return orc_a->type == ORC_TYPE_UNDEFINED ? -1 : 1;
  133. }
  134. static void *sort_orctable(void *arg)
  135. {
  136. int i;
  137. int *idxs = NULL;
  138. int *tmp_orc_ip_table = NULL;
  139. struct orc_entry *tmp_orc_table = NULL;
  140. unsigned int *orc_ip_size = (unsigned int *)arg;
  141. unsigned int num_entries = *orc_ip_size / sizeof(int);
  142. unsigned int orc_size = num_entries * sizeof(struct orc_entry);
  143. idxs = (int *)malloc(*orc_ip_size);
  144. if (!idxs) {
  145. snprintf(g_err, ERRSTR_MAXSZ, "malloc idxs: %s",
  146. strerror(errno));
  147. pthread_exit(g_err);
  148. }
  149. tmp_orc_ip_table = (int *)malloc(*orc_ip_size);
  150. if (!tmp_orc_ip_table) {
  151. snprintf(g_err, ERRSTR_MAXSZ, "malloc tmp_orc_ip_table: %s",
  152. strerror(errno));
  153. pthread_exit(g_err);
  154. }
  155. tmp_orc_table = (struct orc_entry *)malloc(orc_size);
  156. if (!tmp_orc_table) {
  157. snprintf(g_err, ERRSTR_MAXSZ, "malloc tmp_orc_table: %s",
  158. strerror(errno));
  159. pthread_exit(g_err);
  160. }
  161. /* initialize indices array, convert ip_table to absolute address */
  162. for (i = 0; i < num_entries; i++) {
  163. idxs[i] = i;
  164. tmp_orc_ip_table[i] = g_orc_ip_table[i] + i * sizeof(int);
  165. }
  166. memcpy(tmp_orc_table, g_orc_table, orc_size);
  167. qsort(idxs, num_entries, sizeof(int), orc_sort_cmp);
  168. for (i = 0; i < num_entries; i++) {
  169. if (idxs[i] == i)
  170. continue;
  171. /* convert back to relative address */
  172. g_orc_ip_table[i] = tmp_orc_ip_table[idxs[i]] - i * sizeof(int);
  173. g_orc_table[i] = tmp_orc_table[idxs[i]];
  174. }
  175. free(idxs);
  176. free(tmp_orc_ip_table);
  177. free(tmp_orc_table);
  178. pthread_exit(NULL);
  179. }
  180. #endif
  181. #ifdef MCOUNT_SORT_ENABLED
  182. static int compare_values_64(const void *a, const void *b)
  183. {
  184. uint64_t av = *(uint64_t *)a;
  185. uint64_t bv = *(uint64_t *)b;
  186. if (av < bv)
  187. return -1;
  188. return av > bv;
  189. }
  190. static int compare_values_32(const void *a, const void *b)
  191. {
  192. uint32_t av = *(uint32_t *)a;
  193. uint32_t bv = *(uint32_t *)b;
  194. if (av < bv)
  195. return -1;
  196. return av > bv;
  197. }
  198. static int (*compare_values)(const void *a, const void *b);
  199. /* Only used for sorting mcount table */
  200. static void rela_write_addend(Elf_Rela *rela, uint64_t val)
  201. {
  202. elf_parser.rela_write_addend(rela, val);
  203. }
  204. struct func_info {
  205. uint64_t addr;
  206. uint64_t size;
  207. };
  208. /* List of functions created by: nm -S vmlinux */
  209. static struct func_info *function_list;
  210. static int function_list_size;
  211. /* Allocate functions in 1k blocks */
  212. #define FUNC_BLK_SIZE 1024
  213. #define FUNC_BLK_MASK (FUNC_BLK_SIZE - 1)
  214. static int add_field(uint64_t addr, uint64_t size)
  215. {
  216. struct func_info *fi;
  217. int fsize = function_list_size;
  218. if (!(fsize & FUNC_BLK_MASK)) {
  219. fsize += FUNC_BLK_SIZE;
  220. fi = realloc(function_list, fsize * sizeof(struct func_info));
  221. if (!fi)
  222. return -1;
  223. function_list = fi;
  224. }
  225. fi = &function_list[function_list_size++];
  226. fi->addr = addr;
  227. fi->size = size;
  228. return 0;
  229. }
  230. /* Used for when mcount/fentry is before the function entry */
  231. static int before_func;
  232. /* Only return match if the address lies inside the function size */
  233. static int cmp_func_addr(const void *K, const void *A)
  234. {
  235. uint64_t key = *(const uint64_t *)K;
  236. const struct func_info *a = A;
  237. if (key + before_func < a->addr)
  238. return -1;
  239. return key >= a->addr + a->size;
  240. }
  241. /* Find the function in function list that is bounded by the function size */
  242. static int find_func(uint64_t key)
  243. {
  244. return bsearch(&key, function_list, function_list_size,
  245. sizeof(struct func_info), cmp_func_addr) != NULL;
  246. }
  247. static int cmp_funcs(const void *A, const void *B)
  248. {
  249. const struct func_info *a = A;
  250. const struct func_info *b = B;
  251. if (a->addr < b->addr)
  252. return -1;
  253. return a->addr > b->addr;
  254. }
  255. static int parse_symbols(const char *fname)
  256. {
  257. FILE *fp;
  258. char addr_str[20]; /* Only need 17, but round up to next int size */
  259. char size_str[20];
  260. char type;
  261. fp = fopen(fname, "r");
  262. if (!fp) {
  263. perror(fname);
  264. return -1;
  265. }
  266. while (fscanf(fp, "%16s %16s %c %*s\n", addr_str, size_str, &type) == 3) {
  267. uint64_t addr;
  268. uint64_t size;
  269. /* Only care about functions */
  270. if (type != 't' && type != 'T' && type != 'W')
  271. continue;
  272. addr = strtoull(addr_str, NULL, 16);
  273. size = strtoull(size_str, NULL, 16);
  274. if (add_field(addr, size) < 0)
  275. return -1;
  276. }
  277. fclose(fp);
  278. qsort(function_list, function_list_size, sizeof(struct func_info), cmp_funcs);
  279. return 0;
  280. }
  281. static pthread_t mcount_sort_thread;
  282. static bool sort_reloc;
  283. static long rela_type;
  284. static char m_err[ERRSTR_MAXSZ];
  285. struct elf_mcount_loc {
  286. Elf_Ehdr *ehdr;
  287. Elf_Shdr *init_data_sec;
  288. uint64_t start_mcount_loc;
  289. uint64_t stop_mcount_loc;
  290. };
  291. /* Fill the array with the content of the relocs */
  292. static int fill_relocs(void *ptr, uint64_t size, Elf_Ehdr *ehdr, uint64_t start_loc)
  293. {
  294. Elf_Shdr *shdr_start;
  295. Elf_Rela *rel;
  296. unsigned int shnum;
  297. unsigned int count = 0;
  298. int shentsize;
  299. void *array_end = ptr + size;
  300. shdr_start = (Elf_Shdr *)((char *)ehdr + ehdr_shoff(ehdr));
  301. shentsize = ehdr_shentsize(ehdr);
  302. shnum = ehdr_shnum(ehdr);
  303. if (shnum == SHN_UNDEF)
  304. shnum = shdr_size(shdr_start);
  305. for (int i = 0; i < shnum; i++) {
  306. Elf_Shdr *shdr = get_index(shdr_start, shentsize, i);
  307. void *end;
  308. if (shdr_type(shdr) != SHT_RELA)
  309. continue;
  310. rel = (void *)ehdr + shdr_offset(shdr);
  311. end = (void *)rel + shdr_size(shdr);
  312. for (; (void *)rel < end; rel = (void *)rel + shdr_entsize(shdr)) {
  313. uint64_t offset = rela_offset(rel);
  314. if (offset >= start_loc && offset < start_loc + size) {
  315. if (ptr + long_size > array_end) {
  316. snprintf(m_err, ERRSTR_MAXSZ,
  317. "Too many relocations");
  318. return -1;
  319. }
  320. /* Make sure this has the correct type */
  321. if (rela_info(rel) != rela_type) {
  322. snprintf(m_err, ERRSTR_MAXSZ,
  323. "rela has type %lx but expected %lx\n",
  324. (long)rela_info(rel), rela_type);
  325. return -1;
  326. }
  327. if (long_size == 4)
  328. *(uint32_t *)ptr = rela_addend(rel);
  329. else
  330. *(uint64_t *)ptr = rela_addend(rel);
  331. ptr += long_size;
  332. count++;
  333. }
  334. }
  335. }
  336. return count;
  337. }
  338. /* Put the sorted vals back into the relocation elements */
  339. static void replace_relocs(void *ptr, uint64_t size, Elf_Ehdr *ehdr, uint64_t start_loc)
  340. {
  341. Elf_Shdr *shdr_start;
  342. Elf_Rela *rel;
  343. unsigned int shnum;
  344. int shentsize;
  345. shdr_start = (Elf_Shdr *)((char *)ehdr + ehdr_shoff(ehdr));
  346. shentsize = ehdr_shentsize(ehdr);
  347. shnum = ehdr_shnum(ehdr);
  348. if (shnum == SHN_UNDEF)
  349. shnum = shdr_size(shdr_start);
  350. for (int i = 0; i < shnum; i++) {
  351. Elf_Shdr *shdr = get_index(shdr_start, shentsize, i);
  352. void *end;
  353. if (shdr_type(shdr) != SHT_RELA)
  354. continue;
  355. rel = (void *)ehdr + shdr_offset(shdr);
  356. end = (void *)rel + shdr_size(shdr);
  357. for (; (void *)rel < end; rel = (void *)rel + shdr_entsize(shdr)) {
  358. uint64_t offset = rela_offset(rel);
  359. if (offset >= start_loc && offset < start_loc + size) {
  360. if (long_size == 4)
  361. rela_write_addend(rel, *(uint32_t *)ptr);
  362. else
  363. rela_write_addend(rel, *(uint64_t *)ptr);
  364. ptr += long_size;
  365. }
  366. }
  367. }
  368. }
  369. static int fill_addrs(void *ptr, uint64_t size, void *addrs)
  370. {
  371. void *end = ptr + size;
  372. int count = 0;
  373. for (; ptr < end; ptr += long_size, addrs += long_size, count++) {
  374. if (long_size == 4)
  375. *(uint32_t *)ptr = elf_parser.r(addrs);
  376. else
  377. *(uint64_t *)ptr = elf_parser.r8(addrs);
  378. }
  379. return count;
  380. }
  381. static void replace_addrs(void *ptr, uint64_t size, void *addrs)
  382. {
  383. void *end = ptr + size;
  384. for (; ptr < end; ptr += long_size, addrs += long_size) {
  385. if (long_size == 4)
  386. elf_parser.w(*(uint32_t *)ptr, addrs);
  387. else
  388. elf_parser.w8(*(uint64_t *)ptr, addrs);
  389. }
  390. }
  391. /* Sort the addresses stored between __start_mcount_loc to __stop_mcount_loc in vmlinux */
  392. static void *sort_mcount_loc(void *arg)
  393. {
  394. struct elf_mcount_loc *emloc = (struct elf_mcount_loc *)arg;
  395. uint64_t offset = emloc->start_mcount_loc - shdr_addr(emloc->init_data_sec)
  396. + shdr_offset(emloc->init_data_sec);
  397. uint64_t size = emloc->stop_mcount_loc - emloc->start_mcount_loc;
  398. unsigned char *start_loc = (void *)emloc->ehdr + offset;
  399. Elf_Ehdr *ehdr = emloc->ehdr;
  400. void *e_msg = NULL;
  401. void *vals;
  402. int count;
  403. vals = malloc(long_size * size);
  404. if (!vals) {
  405. snprintf(m_err, ERRSTR_MAXSZ, "Failed to allocate sort array");
  406. pthread_exit(m_err);
  407. }
  408. if (sort_reloc) {
  409. count = fill_relocs(vals, size, ehdr, emloc->start_mcount_loc);
  410. /* gcc may use relocs to save the addresses, but clang does not. */
  411. if (!count) {
  412. count = fill_addrs(vals, size, start_loc);
  413. sort_reloc = 0;
  414. }
  415. } else
  416. count = fill_addrs(vals, size, start_loc);
  417. if (count < 0) {
  418. e_msg = m_err;
  419. goto out;
  420. }
  421. if (count != size / long_size) {
  422. snprintf(m_err, ERRSTR_MAXSZ, "Expected %u mcount elements but found %u\n",
  423. (int)(size / long_size), count);
  424. e_msg = m_err;
  425. goto out;
  426. }
  427. /* zero out any locations not found by function list */
  428. if (function_list_size) {
  429. for (void *ptr = vals; ptr < vals + size; ptr += long_size) {
  430. uint64_t key;
  431. key = long_size == 4 ? *(uint32_t *)ptr : *(uint64_t *)ptr;
  432. if (!find_func(key)) {
  433. if (long_size == 4)
  434. *(uint32_t *)ptr = 0;
  435. else
  436. *(uint64_t *)ptr = 0;
  437. }
  438. }
  439. }
  440. compare_values = long_size == 4 ? compare_values_32 : compare_values_64;
  441. qsort(vals, count, long_size, compare_values);
  442. if (sort_reloc)
  443. replace_relocs(vals, size, ehdr, emloc->start_mcount_loc);
  444. else
  445. replace_addrs(vals, size, start_loc);
  446. out:
  447. free(vals);
  448. pthread_exit(e_msg);
  449. }
  450. /* Get the address of __start_mcount_loc and __stop_mcount_loc in System.map */
  451. static void get_mcount_loc(struct elf_mcount_loc *emloc, Elf_Shdr *symtab_sec,
  452. const char *strtab)
  453. {
  454. Elf_Sym *sym, *end_sym;
  455. int symentsize = shdr_entsize(symtab_sec);
  456. int found = 0;
  457. sym = (void *)emloc->ehdr + shdr_offset(symtab_sec);
  458. end_sym = (void *)sym + shdr_size(symtab_sec);
  459. while (sym < end_sym) {
  460. if (!strcmp(strtab + sym_name(sym), "__start_mcount_loc")) {
  461. emloc->start_mcount_loc = sym_value(sym);
  462. if (++found == 2)
  463. break;
  464. } else if (!strcmp(strtab + sym_name(sym), "__stop_mcount_loc")) {
  465. emloc->stop_mcount_loc = sym_value(sym);
  466. if (++found == 2)
  467. break;
  468. }
  469. sym = (void *)sym + symentsize;
  470. }
  471. if (!emloc->start_mcount_loc) {
  472. fprintf(stderr, "get start_mcount_loc error!");
  473. return;
  474. }
  475. if (!emloc->stop_mcount_loc) {
  476. fprintf(stderr, "get stop_mcount_loc error!");
  477. return;
  478. }
  479. }
  480. #else /* MCOUNT_SORT_ENABLED */
  481. static inline int parse_symbols(const char *fname) { return 0; }
  482. #endif
  483. static int do_sort(Elf_Ehdr *ehdr,
  484. char const *const fname,
  485. table_sort_t custom_sort)
  486. {
  487. int rc = -1;
  488. Elf_Shdr *shdr_start;
  489. Elf_Shdr *strtab_sec = NULL;
  490. Elf_Shdr *symtab_sec = NULL;
  491. Elf_Shdr *extab_sec = NULL;
  492. Elf_Shdr *string_sec;
  493. Elf_Sym *sym;
  494. const Elf_Sym *symtab;
  495. Elf32_Word *symtab_shndx = NULL;
  496. Elf_Sym *sort_needed_sym = NULL;
  497. Elf_Shdr *sort_needed_sec;
  498. uint32_t *sort_needed_loc;
  499. void *sym_start;
  500. void *sym_end;
  501. const char *secstrings;
  502. const char *strtab;
  503. char *extab_image;
  504. int sort_need_index;
  505. int symentsize;
  506. int shentsize;
  507. int idx;
  508. int i;
  509. unsigned int shnum;
  510. unsigned int shstrndx;
  511. #ifdef MCOUNT_SORT_ENABLED
  512. struct elf_mcount_loc mstruct = {0};
  513. #endif
  514. #ifdef UNWINDER_ORC_ENABLED
  515. unsigned int orc_ip_size = 0;
  516. unsigned int orc_size = 0;
  517. unsigned int orc_num_entries = 0;
  518. #endif
  519. shdr_start = (Elf_Shdr *)((char *)ehdr + ehdr_shoff(ehdr));
  520. shentsize = ehdr_shentsize(ehdr);
  521. shstrndx = ehdr_shstrndx(ehdr);
  522. if (shstrndx == SHN_XINDEX)
  523. shstrndx = shdr_link(shdr_start);
  524. string_sec = get_index(shdr_start, shentsize, shstrndx);
  525. secstrings = (const char *)ehdr + shdr_offset(string_sec);
  526. shnum = ehdr_shnum(ehdr);
  527. if (shnum == SHN_UNDEF)
  528. shnum = shdr_size(shdr_start);
  529. for (i = 0; i < shnum; i++) {
  530. Elf_Shdr *shdr = get_index(shdr_start, shentsize, i);
  531. idx = shdr_name(shdr);
  532. if (!strcmp(secstrings + idx, "__ex_table"))
  533. extab_sec = shdr;
  534. if (!strcmp(secstrings + idx, ".symtab"))
  535. symtab_sec = shdr;
  536. if (!strcmp(secstrings + idx, ".strtab"))
  537. strtab_sec = shdr;
  538. if (shdr_type(shdr) == SHT_SYMTAB_SHNDX)
  539. symtab_shndx = (Elf32_Word *)((const char *)ehdr +
  540. shdr_offset(shdr));
  541. #ifdef MCOUNT_SORT_ENABLED
  542. /* locate the .init.data section in vmlinux */
  543. if (!strcmp(secstrings + idx, ".init.data"))
  544. mstruct.init_data_sec = shdr;
  545. #endif
  546. #ifdef UNWINDER_ORC_ENABLED
  547. /* locate the ORC unwind tables */
  548. if (!strcmp(secstrings + idx, ".orc_unwind_ip")) {
  549. orc_ip_size = shdr_size(shdr);
  550. g_orc_ip_table = (int *)((void *)ehdr +
  551. shdr_offset(shdr));
  552. }
  553. if (!strcmp(secstrings + idx, ".orc_unwind")) {
  554. orc_size = shdr_size(shdr);
  555. g_orc_table = (struct orc_entry *)((void *)ehdr +
  556. shdr_offset(shdr));
  557. }
  558. #endif
  559. } /* for loop */
  560. #ifdef UNWINDER_ORC_ENABLED
  561. if (!g_orc_ip_table || !g_orc_table) {
  562. fprintf(stderr,
  563. "incomplete ORC unwind tables in file: %s\n", fname);
  564. goto out;
  565. }
  566. orc_num_entries = orc_ip_size / sizeof(int);
  567. if (orc_ip_size % sizeof(int) != 0 ||
  568. orc_size % sizeof(struct orc_entry) != 0 ||
  569. orc_num_entries != orc_size / sizeof(struct orc_entry)) {
  570. fprintf(stderr,
  571. "inconsistent ORC unwind table entries in file: %s\n",
  572. fname);
  573. goto out;
  574. }
  575. /* create thread to sort ORC unwind tables concurrently */
  576. if (pthread_create(&orc_sort_thread, NULL,
  577. sort_orctable, &orc_ip_size)) {
  578. fprintf(stderr,
  579. "pthread_create orc_sort_thread failed '%s': %s\n",
  580. strerror(errno), fname);
  581. goto out;
  582. }
  583. #endif
  584. if (!extab_sec) {
  585. fprintf(stderr, "no __ex_table in file: %s\n", fname);
  586. goto out;
  587. }
  588. if (!symtab_sec) {
  589. fprintf(stderr, "no .symtab in file: %s\n", fname);
  590. goto out;
  591. }
  592. if (!strtab_sec) {
  593. fprintf(stderr, "no .strtab in file: %s\n", fname);
  594. goto out;
  595. }
  596. extab_image = (void *)ehdr + shdr_offset(extab_sec);
  597. strtab = (const char *)ehdr + shdr_offset(strtab_sec);
  598. symtab = (const Elf_Sym *)((const char *)ehdr + shdr_offset(symtab_sec));
  599. #ifdef MCOUNT_SORT_ENABLED
  600. mstruct.ehdr = ehdr;
  601. get_mcount_loc(&mstruct, symtab_sec, strtab);
  602. if (!mstruct.init_data_sec || !mstruct.start_mcount_loc || !mstruct.stop_mcount_loc) {
  603. fprintf(stderr,
  604. "incomplete mcount's sort in file: %s\n",
  605. fname);
  606. goto out;
  607. }
  608. /* create thread to sort mcount_loc concurrently */
  609. if (pthread_create(&mcount_sort_thread, NULL, &sort_mcount_loc, &mstruct)) {
  610. fprintf(stderr,
  611. "pthread_create mcount_sort_thread failed '%s': %s\n",
  612. strerror(errno), fname);
  613. goto out;
  614. }
  615. #endif
  616. if (custom_sort) {
  617. custom_sort(extab_image, shdr_size(extab_sec));
  618. } else {
  619. int num_entries = shdr_size(extab_sec) / extable_ent_size;
  620. qsort(extab_image, num_entries,
  621. extable_ent_size, compare_extable);
  622. }
  623. /* find the flag main_extable_sort_needed */
  624. sym_start = (void *)ehdr + shdr_offset(symtab_sec);
  625. sym_end = sym_start + shdr_size(symtab_sec);
  626. symentsize = shdr_entsize(symtab_sec);
  627. for (sym = sym_start; (void *)sym + symentsize < sym_end;
  628. sym = (void *)sym + symentsize) {
  629. if (sym_type(sym) != STT_OBJECT)
  630. continue;
  631. if (!strcmp(strtab + sym_name(sym),
  632. "main_extable_sort_needed")) {
  633. sort_needed_sym = sym;
  634. break;
  635. }
  636. }
  637. if (!sort_needed_sym) {
  638. fprintf(stderr,
  639. "no main_extable_sort_needed symbol in file: %s\n",
  640. fname);
  641. goto out;
  642. }
  643. sort_need_index = get_secindex(sym_shndx(sym),
  644. ((void *)sort_needed_sym - (void *)symtab) / symentsize,
  645. symtab_shndx);
  646. sort_needed_sec = get_index(shdr_start, shentsize, sort_need_index);
  647. sort_needed_loc = (void *)ehdr +
  648. shdr_offset(sort_needed_sec) +
  649. sym_value(sort_needed_sym) - shdr_addr(sort_needed_sec);
  650. /* extable has been sorted, clear the flag */
  651. elf_parser.w(0, sort_needed_loc);
  652. rc = 0;
  653. out:
  654. #ifdef UNWINDER_ORC_ENABLED
  655. if (orc_sort_thread) {
  656. void *retval = NULL;
  657. /* wait for ORC tables sort done */
  658. rc = pthread_join(orc_sort_thread, &retval);
  659. if (rc) {
  660. fprintf(stderr,
  661. "pthread_join failed '%s': %s\n",
  662. strerror(errno), fname);
  663. } else if (retval) {
  664. rc = -1;
  665. fprintf(stderr,
  666. "failed to sort ORC tables '%s': %s\n",
  667. (char *)retval, fname);
  668. }
  669. }
  670. #endif
  671. #ifdef MCOUNT_SORT_ENABLED
  672. if (mcount_sort_thread) {
  673. void *retval = NULL;
  674. /* wait for mcount sort done */
  675. rc = pthread_join(mcount_sort_thread, &retval);
  676. if (rc) {
  677. fprintf(stderr,
  678. "pthread_join failed '%s': %s\n",
  679. strerror(errno), fname);
  680. } else if (retval) {
  681. rc = -1;
  682. fprintf(stderr,
  683. "failed to sort mcount '%s': %s\n",
  684. (char *)retval, fname);
  685. }
  686. }
  687. #endif
  688. return rc;
  689. }
  690. static int compare_relative_table(const void *a, const void *b)
  691. {
  692. int32_t av = (int32_t)elf_parser.r(a);
  693. int32_t bv = (int32_t)elf_parser.r(b);
  694. if (av < bv)
  695. return -1;
  696. if (av > bv)
  697. return 1;
  698. return 0;
  699. }
  700. static void sort_relative_table(char *extab_image, int image_size)
  701. {
  702. int i = 0;
  703. /*
  704. * Do the same thing the runtime sort does, first normalize to
  705. * being relative to the start of the section.
  706. */
  707. while (i < image_size) {
  708. uint32_t *loc = (uint32_t *)(extab_image + i);
  709. elf_parser.w(elf_parser.r(loc) + i, loc);
  710. i += 4;
  711. }
  712. qsort(extab_image, image_size / 8, 8, compare_relative_table);
  713. /* Now denormalize. */
  714. i = 0;
  715. while (i < image_size) {
  716. uint32_t *loc = (uint32_t *)(extab_image + i);
  717. elf_parser.w(elf_parser.r(loc) - i, loc);
  718. i += 4;
  719. }
  720. }
  721. static void sort_relative_table_with_data(char *extab_image, int image_size)
  722. {
  723. int i = 0;
  724. while (i < image_size) {
  725. uint32_t *loc = (uint32_t *)(extab_image + i);
  726. elf_parser.w(elf_parser.r(loc) + i, loc);
  727. elf_parser.w(elf_parser.r(loc + 1) + i + 4, loc + 1);
  728. /* Don't touch the fixup type or data */
  729. i += sizeof(uint32_t) * 3;
  730. }
  731. qsort(extab_image, image_size / 12, 12, compare_relative_table);
  732. i = 0;
  733. while (i < image_size) {
  734. uint32_t *loc = (uint32_t *)(extab_image + i);
  735. elf_parser.w(elf_parser.r(loc) - i, loc);
  736. elf_parser.w(elf_parser.r(loc + 1) - (i + 4), loc + 1);
  737. /* Don't touch the fixup type or data */
  738. i += sizeof(uint32_t) * 3;
  739. }
  740. }
  741. static int do_file(char const *const fname, void *addr)
  742. {
  743. Elf_Ehdr *ehdr = addr;
  744. table_sort_t custom_sort = NULL;
  745. switch (elf_map_machine(ehdr)) {
  746. case EM_AARCH64:
  747. #ifdef MCOUNT_SORT_ENABLED
  748. sort_reloc = true;
  749. rela_type = 0x403;
  750. /* arm64 uses patchable function entry placing before function */
  751. before_func = 8;
  752. #endif
  753. /* fallthrough */
  754. case EM_386:
  755. case EM_LOONGARCH:
  756. case EM_RISCV:
  757. case EM_S390:
  758. case EM_X86_64:
  759. custom_sort = sort_relative_table_with_data;
  760. break;
  761. case EM_PARISC:
  762. case EM_PPC:
  763. case EM_PPC64:
  764. custom_sort = sort_relative_table;
  765. break;
  766. case EM_ARCOMPACT:
  767. case EM_ARCV2:
  768. case EM_ARM:
  769. case EM_MICROBLAZE:
  770. case EM_MIPS:
  771. case EM_XTENSA:
  772. break;
  773. default:
  774. fprintf(stderr, "unrecognized e_machine %d %s\n",
  775. elf_parser.r2(&ehdr->e32.e_machine), fname);
  776. return -1;
  777. }
  778. switch (elf_map_long_size(addr)) {
  779. case 4:
  780. compare_extable = compare_extable_32,
  781. long_size = 4;
  782. extable_ent_size = 8;
  783. if (elf_parser.r2(&ehdr->e32.e_ehsize) != sizeof(Elf32_Ehdr) ||
  784. elf_parser.r2(&ehdr->e32.e_shentsize) != sizeof(Elf32_Shdr)) {
  785. fprintf(stderr,
  786. "unrecognized ET_EXEC/ET_DYN file: %s\n", fname);
  787. return -1;
  788. }
  789. break;
  790. case 8:
  791. compare_extable = compare_extable_64,
  792. long_size = 8;
  793. extable_ent_size = 16;
  794. if (elf_parser.r2(&ehdr->e64.e_ehsize) != sizeof(Elf64_Ehdr) ||
  795. elf_parser.r2(&ehdr->e64.e_shentsize) != sizeof(Elf64_Shdr)) {
  796. fprintf(stderr,
  797. "unrecognized ET_EXEC/ET_DYN file: %s\n",
  798. fname);
  799. return -1;
  800. }
  801. break;
  802. default:
  803. fprintf(stderr, "unrecognized ELF class %d %s\n",
  804. ehdr->e32.e_ident[EI_CLASS], fname);
  805. return -1;
  806. }
  807. return do_sort(ehdr, fname, custom_sort);
  808. }
  809. int main(int argc, char *argv[])
  810. {
  811. int i, n_error = 0; /* gcc-4.3.0 false positive complaint */
  812. size_t size = 0;
  813. void *addr = NULL;
  814. int c;
  815. while ((c = getopt(argc, argv, "s:")) >= 0) {
  816. switch (c) {
  817. case 's':
  818. if (parse_symbols(optarg) < 0) {
  819. fprintf(stderr, "Could not parse %s\n", optarg);
  820. return -1;
  821. }
  822. break;
  823. default:
  824. fprintf(stderr, "usage: sorttable [-s nm-file] vmlinux...\n");
  825. return 0;
  826. }
  827. }
  828. if ((argc - optind) < 1) {
  829. fprintf(stderr, "usage: sorttable vmlinux...\n");
  830. return 0;
  831. }
  832. /* Process each file in turn, allowing deep failure. */
  833. for (i = optind; i < argc; i++) {
  834. addr = elf_map(argv[i], &size, (1 << ET_EXEC) | (1 << ET_DYN));
  835. if (!addr) {
  836. ++n_error;
  837. continue;
  838. }
  839. if (do_file(argv[i], addr))
  840. ++n_error;
  841. elf_unmap(addr, size);
  842. }
  843. return !!n_error;
  844. }