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