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