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 *start_sym
, *end_sym
;
41 unsigned long long start
, end
;
44 static unsigned long long _text
;
45 static struct addr_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 addr_range percpu_range
= {
55 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
58 static struct sym_entry
*table
;
59 static unsigned int table_size
, table_cnt
;
60 static int all_symbols
= 0;
61 static int absolute_percpu
= 0;
62 static char symbol_prefix_char
= '\0';
63 static unsigned long long kernel_start_addr
= 0;
65 int token_profit
[0x10000];
67 /* the table that holds the result of the compression */
68 unsigned char best_table
[256][2];
69 unsigned char best_table_len
[256];
72 static void usage(void)
74 fprintf(stderr
, "Usage: kallsyms [--all-symbols] "
75 "[--symbol-prefix=<prefix char>] "
76 "[--page-offset=<CONFIG_PAGE_OFFSET>] "
77 "< in.map > out.S\n");
82 * This ignores the intensely annoying "mapping symbols" found
83 * in ARM ELF files: $a, $t and $d.
85 static inline int is_arm_mapping_symbol(const char *str
)
87 return str
[0] == '$' && strchr("axtd", str
[1])
88 && (str
[2] == '\0' || str
[2] == '.');
91 static int check_symbol_range(const char *sym
, unsigned long long addr
,
92 struct addr_range
*ranges
, int entries
)
95 struct addr_range
*ar
;
97 for (i
= 0; i
< entries
; ++i
) {
100 if (strcmp(sym
, ar
->start_sym
) == 0) {
103 } else if (strcmp(sym
, ar
->end_sym
) == 0) {
112 static int read_symbol(FILE *in
, struct sym_entry
*s
)
118 rc
= fscanf(in
, "%llx %c %499s\n", &s
->addr
, &stype
, str
);
120 if (rc
!= EOF
&& fgets(str
, 500, in
) == NULL
)
121 fprintf(stderr
, "Read error or end of file.\n");
124 if (strlen(str
) > KSYM_NAME_LEN
) {
125 fprintf(stderr
, "Symbol %s too long for kallsyms (%zu vs %d).\n"
126 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
127 str
, strlen(str
), KSYM_NAME_LEN
);
132 /* skip prefix char */
133 if (symbol_prefix_char
&& str
[0] == symbol_prefix_char
)
136 /* Ignore most absolute/undefined (?) symbols. */
137 if (strcmp(sym
, "_text") == 0)
139 else if (check_symbol_range(sym
, s
->addr
, text_ranges
,
140 ARRAY_SIZE(text_ranges
)) == 0)
142 else if (toupper(stype
) == 'A')
144 /* Keep these useful absolute symbols */
145 if (strcmp(sym
, "__kernel_syscall_via_break") &&
146 strcmp(sym
, "__kernel_syscall_via_epc") &&
147 strcmp(sym
, "__kernel_sigtramp") &&
152 else if (toupper(stype
) == 'U' ||
153 is_arm_mapping_symbol(sym
))
155 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
156 else if (str
[0] == '$')
158 /* exclude debugging symbols */
159 else if (stype
== 'N')
161 /* exclude s390 kasan local symbols */
162 else if (!strncmp(sym
, ".LASANPC", 8))
165 /* include the type field in the symbol name, so that it gets
166 * compressed together */
167 s
->len
= strlen(str
) + 1;
168 s
->sym
= malloc(s
->len
+ 1);
170 fprintf(stderr
, "kallsyms failure: "
171 "unable to allocate required amount of memory\n");
174 strcpy((char *)s
->sym
+ 1, str
);
177 /* Record if we've found __per_cpu_start/end. */
178 check_symbol_range(sym
, s
->addr
, &percpu_range
, 1);
183 static int symbol_in_range(struct sym_entry
*s
, struct addr_range
*ranges
,
187 struct addr_range
*ar
;
189 for (i
= 0; i
< entries
; ++i
) {
192 if (s
->addr
>= ar
->start
&& s
->addr
<= ar
->end
)
199 static int symbol_valid(struct sym_entry
*s
)
201 /* Symbols which vary between passes. Passes 1 and 2 must have
202 * identical symbol lists. The kallsyms_* symbols below are only added
203 * after pass 1, they would be included in pass 2 when --all-symbols is
204 * specified so exclude them to get a stable symbol list.
206 static char *special_symbols
[] = {
207 "kallsyms_addresses",
211 "kallsyms_token_table",
212 "kallsyms_token_index",
214 /* Exclude linker generated symbols which vary between passes */
215 "_SDA_BASE_", /* ppc */
216 "_SDA2_BASE_", /* ppc */
219 static char *special_suffixes
[] = {
224 char *sym_name
= (char *)s
->sym
+ 1;
227 if (s
->addr
< kernel_start_addr
)
230 /* skip prefix char */
231 if (symbol_prefix_char
&& *sym_name
== symbol_prefix_char
)
235 /* if --all-symbols is not specified, then symbols outside the text
236 * and inittext sections are discarded */
238 if (symbol_in_range(s
, text_ranges
,
239 ARRAY_SIZE(text_ranges
)) == 0)
241 /* Corner case. Discard any symbols with the same value as
242 * _etext _einittext; they can move between pass 1 and 2 when
243 * the kallsyms data are added. If these symbols move then
244 * they may get dropped in pass 2, which breaks the kallsyms
247 if ((s
->addr
== text_range_text
->end
&&
249 text_range_text
->end_sym
)) ||
250 (s
->addr
== text_range_inittext
->end
&&
252 text_range_inittext
->end_sym
)))
256 /* Exclude symbols which vary between passes. */
257 for (i
= 0; special_symbols
[i
]; i
++)
258 if (strcmp(sym_name
, special_symbols
[i
]) == 0)
261 for (i
= 0; special_suffixes
[i
]; i
++) {
262 int l
= strlen(sym_name
) - strlen(special_suffixes
[i
]);
264 if (l
>= 0 && strcmp(sym_name
+ l
, special_suffixes
[i
]) == 0)
271 static void read_map(FILE *in
)
274 if (table_cnt
>= table_size
) {
276 table
= realloc(table
, sizeof(*table
) * table_size
);
278 fprintf(stderr
, "out of memory\n");
282 if (read_symbol(in
, &table
[table_cnt
]) == 0) {
283 table
[table_cnt
].start_pos
= table_cnt
;
289 static void output_label(char *label
)
291 if (symbol_prefix_char
)
292 printf(".globl %c%s\n", symbol_prefix_char
, label
);
294 printf(".globl %s\n", label
);
296 if (symbol_prefix_char
)
297 printf("%c%s:\n", symbol_prefix_char
, label
);
299 printf("%s:\n", label
);
302 /* uncompress a compressed symbol. When this function is called, the best table
303 * might still be compressed itself, so the function needs to be recursive */
304 static int expand_symbol(unsigned char *data
, int len
, char *result
)
306 int c
, rlen
, total
=0;
310 /* if the table holds a single char that is the same as the one
311 * we are looking for, then end the search */
312 if (best_table
[c
][0]==c
&& best_table_len
[c
]==1) {
316 /* if not, recurse and expand */
317 rlen
= expand_symbol(best_table
[c
], best_table_len
[c
], result
);
329 static int symbol_absolute(struct sym_entry
*s
)
331 return toupper(s
->sym
[0]) == 'A';
334 static void write_src(void)
336 unsigned int i
, k
, off
;
337 unsigned int best_idx
[256];
338 unsigned int *markers
;
339 char buf
[KSYM_NAME_LEN
];
341 printf("#include <asm/types.h>\n");
342 printf("#if BITS_PER_LONG == 64\n");
343 printf("#define PTR .quad\n");
344 printf("#define ALGN .align 8\n");
346 printf("#define PTR .long\n");
347 printf("#define ALGN .align 4\n");
350 printf("\t.section .rodata, \"a\"\n");
352 /* Provide proper symbols relocatability by their '_text'
353 * relativeness. The symbol names cannot be used to construct
354 * normal symbol references as the list of symbols contains
355 * symbols that are declared static and are private to their
356 * .o files. This prevents .tmp_kallsyms.o or any other
357 * object from referencing them.
359 output_label("kallsyms_addresses");
360 for (i
= 0; i
< table_cnt
; i
++) {
361 if (!symbol_absolute(&table
[i
])) {
362 if (_text
<= table
[i
].addr
)
363 printf("\tPTR\t_text + %#llx\n",
364 table
[i
].addr
- _text
);
366 printf("\tPTR\t_text - %#llx\n",
367 _text
- table
[i
].addr
);
369 printf("\tPTR\t%#llx\n", table
[i
].addr
);
374 output_label("kallsyms_num_syms");
375 printf("\tPTR\t%d\n", table_cnt
);
378 /* table of offset markers, that give the offset in the compressed stream
379 * every 256 symbols */
380 markers
= malloc(sizeof(unsigned int) * ((table_cnt
+ 255) / 256));
382 fprintf(stderr
, "kallsyms failure: "
383 "unable to allocate required memory\n");
387 output_label("kallsyms_names");
389 for (i
= 0; i
< table_cnt
; i
++) {
391 markers
[i
>> 8] = off
;
393 printf("\t.byte 0x%02x", table
[i
].len
);
394 for (k
= 0; k
< table
[i
].len
; k
++)
395 printf(", 0x%02x", table
[i
].sym
[k
]);
398 off
+= table
[i
].len
+ 1;
402 output_label("kallsyms_markers");
403 for (i
= 0; i
< ((table_cnt
+ 255) >> 8); i
++)
404 printf("\tPTR\t%d\n", markers
[i
]);
409 output_label("kallsyms_token_table");
411 for (i
= 0; i
< 256; i
++) {
413 expand_symbol(best_table
[i
], best_table_len
[i
], buf
);
414 printf("\t.asciz\t\"%s\"\n", buf
);
415 off
+= strlen(buf
) + 1;
419 output_label("kallsyms_token_index");
420 for (i
= 0; i
< 256; i
++)
421 printf("\t.short\t%d\n", best_idx
[i
]);
426 /* table lookup compression functions */
428 /* count all the possible tokens in a symbol */
429 static void learn_symbol(unsigned char *symbol
, int len
)
433 for (i
= 0; i
< len
- 1; i
++)
434 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]++;
437 /* decrease the count for all the possible tokens in a symbol */
438 static void forget_symbol(unsigned char *symbol
, int len
)
442 for (i
= 0; i
< len
- 1; i
++)
443 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]--;
446 /* remove all the invalid symbols from the table and do the initial token count */
447 static void build_initial_tok_table(void)
452 for (i
= 0; i
< table_cnt
; i
++) {
453 if ( symbol_valid(&table
[i
]) ) {
455 table
[pos
] = table
[i
];
456 learn_symbol(table
[pos
].sym
, table
[pos
].len
);
463 static void *find_token(unsigned char *str
, int len
, unsigned char *token
)
467 for (i
= 0; i
< len
- 1; i
++) {
468 if (str
[i
] == token
[0] && str
[i
+1] == token
[1])
474 /* replace a given token in all the valid symbols. Use the sampled symbols
475 * to update the counts */
476 static void compress_symbols(unsigned char *str
, int idx
)
478 unsigned int i
, len
, size
;
479 unsigned char *p1
, *p2
;
481 for (i
= 0; i
< table_cnt
; i
++) {
486 /* find the token on the symbol */
487 p2
= find_token(p1
, len
, str
);
490 /* decrease the counts for this symbol's tokens */
491 forget_symbol(table
[i
].sym
, len
);
499 memmove(p2
, p2
+ 1, size
);
505 /* find the token on the symbol */
506 p2
= find_token(p1
, size
, str
);
512 /* increase the counts for this symbol's new tokens */
513 learn_symbol(table
[i
].sym
, len
);
517 /* search the token with the maximum profit */
518 static int find_best_token(void)
520 int i
, best
, bestprofit
;
525 for (i
= 0; i
< 0x10000; i
++) {
526 if (token_profit
[i
] > bestprofit
) {
528 bestprofit
= token_profit
[i
];
534 /* this is the core of the algorithm: calculate the "best" table */
535 static void optimize_result(void)
539 /* using the '\0' symbol last allows compress_symbols to use standard
540 * fast string functions */
541 for (i
= 255; i
>= 0; i
--) {
543 /* if this table slot is empty (it is not used by an actual
544 * original char code */
545 if (!best_table_len
[i
]) {
547 /* find the token with the breates profit value */
548 best
= find_best_token();
549 if (token_profit
[best
] == 0)
552 /* place it in the "best" table */
553 best_table_len
[i
] = 2;
554 best_table
[i
][0] = best
& 0xFF;
555 best_table
[i
][1] = (best
>> 8) & 0xFF;
557 /* replace this token in all the valid symbols */
558 compress_symbols(best_table
[i
], i
);
563 /* start by placing the symbols that are actually used on the table */
564 static void insert_real_symbols_in_table(void)
566 unsigned int i
, j
, c
;
568 memset(best_table
, 0, sizeof(best_table
));
569 memset(best_table_len
, 0, sizeof(best_table_len
));
571 for (i
= 0; i
< table_cnt
; i
++) {
572 for (j
= 0; j
< table
[i
].len
; j
++) {
580 static void optimize_token_table(void)
582 build_initial_tok_table();
584 insert_real_symbols_in_table();
586 /* When valid symbol is not registered, exit to error */
588 fprintf(stderr
, "No valid symbol.\n");
595 /* guess for "linker script provide" symbol */
596 static int may_be_linker_script_provide_symbol(const struct sym_entry
*se
)
598 const char *symbol
= (char *)se
->sym
+ 1;
599 int len
= se
->len
- 1;
604 if (symbol
[0] != '_' || symbol
[1] != '_')
608 if (!memcmp(symbol
+ 2, "start_", 6))
612 if (!memcmp(symbol
+ 2, "stop_", 5))
616 if (!memcmp(symbol
+ 2, "end_", 4))
620 if (!memcmp(symbol
+ len
- 6, "_start", 6))
624 if (!memcmp(symbol
+ len
- 4, "_end", 4))
630 static int prefix_underscores_count(const char *str
)
632 const char *tail
= str
;
640 static int compare_symbols(const void *a
, const void *b
)
642 const struct sym_entry
*sa
;
643 const struct sym_entry
*sb
;
649 /* sort by address first */
650 if (sa
->addr
> sb
->addr
)
652 if (sa
->addr
< sb
->addr
)
655 /* sort by "weakness" type */
656 wa
= (sa
->sym
[0] == 'w') || (sa
->sym
[0] == 'W');
657 wb
= (sb
->sym
[0] == 'w') || (sb
->sym
[0] == 'W');
661 /* sort by "linker script provide" type */
662 wa
= may_be_linker_script_provide_symbol(sa
);
663 wb
= may_be_linker_script_provide_symbol(sb
);
667 /* sort by the number of prefix underscores */
668 wa
= prefix_underscores_count((const char *)sa
->sym
+ 1);
669 wb
= prefix_underscores_count((const char *)sb
->sym
+ 1);
673 /* sort by initial order, so that other symbols are left undisturbed */
674 return sa
->start_pos
- sb
->start_pos
;
677 static void sort_symbols(void)
679 qsort(table
, table_cnt
, sizeof(struct sym_entry
), compare_symbols
);
682 static void make_percpus_absolute(void)
686 for (i
= 0; i
< table_cnt
; i
++)
687 if (symbol_in_range(&table
[i
], &percpu_range
, 1))
688 table
[i
].sym
[0] = 'A';
691 int main(int argc
, char **argv
)
695 for (i
= 1; i
< argc
; i
++) {
696 if(strcmp(argv
[i
], "--all-symbols") == 0)
698 else if (strcmp(argv
[i
], "--absolute-percpu") == 0)
700 else if (strncmp(argv
[i
], "--symbol-prefix=", 16) == 0) {
701 char *p
= &argv
[i
][16];
703 if ((*p
== '"' && *(p
+2) == '"') || (*p
== '\'' && *(p
+2) == '\''))
705 symbol_prefix_char
= *p
;
706 } else if (strncmp(argv
[i
], "--page-offset=", 14) == 0) {
707 const char *p
= &argv
[i
][14];
708 kernel_start_addr
= strtoull(p
, NULL
, 16);
712 } else if (argc
!= 1)
717 make_percpus_absolute();
719 optimize_token_table();