[PATCH] DVB: misc driver updates
[linux-2.6/history.git] / scripts / kallsyms.c
blob2e0e07afb84c2870013e1bfd34dc2a6234d74e9c
1 /* Generate assembler source containing symbol information
3 * Copyright 2002 by Kai Germaschewski
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
8 * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
10 * ChangeLog:
12 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
13 * Changed the compression method from stem compression to "table lookup"
14 * compression
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%.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <ctype.h>
32 /* maximum token length used. It doesn't pay to increase it a lot, because
33 * very long substrings probably don't repeat themselves too often. */
34 #define MAX_TOK_SIZE 11
35 #define KSYM_NAME_LEN 127
37 /* we use only a subset of the complete symbol table to gather the token count,
38 * to speed up compression, at the expense of a little compression ratio */
39 #define WORKING_SET 1024
41 /* first find the best token only on the list of tokens that would profit more
42 * than GOOD_BAD_THRESHOLD. Only if this list is empty go to the "bad" list.
43 * Increasing this value will put less tokens on the "good" list, so the search
44 * is faster. However, if the good list runs out of tokens, we must painfully
45 * search the bad list. */
46 #define GOOD_BAD_THRESHOLD 10
48 /* token hash parameters */
49 #define HASH_BITS 18
50 #define HASH_TABLE_SIZE (1 << HASH_BITS)
51 #define HASH_MASK (HASH_TABLE_SIZE - 1)
52 #define HASH_BASE_OFFSET 2166136261U
53 #define HASH_FOLD(a) ((a)&(HASH_MASK))
55 /* flags to mark symbols */
56 #define SYM_FLAG_VALID 1
57 #define SYM_FLAG_SAMPLED 2
59 struct sym_entry {
60 unsigned long long addr;
61 char type;
62 unsigned char flags;
63 unsigned char len;
64 unsigned char *sym;
68 static struct sym_entry *table;
69 static int size, cnt;
70 static unsigned long long _stext, _etext, _sinittext, _einittext;
71 static int all_symbols = 0;
73 struct token {
74 unsigned char data[MAX_TOK_SIZE];
75 unsigned char len;
76 /* profit: the number of bytes that could be saved by inserting this
77 * token into the table */
78 int profit;
79 struct token *next; /* next token on the hash list */
80 struct token *right; /* next token on the good/bad list */
81 struct token *left; /* previous token on the good/bad list */
82 struct token *smaller; /* token that is less one letter than this one */
85 struct token bad_head, good_head;
86 struct token *hash_table[HASH_TABLE_SIZE];
88 /* the table that holds the result of the compression */
89 unsigned char best_table[256][MAX_TOK_SIZE+1];
90 unsigned char best_table_len[256];
93 static void
94 usage(void)
96 fprintf(stderr, "Usage: kallsyms [--all-symbols] < in.map > out.S\n");
97 exit(1);
100 static int
101 read_symbol(FILE *in, struct sym_entry *s)
103 char str[500];
104 int rc;
106 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &s->type, str);
107 if (rc != 3) {
108 if (rc != EOF) {
109 /* skip line */
110 fgets(str, 500, in);
112 return -1;
115 /* Ignore most absolute/undefined (?) symbols. */
116 if (strcmp(str, "_stext") == 0)
117 _stext = s->addr;
118 else if (strcmp(str, "_etext") == 0)
119 _etext = s->addr;
120 else if (strcmp(str, "_sinittext") == 0)
121 _sinittext = s->addr;
122 else if (strcmp(str, "_einittext") == 0)
123 _einittext = s->addr;
124 else if (toupper(s->type) == 'A' || toupper(s->type) == 'U')
125 return -1;
127 /* include the type field in the symbol name, so that it gets
128 * compressed together */
129 s->len = strlen(str) + 1;
130 s->sym = (char *) malloc(s->len + 1);
131 strcpy(s->sym + 1, str);
132 s->sym[0] = s->type;
134 return 0;
137 static int
138 symbol_valid(struct sym_entry *s)
140 /* Symbols which vary between passes. Passes 1 and 2 must have
141 * identical symbol lists. The kallsyms_* symbols below are only added
142 * after pass 1, they would be included in pass 2 when --all-symbols is
143 * specified so exclude them to get a stable symbol list.
145 static char *special_symbols[] = {
146 "kallsyms_addresses",
147 "kallsyms_num_syms",
148 "kallsyms_names",
149 "kallsyms_markers",
150 "kallsyms_token_table",
151 "kallsyms_token_index",
153 /* Exclude linker generated symbols which vary between passes */
154 "_SDA_BASE_", /* ppc */
155 "_SDA2_BASE_", /* ppc */
156 NULL };
157 int i;
159 /* if --all-symbols is not specified, then symbols outside the text
160 * and inittext sections are discarded */
161 if (!all_symbols) {
162 if ((s->addr < _stext || s->addr > _etext)
163 && (s->addr < _sinittext || s->addr > _einittext))
164 return 0;
167 /* Exclude symbols which vary between passes. */
168 if (strstr(s->sym + 1, "_compiled."))
169 return 0;
171 for (i = 0; special_symbols[i]; i++)
172 if( strcmp(s->sym + 1, special_symbols[i]) == 0 )
173 return 0;
175 return 1;
178 static void
179 read_map(FILE *in)
181 while (!feof(in)) {
182 if (cnt >= size) {
183 size += 10000;
184 table = realloc(table, sizeof(*table) * size);
185 if (!table) {
186 fprintf(stderr, "out of memory\n");
187 exit (1);
190 if (read_symbol(in, &table[cnt]) == 0)
191 cnt++;
195 static void output_label(char *label)
197 printf(".globl %s\n",label);
198 printf("\tALGN\n");
199 printf("%s:\n",label);
202 /* uncompress a compressed symbol. When this function is called, the best table
203 * might still be compressed itself, so the function needs to be recursive */
204 static int expand_symbol(unsigned char *data, int len, char *result)
206 int c, rlen, total=0;
208 while (len) {
209 c = *data;
210 /* if the table holds a single char that is the same as the one
211 * we are looking for, then end the search */
212 if (best_table[c][0]==c && best_table_len[c]==1) {
213 *result++ = c;
214 total++;
215 } else {
216 /* if not, recurse and expand */
217 rlen = expand_symbol(best_table[c], best_table_len[c], result);
218 total += rlen;
219 result += rlen;
221 data++;
222 len--;
224 *result=0;
226 return total;
229 static void
230 write_src(void)
232 int i, k, off, valid;
233 unsigned int best_idx[256];
234 unsigned int *markers;
235 char buf[KSYM_NAME_LEN+1];
237 printf("#include <asm/types.h>\n");
238 printf("#if BITS_PER_LONG == 64\n");
239 printf("#define PTR .quad\n");
240 printf("#define ALGN .align 8\n");
241 printf("#else\n");
242 printf("#define PTR .long\n");
243 printf("#define ALGN .align 4\n");
244 printf("#endif\n");
246 printf(".data\n");
248 output_label("kallsyms_addresses");
249 valid = 0;
250 for (i = 0; i < cnt; i++) {
251 if (table[i].flags & SYM_FLAG_VALID) {
252 printf("\tPTR\t%#llx\n", table[i].addr);
253 valid++;
256 printf("\n");
258 output_label("kallsyms_num_syms");
259 printf("\tPTR\t%d\n", valid);
260 printf("\n");
262 /* table of offset markers, that give the offset in the compressed stream
263 * every 256 symbols */
264 markers = (unsigned int *) malloc(sizeof(unsigned int)*((valid + 255) / 256));
266 output_label("kallsyms_names");
267 valid = 0;
268 off = 0;
269 for (i = 0; i < cnt; i++) {
271 if (!table[i].flags & SYM_FLAG_VALID)
272 continue;
274 if ((valid & 0xFF) == 0)
275 markers[valid >> 8] = off;
277 printf("\t.byte 0x%02x", table[i].len);
278 for (k = 0; k < table[i].len; k++)
279 printf(", 0x%02x", table[i].sym[k]);
280 printf("\n");
282 off += table[i].len + 1;
283 valid++;
285 printf("\n");
287 output_label("kallsyms_markers");
288 for (i = 0; i < ((valid + 255) >> 8); i++)
289 printf("\tPTR\t%d\n", markers[i]);
290 printf("\n");
292 free(markers);
294 output_label("kallsyms_token_table");
295 off = 0;
296 for (i = 0; i < 256; i++) {
297 best_idx[i] = off;
298 expand_symbol(best_table[i],best_table_len[i],buf);
299 printf("\t.asciz\t\"%s\"\n", buf);
300 off += strlen(buf) + 1;
302 printf("\n");
304 output_label("kallsyms_token_index");
305 for (i = 0; i < 256; i++)
306 printf("\t.short\t%d\n", best_idx[i]);
307 printf("\n");
311 /* table lookup compression functions */
313 static inline unsigned int rehash_token(unsigned int hash, unsigned char data)
315 return ((hash * 16777619) ^ data);
318 static unsigned int hash_token(unsigned char *data, int len)
320 unsigned int hash=HASH_BASE_OFFSET;
321 int i;
323 for (i = 0; i < len; i++)
324 hash = rehash_token(hash, data[i]);
326 return HASH_FOLD(hash);
329 /* find a token given its data and hash value */
330 static struct token *find_token_hash(unsigned char *data, int len, unsigned int hash)
332 struct token *ptr;
334 ptr = hash_table[hash];
336 while (ptr) {
337 if ((ptr->len == len) && (memcmp(ptr->data, data, len) == 0))
338 return ptr;
339 ptr=ptr->next;
342 return NULL;
345 static inline void insert_token_in_group(struct token *head, struct token *ptr)
347 ptr->right = head->right;
348 ptr->right->left = ptr;
349 head->right = ptr;
350 ptr->left = head;
353 static inline void remove_token_from_group(struct token *ptr)
355 ptr->left->right = ptr->right;
356 ptr->right->left = ptr->left;
360 /* build the counts for all the tokens that start with "data", and have lenghts
361 * from 2 to "len" */
362 static void learn_token(unsigned char *data, int len)
364 struct token *ptr,*last_ptr;
365 int i, newprofit;
366 unsigned int hash = HASH_BASE_OFFSET;
367 unsigned int hashes[MAX_TOK_SIZE + 1];
369 if (len > MAX_TOK_SIZE)
370 len = MAX_TOK_SIZE;
372 /* calculate and store the hash values for all the sub-tokens */
373 hash = rehash_token(hash, data[0]);
374 for (i = 2; i <= len; i++) {
375 hash = rehash_token(hash, data[i-1]);
376 hashes[i] = HASH_FOLD(hash);
379 last_ptr = NULL;
380 ptr = NULL;
382 for (i = len; i >= 2; i--) {
383 hash = hashes[i];
385 if (!ptr) ptr = find_token_hash(data, i, hash);
387 if (!ptr) {
388 /* create a new token entry */
389 ptr = (struct token *) malloc(sizeof(*ptr));
391 memcpy(ptr->data, data, i);
392 ptr->len = i;
394 /* when we create an entry, it's profit is 0 because
395 * we also take into account the size of the token on
396 * the compressed table. We then subtract GOOD_BAD_THRESHOLD
397 * so that the test to see if this token belongs to
398 * the good or bad list, is a comparison to zero */
399 ptr->profit = -GOOD_BAD_THRESHOLD;
401 ptr->next = hash_table[hash];
402 hash_table[hash] = ptr;
404 insert_token_in_group(&bad_head, ptr);
406 ptr->smaller = NULL;
407 } else {
408 newprofit = ptr->profit + (ptr->len - 1);
409 /* check to see if this token needs to be moved to a
410 * different list */
411 if((ptr->profit < 0) && (newprofit >= 0)) {
412 remove_token_from_group(ptr);
413 insert_token_in_group(&good_head,ptr);
415 ptr->profit = newprofit;
418 if (last_ptr) last_ptr->smaller = ptr;
419 last_ptr = ptr;
421 ptr = ptr->smaller;
425 /* decrease the counts for all the tokens that start with "data", and have lenghts
426 * from 2 to "len". This function is much simpler than learn_token because we have
427 * more guarantees (tho tokens exist, the ->smaller pointer is set, etc.)
428 * The two separate functions exist only because of compression performance */
429 static void forget_token(unsigned char *data, int len)
431 struct token *ptr;
432 int i, newprofit;
433 unsigned int hash=0;
435 if (len > MAX_TOK_SIZE) len = MAX_TOK_SIZE;
437 hash = hash_token(data, len);
438 ptr = find_token_hash(data, len, hash);
440 for (i = len; i >= 2; i--) {
442 newprofit = ptr->profit - (ptr->len - 1);
443 if ((ptr->profit >= 0) && (newprofit < 0)) {
444 remove_token_from_group(ptr);
445 insert_token_in_group(&bad_head, ptr);
447 ptr->profit=newprofit;
449 ptr=ptr->smaller;
453 /* count all the possible tokens in a symbol */
454 static void learn_symbol(unsigned char *symbol, int len)
456 int i;
458 for (i = 0; i < len - 1; i++)
459 learn_token(symbol + i, len - i);
462 /* decrease the count for all the possible tokens in a symbol */
463 static void forget_symbol(unsigned char *symbol, int len)
465 int i;
467 for (i = 0; i < len - 1; i++)
468 forget_token(symbol + i, len - i);
471 /* set all the symbol flags and do the initial token count */
472 static void build_initial_tok_table(void)
474 int i, use_it, valid;
476 valid = 0;
477 for (i = 0; i < cnt; i++) {
478 table[i].flags = 0;
479 if ( symbol_valid(&table[i]) ) {
480 table[i].flags |= SYM_FLAG_VALID;
481 valid++;
485 use_it = 0;
486 for (i = 0; i < cnt; i++) {
488 /* subsample the available symbols. This method is almost like
489 * a Bresenham's algorithm to get uniformly distributed samples
490 * across the symbol table */
491 if (table[i].flags & SYM_FLAG_VALID) {
493 use_it += WORKING_SET;
495 if (use_it >= valid) {
496 table[i].flags |= SYM_FLAG_SAMPLED;
497 use_it -= valid;
500 if (table[i].flags & SYM_FLAG_SAMPLED)
501 learn_symbol(table[i].sym, table[i].len);
505 /* replace a given token in all the valid symbols. Use the sampled symbols
506 * to update the counts */
507 static void compress_symbols(unsigned char *str, int tlen, int idx)
509 int i, len, learn, size;
510 unsigned char *p;
512 for (i = 0; i < cnt; i++) {
514 if (!(table[i].flags & SYM_FLAG_VALID)) continue;
516 len = table[i].len;
517 learn = 0;
518 p = table[i].sym;
520 do {
521 /* find the token on the symbol */
522 p = (unsigned char *) strstr((char *) p, (char *) str);
523 if (!p) break;
525 if (!learn) {
526 /* if this symbol was used to count, decrease it */
527 if (table[i].flags & SYM_FLAG_SAMPLED)
528 forget_symbol(table[i].sym, len);
529 learn = 1;
532 *p = idx;
533 size = (len - (p - table[i].sym)) - tlen + 1;
534 memmove(p + 1, p + tlen, size);
535 p++;
536 len -= tlen - 1;
538 } while (size >= tlen);
540 if(learn) {
541 table[i].len = len;
542 /* if this symbol was used to count, learn it again */
543 if(table[i].flags & SYM_FLAG_SAMPLED)
544 learn_symbol(table[i].sym, len);
549 /* search the token with the maximum profit */
550 static struct token *find_best_token(void)
552 struct token *ptr,*best,*head;
553 int bestprofit;
555 bestprofit=-10000;
557 /* failsafe: if the "good" list is empty search from the "bad" list */
558 if(good_head.right == &good_head) head = &bad_head;
559 else head = &good_head;
561 ptr = head->right;
562 best = NULL;
563 while (ptr != head) {
564 if (ptr->profit > bestprofit) {
565 bestprofit = ptr->profit;
566 best = ptr;
568 ptr = ptr->right;
571 return best;
574 /* this is the core of the algorithm: calculate the "best" table */
575 static void optimize_result(void)
577 struct token *best;
578 int i;
580 /* using the '\0' symbol last allows compress_symbols to use standard
581 * fast string functions */
582 for (i = 255; i >= 0; i--) {
584 /* if this table slot is empty (it is not used by an actual
585 * original char code */
586 if (!best_table_len[i]) {
588 /* find the token with the breates profit value */
589 best = find_best_token();
591 /* place it in the "best" table */
592 best_table_len[i] = best->len;
593 memcpy(best_table[i], best->data, best_table_len[i]);
594 /* zero terminate the token so that we can use strstr
595 in compress_symbols */
596 best_table[i][best_table_len[i]]='\0';
598 /* replace this token in all the valid symbols */
599 compress_symbols(best_table[i], best_table_len[i], i);
604 /* start by placing the symbols that are actually used on the table */
605 static void insert_real_symbols_in_table(void)
607 int i, j, c;
609 memset(best_table, 0, sizeof(best_table));
610 memset(best_table_len, 0, sizeof(best_table_len));
612 for (i = 0; i < cnt; i++) {
613 if (table[i].flags & SYM_FLAG_VALID) {
614 for (j = 0; j < table[i].len; j++) {
615 c = table[i].sym[j];
616 best_table[c][0]=c;
617 best_table_len[c]=1;
623 static void optimize_token_table(void)
625 memset(hash_table, 0, sizeof(hash_table));
627 good_head.left = &good_head;
628 good_head.right = &good_head;
630 bad_head.left = &bad_head;
631 bad_head.right = &bad_head;
633 build_initial_tok_table();
635 insert_real_symbols_in_table();
637 optimize_result();
642 main(int argc, char **argv)
644 if (argc == 2 && strcmp(argv[1], "--all-symbols") == 0)
645 all_symbols = 1;
646 else if (argc != 1)
647 usage();
649 read_map(stdin);
650 optimize_token_table();
651 write_src();
653 return 0;