kallsyms.c 18 KB

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