1 /* Generate assembler source containing symbol information
3 * Copyright 2002 by Kai Germaschewski
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
8 * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
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
27 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
30 #define KSYM_NAME_LEN 128
33 unsigned long long addr
;
35 unsigned int start_pos
;
40 const char *stext
, *etext
;
41 unsigned long long start
, end
;
44 static unsigned long long _text
;
45 static struct text_range text_ranges
[] = {
46 { "_stext", "_etext" },
47 { "_sinittext", "_einittext" },
48 { "_stext_l1", "_etext_l1" }, /* Blackfin on-chip L1 inst SRAM */
49 { "_stext_l2", "_etext_l2" }, /* Blackfin on-chip L2 SRAM */
51 #define text_range_text (&text_ranges[0])
52 #define text_range_inittext (&text_ranges[1])
54 static struct sym_entry
*table
;
55 static unsigned int table_size
, table_cnt
;
56 static int all_symbols
= 0;
57 static char symbol_prefix_char
= '\0';
59 int token_profit
[0x10000];
61 /* the table that holds the result of the compression */
62 unsigned char best_table
[256][2];
63 unsigned char best_table_len
[256];
66 static void usage(void)
68 fprintf(stderr
, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
73 * This ignores the intensely annoying "mapping symbols" found
74 * in ARM ELF files: $a, $t and $d.
76 static inline int is_arm_mapping_symbol(const char *str
)
78 return str
[0] == '$' && strchr("atd", str
[1])
79 && (str
[2] == '\0' || str
[2] == '.');
82 static int read_symbol_tr(const char *sym
, unsigned long long addr
)
85 struct text_range
*tr
;
87 for (i
= 0; i
< ARRAY_SIZE(text_ranges
); ++i
) {
90 if (strcmp(sym
, tr
->stext
) == 0) {
93 } else if (strcmp(sym
, tr
->etext
) == 0) {
102 static int read_symbol(FILE *in
, struct sym_entry
*s
)
108 rc
= fscanf(in
, "%llx %c %499s\n", &s
->addr
, &stype
, str
);
118 /* skip prefix char */
119 if (symbol_prefix_char
&& str
[0] == symbol_prefix_char
)
122 /* Ignore most absolute/undefined (?) symbols. */
123 if (strcmp(sym
, "_text") == 0)
125 else if (read_symbol_tr(sym
, s
->addr
) == 0)
127 else if (toupper(stype
) == 'A')
129 /* Keep these useful absolute symbols */
130 if (strcmp(sym
, "__kernel_syscall_via_break") &&
131 strcmp(sym
, "__kernel_syscall_via_epc") &&
132 strcmp(sym
, "__kernel_sigtramp") &&
137 else if (toupper(stype
) == 'U' ||
138 is_arm_mapping_symbol(sym
))
140 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
141 else if (str
[0] == '$')
143 /* exclude debugging symbols */
144 else if (stype
== 'N')
147 /* include the type field in the symbol name, so that it gets
148 * compressed together */
149 s
->len
= strlen(str
) + 1;
150 s
->sym
= malloc(s
->len
+ 1);
152 fprintf(stderr
, "kallsyms failure: "
153 "unable to allocate required amount of memory\n");
156 strcpy((char *)s
->sym
+ 1, str
);
162 static int symbol_valid_tr(struct sym_entry
*s
)
165 struct text_range
*tr
;
167 for (i
= 0; i
< ARRAY_SIZE(text_ranges
); ++i
) {
168 tr
= &text_ranges
[i
];
170 if (s
->addr
>= tr
->start
&& s
->addr
<= tr
->end
)
177 static int symbol_valid(struct sym_entry
*s
)
179 /* Symbols which vary between passes. Passes 1 and 2 must have
180 * identical symbol lists. The kallsyms_* symbols below are only added
181 * after pass 1, they would be included in pass 2 when --all-symbols is
182 * specified so exclude them to get a stable symbol list.
184 static char *special_symbols
[] = {
185 "kallsyms_addresses",
189 "kallsyms_token_table",
190 "kallsyms_token_index",
192 /* Exclude linker generated symbols which vary between passes */
193 "_SDA_BASE_", /* ppc */
194 "_SDA2_BASE_", /* ppc */
199 /* skip prefix char */
200 if (symbol_prefix_char
&& *(s
->sym
+ 1) == symbol_prefix_char
)
203 /* if --all-symbols is not specified, then symbols outside the text
204 * and inittext sections are discarded */
206 if (symbol_valid_tr(s
) == 0)
208 /* Corner case. Discard any symbols with the same value as
209 * _etext _einittext; they can move between pass 1 and 2 when
210 * the kallsyms data are added. If these symbols move then
211 * they may get dropped in pass 2, which breaks the kallsyms
214 if ((s
->addr
== text_range_text
->end
&&
215 strcmp((char *)s
->sym
+ offset
, text_range_text
->etext
)) ||
216 (s
->addr
== text_range_inittext
->end
&&
217 strcmp((char *)s
->sym
+ offset
, text_range_inittext
->etext
)))
221 /* Exclude symbols which vary between passes. */
222 if (strstr((char *)s
->sym
+ offset
, "_compiled."))
225 for (i
= 0; special_symbols
[i
]; i
++)
226 if( strcmp((char *)s
->sym
+ offset
, special_symbols
[i
]) == 0 )
232 static void read_map(FILE *in
)
235 if (table_cnt
>= table_size
) {
237 table
= realloc(table
, sizeof(*table
) * table_size
);
239 fprintf(stderr
, "out of memory\n");
243 if (read_symbol(in
, &table
[table_cnt
]) == 0) {
244 table
[table_cnt
].start_pos
= table_cnt
;
250 static void output_label(char *label
)
252 if (symbol_prefix_char
)
253 printf(".globl %c%s\n", symbol_prefix_char
, label
);
255 printf(".globl %s\n", label
);
257 if (symbol_prefix_char
)
258 printf("%c%s:\n", symbol_prefix_char
, label
);
260 printf("%s:\n", label
);
263 /* uncompress a compressed symbol. When this function is called, the best table
264 * might still be compressed itself, so the function needs to be recursive */
265 static int expand_symbol(unsigned char *data
, int len
, char *result
)
267 int c
, rlen
, total
=0;
271 /* if the table holds a single char that is the same as the one
272 * we are looking for, then end the search */
273 if (best_table
[c
][0]==c
&& best_table_len
[c
]==1) {
277 /* if not, recurse and expand */
278 rlen
= expand_symbol(best_table
[c
], best_table_len
[c
], result
);
290 static void write_src(void)
292 unsigned int i
, k
, off
;
293 unsigned int best_idx
[256];
294 unsigned int *markers
;
295 char buf
[KSYM_NAME_LEN
];
297 printf("#include <asm/types.h>\n");
298 printf("#if BITS_PER_LONG == 64\n");
299 printf("#define PTR .quad\n");
300 printf("#define ALGN .align 8\n");
302 printf("#define PTR .long\n");
303 printf("#define ALGN .align 4\n");
306 printf("\t.section .rodata, \"a\"\n");
308 /* Provide proper symbols relocatability by their '_text'
309 * relativeness. The symbol names cannot be used to construct
310 * normal symbol references as the list of symbols contains
311 * symbols that are declared static and are private to their
312 * .o files. This prevents .tmp_kallsyms.o or any other
313 * object from referencing them.
315 output_label("kallsyms_addresses");
316 for (i
= 0; i
< table_cnt
; i
++) {
317 if (toupper(table
[i
].sym
[0]) != 'A') {
318 if (_text
<= table
[i
].addr
)
319 printf("\tPTR\t_text + %#llx\n",
320 table
[i
].addr
- _text
);
322 printf("\tPTR\t_text - %#llx\n",
323 _text
- table
[i
].addr
);
325 printf("\tPTR\t%#llx\n", table
[i
].addr
);
330 output_label("kallsyms_num_syms");
331 printf("\tPTR\t%d\n", table_cnt
);
334 /* table of offset markers, that give the offset in the compressed stream
335 * every 256 symbols */
336 markers
= malloc(sizeof(unsigned int) * ((table_cnt
+ 255) / 256));
338 fprintf(stderr
, "kallsyms failure: "
339 "unable to allocate required memory\n");
343 output_label("kallsyms_names");
345 for (i
= 0; i
< table_cnt
; i
++) {
347 markers
[i
>> 8] = off
;
349 printf("\t.byte 0x%02x", table
[i
].len
);
350 for (k
= 0; k
< table
[i
].len
; k
++)
351 printf(", 0x%02x", table
[i
].sym
[k
]);
354 off
+= table
[i
].len
+ 1;
358 output_label("kallsyms_markers");
359 for (i
= 0; i
< ((table_cnt
+ 255) >> 8); i
++)
360 printf("\tPTR\t%d\n", markers
[i
]);
365 output_label("kallsyms_token_table");
367 for (i
= 0; i
< 256; i
++) {
369 expand_symbol(best_table
[i
], best_table_len
[i
], buf
);
370 printf("\t.asciz\t\"%s\"\n", buf
);
371 off
+= strlen(buf
) + 1;
375 output_label("kallsyms_token_index");
376 for (i
= 0; i
< 256; i
++)
377 printf("\t.short\t%d\n", best_idx
[i
]);
382 /* table lookup compression functions */
384 /* count all the possible tokens in a symbol */
385 static void learn_symbol(unsigned char *symbol
, int len
)
389 for (i
= 0; i
< len
- 1; i
++)
390 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]++;
393 /* decrease the count for all the possible tokens in a symbol */
394 static void forget_symbol(unsigned char *symbol
, int len
)
398 for (i
= 0; i
< len
- 1; i
++)
399 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]--;
402 /* remove all the invalid symbols from the table and do the initial token count */
403 static void build_initial_tok_table(void)
408 for (i
= 0; i
< table_cnt
; i
++) {
409 if ( symbol_valid(&table
[i
]) ) {
411 table
[pos
] = table
[i
];
412 learn_symbol(table
[pos
].sym
, table
[pos
].len
);
419 static void *find_token(unsigned char *str
, int len
, unsigned char *token
)
423 for (i
= 0; i
< len
- 1; i
++) {
424 if (str
[i
] == token
[0] && str
[i
+1] == token
[1])
430 /* replace a given token in all the valid symbols. Use the sampled symbols
431 * to update the counts */
432 static void compress_symbols(unsigned char *str
, int idx
)
434 unsigned int i
, len
, size
;
435 unsigned char *p1
, *p2
;
437 for (i
= 0; i
< table_cnt
; i
++) {
442 /* find the token on the symbol */
443 p2
= find_token(p1
, len
, str
);
446 /* decrease the counts for this symbol's tokens */
447 forget_symbol(table
[i
].sym
, len
);
455 memmove(p2
, p2
+ 1, size
);
461 /* find the token on the symbol */
462 p2
= find_token(p1
, size
, str
);
468 /* increase the counts for this symbol's new tokens */
469 learn_symbol(table
[i
].sym
, len
);
473 /* search the token with the maximum profit */
474 static int find_best_token(void)
476 int i
, best
, bestprofit
;
481 for (i
= 0; i
< 0x10000; i
++) {
482 if (token_profit
[i
] > bestprofit
) {
484 bestprofit
= token_profit
[i
];
490 /* this is the core of the algorithm: calculate the "best" table */
491 static void optimize_result(void)
495 /* using the '\0' symbol last allows compress_symbols to use standard
496 * fast string functions */
497 for (i
= 255; i
>= 0; i
--) {
499 /* if this table slot is empty (it is not used by an actual
500 * original char code */
501 if (!best_table_len
[i
]) {
503 /* find the token with the breates profit value */
504 best
= find_best_token();
506 /* place it in the "best" table */
507 best_table_len
[i
] = 2;
508 best_table
[i
][0] = best
& 0xFF;
509 best_table
[i
][1] = (best
>> 8) & 0xFF;
511 /* replace this token in all the valid symbols */
512 compress_symbols(best_table
[i
], i
);
517 /* start by placing the symbols that are actually used on the table */
518 static void insert_real_symbols_in_table(void)
520 unsigned int i
, j
, c
;
522 memset(best_table
, 0, sizeof(best_table
));
523 memset(best_table_len
, 0, sizeof(best_table_len
));
525 for (i
= 0; i
< table_cnt
; i
++) {
526 for (j
= 0; j
< table
[i
].len
; j
++) {
534 static void optimize_token_table(void)
536 build_initial_tok_table();
538 insert_real_symbols_in_table();
540 /* When valid symbol is not registered, exit to error */
542 fprintf(stderr
, "No valid symbol.\n");
549 /* guess for "linker script provide" symbol */
550 static int may_be_linker_script_provide_symbol(const struct sym_entry
*se
)
552 const char *symbol
= (char *)se
->sym
+ 1;
553 int len
= se
->len
- 1;
558 if (symbol
[0] != '_' || symbol
[1] != '_')
562 if (!memcmp(symbol
+ 2, "start_", 6))
566 if (!memcmp(symbol
+ 2, "stop_", 5))
570 if (!memcmp(symbol
+ 2, "end_", 4))
574 if (!memcmp(symbol
+ len
- 6, "_start", 6))
578 if (!memcmp(symbol
+ len
- 4, "_end", 4))
584 static int prefix_underscores_count(const char *str
)
586 const char *tail
= str
;
594 static int compare_symbols(const void *a
, const void *b
)
596 const struct sym_entry
*sa
;
597 const struct sym_entry
*sb
;
603 /* sort by address first */
604 if (sa
->addr
> sb
->addr
)
606 if (sa
->addr
< sb
->addr
)
609 /* sort by "weakness" type */
610 wa
= (sa
->sym
[0] == 'w') || (sa
->sym
[0] == 'W');
611 wb
= (sb
->sym
[0] == 'w') || (sb
->sym
[0] == 'W');
615 /* sort by "linker script provide" type */
616 wa
= may_be_linker_script_provide_symbol(sa
);
617 wb
= may_be_linker_script_provide_symbol(sb
);
621 /* sort by the number of prefix underscores */
622 wa
= prefix_underscores_count((const char *)sa
->sym
+ 1);
623 wb
= prefix_underscores_count((const char *)sb
->sym
+ 1);
627 /* sort by initial order, so that other symbols are left undisturbed */
628 return sa
->start_pos
- sb
->start_pos
;
631 static void sort_symbols(void)
633 qsort(table
, table_cnt
, sizeof(struct sym_entry
), compare_symbols
);
636 int main(int argc
, char **argv
)
640 for (i
= 1; i
< argc
; i
++) {
641 if(strcmp(argv
[i
], "--all-symbols") == 0)
643 else if (strncmp(argv
[i
], "--symbol-prefix=", 16) == 0) {
644 char *p
= &argv
[i
][16];
646 if ((*p
== '"' && *(p
+2) == '"') || (*p
== '\'' && *(p
+2) == '\''))
648 symbol_prefix_char
= *p
;
652 } else if (argc
!= 1)
657 optimize_token_table();