kallsyms.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* Generate assembler source containing symbol information
  2. *
  3. * Copyright 2002 by Kai Germaschewski
  4. *
  5. * This software may be used and distributed according to the terms
  6. * of the GNU General Public License, incorporated herein by reference.
  7. *
  8. * Usage: kallsyms [--all-symbols] [--absolute-percpu]
  9. * [--base-relative] [--lto-clang] in.map > out.S
  10. *
  11. * Table compression uses all the unused char codes on the symbols and
  12. * maps these to the most used substrings (tokens). For instance, it might
  13. * map char code 0xF7 to represent "write_" and then in every symbol where
  14. * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
  15. * The used codes themselves are also placed in the table so that the
  16. * decompresion can work without "special cases".
  17. * Applied to kernel symbols, this usually produces a compression ratio
  18. * of about 50%.
  19. *
  20. */
  21. #include <getopt.h>
  22. #include <stdbool.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <limits.h>
  28. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  29. #define _stringify_1(x) #x
  30. #define _stringify(x) _stringify_1(x)
  31. #define KSYM_NAME_LEN 512
  32. /*
  33. * A substantially bigger size than the current maximum.
  34. *
  35. * It cannot be defined as an expression because it gets stringified
  36. * for the fscanf() format string. Therefore, a _Static_assert() is
  37. * used instead to maintain the relationship with KSYM_NAME_LEN.
  38. */
  39. #define KSYM_NAME_LEN_BUFFER 2048
  40. _Static_assert(
  41. KSYM_NAME_LEN_BUFFER == KSYM_NAME_LEN * 4,
  42. "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN"
  43. );
  44. struct sym_entry {
  45. unsigned long long addr;
  46. unsigned int len;
  47. unsigned int seq;
  48. unsigned int start_pos;
  49. unsigned int percpu_absolute;
  50. unsigned char sym[];
  51. };
  52. struct addr_range {
  53. const char *start_sym, *end_sym;
  54. unsigned long long start, end;
  55. };
  56. static unsigned long long _text;
  57. static unsigned long long relative_base;
  58. static struct addr_range text_ranges[] = {
  59. { "_stext", "_etext" },
  60. { "_sinittext", "_einittext" },
  61. };
  62. #define text_range_text (&text_ranges[0])
  63. #define text_range_inittext (&text_ranges[1])
  64. static struct addr_range percpu_range = {
  65. "__per_cpu_start", "__per_cpu_end", -1ULL, 0
  66. };
  67. static struct sym_entry **table;
  68. static unsigned int table_size, table_cnt;
  69. static int all_symbols;
  70. static int absolute_percpu;
  71. static int base_relative;
  72. static int lto_clang;
  73. static int token_profit[0x10000];
  74. /* the table that holds the result of the compression */
  75. static unsigned char best_table[256][2];
  76. static unsigned char best_table_len[256];
  77. static void usage(void)
  78. {
  79. fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
  80. "[--base-relative] [--lto-clang] in.map > out.S\n");
  81. exit(1);
  82. }
  83. static char *sym_name(const struct sym_entry *s)
  84. {
  85. return (char *)s->sym + 1;
  86. }
  87. static bool is_ignored_symbol(const char *name, char type)
  88. {
  89. /* Symbol names that exactly match to the following are ignored.*/
  90. static const char * const ignored_symbols[] = {
  91. /*
  92. * Symbols which vary between passes. Passes 1 and 2 must have
  93. * identical symbol lists. The kallsyms_* symbols below are
  94. * only added after pass 1, they would be included in pass 2
  95. * when --all-symbols is specified so exclude them to get a
  96. * stable symbol list.
  97. */
  98. "kallsyms_addresses",
  99. "kallsyms_offsets",
  100. "kallsyms_relative_base",
  101. "kallsyms_num_syms",
  102. "kallsyms_names",
  103. "kallsyms_markers",
  104. "kallsyms_token_table",
  105. "kallsyms_token_index",
  106. "kallsyms_seqs_of_names",
  107. /* Exclude linker generated symbols which vary between passes */
  108. "_SDA_BASE_", /* ppc */
  109. "_SDA2_BASE_", /* ppc */
  110. NULL
  111. };
  112. /* Symbol names that begin with the following are ignored.*/
  113. static const char * const ignored_prefixes[] = {
  114. "__efistub_", /* arm64 EFI stub namespace */
  115. "__kvm_nvhe_$", /* arm64 local symbols in non-VHE KVM namespace */
  116. "__kvm_nvhe_.L", /* arm64 local symbols in non-VHE KVM namespace */
  117. "__AArch64ADRPThunk_", /* arm64 lld */
  118. "__ARMV5PILongThunk_", /* arm lld */
  119. "__ARMV7PILongThunk_",
  120. "__ThumbV7PILongThunk_",
  121. "__LA25Thunk_", /* mips lld */
  122. "__microLA25Thunk_",
  123. "__kcfi_typeid_", /* CFI type identifiers */
  124. NULL
  125. };
  126. /* Symbol names that end with the following are ignored.*/
  127. static const char * const ignored_suffixes[] = {
  128. "_from_arm", /* arm */
  129. "_from_thumb", /* arm */
  130. "_veneer", /* arm */
  131. NULL
  132. };
  133. /* Symbol names that contain the following are ignored.*/
  134. static const char * const ignored_matches[] = {
  135. ".long_branch.", /* ppc stub */
  136. ".plt_branch.", /* ppc stub */
  137. NULL
  138. };
  139. const char * const *p;
  140. for (p = ignored_symbols; *p; p++)
  141. if (!strcmp(name, *p))
  142. return true;
  143. for (p = ignored_prefixes; *p; p++)
  144. if (!strncmp(name, *p, strlen(*p)))
  145. return true;
  146. for (p = ignored_suffixes; *p; p++) {
  147. int l = strlen(name) - strlen(*p);
  148. if (l >= 0 && !strcmp(name + l, *p))
  149. return true;
  150. }
  151. for (p = ignored_matches; *p; p++) {
  152. if (strstr(name, *p))
  153. return true;
  154. }
  155. if (type == 'U' || type == 'u')
  156. return true;
  157. /* exclude debugging symbols */
  158. if (type == 'N' || type == 'n')
  159. return true;
  160. if (toupper(type) == 'A') {
  161. /* Keep these useful absolute symbols */
  162. if (strcmp(name, "__kernel_syscall_via_break") &&
  163. strcmp(name, "__kernel_syscall_via_epc") &&
  164. strcmp(name, "__kernel_sigtramp") &&
  165. strcmp(name, "__gp"))
  166. return true;
  167. }
  168. return false;
  169. }
  170. static void check_symbol_range(const char *sym, unsigned long long addr,
  171. struct addr_range *ranges, int entries)
  172. {
  173. size_t i;
  174. struct addr_range *ar;
  175. for (i = 0; i < entries; ++i) {
  176. ar = &ranges[i];
  177. if (strcmp(sym, ar->start_sym) == 0) {
  178. ar->start = addr;
  179. return;
  180. } else if (strcmp(sym, ar->end_sym) == 0) {
  181. ar->end = addr;
  182. return;
  183. }
  184. }
  185. }
  186. static struct sym_entry *read_symbol(FILE *in)
  187. {
  188. char name[KSYM_NAME_LEN_BUFFER+1], type;
  189. unsigned long long addr;
  190. unsigned int len;
  191. struct sym_entry *sym;
  192. int rc;
  193. rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
  194. if (rc != 3) {
  195. if (rc != EOF && fgets(name, ARRAY_SIZE(name), in) == NULL)
  196. fprintf(stderr, "Read error or end of file.\n");
  197. return NULL;
  198. }
  199. if (strlen(name) >= KSYM_NAME_LEN) {
  200. fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
  201. "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
  202. name, strlen(name), KSYM_NAME_LEN);
  203. return NULL;
  204. }
  205. if (strcmp(name, "_text") == 0)
  206. _text = addr;
  207. /* Ignore most absolute/undefined (?) symbols. */
  208. if (is_ignored_symbol(name, type))
  209. return NULL;
  210. check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
  211. check_symbol_range(name, addr, &percpu_range, 1);
  212. /* include the type field in the symbol name, so that it gets
  213. * compressed together */
  214. len = strlen(name) + 1;
  215. sym = malloc(sizeof(*sym) + len + 1);
  216. if (!sym) {
  217. fprintf(stderr, "kallsyms failure: "
  218. "unable to allocate required amount of memory\n");
  219. exit(EXIT_FAILURE);
  220. }
  221. sym->addr = addr;
  222. sym->len = len;
  223. sym->sym[0] = type;
  224. strcpy(sym_name(sym), name);
  225. sym->percpu_absolute = 0;
  226. return sym;
  227. }
  228. static int symbol_in_range(const struct sym_entry *s,
  229. const struct addr_range *ranges, int entries)
  230. {
  231. size_t i;
  232. const struct addr_range *ar;
  233. for (i = 0; i < entries; ++i) {
  234. ar = &ranges[i];
  235. if (s->addr >= ar->start && s->addr <= ar->end)
  236. return 1;
  237. }
  238. return 0;
  239. }
  240. static int symbol_valid(const struct sym_entry *s)
  241. {
  242. const char *name = sym_name(s);
  243. /* if --all-symbols is not specified, then symbols outside the text
  244. * and inittext sections are discarded */
  245. if (!all_symbols) {
  246. if (symbol_in_range(s, text_ranges,
  247. ARRAY_SIZE(text_ranges)) == 0)
  248. return 0;
  249. /* Corner case. Discard any symbols with the same value as
  250. * _etext _einittext; they can move between pass 1 and 2 when
  251. * the kallsyms data are added. If these symbols move then
  252. * they may get dropped in pass 2, which breaks the kallsyms
  253. * rules.
  254. */
  255. if ((s->addr == text_range_text->end &&
  256. strcmp(name, text_range_text->end_sym)) ||
  257. (s->addr == text_range_inittext->end &&
  258. strcmp(name, text_range_inittext->end_sym)))
  259. return 0;
  260. }
  261. return 1;
  262. }
  263. /* remove all the invalid symbols from the table */
  264. static void shrink_table(void)
  265. {
  266. unsigned int i, pos;
  267. pos = 0;
  268. for (i = 0; i < table_cnt; i++) {
  269. if (symbol_valid(table[i])) {
  270. if (pos != i)
  271. table[pos] = table[i];
  272. pos++;
  273. } else {
  274. free(table[i]);
  275. }
  276. }
  277. table_cnt = pos;
  278. /* When valid symbol is not registered, exit to error */
  279. if (!table_cnt) {
  280. fprintf(stderr, "No valid symbol.\n");
  281. exit(1);
  282. }
  283. }
  284. static void read_map(const char *in)
  285. {
  286. FILE *fp;
  287. struct sym_entry *sym;
  288. fp = fopen(in, "r");
  289. if (!fp) {
  290. perror(in);
  291. exit(1);
  292. }
  293. while (!feof(fp)) {
  294. sym = read_symbol(fp);
  295. if (!sym)
  296. continue;
  297. sym->start_pos = table_cnt;
  298. if (table_cnt >= table_size) {
  299. table_size += 10000;
  300. table = realloc(table, sizeof(*table) * table_size);
  301. if (!table) {
  302. fprintf(stderr, "out of memory\n");
  303. fclose(fp);
  304. exit (1);
  305. }
  306. }
  307. table[table_cnt++] = sym;
  308. }
  309. fclose(fp);
  310. }
  311. static void output_label(const char *label)
  312. {
  313. printf(".globl %s\n", label);
  314. printf("\tALGN\n");
  315. printf("%s:\n", label);
  316. }
  317. /* Provide proper symbols relocatability by their '_text' relativeness. */
  318. static void output_address(unsigned long long addr)
  319. {
  320. if (_text <= addr)
  321. printf("\tPTR\t_text + %#llx\n", addr - _text);
  322. else
  323. printf("\tPTR\t_text - %#llx\n", _text - addr);
  324. }
  325. /* uncompress a compressed symbol. When this function is called, the best table
  326. * might still be compressed itself, so the function needs to be recursive */
  327. static int expand_symbol(const unsigned char *data, int len, char *result)
  328. {
  329. int c, rlen, total=0;
  330. while (len) {
  331. c = *data;
  332. /* if the table holds a single char that is the same as the one
  333. * we are looking for, then end the search */
  334. if (best_table[c][0]==c && best_table_len[c]==1) {
  335. *result++ = c;
  336. total++;
  337. } else {
  338. /* if not, recurse and expand */
  339. rlen = expand_symbol(best_table[c], best_table_len[c], result);
  340. total += rlen;
  341. result += rlen;
  342. }
  343. data++;
  344. len--;
  345. }
  346. *result=0;
  347. return total;
  348. }
  349. static int symbol_absolute(const struct sym_entry *s)
  350. {
  351. return s->percpu_absolute;
  352. }
  353. static char * s_name(char *buf)
  354. {
  355. /* Skip the symbol type */
  356. return buf + 1;
  357. }
  358. static void cleanup_symbol_name(char *s)
  359. {
  360. char *p;
  361. if (!lto_clang)
  362. return;
  363. /*
  364. * ASCII[.] = 2e
  365. * ASCII[0-9] = 30,39
  366. * ASCII[A-Z] = 41,5a
  367. * ASCII[_] = 5f
  368. * ASCII[a-z] = 61,7a
  369. *
  370. * As above, replacing the first '.' in ".llvm." with '\0' does not
  371. * affect the main sorting, but it helps us with subsorting.
  372. */
  373. p = strstr(s, ".llvm.");
  374. if (p)
  375. *p = '\0';
  376. }
  377. static int compare_names(const void *a, const void *b)
  378. {
  379. int ret;
  380. char sa_namebuf[KSYM_NAME_LEN];
  381. char sb_namebuf[KSYM_NAME_LEN];
  382. const struct sym_entry *sa = *(const struct sym_entry **)a;
  383. const struct sym_entry *sb = *(const struct sym_entry **)b;
  384. expand_symbol(sa->sym, sa->len, sa_namebuf);
  385. expand_symbol(sb->sym, sb->len, sb_namebuf);
  386. cleanup_symbol_name(s_name(sa_namebuf));
  387. cleanup_symbol_name(s_name(sb_namebuf));
  388. ret = strcmp(s_name(sa_namebuf), s_name(sb_namebuf));
  389. if (!ret) {
  390. if (sa->addr > sb->addr)
  391. return 1;
  392. else if (sa->addr < sb->addr)
  393. return -1;
  394. /* keep old order */
  395. return (int)(sa->seq - sb->seq);
  396. }
  397. return ret;
  398. }
  399. static void sort_symbols_by_name(void)
  400. {
  401. qsort(table, table_cnt, sizeof(table[0]), compare_names);
  402. }
  403. static void write_src(void)
  404. {
  405. unsigned int i, k, off;
  406. unsigned int best_idx[256];
  407. unsigned int *markers;
  408. char buf[KSYM_NAME_LEN];
  409. printf("#include <asm/bitsperlong.h>\n");
  410. printf("#if BITS_PER_LONG == 64\n");
  411. printf("#define PTR .quad\n");
  412. printf("#define ALGN .balign 8\n");
  413. printf("#else\n");
  414. printf("#define PTR .long\n");
  415. printf("#define ALGN .balign 4\n");
  416. printf("#endif\n");
  417. printf("\t.section .rodata, \"a\"\n");
  418. if (!base_relative)
  419. output_label("kallsyms_addresses");
  420. else
  421. output_label("kallsyms_offsets");
  422. for (i = 0; i < table_cnt; i++) {
  423. if (base_relative) {
  424. /*
  425. * Use the offset relative to the lowest value
  426. * encountered of all relative symbols, and emit
  427. * non-relocatable fixed offsets that will be fixed
  428. * up at runtime.
  429. */
  430. long long offset;
  431. int overflow;
  432. if (!absolute_percpu) {
  433. offset = table[i]->addr - relative_base;
  434. overflow = (offset < 0 || offset > UINT_MAX);
  435. } else if (symbol_absolute(table[i])) {
  436. offset = table[i]->addr;
  437. overflow = (offset < 0 || offset > INT_MAX);
  438. } else {
  439. offset = relative_base - table[i]->addr - 1;
  440. overflow = (offset < INT_MIN || offset >= 0);
  441. }
  442. if (overflow) {
  443. fprintf(stderr, "kallsyms failure: "
  444. "%s symbol value %#llx out of range in relative mode\n",
  445. symbol_absolute(table[i]) ? "absolute" : "relative",
  446. table[i]->addr);
  447. exit(EXIT_FAILURE);
  448. }
  449. printf("\t.long\t%#x\n", (int)offset);
  450. } else if (!symbol_absolute(table[i])) {
  451. output_address(table[i]->addr);
  452. } else {
  453. printf("\tPTR\t%#llx\n", table[i]->addr);
  454. }
  455. }
  456. printf("\n");
  457. if (base_relative) {
  458. output_label("kallsyms_relative_base");
  459. output_address(relative_base);
  460. printf("\n");
  461. }
  462. output_label("kallsyms_num_syms");
  463. printf("\t.long\t%u\n", table_cnt);
  464. printf("\n");
  465. /* table of offset markers, that give the offset in the compressed stream
  466. * every 256 symbols */
  467. markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
  468. if (!markers) {
  469. fprintf(stderr, "kallsyms failure: "
  470. "unable to allocate required memory\n");
  471. exit(EXIT_FAILURE);
  472. }
  473. output_label("kallsyms_names");
  474. off = 0;
  475. for (i = 0; i < table_cnt; i++) {
  476. if ((i & 0xFF) == 0)
  477. markers[i >> 8] = off;
  478. table[i]->seq = i;
  479. /* There cannot be any symbol of length zero. */
  480. if (table[i]->len == 0) {
  481. fprintf(stderr, "kallsyms failure: "
  482. "unexpected zero symbol length\n");
  483. exit(EXIT_FAILURE);
  484. }
  485. /* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
  486. if (table[i]->len > 0x3FFF) {
  487. fprintf(stderr, "kallsyms failure: "
  488. "unexpected huge symbol length\n");
  489. exit(EXIT_FAILURE);
  490. }
  491. /* Encode length with ULEB128. */
  492. if (table[i]->len <= 0x7F) {
  493. /* Most symbols use a single byte for the length. */
  494. printf("\t.byte 0x%02x", table[i]->len);
  495. off += table[i]->len + 1;
  496. } else {
  497. /* "Big" symbols use two bytes. */
  498. printf("\t.byte 0x%02x, 0x%02x",
  499. (table[i]->len & 0x7F) | 0x80,
  500. (table[i]->len >> 7) & 0x7F);
  501. off += table[i]->len + 2;
  502. }
  503. for (k = 0; k < table[i]->len; k++)
  504. printf(", 0x%02x", table[i]->sym[k]);
  505. printf("\n");
  506. }
  507. printf("\n");
  508. output_label("kallsyms_markers");
  509. for (i = 0; i < ((table_cnt + 255) >> 8); i++)
  510. printf("\t.long\t%u\n", markers[i]);
  511. printf("\n");
  512. free(markers);
  513. sort_symbols_by_name();
  514. output_label("kallsyms_seqs_of_names");
  515. for (i = 0; i < table_cnt; i++)
  516. printf("\t.byte 0x%02x, 0x%02x, 0x%02x\n",
  517. (unsigned char)(table[i]->seq >> 16),
  518. (unsigned char)(table[i]->seq >> 8),
  519. (unsigned char)(table[i]->seq >> 0));
  520. printf("\n");
  521. output_label("kallsyms_token_table");
  522. off = 0;
  523. for (i = 0; i < 256; i++) {
  524. best_idx[i] = off;
  525. expand_symbol(best_table[i], best_table_len[i], buf);
  526. printf("\t.asciz\t\"%s\"\n", buf);
  527. off += strlen(buf) + 1;
  528. }
  529. printf("\n");
  530. output_label("kallsyms_token_index");
  531. for (i = 0; i < 256; i++)
  532. printf("\t.short\t%d\n", best_idx[i]);
  533. printf("\n");
  534. }
  535. /* table lookup compression functions */
  536. /* count all the possible tokens in a symbol */
  537. static void learn_symbol(const unsigned char *symbol, int len)
  538. {
  539. int i;
  540. for (i = 0; i < len - 1; i++)
  541. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
  542. }
  543. /* decrease the count for all the possible tokens in a symbol */
  544. static void forget_symbol(const unsigned char *symbol, int len)
  545. {
  546. int i;
  547. for (i = 0; i < len - 1; i++)
  548. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
  549. }
  550. /* do the initial token count */
  551. static void build_initial_tok_table(void)
  552. {
  553. unsigned int i;
  554. for (i = 0; i < table_cnt; i++)
  555. learn_symbol(table[i]->sym, table[i]->len);
  556. }
  557. static unsigned char *find_token(unsigned char *str, int len,
  558. const unsigned char *token)
  559. {
  560. int i;
  561. for (i = 0; i < len - 1; i++) {
  562. if (str[i] == token[0] && str[i+1] == token[1])
  563. return &str[i];
  564. }
  565. return NULL;
  566. }
  567. /* replace a given token in all the valid symbols. Use the sampled symbols
  568. * to update the counts */
  569. static void compress_symbols(const unsigned char *str, int idx)
  570. {
  571. unsigned int i, len, size;
  572. unsigned char *p1, *p2;
  573. for (i = 0; i < table_cnt; i++) {
  574. len = table[i]->len;
  575. p1 = table[i]->sym;
  576. /* find the token on the symbol */
  577. p2 = find_token(p1, len, str);
  578. if (!p2) continue;
  579. /* decrease the counts for this symbol's tokens */
  580. forget_symbol(table[i]->sym, len);
  581. size = len;
  582. do {
  583. *p2 = idx;
  584. p2++;
  585. size -= (p2 - p1);
  586. memmove(p2, p2 + 1, size);
  587. p1 = p2;
  588. len--;
  589. if (size < 2) break;
  590. /* find the token on the symbol */
  591. p2 = find_token(p1, size, str);
  592. } while (p2);
  593. table[i]->len = len;
  594. /* increase the counts for this symbol's new tokens */
  595. learn_symbol(table[i]->sym, len);
  596. }
  597. }
  598. /* search the token with the maximum profit */
  599. static int find_best_token(void)
  600. {
  601. int i, best, bestprofit;
  602. bestprofit=-10000;
  603. best = 0;
  604. for (i = 0; i < 0x10000; i++) {
  605. if (token_profit[i] > bestprofit) {
  606. best = i;
  607. bestprofit = token_profit[i];
  608. }
  609. }
  610. return best;
  611. }
  612. /* this is the core of the algorithm: calculate the "best" table */
  613. static void optimize_result(void)
  614. {
  615. int i, best;
  616. /* using the '\0' symbol last allows compress_symbols to use standard
  617. * fast string functions */
  618. for (i = 255; i >= 0; i--) {
  619. /* if this table slot is empty (it is not used by an actual
  620. * original char code */
  621. if (!best_table_len[i]) {
  622. /* find the token with the best profit value */
  623. best = find_best_token();
  624. if (token_profit[best] == 0)
  625. break;
  626. /* place it in the "best" table */
  627. best_table_len[i] = 2;
  628. best_table[i][0] = best & 0xFF;
  629. best_table[i][1] = (best >> 8) & 0xFF;
  630. /* replace this token in all the valid symbols */
  631. compress_symbols(best_table[i], i);
  632. }
  633. }
  634. }
  635. /* start by placing the symbols that are actually used on the table */
  636. static void insert_real_symbols_in_table(void)
  637. {
  638. unsigned int i, j, c;
  639. for (i = 0; i < table_cnt; i++) {
  640. for (j = 0; j < table[i]->len; j++) {
  641. c = table[i]->sym[j];
  642. best_table[c][0]=c;
  643. best_table_len[c]=1;
  644. }
  645. }
  646. }
  647. static void optimize_token_table(void)
  648. {
  649. build_initial_tok_table();
  650. insert_real_symbols_in_table();
  651. optimize_result();
  652. }
  653. /* guess for "linker script provide" symbol */
  654. static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
  655. {
  656. const char *symbol = sym_name(se);
  657. int len = se->len - 1;
  658. if (len < 8)
  659. return 0;
  660. if (symbol[0] != '_' || symbol[1] != '_')
  661. return 0;
  662. /* __start_XXXXX */
  663. if (!memcmp(symbol + 2, "start_", 6))
  664. return 1;
  665. /* __stop_XXXXX */
  666. if (!memcmp(symbol + 2, "stop_", 5))
  667. return 1;
  668. /* __end_XXXXX */
  669. if (!memcmp(symbol + 2, "end_", 4))
  670. return 1;
  671. /* __XXXXX_start */
  672. if (!memcmp(symbol + len - 6, "_start", 6))
  673. return 1;
  674. /* __XXXXX_end */
  675. if (!memcmp(symbol + len - 4, "_end", 4))
  676. return 1;
  677. return 0;
  678. }
  679. static int compare_symbols(const void *a, const void *b)
  680. {
  681. const struct sym_entry *sa = *(const struct sym_entry **)a;
  682. const struct sym_entry *sb = *(const struct sym_entry **)b;
  683. int wa, wb;
  684. /* sort by address first */
  685. if (sa->addr > sb->addr)
  686. return 1;
  687. if (sa->addr < sb->addr)
  688. return -1;
  689. /* sort by "weakness" type */
  690. wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
  691. wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
  692. if (wa != wb)
  693. return wa - wb;
  694. /* sort by "linker script provide" type */
  695. wa = may_be_linker_script_provide_symbol(sa);
  696. wb = may_be_linker_script_provide_symbol(sb);
  697. if (wa != wb)
  698. return wa - wb;
  699. /* sort by the number of prefix underscores */
  700. wa = strspn(sym_name(sa), "_");
  701. wb = strspn(sym_name(sb), "_");
  702. if (wa != wb)
  703. return wa - wb;
  704. /* sort by initial order, so that other symbols are left undisturbed */
  705. return sa->start_pos - sb->start_pos;
  706. }
  707. static void sort_symbols(void)
  708. {
  709. qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
  710. }
  711. static void make_percpus_absolute(void)
  712. {
  713. unsigned int i;
  714. for (i = 0; i < table_cnt; i++)
  715. if (symbol_in_range(table[i], &percpu_range, 1)) {
  716. /*
  717. * Keep the 'A' override for percpu symbols to
  718. * ensure consistent behavior compared to older
  719. * versions of this tool.
  720. */
  721. table[i]->sym[0] = 'A';
  722. table[i]->percpu_absolute = 1;
  723. }
  724. }
  725. /* find the minimum non-absolute symbol address */
  726. static void record_relative_base(void)
  727. {
  728. unsigned int i;
  729. for (i = 0; i < table_cnt; i++)
  730. if (!symbol_absolute(table[i])) {
  731. /*
  732. * The table is sorted by address.
  733. * Take the first non-absolute symbol value.
  734. */
  735. relative_base = table[i]->addr;
  736. return;
  737. }
  738. }
  739. int main(int argc, char **argv)
  740. {
  741. while (1) {
  742. static struct option long_options[] = {
  743. {"all-symbols", no_argument, &all_symbols, 1},
  744. {"absolute-percpu", no_argument, &absolute_percpu, 1},
  745. {"base-relative", no_argument, &base_relative, 1},
  746. {"lto-clang", no_argument, &lto_clang, 1},
  747. {},
  748. };
  749. int c = getopt_long(argc, argv, "", long_options, NULL);
  750. if (c == -1)
  751. break;
  752. if (c != 0)
  753. usage();
  754. }
  755. if (optind >= argc)
  756. usage();
  757. read_map(argv[optind]);
  758. shrink_table();
  759. if (absolute_percpu)
  760. make_percpus_absolute();
  761. sort_symbols();
  762. if (base_relative)
  763. record_relative_base();
  764. optimize_token_table();
  765. write_src();
  766. return 0;
  767. }