kallsyms.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
  9. *
  10. * Table compression uses all the unused char codes on the symbols and
  11. * maps these to the most used substrings (tokens). For instance, it might
  12. * map char code 0xF7 to represent "write_" and then in every symbol where
  13. * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
  14. * The used codes themselves are also placed in the table so that the
  15. * decompresion can work without "special cases".
  16. * Applied to kernel symbols, this usually produces a compression ratio
  17. * of about 50%.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <limits.h>
  25. #ifndef ARRAY_SIZE
  26. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  27. #endif
  28. #define KSYM_NAME_LEN 128
  29. struct sym_entry {
  30. unsigned long long addr;
  31. unsigned int len;
  32. unsigned int start_pos;
  33. unsigned char *sym;
  34. unsigned int percpu_absolute;
  35. };
  36. struct addr_range {
  37. const char *start_sym, *end_sym;
  38. unsigned long long start, end;
  39. };
  40. static unsigned long long _text;
  41. static unsigned long long relative_base;
  42. static struct addr_range text_ranges[] = {
  43. { "_stext", "_etext" },
  44. { "_sinittext", "_einittext" },
  45. { "_stext_l1", "_etext_l1" }, /* Blackfin on-chip L1 inst SRAM */
  46. { "_stext_l2", "_etext_l2" }, /* Blackfin on-chip L2 SRAM */
  47. };
  48. #define text_range_text (&text_ranges[0])
  49. #define text_range_inittext (&text_ranges[1])
  50. static struct addr_range percpu_range = {
  51. "__per_cpu_start", "__per_cpu_end", -1ULL, 0
  52. };
  53. static struct sym_entry *table;
  54. static unsigned int table_size, table_cnt;
  55. static int all_symbols = 0;
  56. static int absolute_percpu = 0;
  57. static char symbol_prefix_char = '\0';
  58. static int base_relative = 0;
  59. int token_profit[0x10000];
  60. /* the table that holds the result of the compression */
  61. unsigned char best_table[256][2];
  62. unsigned char best_table_len[256];
  63. static void usage(void)
  64. {
  65. fprintf(stderr, "Usage: kallsyms [--all-symbols] "
  66. "[--symbol-prefix=<prefix char>] "
  67. "[--base-relative] < in.map > out.S\n");
  68. exit(1);
  69. }
  70. /*
  71. * This ignores the intensely annoying "mapping symbols" found
  72. * in ARM ELF files: $a, $t and $d.
  73. */
  74. static inline int is_arm_mapping_symbol(const char *str)
  75. {
  76. return str[0] == '$' && strchr("axtd", str[1])
  77. && (str[2] == '\0' || str[2] == '.');
  78. }
  79. static int check_symbol_range(const char *sym, unsigned long long addr,
  80. struct addr_range *ranges, int entries)
  81. {
  82. size_t i;
  83. struct addr_range *ar;
  84. for (i = 0; i < entries; ++i) {
  85. ar = &ranges[i];
  86. if (strcmp(sym, ar->start_sym) == 0) {
  87. ar->start = addr;
  88. return 0;
  89. } else if (strcmp(sym, ar->end_sym) == 0) {
  90. ar->end = addr;
  91. return 0;
  92. }
  93. }
  94. return 1;
  95. }
  96. static int read_symbol(FILE *in, struct sym_entry *s)
  97. {
  98. char str[500];
  99. char *sym, stype;
  100. int rc;
  101. rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
  102. if (rc != 3) {
  103. if (rc != EOF && fgets(str, 500, in) == NULL)
  104. fprintf(stderr, "Read error or end of file.\n");
  105. return -1;
  106. }
  107. if (strlen(str) > KSYM_NAME_LEN) {
  108. fprintf(stderr, "Symbol %s too long for kallsyms (%zu vs %d).\n"
  109. "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
  110. str, strlen(str), KSYM_NAME_LEN);
  111. return -1;
  112. }
  113. sym = str;
  114. /* skip prefix char */
  115. if (symbol_prefix_char && str[0] == symbol_prefix_char)
  116. sym++;
  117. /* Ignore most absolute/undefined (?) symbols. */
  118. if (strcmp(sym, "_text") == 0)
  119. _text = s->addr;
  120. else if (check_symbol_range(sym, s->addr, text_ranges,
  121. ARRAY_SIZE(text_ranges)) == 0)
  122. /* nothing to do */;
  123. else if (toupper(stype) == 'A')
  124. {
  125. /* Keep these useful absolute symbols */
  126. if (strcmp(sym, "__kernel_syscall_via_break") &&
  127. strcmp(sym, "__kernel_syscall_via_epc") &&
  128. strcmp(sym, "__kernel_sigtramp") &&
  129. strcmp(sym, "__gp"))
  130. return -1;
  131. }
  132. else if (toupper(stype) == 'U' ||
  133. is_arm_mapping_symbol(sym))
  134. return -1;
  135. /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
  136. else if (str[0] == '$')
  137. return -1;
  138. /* exclude debugging symbols */
  139. else if (stype == 'N' || stype == 'n')
  140. return -1;
  141. /* exclude s390 kasan local symbols */
  142. else if (!strncmp(sym, ".LASANPC", 8))
  143. return -1;
  144. /* include the type field in the symbol name, so that it gets
  145. * compressed together */
  146. s->len = strlen(str) + 1;
  147. s->sym = malloc(s->len + 1);
  148. if (!s->sym) {
  149. fprintf(stderr, "kallsyms failure: "
  150. "unable to allocate required amount of memory\n");
  151. exit(EXIT_FAILURE);
  152. }
  153. strcpy((char *)s->sym + 1, str);
  154. s->sym[0] = stype;
  155. s->percpu_absolute = 0;
  156. /* Record if we've found __per_cpu_start/end. */
  157. check_symbol_range(sym, s->addr, &percpu_range, 1);
  158. return 0;
  159. }
  160. static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
  161. int entries)
  162. {
  163. size_t i;
  164. struct addr_range *ar;
  165. for (i = 0; i < entries; ++i) {
  166. ar = &ranges[i];
  167. if (s->addr >= ar->start && s->addr <= ar->end)
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static int symbol_valid(struct sym_entry *s)
  173. {
  174. /* Symbols which vary between passes. Passes 1 and 2 must have
  175. * identical symbol lists. The kallsyms_* symbols below are only added
  176. * after pass 1, they would be included in pass 2 when --all-symbols is
  177. * specified so exclude them to get a stable symbol list.
  178. */
  179. static char *special_symbols[] = {
  180. "kallsyms_addresses",
  181. "kallsyms_offsets",
  182. "kallsyms_relative_base",
  183. "kallsyms_num_syms",
  184. "kallsyms_names",
  185. "kallsyms_markers",
  186. "kallsyms_token_table",
  187. "kallsyms_token_index",
  188. /* Exclude linker generated symbols which vary between passes */
  189. "_SDA_BASE_", /* ppc */
  190. "_SDA2_BASE_", /* ppc */
  191. NULL };
  192. static char *special_prefixes[] = {
  193. "__crc_", /* modversions */
  194. "__efistub_", /* arm64 EFI stub namespace */
  195. NULL };
  196. static char *special_suffixes[] = {
  197. "_veneer", /* arm */
  198. "_from_arm", /* arm */
  199. "_from_thumb", /* arm */
  200. NULL };
  201. int i;
  202. char *sym_name = (char *)s->sym + 1;
  203. /* skip prefix char */
  204. if (symbol_prefix_char && *sym_name == symbol_prefix_char)
  205. sym_name++;
  206. /* if --all-symbols is not specified, then symbols outside the text
  207. * and inittext sections are discarded */
  208. if (!all_symbols) {
  209. if (symbol_in_range(s, text_ranges,
  210. ARRAY_SIZE(text_ranges)) == 0)
  211. return 0;
  212. /* Corner case. Discard any symbols with the same value as
  213. * _etext _einittext; they can move between pass 1 and 2 when
  214. * the kallsyms data are added. If these symbols move then
  215. * they may get dropped in pass 2, which breaks the kallsyms
  216. * rules.
  217. */
  218. if ((s->addr == text_range_text->end &&
  219. strcmp(sym_name,
  220. text_range_text->end_sym)) ||
  221. (s->addr == text_range_inittext->end &&
  222. strcmp(sym_name,
  223. text_range_inittext->end_sym)))
  224. return 0;
  225. }
  226. /* Exclude symbols which vary between passes. */
  227. for (i = 0; special_symbols[i]; i++)
  228. if (strcmp(sym_name, special_symbols[i]) == 0)
  229. return 0;
  230. for (i = 0; special_prefixes[i]; i++) {
  231. int l = strlen(special_prefixes[i]);
  232. if (l <= strlen(sym_name) &&
  233. strncmp(sym_name, special_prefixes[i], l) == 0)
  234. return 0;
  235. }
  236. for (i = 0; special_suffixes[i]; i++) {
  237. int l = strlen(sym_name) - strlen(special_suffixes[i]);
  238. if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0)
  239. return 0;
  240. }
  241. return 1;
  242. }
  243. static void read_map(FILE *in)
  244. {
  245. while (!feof(in)) {
  246. if (table_cnt >= table_size) {
  247. table_size += 10000;
  248. table = realloc(table, sizeof(*table) * table_size);
  249. if (!table) {
  250. fprintf(stderr, "out of memory\n");
  251. exit (1);
  252. }
  253. }
  254. if (read_symbol(in, &table[table_cnt]) == 0) {
  255. table[table_cnt].start_pos = table_cnt;
  256. table_cnt++;
  257. }
  258. }
  259. }
  260. static void output_label(char *label)
  261. {
  262. if (symbol_prefix_char)
  263. printf(".globl %c%s\n", symbol_prefix_char, label);
  264. else
  265. printf(".globl %s\n", label);
  266. printf("\tALGN\n");
  267. if (symbol_prefix_char)
  268. printf("%c%s:\n", symbol_prefix_char, label);
  269. else
  270. printf("%s:\n", label);
  271. }
  272. /* uncompress a compressed symbol. When this function is called, the best table
  273. * might still be compressed itself, so the function needs to be recursive */
  274. static int expand_symbol(unsigned char *data, int len, char *result)
  275. {
  276. int c, rlen, total=0;
  277. while (len) {
  278. c = *data;
  279. /* if the table holds a single char that is the same as the one
  280. * we are looking for, then end the search */
  281. if (best_table[c][0]==c && best_table_len[c]==1) {
  282. *result++ = c;
  283. total++;
  284. } else {
  285. /* if not, recurse and expand */
  286. rlen = expand_symbol(best_table[c], best_table_len[c], result);
  287. total += rlen;
  288. result += rlen;
  289. }
  290. data++;
  291. len--;
  292. }
  293. *result=0;
  294. return total;
  295. }
  296. static int symbol_absolute(struct sym_entry *s)
  297. {
  298. return s->percpu_absolute;
  299. }
  300. static void write_src(void)
  301. {
  302. unsigned int i, k, off;
  303. unsigned int best_idx[256];
  304. unsigned int *markers;
  305. char buf[KSYM_NAME_LEN];
  306. printf("#include <asm/types.h>\n");
  307. printf("#if BITS_PER_LONG == 64\n");
  308. printf("#define PTR .quad\n");
  309. printf("#define ALGN .align 8\n");
  310. printf("#else\n");
  311. printf("#define PTR .long\n");
  312. printf("#define ALGN .align 4\n");
  313. printf("#endif\n");
  314. printf("\t.section .rodata, \"a\"\n");
  315. /* Provide proper symbols relocatability by their relativeness
  316. * to a fixed anchor point in the runtime image, either '_text'
  317. * for absolute address tables, in which case the linker will
  318. * emit the final addresses at build time. Otherwise, use the
  319. * offset relative to the lowest value encountered of all relative
  320. * symbols, and emit non-relocatable fixed offsets that will be fixed
  321. * up at runtime.
  322. *
  323. * The symbol names cannot be used to construct normal symbol
  324. * references as the list of symbols contains symbols that are
  325. * declared static and are private to their .o files. This prevents
  326. * .tmp_kallsyms.o or any other object from referencing them.
  327. */
  328. if (!base_relative)
  329. output_label("kallsyms_addresses");
  330. else
  331. output_label("kallsyms_offsets");
  332. for (i = 0; i < table_cnt; i++) {
  333. if (base_relative) {
  334. long long offset;
  335. int overflow;
  336. if (!absolute_percpu) {
  337. offset = table[i].addr - relative_base;
  338. overflow = (offset < 0 || offset > UINT_MAX);
  339. } else if (symbol_absolute(&table[i])) {
  340. offset = table[i].addr;
  341. overflow = (offset < 0 || offset > INT_MAX);
  342. } else {
  343. offset = relative_base - table[i].addr - 1;
  344. overflow = (offset < INT_MIN || offset >= 0);
  345. }
  346. if (overflow) {
  347. fprintf(stderr, "kallsyms failure: "
  348. "%s symbol value %#llx out of range in relative mode\n",
  349. symbol_absolute(&table[i]) ? "absolute" : "relative",
  350. table[i].addr);
  351. exit(EXIT_FAILURE);
  352. }
  353. printf("\t.long\t%#x\n", (int)offset);
  354. } else if (!symbol_absolute(&table[i])) {
  355. if (_text <= table[i].addr)
  356. printf("\tPTR\t_text + %#llx\n",
  357. table[i].addr - _text);
  358. else
  359. printf("\tPTR\t_text - %#llx\n",
  360. _text - table[i].addr);
  361. } else {
  362. printf("\tPTR\t%#llx\n", table[i].addr);
  363. }
  364. }
  365. printf("\n");
  366. if (base_relative) {
  367. output_label("kallsyms_relative_base");
  368. printf("\tPTR\t_text - %#llx\n", _text - relative_base);
  369. printf("\n");
  370. }
  371. output_label("kallsyms_num_syms");
  372. printf("\tPTR\t%d\n", table_cnt);
  373. printf("\n");
  374. /* table of offset markers, that give the offset in the compressed stream
  375. * every 256 symbols */
  376. markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
  377. if (!markers) {
  378. fprintf(stderr, "kallsyms failure: "
  379. "unable to allocate required memory\n");
  380. exit(EXIT_FAILURE);
  381. }
  382. output_label("kallsyms_names");
  383. off = 0;
  384. for (i = 0; i < table_cnt; i++) {
  385. if ((i & 0xFF) == 0)
  386. markers[i >> 8] = off;
  387. printf("\t.byte 0x%02x", table[i].len);
  388. for (k = 0; k < table[i].len; k++)
  389. printf(", 0x%02x", table[i].sym[k]);
  390. printf("\n");
  391. off += table[i].len + 1;
  392. }
  393. printf("\n");
  394. output_label("kallsyms_markers");
  395. for (i = 0; i < ((table_cnt + 255) >> 8); i++)
  396. printf("\tPTR\t%d\n", markers[i]);
  397. printf("\n");
  398. free(markers);
  399. output_label("kallsyms_token_table");
  400. off = 0;
  401. for (i = 0; i < 256; i++) {
  402. best_idx[i] = off;
  403. expand_symbol(best_table[i], best_table_len[i], buf);
  404. printf("\t.asciz\t\"%s\"\n", buf);
  405. off += strlen(buf) + 1;
  406. }
  407. printf("\n");
  408. output_label("kallsyms_token_index");
  409. for (i = 0; i < 256; i++)
  410. printf("\t.short\t%d\n", best_idx[i]);
  411. printf("\n");
  412. }
  413. /* table lookup compression functions */
  414. /* count all the possible tokens in a symbol */
  415. static void learn_symbol(unsigned char *symbol, int len)
  416. {
  417. int i;
  418. for (i = 0; i < len - 1; i++)
  419. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
  420. }
  421. /* decrease the count for all the possible tokens in a symbol */
  422. static void forget_symbol(unsigned char *symbol, int len)
  423. {
  424. int i;
  425. for (i = 0; i < len - 1; i++)
  426. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
  427. }
  428. /* remove all the invalid symbols from the table and do the initial token count */
  429. static void build_initial_tok_table(void)
  430. {
  431. unsigned int i, pos;
  432. pos = 0;
  433. for (i = 0; i < table_cnt; i++) {
  434. if ( symbol_valid(&table[i]) ) {
  435. if (pos != i)
  436. table[pos] = table[i];
  437. learn_symbol(table[pos].sym, table[pos].len);
  438. pos++;
  439. } else {
  440. free(table[i].sym);
  441. }
  442. }
  443. table_cnt = pos;
  444. }
  445. static void *find_token(unsigned char *str, int len, unsigned char *token)
  446. {
  447. int i;
  448. for (i = 0; i < len - 1; i++) {
  449. if (str[i] == token[0] && str[i+1] == token[1])
  450. return &str[i];
  451. }
  452. return NULL;
  453. }
  454. /* replace a given token in all the valid symbols. Use the sampled symbols
  455. * to update the counts */
  456. static void compress_symbols(unsigned char *str, int idx)
  457. {
  458. unsigned int i, len, size;
  459. unsigned char *p1, *p2;
  460. for (i = 0; i < table_cnt; i++) {
  461. len = table[i].len;
  462. p1 = table[i].sym;
  463. /* find the token on the symbol */
  464. p2 = find_token(p1, len, str);
  465. if (!p2) continue;
  466. /* decrease the counts for this symbol's tokens */
  467. forget_symbol(table[i].sym, len);
  468. size = len;
  469. do {
  470. *p2 = idx;
  471. p2++;
  472. size -= (p2 - p1);
  473. memmove(p2, p2 + 1, size);
  474. p1 = p2;
  475. len--;
  476. if (size < 2) break;
  477. /* find the token on the symbol */
  478. p2 = find_token(p1, size, str);
  479. } while (p2);
  480. table[i].len = len;
  481. /* increase the counts for this symbol's new tokens */
  482. learn_symbol(table[i].sym, len);
  483. }
  484. }
  485. /* search the token with the maximum profit */
  486. static int find_best_token(void)
  487. {
  488. int i, best, bestprofit;
  489. bestprofit=-10000;
  490. best = 0;
  491. for (i = 0; i < 0x10000; i++) {
  492. if (token_profit[i] > bestprofit) {
  493. best = i;
  494. bestprofit = token_profit[i];
  495. }
  496. }
  497. return best;
  498. }
  499. /* this is the core of the algorithm: calculate the "best" table */
  500. static void optimize_result(void)
  501. {
  502. int i, best;
  503. /* using the '\0' symbol last allows compress_symbols to use standard
  504. * fast string functions */
  505. for (i = 255; i >= 0; i--) {
  506. /* if this table slot is empty (it is not used by an actual
  507. * original char code */
  508. if (!best_table_len[i]) {
  509. /* find the token with the breates profit value */
  510. best = find_best_token();
  511. if (token_profit[best] == 0)
  512. break;
  513. /* place it in the "best" table */
  514. best_table_len[i] = 2;
  515. best_table[i][0] = best & 0xFF;
  516. best_table[i][1] = (best >> 8) & 0xFF;
  517. /* replace this token in all the valid symbols */
  518. compress_symbols(best_table[i], i);
  519. }
  520. }
  521. }
  522. /* start by placing the symbols that are actually used on the table */
  523. static void insert_real_symbols_in_table(void)
  524. {
  525. unsigned int i, j, c;
  526. memset(best_table, 0, sizeof(best_table));
  527. memset(best_table_len, 0, sizeof(best_table_len));
  528. for (i = 0; i < table_cnt; i++) {
  529. for (j = 0; j < table[i].len; j++) {
  530. c = table[i].sym[j];
  531. best_table[c][0]=c;
  532. best_table_len[c]=1;
  533. }
  534. }
  535. }
  536. static void optimize_token_table(void)
  537. {
  538. build_initial_tok_table();
  539. insert_real_symbols_in_table();
  540. /* When valid symbol is not registered, exit to error */
  541. if (!table_cnt) {
  542. fprintf(stderr, "No valid symbol.\n");
  543. exit(1);
  544. }
  545. optimize_result();
  546. }
  547. /* guess for "linker script provide" symbol */
  548. static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
  549. {
  550. const char *symbol = (char *)se->sym + 1;
  551. int len = se->len - 1;
  552. if (len < 8)
  553. return 0;
  554. if (symbol[0] != '_' || symbol[1] != '_')
  555. return 0;
  556. /* __start_XXXXX */
  557. if (!memcmp(symbol + 2, "start_", 6))
  558. return 1;
  559. /* __stop_XXXXX */
  560. if (!memcmp(symbol + 2, "stop_", 5))
  561. return 1;
  562. /* __end_XXXXX */
  563. if (!memcmp(symbol + 2, "end_", 4))
  564. return 1;
  565. /* __XXXXX_start */
  566. if (!memcmp(symbol + len - 6, "_start", 6))
  567. return 1;
  568. /* __XXXXX_end */
  569. if (!memcmp(symbol + len - 4, "_end", 4))
  570. return 1;
  571. return 0;
  572. }
  573. static int prefix_underscores_count(const char *str)
  574. {
  575. const char *tail = str;
  576. while (*tail == '_')
  577. tail++;
  578. return tail - str;
  579. }
  580. static int compare_symbols(const void *a, const void *b)
  581. {
  582. const struct sym_entry *sa;
  583. const struct sym_entry *sb;
  584. int wa, wb;
  585. sa = a;
  586. sb = b;
  587. /* sort by address first */
  588. if (sa->addr > sb->addr)
  589. return 1;
  590. if (sa->addr < sb->addr)
  591. return -1;
  592. /* sort by "weakness" type */
  593. wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
  594. wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
  595. if (wa != wb)
  596. return wa - wb;
  597. /* sort by "linker script provide" type */
  598. wa = may_be_linker_script_provide_symbol(sa);
  599. wb = may_be_linker_script_provide_symbol(sb);
  600. if (wa != wb)
  601. return wa - wb;
  602. /* sort by the number of prefix underscores */
  603. wa = prefix_underscores_count((const char *)sa->sym + 1);
  604. wb = prefix_underscores_count((const char *)sb->sym + 1);
  605. if (wa != wb)
  606. return wa - wb;
  607. /* sort by initial order, so that other symbols are left undisturbed */
  608. return sa->start_pos - sb->start_pos;
  609. }
  610. static void sort_symbols(void)
  611. {
  612. qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
  613. }
  614. static void make_percpus_absolute(void)
  615. {
  616. unsigned int i;
  617. for (i = 0; i < table_cnt; i++)
  618. if (symbol_in_range(&table[i], &percpu_range, 1)) {
  619. /*
  620. * Keep the 'A' override for percpu symbols to
  621. * ensure consistent behavior compared to older
  622. * versions of this tool.
  623. */
  624. table[i].sym[0] = 'A';
  625. table[i].percpu_absolute = 1;
  626. }
  627. }
  628. /* find the minimum non-absolute symbol address */
  629. static void record_relative_base(void)
  630. {
  631. unsigned int i;
  632. relative_base = -1ULL;
  633. for (i = 0; i < table_cnt; i++)
  634. if (!symbol_absolute(&table[i]) &&
  635. table[i].addr < relative_base)
  636. relative_base = table[i].addr;
  637. }
  638. int main(int argc, char **argv)
  639. {
  640. if (argc >= 2) {
  641. int i;
  642. for (i = 1; i < argc; i++) {
  643. if(strcmp(argv[i], "--all-symbols") == 0)
  644. all_symbols = 1;
  645. else if (strcmp(argv[i], "--absolute-percpu") == 0)
  646. absolute_percpu = 1;
  647. else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
  648. char *p = &argv[i][16];
  649. /* skip quote */
  650. if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
  651. p++;
  652. symbol_prefix_char = *p;
  653. } else if (strcmp(argv[i], "--base-relative") == 0)
  654. base_relative = 1;
  655. else
  656. usage();
  657. }
  658. } else if (argc != 1)
  659. usage();
  660. read_map(stdin);
  661. if (absolute_percpu)
  662. make_percpus_absolute();
  663. if (base_relative)
  664. record_relative_base();
  665. sort_symbols();
  666. optimize_token_table();
  667. write_src();
  668. return 0;
  669. }