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