kallsyms.c 13 KB

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