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 127
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, _sextratext, _eextratext;
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 (strcmp(sym, "_sextratext") == 0)
105 _sextratext = s->addr;
106 else if (strcmp(sym, "_eextratext") == 0)
107 _eextratext = s->addr;
108 else if (toupper(stype) == 'A')
110 /* Keep these useful absolute symbols */
111 if (strcmp(sym, "__kernel_syscall_via_break") &&
112 strcmp(sym, "__kernel_syscall_via_epc") &&
113 strcmp(sym, "__kernel_sigtramp") &&
118 else if (toupper(stype) == 'U' ||
119 is_arm_mapping_symbol(sym))
121 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
122 else if (str[0] == '$')
125 /* include the type field in the symbol name, so that it gets
126 * compressed together */
127 s->len = strlen(str) + 1;
128 s->sym = malloc(s->len + 1);
130 fprintf(stderr, "kallsyms failure: "
131 "unable to allocate required amount of memory\n");
134 strcpy((char *)s->sym + 1, str);
140 static int symbol_valid(struct sym_entry *s)
142 /* Symbols which vary between passes. Passes 1 and 2 must have
143 * identical symbol lists. The kallsyms_* symbols below are only added
144 * after pass 1, they would be included in pass 2 when --all-symbols is
145 * specified so exclude them to get a stable symbol list.
147 static char *special_symbols[] = {
148 "kallsyms_addresses",
152 "kallsyms_token_table",
153 "kallsyms_token_index",
155 /* Exclude linker generated symbols which vary between passes */
156 "_SDA_BASE_", /* ppc */
157 "_SDA2_BASE_", /* ppc */
162 /* skip prefix char */
163 if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
166 /* if --all-symbols is not specified, then symbols outside the text
167 * and inittext sections are discarded */
169 if ((s->addr < _stext || s->addr > _etext)
170 && (s->addr < _sinittext || s->addr > _einittext)
171 && (s->addr < _sextratext || s->addr > _eextratext))
173 /* Corner case. Discard any symbols with the same value as
174 * _etext _einittext or _eextratext; they can move between pass
175 * 1 and 2 when the kallsyms data are added. If these symbols
176 * move then they may get dropped in pass 2, which breaks the
179 if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) ||
180 (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) ||
181 (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext")))
185 /* Exclude symbols which vary between passes. */
186 if (strstr((char *)s->sym + offset, "_compiled."))
189 for (i = 0; special_symbols[i]; i++)
190 if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
196 static void read_map(FILE *in)
199 if (table_cnt >= table_size) {
201 table = realloc(table, sizeof(*table) * table_size);
203 fprintf(stderr, "out of memory\n");
207 if (read_symbol(in, &table[table_cnt]) == 0)
212 static void output_label(char *label)
214 if (symbol_prefix_char)
215 printf(".globl %c%s\n", symbol_prefix_char, label);
217 printf(".globl %s\n", label);
219 if (symbol_prefix_char)
220 printf("%c%s:\n", symbol_prefix_char, label);
222 printf("%s:\n", label);
225 /* uncompress a compressed symbol. When this function is called, the best table
226 * might still be compressed itself, so the function needs to be recursive */
227 static int expand_symbol(unsigned char *data, int len, char *result)
229 int c, rlen, total=0;
233 /* if the table holds a single char that is the same as the one
234 * we are looking for, then end the search */
235 if (best_table[c][0]==c && best_table_len[c]==1) {
239 /* if not, recurse and expand */
240 rlen = expand_symbol(best_table[c], best_table_len[c], result);
252 static void write_src(void)
254 unsigned int i, k, off;
255 unsigned int best_idx[256];
256 unsigned int *markers;
257 char buf[KSYM_NAME_LEN+1];
259 printf("#include <asm/types.h>\n");
260 printf("#if BITS_PER_LONG == 64\n");
261 printf("#define PTR .quad\n");
262 printf("#define ALGN .align 8\n");
264 printf("#define PTR .long\n");
265 printf("#define ALGN .align 4\n");
268 printf("\t.section .rodata, \"a\"\n");
270 /* Provide proper symbols relocatability by their '_text'
271 * relativeness. The symbol names cannot be used to construct
272 * normal symbol references as the list of symbols contains
273 * symbols that are declared static and are private to their
274 * .o files. This prevents .tmp_kallsyms.o or any other
275 * object from referencing them.
277 output_label("kallsyms_addresses");
278 for (i = 0; i < table_cnt; i++) {
279 if (toupper(table[i].sym[0]) != 'A') {
280 if (_text <= table[i].addr)
281 printf("\tPTR\t_text + %#llx\n",
282 table[i].addr - _text);
284 printf("\tPTR\t_text - %#llx\n",
285 _text - table[i].addr);
287 printf("\tPTR\t%#llx\n", table[i].addr);
292 output_label("kallsyms_num_syms");
293 printf("\tPTR\t%d\n", table_cnt);
296 /* table of offset markers, that give the offset in the compressed stream
297 * every 256 symbols */
298 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
300 fprintf(stderr, "kallsyms failure: "
301 "unable to allocate required memory\n");
305 output_label("kallsyms_names");
307 for (i = 0; i < table_cnt; i++) {
309 markers[i >> 8] = off;
311 printf("\t.byte 0x%02x", table[i].len);
312 for (k = 0; k < table[i].len; k++)
313 printf(", 0x%02x", table[i].sym[k]);
316 off += table[i].len + 1;
320 output_label("kallsyms_markers");
321 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
322 printf("\tPTR\t%d\n", markers[i]);
327 output_label("kallsyms_token_table");
329 for (i = 0; i < 256; i++) {
331 expand_symbol(best_table[i], best_table_len[i], buf);
332 printf("\t.asciz\t\"%s\"\n", buf);
333 off += strlen(buf) + 1;
337 output_label("kallsyms_token_index");
338 for (i = 0; i < 256; i++)
339 printf("\t.short\t%d\n", best_idx[i]);
344 /* table lookup compression functions */
346 /* count all the possible tokens in a symbol */
347 static void learn_symbol(unsigned char *symbol, int len)
351 for (i = 0; i < len - 1; i++)
352 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
355 /* decrease the count for all the possible tokens in a symbol */
356 static void forget_symbol(unsigned char *symbol, int len)
360 for (i = 0; i < len - 1; i++)
361 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
364 /* remove all the invalid symbols from the table and do the initial token count */
365 static void build_initial_tok_table(void)
370 for (i = 0; i < table_cnt; i++) {
371 if ( symbol_valid(&table[i]) ) {
373 table[pos] = table[i];
374 learn_symbol(table[pos].sym, table[pos].len);
381 /* replace a given token in all the valid symbols. Use the sampled symbols
382 * to update the counts */
383 static void compress_symbols(unsigned char *str, int idx)
385 unsigned int i, len, size;
386 unsigned char *p1, *p2;
388 for (i = 0; i < table_cnt; i++) {
393 /* find the token on the symbol */
394 p2 = memmem(p1, len, str, 2);
397 /* decrease the counts for this symbol's tokens */
398 forget_symbol(table[i].sym, len);
406 memmove(p2, p2 + 1, size);
412 /* find the token on the symbol */
413 p2 = memmem(p1, size, str, 2);
419 /* increase the counts for this symbol's new tokens */
420 learn_symbol(table[i].sym, len);
424 /* search the token with the maximum profit */
425 static int find_best_token(void)
427 int i, best, bestprofit;
432 for (i = 0; i < 0x10000; i++) {
433 if (token_profit[i] > bestprofit) {
435 bestprofit = token_profit[i];
441 /* this is the core of the algorithm: calculate the "best" table */
442 static void optimize_result(void)
446 /* using the '\0' symbol last allows compress_symbols to use standard
447 * fast string functions */
448 for (i = 255; i >= 0; i--) {
450 /* if this table slot is empty (it is not used by an actual
451 * original char code */
452 if (!best_table_len[i]) {
454 /* find the token with the breates profit value */
455 best = find_best_token();
457 /* place it in the "best" table */
458 best_table_len[i] = 2;
459 best_table[i][0] = best & 0xFF;
460 best_table[i][1] = (best >> 8) & 0xFF;
462 /* replace this token in all the valid symbols */
463 compress_symbols(best_table[i], i);
468 /* start by placing the symbols that are actually used on the table */
469 static void insert_real_symbols_in_table(void)
471 unsigned int i, j, c;
473 memset(best_table, 0, sizeof(best_table));
474 memset(best_table_len, 0, sizeof(best_table_len));
476 for (i = 0; i < table_cnt; i++) {
477 for (j = 0; j < table[i].len; j++) {
485 static void optimize_token_table(void)
487 build_initial_tok_table();
489 insert_real_symbols_in_table();
491 /* When valid symbol is not registered, exit to error */
493 fprintf(stderr, "No valid symbol.\n");
501 int main(int argc, char **argv)
505 for (i = 1; i < argc; i++) {
506 if(strcmp(argv[i], "--all-symbols") == 0)
508 else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
509 char *p = &argv[i][16];
511 if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
513 symbol_prefix_char = *p;
517 } else if (argc != 1)
521 optimize_token_table();