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