kallsyms.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 _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, "_stext") == 0)
  79. _stext = s->addr;
  80. else if (strcmp(sym, "_etext") == 0)
  81. _etext = s->addr;
  82. else if (strcmp(sym, "_sinittext") == 0)
  83. _sinittext = s->addr;
  84. else if (strcmp(sym, "_einittext") == 0)
  85. _einittext = s->addr;
  86. else if (strcmp(sym, "_sextratext") == 0)
  87. _sextratext = s->addr;
  88. else if (strcmp(sym, "_eextratext") == 0)
  89. _eextratext = s->addr;
  90. else if (toupper(stype) == 'A')
  91. {
  92. /* Keep these useful absolute symbols */
  93. if (strcmp(sym, "__kernel_syscall_via_break") &&
  94. strcmp(sym, "__kernel_syscall_via_epc") &&
  95. strcmp(sym, "__kernel_sigtramp") &&
  96. strcmp(sym, "__gp"))
  97. return -1;
  98. }
  99. else if (toupper(stype) == 'U' ||
  100. is_arm_mapping_symbol(sym))
  101. return -1;
  102. /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
  103. else if (str[0] == '$')
  104. return -1;
  105. /* include the type field in the symbol name, so that it gets
  106. * compressed together */
  107. s->len = strlen(str) + 1;
  108. s->sym = malloc(s->len + 1);
  109. strcpy((char *)s->sym + 1, str);
  110. s->sym[0] = stype;
  111. return 0;
  112. }
  113. static int symbol_valid(struct sym_entry *s)
  114. {
  115. /* Symbols which vary between passes. Passes 1 and 2 must have
  116. * identical symbol lists. The kallsyms_* symbols below are only added
  117. * after pass 1, they would be included in pass 2 when --all-symbols is
  118. * specified so exclude them to get a stable symbol list.
  119. */
  120. static char *special_symbols[] = {
  121. "kallsyms_addresses",
  122. "kallsyms_num_syms",
  123. "kallsyms_names",
  124. "kallsyms_markers",
  125. "kallsyms_token_table",
  126. "kallsyms_token_index",
  127. /* Exclude linker generated symbols which vary between passes */
  128. "_SDA_BASE_", /* ppc */
  129. "_SDA2_BASE_", /* ppc */
  130. NULL };
  131. int i;
  132. int offset = 1;
  133. /* skip prefix char */
  134. if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
  135. offset++;
  136. /* if --all-symbols is not specified, then symbols outside the text
  137. * and inittext sections are discarded */
  138. if (!all_symbols) {
  139. if ((s->addr < _stext || s->addr > _etext)
  140. && (s->addr < _sinittext || s->addr > _einittext)
  141. && (s->addr < _sextratext || s->addr > _eextratext))
  142. return 0;
  143. /* Corner case. Discard any symbols with the same value as
  144. * _etext _einittext or _eextratext; they can move between pass
  145. * 1 and 2 when the kallsyms data are added. If these symbols
  146. * move then they may get dropped in pass 2, which breaks the
  147. * kallsyms rules.
  148. */
  149. if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) ||
  150. (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) ||
  151. (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext")))
  152. return 0;
  153. }
  154. /* Exclude symbols which vary between passes. */
  155. if (strstr((char *)s->sym + offset, "_compiled."))
  156. return 0;
  157. for (i = 0; special_symbols[i]; i++)
  158. if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
  159. return 0;
  160. return 1;
  161. }
  162. static void read_map(FILE *in)
  163. {
  164. while (!feof(in)) {
  165. if (table_cnt >= table_size) {
  166. table_size += 10000;
  167. table = realloc(table, sizeof(*table) * table_size);
  168. if (!table) {
  169. fprintf(stderr, "out of memory\n");
  170. exit (1);
  171. }
  172. }
  173. if (read_symbol(in, &table[table_cnt]) == 0)
  174. table_cnt++;
  175. }
  176. }
  177. static void output_label(char *label)
  178. {
  179. if (symbol_prefix_char)
  180. printf(".globl %c%s\n", symbol_prefix_char, label);
  181. else
  182. printf(".globl %s\n", label);
  183. printf("\tALGN\n");
  184. if (symbol_prefix_char)
  185. printf("%c%s:\n", symbol_prefix_char, label);
  186. else
  187. printf("%s:\n", label);
  188. }
  189. /* uncompress a compressed symbol. When this function is called, the best table
  190. * might still be compressed itself, so the function needs to be recursive */
  191. static int expand_symbol(unsigned char *data, int len, char *result)
  192. {
  193. int c, rlen, total=0;
  194. while (len) {
  195. c = *data;
  196. /* if the table holds a single char that is the same as the one
  197. * we are looking for, then end the search */
  198. if (best_table[c][0]==c && best_table_len[c]==1) {
  199. *result++ = c;
  200. total++;
  201. } else {
  202. /* if not, recurse and expand */
  203. rlen = expand_symbol(best_table[c], best_table_len[c], result);
  204. total += rlen;
  205. result += rlen;
  206. }
  207. data++;
  208. len--;
  209. }
  210. *result=0;
  211. return total;
  212. }
  213. static void write_src(void)
  214. {
  215. unsigned int i, k, off;
  216. unsigned int best_idx[256];
  217. unsigned int *markers;
  218. char buf[KSYM_NAME_LEN+1];
  219. printf("#include <asm/types.h>\n");
  220. printf("#if BITS_PER_LONG == 64\n");
  221. printf("#define PTR .quad\n");
  222. printf("#define ALGN .align 8\n");
  223. printf("#else\n");
  224. printf("#define PTR .long\n");
  225. printf("#define ALGN .align 4\n");
  226. printf("#endif\n");
  227. printf(".data\n");
  228. output_label("kallsyms_addresses");
  229. for (i = 0; i < table_cnt; i++) {
  230. printf("\tPTR\t%#llx\n", table[i].addr);
  231. }
  232. printf("\n");
  233. output_label("kallsyms_num_syms");
  234. printf("\tPTR\t%d\n", table_cnt);
  235. printf("\n");
  236. /* table of offset markers, that give the offset in the compressed stream
  237. * every 256 symbols */
  238. markers = (unsigned int *) malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
  239. output_label("kallsyms_names");
  240. off = 0;
  241. for (i = 0; i < table_cnt; i++) {
  242. if ((i & 0xFF) == 0)
  243. markers[i >> 8] = off;
  244. printf("\t.byte 0x%02x", table[i].len);
  245. for (k = 0; k < table[i].len; k++)
  246. printf(", 0x%02x", table[i].sym[k]);
  247. printf("\n");
  248. off += table[i].len + 1;
  249. }
  250. printf("\n");
  251. output_label("kallsyms_markers");
  252. for (i = 0; i < ((table_cnt + 255) >> 8); i++)
  253. printf("\tPTR\t%d\n", markers[i]);
  254. printf("\n");
  255. free(markers);
  256. output_label("kallsyms_token_table");
  257. off = 0;
  258. for (i = 0; i < 256; i++) {
  259. best_idx[i] = off;
  260. expand_symbol(best_table[i], best_table_len[i], buf);
  261. printf("\t.asciz\t\"%s\"\n", buf);
  262. off += strlen(buf) + 1;
  263. }
  264. printf("\n");
  265. output_label("kallsyms_token_index");
  266. for (i = 0; i < 256; i++)
  267. printf("\t.short\t%d\n", best_idx[i]);
  268. printf("\n");
  269. }
  270. /* table lookup compression functions */
  271. /* count all the possible tokens in a symbol */
  272. static void learn_symbol(unsigned char *symbol, int len)
  273. {
  274. int i;
  275. for (i = 0; i < len - 1; i++)
  276. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
  277. }
  278. /* decrease the count for all the possible tokens in a symbol */
  279. static void forget_symbol(unsigned char *symbol, int len)
  280. {
  281. int i;
  282. for (i = 0; i < len - 1; i++)
  283. token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
  284. }
  285. /* remove all the invalid symbols from the table and do the initial token count */
  286. static void build_initial_tok_table(void)
  287. {
  288. unsigned int i, pos;
  289. pos = 0;
  290. for (i = 0; i < table_cnt; i++) {
  291. if ( symbol_valid(&table[i]) ) {
  292. if (pos != i)
  293. table[pos] = table[i];
  294. learn_symbol(table[pos].sym, table[pos].len);
  295. pos++;
  296. }
  297. }
  298. table_cnt = pos;
  299. }
  300. /* replace a given token in all the valid symbols. Use the sampled symbols
  301. * to update the counts */
  302. static void compress_symbols(unsigned char *str, int idx)
  303. {
  304. unsigned int i, len, size;
  305. unsigned char *p1, *p2;
  306. for (i = 0; i < table_cnt; i++) {
  307. len = table[i].len;
  308. p1 = table[i].sym;
  309. /* find the token on the symbol */
  310. p2 = memmem(p1, len, str, 2);
  311. if (!p2) continue;
  312. /* decrease the counts for this symbol's tokens */
  313. forget_symbol(table[i].sym, len);
  314. size = len;
  315. do {
  316. *p2 = idx;
  317. p2++;
  318. size -= (p2 - p1);
  319. memmove(p2, p2 + 1, size);
  320. p1 = p2;
  321. len--;
  322. if (size < 2) break;
  323. /* find the token on the symbol */
  324. p2 = memmem(p1, size, str, 2);
  325. } while (p2);
  326. table[i].len = len;
  327. /* increase the counts for this symbol's new tokens */
  328. learn_symbol(table[i].sym, len);
  329. }
  330. }
  331. /* search the token with the maximum profit */
  332. static int find_best_token(void)
  333. {
  334. int i, best, bestprofit;
  335. bestprofit=-10000;
  336. best = 0;
  337. for (i = 0; i < 0x10000; i++) {
  338. if (token_profit[i] > bestprofit) {
  339. best = i;
  340. bestprofit = token_profit[i];
  341. }
  342. }
  343. return best;
  344. }
  345. /* this is the core of the algorithm: calculate the "best" table */
  346. static void optimize_result(void)
  347. {
  348. int i, best;
  349. /* using the '\0' symbol last allows compress_symbols to use standard
  350. * fast string functions */
  351. for (i = 255; i >= 0; i--) {
  352. /* if this table slot is empty (it is not used by an actual
  353. * original char code */
  354. if (!best_table_len[i]) {
  355. /* find the token with the breates profit value */
  356. best = find_best_token();
  357. /* place it in the "best" table */
  358. best_table_len[i] = 2;
  359. best_table[i][0] = best & 0xFF;
  360. best_table[i][1] = (best >> 8) & 0xFF;
  361. /* replace this token in all the valid symbols */
  362. compress_symbols(best_table[i], i);
  363. }
  364. }
  365. }
  366. /* start by placing the symbols that are actually used on the table */
  367. static void insert_real_symbols_in_table(void)
  368. {
  369. unsigned int i, j, c;
  370. memset(best_table, 0, sizeof(best_table));
  371. memset(best_table_len, 0, sizeof(best_table_len));
  372. for (i = 0; i < table_cnt; i++) {
  373. for (j = 0; j < table[i].len; j++) {
  374. c = table[i].sym[j];
  375. best_table[c][0]=c;
  376. best_table_len[c]=1;
  377. }
  378. }
  379. }
  380. static void optimize_token_table(void)
  381. {
  382. build_initial_tok_table();
  383. insert_real_symbols_in_table();
  384. /* When valid symbol is not registered, exit to error */
  385. if (!table_cnt) {
  386. fprintf(stderr, "No valid symbol.\n");
  387. exit(1);
  388. }
  389. optimize_result();
  390. }
  391. int main(int argc, char **argv)
  392. {
  393. if (argc >= 2) {
  394. int i;
  395. for (i = 1; i < argc; i++) {
  396. if(strcmp(argv[i], "--all-symbols") == 0)
  397. all_symbols = 1;
  398. else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
  399. char *p = &argv[i][16];
  400. /* skip quote */
  401. if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
  402. p++;
  403. symbol_prefix_char = *p;
  404. } else
  405. usage();
  406. }
  407. } else if (argc != 1)
  408. usage();
  409. read_map(stdin);
  410. optimize_token_table();
  411. write_src();
  412. return 0;
  413. }