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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * Table compression uses all the unused char codes on the symbols and |
| 11 | * maps these to the most used substrings (tokens). For instance, it might |
| 12 | * map char code 0xF7 to represent "write_" and then in every symbol where |
| 13 | * "write_" appears it can be replaced by 0xF7, saving 5 bytes. |
| 14 | * The used codes themselves are also placed in the table so that the |
| 15 | * decompresion can work without "special cases". |
| 16 | * Applied to kernel symbols, this usually produces a compression ratio |
| 17 | * of about 50%. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <ctype.h> |
| 25 | |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 26 | #define KSYM_NAME_LEN 128 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct sym_entry { |
| 29 | unsigned long long addr; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 30 | unsigned int len; |
Paulo Marques | f2df3f6 | 2008-02-06 01:37:33 -0800 | [diff] [blame] | 31 | unsigned int start_pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | unsigned char *sym; |
| 33 | }; |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static struct sym_entry *table; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 36 | static unsigned int table_size, table_cnt; |
Robin Getz | a3b8111 | 2008-02-06 01:36:26 -0800 | [diff] [blame] | 37 | static unsigned long long _text, _stext, _etext, _sinittext, _einittext; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static int all_symbols = 0; |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 39 | static char symbol_prefix_char = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 41 | int token_profit[0x10000]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* the table that holds the result of the compression */ |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 44 | unsigned char best_table[256][2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | unsigned char best_table_len[256]; |
| 46 | |
| 47 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 48 | static void usage(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 50 | 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] | 51 | exit(1); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * This ignores the intensely annoying "mapping symbols" found |
| 56 | * in ARM ELF files: $a, $t and $d. |
| 57 | */ |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 58 | static inline int is_arm_mapping_symbol(const char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | return str[0] == '$' && strchr("atd", str[1]) |
| 61 | && (str[2] == '\0' || str[2] == '.'); |
| 62 | } |
| 63 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 64 | static int read_symbol(FILE *in, struct sym_entry *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | char str[500]; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 67 | char *sym, stype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | int rc; |
| 69 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 70 | rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | if (rc != 3) { |
| 72 | if (rc != EOF) { |
| 73 | /* skip line */ |
| 74 | fgets(str, 500, in); |
| 75 | } |
| 76 | return -1; |
| 77 | } |
| 78 | |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 79 | sym = str; |
| 80 | /* skip prefix char */ |
| 81 | if (symbol_prefix_char && str[0] == symbol_prefix_char) |
| 82 | sym++; |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /* Ignore most absolute/undefined (?) symbols. */ |
Eric W. Biederman | fd593d1 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 85 | if (strcmp(sym, "_text") == 0) |
| 86 | _text = s->addr; |
| 87 | else if (strcmp(sym, "_stext") == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | _stext = s->addr; |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 89 | else if (strcmp(sym, "_etext") == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | _etext = s->addr; |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 91 | else if (strcmp(sym, "_sinittext") == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | _sinittext = s->addr; |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 93 | else if (strcmp(sym, "_einittext") == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | _einittext = s->addr; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 95 | else if (toupper(stype) == 'A') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 97 | /* Keep these useful absolute symbols */ |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 98 | if (strcmp(sym, "__kernel_syscall_via_break") && |
| 99 | strcmp(sym, "__kernel_syscall_via_epc") && |
| 100 | strcmp(sym, "__kernel_sigtramp") && |
| 101 | strcmp(sym, "__gp")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | return -1; |
| 103 | |
| 104 | } |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 105 | else if (toupper(stype) == 'U' || |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 106 | is_arm_mapping_symbol(sym)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return -1; |
Ralf Baechle | 6f00df2 | 2005-09-06 15:16:41 -0700 | [diff] [blame] | 108 | /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */ |
| 109 | else if (str[0] == '$') |
| 110 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | /* include the type field in the symbol name, so that it gets |
| 113 | * compressed together */ |
| 114 | s->len = strlen(str) + 1; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 115 | s->sym = malloc(s->len + 1); |
Jesper Juhl | f1a136e | 2006-03-25 03:07:46 -0800 | [diff] [blame] | 116 | if (!s->sym) { |
| 117 | fprintf(stderr, "kallsyms failure: " |
| 118 | "unable to allocate required amount of memory\n"); |
| 119 | exit(EXIT_FAILURE); |
| 120 | } |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 121 | strcpy((char *)s->sym + 1, str); |
| 122 | s->sym[0] = stype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 127 | static int symbol_valid(struct sym_entry *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | /* Symbols which vary between passes. Passes 1 and 2 must have |
| 130 | * identical symbol lists. The kallsyms_* symbols below are only added |
| 131 | * after pass 1, they would be included in pass 2 when --all-symbols is |
| 132 | * specified so exclude them to get a stable symbol list. |
| 133 | */ |
| 134 | static char *special_symbols[] = { |
| 135 | "kallsyms_addresses", |
| 136 | "kallsyms_num_syms", |
| 137 | "kallsyms_names", |
| 138 | "kallsyms_markers", |
| 139 | "kallsyms_token_table", |
| 140 | "kallsyms_token_index", |
| 141 | |
| 142 | /* Exclude linker generated symbols which vary between passes */ |
| 143 | "_SDA_BASE_", /* ppc */ |
| 144 | "_SDA2_BASE_", /* ppc */ |
| 145 | NULL }; |
| 146 | int i; |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 147 | int offset = 1; |
| 148 | |
| 149 | /* skip prefix char */ |
| 150 | if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char) |
| 151 | offset++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | /* if --all-symbols is not specified, then symbols outside the text |
| 154 | * and inittext sections are discarded */ |
| 155 | if (!all_symbols) { |
| 156 | if ((s->addr < _stext || s->addr > _etext) |
Robin Getz | a3b8111 | 2008-02-06 01:36:26 -0800 | [diff] [blame] | 157 | && (s->addr < _sinittext || s->addr > _einittext)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return 0; |
| 159 | /* Corner case. Discard any symbols with the same value as |
Robin Getz | a3b8111 | 2008-02-06 01:36:26 -0800 | [diff] [blame] | 160 | * _etext _einittext; they can move between pass 1 and 2 when |
| 161 | * the kallsyms data are added. If these symbols move then |
| 162 | * they may get dropped in pass 2, which breaks the kallsyms |
| 163 | * rules. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | */ |
Robin Getz | a3b8111 | 2008-02-06 01:36:26 -0800 | [diff] [blame] | 165 | if ((s->addr == _etext && |
| 166 | strcmp((char *)s->sym + offset, "_etext")) || |
| 167 | (s->addr == _einittext && |
| 168 | strcmp((char *)s->sym + offset, "_einittext"))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* Exclude symbols which vary between passes. */ |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 173 | if (strstr((char *)s->sym + offset, "_compiled.")) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return 0; |
| 175 | |
| 176 | for (i = 0; special_symbols[i]; i++) |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 177 | if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | return 0; |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 183 | static void read_map(FILE *in) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
| 185 | while (!feof(in)) { |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 186 | if (table_cnt >= table_size) { |
| 187 | table_size += 10000; |
| 188 | table = realloc(table, sizeof(*table) * table_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (!table) { |
| 190 | fprintf(stderr, "out of memory\n"); |
| 191 | exit (1); |
| 192 | } |
| 193 | } |
Paulo Marques | f2df3f6 | 2008-02-06 01:37:33 -0800 | [diff] [blame] | 194 | if (read_symbol(in, &table[table_cnt]) == 0) { |
| 195 | table[table_cnt].start_pos = table_cnt; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 196 | table_cnt++; |
Paulo Marques | f2df3f6 | 2008-02-06 01:37:33 -0800 | [diff] [blame] | 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | static void output_label(char *label) |
| 202 | { |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 203 | if (symbol_prefix_char) |
| 204 | printf(".globl %c%s\n", symbol_prefix_char, label); |
| 205 | else |
| 206 | printf(".globl %s\n", label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | printf("\tALGN\n"); |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 208 | if (symbol_prefix_char) |
| 209 | printf("%c%s:\n", symbol_prefix_char, label); |
| 210 | else |
| 211 | printf("%s:\n", label); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* uncompress a compressed symbol. When this function is called, the best table |
| 215 | * might still be compressed itself, so the function needs to be recursive */ |
| 216 | static int expand_symbol(unsigned char *data, int len, char *result) |
| 217 | { |
| 218 | int c, rlen, total=0; |
| 219 | |
| 220 | while (len) { |
| 221 | c = *data; |
| 222 | /* if the table holds a single char that is the same as the one |
| 223 | * we are looking for, then end the search */ |
| 224 | if (best_table[c][0]==c && best_table_len[c]==1) { |
| 225 | *result++ = c; |
| 226 | total++; |
| 227 | } else { |
| 228 | /* if not, recurse and expand */ |
| 229 | rlen = expand_symbol(best_table[c], best_table_len[c], result); |
| 230 | total += rlen; |
| 231 | result += rlen; |
| 232 | } |
| 233 | data++; |
| 234 | len--; |
| 235 | } |
| 236 | *result=0; |
| 237 | |
| 238 | return total; |
| 239 | } |
| 240 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 241 | static void write_src(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 243 | unsigned int i, k, off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | unsigned int best_idx[256]; |
| 245 | unsigned int *markers; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 246 | char buf[KSYM_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | printf("#include <asm/types.h>\n"); |
| 249 | printf("#if BITS_PER_LONG == 64\n"); |
| 250 | printf("#define PTR .quad\n"); |
| 251 | printf("#define ALGN .align 8\n"); |
| 252 | printf("#else\n"); |
| 253 | printf("#define PTR .long\n"); |
| 254 | printf("#define ALGN .align 4\n"); |
| 255 | printf("#endif\n"); |
| 256 | |
Jan Beulich | aad0947 | 2006-12-08 02:35:57 -0800 | [diff] [blame] | 257 | printf("\t.section .rodata, \"a\"\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Eric W. Biederman | fd593d1 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 259 | /* Provide proper symbols relocatability by their '_text' |
| 260 | * relativeness. The symbol names cannot be used to construct |
| 261 | * normal symbol references as the list of symbols contains |
| 262 | * symbols that are declared static and are private to their |
| 263 | * .o files. This prevents .tmp_kallsyms.o or any other |
| 264 | * object from referencing them. |
| 265 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | output_label("kallsyms_addresses"); |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 267 | for (i = 0; i < table_cnt; i++) { |
Eric W. Biederman | fd593d1 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 268 | if (toupper(table[i].sym[0]) != 'A') { |
Vivek Goyal | 2c22d8b | 2006-12-07 02:14:10 +0100 | [diff] [blame] | 269 | if (_text <= table[i].addr) |
| 270 | printf("\tPTR\t_text + %#llx\n", |
| 271 | table[i].addr - _text); |
| 272 | else |
| 273 | printf("\tPTR\t_text - %#llx\n", |
| 274 | _text - table[i].addr); |
Eric W. Biederman | fd593d1 | 2006-12-07 02:14:04 +0100 | [diff] [blame] | 275 | } else { |
| 276 | printf("\tPTR\t%#llx\n", table[i].addr); |
| 277 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | printf("\n"); |
| 280 | |
| 281 | output_label("kallsyms_num_syms"); |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 282 | printf("\tPTR\t%d\n", table_cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | printf("\n"); |
| 284 | |
| 285 | /* table of offset markers, that give the offset in the compressed stream |
| 286 | * every 256 symbols */ |
Jesper Juhl | f1a136e | 2006-03-25 03:07:46 -0800 | [diff] [blame] | 287 | markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256)); |
| 288 | if (!markers) { |
| 289 | fprintf(stderr, "kallsyms failure: " |
| 290 | "unable to allocate required memory\n"); |
| 291 | exit(EXIT_FAILURE); |
| 292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | output_label("kallsyms_names"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | off = 0; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 296 | for (i = 0; i < table_cnt; i++) { |
| 297 | if ((i & 0xFF) == 0) |
| 298 | markers[i >> 8] = off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
| 300 | printf("\t.byte 0x%02x", table[i].len); |
| 301 | for (k = 0; k < table[i].len; k++) |
| 302 | printf(", 0x%02x", table[i].sym[k]); |
| 303 | printf("\n"); |
| 304 | |
| 305 | off += table[i].len + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | printf("\n"); |
| 308 | |
| 309 | output_label("kallsyms_markers"); |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 310 | for (i = 0; i < ((table_cnt + 255) >> 8); i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | printf("\tPTR\t%d\n", markers[i]); |
| 312 | printf("\n"); |
| 313 | |
| 314 | free(markers); |
| 315 | |
| 316 | output_label("kallsyms_token_table"); |
| 317 | off = 0; |
| 318 | for (i = 0; i < 256; i++) { |
| 319 | best_idx[i] = off; |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 320 | expand_symbol(best_table[i], best_table_len[i], buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | printf("\t.asciz\t\"%s\"\n", buf); |
| 322 | off += strlen(buf) + 1; |
| 323 | } |
| 324 | printf("\n"); |
| 325 | |
| 326 | output_label("kallsyms_token_index"); |
| 327 | for (i = 0; i < 256; i++) |
| 328 | printf("\t.short\t%d\n", best_idx[i]); |
| 329 | printf("\n"); |
| 330 | } |
| 331 | |
| 332 | |
| 333 | /* table lookup compression functions */ |
| 334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* count all the possible tokens in a symbol */ |
| 336 | static void learn_symbol(unsigned char *symbol, int len) |
| 337 | { |
| 338 | int i; |
| 339 | |
| 340 | for (i = 0; i < len - 1; i++) |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 341 | token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* decrease the count for all the possible tokens in a symbol */ |
| 345 | static void forget_symbol(unsigned char *symbol, int len) |
| 346 | { |
| 347 | int i; |
| 348 | |
| 349 | for (i = 0; i < len - 1; i++) |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 350 | token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 353 | /* 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] | 354 | static void build_initial_tok_table(void) |
| 355 | { |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 356 | unsigned int i, pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 358 | pos = 0; |
| 359 | for (i = 0; i < table_cnt; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | if ( symbol_valid(&table[i]) ) { |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 361 | if (pos != i) |
| 362 | table[pos] = table[i]; |
| 363 | learn_symbol(table[pos].sym, table[pos].len); |
| 364 | pos++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 367 | table_cnt = pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Paulo Marques | 7c5d249 | 2007-06-20 18:09:00 +0100 | [diff] [blame] | 370 | static void *find_token(unsigned char *str, int len, unsigned char *token) |
| 371 | { |
| 372 | int i; |
| 373 | |
| 374 | for (i = 0; i < len - 1; i++) { |
| 375 | if (str[i] == token[0] && str[i+1] == token[1]) |
| 376 | return &str[i]; |
| 377 | } |
| 378 | return NULL; |
| 379 | } |
| 380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Paulo Marques | 7c5d249 | 2007-06-20 18:09:00 +0100 | [diff] [blame] | 394 | p2 = find_token(p1, len, str); |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 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 | 7c5d249 | 2007-06-20 18:09:00 +0100 | [diff] [blame] | 413 | p2 = find_token(p1, size, str); |
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 | |
Paulo Marques | f2df3f6 | 2008-02-06 01:37:33 -0800 | [diff] [blame] | 500 | static int compare_symbols(const void *a, const void *b) |
| 501 | { |
| 502 | const struct sym_entry *sa; |
| 503 | const struct sym_entry *sb; |
| 504 | int wa, wb; |
| 505 | |
| 506 | sa = a; |
| 507 | sb = b; |
| 508 | |
| 509 | /* sort by address first */ |
| 510 | if (sa->addr > sb->addr) |
| 511 | return 1; |
| 512 | if (sa->addr < sb->addr) |
| 513 | return -1; |
| 514 | |
| 515 | /* sort by "weakness" type */ |
| 516 | wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W'); |
| 517 | wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W'); |
| 518 | if (wa != wb) |
| 519 | return wa - wb; |
| 520 | |
| 521 | /* sort by initial order, so that other symbols are left undisturbed */ |
| 522 | return sa->start_pos - sb->start_pos; |
| 523 | } |
| 524 | |
| 525 | static void sort_symbols(void) |
| 526 | { |
| 527 | qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols); |
| 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Paulo Marques | b3dbb4e | 2005-09-06 15:16:31 -0700 | [diff] [blame] | 530 | int main(int argc, char **argv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Yoshinori Sato | 41f11a4 | 2005-05-01 08:59:06 -0700 | [diff] [blame] | 532 | if (argc >= 2) { |
| 533 | int i; |
| 534 | for (i = 1; i < argc; i++) { |
| 535 | if(strcmp(argv[i], "--all-symbols") == 0) |
| 536 | all_symbols = 1; |
| 537 | else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) { |
| 538 | char *p = &argv[i][16]; |
| 539 | /* skip quote */ |
| 540 | if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\'')) |
| 541 | p++; |
| 542 | symbol_prefix_char = *p; |
| 543 | } else |
| 544 | usage(); |
| 545 | } |
| 546 | } else if (argc != 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | usage(); |
| 548 | |
| 549 | read_map(stdin); |
Paulo Marques | f2df3f6 | 2008-02-06 01:37:33 -0800 | [diff] [blame] | 550 | sort_symbols(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | optimize_token_table(); |
| 552 | write_src(); |
| 553 | |
| 554 | return 0; |
| 555 | } |