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] in.map > out.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
32 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
34 #define KSYM_NAME_LEN 512
37 unsigned long long addr
;
45 const char *start_sym
, *end_sym
;
46 unsigned long long start
, end
;
49 static unsigned long long _text
;
50 static unsigned long long relative_base
;
51 static struct addr_range text_ranges
[] = {
52 { "_stext", "_etext" },
53 { "_sinittext", "_einittext" },
55 #define text_range_text (&text_ranges[0])
56 #define text_range_inittext (&text_ranges[1])
58 static struct addr_range percpu_range
= {
59 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
62 static struct sym_entry
**table
;
63 static unsigned int table_size
, table_cnt
;
64 static int all_symbols
;
65 static int absolute_percpu
;
67 static int token_profit
[0x10000];
69 /* the table that holds the result of the compression */
70 static unsigned char best_table
[256][2];
71 static unsigned char best_table_len
[256];
74 static void usage(void)
76 fprintf(stderr
, "Usage: kallsyms [--all-symbols] [--absolute-percpu] in.map > out.S\n");
80 static char *sym_name(const struct sym_entry
*s
)
82 return (char *)s
->sym
+ 1;
85 static bool is_ignored_symbol(const char *name
, char type
)
87 if (type
== 'u' || type
== 'n')
90 if (toupper(type
) == 'A') {
91 /* Keep these useful absolute symbols */
92 if (strcmp(name
, "__kernel_syscall_via_break") &&
93 strcmp(name
, "__kernel_syscall_via_epc") &&
94 strcmp(name
, "__kernel_sigtramp") &&
102 static void check_symbol_range(const char *sym
, unsigned long long addr
,
103 struct addr_range
*ranges
, int entries
)
106 struct addr_range
*ar
;
108 for (i
= 0; i
< entries
; ++i
) {
111 if (strcmp(sym
, ar
->start_sym
) == 0) {
114 } else if (strcmp(sym
, ar
->end_sym
) == 0) {
121 static struct sym_entry
*read_symbol(FILE *in
, char **buf
, size_t *buf_len
)
123 char *name
, type
, *p
;
124 unsigned long long addr
;
127 struct sym_entry
*sym
;
130 readlen
= getline(buf
, buf_len
, in
);
133 perror("read_symbol");
139 if ((*buf
)[readlen
- 1] == '\n')
140 (*buf
)[readlen
- 1] = 0;
142 addr
= strtoull(*buf
, &p
, 16);
144 if (*buf
== p
|| *p
++ != ' ' || !isascii((type
= *p
++)) || *p
++ != ' ') {
145 fprintf(stderr
, "line format error\n");
152 if (len
>= KSYM_NAME_LEN
) {
153 fprintf(stderr
, "Symbol %s too long for kallsyms (%zu >= %d).\n"
154 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
155 name
, len
, KSYM_NAME_LEN
);
159 if (strcmp(name
, "_text") == 0)
162 /* Ignore most absolute/undefined (?) symbols. */
163 if (is_ignored_symbol(name
, type
))
166 check_symbol_range(name
, addr
, text_ranges
, ARRAY_SIZE(text_ranges
));
167 check_symbol_range(name
, addr
, &percpu_range
, 1);
169 /* include the type field in the symbol name, so that it gets
170 * compressed together */
173 sym
= xmalloc(sizeof(*sym
) + len
+ 1);
177 strcpy(sym_name(sym
), name
);
178 sym
->percpu_absolute
= false;
183 static int symbol_in_range(const struct sym_entry
*s
,
184 const struct addr_range
*ranges
, int entries
)
187 const struct addr_range
*ar
;
189 for (i
= 0; i
< entries
; ++i
) {
192 if (s
->addr
>= ar
->start
&& s
->addr
<= ar
->end
)
199 static bool string_starts_with(const char *s
, const char *prefix
)
201 return strncmp(s
, prefix
, strlen(prefix
)) == 0;
204 static int symbol_valid(const struct sym_entry
*s
)
206 const char *name
= sym_name(s
);
208 /* if --all-symbols is not specified, then symbols outside the text
209 * and inittext sections are discarded */
212 * Symbols starting with __start and __stop are used to denote
213 * section boundaries, and should always be included:
215 if (string_starts_with(name
, "__start_") ||
216 string_starts_with(name
, "__stop_"))
219 if (symbol_in_range(s
, text_ranges
,
220 ARRAY_SIZE(text_ranges
)) == 0)
222 /* Corner case. Discard any symbols with the same value as
223 * _etext _einittext; they can move between pass 1 and 2 when
224 * the kallsyms data are added. If these symbols move then
225 * they may get dropped in pass 2, which breaks the kallsyms
228 if ((s
->addr
== text_range_text
->end
&&
229 strcmp(name
, text_range_text
->end_sym
)) ||
230 (s
->addr
== text_range_inittext
->end
&&
231 strcmp(name
, text_range_inittext
->end_sym
)))
238 /* remove all the invalid symbols from the table */
239 static void shrink_table(void)
244 for (i
= 0; i
< table_cnt
; i
++) {
245 if (symbol_valid(table
[i
])) {
247 table
[pos
] = table
[i
];
256 static void read_map(const char *in
)
259 struct sym_entry
*sym
;
270 sym
= read_symbol(fp
, &buf
, &buflen
);
274 sym
->seq
= table_cnt
;
276 if (table_cnt
>= table_size
) {
278 table
= xrealloc(table
, sizeof(*table
) * table_size
);
281 table
[table_cnt
++] = sym
;
288 static void output_label(const char *label
)
290 printf(".globl %s\n", label
);
292 printf("%s:\n", label
);
295 /* uncompress a compressed symbol. When this function is called, the best table
296 * might still be compressed itself, so the function needs to be recursive */
297 static int expand_symbol(const unsigned char *data
, int len
, char *result
)
299 int c
, rlen
, total
=0;
303 /* if the table holds a single char that is the same as the one
304 * we are looking for, then end the search */
305 if (best_table
[c
][0]==c
&& best_table_len
[c
]==1) {
309 /* if not, recurse and expand */
310 rlen
= expand_symbol(best_table
[c
], best_table_len
[c
], result
);
322 static bool symbol_absolute(const struct sym_entry
*s
)
324 return s
->percpu_absolute
;
327 static int compare_names(const void *a
, const void *b
)
330 const struct sym_entry
*sa
= *(const struct sym_entry
**)a
;
331 const struct sym_entry
*sb
= *(const struct sym_entry
**)b
;
333 ret
= strcmp(sym_name(sa
), sym_name(sb
));
335 if (sa
->addr
> sb
->addr
)
337 else if (sa
->addr
< sb
->addr
)
341 return (int)(sa
->seq
- sb
->seq
);
347 static void sort_symbols_by_name(void)
349 qsort(table
, table_cnt
, sizeof(table
[0]), compare_names
);
352 static void write_src(void)
354 unsigned int i
, k
, off
;
355 unsigned int best_idx
[256];
356 unsigned int *markers
, markers_cnt
;
357 char buf
[KSYM_NAME_LEN
];
359 printf("#include <asm/bitsperlong.h>\n");
360 printf("#if BITS_PER_LONG == 64\n");
361 printf("#define PTR .quad\n");
362 printf("#define ALGN .balign 8\n");
364 printf("#define PTR .long\n");
365 printf("#define ALGN .balign 4\n");
368 printf("\t.section .rodata, \"a\"\n");
370 output_label("kallsyms_num_syms");
371 printf("\t.long\t%u\n", table_cnt
);
374 /* table of offset markers, that give the offset in the compressed stream
375 * every 256 symbols */
376 markers_cnt
= (table_cnt
+ 255) / 256;
377 markers
= xmalloc(sizeof(*markers
) * markers_cnt
);
379 output_label("kallsyms_names");
381 for (i
= 0; i
< table_cnt
; i
++) {
383 markers
[i
>> 8] = off
;
386 /* There cannot be any symbol of length zero. */
387 if (table
[i
]->len
== 0) {
388 fprintf(stderr
, "kallsyms failure: "
389 "unexpected zero symbol length\n");
393 /* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
394 if (table
[i
]->len
> 0x3FFF) {
395 fprintf(stderr
, "kallsyms failure: "
396 "unexpected huge symbol length\n");
400 /* Encode length with ULEB128. */
401 if (table
[i
]->len
<= 0x7F) {
402 /* Most symbols use a single byte for the length. */
403 printf("\t.byte 0x%02x", table
[i
]->len
);
404 off
+= table
[i
]->len
+ 1;
406 /* "Big" symbols use two bytes. */
407 printf("\t.byte 0x%02x, 0x%02x",
408 (table
[i
]->len
& 0x7F) | 0x80,
409 (table
[i
]->len
>> 7) & 0x7F);
410 off
+= table
[i
]->len
+ 2;
412 for (k
= 0; k
< table
[i
]->len
; k
++)
413 printf(", 0x%02x", table
[i
]->sym
[k
]);
416 * Now that we wrote out the compressed symbol name, restore the
417 * original name and print it in the comment.
419 expand_symbol(table
[i
]->sym
, table
[i
]->len
, buf
);
420 strcpy((char *)table
[i
]->sym
, buf
);
421 printf("\t/* %s */\n", table
[i
]->sym
);
425 output_label("kallsyms_markers");
426 for (i
= 0; i
< markers_cnt
; i
++)
427 printf("\t.long\t%u\n", markers
[i
]);
432 output_label("kallsyms_token_table");
434 for (i
= 0; i
< 256; i
++) {
436 expand_symbol(best_table
[i
], best_table_len
[i
], buf
);
437 printf("\t.asciz\t\"%s\"\n", buf
);
438 off
+= strlen(buf
) + 1;
442 output_label("kallsyms_token_index");
443 for (i
= 0; i
< 256; i
++)
444 printf("\t.short\t%d\n", best_idx
[i
]);
447 output_label("kallsyms_offsets");
449 for (i
= 0; i
< table_cnt
; i
++) {
451 * Use the offset relative to the lowest value
452 * encountered of all relative symbols, and emit
453 * non-relocatable fixed offsets that will be fixed
460 if (!absolute_percpu
) {
461 offset
= table
[i
]->addr
- relative_base
;
462 overflow
= offset
< 0 || offset
> UINT_MAX
;
463 } else if (symbol_absolute(table
[i
])) {
464 offset
= table
[i
]->addr
;
465 overflow
= offset
< 0 || offset
> INT_MAX
;
467 offset
= relative_base
- table
[i
]->addr
- 1;
468 overflow
= offset
< INT_MIN
|| offset
>= 0;
471 fprintf(stderr
, "kallsyms failure: "
472 "%s symbol value %#llx out of range in relative mode\n",
473 symbol_absolute(table
[i
]) ? "absolute" : "relative",
477 printf("\t.long\t%#x\t/* %s */\n", (int)offset
, table
[i
]->sym
);
481 output_label("kallsyms_relative_base");
482 /* Provide proper symbols relocatability by their '_text' relativeness. */
483 if (_text
<= relative_base
)
484 printf("\tPTR\t_text + %#llx\n", relative_base
- _text
);
486 printf("\tPTR\t_text - %#llx\n", _text
- relative_base
);
489 sort_symbols_by_name();
490 output_label("kallsyms_seqs_of_names");
491 for (i
= 0; i
< table_cnt
; i
++)
492 printf("\t.byte 0x%02x, 0x%02x, 0x%02x\t/* %s */\n",
493 (unsigned char)(table
[i
]->seq
>> 16),
494 (unsigned char)(table
[i
]->seq
>> 8),
495 (unsigned char)(table
[i
]->seq
>> 0),
501 /* table lookup compression functions */
503 /* count all the possible tokens in a symbol */
504 static void learn_symbol(const unsigned char *symbol
, int len
)
508 for (i
= 0; i
< len
- 1; i
++)
509 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]++;
512 /* decrease the count for all the possible tokens in a symbol */
513 static void forget_symbol(const unsigned char *symbol
, int len
)
517 for (i
= 0; i
< len
- 1; i
++)
518 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]--;
521 /* do the initial token count */
522 static void build_initial_token_table(void)
526 for (i
= 0; i
< table_cnt
; i
++)
527 learn_symbol(table
[i
]->sym
, table
[i
]->len
);
530 static unsigned char *find_token(unsigned char *str
, int len
,
531 const unsigned char *token
)
535 for (i
= 0; i
< len
- 1; i
++) {
536 if (str
[i
] == token
[0] && str
[i
+1] == token
[1])
542 /* replace a given token in all the valid symbols. Use the sampled symbols
543 * to update the counts */
544 static void compress_symbols(const unsigned char *str
, int idx
)
546 unsigned int i
, len
, size
;
547 unsigned char *p1
, *p2
;
549 for (i
= 0; i
< table_cnt
; i
++) {
554 /* find the token on the symbol */
555 p2
= find_token(p1
, len
, str
);
558 /* decrease the counts for this symbol's tokens */
559 forget_symbol(table
[i
]->sym
, len
);
567 memmove(p2
, p2
+ 1, size
);
573 /* find the token on the symbol */
574 p2
= find_token(p1
, size
, str
);
580 /* increase the counts for this symbol's new tokens */
581 learn_symbol(table
[i
]->sym
, len
);
585 /* search the token with the maximum profit */
586 static int find_best_token(void)
588 int i
, best
, bestprofit
;
593 for (i
= 0; i
< 0x10000; i
++) {
594 if (token_profit
[i
] > bestprofit
) {
596 bestprofit
= token_profit
[i
];
602 /* this is the core of the algorithm: calculate the "best" table */
603 static void optimize_result(void)
607 /* using the '\0' symbol last allows compress_symbols to use standard
608 * fast string functions */
609 for (i
= 255; i
>= 0; i
--) {
611 /* if this table slot is empty (it is not used by an actual
612 * original char code */
613 if (!best_table_len
[i
]) {
615 /* find the token with the best profit value */
616 best
= find_best_token();
617 if (token_profit
[best
] == 0)
620 /* place it in the "best" table */
621 best_table_len
[i
] = 2;
622 best_table
[i
][0] = best
& 0xFF;
623 best_table
[i
][1] = (best
>> 8) & 0xFF;
625 /* replace this token in all the valid symbols */
626 compress_symbols(best_table
[i
], i
);
631 /* start by placing the symbols that are actually used on the table */
632 static void insert_real_symbols_in_table(void)
634 unsigned int i
, j
, c
;
636 for (i
= 0; i
< table_cnt
; i
++) {
637 for (j
= 0; j
< table
[i
]->len
; j
++) {
638 c
= table
[i
]->sym
[j
];
645 static void optimize_token_table(void)
647 build_initial_token_table();
649 insert_real_symbols_in_table();
654 /* guess for "linker script provide" symbol */
655 static int may_be_linker_script_provide_symbol(const struct sym_entry
*se
)
657 const char *symbol
= sym_name(se
);
658 int len
= se
->len
- 1;
663 if (symbol
[0] != '_' || symbol
[1] != '_')
667 if (!memcmp(symbol
+ 2, "start_", 6))
671 if (!memcmp(symbol
+ 2, "stop_", 5))
675 if (!memcmp(symbol
+ 2, "end_", 4))
679 if (!memcmp(symbol
+ len
- 6, "_start", 6))
683 if (!memcmp(symbol
+ len
- 4, "_end", 4))
689 static int compare_symbols(const void *a
, const void *b
)
691 const struct sym_entry
*sa
= *(const struct sym_entry
**)a
;
692 const struct sym_entry
*sb
= *(const struct sym_entry
**)b
;
695 /* sort by address first */
696 if (sa
->addr
> sb
->addr
)
698 if (sa
->addr
< sb
->addr
)
701 /* sort by "weakness" type */
702 wa
= (sa
->sym
[0] == 'w') || (sa
->sym
[0] == 'W');
703 wb
= (sb
->sym
[0] == 'w') || (sb
->sym
[0] == 'W');
707 /* sort by "linker script provide" type */
708 wa
= may_be_linker_script_provide_symbol(sa
);
709 wb
= may_be_linker_script_provide_symbol(sb
);
713 /* sort by the number of prefix underscores */
714 wa
= strspn(sym_name(sa
), "_");
715 wb
= strspn(sym_name(sb
), "_");
719 /* sort by initial order, so that other symbols are left undisturbed */
720 return sa
->seq
- sb
->seq
;
723 static void sort_symbols(void)
725 qsort(table
, table_cnt
, sizeof(table
[0]), compare_symbols
);
728 static void make_percpus_absolute(void)
732 for (i
= 0; i
< table_cnt
; i
++)
733 if (symbol_in_range(table
[i
], &percpu_range
, 1)) {
735 * Keep the 'A' override for percpu symbols to
736 * ensure consistent behavior compared to older
737 * versions of this tool.
739 table
[i
]->sym
[0] = 'A';
740 table
[i
]->percpu_absolute
= true;
744 /* find the minimum non-absolute symbol address */
745 static void record_relative_base(void)
749 for (i
= 0; i
< table_cnt
; i
++)
750 if (!symbol_absolute(table
[i
])) {
752 * The table is sorted by address.
753 * Take the first non-absolute symbol value.
755 relative_base
= table
[i
]->addr
;
760 int main(int argc
, char **argv
)
763 static const struct option long_options
[] = {
764 {"all-symbols", no_argument
, &all_symbols
, 1},
765 {"absolute-percpu", no_argument
, &absolute_percpu
, 1},
769 int c
= getopt_long(argc
, argv
, "", long_options
, NULL
);
780 read_map(argv
[optind
]);
783 make_percpus_absolute();
785 record_relative_base();
786 optimize_token_table();