11da177e4SLinus Torvalds /* Generate assembler source containing symbol information 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Copyright 2002 by Kai Germaschewski 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This software may be used and distributed according to the terms 61da177e4SLinus Torvalds * of the GNU General Public License, incorporated herein by reference. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * ChangeLog: 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com> 131da177e4SLinus Torvalds * Changed the compression method from stem compression to "table lookup" 141da177e4SLinus Torvalds * compression 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Table compression uses all the unused char codes on the symbols and 171da177e4SLinus Torvalds * maps these to the most used substrings (tokens). For instance, it might 181da177e4SLinus Torvalds * map char code 0xF7 to represent "write_" and then in every symbol where 191da177e4SLinus Torvalds * "write_" appears it can be replaced by 0xF7, saving 5 bytes. 201da177e4SLinus Torvalds * The used codes themselves are also placed in the table so that the 211da177e4SLinus Torvalds * decompresion can work without "special cases". 221da177e4SLinus Torvalds * Applied to kernel symbols, this usually produces a compression ratio 231da177e4SLinus Torvalds * of about 50%. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 27b3dbb4ecSPaulo Marques #define _GNU_SOURCE 28b3dbb4ecSPaulo Marques 291da177e4SLinus Torvalds #include <stdio.h> 301da177e4SLinus Torvalds #include <stdlib.h> 311da177e4SLinus Torvalds #include <string.h> 321da177e4SLinus Torvalds #include <ctype.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #define KSYM_NAME_LEN 127 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds struct sym_entry { 381da177e4SLinus Torvalds unsigned long long addr; 39b3dbb4ecSPaulo Marques unsigned int len; 401da177e4SLinus Torvalds unsigned char *sym; 411da177e4SLinus Torvalds }; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds static struct sym_entry *table; 45b3dbb4ecSPaulo Marques static unsigned int table_size, table_cnt; 46075d6eb1SDavid Woodhouse static unsigned long long _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext; 471da177e4SLinus Torvalds static int all_symbols = 0; 4841f11a4fSYoshinori Sato static char symbol_prefix_char = '\0'; 491da177e4SLinus Torvalds 50b3dbb4ecSPaulo Marques int token_profit[0x10000]; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* the table that holds the result of the compression */ 53b3dbb4ecSPaulo Marques unsigned char best_table[256][2]; 541da177e4SLinus Torvalds unsigned char best_table_len[256]; 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds 57b3dbb4ecSPaulo Marques static void usage(void) 581da177e4SLinus Torvalds { 5941f11a4fSYoshinori Sato fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n"); 601da177e4SLinus Torvalds exit(1); 611da177e4SLinus Torvalds } 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* 641da177e4SLinus Torvalds * This ignores the intensely annoying "mapping symbols" found 651da177e4SLinus Torvalds * in ARM ELF files: $a, $t and $d. 661da177e4SLinus Torvalds */ 67b3dbb4ecSPaulo Marques static inline int is_arm_mapping_symbol(const char *str) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds return str[0] == '$' && strchr("atd", str[1]) 701da177e4SLinus Torvalds && (str[2] == '\0' || str[2] == '.'); 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds 73b3dbb4ecSPaulo Marques static int read_symbol(FILE *in, struct sym_entry *s) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds char str[500]; 76b3dbb4ecSPaulo Marques char *sym, stype; 771da177e4SLinus Torvalds int rc; 781da177e4SLinus Torvalds 79b3dbb4ecSPaulo Marques rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str); 801da177e4SLinus Torvalds if (rc != 3) { 811da177e4SLinus Torvalds if (rc != EOF) { 821da177e4SLinus Torvalds /* skip line */ 831da177e4SLinus Torvalds fgets(str, 500, in); 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds return -1; 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds 8841f11a4fSYoshinori Sato sym = str; 8941f11a4fSYoshinori Sato /* skip prefix char */ 9041f11a4fSYoshinori Sato if (symbol_prefix_char && str[0] == symbol_prefix_char) 9141f11a4fSYoshinori Sato sym++; 9241f11a4fSYoshinori Sato 931da177e4SLinus Torvalds /* Ignore most absolute/undefined (?) symbols. */ 9441f11a4fSYoshinori Sato if (strcmp(sym, "_stext") == 0) 951da177e4SLinus Torvalds _stext = s->addr; 9641f11a4fSYoshinori Sato else if (strcmp(sym, "_etext") == 0) 971da177e4SLinus Torvalds _etext = s->addr; 9841f11a4fSYoshinori Sato else if (strcmp(sym, "_sinittext") == 0) 991da177e4SLinus Torvalds _sinittext = s->addr; 10041f11a4fSYoshinori Sato else if (strcmp(sym, "_einittext") == 0) 1011da177e4SLinus Torvalds _einittext = s->addr; 102075d6eb1SDavid Woodhouse else if (strcmp(sym, "_sextratext") == 0) 103075d6eb1SDavid Woodhouse _sextratext = s->addr; 104075d6eb1SDavid Woodhouse else if (strcmp(sym, "_eextratext") == 0) 105075d6eb1SDavid Woodhouse _eextratext = s->addr; 106b3dbb4ecSPaulo Marques else if (toupper(stype) == 'A') 1071da177e4SLinus Torvalds { 1081da177e4SLinus Torvalds /* Keep these useful absolute symbols */ 10941f11a4fSYoshinori Sato if (strcmp(sym, "__kernel_syscall_via_break") && 11041f11a4fSYoshinori Sato strcmp(sym, "__kernel_syscall_via_epc") && 11141f11a4fSYoshinori Sato strcmp(sym, "__kernel_sigtramp") && 11241f11a4fSYoshinori Sato strcmp(sym, "__gp")) 1131da177e4SLinus Torvalds return -1; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds } 116b3dbb4ecSPaulo Marques else if (toupper(stype) == 'U' || 11741f11a4fSYoshinori Sato is_arm_mapping_symbol(sym)) 1181da177e4SLinus Torvalds return -1; 119*6f00df24SRalf Baechle /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */ 120*6f00df24SRalf Baechle else if (str[0] == '$') 121*6f00df24SRalf Baechle return -1; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* include the type field in the symbol name, so that it gets 1241da177e4SLinus Torvalds * compressed together */ 1251da177e4SLinus Torvalds s->len = strlen(str) + 1; 126b3dbb4ecSPaulo Marques s->sym = malloc(s->len + 1); 127b3dbb4ecSPaulo Marques strcpy((char *)s->sym + 1, str); 128b3dbb4ecSPaulo Marques s->sym[0] = stype; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds return 0; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 133b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds /* Symbols which vary between passes. Passes 1 and 2 must have 1361da177e4SLinus Torvalds * identical symbol lists. The kallsyms_* symbols below are only added 1371da177e4SLinus Torvalds * after pass 1, they would be included in pass 2 when --all-symbols is 1381da177e4SLinus Torvalds * specified so exclude them to get a stable symbol list. 1391da177e4SLinus Torvalds */ 1401da177e4SLinus Torvalds static char *special_symbols[] = { 1411da177e4SLinus Torvalds "kallsyms_addresses", 1421da177e4SLinus Torvalds "kallsyms_num_syms", 1431da177e4SLinus Torvalds "kallsyms_names", 1441da177e4SLinus Torvalds "kallsyms_markers", 1451da177e4SLinus Torvalds "kallsyms_token_table", 1461da177e4SLinus Torvalds "kallsyms_token_index", 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* Exclude linker generated symbols which vary between passes */ 1491da177e4SLinus Torvalds "_SDA_BASE_", /* ppc */ 1501da177e4SLinus Torvalds "_SDA2_BASE_", /* ppc */ 1511da177e4SLinus Torvalds NULL }; 1521da177e4SLinus Torvalds int i; 15341f11a4fSYoshinori Sato int offset = 1; 15441f11a4fSYoshinori Sato 15541f11a4fSYoshinori Sato /* skip prefix char */ 15641f11a4fSYoshinori Sato if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char) 15741f11a4fSYoshinori Sato offset++; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* if --all-symbols is not specified, then symbols outside the text 1601da177e4SLinus Torvalds * and inittext sections are discarded */ 1611da177e4SLinus Torvalds if (!all_symbols) { 1621da177e4SLinus Torvalds if ((s->addr < _stext || s->addr > _etext) 163075d6eb1SDavid Woodhouse && (s->addr < _sinittext || s->addr > _einittext) 164075d6eb1SDavid Woodhouse && (s->addr < _sextratext || s->addr > _eextratext)) 1651da177e4SLinus Torvalds return 0; 1661da177e4SLinus Torvalds /* Corner case. Discard any symbols with the same value as 167075d6eb1SDavid Woodhouse * _etext _einittext or _eextratext; they can move between pass 168075d6eb1SDavid Woodhouse * 1 and 2 when the kallsyms data are added. If these symbols 169075d6eb1SDavid Woodhouse * move then they may get dropped in pass 2, which breaks the 170075d6eb1SDavid Woodhouse * kallsyms rules. 1711da177e4SLinus Torvalds */ 17261d9cdf2SJ.A. Magallon if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) || 17361d9cdf2SJ.A. Magallon (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) || 17461d9cdf2SJ.A. Magallon (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext"))) 1751da177e4SLinus Torvalds return 0; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds /* Exclude symbols which vary between passes. */ 179b3dbb4ecSPaulo Marques if (strstr((char *)s->sym + offset, "_compiled.")) 1801da177e4SLinus Torvalds return 0; 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds for (i = 0; special_symbols[i]; i++) 183b3dbb4ecSPaulo Marques if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 ) 1841da177e4SLinus Torvalds return 0; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds return 1; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 189b3dbb4ecSPaulo Marques static void read_map(FILE *in) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds while (!feof(in)) { 192b3dbb4ecSPaulo Marques if (table_cnt >= table_size) { 193b3dbb4ecSPaulo Marques table_size += 10000; 194b3dbb4ecSPaulo Marques table = realloc(table, sizeof(*table) * table_size); 1951da177e4SLinus Torvalds if (!table) { 1961da177e4SLinus Torvalds fprintf(stderr, "out of memory\n"); 1971da177e4SLinus Torvalds exit (1); 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds } 200b3dbb4ecSPaulo Marques if (read_symbol(in, &table[table_cnt]) == 0) 201b3dbb4ecSPaulo Marques table_cnt++; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds static void output_label(char *label) 2061da177e4SLinus Torvalds { 20741f11a4fSYoshinori Sato if (symbol_prefix_char) 20841f11a4fSYoshinori Sato printf(".globl %c%s\n", symbol_prefix_char, label); 20941f11a4fSYoshinori Sato else 2101da177e4SLinus Torvalds printf(".globl %s\n", label); 2111da177e4SLinus Torvalds printf("\tALGN\n"); 21241f11a4fSYoshinori Sato if (symbol_prefix_char) 21341f11a4fSYoshinori Sato printf("%c%s:\n", symbol_prefix_char, label); 21441f11a4fSYoshinori Sato else 2151da177e4SLinus Torvalds printf("%s:\n", label); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table 2191da177e4SLinus Torvalds * might still be compressed itself, so the function needs to be recursive */ 2201da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result) 2211da177e4SLinus Torvalds { 2221da177e4SLinus Torvalds int c, rlen, total=0; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds while (len) { 2251da177e4SLinus Torvalds c = *data; 2261da177e4SLinus Torvalds /* if the table holds a single char that is the same as the one 2271da177e4SLinus Torvalds * we are looking for, then end the search */ 2281da177e4SLinus Torvalds if (best_table[c][0]==c && best_table_len[c]==1) { 2291da177e4SLinus Torvalds *result++ = c; 2301da177e4SLinus Torvalds total++; 2311da177e4SLinus Torvalds } else { 2321da177e4SLinus Torvalds /* if not, recurse and expand */ 2331da177e4SLinus Torvalds rlen = expand_symbol(best_table[c], best_table_len[c], result); 2341da177e4SLinus Torvalds total += rlen; 2351da177e4SLinus Torvalds result += rlen; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds data++; 2381da177e4SLinus Torvalds len--; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds *result=0; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds return total; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 245b3dbb4ecSPaulo Marques static void write_src(void) 2461da177e4SLinus Torvalds { 247b3dbb4ecSPaulo Marques unsigned int i, k, off; 2481da177e4SLinus Torvalds unsigned int best_idx[256]; 2491da177e4SLinus Torvalds unsigned int *markers; 2501da177e4SLinus Torvalds char buf[KSYM_NAME_LEN+1]; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds printf("#include <asm/types.h>\n"); 2531da177e4SLinus Torvalds printf("#if BITS_PER_LONG == 64\n"); 2541da177e4SLinus Torvalds printf("#define PTR .quad\n"); 2551da177e4SLinus Torvalds printf("#define ALGN .align 8\n"); 2561da177e4SLinus Torvalds printf("#else\n"); 2571da177e4SLinus Torvalds printf("#define PTR .long\n"); 2581da177e4SLinus Torvalds printf("#define ALGN .align 4\n"); 2591da177e4SLinus Torvalds printf("#endif\n"); 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds printf(".data\n"); 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds output_label("kallsyms_addresses"); 264b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 2651da177e4SLinus Torvalds printf("\tPTR\t%#llx\n", table[i].addr); 2661da177e4SLinus Torvalds } 2671da177e4SLinus Torvalds printf("\n"); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds output_label("kallsyms_num_syms"); 270b3dbb4ecSPaulo Marques printf("\tPTR\t%d\n", table_cnt); 2711da177e4SLinus Torvalds printf("\n"); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds /* table of offset markers, that give the offset in the compressed stream 2741da177e4SLinus Torvalds * every 256 symbols */ 275b3dbb4ecSPaulo Marques markers = (unsigned int *) malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256)); 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds output_label("kallsyms_names"); 2781da177e4SLinus Torvalds off = 0; 279b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 280b3dbb4ecSPaulo Marques if ((i & 0xFF) == 0) 281b3dbb4ecSPaulo Marques markers[i >> 8] = off; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds printf("\t.byte 0x%02x", table[i].len); 2841da177e4SLinus Torvalds for (k = 0; k < table[i].len; k++) 2851da177e4SLinus Torvalds printf(", 0x%02x", table[i].sym[k]); 2861da177e4SLinus Torvalds printf("\n"); 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds off += table[i].len + 1; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds printf("\n"); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds output_label("kallsyms_markers"); 293b3dbb4ecSPaulo Marques for (i = 0; i < ((table_cnt + 255) >> 8); i++) 2941da177e4SLinus Torvalds printf("\tPTR\t%d\n", markers[i]); 2951da177e4SLinus Torvalds printf("\n"); 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds free(markers); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds output_label("kallsyms_token_table"); 3001da177e4SLinus Torvalds off = 0; 3011da177e4SLinus Torvalds for (i = 0; i < 256; i++) { 3021da177e4SLinus Torvalds best_idx[i] = off; 3031da177e4SLinus Torvalds expand_symbol(best_table[i], best_table_len[i], buf); 3041da177e4SLinus Torvalds printf("\t.asciz\t\"%s\"\n", buf); 3051da177e4SLinus Torvalds off += strlen(buf) + 1; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds printf("\n"); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds output_label("kallsyms_token_index"); 3101da177e4SLinus Torvalds for (i = 0; i < 256; i++) 3111da177e4SLinus Torvalds printf("\t.short\t%d\n", best_idx[i]); 3121da177e4SLinus Torvalds printf("\n"); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* table lookup compression functions */ 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds /* count all the possible tokens in a symbol */ 3191da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len) 3201da177e4SLinus Torvalds { 3211da177e4SLinus Torvalds int i; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds for (i = 0; i < len - 1; i++) 324b3dbb4ecSPaulo Marques token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */ 3281da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len) 3291da177e4SLinus Torvalds { 3301da177e4SLinus Torvalds int i; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds for (i = 0; i < len - 1; i++) 333b3dbb4ecSPaulo Marques token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 336b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */ 3371da177e4SLinus Torvalds static void build_initial_tok_table(void) 3381da177e4SLinus Torvalds { 339b3dbb4ecSPaulo Marques unsigned int i, pos; 3401da177e4SLinus Torvalds 341b3dbb4ecSPaulo Marques pos = 0; 342b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 3431da177e4SLinus Torvalds if ( symbol_valid(&table[i]) ) { 344b3dbb4ecSPaulo Marques if (pos != i) 345b3dbb4ecSPaulo Marques table[pos] = table[i]; 346b3dbb4ecSPaulo Marques learn_symbol(table[pos].sym, table[pos].len); 347b3dbb4ecSPaulo Marques pos++; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds } 350b3dbb4ecSPaulo Marques table_cnt = pos; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols 3541da177e4SLinus Torvalds * to update the counts */ 355b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx) 3561da177e4SLinus Torvalds { 357b3dbb4ecSPaulo Marques unsigned int i, len, size; 358b3dbb4ecSPaulo Marques unsigned char *p1, *p2; 3591da177e4SLinus Torvalds 360b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds len = table[i].len; 363b3dbb4ecSPaulo Marques p1 = table[i].sym; 364b3dbb4ecSPaulo Marques 365b3dbb4ecSPaulo Marques /* find the token on the symbol */ 366b3dbb4ecSPaulo Marques p2 = memmem(p1, len, str, 2); 367b3dbb4ecSPaulo Marques if (!p2) continue; 368b3dbb4ecSPaulo Marques 369b3dbb4ecSPaulo Marques /* decrease the counts for this symbol's tokens */ 370b3dbb4ecSPaulo Marques forget_symbol(table[i].sym, len); 371b3dbb4ecSPaulo Marques 372b3dbb4ecSPaulo Marques size = len; 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds do { 375b3dbb4ecSPaulo Marques *p2 = idx; 376b3dbb4ecSPaulo Marques p2++; 377b3dbb4ecSPaulo Marques size -= (p2 - p1); 378b3dbb4ecSPaulo Marques memmove(p2, p2 + 1, size); 379b3dbb4ecSPaulo Marques p1 = p2; 380b3dbb4ecSPaulo Marques len--; 381b3dbb4ecSPaulo Marques 382b3dbb4ecSPaulo Marques if (size < 2) break; 383b3dbb4ecSPaulo Marques 3841da177e4SLinus Torvalds /* find the token on the symbol */ 385b3dbb4ecSPaulo Marques p2 = memmem(p1, size, str, 2); 3861da177e4SLinus Torvalds 387b3dbb4ecSPaulo Marques } while (p2); 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds table[i].len = len; 390b3dbb4ecSPaulo Marques 391b3dbb4ecSPaulo Marques /* increase the counts for this symbol's new tokens */ 3921da177e4SLinus Torvalds learn_symbol(table[i].sym, len); 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* search the token with the maximum profit */ 397b3dbb4ecSPaulo Marques static int find_best_token(void) 3981da177e4SLinus Torvalds { 399b3dbb4ecSPaulo Marques int i, best, bestprofit; 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds bestprofit=-10000; 402b3dbb4ecSPaulo Marques best = 0; 4031da177e4SLinus Torvalds 404b3dbb4ecSPaulo Marques for (i = 0; i < 0x10000; i++) { 405b3dbb4ecSPaulo Marques if (token_profit[i] > bestprofit) { 406b3dbb4ecSPaulo Marques best = i; 407b3dbb4ecSPaulo Marques bestprofit = token_profit[i]; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds return best; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */ 4141da177e4SLinus Torvalds static void optimize_result(void) 4151da177e4SLinus Torvalds { 416b3dbb4ecSPaulo Marques int i, best; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds /* using the '\0' symbol last allows compress_symbols to use standard 4191da177e4SLinus Torvalds * fast string functions */ 4201da177e4SLinus Torvalds for (i = 255; i >= 0; i--) { 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds /* if this table slot is empty (it is not used by an actual 4231da177e4SLinus Torvalds * original char code */ 4241da177e4SLinus Torvalds if (!best_table_len[i]) { 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* find the token with the breates profit value */ 4271da177e4SLinus Torvalds best = find_best_token(); 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* place it in the "best" table */ 430b3dbb4ecSPaulo Marques best_table_len[i] = 2; 431b3dbb4ecSPaulo Marques best_table[i][0] = best & 0xFF; 432b3dbb4ecSPaulo Marques best_table[i][1] = (best >> 8) & 0xFF; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* replace this token in all the valid symbols */ 435b3dbb4ecSPaulo Marques compress_symbols(best_table[i], i); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */ 4411da177e4SLinus Torvalds static void insert_real_symbols_in_table(void) 4421da177e4SLinus Torvalds { 443b3dbb4ecSPaulo Marques unsigned int i, j, c; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds memset(best_table, 0, sizeof(best_table)); 4461da177e4SLinus Torvalds memset(best_table_len, 0, sizeof(best_table_len)); 4471da177e4SLinus Torvalds 448b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 4491da177e4SLinus Torvalds for (j = 0; j < table[i].len; j++) { 4501da177e4SLinus Torvalds c = table[i].sym[j]; 4511da177e4SLinus Torvalds best_table[c][0]=c; 4521da177e4SLinus Torvalds best_table_len[c]=1; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds static void optimize_token_table(void) 4581da177e4SLinus Torvalds { 4591da177e4SLinus Torvalds build_initial_tok_table(); 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds insert_real_symbols_in_table(); 4621da177e4SLinus Torvalds 46341f11a4fSYoshinori Sato /* When valid symbol is not registered, exit to error */ 464b3dbb4ecSPaulo Marques if (!table_cnt) { 46541f11a4fSYoshinori Sato fprintf(stderr, "No valid symbol.\n"); 46641f11a4fSYoshinori Sato exit(1); 46741f11a4fSYoshinori Sato } 46841f11a4fSYoshinori Sato 4691da177e4SLinus Torvalds optimize_result(); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds 473b3dbb4ecSPaulo Marques int main(int argc, char **argv) 4741da177e4SLinus Torvalds { 47541f11a4fSYoshinori Sato if (argc >= 2) { 47641f11a4fSYoshinori Sato int i; 47741f11a4fSYoshinori Sato for (i = 1; i < argc; i++) { 47841f11a4fSYoshinori Sato if(strcmp(argv[i], "--all-symbols") == 0) 4791da177e4SLinus Torvalds all_symbols = 1; 48041f11a4fSYoshinori Sato else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) { 48141f11a4fSYoshinori Sato char *p = &argv[i][16]; 48241f11a4fSYoshinori Sato /* skip quote */ 48341f11a4fSYoshinori Sato if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\'')) 48441f11a4fSYoshinori Sato p++; 48541f11a4fSYoshinori Sato symbol_prefix_char = *p; 48641f11a4fSYoshinori Sato } else 48741f11a4fSYoshinori Sato usage(); 48841f11a4fSYoshinori Sato } 48941f11a4fSYoshinori Sato } else if (argc != 1) 4901da177e4SLinus Torvalds usage(); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds read_map(stdin); 4931da177e4SLinus Torvalds optimize_token_table(); 4941da177e4SLinus Torvalds write_src(); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds return 0; 4971da177e4SLinus Torvalds } 498