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
);
110 if (rc
!= EOF
&& fgets(str
, 500, in
) == NULL
)
111 fprintf(stderr
, "Read error or end of file.\n");
114 if (strlen(str
) > KSYM_NAME_LEN
) {
115 fprintf(stderr
, "Symbol %s too long for kallsyms (%zu vs %d).\n"
116 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
117 str
, strlen(str
), KSYM_NAME_LEN
);
122 /* skip prefix char */
123 if (symbol_prefix_char
&& str
[0] == symbol_prefix_char
)
126 /* Ignore most absolute/undefined (?) symbols. */
127 if (strcmp(sym
, "_text") == 0)
129 else if (read_symbol_tr(sym
, s
->addr
) == 0)
131 else if (toupper(stype
) == 'A')
133 /* Keep these useful absolute symbols */
134 if (strcmp(sym
, "__kernel_syscall_via_break") &&
135 strcmp(sym
, "__kernel_syscall_via_epc") &&
136 strcmp(sym
, "__kernel_sigtramp") &&
141 else if (toupper(stype
) == 'U' ||
142 is_arm_mapping_symbol(sym
))
144 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
145 else if (str
[0] == '$')
147 /* exclude debugging symbols */
148 else if (stype
== 'N')
151 /* include the type field in the symbol name, so that it gets
152 * compressed together */
153 s
->len
= strlen(str
) + 1;
154 s
->sym
= malloc(s
->len
+ 1);
156 fprintf(stderr
, "kallsyms failure: "
157 "unable to allocate required amount of memory\n");
160 strcpy((char *)s
->sym
+ 1, str
);
166 static int symbol_valid_tr(struct sym_entry
*s
)
169 struct text_range
*tr
;
171 for (i
= 0; i
< ARRAY_SIZE(text_ranges
); ++i
) {
172 tr
= &text_ranges
[i
];
174 if (s
->addr
>= tr
->start
&& s
->addr
<= tr
->end
)
181 static int symbol_valid(struct sym_entry
*s
)
183 /* Symbols which vary between passes. Passes 1 and 2 must have
184 * identical symbol lists. The kallsyms_* symbols below are only added
185 * after pass 1, they would be included in pass 2 when --all-symbols is
186 * specified so exclude them to get a stable symbol list.
188 static char *special_symbols
[] = {
189 "kallsyms_addresses",
193 "kallsyms_token_table",
194 "kallsyms_token_index",
196 /* Exclude linker generated symbols which vary between passes */
197 "_SDA_BASE_", /* ppc */
198 "_SDA2_BASE_", /* ppc */
203 /* skip prefix char */
204 if (symbol_prefix_char
&& *(s
->sym
+ 1) == symbol_prefix_char
)
207 /* if --all-symbols is not specified, then symbols outside the text
208 * and inittext sections are discarded */
210 if (symbol_valid_tr(s
) == 0)
212 /* Corner case. Discard any symbols with the same value as
213 * _etext _einittext; they can move between pass 1 and 2 when
214 * the kallsyms data are added. If these symbols move then
215 * they may get dropped in pass 2, which breaks the kallsyms
218 if ((s
->addr
== text_range_text
->end
&&
219 strcmp((char *)s
->sym
+ offset
, text_range_text
->etext
)) ||
220 (s
->addr
== text_range_inittext
->end
&&
221 strcmp((char *)s
->sym
+ offset
, text_range_inittext
->etext
)))
225 /* Exclude symbols which vary between passes. */
226 if (strstr((char *)s
->sym
+ offset
, "_compiled."))
229 for (i
= 0; special_symbols
[i
]; i
++)
230 if( strcmp((char *)s
->sym
+ offset
, special_symbols
[i
]) == 0 )
236 static void read_map(FILE *in
)
239 if (table_cnt
>= table_size
) {
241 table
= realloc(table
, sizeof(*table
) * table_size
);
243 fprintf(stderr
, "out of memory\n");
247 if (read_symbol(in
, &table
[table_cnt
]) == 0) {
248 table
[table_cnt
].start_pos
= table_cnt
;
254 static void output_label(char *label
)
256 if (symbol_prefix_char
)
257 printf(".globl %c%s\n", symbol_prefix_char
, label
);
259 printf(".globl %s\n", label
);
261 if (symbol_prefix_char
)
262 printf("%c%s:\n", symbol_prefix_char
, label
);
264 printf("%s:\n", label
);
267 /* uncompress a compressed symbol. When this function is called, the best table
268 * might still be compressed itself, so the function needs to be recursive */
269 static int expand_symbol(unsigned char *data
, int len
, char *result
)
271 int c
, rlen
, total
=0;
275 /* if the table holds a single char that is the same as the one
276 * we are looking for, then end the search */
277 if (best_table
[c
][0]==c
&& best_table_len
[c
]==1) {
281 /* if not, recurse and expand */
282 rlen
= expand_symbol(best_table
[c
], best_table_len
[c
], result
);
294 static void write_src(void)
296 unsigned int i
, k
, off
;
297 unsigned int best_idx
[256];
298 unsigned int *markers
;
299 char buf
[KSYM_NAME_LEN
];
301 printf("#include <asm/types.h>\n");
302 printf("#if BITS_PER_LONG == 64\n");
303 printf("#define PTR .quad\n");
304 printf("#define ALGN .align 8\n");
306 printf("#define PTR .long\n");
307 printf("#define ALGN .align 4\n");
310 printf("\t.section .rodata, \"a\"\n");
312 /* Provide proper symbols relocatability by their '_text'
313 * relativeness. The symbol names cannot be used to construct
314 * normal symbol references as the list of symbols contains
315 * symbols that are declared static and are private to their
316 * .o files. This prevents .tmp_kallsyms.o or any other
317 * object from referencing them.
319 output_label("kallsyms_addresses");
320 for (i
= 0; i
< table_cnt
; i
++) {
321 if (toupper(table
[i
].sym
[0]) != 'A') {
322 if (_text
<= table
[i
].addr
)
323 printf("\tPTR\t_text + %#llx\n",
324 table
[i
].addr
- _text
);
326 printf("\tPTR\t_text - %#llx\n",
327 _text
- table
[i
].addr
);
329 printf("\tPTR\t%#llx\n", table
[i
].addr
);
334 output_label("kallsyms_num_syms");
335 printf("\tPTR\t%d\n", table_cnt
);
338 /* table of offset markers, that give the offset in the compressed stream
339 * every 256 symbols */
340 markers
= malloc(sizeof(unsigned int) * ((table_cnt
+ 255) / 256));
342 fprintf(stderr
, "kallsyms failure: "
343 "unable to allocate required memory\n");
347 output_label("kallsyms_names");
349 for (i
= 0; i
< table_cnt
; i
++) {
351 markers
[i
>> 8] = off
;
353 printf("\t.byte 0x%02x", table
[i
].len
);
354 for (k
= 0; k
< table
[i
].len
; k
++)
355 printf(", 0x%02x", table
[i
].sym
[k
]);
358 off
+= table
[i
].len
+ 1;
362 output_label("kallsyms_markers");
363 for (i
= 0; i
< ((table_cnt
+ 255) >> 8); i
++)
364 printf("\tPTR\t%d\n", markers
[i
]);
369 output_label("kallsyms_token_table");
371 for (i
= 0; i
< 256; i
++) {
373 expand_symbol(best_table
[i
], best_table_len
[i
], buf
);
374 printf("\t.asciz\t\"%s\"\n", buf
);
375 off
+= strlen(buf
) + 1;
379 output_label("kallsyms_token_index");
380 for (i
= 0; i
< 256; i
++)
381 printf("\t.short\t%d\n", best_idx
[i
]);
386 /* table lookup compression functions */
388 /* count all the possible tokens in a symbol */
389 static void learn_symbol(unsigned char *symbol
, int len
)
393 for (i
= 0; i
< len
- 1; i
++)
394 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]++;
397 /* decrease the count for all the possible tokens in a symbol */
398 static void forget_symbol(unsigned char *symbol
, int len
)
402 for (i
= 0; i
< len
- 1; i
++)
403 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]--;
406 /* remove all the invalid symbols from the table and do the initial token count */
407 static void build_initial_tok_table(void)
412 for (i
= 0; i
< table_cnt
; i
++) {
413 if ( symbol_valid(&table
[i
]) ) {
415 table
[pos
] = table
[i
];
416 learn_symbol(table
[pos
].sym
, table
[pos
].len
);
423 static void *find_token(unsigned char *str
, int len
, unsigned char *token
)
427 for (i
= 0; i
< len
- 1; i
++) {
428 if (str
[i
] == token
[0] && str
[i
+1] == token
[1])
434 /* replace a given token in all the valid symbols. Use the sampled symbols
435 * to update the counts */
436 static void compress_symbols(unsigned char *str
, int idx
)
438 unsigned int i
, len
, size
;
439 unsigned char *p1
, *p2
;
441 for (i
= 0; i
< table_cnt
; i
++) {
446 /* find the token on the symbol */
447 p2
= find_token(p1
, len
, str
);
450 /* decrease the counts for this symbol's tokens */
451 forget_symbol(table
[i
].sym
, len
);
459 memmove(p2
, p2
+ 1, size
);
465 /* find the token on the symbol */
466 p2
= find_token(p1
, size
, str
);
472 /* increase the counts for this symbol's new tokens */
473 learn_symbol(table
[i
].sym
, len
);
477 /* search the token with the maximum profit */
478 static int find_best_token(void)
480 int i
, best
, bestprofit
;
485 for (i
= 0; i
< 0x10000; i
++) {
486 if (token_profit
[i
] > bestprofit
) {
488 bestprofit
= token_profit
[i
];
494 /* this is the core of the algorithm: calculate the "best" table */
495 static void optimize_result(void)
499 /* using the '\0' symbol last allows compress_symbols to use standard
500 * fast string functions */
501 for (i
= 255; i
>= 0; i
--) {
503 /* if this table slot is empty (it is not used by an actual
504 * original char code */
505 if (!best_table_len
[i
]) {
507 /* find the token with the breates profit value */
508 best
= find_best_token();
509 if (token_profit
[best
] == 0)
512 /* place it in the "best" table */
513 best_table_len
[i
] = 2;
514 best_table
[i
][0] = best
& 0xFF;
515 best_table
[i
][1] = (best
>> 8) & 0xFF;
517 /* replace this token in all the valid symbols */
518 compress_symbols(best_table
[i
], i
);
523 /* start by placing the symbols that are actually used on the table */
524 static void insert_real_symbols_in_table(void)
526 unsigned int i
, j
, c
;
528 memset(best_table
, 0, sizeof(best_table
));
529 memset(best_table_len
, 0, sizeof(best_table_len
));
531 for (i
= 0; i
< table_cnt
; i
++) {
532 for (j
= 0; j
< table
[i
].len
; j
++) {
540 static void optimize_token_table(void)
542 build_initial_tok_table();
544 insert_real_symbols_in_table();
546 /* When valid symbol is not registered, exit to error */
548 fprintf(stderr
, "No valid symbol.\n");
555 /* guess for "linker script provide" symbol */
556 static int may_be_linker_script_provide_symbol(const struct sym_entry
*se
)
558 const char *symbol
= (char *)se
->sym
+ 1;
559 int len
= se
->len
- 1;
564 if (symbol
[0] != '_' || symbol
[1] != '_')
568 if (!memcmp(symbol
+ 2, "start_", 6))
572 if (!memcmp(symbol
+ 2, "stop_", 5))
576 if (!memcmp(symbol
+ 2, "end_", 4))
580 if (!memcmp(symbol
+ len
- 6, "_start", 6))
584 if (!memcmp(symbol
+ len
- 4, "_end", 4))
590 static int prefix_underscores_count(const char *str
)
592 const char *tail
= str
;
600 static int compare_symbols(const void *a
, const void *b
)
602 const struct sym_entry
*sa
;
603 const struct sym_entry
*sb
;
609 /* sort by address first */
610 if (sa
->addr
> sb
->addr
)
612 if (sa
->addr
< sb
->addr
)
615 /* sort by "weakness" type */
616 wa
= (sa
->sym
[0] == 'w') || (sa
->sym
[0] == 'W');
617 wb
= (sb
->sym
[0] == 'w') || (sb
->sym
[0] == 'W');
621 /* sort by "linker script provide" type */
622 wa
= may_be_linker_script_provide_symbol(sa
);
623 wb
= may_be_linker_script_provide_symbol(sb
);
627 /* sort by the number of prefix underscores */
628 wa
= prefix_underscores_count((const char *)sa
->sym
+ 1);
629 wb
= prefix_underscores_count((const char *)sb
->sym
+ 1);
633 /* sort by initial order, so that other symbols are left undisturbed */
634 return sa
->start_pos
- sb
->start_pos
;
637 static void sort_symbols(void)
639 qsort(table
, table_cnt
, sizeof(struct sym_entry
), compare_symbols
);
642 int main(int argc
, char **argv
)
646 for (i
= 1; i
< argc
; i
++) {
647 if(strcmp(argv
[i
], "--all-symbols") == 0)
649 else if (strncmp(argv
[i
], "--symbol-prefix=", 16) == 0) {
650 char *p
= &argv
[i
][16];
652 if ((*p
== '"' && *(p
+2) == '"') || (*p
== '\'' && *(p
+2) == '\''))
654 symbol_prefix_char
= *p
;
658 } else if (argc
!= 1)
663 optimize_token_table();
This page took 0.070689 seconds and 6 git commands to generate.