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
12 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
13 * Changed the compression method from stem compression to "table lookup"
16 * Table compression uses all the unused char codes on the symbols and
17 * maps these to the most used substrings (tokens). For instance, it might
18 * map char code 0xF7 to represent "write_" and then in every symbol where
19 * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
20 * The used codes themselves are also placed in the table so that the
21 * decompresion can work without "special cases".
22 * Applied to kernel symbols, this usually produces a compression ratio
34 #define KSYM_NAME_LEN 128
38 unsigned long long addr
;
44 static struct sym_entry
*table
;
45 static unsigned int table_size
, table_cnt
;
46 static unsigned long long _text
, _stext
, _etext
, _sinittext
, _einittext
;
47 static int all_symbols
= 0;
48 static char symbol_prefix_char
= '\0';
50 int token_profit
[0x10000];
52 /* the table that holds the result of the compression */
53 unsigned char best_table
[256][2];
54 unsigned char best_table_len
[256];
57 static void usage(void)
59 fprintf(stderr
, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
64 * This ignores the intensely annoying "mapping symbols" found
65 * in ARM ELF files: $a, $t and $d.
67 static inline int is_arm_mapping_symbol(const char *str
)
69 return str
[0] == '$' && strchr("atd", str
[1])
70 && (str
[2] == '\0' || str
[2] == '.');
73 static int read_symbol(FILE *in
, struct sym_entry
*s
)
79 rc
= fscanf(in
, "%llx %c %499s\n", &s
->addr
, &stype
, str
);
89 /* skip prefix char */
90 if (symbol_prefix_char
&& str
[0] == symbol_prefix_char
)
93 /* Ignore most absolute/undefined (?) symbols. */
94 if (strcmp(sym
, "_text") == 0)
96 else if (strcmp(sym
, "_stext") == 0)
98 else if (strcmp(sym
, "_etext") == 0)
100 else if (strcmp(sym
, "_sinittext") == 0)
101 _sinittext
= s
->addr
;
102 else if (strcmp(sym
, "_einittext") == 0)
103 _einittext
= s
->addr
;
104 else if (toupper(stype
) == 'A')
106 /* Keep these useful absolute symbols */
107 if (strcmp(sym
, "__kernel_syscall_via_break") &&
108 strcmp(sym
, "__kernel_syscall_via_epc") &&
109 strcmp(sym
, "__kernel_sigtramp") &&
114 else if (toupper(stype
) == 'U' ||
115 is_arm_mapping_symbol(sym
))
117 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
118 else if (str
[0] == '$')
121 /* include the type field in the symbol name, so that it gets
122 * compressed together */
123 s
->len
= strlen(str
) + 1;
124 s
->sym
= malloc(s
->len
+ 1);
126 fprintf(stderr
, "kallsyms failure: "
127 "unable to allocate required amount of memory\n");
130 strcpy((char *)s
->sym
+ 1, str
);
136 static int symbol_valid(struct sym_entry
*s
)
138 /* Symbols which vary between passes. Passes 1 and 2 must have
139 * identical symbol lists. The kallsyms_* symbols below are only added
140 * after pass 1, they would be included in pass 2 when --all-symbols is
141 * specified so exclude them to get a stable symbol list.
143 static char *special_symbols
[] = {
144 "kallsyms_addresses",
148 "kallsyms_token_table",
149 "kallsyms_token_index",
151 /* Exclude linker generated symbols which vary between passes */
152 "_SDA_BASE_", /* ppc */
153 "_SDA2_BASE_", /* ppc */
158 /* skip prefix char */
159 if (symbol_prefix_char
&& *(s
->sym
+ 1) == symbol_prefix_char
)
162 /* if --all-symbols is not specified, then symbols outside the text
163 * and inittext sections are discarded */
165 if ((s
->addr
< _stext
|| s
->addr
> _etext
)
166 && (s
->addr
< _sinittext
|| s
->addr
> _einittext
))
168 /* Corner case. Discard any symbols with the same value as
169 * _etext _einittext; they can move between pass 1 and 2 when
170 * the kallsyms data are added. If these symbols move then
171 * they may get dropped in pass 2, which breaks the kallsyms
174 if ((s
->addr
== _etext
&&
175 strcmp((char *)s
->sym
+ offset
, "_etext")) ||
176 (s
->addr
== _einittext
&&
177 strcmp((char *)s
->sym
+ offset
, "_einittext")))
181 /* Exclude symbols which vary between passes. */
182 if (strstr((char *)s
->sym
+ offset
, "_compiled."))
185 for (i
= 0; special_symbols
[i
]; i
++)
186 if( strcmp((char *)s
->sym
+ offset
, special_symbols
[i
]) == 0 )
192 static void read_map(FILE *in
)
195 if (table_cnt
>= table_size
) {
197 table
= realloc(table
, sizeof(*table
) * table_size
);
199 fprintf(stderr
, "out of memory\n");
203 if (read_symbol(in
, &table
[table_cnt
]) == 0)
208 static void output_label(char *label
)
210 if (symbol_prefix_char
)
211 printf(".globl %c%s\n", symbol_prefix_char
, label
);
213 printf(".globl %s\n", label
);
215 if (symbol_prefix_char
)
216 printf("%c%s:\n", symbol_prefix_char
, label
);
218 printf("%s:\n", label
);
221 /* uncompress a compressed symbol. When this function is called, the best table
222 * might still be compressed itself, so the function needs to be recursive */
223 static int expand_symbol(unsigned char *data
, int len
, char *result
)
225 int c
, rlen
, total
=0;
229 /* if the table holds a single char that is the same as the one
230 * we are looking for, then end the search */
231 if (best_table
[c
][0]==c
&& best_table_len
[c
]==1) {
235 /* if not, recurse and expand */
236 rlen
= expand_symbol(best_table
[c
], best_table_len
[c
], result
);
248 static void write_src(void)
250 unsigned int i
, k
, off
;
251 unsigned int best_idx
[256];
252 unsigned int *markers
;
253 char buf
[KSYM_NAME_LEN
];
255 printf("#include <asm/types.h>\n");
256 printf("#if BITS_PER_LONG == 64\n");
257 printf("#define PTR .quad\n");
258 printf("#define ALGN .align 8\n");
260 printf("#define PTR .long\n");
261 printf("#define ALGN .align 4\n");
264 printf("\t.section .rodata, \"a\"\n");
266 /* Provide proper symbols relocatability by their '_text'
267 * relativeness. The symbol names cannot be used to construct
268 * normal symbol references as the list of symbols contains
269 * symbols that are declared static and are private to their
270 * .o files. This prevents .tmp_kallsyms.o or any other
271 * object from referencing them.
273 output_label("kallsyms_addresses");
274 for (i
= 0; i
< table_cnt
; i
++) {
275 if (toupper(table
[i
].sym
[0]) != 'A') {
276 if (_text
<= table
[i
].addr
)
277 printf("\tPTR\t_text + %#llx\n",
278 table
[i
].addr
- _text
);
280 printf("\tPTR\t_text - %#llx\n",
281 _text
- table
[i
].addr
);
283 printf("\tPTR\t%#llx\n", table
[i
].addr
);
288 output_label("kallsyms_num_syms");
289 printf("\tPTR\t%d\n", table_cnt
);
292 /* table of offset markers, that give the offset in the compressed stream
293 * every 256 symbols */
294 markers
= malloc(sizeof(unsigned int) * ((table_cnt
+ 255) / 256));
296 fprintf(stderr
, "kallsyms failure: "
297 "unable to allocate required memory\n");
301 output_label("kallsyms_names");
303 for (i
= 0; i
< table_cnt
; i
++) {
305 markers
[i
>> 8] = off
;
307 printf("\t.byte 0x%02x", table
[i
].len
);
308 for (k
= 0; k
< table
[i
].len
; k
++)
309 printf(", 0x%02x", table
[i
].sym
[k
]);
312 off
+= table
[i
].len
+ 1;
316 output_label("kallsyms_markers");
317 for (i
= 0; i
< ((table_cnt
+ 255) >> 8); i
++)
318 printf("\tPTR\t%d\n", markers
[i
]);
323 output_label("kallsyms_token_table");
325 for (i
= 0; i
< 256; i
++) {
327 expand_symbol(best_table
[i
], best_table_len
[i
], buf
);
328 printf("\t.asciz\t\"%s\"\n", buf
);
329 off
+= strlen(buf
) + 1;
333 output_label("kallsyms_token_index");
334 for (i
= 0; i
< 256; i
++)
335 printf("\t.short\t%d\n", best_idx
[i
]);
340 /* table lookup compression functions */
342 /* count all the possible tokens in a symbol */
343 static void learn_symbol(unsigned char *symbol
, int len
)
347 for (i
= 0; i
< len
- 1; i
++)
348 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]++;
351 /* decrease the count for all the possible tokens in a symbol */
352 static void forget_symbol(unsigned char *symbol
, int len
)
356 for (i
= 0; i
< len
- 1; i
++)
357 token_profit
[ symbol
[i
] + (symbol
[i
+ 1] << 8) ]--;
360 /* remove all the invalid symbols from the table and do the initial token count */
361 static void build_initial_tok_table(void)
366 for (i
= 0; i
< table_cnt
; i
++) {
367 if ( symbol_valid(&table
[i
]) ) {
369 table
[pos
] = table
[i
];
370 learn_symbol(table
[pos
].sym
, table
[pos
].len
);
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
= memmem(p1
, len
, str
, 2);
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
= memmem(p1
, size
, str
, 2);
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");
497 int main(int argc
, char **argv
)
501 for (i
= 1; i
< argc
; i
++) {
502 if(strcmp(argv
[i
], "--all-symbols") == 0)
504 else if (strncmp(argv
[i
], "--symbol-prefix=", 16) == 0) {
505 char *p
= &argv
[i
][16];
507 if ((*p
== '"' && *(p
+2) == '"') || (*p
== '\'' && *(p
+2) == '\''))
509 symbol_prefix_char
= *p
;
513 } else if (argc
!= 1)
517 optimize_token_table();