kallsyms.c 19 KB

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