]>
git.ipfire.org Git - people/ms/linux.git/blob - scripts/kallsyms.c
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
28 #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
;
36 unsigned int percpu_absolute
;
41 const char *start_sym
, *end_sym
;
42 unsigned long long start
, end
;
45 static unsigned long long _text
;
46 static unsigned long long relative_base
;
47 static struct addr_range text_ranges
[] = {
48 { "_stext", "_etext" },
49 { "_sinittext", "_einittext" },
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
;
61 static int absolute_percpu
;
62 static int base_relative
;
64 static int token_profit
[0x10000];
66 /* the table that holds the result of the compression */
67 static unsigned char best_table
[256][2];
68 static unsigned char best_table_len
[256];
71 static void usage(void)
73 fprintf(stderr
, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
74 "[--base-relative] < in.map > out.S\n");
78 static char *sym_name(const struct sym_entry
*s
)
80 return (char *)s
->sym
+ 1;
83 static bool is_ignored_symbol(const char *name
, char type
)
85 /* Symbol names that exactly match to the following are ignored.*/
86 static const char * const ignored_symbols
[] = {
88 * Symbols which vary between passes. Passes 1 and 2 must have
89 * identical symbol lists. The kallsyms_* symbols below are
90 * only added after pass 1, they would be included in pass 2
91 * when --all-symbols is specified so exclude them to get a
96 "kallsyms_relative_base",
100 "kallsyms_token_table",
101 "kallsyms_token_index",
102 /* Exclude linker generated symbols which vary between passes */
103 "_SDA_BASE_", /* ppc */
104 "_SDA2_BASE_", /* ppc */
108 /* Symbol names that begin with the following are ignored.*/
109 static const char * const ignored_prefixes
[] = {
110 "$", /* local symbols for ARM, MIPS, etc. */
111 ".L", /* local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc. */
112 "__crc_", /* modversions */
113 "__efistub_", /* arm64 EFI stub namespace */
114 "__kvm_nvhe_$", /* arm64 local symbols in non-VHE KVM namespace */
115 "__kvm_nvhe_.L", /* arm64 local symbols in non-VHE KVM namespace */
116 "__AArch64ADRPThunk_", /* arm64 lld */
117 "__ARMV5PILongThunk_", /* arm lld */
118 "__ARMV7PILongThunk_",
119 "__ThumbV7PILongThunk_",
120 "__LA25Thunk_", /* mips lld */
125 /* Symbol names that end with the following are ignored.*/
126 static const char * const ignored_suffixes
[] = {
127 "_from_arm", /* arm */
128 "_from_thumb", /* arm */
133 /* Symbol names that contain the following are ignored.*/
134 static const char * const ignored_matches
[] = {
135 ".long_branch.", /* ppc stub */
136 ".plt_branch.", /* ppc stub */
140 const char * const *p
;
142 for (p
= ignored_symbols
; *p
; p
++)
143 if (!strcmp(name
, *p
))
146 for (p
= ignored_prefixes
; *p
; p
++)
147 if (!strncmp(name
, *p
, strlen(*p
)))
150 for (p
= ignored_suffixes
; *p
; p
++) {
151 int l
= strlen(name
) - strlen(*p
);
153 if (l
>= 0 && !strcmp(name
+ l
, *p
))
157 for (p
= ignored_matches
; *p
; p
++) {
158 if (strstr(name
, *p
))
162 if (type
== 'U' || type
== 'u')
164 /* exclude debugging symbols */
165 if (type
== 'N' || type
== 'n')
168 if (toupper(type
) == 'A') {
169 /* Keep these useful absolute symbols */
170 if (strcmp(name
, "__kernel_syscall_via_break") &&
171 strcmp(name
, "__kernel_syscall_via_epc") &&
172 strcmp(name
, "__kernel_sigtramp") &&
173 strcmp(name
, "__gp"))
180 static void check_symbol_range(const char *sym
, unsigned long long addr
,
181 struct addr_range
*ranges
, int entries
)
184 struct addr_range
*ar
;
186 for (i
= 0; i
< entries
; ++i
) {
189 if (strcmp(sym
, ar
->start_sym
) == 0) {
192 } else if (strcmp(sym
, ar
->end_sym
) == 0) {
199 static struct sym_entry
*read_symbol(FILE *in
)
201 char name
[500], type
;
202 unsigned long long addr
;
204 struct sym_entry
*sym
;
207 rc
= fscanf(in
, "%llx %c %499s\n", &addr
, &type
, name
);
209 if (rc
!= EOF
&& fgets(name
, 500, in
) == NULL
)
210 fprintf(stderr
, "Read error or end of file.\n");
213 if (strlen(name
) >= KSYM_NAME_LEN
) {
214 fprintf(stderr
, "Symbol %s too long for kallsyms (%zu >= %d).\n"
215 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
216 name
, strlen(name
), KSYM_NAME_LEN
);
220 if (strcmp(name
, "_text") == 0)
223 /* Ignore most absolute/undefined (?) symbols. */
224 if (is_ignored_symbol(name
, type
))
227 check_symbol_range(name
, addr
, text_ranges
, ARRAY_SIZE(text_ranges
));
228 check_symbol_range(name
, addr
, &percpu_range
, 1);
230 /* include the type field in the symbol name, so that it gets
231 * compressed together */
233 len
= strlen(name
) + 1;
235 sym
= malloc(sizeof(*sym
) + len
+ 1);
237 fprintf(stderr
, "kallsyms failure: "
238 "unable to allocate required amount of memory\n");
244 strcpy(sym_name(sym
), name
);
245 sym
->percpu_absolute
= 0;
250 static int symbol_in_range(const struct sym_entry
*s
,
251 const struct addr_range
*ranges
, int entries
)
254 const struct addr_range
*ar
;
256 for (i
= 0; i
< entries
; ++i
) {
259 if (s
->addr
>= ar
->start
&& s
->addr
<= ar
->end
)
266 static int symbol_valid(const struct sym_entry
*s
)
268 const char *name
= sym_name(s
);
270 /* if --all-symbols is not specified, then symbols outside the text
271 * and inittext sections are discarded */
273 if (symbol_in_range(s
, text_ranges
,
274 ARRAY_SIZE(text_ranges
)) == 0)
276 /* Corner case. Discard any symbols with the same value as
277 * _etext _einittext; they can move between pass 1 and 2 when
278 * the kallsyms data are added. If these symbols move then
279 * they may get dropped in pass 2, which breaks the kallsyms
282 if ((s
->addr
== text_range_text
->end
&&
283 strcmp(name
, text_range_text
->end_sym
)) ||
284 (s
->addr
== text_range_inittext
->end
&&
285 strcmp(name
, text_range_inittext
->end_sym
)))
292 /* remove all the invalid symbols from the table */
293 static void shrink_table(void)
298 for (i
= 0; i
< table_cnt
; i
++) {
299 if (symbol_valid(table
[i
])) {
301 table
[pos
] = table
[i
];
309 /* When valid symbol is not registered, exit to error */
311 fprintf(stderr
, "No valid symbol.\n");
316 static void read_map(FILE *in
)
318 struct sym_entry
*sym
;
321 sym
= read_symbol(in
);
325 sym
->start_pos
= table_cnt
;
327 if (table_cnt
>= table_size
) {
329 table
= realloc(table
, sizeof(*table
) * table_size
);
331 fprintf(stderr
, "out of memory\n");
336 table
[table_cnt
++] = sym
;
340 static void output_label(const char *label
)
342 printf(".globl %s\n", label
);
344 printf("%s:\n", label
);
347 /* Provide proper symbols relocatability by their '_text' relativeness. */
348 static void output_address(unsigned long long addr
)
351 printf("\tPTR\t_text + %#llx\n", addr
- _text
);
353 printf("\tPTR\t_text - %#llx\n", _text
- addr
);
356 /* uncompress a compressed symbol. When this function is called, the best table
357 * might still be compressed itself, so the function needs to be recursive */
358 static int expand_symbol(const unsigned char *data
, int len
, char *result
)
360 int c
, rlen
, total
=0;
364 /* if the table holds a single char that is the same as the one
365 * we are looking for, then end the search */
366 if (best_table
[c
][0]==c
&& best_table_len
[c
]==1) {
370 /* if not, recurse and expand */
371 rlen
= expand_symbol(best_table
[c
], best_table_len
[c
], result
);
383 static int symbol_absolute(const struct sym_entry
*s
)
385 return s
->percpu_absolute
;
388 static void write_src(void)
390 unsigned int i
, k
, off
;
391 unsigned int best_idx
[256];
392 unsigned int *markers
;
393 char buf
[KSYM_NAME_LEN
];
395 printf("#include <asm/bitsperlong.h>\n");
396 printf("#if BITS_PER_LONG == 64\n");
397 printf("#define PTR .quad\n");
398 printf("#define ALGN .balign 8\n");
400 printf("#define PTR .long\n");
401 printf("#define ALGN .balign 4\n");
404 printf("\t.section .rodata, \"a\"\n");
407 output_label("kallsyms_addresses");
409 output_label("kallsyms_offsets");
411 for (i
= 0; i
< table_cnt
; i
++) {
414 * Use the offset relative to the lowest value
415 * encountered of all relative symbols, and emit
416 * non-relocatable fixed offsets that will be fixed
423 if (!absolute_percpu
) {
424 offset
= table
[i
]->addr
- relative_base
;
425 overflow
= (offset
< 0 || offset
> UINT_MAX
);
426 } else if (symbol_absolute(table
[i
])) {
427 offset
= table
[i
]->addr
;
428 overflow
= (offset
< 0 || offset
> INT_MAX
);
430 offset
= relative_base
- table
[i
]->addr
- 1;
431 overflow
= (offset
< INT_MIN
|| offset
>= 0);
434 fprintf(stderr
, "kallsyms failure: "
435 "%s symbol value %#llx out of range in relative mode\n",
436 symbol_absolute(table
[i
]) ? "absolute" : "relative",
440 printf("\t.long\t%#x\n", (int)offset
);
441 } else if (!symbol_absolute(table
[i
])) {
442 output_address(table
[i
]->addr
);
444 printf("\tPTR\t%#llx\n", table
[i
]->addr
);
450 output_label("kallsyms_relative_base");
451 output_address(relative_base
);
455 output_label("kallsyms_num_syms");
456 printf("\t.long\t%u\n", table_cnt
);
459 /* table of offset markers, that give the offset in the compressed stream
460 * every 256 symbols */
461 markers
= malloc(sizeof(unsigned int) * ((table_cnt
+ 255) / 256));
463 fprintf(stderr
, "kallsyms failure: "
464 "unable to allocate required memory\n");
468 output_label("kallsyms_names");
470 for (i
= 0; i
< table_cnt
; i
++) {
472 markers
[i
>> 8] = off
;
474 printf("\t.byte 0x%02x", table
[i
]->len
);
475 for (k
= 0; k
< table
[i
]->len
; k
++)
476 printf(", 0x%02x", table
[i
]->sym
[k
]);
479 off
+= table
[i
]->len
+ 1;
483 output_label("kallsyms_markers");
484 for (i
= 0; i
< ((table_cnt
+ 255) >> 8); i
++)
485 printf("\t.long\t%u\n", markers
[i
]);
490 output_label("kallsyms_token_table");
492 for (i
= 0; i
< 256; i
++) {
494 expand_symbol(best_table
[i
], best_table_len
[i
], buf
);
495 printf("\t.asciz\t\"%s\"\n", buf
);
496 off
+= strlen(buf
) + 1;
500 output_label("kallsyms_token_index");
501 for (i
= 0; i
< 256; i
++)
502 printf("\t.short\t%d\n", best_idx
[i
]);
507 /* table lookup compression functions */
509 /* count all the possible tokens in a symbol */
510 static void learn_symbol(const unsigned char *symbol
, int len
)
514 for (i
= 0; i
< len
- 1; i
++)
515 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]++;
518 /* decrease the count for all the possible tokens in a symbol */
519 static void forget_symbol(const unsigned char *symbol
, int len
)
523 for (i
= 0; i
< len
- 1; i
++)
524 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]--;
527 /* do the initial token count */
528 static void build_initial_tok_table(void)
532 for (i
= 0; i
< table_cnt
; i
++)
533 learn_symbol(table
[i
]->sym
, table
[i
]->len
);
536 static unsigned char *find_token(unsigned char *str
, int len
,
537 const unsigned char *token
)
541 for (i
= 0; i
< len
- 1; i
++) {
542 if (str
[i
] == token
[0] && str
[i
+1] == token
[1])
548 /* replace a given token in all the valid symbols. Use the sampled symbols
549 * to update the counts */
550 static void compress_symbols(const unsigned char *str
, int idx
)
552 unsigned int i
, len
, size
;
553 unsigned char *p1
, *p2
;
555 for (i
= 0; i
< table_cnt
; i
++) {
560 /* find the token on the symbol */
561 p2
= find_token(p1
, len
, str
);
564 /* decrease the counts for this symbol's tokens */
565 forget_symbol(table
[i
]->sym
, len
);
573 memmove(p2
, p2
+ 1, size
);
579 /* find the token on the symbol */
580 p2
= find_token(p1
, size
, str
);
586 /* increase the counts for this symbol's new tokens */
587 learn_symbol(table
[i
]->sym
, len
);
591 /* search the token with the maximum profit */
592 static int find_best_token(void)
594 int i
, best
, bestprofit
;
599 for (i
= 0; i
< 0x10000; i
++) {
600 if (token_profit
[i
] > bestprofit
) {
602 bestprofit
= token_profit
[i
];
608 /* this is the core of the algorithm: calculate the "best" table */
609 static void optimize_result(void)
613 /* using the '\0' symbol last allows compress_symbols to use standard
614 * fast string functions */
615 for (i
= 255; i
>= 0; i
--) {
617 /* if this table slot is empty (it is not used by an actual
618 * original char code */
619 if (!best_table_len
[i
]) {
621 /* find the token with the best profit value */
622 best
= find_best_token();
623 if (token_profit
[best
] == 0)
626 /* place it in the "best" table */
627 best_table_len
[i
] = 2;
628 best_table
[i
][0] = best
& 0xFF;
629 best_table
[i
][1] = (best
>> 8) & 0xFF;
631 /* replace this token in all the valid symbols */
632 compress_symbols(best_table
[i
], i
);
637 /* start by placing the symbols that are actually used on the table */
638 static void insert_real_symbols_in_table(void)
640 unsigned int i
, j
, c
;
642 for (i
= 0; i
< table_cnt
; i
++) {
643 for (j
= 0; j
< table
[i
]->len
; j
++) {
644 c
= table
[i
]->sym
[j
];
651 static void optimize_token_table(void)
653 build_initial_tok_table();
655 insert_real_symbols_in_table();
660 /* guess for "linker script provide" symbol */
661 static int may_be_linker_script_provide_symbol(const struct sym_entry
*se
)
663 const char *symbol
= sym_name(se
);
664 int len
= se
->len
- 1;
669 if (symbol
[0] != '_' || symbol
[1] != '_')
673 if (!memcmp(symbol
+ 2, "start_", 6))
677 if (!memcmp(symbol
+ 2, "stop_", 5))
681 if (!memcmp(symbol
+ 2, "end_", 4))
685 if (!memcmp(symbol
+ len
- 6, "_start", 6))
689 if (!memcmp(symbol
+ len
- 4, "_end", 4))
695 static int compare_symbols(const void *a
, const void *b
)
697 const struct sym_entry
*sa
= *(const struct sym_entry
**)a
;
698 const struct sym_entry
*sb
= *(const struct sym_entry
**)b
;
701 /* sort by address first */
702 if (sa
->addr
> sb
->addr
)
704 if (sa
->addr
< sb
->addr
)
707 /* sort by "weakness" type */
708 wa
= (sa
->sym
[0] == 'w') || (sa
->sym
[0] == 'W');
709 wb
= (sb
->sym
[0] == 'w') || (sb
->sym
[0] == 'W');
713 /* sort by "linker script provide" type */
714 wa
= may_be_linker_script_provide_symbol(sa
);
715 wb
= may_be_linker_script_provide_symbol(sb
);
719 /* sort by the number of prefix underscores */
720 wa
= strspn(sym_name(sa
), "_");
721 wb
= strspn(sym_name(sb
), "_");
725 /* sort by initial order, so that other symbols are left undisturbed */
726 return sa
->start_pos
- sb
->start_pos
;
729 static void sort_symbols(void)
731 qsort(table
, table_cnt
, sizeof(table
[0]), compare_symbols
);
734 static void make_percpus_absolute(void)
738 for (i
= 0; i
< table_cnt
; i
++)
739 if (symbol_in_range(table
[i
], &percpu_range
, 1)) {
741 * Keep the 'A' override for percpu symbols to
742 * ensure consistent behavior compared to older
743 * versions of this tool.
745 table
[i
]->sym
[0] = 'A';
746 table
[i
]->percpu_absolute
= 1;
750 /* find the minimum non-absolute symbol address */
751 static void record_relative_base(void)
755 for (i
= 0; i
< table_cnt
; i
++)
756 if (!symbol_absolute(table
[i
])) {
758 * The table is sorted by address.
759 * Take the first non-absolute symbol value.
761 relative_base
= table
[i
]->addr
;
766 int main(int argc
, char **argv
)
770 for (i
= 1; i
< argc
; i
++) {
771 if(strcmp(argv
[i
], "--all-symbols") == 0)
773 else if (strcmp(argv
[i
], "--absolute-percpu") == 0)
775 else if (strcmp(argv
[i
], "--base-relative") == 0)
780 } else if (argc
!= 1)
786 make_percpus_absolute();
789 record_relative_base();
790 optimize_token_table();