kallsyms.c 18 KB

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