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: kallsyms [--all-symbols] [--absolute-percpu]
9 * [--base-relative] [--lto-clang] in.map > out.S
11 * Table compression uses all the unused char codes on the symbols and
12 * maps these to the most used substrings (tokens). For instance, it might
13 * map char code 0xF7 to represent "write_" and then in every symbol where
14 * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
15 * The used codes themselves are also placed in the table so that the
16 * decompresion can work without "special cases".
17 * Applied to kernel symbols, this usually produces a compression ratio
30 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
32 #define _stringify_1(x) #x
33 #define _stringify(x) _stringify_1(x)
35 #define KSYM_NAME_LEN 512
38 * A substantially bigger size than the current maximum.
40 * It cannot be defined as an expression because it gets stringified
41 * for the fscanf() format string. Therefore, a _Static_assert() is
42 * used instead to maintain the relationship with KSYM_NAME_LEN.
44 #define KSYM_NAME_LEN_BUFFER 2048
46 KSYM_NAME_LEN_BUFFER == KSYM_NAME_LEN * 4,
47 "Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN"
51 unsigned long long addr;
54 unsigned int start_pos;
55 unsigned int percpu_absolute;
60 const char *start_sym, *end_sym;
61 unsigned long long start, end;
64 static unsigned long long _text;
65 static unsigned long long relative_base;
66 static struct addr_range text_ranges[] = {
67 { "_stext", "_etext" },
68 { "_sinittext", "_einittext" },
70 #define text_range_text (&text_ranges[0])
71 #define text_range_inittext (&text_ranges[1])
73 static struct addr_range percpu_range = {
74 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
77 static struct sym_entry **table;
78 static unsigned int table_size, table_cnt;
79 static int all_symbols;
80 static int absolute_percpu;
81 static int base_relative;
84 static int token_profit[0x10000];
86 /* the table that holds the result of the compression */
87 static unsigned char best_table[256][2];
88 static unsigned char best_table_len[256];
91 static void usage(void)
93 fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
94 "[--base-relative] [--lto-clang] in.map > out.S\n");
98 static char *sym_name(const struct sym_entry *s)
100 return (char *)s->sym + 1;
103 static bool is_ignored_symbol(const char *name, char type)
105 /* Symbol names that exactly match to the following are ignored.*/
106 static const char * const ignored_symbols[] = {
108 * Symbols which vary between passes. Passes 1 and 2 must have
109 * identical symbol lists. The kallsyms_* symbols below are
110 * only added after pass 1, they would be included in pass 2
111 * when --all-symbols is specified so exclude them to get a
112 * stable symbol list.
114 "kallsyms_addresses",
116 "kallsyms_relative_base",
120 "kallsyms_token_table",
121 "kallsyms_token_index",
122 "kallsyms_seqs_of_names",
123 /* Exclude linker generated symbols which vary between passes */
124 "_SDA_BASE_", /* ppc */
125 "_SDA2_BASE_", /* ppc */
129 /* Symbol names that begin with the following are ignored.*/
130 static const char * const ignored_prefixes[] = {
131 "__efistub_", /* arm64 EFI stub namespace */
132 "__kvm_nvhe_$", /* arm64 local symbols in non-VHE KVM namespace */
133 "__kvm_nvhe_.L", /* arm64 local symbols in non-VHE KVM namespace */
134 "__AArch64ADRPThunk_", /* arm64 lld */
135 "__ARMV5PILongThunk_", /* arm lld */
136 "__ARMV7PILongThunk_",
137 "__ThumbV7PILongThunk_",
138 "__LA25Thunk_", /* mips lld */
140 "__kcfi_typeid_", /* CFI type identifiers */
144 /* Symbol names that end with the following are ignored.*/
145 static const char * const ignored_suffixes[] = {
146 "_from_arm", /* arm */
147 "_from_thumb", /* arm */
152 /* Symbol names that contain the following are ignored.*/
153 static const char * const ignored_matches[] = {
154 ".long_branch.", /* ppc stub */
155 ".plt_branch.", /* ppc stub */
159 const char * const *p;
161 for (p = ignored_symbols; *p; p++)
162 if (!strcmp(name, *p))
165 for (p = ignored_prefixes; *p; p++)
166 if (!strncmp(name, *p, strlen(*p)))
169 for (p = ignored_suffixes; *p; p++) {
170 int l = strlen(name) - strlen(*p);
172 if (l >= 0 && !strcmp(name + l, *p))
176 for (p = ignored_matches; *p; p++) {
177 if (strstr(name, *p))
181 if (type == 'U' || type == 'u')
183 /* exclude debugging symbols */
184 if (type == 'N' || type == 'n')
187 if (toupper(type) == 'A') {
188 /* Keep these useful absolute symbols */
189 if (strcmp(name, "__kernel_syscall_via_break") &&
190 strcmp(name, "__kernel_syscall_via_epc") &&
191 strcmp(name, "__kernel_sigtramp") &&
192 strcmp(name, "__gp"))
199 static void check_symbol_range(const char *sym, unsigned long long addr,
200 struct addr_range *ranges, int entries)
203 struct addr_range *ar;
205 for (i = 0; i < entries; ++i) {
208 if (strcmp(sym, ar->start_sym) == 0) {
211 } else if (strcmp(sym, ar->end_sym) == 0) {
218 static struct sym_entry *read_symbol(FILE *in)
220 char name[KSYM_NAME_LEN_BUFFER+1], type;
221 unsigned long long addr;
223 struct sym_entry *sym;
226 rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
228 if (rc != EOF && fgets(name, ARRAY_SIZE(name), in) == NULL)
229 fprintf(stderr, "Read error or end of file.\n");
232 if (strlen(name) >= KSYM_NAME_LEN) {
233 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
234 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
235 name, strlen(name), KSYM_NAME_LEN);
239 if (strcmp(name, "_text") == 0)
242 /* Ignore most absolute/undefined (?) symbols. */
243 if (is_ignored_symbol(name, type))
246 check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
247 check_symbol_range(name, addr, &percpu_range, 1);
249 /* include the type field in the symbol name, so that it gets
250 * compressed together */
252 len = strlen(name) + 1;
254 sym = malloc(sizeof(*sym) + len + 1);
256 fprintf(stderr, "kallsyms failure: "
257 "unable to allocate required amount of memory\n");
263 strcpy(sym_name(sym), name);
264 sym->percpu_absolute = 0;
269 static int symbol_in_range(const struct sym_entry *s,
270 const struct addr_range *ranges, int entries)
273 const struct addr_range *ar;
275 for (i = 0; i < entries; ++i) {
278 if (s->addr >= ar->start && s->addr <= ar->end)
285 static int symbol_valid(const struct sym_entry *s)
287 const char *name = sym_name(s);
289 /* if --all-symbols is not specified, then symbols outside the text
290 * and inittext sections are discarded */
292 if (symbol_in_range(s, text_ranges,
293 ARRAY_SIZE(text_ranges)) == 0)
295 /* Corner case. Discard any symbols with the same value as
296 * _etext _einittext; they can move between pass 1 and 2 when
297 * the kallsyms data are added. If these symbols move then
298 * they may get dropped in pass 2, which breaks the kallsyms
301 if ((s->addr == text_range_text->end &&
302 strcmp(name, text_range_text->end_sym)) ||
303 (s->addr == text_range_inittext->end &&
304 strcmp(name, text_range_inittext->end_sym)))
311 /* remove all the invalid symbols from the table */
312 static void shrink_table(void)
317 for (i = 0; i < table_cnt; i++) {
318 if (symbol_valid(table[i])) {
320 table[pos] = table[i];
328 /* When valid symbol is not registered, exit to error */
330 fprintf(stderr, "No valid symbol.\n");
335 static void read_map(const char *in)
338 struct sym_entry *sym;
347 sym = read_symbol(fp);
351 sym->start_pos = table_cnt;
353 if (table_cnt >= table_size) {
355 table = realloc(table, sizeof(*table) * table_size);
357 fprintf(stderr, "out of memory\n");
363 table[table_cnt++] = sym;
369 static void output_label(const char *label)
371 printf(".globl %s\n", label);
373 printf("%s:\n", label);
376 /* Provide proper symbols relocatability by their '_text' relativeness. */
377 static void output_address(unsigned long long addr)
380 printf("\tPTR\t_text + %#llx\n", addr - _text);
382 printf("\tPTR\t_text - %#llx\n", _text - addr);
385 /* uncompress a compressed symbol. When this function is called, the best table
386 * might still be compressed itself, so the function needs to be recursive */
387 static int expand_symbol(const unsigned char *data, int len, char *result)
389 int c, rlen, total=0;
393 /* if the table holds a single char that is the same as the one
394 * we are looking for, then end the search */
395 if (best_table[c][0]==c && best_table_len[c]==1) {
399 /* if not, recurse and expand */
400 rlen = expand_symbol(best_table[c], best_table_len[c], result);
412 static int symbol_absolute(const struct sym_entry *s)
414 return s->percpu_absolute;
417 static char * s_name(char *buf)
419 /* Skip the symbol type */
423 static void cleanup_symbol_name(char *s)
437 * As above, replacing the first '.' in ".llvm." with '\0' does not
438 * affect the main sorting, but it helps us with subsorting.
440 p = strstr(s, ".llvm.");
445 static int compare_names(const void *a, const void *b)
448 char sa_namebuf[KSYM_NAME_LEN];
449 char sb_namebuf[KSYM_NAME_LEN];
450 const struct sym_entry *sa = *(const struct sym_entry **)a;
451 const struct sym_entry *sb = *(const struct sym_entry **)b;
453 expand_symbol(sa->sym, sa->len, sa_namebuf);
454 expand_symbol(sb->sym, sb->len, sb_namebuf);
455 cleanup_symbol_name(s_name(sa_namebuf));
456 cleanup_symbol_name(s_name(sb_namebuf));
457 ret = strcmp(s_name(sa_namebuf), s_name(sb_namebuf));
459 if (sa->addr > sb->addr)
461 else if (sa->addr < sb->addr)
465 return (int)(sa->seq - sb->seq);
471 static void sort_symbols_by_name(void)
473 qsort(table, table_cnt, sizeof(table[0]), compare_names);
476 static void write_src(void)
478 unsigned int i, k, off;
479 unsigned int best_idx[256];
480 unsigned int *markers;
481 char buf[KSYM_NAME_LEN];
483 printf("#include <asm/bitsperlong.h>\n");
484 printf("#if BITS_PER_LONG == 64\n");
485 printf("#define PTR .quad\n");
486 printf("#define ALGN .balign 8\n");
488 printf("#define PTR .long\n");
489 printf("#define ALGN .balign 4\n");
492 printf("\t.section .rodata, \"a\"\n");
495 output_label("kallsyms_addresses");
497 output_label("kallsyms_offsets");
499 for (i = 0; i < table_cnt; i++) {
502 * Use the offset relative to the lowest value
503 * encountered of all relative symbols, and emit
504 * non-relocatable fixed offsets that will be fixed
511 if (!absolute_percpu) {
512 offset = table[i]->addr - relative_base;
513 overflow = (offset < 0 || offset > UINT_MAX);
514 } else if (symbol_absolute(table[i])) {
515 offset = table[i]->addr;
516 overflow = (offset < 0 || offset > INT_MAX);
518 offset = relative_base - table[i]->addr - 1;
519 overflow = (offset < INT_MIN || offset >= 0);
522 fprintf(stderr, "kallsyms failure: "
523 "%s symbol value %#llx out of range in relative mode\n",
524 symbol_absolute(table[i]) ? "absolute" : "relative",
528 printf("\t.long\t%#x\n", (int)offset);
529 } else if (!symbol_absolute(table[i])) {
530 output_address(table[i]->addr);
532 printf("\tPTR\t%#llx\n", table[i]->addr);
538 output_label("kallsyms_relative_base");
539 output_address(relative_base);
543 output_label("kallsyms_num_syms");
544 printf("\t.long\t%u\n", table_cnt);
547 /* table of offset markers, that give the offset in the compressed stream
548 * every 256 symbols */
549 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
551 fprintf(stderr, "kallsyms failure: "
552 "unable to allocate required memory\n");
556 output_label("kallsyms_names");
558 for (i = 0; i < table_cnt; i++) {
560 markers[i >> 8] = off;
563 /* There cannot be any symbol of length zero. */
564 if (table[i]->len == 0) {
565 fprintf(stderr, "kallsyms failure: "
566 "unexpected zero symbol length\n");
570 /* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
571 if (table[i]->len > 0x3FFF) {
572 fprintf(stderr, "kallsyms failure: "
573 "unexpected huge symbol length\n");
577 /* Encode length with ULEB128. */
578 if (table[i]->len <= 0x7F) {
579 /* Most symbols use a single byte for the length. */
580 printf("\t.byte 0x%02x", table[i]->len);
581 off += table[i]->len + 1;
583 /* "Big" symbols use two bytes. */
584 printf("\t.byte 0x%02x, 0x%02x",
585 (table[i]->len & 0x7F) | 0x80,
586 (table[i]->len >> 7) & 0x7F);
587 off += table[i]->len + 2;
589 for (k = 0; k < table[i]->len; k++)
590 printf(", 0x%02x", table[i]->sym[k]);
595 output_label("kallsyms_markers");
596 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
597 printf("\t.long\t%u\n", markers[i]);
602 sort_symbols_by_name();
603 output_label("kallsyms_seqs_of_names");
604 for (i = 0; i < table_cnt; i++)
605 printf("\t.byte 0x%02x, 0x%02x, 0x%02x\n",
606 (unsigned char)(table[i]->seq >> 16),
607 (unsigned char)(table[i]->seq >> 8),
608 (unsigned char)(table[i]->seq >> 0));
611 output_label("kallsyms_token_table");
613 for (i = 0; i < 256; i++) {
615 expand_symbol(best_table[i], best_table_len[i], buf);
616 printf("\t.asciz\t\"%s\"\n", buf);
617 off += strlen(buf) + 1;
621 output_label("kallsyms_token_index");
622 for (i = 0; i < 256; i++)
623 printf("\t.short\t%d\n", best_idx[i]);
628 /* table lookup compression functions */
630 /* count all the possible tokens in a symbol */
631 static void learn_symbol(const unsigned char *symbol, int len)
635 for (i = 0; i < len - 1; i++)
636 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
639 /* decrease the count for all the possible tokens in a symbol */
640 static void forget_symbol(const unsigned char *symbol, int len)
644 for (i = 0; i < len - 1; i++)
645 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
648 /* do the initial token count */
649 static void build_initial_tok_table(void)
653 for (i = 0; i < table_cnt; i++)
654 learn_symbol(table[i]->sym, table[i]->len);
657 static unsigned char *find_token(unsigned char *str, int len,
658 const unsigned char *token)
662 for (i = 0; i < len - 1; i++) {
663 if (str[i] == token[0] && str[i+1] == token[1])
669 /* replace a given token in all the valid symbols. Use the sampled symbols
670 * to update the counts */
671 static void compress_symbols(const unsigned char *str, int idx)
673 unsigned int i, len, size;
674 unsigned char *p1, *p2;
676 for (i = 0; i < table_cnt; i++) {
681 /* find the token on the symbol */
682 p2 = find_token(p1, len, str);
685 /* decrease the counts for this symbol's tokens */
686 forget_symbol(table[i]->sym, len);
694 memmove(p2, p2 + 1, size);
700 /* find the token on the symbol */
701 p2 = find_token(p1, size, str);
707 /* increase the counts for this symbol's new tokens */
708 learn_symbol(table[i]->sym, len);
712 /* search the token with the maximum profit */
713 static int find_best_token(void)
715 int i, best, bestprofit;
720 for (i = 0; i < 0x10000; i++) {
721 if (token_profit[i] > bestprofit) {
723 bestprofit = token_profit[i];
729 /* this is the core of the algorithm: calculate the "best" table */
730 static void optimize_result(void)
734 /* using the '\0' symbol last allows compress_symbols to use standard
735 * fast string functions */
736 for (i = 255; i >= 0; i--) {
738 /* if this table slot is empty (it is not used by an actual
739 * original char code */
740 if (!best_table_len[i]) {
742 /* find the token with the best profit value */
743 best = find_best_token();
744 if (token_profit[best] == 0)
747 /* place it in the "best" table */
748 best_table_len[i] = 2;
749 best_table[i][0] = best & 0xFF;
750 best_table[i][1] = (best >> 8) & 0xFF;
752 /* replace this token in all the valid symbols */
753 compress_symbols(best_table[i], i);
758 /* start by placing the symbols that are actually used on the table */
759 static void insert_real_symbols_in_table(void)
761 unsigned int i, j, c;
763 for (i = 0; i < table_cnt; i++) {
764 for (j = 0; j < table[i]->len; j++) {
765 c = table[i]->sym[j];
772 static void optimize_token_table(void)
774 build_initial_tok_table();
776 insert_real_symbols_in_table();
781 /* guess for "linker script provide" symbol */
782 static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
784 const char *symbol = sym_name(se);
785 int len = se->len - 1;
790 if (symbol[0] != '_' || symbol[1] != '_')
794 if (!memcmp(symbol + 2, "start_", 6))
798 if (!memcmp(symbol + 2, "stop_", 5))
802 if (!memcmp(symbol + 2, "end_", 4))
806 if (!memcmp(symbol + len - 6, "_start", 6))
810 if (!memcmp(symbol + len - 4, "_end", 4))
816 static int compare_symbols(const void *a, const void *b)
818 const struct sym_entry *sa = *(const struct sym_entry **)a;
819 const struct sym_entry *sb = *(const struct sym_entry **)b;
822 /* sort by address first */
823 if (sa->addr > sb->addr)
825 if (sa->addr < sb->addr)
828 /* sort by "weakness" type */
829 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
830 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
834 /* sort by "linker script provide" type */
835 wa = may_be_linker_script_provide_symbol(sa);
836 wb = may_be_linker_script_provide_symbol(sb);
840 /* sort by the number of prefix underscores */
841 wa = strspn(sym_name(sa), "_");
842 wb = strspn(sym_name(sb), "_");
846 /* sort by initial order, so that other symbols are left undisturbed */
847 return sa->start_pos - sb->start_pos;
850 static void sort_symbols(void)
852 qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
855 static void make_percpus_absolute(void)
859 for (i = 0; i < table_cnt; i++)
860 if (symbol_in_range(table[i], &percpu_range, 1)) {
862 * Keep the 'A' override for percpu symbols to
863 * ensure consistent behavior compared to older
864 * versions of this tool.
866 table[i]->sym[0] = 'A';
867 table[i]->percpu_absolute = 1;
871 /* find the minimum non-absolute symbol address */
872 static void record_relative_base(void)
876 for (i = 0; i < table_cnt; i++)
877 if (!symbol_absolute(table[i])) {
879 * The table is sorted by address.
880 * Take the first non-absolute symbol value.
882 relative_base = table[i]->addr;
887 int main(int argc, char **argv)
890 static struct option long_options[] = {
891 {"all-symbols", no_argument, &all_symbols, 1},
892 {"absolute-percpu", no_argument, &absolute_percpu, 1},
893 {"base-relative", no_argument, &base_relative, 1},
894 {"lto-clang", no_argument, <o_clang, 1},
898 int c = getopt_long(argc, argv, "", long_options, NULL);
909 read_map(argv[optind]);
912 make_percpus_absolute();
915 record_relative_base();
916 optimize_token_table();