kallsyms.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. * ChangeLog:
  11. *
  12. * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
  13. * Changed the compression method from stem compression to "table lookup"
  14. * compression
  15. *
  16. * Table compression uses all the unused char codes on the symbols and
  17. * maps these to the most used substrings (tokens). For instance, it might
  18. * map char code 0xF7 to represent "write_" and then in every symbol where
  19. * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
  20. * The used codes themselves are also placed in the table so that the
  21. * decompresion can work without "special cases".
  22. * Applied to kernel symbols, this usually produces a compression ratio
  23. * of about 50%.
  24. *
  25. */
  26. #define _GNU_SOURCE
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #define KSYM_NAME_LEN 127
  32. struct sym_entry {
  33. unsigned long long addr;
  34. unsigned int len;
  35. unsigned char *sym;
  36. };
  37. static struct sym_entry *table;
  38. static unsigned int table_size, table_cnt;
  39. static unsigned long long _text, _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext;
  40. static int all_symbols = 0;
  41. static char symbol_prefix_char = '\0';
  42. int token_profit[0x10000];
  43. /* the table that holds the result of the compression */
  44. unsigned char best_table[256][2];
  45. unsigned char best_table_len[256];
  46. static void usage(void)
  47. {
  48. fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
  49. exit(1);
  50. }
  51. /*
  52. * This ignores the intensely annoying "mapping symbols" found
  53. * in ARM ELF files: $a, $t and $d.
  54. */
  55. static inline int is_arm_mapping_symbol(const char *str)
  56. {
  57. return str[0] == '$' && strchr("atd", str[1])
  58. && (str[2] == '\0' || str[2] == '.');
  59. }
  60. static int read_symbol(FILE *in, struct sym_entry *s)
  61. {
  62. char str[500];
  63. char *sym, stype;
  64. int rc;
  65. rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
  66. if (rc != 3) {
  67. if (rc != EOF) {
  68. /* skip line */
  69. fgets(str, 500, in);
  70. }
  71. return -1;
  72. }
  73. sym = str;
  74. /* skip prefix char */
  75. if (symbol_prefix_char && str[0] == symbol_prefix_char)
  76. sym++;
  77. /* Ignore most absolute/undefined (?) symbols. */
  78. if (strcmp(sym, "_text") == 0)
  79. _text = s->addr;
  80. else if (strcmp(sym, "_stext") == 0)
  81. _stext = s->addr;
  82. else if (strcmp(sym, "_etext") == 0)
  83. _etext = s->addr;
  84. else if (strcmp(sym, "_sinittext") == 0)
  85. _sinittext = s->addr;
  86. else if (strcmp(sym, "_einittext") == 0)
  87. _einittext = s->addr;
  88. else if (strcmp(sym, "_sextratext") == 0)
  89. _sextratext = s->addr;
  90. else if (strcmp(sym, "_eextratext") == 0)
  91. _eextratext = s->addr;
  92. else if (toupper(stype) == 'A')
  93. {
  94. /* Keep these useful absolute symbols */
  95. if (strcmp(sym, "__kernel_syscall_via_break") &&
  96. strcmp(sym, "__kernel_syscall_via_epc") &&
  97. strcmp(sym, "__kernel_sigtramp") &&
  98. strcmp(sym, "__gp"))
  99. return -1;
  100. }
  101. else if (toupper(stype) == 'U' ||
  102. is_arm_mapping_symbol(sym))
  103. return -1;
  104. /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
  105. else if (str[0] == '$')
  106. return -1;
  107. /* include the type field in the symbol name, so that it gets
  108. * compressed together */
  109. s->len = strlen(str) + 1;
  110. s->sym = malloc(s->len + 1);
  111. if (!s->sym) {
  112. fprintf(stderr, "kallsyms failure: "
  113. "unable to allocate required amount of memory\n");
  114. exit(EXIT_FAILURE);
  115. }
  116. strcpy((char *)s->sym + 1, str);
  117. s->sym[0] = stype;
  118. return 0;
  119. }
  120. static int symbol_valid(struct sym_entry *s)
  121. {
  122. /* Symbols which vary between passes. Passes 1 and 2 must have
  123. * identical symbol lists. The kallsyms_* symbols below are only added
  124. * after pass 1, they would be included in pass 2 when --all-symbols is
  125. * specified so exclude them to get a stable symbol list.
  126. */
  127. static char *special_symbols[] = {
  128. "kallsyms_addresses",
  129. "kallsyms_num_syms",
  130. "kallsyms_names",
  131. "kallsyms_markers",
  132. "kallsyms_token_table",
  133. "kallsyms_token_index",
  134. /* Exclude linker generated symbols which vary between passes */
  135. "_SDA_BASE_", /* ppc */
  136. "_SDA2_BASE_", /* ppc */
  137. NULL };
  138. int i;
  139. int offset = 1;
  140. /* skip prefix char */
  141. if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
  142. offset++;
  143. /* if --all-symbols is not specified, then symbols outside the text
  144. * and inittext sections are discarded */
  145. if (!all_symbols) {
  146. if ((s->addr < _stext || s->addr > _etext)
  147. && (s->addr < _sinittext || s->addr > _einittext)
  148. && (s->addr < _sextratext || s->addr > _eextratext))
  149. return 0;
  150. /* Corner case. Discard any symbols with the same value as
  151. * _etext _einittext or _eextratext; they can move between pass
  152. * 1 and 2 when the kallsyms data are added. If these symbols
  153. * move then they may get dropped in pass 2, which breaks the
  154. * kallsyms rules.
  155. */
  156. if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) ||
  157. (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) ||
  158. (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext")))
  159. return 0;
  160. }
  161. /* Exclude symbols which vary between passes. */
  162. if (strstr((char *)s->sym + offset, "_compiled."))
  163. return 0;
  164. for (i = 0; special_symbols[i]; i++)
  165. if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
  166. return 0;
  167. return 1;
  168. }
  169. static void read_map(FILE *in)
  170. {
  171. while (!feof(in)) {
  172. if (table_cnt >= table_size) {
  173. table_size += 10000;
  174. table = realloc(table, sizeof(*table) * table_size);
  175. if (!table) {
  176. fprintf(stderr, "out of memory\n");
  177. exit (1);
  178. }
  179. }
  180. if (read_symbol(in, &table[table_cnt]) == 0)
  181. table_cnt++;
  182. }
  183. }
  184. static void output_label(char *label)
  185. {
  186. if (symbol_prefix_char)
  187. printf(".globl %c%s\n", symbol_prefix_char, label);
  188. else
  189. printf(".globl %s\n", label);
  190. printf("\tALGN\n");
  191. if (symbol_prefix_char)
  192. printf("%c%s:\n", symbol_prefix_char, label);
  193. else
  194. printf("%s:\n", label);
  195. }
  196. /* uncompress a compressed symbol. When this function is called, the best table
  197. * might still be compressed itself, so the function needs to be recursive */
  198. static int expand_symbol(unsigned char *data, int len, char *result)
  199. {
  200. int c, rlen, total=0;
  201. while (len) {
  202. c = *data;
  203. /* if the table holds a single char that is the same as the one
  204. * we are looking for, then end the search */
  205. if (best_table[c][0]==c && best_table_len[c]==1) {
  206. *result++ = c;
  207. total++;
  208. } else {
  209. /* if not, recurse and expand */
  210. rlen = expand_symbol(best_table[c], best_table_len[c], result);
  211. total += rlen;
  212. result += rlen;
  213. }
  214. data++;
  215. len--;
  216. }
  217. *result=0;
  218. return total;
  219. }
  220. static void write_src(void)
  221. {
  222. unsigned int i, k, off;
  223. unsigned int best_idx[256];
  224. unsigned int *markers;
  225. char buf[KSYM_NAME_LEN+1];
  226. printf("#include <asm/types.h>\n");
  227. printf("#if BITS_PER_LONG == 64\n");
  228. printf("#define PTR .quad\n");
  229. printf("#define ALGN .align 8\n");
  230. printf("#else\n");
  231. printf("#define PTR .long\n");
  232. printf("#define ALGN .align 4\n");
  233. printf("#endif\n");
  234. printf("\t.section .rodata, \"a\"\n");
  235. /* Provide proper symbols relocatability by their '_text'
  236. * relativeness. The symbol names cannot be used to construct
  237. * normal symbol references as the list of symbols contains
  238. * symbols that are declared static and are private to their
  239. * .o files. This prevents .tmp_kallsyms.o or any other
  240. * object from referencing them.
  241. */
  242. output_label("kallsyms_addresses");
  243. for (i = 0; i < table_cnt; i++) {
  244. if (toupper(table[i].sym[0]) != 'A') {
  245. if (_text <= table[i].addr)
  246. printf("\tPTR\t_text + %#llx\n",
  247. table[i].addr - _text);
  248. else
  249. printf("\tPTR\t_text - %#llx\n",
  250. _text - table[i].addr);
  251. } else {
  252. printf("\tPTR\t%#llx\n", table[i].addr);
  253. }
  254. }
  255. printf("\n");
  256. output_label("kallsyms_num_syms");
  257. printf("\tPTR\t%d\n", table_cnt);
  258. printf("\n");
  259. /* table of offset markers, that give the offset in the compressed stream
  260. * every 256 symbols */
  261. markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
  262. if (!markers) {
  263. fprintf(stderr, "kallsyms failure: "
  264. "unable to allocate required memory\n");
  265. exit(EXIT_FAILURE);
  266. }
  267. output_label("kallsyms_names");
  268. off = 0;
  269. for (i = 0; i < table_cnt; i++) {
  270. if ((i & 0xFF) == 0)
  271. markers[i >> 8] = off;
  272. printf("\t.byte 0x%02x", table[i].len);
  273. for (k = 0; k < table[i].len; k++)
  274. printf(", 0x%02x", table[i].sym[k]);
  275. printf("\n");
  276. off += table[i].len + 1;
  277. }
  278. printf("\n");
  279. output_label("kallsyms_markers");
  280. for (i = 0; i < ((table_cnt + 255) >> 8); i++)
  281. printf("\tPTR\t%d\n", markers[i]);
  282. printf("\n");
  283. free(markers);
  284. output_label("kallsyms_token_table");
  285. off = 0;
  286. for (i = 0; i < 256; i++) {
  287. best_idx[i] = off;
  288. expand_symbol(best_table[i], best_table_len[i], buf);
  289. printf("\t.asciz\t\"%s\"\n", buf);
  290. off += strlen(buf) + 1;
  291. }
  292. printf("\n");
  293. output_label("kallsyms_token_index");
  294. for (i = 0; i < 256; i++)
  295. printf("\t.short\t%d\n", best_idx[i]);
  296. printf("\n");
  297. }
  298. /* table lookup compression functions */
  299. /* count all the possible tokens in a symbol */
  300. static void learn_symbol(unsigned char *symbol, int len)
  301. {
  302. int i;
  303. for (i = 0; i < len - 1; i++)
  304. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
  305. }
  306. /* decrease the count for all the possible tokens in a symbol */
  307. static void forget_symbol(unsigned char *symbol, int len)
  308. {
  309. int i;
  310. for (i = 0; i < len - 1; i++)
  311. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
  312. }
  313. /* remove all the invalid symbols from the table and do the initial token count */
  314. static void build_initial_tok_table(void)
  315. {
  316. unsigned int i, pos;
  317. pos = 0;
  318. for (i = 0; i < table_cnt; i++) {
  319. if ( symbol_valid(&table[i]) ) {
  320. if (pos != i)
  321. table[pos] = table[i];
  322. learn_symbol(table[pos].sym, table[pos].len);
  323. pos++;
  324. }
  325. }
  326. table_cnt = pos;
  327. }
  328. /* replace a given token in all the valid symbols. Use the sampled symbols
  329. * to update the counts */
  330. static void compress_symbols(unsigned char *str, int idx)
  331. {
  332. unsigned int i, len, size;
  333. unsigned char *p1, *p2;
  334. for (i = 0; i < table_cnt; i++) {
  335. len = table[i].len;
  336. p1 = table[i].sym;
  337. /* find the token on the symbol */
  338. p2 = memmem(p1, len, str, 2);
  339. if (!p2) continue;
  340. /* decrease the counts for this symbol's tokens */
  341. forget_symbol(table[i].sym, len);
  342. size = len;
  343. do {
  344. *p2 = idx;
  345. p2++;
  346. size -= (p2 - p1);
  347. memmove(p2, p2 + 1, size);
  348. p1 = p2;
  349. len--;
  350. if (size < 2) break;
  351. /* find the token on the symbol */
  352. p2 = memmem(p1, size, str, 2);
  353. } while (p2);
  354. table[i].len = len;
  355. /* increase the counts for this symbol's new tokens */
  356. learn_symbol(table[i].sym, len);
  357. }
  358. }
  359. /* search the token with the maximum profit */
  360. static int find_best_token(void)
  361. {
  362. int i, best, bestprofit;
  363. bestprofit=-10000;
  364. best = 0;
  365. for (i = 0; i < 0x10000; i++) {
  366. if (token_profit[i] > bestprofit) {
  367. best = i;
  368. bestprofit = token_profit[i];
  369. }
  370. }
  371. return best;
  372. }
  373. /* this is the core of the algorithm: calculate the "best" table */
  374. static void optimize_result(void)
  375. {
  376. int i, best;
  377. /* using the '\0' symbol last allows compress_symbols to use standard
  378. * fast string functions */
  379. for (i = 255; i >= 0; i--) {
  380. /* if this table slot is empty (it is not used by an actual
  381. * original char code */
  382. if (!best_table_len[i]) {
  383. /* find the token with the breates profit value */
  384. best = find_best_token();
  385. /* place it in the "best" table */
  386. best_table_len[i] = 2;
  387. best_table[i][0] = best & 0xFF;
  388. best_table[i][1] = (best >> 8) & 0xFF;
  389. /* replace this token in all the valid symbols */
  390. compress_symbols(best_table[i], i);
  391. }
  392. }
  393. }
  394. /* start by placing the symbols that are actually used on the table */
  395. static void insert_real_symbols_in_table(void)
  396. {
  397. unsigned int i, j, c;
  398. memset(best_table, 0, sizeof(best_table));
  399. memset(best_table_len, 0, sizeof(best_table_len));
  400. for (i = 0; i < table_cnt; i++) {
  401. for (j = 0; j < table[i].len; j++) {
  402. c = table[i].sym[j];
  403. best_table[c][0]=c;
  404. best_table_len[c]=1;
  405. }
  406. }
  407. }
  408. static void optimize_token_table(void)
  409. {
  410. build_initial_tok_table();
  411. insert_real_symbols_in_table();
  412. /* When valid symbol is not registered, exit to error */
  413. if (!table_cnt) {
  414. fprintf(stderr, "No valid symbol.\n");
  415. exit(1);
  416. }
  417. optimize_result();
  418. }
  419. int main(int argc, char **argv)
  420. {
  421. if (argc >= 2) {
  422. int i;
  423. for (i = 1; i < argc; i++) {
  424. if(strcmp(argv[i], "--all-symbols") == 0)
  425. all_symbols = 1;
  426. else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
  427. char *p = &argv[i][16];
  428. /* skip quote */
  429. if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
  430. p++;
  431. symbol_prefix_char = *p;
  432. } else
  433. usage();
  434. }
  435. } else if (argc != 1)
  436. usage();
  437. read_map(stdin);
  438. optimize_token_table();
  439. write_src();
  440. return 0;
  441. }