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