xref: /linux/scripts/kallsyms.c (revision 2ea038917bbdd51a7ae4a898c6a04641324dd033)
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 
269281aceaSTejun Heo #define KSYM_NAME_LEN		128
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds struct sym_entry {
291da177e4SLinus Torvalds 	unsigned long long addr;
30b3dbb4ecSPaulo Marques 	unsigned int len;
31f2df3f65SPaulo Marques 	unsigned int start_pos;
321da177e4SLinus Torvalds 	unsigned char *sym;
331da177e4SLinus Torvalds };
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static struct sym_entry *table;
36b3dbb4ecSPaulo Marques static unsigned int table_size, table_cnt;
37a3b81113SRobin Getz static unsigned long long _text, _stext, _etext, _sinittext, _einittext;
381da177e4SLinus Torvalds static int all_symbols = 0;
3941f11a4fSYoshinori Sato static char symbol_prefix_char = '\0';
401da177e4SLinus Torvalds 
41b3dbb4ecSPaulo Marques int token_profit[0x10000];
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* the table that holds the result of the compression */
44b3dbb4ecSPaulo Marques unsigned char best_table[256][2];
451da177e4SLinus Torvalds unsigned char best_table_len[256];
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds 
48b3dbb4ecSPaulo Marques static void usage(void)
491da177e4SLinus Torvalds {
5041f11a4fSYoshinori Sato 	fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
511da177e4SLinus Torvalds 	exit(1);
521da177e4SLinus Torvalds }
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  * This ignores the intensely annoying "mapping symbols" found
561da177e4SLinus Torvalds  * in ARM ELF files: $a, $t and $d.
571da177e4SLinus Torvalds  */
58b3dbb4ecSPaulo Marques static inline int is_arm_mapping_symbol(const char *str)
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	return str[0] == '$' && strchr("atd", str[1])
611da177e4SLinus Torvalds 	       && (str[2] == '\0' || str[2] == '.');
621da177e4SLinus Torvalds }
631da177e4SLinus Torvalds 
64b3dbb4ecSPaulo Marques static int read_symbol(FILE *in, struct sym_entry *s)
651da177e4SLinus Torvalds {
661da177e4SLinus Torvalds 	char str[500];
67b3dbb4ecSPaulo Marques 	char *sym, stype;
681da177e4SLinus Torvalds 	int rc;
691da177e4SLinus Torvalds 
70b3dbb4ecSPaulo Marques 	rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
711da177e4SLinus Torvalds 	if (rc != 3) {
721da177e4SLinus Torvalds 		if (rc != EOF) {
731da177e4SLinus Torvalds 			/* skip line */
741da177e4SLinus Torvalds 			fgets(str, 500, in);
751da177e4SLinus Torvalds 		}
761da177e4SLinus Torvalds 		return -1;
771da177e4SLinus Torvalds 	}
781da177e4SLinus Torvalds 
7941f11a4fSYoshinori Sato 	sym = str;
8041f11a4fSYoshinori Sato 	/* skip prefix char */
8141f11a4fSYoshinori Sato 	if (symbol_prefix_char && str[0] == symbol_prefix_char)
8241f11a4fSYoshinori Sato 		sym++;
8341f11a4fSYoshinori Sato 
841da177e4SLinus Torvalds 	/* Ignore most absolute/undefined (?) symbols. */
85fd593d12SEric W. Biederman 	if (strcmp(sym, "_text") == 0)
86fd593d12SEric W. Biederman 		_text = s->addr;
87fd593d12SEric W. Biederman 	else if (strcmp(sym, "_stext") == 0)
881da177e4SLinus Torvalds 		_stext = s->addr;
8941f11a4fSYoshinori Sato 	else if (strcmp(sym, "_etext") == 0)
901da177e4SLinus Torvalds 		_etext = s->addr;
9141f11a4fSYoshinori Sato 	else if (strcmp(sym, "_sinittext") == 0)
921da177e4SLinus Torvalds 		_sinittext = s->addr;
9341f11a4fSYoshinori Sato 	else if (strcmp(sym, "_einittext") == 0)
941da177e4SLinus Torvalds 		_einittext = s->addr;
95b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'A')
961da177e4SLinus Torvalds 	{
971da177e4SLinus Torvalds 		/* Keep these useful absolute symbols */
9841f11a4fSYoshinori Sato 		if (strcmp(sym, "__kernel_syscall_via_break") &&
9941f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_syscall_via_epc") &&
10041f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_sigtramp") &&
10141f11a4fSYoshinori Sato 		    strcmp(sym, "__gp"))
1021da177e4SLinus Torvalds 			return -1;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	}
105b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'U' ||
10641f11a4fSYoshinori Sato 		 is_arm_mapping_symbol(sym))
1071da177e4SLinus Torvalds 		return -1;
1086f00df24SRalf Baechle 	/* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
1096f00df24SRalf Baechle 	else if (str[0] == '$')
1106f00df24SRalf Baechle 		return -1;
111aab34ac8SSam Ravnborg 	/* exclude debugging symbols */
112aab34ac8SSam Ravnborg 	else if (stype == 'N')
113aab34ac8SSam Ravnborg 		return -1;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	/* include the type field in the symbol name, so that it gets
1161da177e4SLinus Torvalds 	 * compressed together */
1171da177e4SLinus Torvalds 	s->len = strlen(str) + 1;
118b3dbb4ecSPaulo Marques 	s->sym = malloc(s->len + 1);
119f1a136e0SJesper Juhl 	if (!s->sym) {
120f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
121f1a136e0SJesper Juhl 			"unable to allocate required amount of memory\n");
122f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
123f1a136e0SJesper Juhl 	}
124b3dbb4ecSPaulo Marques 	strcpy((char *)s->sym + 1, str);
125b3dbb4ecSPaulo Marques 	s->sym[0] = stype;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	return 0;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
130b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	/* Symbols which vary between passes.  Passes 1 and 2 must have
133*2ea03891SSam Ravnborg 	 * identical symbol lists.  The kallsyms_* symbols below are only added
134*2ea03891SSam Ravnborg 	 * after pass 1, they would be included in pass 2 when --all-symbols is
135*2ea03891SSam Ravnborg 	 * specified so exclude them to get a stable symbol list.
1361da177e4SLinus Torvalds 	 */
1371da177e4SLinus Torvalds 	static char *special_symbols[] = {
138*2ea03891SSam Ravnborg 		"kallsyms_addresses",
139*2ea03891SSam Ravnborg 		"kallsyms_num_syms",
140*2ea03891SSam Ravnborg 		"kallsyms_names",
141*2ea03891SSam Ravnborg 		"kallsyms_markers",
142*2ea03891SSam Ravnborg 		"kallsyms_token_table",
143*2ea03891SSam Ravnborg 		"kallsyms_token_index",
144*2ea03891SSam Ravnborg 
1451da177e4SLinus Torvalds 	/* Exclude linker generated symbols which vary between passes */
1461da177e4SLinus Torvalds 		"_SDA_BASE_",		/* ppc */
1471da177e4SLinus Torvalds 		"_SDA2_BASE_",		/* ppc */
1481da177e4SLinus Torvalds 		NULL };
1491da177e4SLinus Torvalds 	int i;
15041f11a4fSYoshinori Sato 	int offset = 1;
15141f11a4fSYoshinori Sato 
15241f11a4fSYoshinori Sato 	/* skip prefix char */
15341f11a4fSYoshinori Sato 	if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
15441f11a4fSYoshinori Sato 		offset++;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	/* if --all-symbols is not specified, then symbols outside the text
1571da177e4SLinus Torvalds 	 * and inittext sections are discarded */
1581da177e4SLinus Torvalds 	if (!all_symbols) {
1591da177e4SLinus Torvalds 		if ((s->addr < _stext || s->addr > _etext)
160a3b81113SRobin Getz 		    && (s->addr < _sinittext || s->addr > _einittext))
1611da177e4SLinus Torvalds 			return 0;
1621da177e4SLinus Torvalds 		/* Corner case.  Discard any symbols with the same value as
163a3b81113SRobin Getz 		 * _etext _einittext; they can move between pass 1 and 2 when
164a3b81113SRobin Getz 		 * the kallsyms data are added.  If these symbols move then
165a3b81113SRobin Getz 		 * they may get dropped in pass 2, which breaks the kallsyms
166a3b81113SRobin Getz 		 * rules.
1671da177e4SLinus Torvalds 		 */
168a3b81113SRobin Getz 		if ((s->addr == _etext &&
169a3b81113SRobin Getz 				strcmp((char *)s->sym + offset, "_etext")) ||
170a3b81113SRobin Getz 		    (s->addr == _einittext &&
171a3b81113SRobin Getz 				strcmp((char *)s->sym + offset, "_einittext")))
1721da177e4SLinus Torvalds 			return 0;
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	/* Exclude symbols which vary between passes. */
176*2ea03891SSam Ravnborg 	if (strstr((char *)s->sym + offset, "_compiled."))
1771da177e4SLinus Torvalds 		return 0;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	for (i = 0; special_symbols[i]; i++)
180b3dbb4ecSPaulo Marques 		if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
1811da177e4SLinus Torvalds 			return 0;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	return 1;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
186b3dbb4ecSPaulo Marques static void read_map(FILE *in)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	while (!feof(in)) {
189b3dbb4ecSPaulo Marques 		if (table_cnt >= table_size) {
190b3dbb4ecSPaulo Marques 			table_size += 10000;
191b3dbb4ecSPaulo Marques 			table = realloc(table, sizeof(*table) * table_size);
1921da177e4SLinus Torvalds 			if (!table) {
1931da177e4SLinus Torvalds 				fprintf(stderr, "out of memory\n");
1941da177e4SLinus Torvalds 				exit (1);
1951da177e4SLinus Torvalds 			}
1961da177e4SLinus Torvalds 		}
197f2df3f65SPaulo Marques 		if (read_symbol(in, &table[table_cnt]) == 0) {
198f2df3f65SPaulo Marques 			table[table_cnt].start_pos = table_cnt;
199b3dbb4ecSPaulo Marques 			table_cnt++;
2001da177e4SLinus Torvalds 		}
2011da177e4SLinus Torvalds 	}
202f2df3f65SPaulo Marques }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static void output_label(char *label)
2051da177e4SLinus Torvalds {
20641f11a4fSYoshinori Sato 	if (symbol_prefix_char)
20741f11a4fSYoshinori Sato 		printf(".globl %c%s\n", symbol_prefix_char, label);
20841f11a4fSYoshinori Sato 	else
2091da177e4SLinus Torvalds 		printf(".globl %s\n", label);
2101da177e4SLinus Torvalds 	printf("\tALGN\n");
21141f11a4fSYoshinori Sato 	if (symbol_prefix_char)
21241f11a4fSYoshinori Sato 		printf("%c%s:\n", symbol_prefix_char, label);
21341f11a4fSYoshinori Sato 	else
2141da177e4SLinus Torvalds 		printf("%s:\n", label);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table
2181da177e4SLinus Torvalds  * might still be compressed itself, so the function needs to be recursive */
2191da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	int c, rlen, total=0;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	while (len) {
2241da177e4SLinus Torvalds 		c = *data;
2251da177e4SLinus Torvalds 		/* if the table holds a single char that is the same as the one
2261da177e4SLinus Torvalds 		 * we are looking for, then end the search */
2271da177e4SLinus Torvalds 		if (best_table[c][0]==c && best_table_len[c]==1) {
2281da177e4SLinus Torvalds 			*result++ = c;
2291da177e4SLinus Torvalds 			total++;
2301da177e4SLinus Torvalds 		} else {
2311da177e4SLinus Torvalds 			/* if not, recurse and expand */
2321da177e4SLinus Torvalds 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
2331da177e4SLinus Torvalds 			total += rlen;
2341da177e4SLinus Torvalds 			result += rlen;
2351da177e4SLinus Torvalds 		}
2361da177e4SLinus Torvalds 		data++;
2371da177e4SLinus Torvalds 		len--;
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds 	*result=0;
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	return total;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
244b3dbb4ecSPaulo Marques static void write_src(void)
2451da177e4SLinus Torvalds {
246b3dbb4ecSPaulo Marques 	unsigned int i, k, off;
2471da177e4SLinus Torvalds 	unsigned int best_idx[256];
2481da177e4SLinus Torvalds 	unsigned int *markers;
2499281aceaSTejun Heo 	char buf[KSYM_NAME_LEN];
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	printf("#include <asm/types.h>\n");
2521da177e4SLinus Torvalds 	printf("#if BITS_PER_LONG == 64\n");
2531da177e4SLinus Torvalds 	printf("#define PTR .quad\n");
2541da177e4SLinus Torvalds 	printf("#define ALGN .align 8\n");
2551da177e4SLinus Torvalds 	printf("#else\n");
2561da177e4SLinus Torvalds 	printf("#define PTR .long\n");
2571da177e4SLinus Torvalds 	printf("#define ALGN .align 4\n");
2581da177e4SLinus Torvalds 	printf("#endif\n");
2591da177e4SLinus Torvalds 
260aad09470SJan Beulich 	printf("\t.section .rodata, \"a\"\n");
2611da177e4SLinus Torvalds 
262fd593d12SEric W. Biederman 	/* Provide proper symbols relocatability by their '_text'
263fd593d12SEric W. Biederman 	 * relativeness.  The symbol names cannot be used to construct
264fd593d12SEric W. Biederman 	 * normal symbol references as the list of symbols contains
265fd593d12SEric W. Biederman 	 * symbols that are declared static and are private to their
266fd593d12SEric W. Biederman 	 * .o files.  This prevents .tmp_kallsyms.o or any other
267fd593d12SEric W. Biederman 	 * object from referencing them.
268fd593d12SEric W. Biederman 	 */
2691da177e4SLinus Torvalds 	output_label("kallsyms_addresses");
270b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
271fd593d12SEric W. Biederman 		if (toupper(table[i].sym[0]) != 'A') {
2722c22d8baSVivek Goyal 			if (_text <= table[i].addr)
273fd593d12SEric W. Biederman 				printf("\tPTR\t_text + %#llx\n",
274fd593d12SEric W. Biederman 					table[i].addr - _text);
2752c22d8baSVivek Goyal 			else
2762c22d8baSVivek Goyal 				printf("\tPTR\t_text - %#llx\n",
2772c22d8baSVivek Goyal 					_text - table[i].addr);
278fd593d12SEric W. Biederman 		} else {
2791da177e4SLinus Torvalds 			printf("\tPTR\t%#llx\n", table[i].addr);
2801da177e4SLinus Torvalds 		}
281fd593d12SEric W. Biederman 	}
2821da177e4SLinus Torvalds 	printf("\n");
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	output_label("kallsyms_num_syms");
285b3dbb4ecSPaulo Marques 	printf("\tPTR\t%d\n", table_cnt);
2861da177e4SLinus Torvalds 	printf("\n");
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/* table of offset markers, that give the offset in the compressed stream
2891da177e4SLinus Torvalds 	 * every 256 symbols */
290f1a136e0SJesper Juhl 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
291f1a136e0SJesper Juhl 	if (!markers) {
292f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
293f1a136e0SJesper Juhl 			"unable to allocate required memory\n");
294f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
295f1a136e0SJesper Juhl 	}
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	output_label("kallsyms_names");
2981da177e4SLinus Torvalds 	off = 0;
299b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
300b3dbb4ecSPaulo Marques 		if ((i & 0xFF) == 0)
301b3dbb4ecSPaulo Marques 			markers[i >> 8] = off;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 		printf("\t.byte 0x%02x", table[i].len);
3041da177e4SLinus Torvalds 		for (k = 0; k < table[i].len; k++)
3051da177e4SLinus Torvalds 			printf(", 0x%02x", table[i].sym[k]);
3061da177e4SLinus Torvalds 		printf("\n");
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 		off += table[i].len + 1;
3091da177e4SLinus Torvalds 	}
3101da177e4SLinus Torvalds 	printf("\n");
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	output_label("kallsyms_markers");
313b3dbb4ecSPaulo Marques 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
3141da177e4SLinus Torvalds 		printf("\tPTR\t%d\n", markers[i]);
3151da177e4SLinus Torvalds 	printf("\n");
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	free(markers);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	output_label("kallsyms_token_table");
3201da177e4SLinus Torvalds 	off = 0;
3211da177e4SLinus Torvalds 	for (i = 0; i < 256; i++) {
3221da177e4SLinus Torvalds 		best_idx[i] = off;
3231da177e4SLinus Torvalds 		expand_symbol(best_table[i], best_table_len[i], buf);
3241da177e4SLinus Torvalds 		printf("\t.asciz\t\"%s\"\n", buf);
3251da177e4SLinus Torvalds 		off += strlen(buf) + 1;
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 	printf("\n");
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	output_label("kallsyms_token_index");
3301da177e4SLinus Torvalds 	for (i = 0; i < 256; i++)
3311da177e4SLinus Torvalds 		printf("\t.short\t%d\n", best_idx[i]);
3321da177e4SLinus Torvalds 	printf("\n");
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds /* table lookup compression functions */
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /* count all the possible tokens in a symbol */
3391da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len)
3401da177e4SLinus Torvalds {
3411da177e4SLinus Torvalds 	int i;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
344b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */
3481da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len)
3491da177e4SLinus Torvalds {
3501da177e4SLinus Torvalds 	int i;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
353b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
356b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */
3571da177e4SLinus Torvalds static void build_initial_tok_table(void)
3581da177e4SLinus Torvalds {
359b3dbb4ecSPaulo Marques 	unsigned int i, pos;
3601da177e4SLinus Torvalds 
361b3dbb4ecSPaulo Marques 	pos = 0;
362b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
3631da177e4SLinus Torvalds 		if ( symbol_valid(&table[i]) ) {
364b3dbb4ecSPaulo Marques 			if (pos != i)
365b3dbb4ecSPaulo Marques 				table[pos] = table[i];
366b3dbb4ecSPaulo Marques 			learn_symbol(table[pos].sym, table[pos].len);
367b3dbb4ecSPaulo Marques 			pos++;
3681da177e4SLinus Torvalds 		}
3691da177e4SLinus Torvalds 	}
370b3dbb4ecSPaulo Marques 	table_cnt = pos;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
3737c5d249aSPaulo Marques static void *find_token(unsigned char *str, int len, unsigned char *token)
3747c5d249aSPaulo Marques {
3757c5d249aSPaulo Marques 	int i;
3767c5d249aSPaulo Marques 
3777c5d249aSPaulo Marques 	for (i = 0; i < len - 1; i++) {
3787c5d249aSPaulo Marques 		if (str[i] == token[0] && str[i+1] == token[1])
3797c5d249aSPaulo Marques 			return &str[i];
3807c5d249aSPaulo Marques 	}
3817c5d249aSPaulo Marques 	return NULL;
3827c5d249aSPaulo Marques }
3837c5d249aSPaulo Marques 
3841da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols
3851da177e4SLinus Torvalds  * to update the counts */
386b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx)
3871da177e4SLinus Torvalds {
388b3dbb4ecSPaulo Marques 	unsigned int i, len, size;
389b3dbb4ecSPaulo Marques 	unsigned char *p1, *p2;
3901da177e4SLinus Torvalds 
391b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		len = table[i].len;
394b3dbb4ecSPaulo Marques 		p1 = table[i].sym;
395b3dbb4ecSPaulo Marques 
396b3dbb4ecSPaulo Marques 		/* find the token on the symbol */
3977c5d249aSPaulo Marques 		p2 = find_token(p1, len, str);
398b3dbb4ecSPaulo Marques 		if (!p2) continue;
399b3dbb4ecSPaulo Marques 
400b3dbb4ecSPaulo Marques 		/* decrease the counts for this symbol's tokens */
401b3dbb4ecSPaulo Marques 		forget_symbol(table[i].sym, len);
402b3dbb4ecSPaulo Marques 
403b3dbb4ecSPaulo Marques 		size = len;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 		do {
406b3dbb4ecSPaulo Marques 			*p2 = idx;
407b3dbb4ecSPaulo Marques 			p2++;
408b3dbb4ecSPaulo Marques 			size -= (p2 - p1);
409b3dbb4ecSPaulo Marques 			memmove(p2, p2 + 1, size);
410b3dbb4ecSPaulo Marques 			p1 = p2;
411b3dbb4ecSPaulo Marques 			len--;
412b3dbb4ecSPaulo Marques 
413b3dbb4ecSPaulo Marques 			if (size < 2) break;
414b3dbb4ecSPaulo Marques 
4151da177e4SLinus Torvalds 			/* find the token on the symbol */
4167c5d249aSPaulo Marques 			p2 = find_token(p1, size, str);
4171da177e4SLinus Torvalds 
418b3dbb4ecSPaulo Marques 		} while (p2);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 		table[i].len = len;
421b3dbb4ecSPaulo Marques 
422b3dbb4ecSPaulo Marques 		/* increase the counts for this symbol's new tokens */
4231da177e4SLinus Torvalds 		learn_symbol(table[i].sym, len);
4241da177e4SLinus Torvalds 	}
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds /* search the token with the maximum profit */
428b3dbb4ecSPaulo Marques static int find_best_token(void)
4291da177e4SLinus Torvalds {
430b3dbb4ecSPaulo Marques 	int i, best, bestprofit;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	bestprofit=-10000;
433b3dbb4ecSPaulo Marques 	best = 0;
4341da177e4SLinus Torvalds 
435b3dbb4ecSPaulo Marques 	for (i = 0; i < 0x10000; i++) {
436b3dbb4ecSPaulo Marques 		if (token_profit[i] > bestprofit) {
437b3dbb4ecSPaulo Marques 			best = i;
438b3dbb4ecSPaulo Marques 			bestprofit = token_profit[i];
4391da177e4SLinus Torvalds 		}
4401da177e4SLinus Torvalds 	}
4411da177e4SLinus Torvalds 	return best;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */
4451da177e4SLinus Torvalds static void optimize_result(void)
4461da177e4SLinus Torvalds {
447b3dbb4ecSPaulo Marques 	int i, best;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	/* using the '\0' symbol last allows compress_symbols to use standard
4501da177e4SLinus Torvalds 	 * fast string functions */
4511da177e4SLinus Torvalds 	for (i = 255; i >= 0; i--) {
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 		/* if this table slot is empty (it is not used by an actual
4541da177e4SLinus Torvalds 		 * original char code */
4551da177e4SLinus Torvalds 		if (!best_table_len[i]) {
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 			/* find the token with the breates profit value */
4581da177e4SLinus Torvalds 			best = find_best_token();
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 			/* place it in the "best" table */
461b3dbb4ecSPaulo Marques 			best_table_len[i] = 2;
462b3dbb4ecSPaulo Marques 			best_table[i][0] = best & 0xFF;
463b3dbb4ecSPaulo Marques 			best_table[i][1] = (best >> 8) & 0xFF;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 			/* replace this token in all the valid symbols */
466b3dbb4ecSPaulo Marques 			compress_symbols(best_table[i], i);
4671da177e4SLinus Torvalds 		}
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */
4721da177e4SLinus Torvalds static void insert_real_symbols_in_table(void)
4731da177e4SLinus Torvalds {
474b3dbb4ecSPaulo Marques 	unsigned int i, j, c;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	memset(best_table, 0, sizeof(best_table));
4771da177e4SLinus Torvalds 	memset(best_table_len, 0, sizeof(best_table_len));
4781da177e4SLinus Torvalds 
479b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
4801da177e4SLinus Torvalds 		for (j = 0; j < table[i].len; j++) {
4811da177e4SLinus Torvalds 			c = table[i].sym[j];
4821da177e4SLinus Torvalds 			best_table[c][0]=c;
4831da177e4SLinus Torvalds 			best_table_len[c]=1;
4841da177e4SLinus Torvalds 		}
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds static void optimize_token_table(void)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	build_initial_tok_table();
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	insert_real_symbols_in_table();
4931da177e4SLinus Torvalds 
49441f11a4fSYoshinori Sato 	/* When valid symbol is not registered, exit to error */
495b3dbb4ecSPaulo Marques 	if (!table_cnt) {
49641f11a4fSYoshinori Sato 		fprintf(stderr, "No valid symbol.\n");
49741f11a4fSYoshinori Sato 		exit(1);
49841f11a4fSYoshinori Sato 	}
49941f11a4fSYoshinori Sato 
5001da177e4SLinus Torvalds 	optimize_result();
5011da177e4SLinus Torvalds }
5021da177e4SLinus Torvalds 
503f2df3f65SPaulo Marques static int compare_symbols(const void *a, const void *b)
504f2df3f65SPaulo Marques {
505f2df3f65SPaulo Marques 	const struct sym_entry *sa;
506f2df3f65SPaulo Marques 	const struct sym_entry *sb;
507f2df3f65SPaulo Marques 	int wa, wb;
508f2df3f65SPaulo Marques 
509f2df3f65SPaulo Marques 	sa = a;
510f2df3f65SPaulo Marques 	sb = b;
511f2df3f65SPaulo Marques 
512f2df3f65SPaulo Marques 	/* sort by address first */
513f2df3f65SPaulo Marques 	if (sa->addr > sb->addr)
514f2df3f65SPaulo Marques 		return 1;
515f2df3f65SPaulo Marques 	if (sa->addr < sb->addr)
516f2df3f65SPaulo Marques 		return -1;
517f2df3f65SPaulo Marques 
518f2df3f65SPaulo Marques 	/* sort by "weakness" type */
519f2df3f65SPaulo Marques 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
520f2df3f65SPaulo Marques 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
521f2df3f65SPaulo Marques 	if (wa != wb)
522f2df3f65SPaulo Marques 		return wa - wb;
523f2df3f65SPaulo Marques 
524f2df3f65SPaulo Marques 	/* sort by initial order, so that other symbols are left undisturbed */
525f2df3f65SPaulo Marques 	return sa->start_pos - sb->start_pos;
526f2df3f65SPaulo Marques }
527f2df3f65SPaulo Marques 
528f2df3f65SPaulo Marques static void sort_symbols(void)
529f2df3f65SPaulo Marques {
530f2df3f65SPaulo Marques 	qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
531f2df3f65SPaulo Marques }
5321da177e4SLinus Torvalds 
533b3dbb4ecSPaulo Marques int main(int argc, char **argv)
5341da177e4SLinus Torvalds {
53541f11a4fSYoshinori Sato 	if (argc >= 2) {
53641f11a4fSYoshinori Sato 		int i;
53741f11a4fSYoshinori Sato 		for (i = 1; i < argc; i++) {
53841f11a4fSYoshinori Sato 			if(strcmp(argv[i], "--all-symbols") == 0)
5391da177e4SLinus Torvalds 				all_symbols = 1;
54041f11a4fSYoshinori Sato 			else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
54141f11a4fSYoshinori Sato 				char *p = &argv[i][16];
54241f11a4fSYoshinori Sato 				/* skip quote */
54341f11a4fSYoshinori Sato 				if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
54441f11a4fSYoshinori Sato 					p++;
54541f11a4fSYoshinori Sato 				symbol_prefix_char = *p;
54641f11a4fSYoshinori Sato 			} else
54741f11a4fSYoshinori Sato 				usage();
54841f11a4fSYoshinori Sato 		}
54941f11a4fSYoshinori Sato 	} else if (argc != 1)
5501da177e4SLinus Torvalds 		usage();
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	read_map(stdin);
553f2df3f65SPaulo Marques 	sort_symbols();
5541da177e4SLinus Torvalds 	optimize_token_table();
5551da177e4SLinus Torvalds 	write_src();
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	return 0;
5581da177e4SLinus Torvalds }
559