kallsyms.c 12 KB

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