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; 46fd593d12SEric W. Biederman static unsigned long long _text, _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. */ 94fd593d12SEric W. Biederman if (strcmp(sym, "_text") == 0) 95fd593d12SEric W. Biederman _text = s->addr; 96fd593d12SEric W. Biederman else if (strcmp(sym, "_stext") == 0) 971da177e4SLinus Torvalds _stext = s->addr; 9841f11a4fSYoshinori Sato else if (strcmp(sym, "_etext") == 0) 991da177e4SLinus Torvalds _etext = s->addr; 10041f11a4fSYoshinori Sato else if (strcmp(sym, "_sinittext") == 0) 1011da177e4SLinus Torvalds _sinittext = s->addr; 10241f11a4fSYoshinori Sato else if (strcmp(sym, "_einittext") == 0) 1031da177e4SLinus Torvalds _einittext = s->addr; 104075d6eb1SDavid Woodhouse else if (strcmp(sym, "_sextratext") == 0) 105075d6eb1SDavid Woodhouse _sextratext = s->addr; 106075d6eb1SDavid Woodhouse else if (strcmp(sym, "_eextratext") == 0) 107075d6eb1SDavid Woodhouse _eextratext = s->addr; 108b3dbb4ecSPaulo Marques else if (toupper(stype) == 'A') 1091da177e4SLinus Torvalds { 1101da177e4SLinus Torvalds /* Keep these useful absolute symbols */ 11141f11a4fSYoshinori Sato if (strcmp(sym, "__kernel_syscall_via_break") && 11241f11a4fSYoshinori Sato strcmp(sym, "__kernel_syscall_via_epc") && 11341f11a4fSYoshinori Sato strcmp(sym, "__kernel_sigtramp") && 11441f11a4fSYoshinori Sato strcmp(sym, "__gp")) 1151da177e4SLinus Torvalds return -1; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds } 118b3dbb4ecSPaulo Marques else if (toupper(stype) == 'U' || 11941f11a4fSYoshinori Sato is_arm_mapping_symbol(sym)) 1201da177e4SLinus Torvalds return -1; 1216f00df24SRalf Baechle /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */ 1226f00df24SRalf Baechle else if (str[0] == '$') 1236f00df24SRalf Baechle return -1; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* include the type field in the symbol name, so that it gets 1261da177e4SLinus Torvalds * compressed together */ 1271da177e4SLinus Torvalds s->len = strlen(str) + 1; 128b3dbb4ecSPaulo Marques s->sym = malloc(s->len + 1); 129f1a136e0SJesper Juhl if (!s->sym) { 130f1a136e0SJesper Juhl fprintf(stderr, "kallsyms failure: " 131f1a136e0SJesper Juhl "unable to allocate required amount of memory\n"); 132f1a136e0SJesper Juhl exit(EXIT_FAILURE); 133f1a136e0SJesper Juhl } 134b3dbb4ecSPaulo Marques strcpy((char *)s->sym + 1, str); 135b3dbb4ecSPaulo Marques s->sym[0] = stype; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds return 0; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 140b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds /* Symbols which vary between passes. Passes 1 and 2 must have 1431da177e4SLinus Torvalds * identical symbol lists. The kallsyms_* symbols below are only added 1441da177e4SLinus Torvalds * after pass 1, they would be included in pass 2 when --all-symbols is 1451da177e4SLinus Torvalds * specified so exclude them to get a stable symbol list. 1461da177e4SLinus Torvalds */ 1471da177e4SLinus Torvalds static char *special_symbols[] = { 1481da177e4SLinus Torvalds "kallsyms_addresses", 1491da177e4SLinus Torvalds "kallsyms_num_syms", 1501da177e4SLinus Torvalds "kallsyms_names", 1511da177e4SLinus Torvalds "kallsyms_markers", 1521da177e4SLinus Torvalds "kallsyms_token_table", 1531da177e4SLinus Torvalds "kallsyms_token_index", 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds /* Exclude linker generated symbols which vary between passes */ 1561da177e4SLinus Torvalds "_SDA_BASE_", /* ppc */ 1571da177e4SLinus Torvalds "_SDA2_BASE_", /* ppc */ 1581da177e4SLinus Torvalds NULL }; 1591da177e4SLinus Torvalds int i; 16041f11a4fSYoshinori Sato int offset = 1; 16141f11a4fSYoshinori Sato 16241f11a4fSYoshinori Sato /* skip prefix char */ 16341f11a4fSYoshinori Sato if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char) 16441f11a4fSYoshinori Sato offset++; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds /* if --all-symbols is not specified, then symbols outside the text 1671da177e4SLinus Torvalds * and inittext sections are discarded */ 1681da177e4SLinus Torvalds if (!all_symbols) { 1691da177e4SLinus Torvalds if ((s->addr < _stext || s->addr > _etext) 170075d6eb1SDavid Woodhouse && (s->addr < _sinittext || s->addr > _einittext) 171075d6eb1SDavid Woodhouse && (s->addr < _sextratext || s->addr > _eextratext)) 1721da177e4SLinus Torvalds return 0; 1731da177e4SLinus Torvalds /* Corner case. Discard any symbols with the same value as 174075d6eb1SDavid Woodhouse * _etext _einittext or _eextratext; they can move between pass 175075d6eb1SDavid Woodhouse * 1 and 2 when the kallsyms data are added. If these symbols 176075d6eb1SDavid Woodhouse * move then they may get dropped in pass 2, which breaks the 177075d6eb1SDavid Woodhouse * kallsyms rules. 1781da177e4SLinus Torvalds */ 17961d9cdf2SJ.A. Magallon if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) || 18061d9cdf2SJ.A. Magallon (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) || 18161d9cdf2SJ.A. Magallon (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext"))) 1821da177e4SLinus Torvalds return 0; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* Exclude symbols which vary between passes. */ 186b3dbb4ecSPaulo Marques if (strstr((char *)s->sym + offset, "_compiled.")) 1871da177e4SLinus Torvalds return 0; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds for (i = 0; special_symbols[i]; i++) 190b3dbb4ecSPaulo Marques if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 ) 1911da177e4SLinus Torvalds return 0; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds return 1; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 196b3dbb4ecSPaulo Marques static void read_map(FILE *in) 1971da177e4SLinus Torvalds { 1981da177e4SLinus Torvalds while (!feof(in)) { 199b3dbb4ecSPaulo Marques if (table_cnt >= table_size) { 200b3dbb4ecSPaulo Marques table_size += 10000; 201b3dbb4ecSPaulo Marques table = realloc(table, sizeof(*table) * table_size); 2021da177e4SLinus Torvalds if (!table) { 2031da177e4SLinus Torvalds fprintf(stderr, "out of memory\n"); 2041da177e4SLinus Torvalds exit (1); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds } 207b3dbb4ecSPaulo Marques if (read_symbol(in, &table[table_cnt]) == 0) 208b3dbb4ecSPaulo Marques table_cnt++; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds static void output_label(char *label) 2131da177e4SLinus Torvalds { 21441f11a4fSYoshinori Sato if (symbol_prefix_char) 21541f11a4fSYoshinori Sato printf(".globl %c%s\n", symbol_prefix_char, label); 21641f11a4fSYoshinori Sato else 2171da177e4SLinus Torvalds printf(".globl %s\n", label); 2181da177e4SLinus Torvalds printf("\tALGN\n"); 21941f11a4fSYoshinori Sato if (symbol_prefix_char) 22041f11a4fSYoshinori Sato printf("%c%s:\n", symbol_prefix_char, label); 22141f11a4fSYoshinori Sato else 2221da177e4SLinus Torvalds printf("%s:\n", label); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table 2261da177e4SLinus Torvalds * might still be compressed itself, so the function needs to be recursive */ 2271da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result) 2281da177e4SLinus Torvalds { 2291da177e4SLinus Torvalds int c, rlen, total=0; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds while (len) { 2321da177e4SLinus Torvalds c = *data; 2331da177e4SLinus Torvalds /* if the table holds a single char that is the same as the one 2341da177e4SLinus Torvalds * we are looking for, then end the search */ 2351da177e4SLinus Torvalds if (best_table[c][0]==c && best_table_len[c]==1) { 2361da177e4SLinus Torvalds *result++ = c; 2371da177e4SLinus Torvalds total++; 2381da177e4SLinus Torvalds } else { 2391da177e4SLinus Torvalds /* if not, recurse and expand */ 2401da177e4SLinus Torvalds rlen = expand_symbol(best_table[c], best_table_len[c], result); 2411da177e4SLinus Torvalds total += rlen; 2421da177e4SLinus Torvalds result += rlen; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds data++; 2451da177e4SLinus Torvalds len--; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds *result=0; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds return total; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 252b3dbb4ecSPaulo Marques static void write_src(void) 2531da177e4SLinus Torvalds { 254b3dbb4ecSPaulo Marques unsigned int i, k, off; 2551da177e4SLinus Torvalds unsigned int best_idx[256]; 2561da177e4SLinus Torvalds unsigned int *markers; 2571da177e4SLinus Torvalds char buf[KSYM_NAME_LEN+1]; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds printf("#include <asm/types.h>\n"); 2601da177e4SLinus Torvalds printf("#if BITS_PER_LONG == 64\n"); 2611da177e4SLinus Torvalds printf("#define PTR .quad\n"); 2621da177e4SLinus Torvalds printf("#define ALGN .align 8\n"); 2631da177e4SLinus Torvalds printf("#else\n"); 2641da177e4SLinus Torvalds printf("#define PTR .long\n"); 2651da177e4SLinus Torvalds printf("#define ALGN .align 4\n"); 2661da177e4SLinus Torvalds printf("#endif\n"); 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds printf(".data\n"); 2691da177e4SLinus Torvalds 270fd593d12SEric W. Biederman /* Provide proper symbols relocatability by their '_text' 271fd593d12SEric W. Biederman * relativeness. The symbol names cannot be used to construct 272fd593d12SEric W. Biederman * normal symbol references as the list of symbols contains 273fd593d12SEric W. Biederman * symbols that are declared static and are private to their 274fd593d12SEric W. Biederman * .o files. This prevents .tmp_kallsyms.o or any other 275fd593d12SEric W. Biederman * object from referencing them. 276fd593d12SEric W. Biederman */ 2771da177e4SLinus Torvalds output_label("kallsyms_addresses"); 278b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 279fd593d12SEric W. Biederman if (toupper(table[i].sym[0]) != 'A') { 280*2c22d8baSVivek Goyal if (_text <= table[i].addr) 281fd593d12SEric W. Biederman printf("\tPTR\t_text + %#llx\n", 282fd593d12SEric W. Biederman table[i].addr - _text); 283*2c22d8baSVivek Goyal else 284*2c22d8baSVivek Goyal printf("\tPTR\t_text - %#llx\n", 285*2c22d8baSVivek Goyal _text - table[i].addr); 286fd593d12SEric W. Biederman } else { 2871da177e4SLinus Torvalds printf("\tPTR\t%#llx\n", table[i].addr); 2881da177e4SLinus Torvalds } 289fd593d12SEric W. Biederman } 2901da177e4SLinus Torvalds printf("\n"); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds output_label("kallsyms_num_syms"); 293b3dbb4ecSPaulo Marques printf("\tPTR\t%d\n", table_cnt); 2941da177e4SLinus Torvalds printf("\n"); 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds /* table of offset markers, that give the offset in the compressed stream 2971da177e4SLinus Torvalds * every 256 symbols */ 298f1a136e0SJesper Juhl markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256)); 299f1a136e0SJesper Juhl if (!markers) { 300f1a136e0SJesper Juhl fprintf(stderr, "kallsyms failure: " 301f1a136e0SJesper Juhl "unable to allocate required memory\n"); 302f1a136e0SJesper Juhl exit(EXIT_FAILURE); 303f1a136e0SJesper Juhl } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds output_label("kallsyms_names"); 3061da177e4SLinus Torvalds off = 0; 307b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 308b3dbb4ecSPaulo Marques if ((i & 0xFF) == 0) 309b3dbb4ecSPaulo Marques markers[i >> 8] = off; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds printf("\t.byte 0x%02x", table[i].len); 3121da177e4SLinus Torvalds for (k = 0; k < table[i].len; k++) 3131da177e4SLinus Torvalds printf(", 0x%02x", table[i].sym[k]); 3141da177e4SLinus Torvalds printf("\n"); 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds off += table[i].len + 1; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds printf("\n"); 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds output_label("kallsyms_markers"); 321b3dbb4ecSPaulo Marques for (i = 0; i < ((table_cnt + 255) >> 8); i++) 3221da177e4SLinus Torvalds printf("\tPTR\t%d\n", markers[i]); 3231da177e4SLinus Torvalds printf("\n"); 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds free(markers); 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds output_label("kallsyms_token_table"); 3281da177e4SLinus Torvalds off = 0; 3291da177e4SLinus Torvalds for (i = 0; i < 256; i++) { 3301da177e4SLinus Torvalds best_idx[i] = off; 3311da177e4SLinus Torvalds expand_symbol(best_table[i], best_table_len[i], buf); 3321da177e4SLinus Torvalds printf("\t.asciz\t\"%s\"\n", buf); 3331da177e4SLinus Torvalds off += strlen(buf) + 1; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds printf("\n"); 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds output_label("kallsyms_token_index"); 3381da177e4SLinus Torvalds for (i = 0; i < 256; i++) 3391da177e4SLinus Torvalds printf("\t.short\t%d\n", best_idx[i]); 3401da177e4SLinus Torvalds printf("\n"); 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /* table lookup compression functions */ 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* count all the possible tokens in a symbol */ 3471da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len) 3481da177e4SLinus Torvalds { 3491da177e4SLinus Torvalds int i; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds for (i = 0; i < len - 1; i++) 352b3dbb4ecSPaulo Marques token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */ 3561da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len) 3571da177e4SLinus Torvalds { 3581da177e4SLinus Torvalds int i; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds for (i = 0; i < len - 1; i++) 361b3dbb4ecSPaulo Marques token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 364b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */ 3651da177e4SLinus Torvalds static void build_initial_tok_table(void) 3661da177e4SLinus Torvalds { 367b3dbb4ecSPaulo Marques unsigned int i, pos; 3681da177e4SLinus Torvalds 369b3dbb4ecSPaulo Marques pos = 0; 370b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 3711da177e4SLinus Torvalds if ( symbol_valid(&table[i]) ) { 372b3dbb4ecSPaulo Marques if (pos != i) 373b3dbb4ecSPaulo Marques table[pos] = table[i]; 374b3dbb4ecSPaulo Marques learn_symbol(table[pos].sym, table[pos].len); 375b3dbb4ecSPaulo Marques pos++; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds } 378b3dbb4ecSPaulo Marques table_cnt = pos; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols 3821da177e4SLinus Torvalds * to update the counts */ 383b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx) 3841da177e4SLinus Torvalds { 385b3dbb4ecSPaulo Marques unsigned int i, len, size; 386b3dbb4ecSPaulo Marques unsigned char *p1, *p2; 3871da177e4SLinus Torvalds 388b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds len = table[i].len; 391b3dbb4ecSPaulo Marques p1 = table[i].sym; 392b3dbb4ecSPaulo Marques 393b3dbb4ecSPaulo Marques /* find the token on the symbol */ 394b3dbb4ecSPaulo Marques p2 = memmem(p1, len, str, 2); 395b3dbb4ecSPaulo Marques if (!p2) continue; 396b3dbb4ecSPaulo Marques 397b3dbb4ecSPaulo Marques /* decrease the counts for this symbol's tokens */ 398b3dbb4ecSPaulo Marques forget_symbol(table[i].sym, len); 399b3dbb4ecSPaulo Marques 400b3dbb4ecSPaulo Marques size = len; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds do { 403b3dbb4ecSPaulo Marques *p2 = idx; 404b3dbb4ecSPaulo Marques p2++; 405b3dbb4ecSPaulo Marques size -= (p2 - p1); 406b3dbb4ecSPaulo Marques memmove(p2, p2 + 1, size); 407b3dbb4ecSPaulo Marques p1 = p2; 408b3dbb4ecSPaulo Marques len--; 409b3dbb4ecSPaulo Marques 410b3dbb4ecSPaulo Marques if (size < 2) break; 411b3dbb4ecSPaulo Marques 4121da177e4SLinus Torvalds /* find the token on the symbol */ 413b3dbb4ecSPaulo Marques p2 = memmem(p1, size, str, 2); 4141da177e4SLinus Torvalds 415b3dbb4ecSPaulo Marques } while (p2); 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds table[i].len = len; 418b3dbb4ecSPaulo Marques 419b3dbb4ecSPaulo Marques /* increase the counts for this symbol's new tokens */ 4201da177e4SLinus Torvalds learn_symbol(table[i].sym, len); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /* search the token with the maximum profit */ 425b3dbb4ecSPaulo Marques static int find_best_token(void) 4261da177e4SLinus Torvalds { 427b3dbb4ecSPaulo Marques int i, best, bestprofit; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds bestprofit=-10000; 430b3dbb4ecSPaulo Marques best = 0; 4311da177e4SLinus Torvalds 432b3dbb4ecSPaulo Marques for (i = 0; i < 0x10000; i++) { 433b3dbb4ecSPaulo Marques if (token_profit[i] > bestprofit) { 434b3dbb4ecSPaulo Marques best = i; 435b3dbb4ecSPaulo Marques bestprofit = token_profit[i]; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds return best; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */ 4421da177e4SLinus Torvalds static void optimize_result(void) 4431da177e4SLinus Torvalds { 444b3dbb4ecSPaulo Marques int i, best; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds /* using the '\0' symbol last allows compress_symbols to use standard 4471da177e4SLinus Torvalds * fast string functions */ 4481da177e4SLinus Torvalds for (i = 255; i >= 0; i--) { 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds /* if this table slot is empty (it is not used by an actual 4511da177e4SLinus Torvalds * original char code */ 4521da177e4SLinus Torvalds if (!best_table_len[i]) { 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds /* find the token with the breates profit value */ 4551da177e4SLinus Torvalds best = find_best_token(); 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds /* place it in the "best" table */ 458b3dbb4ecSPaulo Marques best_table_len[i] = 2; 459b3dbb4ecSPaulo Marques best_table[i][0] = best & 0xFF; 460b3dbb4ecSPaulo Marques best_table[i][1] = (best >> 8) & 0xFF; 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds /* replace this token in all the valid symbols */ 463b3dbb4ecSPaulo Marques compress_symbols(best_table[i], i); 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */ 4691da177e4SLinus Torvalds static void insert_real_symbols_in_table(void) 4701da177e4SLinus Torvalds { 471b3dbb4ecSPaulo Marques unsigned int i, j, c; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds memset(best_table, 0, sizeof(best_table)); 4741da177e4SLinus Torvalds memset(best_table_len, 0, sizeof(best_table_len)); 4751da177e4SLinus Torvalds 476b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 4771da177e4SLinus Torvalds for (j = 0; j < table[i].len; j++) { 4781da177e4SLinus Torvalds c = table[i].sym[j]; 4791da177e4SLinus Torvalds best_table[c][0]=c; 4801da177e4SLinus Torvalds best_table_len[c]=1; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds static void optimize_token_table(void) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds build_initial_tok_table(); 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds insert_real_symbols_in_table(); 4901da177e4SLinus Torvalds 49141f11a4fSYoshinori Sato /* When valid symbol is not registered, exit to error */ 492b3dbb4ecSPaulo Marques if (!table_cnt) { 49341f11a4fSYoshinori Sato fprintf(stderr, "No valid symbol.\n"); 49441f11a4fSYoshinori Sato exit(1); 49541f11a4fSYoshinori Sato } 49641f11a4fSYoshinori Sato 4971da177e4SLinus Torvalds optimize_result(); 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds 501b3dbb4ecSPaulo Marques int main(int argc, char **argv) 5021da177e4SLinus Torvalds { 50341f11a4fSYoshinori Sato if (argc >= 2) { 50441f11a4fSYoshinori Sato int i; 50541f11a4fSYoshinori Sato for (i = 1; i < argc; i++) { 50641f11a4fSYoshinori Sato if(strcmp(argv[i], "--all-symbols") == 0) 5071da177e4SLinus Torvalds all_symbols = 1; 50841f11a4fSYoshinori Sato else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) { 50941f11a4fSYoshinori Sato char *p = &argv[i][16]; 51041f11a4fSYoshinori Sato /* skip quote */ 51141f11a4fSYoshinori Sato if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\'')) 51241f11a4fSYoshinori Sato p++; 51341f11a4fSYoshinori Sato symbol_prefix_char = *p; 51441f11a4fSYoshinori Sato } else 51541f11a4fSYoshinori Sato usage(); 51641f11a4fSYoshinori Sato } 51741f11a4fSYoshinori Sato } else if (argc != 1) 5181da177e4SLinus Torvalds usage(); 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds read_map(stdin); 5211da177e4SLinus Torvalds optimize_token_table(); 5221da177e4SLinus Torvalds write_src(); 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds return 0; 5251da177e4SLinus Torvalds } 526