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
26 #define KSYM_NAME_LEN 128
29 unsigned long long addr
;
31 unsigned int start_pos
;
35 static struct sym_entry
*table
;
36 static unsigned int table_size
, table_cnt
;
37 static unsigned long long _text
, _stext
, _etext
, _sinittext
, _einittext
;
38 static int all_symbols
= 0;
39 static char symbol_prefix_char
= '\0';
41 int token_profit
[0x10000];
43 /* the table that holds the result of the compression */
44 unsigned char best_table
[256][2];
45 unsigned char best_table_len
[256];
48 static void usage(void)
50 fprintf(stderr
, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
55 * This ignores the intensely annoying "mapping symbols" found
56 * in ARM ELF files: $a, $t and $d.
58 static inline int is_arm_mapping_symbol(const char *str
)
60 return str
[0] == '$' && strchr("atd", str
[1])
61 && (str
[2] == '\0' || str
[2] == '.');
64 static int read_symbol(FILE *in
, struct sym_entry
*s
)
70 rc
= fscanf(in
, "%llx %c %499s\n", &s
->addr
, &stype
, str
);
80 /* skip prefix char */
81 if (symbol_prefix_char
&& str
[0] == symbol_prefix_char
)
84 /* Ignore most absolute/undefined (?) symbols. */
85 if (strcmp(sym
, "_text") == 0)
87 else if (strcmp(sym
, "_stext") == 0)
89 else if (strcmp(sym
, "_etext") == 0)
91 else if (strcmp(sym
, "_sinittext") == 0)
93 else if (strcmp(sym
, "_einittext") == 0)
95 else if (toupper(stype
) == 'A')
97 /* Keep these useful absolute symbols */
98 if (strcmp(sym
, "__kernel_syscall_via_break") &&
99 strcmp(sym
, "__kernel_syscall_via_epc") &&
100 strcmp(sym
, "__kernel_sigtramp") &&
105 else if (toupper(stype
) == 'U' ||
106 is_arm_mapping_symbol(sym
))
108 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
109 else if (str
[0] == '$')
111 /* exclude debugging symbols */
112 else if (stype
== 'N')
115 /* include the type field in the symbol name, so that it gets
116 * compressed together */
117 s
->len
= strlen(str
) + 1;
118 s
->sym
= malloc(s
->len
+ 1);
120 fprintf(stderr
, "kallsyms failure: "
121 "unable to allocate required amount of memory\n");
124 strcpy((char *)s
->sym
+ 1, str
);
130 static int symbol_valid(struct sym_entry
*s
)
132 /* Symbols which vary between passes. Passes 1 and 2 must have
133 * identical symbol lists.
135 static char *special_symbols
[] = {
136 /* Exclude linker generated symbols which vary between passes */
137 "_SDA_BASE_", /* ppc */
138 "_SDA2_BASE_", /* ppc */
143 /* skip prefix char */
144 if (symbol_prefix_char
&& *(s
->sym
+ 1) == symbol_prefix_char
)
147 /* if --all-symbols is not specified, then symbols outside the text
148 * and inittext sections are discarded */
150 if ((s
->addr
< _stext
|| s
->addr
> _etext
)
151 && (s
->addr
< _sinittext
|| s
->addr
> _einittext
))
153 /* Corner case. Discard any symbols with the same value as
154 * _etext _einittext; they can move between pass 1 and 2 when
155 * the kallsyms data are added. If these symbols move then
156 * they may get dropped in pass 2, which breaks the kallsyms
159 if ((s
->addr
== _etext
&&
160 strcmp((char *)s
->sym
+ offset
, "_etext")) ||
161 (s
->addr
== _einittext
&&
162 strcmp((char *)s
->sym
+ offset
, "_einittext")))
166 /* Exclude symbols which vary between passes. */
167 if (strstr((char *)s
->sym
+ offset
, "_compiled.") ||
168 strncmp((char*)s
->sym
+ offset
, "__compound_literal.", 19) == 0 ||
169 strncmp((char*)s
->sym
+ offset
, "__compound_literal$", 19) == 0)
172 for (i
= 0; special_symbols
[i
]; i
++)
173 if( strcmp((char *)s
->sym
+ offset
, special_symbols
[i
]) == 0 )
179 static void read_map(FILE *in
)
182 if (table_cnt
>= table_size
) {
184 table
= realloc(table
, sizeof(*table
) * table_size
);
186 fprintf(stderr
, "out of memory\n");
190 if (read_symbol(in
, &table
[table_cnt
]) == 0) {
191 table
[table_cnt
].start_pos
= table_cnt
;
197 static void output_label(char *label
)
199 if (symbol_prefix_char
)
200 printf(".globl %c%s\n", symbol_prefix_char
, label
);
202 printf(".globl %s\n", label
);
204 if (symbol_prefix_char
)
205 printf("%c%s:\n", symbol_prefix_char
, label
);
207 printf("%s:\n", label
);
210 /* uncompress a compressed symbol. When this function is called, the best table
211 * might still be compressed itself, so the function needs to be recursive */
212 static int expand_symbol(unsigned char *data
, int len
, char *result
)
214 int c
, rlen
, total
=0;
218 /* if the table holds a single char that is the same as the one
219 * we are looking for, then end the search */
220 if (best_table
[c
][0]==c
&& best_table_len
[c
]==1) {
224 /* if not, recurse and expand */
225 rlen
= expand_symbol(best_table
[c
], best_table_len
[c
], result
);
237 static void write_src(void)
239 unsigned int i
, k
, off
;
240 unsigned int best_idx
[256];
241 unsigned int *markers
;
242 char buf
[KSYM_NAME_LEN
];
244 printf("#include <asm/types.h>\n");
245 printf("#if BITS_PER_LONG == 64\n");
246 printf("#define PTR .quad\n");
247 printf("#define ALGN .align 8\n");
249 printf("#define PTR .long\n");
250 printf("#define ALGN .align 4\n");
253 printf("\t.section .rodata, \"a\"\n");
255 /* Provide proper symbols relocatability by their '_text'
256 * relativeness. The symbol names cannot be used to construct
257 * normal symbol references as the list of symbols contains
258 * symbols that are declared static and are private to their
259 * .o files. This prevents .tmp_kallsyms.o or any other
260 * object from referencing them.
262 output_label("kallsyms_addresses");
263 for (i
= 0; i
< table_cnt
; i
++) {
264 if (toupper(table
[i
].sym
[0]) != 'A') {
265 if (_text
<= table
[i
].addr
)
266 printf("\tPTR\t_text + %#llx\n",
267 table
[i
].addr
- _text
);
269 printf("\tPTR\t_text - %#llx\n",
270 _text
- table
[i
].addr
);
272 printf("\tPTR\t%#llx\n", table
[i
].addr
);
277 output_label("kallsyms_num_syms");
278 printf("\tPTR\t%d\n", table_cnt
);
281 /* table of offset markers, that give the offset in the compressed stream
282 * every 256 symbols */
283 markers
= malloc(sizeof(unsigned int) * ((table_cnt
+ 255) / 256));
285 fprintf(stderr
, "kallsyms failure: "
286 "unable to allocate required memory\n");
290 output_label("kallsyms_names");
292 for (i
= 0; i
< table_cnt
; i
++) {
294 markers
[i
>> 8] = off
;
296 printf("\t.byte 0x%02x", table
[i
].len
);
297 for (k
= 0; k
< table
[i
].len
; k
++)
298 printf(", 0x%02x", table
[i
].sym
[k
]);
301 off
+= table
[i
].len
+ 1;
305 output_label("kallsyms_markers");
306 for (i
= 0; i
< ((table_cnt
+ 255) >> 8); i
++)
307 printf("\tPTR\t%d\n", markers
[i
]);
312 output_label("kallsyms_token_table");
314 for (i
= 0; i
< 256; i
++) {
316 expand_symbol(best_table
[i
], best_table_len
[i
], buf
);
317 printf("\t.asciz\t\"%s\"\n", buf
);
318 off
+= strlen(buf
) + 1;
322 output_label("kallsyms_token_index");
323 for (i
= 0; i
< 256; i
++)
324 printf("\t.short\t%d\n", best_idx
[i
]);
329 /* table lookup compression functions */
331 /* count all the possible tokens in a symbol */
332 static void learn_symbol(unsigned char *symbol
, int len
)
336 for (i
= 0; i
< len
- 1; i
++)
337 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]++;
340 /* decrease the count for all the possible tokens in a symbol */
341 static void forget_symbol(unsigned char *symbol
, int len
)
345 for (i
= 0; i
< len
- 1; i
++)
346 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]--;
349 /* remove all the invalid symbols from the table and do the initial token count */
350 static void build_initial_tok_table(void)
355 for (i
= 0; i
< table_cnt
; i
++) {
356 if ( symbol_valid(&table
[i
]) ) {
358 table
[pos
] = table
[i
];
359 learn_symbol(table
[pos
].sym
, table
[pos
].len
);
366 static void *find_token(unsigned char *str
, int len
, unsigned char *token
)
370 for (i
= 0; i
< len
- 1; i
++) {
371 if (str
[i
] == token
[0] && str
[i
+1] == token
[1])
377 /* replace a given token in all the valid symbols. Use the sampled symbols
378 * to update the counts */
379 static void compress_symbols(unsigned char *str
, int idx
)
381 unsigned int i
, len
, size
;
382 unsigned char *p1
, *p2
;
384 for (i
= 0; i
< table_cnt
; i
++) {
389 /* find the token on the symbol */
390 p2
= find_token(p1
, len
, str
);
393 /* decrease the counts for this symbol's tokens */
394 forget_symbol(table
[i
].sym
, len
);
402 memmove(p2
, p2
+ 1, size
);
408 /* find the token on the symbol */
409 p2
= find_token(p1
, size
, str
);
415 /* increase the counts for this symbol's new tokens */
416 learn_symbol(table
[i
].sym
, len
);
420 /* search the token with the maximum profit */
421 static int find_best_token(void)
423 int i
, best
, bestprofit
;
428 for (i
= 0; i
< 0x10000; i
++) {
429 if (token_profit
[i
] > bestprofit
) {
431 bestprofit
= token_profit
[i
];
437 /* this is the core of the algorithm: calculate the "best" table */
438 static void optimize_result(void)
442 /* using the '\0' symbol last allows compress_symbols to use standard
443 * fast string functions */
444 for (i
= 255; i
>= 0; i
--) {
446 /* if this table slot is empty (it is not used by an actual
447 * original char code */
448 if (!best_table_len
[i
]) {
450 /* find the token with the breates profit value */
451 best
= find_best_token();
453 /* place it in the "best" table */
454 best_table_len
[i
] = 2;
455 best_table
[i
][0] = best
& 0xFF;
456 best_table
[i
][1] = (best
>> 8) & 0xFF;
458 /* replace this token in all the valid symbols */
459 compress_symbols(best_table
[i
], i
);
464 /* start by placing the symbols that are actually used on the table */
465 static void insert_real_symbols_in_table(void)
467 unsigned int i
, j
, c
;
469 memset(best_table
, 0, sizeof(best_table
));
470 memset(best_table_len
, 0, sizeof(best_table_len
));
472 for (i
= 0; i
< table_cnt
; i
++) {
473 for (j
= 0; j
< table
[i
].len
; j
++) {
481 static void optimize_token_table(void)
483 build_initial_tok_table();
485 insert_real_symbols_in_table();
487 /* When valid symbol is not registered, exit to error */
489 fprintf(stderr
, "No valid symbol.\n");
496 static int compare_symbols(const void *a
, const void *b
)
498 const struct sym_entry
*sa
;
499 const struct sym_entry
*sb
;
505 /* sort by address first */
506 if (sa
->addr
> sb
->addr
)
508 if (sa
->addr
< sb
->addr
)
511 /* sort by "weakness" type */
512 wa
= (sa
->sym
[0] == 'w') || (sa
->sym
[0] == 'W');
513 wb
= (sb
->sym
[0] == 'w') || (sb
->sym
[0] == 'W');
517 /* sort by initial order, so that other symbols are left undisturbed */
518 return sa
->start_pos
- sb
->start_pos
;
521 static void sort_symbols(void)
523 qsort(table
, table_cnt
, sizeof(struct sym_entry
), compare_symbols
);
526 int main(int argc
, char **argv
)
530 for (i
= 1; i
< argc
; i
++) {
531 if(strcmp(argv
[i
], "--all-symbols") == 0)
533 else if (strncmp(argv
[i
], "--symbol-prefix=", 16) == 0) {
534 char *p
= &argv
[i
][16];
536 if ((*p
== '"' && *(p
+2) == '"') || (*p
== '\'' && *(p
+2) == '\''))
538 symbol_prefix_char
= *p
;
542 } else if (argc
!= 1)
548 optimize_token_table();