11da177e4SLinus Torvalds /* Generate assembler source containing symbol information 21da177e4SLinus Torvalds * 31da177e4SLinus Torvalds * Copyright 2002 by Kai Germaschewski 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This software may be used and distributed according to the terms 61da177e4SLinus Torvalds * of the GNU General Public License, incorporated herein by reference. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * Table compression uses all the unused char codes on the symbols and 111da177e4SLinus Torvalds * maps these to the most used substrings (tokens). For instance, it might 121da177e4SLinus Torvalds * map char code 0xF7 to represent "write_" and then in every symbol where 131da177e4SLinus Torvalds * "write_" appears it can be replaced by 0xF7, saving 5 bytes. 141da177e4SLinus Torvalds * The used codes themselves are also placed in the table so that the 151da177e4SLinus Torvalds * decompresion can work without "special cases". 161da177e4SLinus Torvalds * Applied to kernel symbols, this usually produces a compression ratio 171da177e4SLinus Torvalds * of about 50%. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <stdio.h> 221da177e4SLinus Torvalds #include <stdlib.h> 231da177e4SLinus Torvalds #include <string.h> 241da177e4SLinus Torvalds #include <ctype.h> 251da177e4SLinus Torvalds 2617b1f0deSMike Frysinger #ifndef ARRAY_SIZE 2717b1f0deSMike Frysinger #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 2817b1f0deSMike Frysinger #endif 2917b1f0deSMike Frysinger 309281aceaSTejun Heo #define KSYM_NAME_LEN 128 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds struct sym_entry { 331da177e4SLinus Torvalds unsigned long long addr; 34b3dbb4ecSPaulo Marques unsigned int len; 35f2df3f65SPaulo Marques unsigned int start_pos; 361da177e4SLinus Torvalds unsigned char *sym; 371da177e4SLinus Torvalds }; 381da177e4SLinus Torvalds 3917b1f0deSMike Frysinger struct text_range { 4017b1f0deSMike Frysinger const char *stext, *etext; 4117b1f0deSMike Frysinger unsigned long long start, end; 4217b1f0deSMike Frysinger }; 4317b1f0deSMike Frysinger 4417b1f0deSMike Frysinger static unsigned long long _text; 4517b1f0deSMike Frysinger static struct text_range text_ranges[] = { 4617b1f0deSMike Frysinger { "_stext", "_etext" }, 4717b1f0deSMike Frysinger { "_sinittext", "_einittext" }, 4817b1f0deSMike Frysinger { "_stext_l1", "_etext_l1" }, /* Blackfin on-chip L1 inst SRAM */ 4917b1f0deSMike Frysinger { "_stext_l2", "_etext_l2" }, /* Blackfin on-chip L2 SRAM */ 5017b1f0deSMike Frysinger }; 5117b1f0deSMike Frysinger #define text_range_text (&text_ranges[0]) 5217b1f0deSMike Frysinger #define text_range_inittext (&text_ranges[1]) 5317b1f0deSMike Frysinger 541da177e4SLinus Torvalds static struct sym_entry *table; 55b3dbb4ecSPaulo Marques static unsigned int table_size, table_cnt; 561da177e4SLinus Torvalds static int all_symbols = 0; 5741f11a4fSYoshinori Sato static char symbol_prefix_char = '\0'; 581da177e4SLinus Torvalds 59b3dbb4ecSPaulo Marques int token_profit[0x10000]; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds /* the table that holds the result of the compression */ 62b3dbb4ecSPaulo Marques unsigned char best_table[256][2]; 631da177e4SLinus Torvalds unsigned char best_table_len[256]; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds 66b3dbb4ecSPaulo Marques static void usage(void) 671da177e4SLinus Torvalds { 6841f11a4fSYoshinori Sato fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n"); 691da177e4SLinus Torvalds exit(1); 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* 731da177e4SLinus Torvalds * This ignores the intensely annoying "mapping symbols" found 741da177e4SLinus Torvalds * in ARM ELF files: $a, $t and $d. 751da177e4SLinus Torvalds */ 76b3dbb4ecSPaulo Marques static inline int is_arm_mapping_symbol(const char *str) 771da177e4SLinus Torvalds { 781da177e4SLinus Torvalds return str[0] == '$' && strchr("atd", str[1]) 791da177e4SLinus Torvalds && (str[2] == '\0' || str[2] == '.'); 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 8217b1f0deSMike Frysinger static int read_symbol_tr(const char *sym, unsigned long long addr) 8317b1f0deSMike Frysinger { 8417b1f0deSMike Frysinger size_t i; 8517b1f0deSMike Frysinger struct text_range *tr; 8617b1f0deSMike Frysinger 8717b1f0deSMike Frysinger for (i = 0; i < ARRAY_SIZE(text_ranges); ++i) { 8817b1f0deSMike Frysinger tr = &text_ranges[i]; 8917b1f0deSMike Frysinger 9017b1f0deSMike Frysinger if (strcmp(sym, tr->stext) == 0) { 9117b1f0deSMike Frysinger tr->start = addr; 9217b1f0deSMike Frysinger return 0; 9317b1f0deSMike Frysinger } else if (strcmp(sym, tr->etext) == 0) { 9417b1f0deSMike Frysinger tr->end = addr; 9517b1f0deSMike Frysinger return 0; 9617b1f0deSMike Frysinger } 9717b1f0deSMike Frysinger } 9817b1f0deSMike Frysinger 9917b1f0deSMike Frysinger return 1; 10017b1f0deSMike Frysinger } 10117b1f0deSMike Frysinger 102b3dbb4ecSPaulo Marques static int read_symbol(FILE *in, struct sym_entry *s) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds char str[500]; 105b3dbb4ecSPaulo Marques char *sym, stype; 1061da177e4SLinus Torvalds int rc; 1071da177e4SLinus Torvalds 108b3dbb4ecSPaulo Marques rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str); 1091da177e4SLinus Torvalds if (rc != 3) { 1101da177e4SLinus Torvalds if (rc != EOF) { 1111da177e4SLinus Torvalds /* skip line */ 1121da177e4SLinus Torvalds fgets(str, 500, in); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds return -1; 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds 11741f11a4fSYoshinori Sato sym = str; 11841f11a4fSYoshinori Sato /* skip prefix char */ 11941f11a4fSYoshinori Sato if (symbol_prefix_char && str[0] == symbol_prefix_char) 12041f11a4fSYoshinori Sato sym++; 12141f11a4fSYoshinori Sato 1221da177e4SLinus Torvalds /* Ignore most absolute/undefined (?) symbols. */ 123fd593d12SEric W. Biederman if (strcmp(sym, "_text") == 0) 124fd593d12SEric W. Biederman _text = s->addr; 12517b1f0deSMike Frysinger else if (read_symbol_tr(sym, s->addr) == 0) 12617b1f0deSMike Frysinger /* nothing to do */; 127b3dbb4ecSPaulo Marques else if (toupper(stype) == 'A') 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds /* Keep these useful absolute symbols */ 13041f11a4fSYoshinori Sato if (strcmp(sym, "__kernel_syscall_via_break") && 13141f11a4fSYoshinori Sato strcmp(sym, "__kernel_syscall_via_epc") && 13241f11a4fSYoshinori Sato strcmp(sym, "__kernel_sigtramp") && 13341f11a4fSYoshinori Sato strcmp(sym, "__gp")) 1341da177e4SLinus Torvalds return -1; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds } 137b3dbb4ecSPaulo Marques else if (toupper(stype) == 'U' || 13841f11a4fSYoshinori Sato is_arm_mapping_symbol(sym)) 1391da177e4SLinus Torvalds return -1; 1406f00df24SRalf Baechle /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */ 1416f00df24SRalf Baechle else if (str[0] == '$') 1426f00df24SRalf Baechle return -1; 143aab34ac8SSam Ravnborg /* exclude debugging symbols */ 144aab34ac8SSam Ravnborg else if (stype == 'N') 145aab34ac8SSam Ravnborg return -1; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* include the type field in the symbol name, so that it gets 1481da177e4SLinus Torvalds * compressed together */ 1491da177e4SLinus Torvalds s->len = strlen(str) + 1; 150b3dbb4ecSPaulo Marques s->sym = malloc(s->len + 1); 151f1a136e0SJesper Juhl if (!s->sym) { 152f1a136e0SJesper Juhl fprintf(stderr, "kallsyms failure: " 153f1a136e0SJesper Juhl "unable to allocate required amount of memory\n"); 154f1a136e0SJesper Juhl exit(EXIT_FAILURE); 155f1a136e0SJesper Juhl } 156b3dbb4ecSPaulo Marques strcpy((char *)s->sym + 1, str); 157b3dbb4ecSPaulo Marques s->sym[0] = stype; 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds return 0; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 16217b1f0deSMike Frysinger static int symbol_valid_tr(struct sym_entry *s) 16317b1f0deSMike Frysinger { 16417b1f0deSMike Frysinger size_t i; 16517b1f0deSMike Frysinger struct text_range *tr; 16617b1f0deSMike Frysinger 16717b1f0deSMike Frysinger for (i = 0; i < ARRAY_SIZE(text_ranges); ++i) { 16817b1f0deSMike Frysinger tr = &text_ranges[i]; 16917b1f0deSMike Frysinger 170ac6ca5c8SMike Frysinger if (s->addr >= tr->start && s->addr <= tr->end) 171ac6ca5c8SMike Frysinger return 1; 17217b1f0deSMike Frysinger } 17317b1f0deSMike Frysinger 174ac6ca5c8SMike Frysinger return 0; 17517b1f0deSMike Frysinger } 17617b1f0deSMike Frysinger 177b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds /* Symbols which vary between passes. Passes 1 and 2 must have 1802ea03891SSam Ravnborg * identical symbol lists. The kallsyms_* symbols below are only added 1812ea03891SSam Ravnborg * after pass 1, they would be included in pass 2 when --all-symbols is 1822ea03891SSam Ravnborg * specified so exclude them to get a stable symbol list. 1831da177e4SLinus Torvalds */ 1841da177e4SLinus Torvalds static char *special_symbols[] = { 1852ea03891SSam Ravnborg "kallsyms_addresses", 1862ea03891SSam Ravnborg "kallsyms_num_syms", 1872ea03891SSam Ravnborg "kallsyms_names", 1882ea03891SSam Ravnborg "kallsyms_markers", 1892ea03891SSam Ravnborg "kallsyms_token_table", 1902ea03891SSam Ravnborg "kallsyms_token_index", 1912ea03891SSam Ravnborg 1921da177e4SLinus Torvalds /* Exclude linker generated symbols which vary between passes */ 1931da177e4SLinus Torvalds "_SDA_BASE_", /* ppc */ 1941da177e4SLinus Torvalds "_SDA2_BASE_", /* ppc */ 1951da177e4SLinus Torvalds NULL }; 1961da177e4SLinus Torvalds int i; 19741f11a4fSYoshinori Sato int offset = 1; 19841f11a4fSYoshinori Sato 19941f11a4fSYoshinori Sato /* skip prefix char */ 20041f11a4fSYoshinori Sato if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char) 20141f11a4fSYoshinori Sato offset++; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds /* if --all-symbols is not specified, then symbols outside the text 2041da177e4SLinus Torvalds * and inittext sections are discarded */ 2051da177e4SLinus Torvalds if (!all_symbols) { 20617b1f0deSMike Frysinger if (symbol_valid_tr(s) == 0) 2071da177e4SLinus Torvalds return 0; 2081da177e4SLinus Torvalds /* Corner case. Discard any symbols with the same value as 209a3b81113SRobin Getz * _etext _einittext; they can move between pass 1 and 2 when 210a3b81113SRobin Getz * the kallsyms data are added. If these symbols move then 211a3b81113SRobin Getz * they may get dropped in pass 2, which breaks the kallsyms 212a3b81113SRobin Getz * rules. 2131da177e4SLinus Torvalds */ 21417b1f0deSMike Frysinger if ((s->addr == text_range_text->end && 21517b1f0deSMike Frysinger strcmp((char *)s->sym + offset, text_range_text->etext)) || 21617b1f0deSMike Frysinger (s->addr == text_range_inittext->end && 21717b1f0deSMike Frysinger strcmp((char *)s->sym + offset, text_range_inittext->etext))) 2181da177e4SLinus Torvalds return 0; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* Exclude symbols which vary between passes. */ 2222ea03891SSam Ravnborg if (strstr((char *)s->sym + offset, "_compiled.")) 2231da177e4SLinus Torvalds return 0; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds for (i = 0; special_symbols[i]; i++) 226b3dbb4ecSPaulo Marques if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 ) 2271da177e4SLinus Torvalds return 0; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds return 1; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 232b3dbb4ecSPaulo Marques static void read_map(FILE *in) 2331da177e4SLinus Torvalds { 2341da177e4SLinus Torvalds while (!feof(in)) { 235b3dbb4ecSPaulo Marques if (table_cnt >= table_size) { 236b3dbb4ecSPaulo Marques table_size += 10000; 237b3dbb4ecSPaulo Marques table = realloc(table, sizeof(*table) * table_size); 2381da177e4SLinus Torvalds if (!table) { 2391da177e4SLinus Torvalds fprintf(stderr, "out of memory\n"); 2401da177e4SLinus Torvalds exit (1); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds } 243f2df3f65SPaulo Marques if (read_symbol(in, &table[table_cnt]) == 0) { 244f2df3f65SPaulo Marques table[table_cnt].start_pos = table_cnt; 245b3dbb4ecSPaulo Marques table_cnt++; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds } 248f2df3f65SPaulo Marques } 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds static void output_label(char *label) 2511da177e4SLinus Torvalds { 25241f11a4fSYoshinori Sato if (symbol_prefix_char) 25341f11a4fSYoshinori Sato printf(".globl %c%s\n", symbol_prefix_char, label); 25441f11a4fSYoshinori Sato else 2551da177e4SLinus Torvalds printf(".globl %s\n", label); 2561da177e4SLinus Torvalds printf("\tALGN\n"); 25741f11a4fSYoshinori Sato if (symbol_prefix_char) 25841f11a4fSYoshinori Sato printf("%c%s:\n", symbol_prefix_char, label); 25941f11a4fSYoshinori Sato else 2601da177e4SLinus Torvalds printf("%s:\n", label); 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table 2641da177e4SLinus Torvalds * might still be compressed itself, so the function needs to be recursive */ 2651da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds int c, rlen, total=0; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds while (len) { 2701da177e4SLinus Torvalds c = *data; 2711da177e4SLinus Torvalds /* if the table holds a single char that is the same as the one 2721da177e4SLinus Torvalds * we are looking for, then end the search */ 2731da177e4SLinus Torvalds if (best_table[c][0]==c && best_table_len[c]==1) { 2741da177e4SLinus Torvalds *result++ = c; 2751da177e4SLinus Torvalds total++; 2761da177e4SLinus Torvalds } else { 2771da177e4SLinus Torvalds /* if not, recurse and expand */ 2781da177e4SLinus Torvalds rlen = expand_symbol(best_table[c], best_table_len[c], result); 2791da177e4SLinus Torvalds total += rlen; 2801da177e4SLinus Torvalds result += rlen; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds data++; 2831da177e4SLinus Torvalds len--; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds *result=0; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds return total; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds 290b3dbb4ecSPaulo Marques static void write_src(void) 2911da177e4SLinus Torvalds { 292b3dbb4ecSPaulo Marques unsigned int i, k, off; 2931da177e4SLinus Torvalds unsigned int best_idx[256]; 2941da177e4SLinus Torvalds unsigned int *markers; 2959281aceaSTejun Heo char buf[KSYM_NAME_LEN]; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds printf("#include <asm/types.h>\n"); 2981da177e4SLinus Torvalds printf("#if BITS_PER_LONG == 64\n"); 2991da177e4SLinus Torvalds printf("#define PTR .quad\n"); 3001da177e4SLinus Torvalds printf("#define ALGN .align 8\n"); 3011da177e4SLinus Torvalds printf("#else\n"); 3021da177e4SLinus Torvalds printf("#define PTR .long\n"); 3031da177e4SLinus Torvalds printf("#define ALGN .align 4\n"); 3041da177e4SLinus Torvalds printf("#endif\n"); 3051da177e4SLinus Torvalds 306aad09470SJan Beulich printf("\t.section .rodata, \"a\"\n"); 3071da177e4SLinus Torvalds 308fd593d12SEric W. Biederman /* Provide proper symbols relocatability by their '_text' 309fd593d12SEric W. Biederman * relativeness. The symbol names cannot be used to construct 310fd593d12SEric W. Biederman * normal symbol references as the list of symbols contains 311fd593d12SEric W. Biederman * symbols that are declared static and are private to their 312fd593d12SEric W. Biederman * .o files. This prevents .tmp_kallsyms.o or any other 313fd593d12SEric W. Biederman * object from referencing them. 314fd593d12SEric W. Biederman */ 3151da177e4SLinus Torvalds output_label("kallsyms_addresses"); 316b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 317fd593d12SEric W. Biederman if (toupper(table[i].sym[0]) != 'A') { 3182c22d8baSVivek Goyal if (_text <= table[i].addr) 319fd593d12SEric W. Biederman printf("\tPTR\t_text + %#llx\n", 320fd593d12SEric W. Biederman table[i].addr - _text); 3212c22d8baSVivek Goyal else 3222c22d8baSVivek Goyal printf("\tPTR\t_text - %#llx\n", 3232c22d8baSVivek Goyal _text - table[i].addr); 324fd593d12SEric W. Biederman } else { 3251da177e4SLinus Torvalds printf("\tPTR\t%#llx\n", table[i].addr); 3261da177e4SLinus Torvalds } 327fd593d12SEric W. Biederman } 3281da177e4SLinus Torvalds printf("\n"); 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds output_label("kallsyms_num_syms"); 331b3dbb4ecSPaulo Marques printf("\tPTR\t%d\n", table_cnt); 3321da177e4SLinus Torvalds printf("\n"); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* table of offset markers, that give the offset in the compressed stream 3351da177e4SLinus Torvalds * every 256 symbols */ 336f1a136e0SJesper Juhl markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256)); 337f1a136e0SJesper Juhl if (!markers) { 338f1a136e0SJesper Juhl fprintf(stderr, "kallsyms failure: " 339f1a136e0SJesper Juhl "unable to allocate required memory\n"); 340f1a136e0SJesper Juhl exit(EXIT_FAILURE); 341f1a136e0SJesper Juhl } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds output_label("kallsyms_names"); 3441da177e4SLinus Torvalds off = 0; 345b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 346b3dbb4ecSPaulo Marques if ((i & 0xFF) == 0) 347b3dbb4ecSPaulo Marques markers[i >> 8] = off; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds printf("\t.byte 0x%02x", table[i].len); 3501da177e4SLinus Torvalds for (k = 0; k < table[i].len; k++) 3511da177e4SLinus Torvalds printf(", 0x%02x", table[i].sym[k]); 3521da177e4SLinus Torvalds printf("\n"); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds off += table[i].len + 1; 3551da177e4SLinus Torvalds } 3561da177e4SLinus Torvalds printf("\n"); 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds output_label("kallsyms_markers"); 359b3dbb4ecSPaulo Marques for (i = 0; i < ((table_cnt + 255) >> 8); i++) 3601da177e4SLinus Torvalds printf("\tPTR\t%d\n", markers[i]); 3611da177e4SLinus Torvalds printf("\n"); 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds free(markers); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds output_label("kallsyms_token_table"); 3661da177e4SLinus Torvalds off = 0; 3671da177e4SLinus Torvalds for (i = 0; i < 256; i++) { 3681da177e4SLinus Torvalds best_idx[i] = off; 3691da177e4SLinus Torvalds expand_symbol(best_table[i], best_table_len[i], buf); 3701da177e4SLinus Torvalds printf("\t.asciz\t\"%s\"\n", buf); 3711da177e4SLinus Torvalds off += strlen(buf) + 1; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds printf("\n"); 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds output_label("kallsyms_token_index"); 3761da177e4SLinus Torvalds for (i = 0; i < 256; i++) 3771da177e4SLinus Torvalds printf("\t.short\t%d\n", best_idx[i]); 3781da177e4SLinus Torvalds printf("\n"); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds /* table lookup compression functions */ 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds /* count all the possible tokens in a symbol */ 3851da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len) 3861da177e4SLinus Torvalds { 3871da177e4SLinus Torvalds int i; 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds for (i = 0; i < len - 1; i++) 390b3dbb4ecSPaulo Marques token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */ 3941da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len) 3951da177e4SLinus Torvalds { 3961da177e4SLinus Torvalds int i; 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds for (i = 0; i < len - 1; i++) 399b3dbb4ecSPaulo Marques token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 402b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */ 4031da177e4SLinus Torvalds static void build_initial_tok_table(void) 4041da177e4SLinus Torvalds { 405b3dbb4ecSPaulo Marques unsigned int i, pos; 4061da177e4SLinus Torvalds 407b3dbb4ecSPaulo Marques pos = 0; 408b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 4091da177e4SLinus Torvalds if ( symbol_valid(&table[i]) ) { 410b3dbb4ecSPaulo Marques if (pos != i) 411b3dbb4ecSPaulo Marques table[pos] = table[i]; 412b3dbb4ecSPaulo Marques learn_symbol(table[pos].sym, table[pos].len); 413b3dbb4ecSPaulo Marques pos++; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds } 416b3dbb4ecSPaulo Marques table_cnt = pos; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4197c5d249aSPaulo Marques static void *find_token(unsigned char *str, int len, unsigned char *token) 4207c5d249aSPaulo Marques { 4217c5d249aSPaulo Marques int i; 4227c5d249aSPaulo Marques 4237c5d249aSPaulo Marques for (i = 0; i < len - 1; i++) { 4247c5d249aSPaulo Marques if (str[i] == token[0] && str[i+1] == token[1]) 4257c5d249aSPaulo Marques return &str[i]; 4267c5d249aSPaulo Marques } 4277c5d249aSPaulo Marques return NULL; 4287c5d249aSPaulo Marques } 4297c5d249aSPaulo Marques 4301da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols 4311da177e4SLinus Torvalds * to update the counts */ 432b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx) 4331da177e4SLinus Torvalds { 434b3dbb4ecSPaulo Marques unsigned int i, len, size; 435b3dbb4ecSPaulo Marques unsigned char *p1, *p2; 4361da177e4SLinus Torvalds 437b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds len = table[i].len; 440b3dbb4ecSPaulo Marques p1 = table[i].sym; 441b3dbb4ecSPaulo Marques 442b3dbb4ecSPaulo Marques /* find the token on the symbol */ 4437c5d249aSPaulo Marques p2 = find_token(p1, len, str); 444b3dbb4ecSPaulo Marques if (!p2) continue; 445b3dbb4ecSPaulo Marques 446b3dbb4ecSPaulo Marques /* decrease the counts for this symbol's tokens */ 447b3dbb4ecSPaulo Marques forget_symbol(table[i].sym, len); 448b3dbb4ecSPaulo Marques 449b3dbb4ecSPaulo Marques size = len; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds do { 452b3dbb4ecSPaulo Marques *p2 = idx; 453b3dbb4ecSPaulo Marques p2++; 454b3dbb4ecSPaulo Marques size -= (p2 - p1); 455b3dbb4ecSPaulo Marques memmove(p2, p2 + 1, size); 456b3dbb4ecSPaulo Marques p1 = p2; 457b3dbb4ecSPaulo Marques len--; 458b3dbb4ecSPaulo Marques 459b3dbb4ecSPaulo Marques if (size < 2) break; 460b3dbb4ecSPaulo Marques 4611da177e4SLinus Torvalds /* find the token on the symbol */ 4627c5d249aSPaulo Marques p2 = find_token(p1, size, str); 4631da177e4SLinus Torvalds 464b3dbb4ecSPaulo Marques } while (p2); 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds table[i].len = len; 467b3dbb4ecSPaulo Marques 468b3dbb4ecSPaulo Marques /* increase the counts for this symbol's new tokens */ 4691da177e4SLinus Torvalds learn_symbol(table[i].sym, len); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /* search the token with the maximum profit */ 474b3dbb4ecSPaulo Marques static int find_best_token(void) 4751da177e4SLinus Torvalds { 476b3dbb4ecSPaulo Marques int i, best, bestprofit; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds bestprofit=-10000; 479b3dbb4ecSPaulo Marques best = 0; 4801da177e4SLinus Torvalds 481b3dbb4ecSPaulo Marques for (i = 0; i < 0x10000; i++) { 482b3dbb4ecSPaulo Marques if (token_profit[i] > bestprofit) { 483b3dbb4ecSPaulo Marques best = i; 484b3dbb4ecSPaulo Marques bestprofit = token_profit[i]; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds return best; 4881da177e4SLinus Torvalds } 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */ 4911da177e4SLinus Torvalds static void optimize_result(void) 4921da177e4SLinus Torvalds { 493b3dbb4ecSPaulo Marques int i, best; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* using the '\0' symbol last allows compress_symbols to use standard 4961da177e4SLinus Torvalds * fast string functions */ 4971da177e4SLinus Torvalds for (i = 255; i >= 0; i--) { 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /* if this table slot is empty (it is not used by an actual 5001da177e4SLinus Torvalds * original char code */ 5011da177e4SLinus Torvalds if (!best_table_len[i]) { 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds /* find the token with the breates profit value */ 5041da177e4SLinus Torvalds best = find_best_token(); 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /* place it in the "best" table */ 507b3dbb4ecSPaulo Marques best_table_len[i] = 2; 508b3dbb4ecSPaulo Marques best_table[i][0] = best & 0xFF; 509b3dbb4ecSPaulo Marques best_table[i][1] = (best >> 8) & 0xFF; 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /* replace this token in all the valid symbols */ 512b3dbb4ecSPaulo Marques compress_symbols(best_table[i], i); 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds 5171da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */ 5181da177e4SLinus Torvalds static void insert_real_symbols_in_table(void) 5191da177e4SLinus Torvalds { 520b3dbb4ecSPaulo Marques unsigned int i, j, c; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds memset(best_table, 0, sizeof(best_table)); 5231da177e4SLinus Torvalds memset(best_table_len, 0, sizeof(best_table_len)); 5241da177e4SLinus Torvalds 525b3dbb4ecSPaulo Marques for (i = 0; i < table_cnt; i++) { 5261da177e4SLinus Torvalds for (j = 0; j < table[i].len; j++) { 5271da177e4SLinus Torvalds c = table[i].sym[j]; 5281da177e4SLinus Torvalds best_table[c][0]=c; 5291da177e4SLinus Torvalds best_table_len[c]=1; 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds static void optimize_token_table(void) 5351da177e4SLinus Torvalds { 5361da177e4SLinus Torvalds build_initial_tok_table(); 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds insert_real_symbols_in_table(); 5391da177e4SLinus Torvalds 54041f11a4fSYoshinori Sato /* When valid symbol is not registered, exit to error */ 541b3dbb4ecSPaulo Marques if (!table_cnt) { 54241f11a4fSYoshinori Sato fprintf(stderr, "No valid symbol.\n"); 54341f11a4fSYoshinori Sato exit(1); 54441f11a4fSYoshinori Sato } 54541f11a4fSYoshinori Sato 5461da177e4SLinus Torvalds optimize_result(); 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 549b478b782SLai Jiangshan /* guess for "linker script provide" symbol */ 550b478b782SLai Jiangshan static int may_be_linker_script_provide_symbol(const struct sym_entry *se) 551b478b782SLai Jiangshan { 552b478b782SLai Jiangshan const char *symbol = (char *)se->sym + 1; 553b478b782SLai Jiangshan int len = se->len - 1; 554b478b782SLai Jiangshan 555b478b782SLai Jiangshan if (len < 8) 556b478b782SLai Jiangshan return 0; 557b478b782SLai Jiangshan 558b478b782SLai Jiangshan if (symbol[0] != '_' || symbol[1] != '_') 559b478b782SLai Jiangshan return 0; 560b478b782SLai Jiangshan 561b478b782SLai Jiangshan /* __start_XXXXX */ 562b478b782SLai Jiangshan if (!memcmp(symbol + 2, "start_", 6)) 563b478b782SLai Jiangshan return 1; 564b478b782SLai Jiangshan 565b478b782SLai Jiangshan /* __stop_XXXXX */ 566b478b782SLai Jiangshan if (!memcmp(symbol + 2, "stop_", 5)) 567b478b782SLai Jiangshan return 1; 568b478b782SLai Jiangshan 569b478b782SLai Jiangshan /* __end_XXXXX */ 570b478b782SLai Jiangshan if (!memcmp(symbol + 2, "end_", 4)) 571b478b782SLai Jiangshan return 1; 572b478b782SLai Jiangshan 573b478b782SLai Jiangshan /* __XXXXX_start */ 574b478b782SLai Jiangshan if (!memcmp(symbol + len - 6, "_start", 6)) 575b478b782SLai Jiangshan return 1; 576b478b782SLai Jiangshan 577b478b782SLai Jiangshan /* __XXXXX_end */ 578b478b782SLai Jiangshan if (!memcmp(symbol + len - 4, "_end", 4)) 579b478b782SLai Jiangshan return 1; 580b478b782SLai Jiangshan 581b478b782SLai Jiangshan return 0; 582b478b782SLai Jiangshan } 583b478b782SLai Jiangshan 584b478b782SLai Jiangshan static int prefix_underscores_count(const char *str) 585b478b782SLai Jiangshan { 586b478b782SLai Jiangshan const char *tail = str; 587b478b782SLai Jiangshan 588*a9ece53cSPaul Mundt while (*tail == '_') 589b478b782SLai Jiangshan tail++; 590b478b782SLai Jiangshan 591b478b782SLai Jiangshan return tail - str; 592b478b782SLai Jiangshan } 593b478b782SLai Jiangshan 594f2df3f65SPaulo Marques static int compare_symbols(const void *a, const void *b) 595f2df3f65SPaulo Marques { 596f2df3f65SPaulo Marques const struct sym_entry *sa; 597f2df3f65SPaulo Marques const struct sym_entry *sb; 598f2df3f65SPaulo Marques int wa, wb; 599f2df3f65SPaulo Marques 600f2df3f65SPaulo Marques sa = a; 601f2df3f65SPaulo Marques sb = b; 602f2df3f65SPaulo Marques 603f2df3f65SPaulo Marques /* sort by address first */ 604f2df3f65SPaulo Marques if (sa->addr > sb->addr) 605f2df3f65SPaulo Marques return 1; 606f2df3f65SPaulo Marques if (sa->addr < sb->addr) 607f2df3f65SPaulo Marques return -1; 608f2df3f65SPaulo Marques 609f2df3f65SPaulo Marques /* sort by "weakness" type */ 610f2df3f65SPaulo Marques wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W'); 611f2df3f65SPaulo Marques wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W'); 612f2df3f65SPaulo Marques if (wa != wb) 613f2df3f65SPaulo Marques return wa - wb; 614f2df3f65SPaulo Marques 615b478b782SLai Jiangshan /* sort by "linker script provide" type */ 616b478b782SLai Jiangshan wa = may_be_linker_script_provide_symbol(sa); 617b478b782SLai Jiangshan wb = may_be_linker_script_provide_symbol(sb); 618b478b782SLai Jiangshan if (wa != wb) 619b478b782SLai Jiangshan return wa - wb; 620b478b782SLai Jiangshan 621b478b782SLai Jiangshan /* sort by the number of prefix underscores */ 622b478b782SLai Jiangshan wa = prefix_underscores_count((const char *)sa->sym + 1); 623b478b782SLai Jiangshan wb = prefix_underscores_count((const char *)sb->sym + 1); 624b478b782SLai Jiangshan if (wa != wb) 625b478b782SLai Jiangshan return wa - wb; 626b478b782SLai Jiangshan 627f2df3f65SPaulo Marques /* sort by initial order, so that other symbols are left undisturbed */ 628f2df3f65SPaulo Marques return sa->start_pos - sb->start_pos; 629f2df3f65SPaulo Marques } 630f2df3f65SPaulo Marques 631f2df3f65SPaulo Marques static void sort_symbols(void) 632f2df3f65SPaulo Marques { 633f2df3f65SPaulo Marques qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols); 634f2df3f65SPaulo Marques } 6351da177e4SLinus Torvalds 636b3dbb4ecSPaulo Marques int main(int argc, char **argv) 6371da177e4SLinus Torvalds { 63841f11a4fSYoshinori Sato if (argc >= 2) { 63941f11a4fSYoshinori Sato int i; 64041f11a4fSYoshinori Sato for (i = 1; i < argc; i++) { 64141f11a4fSYoshinori Sato if(strcmp(argv[i], "--all-symbols") == 0) 6421da177e4SLinus Torvalds all_symbols = 1; 64341f11a4fSYoshinori Sato else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) { 64441f11a4fSYoshinori Sato char *p = &argv[i][16]; 64541f11a4fSYoshinori Sato /* skip quote */ 64641f11a4fSYoshinori Sato if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\'')) 64741f11a4fSYoshinori Sato p++; 64841f11a4fSYoshinori Sato symbol_prefix_char = *p; 64941f11a4fSYoshinori Sato } else 65041f11a4fSYoshinori Sato usage(); 65141f11a4fSYoshinori Sato } 65241f11a4fSYoshinori Sato } else if (argc != 1) 6531da177e4SLinus Torvalds usage(); 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds read_map(stdin); 656f2df3f65SPaulo Marques sort_symbols(); 6571da177e4SLinus Torvalds optimize_token_table(); 6581da177e4SLinus Torvalds write_src(); 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds return 0; 6611da177e4SLinus Torvalds } 662