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
32 #define KSYM_NAME_LEN 128
36 unsigned long long addr;
42 static struct sym_entry *table;
43 static unsigned int table_size, table_cnt;
44 static unsigned long long _text, _stext, _etext, _sinittext, _einittext;
45 static int all_symbols = 0;
46 static char symbol_prefix_char = '\0';
48 int token_profit[0x10000];
50 /* the table that holds the result of the compression */
51 unsigned char best_table[256][2];
52 unsigned char best_table_len[256];
55 static void usage(void)
57 fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
62 * This ignores the intensely annoying "mapping symbols" found
63 * in ARM ELF files: $a, $t and $d.
65 static inline int is_arm_mapping_symbol(const char *str)
67 return str[0] == '$' && strchr("atd", str[1])
68 && (str[2] == '\0' || str[2] == '.');
71 static int read_symbol(FILE *in, struct sym_entry *s)
77 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
87 /* skip prefix char */
88 if (symbol_prefix_char && str[0] == symbol_prefix_char)
91 /* Ignore most absolute/undefined (?) symbols. */
92 if (strcmp(sym, "_text") == 0)
94 else if (strcmp(sym, "_stext") == 0)
96 else if (strcmp(sym, "_etext") == 0)
98 else if (strcmp(sym, "_sinittext") == 0)
100 else if (strcmp(sym, "_einittext") == 0)
101 _einittext = s->addr;
102 else if (toupper(stype) == 'A')
104 /* Keep these useful absolute symbols */
105 if (strcmp(sym, "__kernel_syscall_via_break") &&
106 strcmp(sym, "__kernel_syscall_via_epc") &&
107 strcmp(sym, "__kernel_sigtramp") &&
112 else if (toupper(stype) == 'U' ||
113 is_arm_mapping_symbol(sym))
115 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
116 else if (str[0] == '$')
119 /* include the type field in the symbol name, so that it gets
120 * compressed together */
121 s->len = strlen(str) + 1;
122 s->sym = malloc(s->len + 1);
124 fprintf(stderr, "kallsyms failure: "
125 "unable to allocate required amount of memory\n");
128 strcpy((char *)s->sym + 1, str);
134 static int symbol_valid(struct sym_entry *s)
136 /* Symbols which vary between passes. Passes 1 and 2 must have
137 * identical symbol lists. The kallsyms_* symbols below are only added
138 * after pass 1, they would be included in pass 2 when --all-symbols is
139 * specified so exclude them to get a stable symbol list.
141 static char *special_symbols[] = {
142 "kallsyms_addresses",
146 "kallsyms_token_table",
147 "kallsyms_token_index",
149 /* Exclude linker generated symbols which vary between passes */
150 "_SDA_BASE_", /* ppc */
151 "_SDA2_BASE_", /* ppc */
156 /* skip prefix char */
157 if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
160 /* if --all-symbols is not specified, then symbols outside the text
161 * and inittext sections are discarded */
163 if ((s->addr < _stext || s->addr > _etext)
164 && (s->addr < _sinittext || s->addr > _einittext))
166 /* Corner case. Discard any symbols with the same value as
167 * _etext _einittext; they can move between pass 1 and 2 when
168 * the kallsyms data are added. If these symbols move then
169 * they may get dropped in pass 2, which breaks the kallsyms
172 if ((s->addr == _etext &&
173 strcmp((char *)s->sym + offset, "_etext")) ||
174 (s->addr == _einittext &&
175 strcmp((char *)s->sym + offset, "_einittext")))
179 /* Exclude symbols which vary between passes. */
180 if (strstr((char *)s->sym + offset, "_compiled."))
183 for (i = 0; special_symbols[i]; i++)
184 if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
190 static void read_map(FILE *in)
193 if (table_cnt >= table_size) {
195 table = realloc(table, sizeof(*table) * table_size);
197 fprintf(stderr, "out of memory\n");
201 if (read_symbol(in, &table[table_cnt]) == 0)
206 static void output_label(char *label)
208 if (symbol_prefix_char)
209 printf(".globl %c%s\n", symbol_prefix_char, label);
211 printf(".globl %s\n", label);
213 if (symbol_prefix_char)
214 printf("%c%s:\n", symbol_prefix_char, label);
216 printf("%s:\n", label);
219 /* uncompress a compressed symbol. When this function is called, the best table
220 * might still be compressed itself, so the function needs to be recursive */
221 static int expand_symbol(unsigned char *data, int len, char *result)
223 int c, rlen, total=0;
227 /* if the table holds a single char that is the same as the one
228 * we are looking for, then end the search */
229 if (best_table[c][0]==c && best_table_len[c]==1) {
233 /* if not, recurse and expand */
234 rlen = expand_symbol(best_table[c], best_table_len[c], result);
246 static void write_src(void)
248 unsigned int i, k, off;
249 unsigned int best_idx[256];
250 unsigned int *markers;
251 char buf[KSYM_NAME_LEN];
253 printf("#include <asm/types.h>\n");
254 printf("#if BITS_PER_LONG == 64\n");
255 printf("#define PTR .quad\n");
256 printf("#define ALGN .align 8\n");
258 printf("#define PTR .long\n");
259 printf("#define ALGN .align 4\n");
262 printf("\t.section .rodata, \"a\"\n");
264 /* Provide proper symbols relocatability by their '_text'
265 * relativeness. The symbol names cannot be used to construct
266 * normal symbol references as the list of symbols contains
267 * symbols that are declared static and are private to their
268 * .o files. This prevents .tmp_kallsyms.o or any other
269 * object from referencing them.
271 output_label("kallsyms_addresses");
272 for (i = 0; i < table_cnt; i++) {
273 if (toupper(table[i].sym[0]) != 'A') {
274 if (_text <= table[i].addr)
275 printf("\tPTR\t_text + %#llx\n",
276 table[i].addr - _text);
278 printf("\tPTR\t_text - %#llx\n",
279 _text - table[i].addr);
281 printf("\tPTR\t%#llx\n", table[i].addr);
286 output_label("kallsyms_num_syms");
287 printf("\tPTR\t%d\n", table_cnt);
290 /* table of offset markers, that give the offset in the compressed stream
291 * every 256 symbols */
292 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
294 fprintf(stderr, "kallsyms failure: "
295 "unable to allocate required memory\n");
299 output_label("kallsyms_names");
301 for (i = 0; i < table_cnt; i++) {
303 markers[i >> 8] = off;
305 printf("\t.byte 0x%02x", table[i].len);
306 for (k = 0; k < table[i].len; k++)
307 printf(", 0x%02x", table[i].sym[k]);
310 off += table[i].len + 1;
314 output_label("kallsyms_markers");
315 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
316 printf("\tPTR\t%d\n", markers[i]);
321 output_label("kallsyms_token_table");
323 for (i = 0; i < 256; i++) {
325 expand_symbol(best_table[i], best_table_len[i], buf);
326 printf("\t.asciz\t\"%s\"\n", buf);
327 off += strlen(buf) + 1;
331 output_label("kallsyms_token_index");
332 for (i = 0; i < 256; i++)
333 printf("\t.short\t%d\n", best_idx[i]);
338 /* table lookup compression functions */
340 /* count all the possible tokens in a symbol */
341 static void learn_symbol(unsigned char *symbol, int len)
345 for (i = 0; i < len - 1; i++)
346 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
349 /* decrease the count for all the possible tokens in a symbol */
350 static void forget_symbol(unsigned char *symbol, int len)
354 for (i = 0; i < len - 1; i++)
355 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
358 /* remove all the invalid symbols from the table and do the initial token count */
359 static void build_initial_tok_table(void)
364 for (i = 0; i < table_cnt; i++) {
365 if ( symbol_valid(&table[i]) ) {
367 table[pos] = table[i];
368 learn_symbol(table[pos].sym, table[pos].len);
375 static void *find_token(unsigned char *str, int len, unsigned char *token)
379 for (i = 0; i < len - 1; i++) {
380 if (str[i] == token[0] && str[i+1] == token[1])
386 /* replace a given token in all the valid symbols. Use the sampled symbols
387 * to update the counts */
388 static void compress_symbols(unsigned char *str, int idx)
390 unsigned int i, len, size;
391 unsigned char *p1, *p2;
393 for (i = 0; i < table_cnt; i++) {
398 /* find the token on the symbol */
399 p2 = find_token(p1, len, str);
402 /* decrease the counts for this symbol's tokens */
403 forget_symbol(table[i].sym, len);
411 memmove(p2, p2 + 1, size);
417 /* find the token on the symbol */
418 p2 = find_token(p1, size, str);
424 /* increase the counts for this symbol's new tokens */
425 learn_symbol(table[i].sym, len);
429 /* search the token with the maximum profit */
430 static int find_best_token(void)
432 int i, best, bestprofit;
437 for (i = 0; i < 0x10000; i++) {
438 if (token_profit[i] > bestprofit) {
440 bestprofit = token_profit[i];
446 /* this is the core of the algorithm: calculate the "best" table */
447 static void optimize_result(void)
451 /* using the '\0' symbol last allows compress_symbols to use standard
452 * fast string functions */
453 for (i = 255; i >= 0; i--) {
455 /* if this table slot is empty (it is not used by an actual
456 * original char code */
457 if (!best_table_len[i]) {
459 /* find the token with the breates profit value */
460 best = find_best_token();
462 /* place it in the "best" table */
463 best_table_len[i] = 2;
464 best_table[i][0] = best & 0xFF;
465 best_table[i][1] = (best >> 8) & 0xFF;
467 /* replace this token in all the valid symbols */
468 compress_symbols(best_table[i], i);
473 /* start by placing the symbols that are actually used on the table */
474 static void insert_real_symbols_in_table(void)
476 unsigned int i, j, c;
478 memset(best_table, 0, sizeof(best_table));
479 memset(best_table_len, 0, sizeof(best_table_len));
481 for (i = 0; i < table_cnt; i++) {
482 for (j = 0; j < table[i].len; j++) {
490 static void optimize_token_table(void)
492 build_initial_tok_table();
494 insert_real_symbols_in_table();
496 /* When valid symbol is not registered, exit to error */
498 fprintf(stderr, "No valid symbol.\n");
506 int main(int argc, char **argv)
510 for (i = 1; i < argc; i++) {
511 if(strcmp(argv[i], "--all-symbols") == 0)
513 else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
514 char *p = &argv[i][16];
516 if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
518 symbol_prefix_char = *p;
522 } else if (argc != 1)
526 optimize_token_table();