xref: /linux/scripts/kallsyms.c (revision 2213e9a66bb87d8344a1256b4ef568220d9587fb)
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>
25*2213e9a6SArd Biesheuvel #include <limits.h>
261da177e4SLinus Torvalds 
2717b1f0deSMike Frysinger #ifndef ARRAY_SIZE
2817b1f0deSMike Frysinger #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
2917b1f0deSMike Frysinger #endif
3017b1f0deSMike Frysinger 
319281aceaSTejun Heo #define KSYM_NAME_LEN		128
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds struct sym_entry {
341da177e4SLinus Torvalds 	unsigned long long addr;
35b3dbb4ecSPaulo Marques 	unsigned int len;
36f2df3f65SPaulo Marques 	unsigned int start_pos;
371da177e4SLinus Torvalds 	unsigned char *sym;
388c996940SArd Biesheuvel 	unsigned int percpu_absolute;
391da177e4SLinus Torvalds };
401da177e4SLinus Torvalds 
4178eb7159SKees Cook struct addr_range {
4278eb7159SKees Cook 	const char *start_sym, *end_sym;
4317b1f0deSMike Frysinger 	unsigned long long start, end;
4417b1f0deSMike Frysinger };
4517b1f0deSMike Frysinger 
4617b1f0deSMike Frysinger static unsigned long long _text;
47*2213e9a6SArd Biesheuvel static unsigned long long relative_base;
4878eb7159SKees Cook static struct addr_range text_ranges[] = {
4917b1f0deSMike Frysinger 	{ "_stext",     "_etext"     },
5017b1f0deSMike Frysinger 	{ "_sinittext", "_einittext" },
5117b1f0deSMike Frysinger 	{ "_stext_l1",  "_etext_l1"  },	/* Blackfin on-chip L1 inst SRAM */
5217b1f0deSMike Frysinger 	{ "_stext_l2",  "_etext_l2"  },	/* Blackfin on-chip L2 SRAM */
5317b1f0deSMike Frysinger };
5417b1f0deSMike Frysinger #define text_range_text     (&text_ranges[0])
5517b1f0deSMike Frysinger #define text_range_inittext (&text_ranges[1])
5617b1f0deSMike Frysinger 
57c6bda7c9SRusty Russell static struct addr_range percpu_range = {
58c6bda7c9SRusty Russell 	"__per_cpu_start", "__per_cpu_end", -1ULL, 0
59c6bda7c9SRusty Russell };
60c6bda7c9SRusty Russell 
611da177e4SLinus Torvalds static struct sym_entry *table;
62b3dbb4ecSPaulo Marques static unsigned int table_size, table_cnt;
631da177e4SLinus Torvalds static int all_symbols = 0;
64c6bda7c9SRusty Russell static int absolute_percpu = 0;
6541f11a4fSYoshinori Sato static char symbol_prefix_char = '\0';
66f6537f2fSMing Lei static unsigned long long kernel_start_addr = 0;
67*2213e9a6SArd Biesheuvel static int base_relative = 0;
681da177e4SLinus Torvalds 
69b3dbb4ecSPaulo Marques int token_profit[0x10000];
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* the table that holds the result of the compression */
72b3dbb4ecSPaulo Marques unsigned char best_table[256][2];
731da177e4SLinus Torvalds unsigned char best_table_len[256];
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 
76b3dbb4ecSPaulo Marques static void usage(void)
771da177e4SLinus Torvalds {
78f6537f2fSMing Lei 	fprintf(stderr, "Usage: kallsyms [--all-symbols] "
79f6537f2fSMing Lei 			"[--symbol-prefix=<prefix char>] "
80f6537f2fSMing Lei 			"[--page-offset=<CONFIG_PAGE_OFFSET>] "
81*2213e9a6SArd Biesheuvel 			"[--base-relative] < in.map > out.S\n");
821da177e4SLinus Torvalds 	exit(1);
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds /*
861da177e4SLinus Torvalds  * This ignores the intensely annoying "mapping symbols" found
871da177e4SLinus Torvalds  * in ARM ELF files: $a, $t and $d.
881da177e4SLinus Torvalds  */
89b3dbb4ecSPaulo Marques static inline int is_arm_mapping_symbol(const char *str)
901da177e4SLinus Torvalds {
916c34f1f5SKyle McMartin 	return str[0] == '$' && strchr("axtd", str[1])
921da177e4SLinus Torvalds 	       && (str[2] == '\0' || str[2] == '.');
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
9578eb7159SKees Cook static int check_symbol_range(const char *sym, unsigned long long addr,
9678eb7159SKees Cook 			      struct addr_range *ranges, int entries)
9717b1f0deSMike Frysinger {
9817b1f0deSMike Frysinger 	size_t i;
9978eb7159SKees Cook 	struct addr_range *ar;
10017b1f0deSMike Frysinger 
10178eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
10278eb7159SKees Cook 		ar = &ranges[i];
10317b1f0deSMike Frysinger 
10478eb7159SKees Cook 		if (strcmp(sym, ar->start_sym) == 0) {
10578eb7159SKees Cook 			ar->start = addr;
10617b1f0deSMike Frysinger 			return 0;
10778eb7159SKees Cook 		} else if (strcmp(sym, ar->end_sym) == 0) {
10878eb7159SKees Cook 			ar->end = addr;
10917b1f0deSMike Frysinger 			return 0;
11017b1f0deSMike Frysinger 		}
11117b1f0deSMike Frysinger 	}
11217b1f0deSMike Frysinger 
11317b1f0deSMike Frysinger 	return 1;
11417b1f0deSMike Frysinger }
11517b1f0deSMike Frysinger 
116b3dbb4ecSPaulo Marques static int read_symbol(FILE *in, struct sym_entry *s)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	char str[500];
119b3dbb4ecSPaulo Marques 	char *sym, stype;
1201da177e4SLinus Torvalds 	int rc;
1211da177e4SLinus Torvalds 
122b3dbb4ecSPaulo Marques 	rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
1231da177e4SLinus Torvalds 	if (rc != 3) {
124ef894870SJean Sacren 		if (rc != EOF && fgets(str, 500, in) == NULL)
125ef894870SJean Sacren 			fprintf(stderr, "Read error or end of file.\n");
1261da177e4SLinus Torvalds 		return -1;
1271da177e4SLinus Torvalds 	}
128f3462aa9SAndi Kleen 	if (strlen(str) > KSYM_NAME_LEN) {
1296f62259bSFabio Estevam 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu vs %d).\n"
130f3462aa9SAndi Kleen 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
131f3462aa9SAndi Kleen 			str, strlen(str), KSYM_NAME_LEN);
132f3462aa9SAndi Kleen 		return -1;
133f3462aa9SAndi Kleen 	}
1341da177e4SLinus Torvalds 
13541f11a4fSYoshinori Sato 	sym = str;
13641f11a4fSYoshinori Sato 	/* skip prefix char */
13741f11a4fSYoshinori Sato 	if (symbol_prefix_char && str[0] == symbol_prefix_char)
13841f11a4fSYoshinori Sato 		sym++;
13941f11a4fSYoshinori Sato 
1401da177e4SLinus Torvalds 	/* Ignore most absolute/undefined (?) symbols. */
141fd593d12SEric W. Biederman 	if (strcmp(sym, "_text") == 0)
142fd593d12SEric W. Biederman 		_text = s->addr;
14378eb7159SKees Cook 	else if (check_symbol_range(sym, s->addr, text_ranges,
14478eb7159SKees Cook 				    ARRAY_SIZE(text_ranges)) == 0)
14517b1f0deSMike Frysinger 		/* nothing to do */;
146b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'A')
1471da177e4SLinus Torvalds 	{
1481da177e4SLinus Torvalds 		/* Keep these useful absolute symbols */
14941f11a4fSYoshinori Sato 		if (strcmp(sym, "__kernel_syscall_via_break") &&
15041f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_syscall_via_epc") &&
15141f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_sigtramp") &&
15241f11a4fSYoshinori Sato 		    strcmp(sym, "__gp"))
1531da177e4SLinus Torvalds 			return -1;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	}
156b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'U' ||
15741f11a4fSYoshinori Sato 		 is_arm_mapping_symbol(sym))
1581da177e4SLinus Torvalds 		return -1;
1596f00df24SRalf Baechle 	/* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
1606f00df24SRalf Baechle 	else if (str[0] == '$')
1616f00df24SRalf Baechle 		return -1;
162aab34ac8SSam Ravnborg 	/* exclude debugging symbols */
163aab34ac8SSam Ravnborg 	else if (stype == 'N')
164aab34ac8SSam Ravnborg 		return -1;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	/* include the type field in the symbol name, so that it gets
1671da177e4SLinus Torvalds 	 * compressed together */
1681da177e4SLinus Torvalds 	s->len = strlen(str) + 1;
169b3dbb4ecSPaulo Marques 	s->sym = malloc(s->len + 1);
170f1a136e0SJesper Juhl 	if (!s->sym) {
171f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
172f1a136e0SJesper Juhl 			"unable to allocate required amount of memory\n");
173f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
174f1a136e0SJesper Juhl 	}
175b3dbb4ecSPaulo Marques 	strcpy((char *)s->sym + 1, str);
176b3dbb4ecSPaulo Marques 	s->sym[0] = stype;
1771da177e4SLinus Torvalds 
1788c996940SArd Biesheuvel 	s->percpu_absolute = 0;
1798c996940SArd Biesheuvel 
180c6bda7c9SRusty Russell 	/* Record if we've found __per_cpu_start/end. */
181c6bda7c9SRusty Russell 	check_symbol_range(sym, s->addr, &percpu_range, 1);
182c6bda7c9SRusty Russell 
1831da177e4SLinus Torvalds 	return 0;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
18678eb7159SKees Cook static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
18778eb7159SKees Cook 			   int entries)
18817b1f0deSMike Frysinger {
18917b1f0deSMike Frysinger 	size_t i;
19078eb7159SKees Cook 	struct addr_range *ar;
19117b1f0deSMike Frysinger 
19278eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
19378eb7159SKees Cook 		ar = &ranges[i];
19417b1f0deSMike Frysinger 
19578eb7159SKees Cook 		if (s->addr >= ar->start && s->addr <= ar->end)
196ac6ca5c8SMike Frysinger 			return 1;
19717b1f0deSMike Frysinger 	}
19817b1f0deSMike Frysinger 
199ac6ca5c8SMike Frysinger 	return 0;
20017b1f0deSMike Frysinger }
20117b1f0deSMike Frysinger 
202b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	/* Symbols which vary between passes.  Passes 1 and 2 must have
2052ea03891SSam Ravnborg 	 * identical symbol lists.  The kallsyms_* symbols below are only added
2062ea03891SSam Ravnborg 	 * after pass 1, they would be included in pass 2 when --all-symbols is
2072ea03891SSam Ravnborg 	 * specified so exclude them to get a stable symbol list.
2081da177e4SLinus Torvalds 	 */
2091da177e4SLinus Torvalds 	static char *special_symbols[] = {
2102ea03891SSam Ravnborg 		"kallsyms_addresses",
211*2213e9a6SArd Biesheuvel 		"kallsyms_offsets",
212*2213e9a6SArd Biesheuvel 		"kallsyms_relative_base",
2132ea03891SSam Ravnborg 		"kallsyms_num_syms",
2142ea03891SSam Ravnborg 		"kallsyms_names",
2152ea03891SSam Ravnborg 		"kallsyms_markers",
2162ea03891SSam Ravnborg 		"kallsyms_token_table",
2172ea03891SSam Ravnborg 		"kallsyms_token_index",
2182ea03891SSam Ravnborg 
2191da177e4SLinus Torvalds 	/* Exclude linker generated symbols which vary between passes */
2201da177e4SLinus Torvalds 		"_SDA_BASE_",		/* ppc */
2211da177e4SLinus Torvalds 		"_SDA2_BASE_",		/* ppc */
2221da177e4SLinus Torvalds 		NULL };
223bd8b22d2SArd Biesheuvel 
224bd8b22d2SArd Biesheuvel 	static char *special_suffixes[] = {
225bd8b22d2SArd Biesheuvel 		"_veneer",		/* arm */
226bd8b22d2SArd Biesheuvel 		NULL };
227bd8b22d2SArd Biesheuvel 
2281da177e4SLinus Torvalds 	int i;
229bd8b22d2SArd Biesheuvel 	char *sym_name = (char *)s->sym + 1;
230bd8b22d2SArd Biesheuvel 
23141f11a4fSYoshinori Sato 
232f6537f2fSMing Lei 	if (s->addr < kernel_start_addr)
233f6537f2fSMing Lei 		return 0;
234f6537f2fSMing Lei 
23541f11a4fSYoshinori Sato 	/* skip prefix char */
236bd8b22d2SArd Biesheuvel 	if (symbol_prefix_char && *sym_name == symbol_prefix_char)
237bd8b22d2SArd Biesheuvel 		sym_name++;
238bd8b22d2SArd Biesheuvel 
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/* if --all-symbols is not specified, then symbols outside the text
2411da177e4SLinus Torvalds 	 * and inittext sections are discarded */
2421da177e4SLinus Torvalds 	if (!all_symbols) {
24378eb7159SKees Cook 		if (symbol_in_range(s, text_ranges,
24478eb7159SKees Cook 				    ARRAY_SIZE(text_ranges)) == 0)
2451da177e4SLinus Torvalds 			return 0;
2461da177e4SLinus Torvalds 		/* Corner case.  Discard any symbols with the same value as
247a3b81113SRobin Getz 		 * _etext _einittext; they can move between pass 1 and 2 when
248a3b81113SRobin Getz 		 * the kallsyms data are added.  If these symbols move then
249a3b81113SRobin Getz 		 * they may get dropped in pass 2, which breaks the kallsyms
250a3b81113SRobin Getz 		 * rules.
2511da177e4SLinus Torvalds 		 */
25217b1f0deSMike Frysinger 		if ((s->addr == text_range_text->end &&
253bd8b22d2SArd Biesheuvel 				strcmp(sym_name,
25478eb7159SKees Cook 				       text_range_text->end_sym)) ||
25517b1f0deSMike Frysinger 		    (s->addr == text_range_inittext->end &&
256bd8b22d2SArd Biesheuvel 				strcmp(sym_name,
25778eb7159SKees Cook 				       text_range_inittext->end_sym)))
2581da177e4SLinus Torvalds 			return 0;
2591da177e4SLinus Torvalds 	}
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	/* Exclude symbols which vary between passes. */
262bd8b22d2SArd Biesheuvel 	for (i = 0; special_symbols[i]; i++)
263bd8b22d2SArd Biesheuvel 		if (strcmp(sym_name, special_symbols[i]) == 0)
2641da177e4SLinus Torvalds 			return 0;
2651da177e4SLinus Torvalds 
266bd8b22d2SArd Biesheuvel 	for (i = 0; special_suffixes[i]; i++) {
267bd8b22d2SArd Biesheuvel 		int l = strlen(sym_name) - strlen(special_suffixes[i]);
268bd8b22d2SArd Biesheuvel 
269bd8b22d2SArd Biesheuvel 		if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0)
2701da177e4SLinus Torvalds 			return 0;
271bd8b22d2SArd Biesheuvel 	}
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	return 1;
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds 
276b3dbb4ecSPaulo Marques static void read_map(FILE *in)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	while (!feof(in)) {
279b3dbb4ecSPaulo Marques 		if (table_cnt >= table_size) {
280b3dbb4ecSPaulo Marques 			table_size += 10000;
281b3dbb4ecSPaulo Marques 			table = realloc(table, sizeof(*table) * table_size);
2821da177e4SLinus Torvalds 			if (!table) {
2831da177e4SLinus Torvalds 				fprintf(stderr, "out of memory\n");
2841da177e4SLinus Torvalds 				exit (1);
2851da177e4SLinus Torvalds 			}
2861da177e4SLinus Torvalds 		}
287f2df3f65SPaulo Marques 		if (read_symbol(in, &table[table_cnt]) == 0) {
288f2df3f65SPaulo Marques 			table[table_cnt].start_pos = table_cnt;
289b3dbb4ecSPaulo Marques 			table_cnt++;
2901da177e4SLinus Torvalds 		}
2911da177e4SLinus Torvalds 	}
292f2df3f65SPaulo Marques }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static void output_label(char *label)
2951da177e4SLinus Torvalds {
29641f11a4fSYoshinori Sato 	if (symbol_prefix_char)
29741f11a4fSYoshinori Sato 		printf(".globl %c%s\n", symbol_prefix_char, label);
29841f11a4fSYoshinori Sato 	else
2991da177e4SLinus Torvalds 		printf(".globl %s\n", label);
3001da177e4SLinus Torvalds 	printf("\tALGN\n");
30141f11a4fSYoshinori Sato 	if (symbol_prefix_char)
30241f11a4fSYoshinori Sato 		printf("%c%s:\n", symbol_prefix_char, label);
30341f11a4fSYoshinori Sato 	else
3041da177e4SLinus Torvalds 		printf("%s:\n", label);
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table
3081da177e4SLinus Torvalds  * might still be compressed itself, so the function needs to be recursive */
3091da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	int c, rlen, total=0;
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	while (len) {
3141da177e4SLinus Torvalds 		c = *data;
3151da177e4SLinus Torvalds 		/* if the table holds a single char that is the same as the one
3161da177e4SLinus Torvalds 		 * we are looking for, then end the search */
3171da177e4SLinus Torvalds 		if (best_table[c][0]==c && best_table_len[c]==1) {
3181da177e4SLinus Torvalds 			*result++ = c;
3191da177e4SLinus Torvalds 			total++;
3201da177e4SLinus Torvalds 		} else {
3211da177e4SLinus Torvalds 			/* if not, recurse and expand */
3221da177e4SLinus Torvalds 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
3231da177e4SLinus Torvalds 			total += rlen;
3241da177e4SLinus Torvalds 			result += rlen;
3251da177e4SLinus Torvalds 		}
3261da177e4SLinus Torvalds 		data++;
3271da177e4SLinus Torvalds 		len--;
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 	*result=0;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	return total;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
33478eb7159SKees Cook static int symbol_absolute(struct sym_entry *s)
33578eb7159SKees Cook {
3368c996940SArd Biesheuvel 	return s->percpu_absolute;
33778eb7159SKees Cook }
33878eb7159SKees Cook 
339b3dbb4ecSPaulo Marques static void write_src(void)
3401da177e4SLinus Torvalds {
341b3dbb4ecSPaulo Marques 	unsigned int i, k, off;
3421da177e4SLinus Torvalds 	unsigned int best_idx[256];
3431da177e4SLinus Torvalds 	unsigned int *markers;
3449281aceaSTejun Heo 	char buf[KSYM_NAME_LEN];
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	printf("#include <asm/types.h>\n");
3471da177e4SLinus Torvalds 	printf("#if BITS_PER_LONG == 64\n");
3481da177e4SLinus Torvalds 	printf("#define PTR .quad\n");
3491da177e4SLinus Torvalds 	printf("#define ALGN .align 8\n");
3501da177e4SLinus Torvalds 	printf("#else\n");
3511da177e4SLinus Torvalds 	printf("#define PTR .long\n");
3521da177e4SLinus Torvalds 	printf("#define ALGN .align 4\n");
3531da177e4SLinus Torvalds 	printf("#endif\n");
3541da177e4SLinus Torvalds 
355aad09470SJan Beulich 	printf("\t.section .rodata, \"a\"\n");
3561da177e4SLinus Torvalds 
357*2213e9a6SArd Biesheuvel 	/* Provide proper symbols relocatability by their relativeness
358*2213e9a6SArd Biesheuvel 	 * to a fixed anchor point in the runtime image, either '_text'
359*2213e9a6SArd Biesheuvel 	 * for absolute address tables, in which case the linker will
360*2213e9a6SArd Biesheuvel 	 * emit the final addresses at build time. Otherwise, use the
361*2213e9a6SArd Biesheuvel 	 * offset relative to the lowest value encountered of all relative
362*2213e9a6SArd Biesheuvel 	 * symbols, and emit non-relocatable fixed offsets that will be fixed
363*2213e9a6SArd Biesheuvel 	 * up at runtime.
364*2213e9a6SArd Biesheuvel 	 *
365*2213e9a6SArd Biesheuvel 	 * The symbol names cannot be used to construct normal symbol
366*2213e9a6SArd Biesheuvel 	 * references as the list of symbols contains symbols that are
367*2213e9a6SArd Biesheuvel 	 * declared static and are private to their .o files.  This prevents
368*2213e9a6SArd Biesheuvel 	 * .tmp_kallsyms.o or any other object from referencing them.
369fd593d12SEric W. Biederman 	 */
370*2213e9a6SArd Biesheuvel 	if (!base_relative)
3711da177e4SLinus Torvalds 		output_label("kallsyms_addresses");
372*2213e9a6SArd Biesheuvel 	else
373*2213e9a6SArd Biesheuvel 		output_label("kallsyms_offsets");
374*2213e9a6SArd Biesheuvel 
375b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
376*2213e9a6SArd Biesheuvel 		if (base_relative) {
377*2213e9a6SArd Biesheuvel 			long long offset;
378*2213e9a6SArd Biesheuvel 			int overflow;
379*2213e9a6SArd Biesheuvel 
380*2213e9a6SArd Biesheuvel 			if (!absolute_percpu) {
381*2213e9a6SArd Biesheuvel 				offset = table[i].addr - relative_base;
382*2213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > UINT_MAX);
383*2213e9a6SArd Biesheuvel 			} else if (symbol_absolute(&table[i])) {
384*2213e9a6SArd Biesheuvel 				offset = table[i].addr;
385*2213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > INT_MAX);
386*2213e9a6SArd Biesheuvel 			} else {
387*2213e9a6SArd Biesheuvel 				offset = relative_base - table[i].addr - 1;
388*2213e9a6SArd Biesheuvel 				overflow = (offset < INT_MIN || offset >= 0);
389*2213e9a6SArd Biesheuvel 			}
390*2213e9a6SArd Biesheuvel 			if (overflow) {
391*2213e9a6SArd Biesheuvel 				fprintf(stderr, "kallsyms failure: "
392*2213e9a6SArd Biesheuvel 					"%s symbol value %#llx out of range in relative mode\n",
393*2213e9a6SArd Biesheuvel 					symbol_absolute(&table[i]) ? "absolute" : "relative",
394*2213e9a6SArd Biesheuvel 					table[i].addr);
395*2213e9a6SArd Biesheuvel 				exit(EXIT_FAILURE);
396*2213e9a6SArd Biesheuvel 			}
397*2213e9a6SArd Biesheuvel 			printf("\t.long\t%#x\n", (int)offset);
398*2213e9a6SArd Biesheuvel 		} else if (!symbol_absolute(&table[i])) {
3992c22d8baSVivek Goyal 			if (_text <= table[i].addr)
400fd593d12SEric W. Biederman 				printf("\tPTR\t_text + %#llx\n",
401fd593d12SEric W. Biederman 					table[i].addr - _text);
4022c22d8baSVivek Goyal 			else
4032930ffc7SAndrew Morton 				printf("\tPTR\t_text - %#llx\n",
4042930ffc7SAndrew Morton 					_text - table[i].addr);
405fd593d12SEric W. Biederman 		} else {
4061da177e4SLinus Torvalds 			printf("\tPTR\t%#llx\n", table[i].addr);
4071da177e4SLinus Torvalds 		}
408fd593d12SEric W. Biederman 	}
4091da177e4SLinus Torvalds 	printf("\n");
4101da177e4SLinus Torvalds 
411*2213e9a6SArd Biesheuvel 	if (base_relative) {
412*2213e9a6SArd Biesheuvel 		output_label("kallsyms_relative_base");
413*2213e9a6SArd Biesheuvel 		printf("\tPTR\t_text - %#llx\n", _text - relative_base);
414*2213e9a6SArd Biesheuvel 		printf("\n");
415*2213e9a6SArd Biesheuvel 	}
416*2213e9a6SArd Biesheuvel 
4171da177e4SLinus Torvalds 	output_label("kallsyms_num_syms");
418b3dbb4ecSPaulo Marques 	printf("\tPTR\t%d\n", table_cnt);
4191da177e4SLinus Torvalds 	printf("\n");
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	/* table of offset markers, that give the offset in the compressed stream
4221da177e4SLinus Torvalds 	 * every 256 symbols */
423f1a136e0SJesper Juhl 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
424f1a136e0SJesper Juhl 	if (!markers) {
425f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
426f1a136e0SJesper Juhl 			"unable to allocate required memory\n");
427f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
428f1a136e0SJesper Juhl 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	output_label("kallsyms_names");
4311da177e4SLinus Torvalds 	off = 0;
432b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
433b3dbb4ecSPaulo Marques 		if ((i & 0xFF) == 0)
434b3dbb4ecSPaulo Marques 			markers[i >> 8] = off;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 		printf("\t.byte 0x%02x", table[i].len);
4371da177e4SLinus Torvalds 		for (k = 0; k < table[i].len; k++)
4381da177e4SLinus Torvalds 			printf(", 0x%02x", table[i].sym[k]);
4391da177e4SLinus Torvalds 		printf("\n");
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 		off += table[i].len + 1;
4421da177e4SLinus Torvalds 	}
4431da177e4SLinus Torvalds 	printf("\n");
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	output_label("kallsyms_markers");
446b3dbb4ecSPaulo Marques 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
4471da177e4SLinus Torvalds 		printf("\tPTR\t%d\n", markers[i]);
4481da177e4SLinus Torvalds 	printf("\n");
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	free(markers);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	output_label("kallsyms_token_table");
4531da177e4SLinus Torvalds 	off = 0;
4541da177e4SLinus Torvalds 	for (i = 0; i < 256; i++) {
4551da177e4SLinus Torvalds 		best_idx[i] = off;
4561da177e4SLinus Torvalds 		expand_symbol(best_table[i], best_table_len[i], buf);
4571da177e4SLinus Torvalds 		printf("\t.asciz\t\"%s\"\n", buf);
4581da177e4SLinus Torvalds 		off += strlen(buf) + 1;
4591da177e4SLinus Torvalds 	}
4601da177e4SLinus Torvalds 	printf("\n");
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	output_label("kallsyms_token_index");
4631da177e4SLinus Torvalds 	for (i = 0; i < 256; i++)
4641da177e4SLinus Torvalds 		printf("\t.short\t%d\n", best_idx[i]);
4651da177e4SLinus Torvalds 	printf("\n");
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /* table lookup compression functions */
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds /* count all the possible tokens in a symbol */
4721da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	int i;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
477b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */
4811da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len)
4821da177e4SLinus Torvalds {
4831da177e4SLinus Torvalds 	int i;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
486b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
489b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */
4901da177e4SLinus Torvalds static void build_initial_tok_table(void)
4911da177e4SLinus Torvalds {
492b3dbb4ecSPaulo Marques 	unsigned int i, pos;
4931da177e4SLinus Torvalds 
494b3dbb4ecSPaulo Marques 	pos = 0;
495b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
4961da177e4SLinus Torvalds 		if ( symbol_valid(&table[i]) ) {
497b3dbb4ecSPaulo Marques 			if (pos != i)
498b3dbb4ecSPaulo Marques 				table[pos] = table[i];
499b3dbb4ecSPaulo Marques 			learn_symbol(table[pos].sym, table[pos].len);
500b3dbb4ecSPaulo Marques 			pos++;
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 	}
503b3dbb4ecSPaulo Marques 	table_cnt = pos;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
5067c5d249aSPaulo Marques static void *find_token(unsigned char *str, int len, unsigned char *token)
5077c5d249aSPaulo Marques {
5087c5d249aSPaulo Marques 	int i;
5097c5d249aSPaulo Marques 
5107c5d249aSPaulo Marques 	for (i = 0; i < len - 1; i++) {
5117c5d249aSPaulo Marques 		if (str[i] == token[0] && str[i+1] == token[1])
5127c5d249aSPaulo Marques 			return &str[i];
5137c5d249aSPaulo Marques 	}
5147c5d249aSPaulo Marques 	return NULL;
5157c5d249aSPaulo Marques }
5167c5d249aSPaulo Marques 
5171da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols
5181da177e4SLinus Torvalds  * to update the counts */
519b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx)
5201da177e4SLinus Torvalds {
521b3dbb4ecSPaulo Marques 	unsigned int i, len, size;
522b3dbb4ecSPaulo Marques 	unsigned char *p1, *p2;
5231da177e4SLinus Torvalds 
524b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 		len = table[i].len;
527b3dbb4ecSPaulo Marques 		p1 = table[i].sym;
528b3dbb4ecSPaulo Marques 
529b3dbb4ecSPaulo Marques 		/* find the token on the symbol */
5307c5d249aSPaulo Marques 		p2 = find_token(p1, len, str);
531b3dbb4ecSPaulo Marques 		if (!p2) continue;
532b3dbb4ecSPaulo Marques 
533b3dbb4ecSPaulo Marques 		/* decrease the counts for this symbol's tokens */
534b3dbb4ecSPaulo Marques 		forget_symbol(table[i].sym, len);
535b3dbb4ecSPaulo Marques 
536b3dbb4ecSPaulo Marques 		size = len;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 		do {
539b3dbb4ecSPaulo Marques 			*p2 = idx;
540b3dbb4ecSPaulo Marques 			p2++;
541b3dbb4ecSPaulo Marques 			size -= (p2 - p1);
542b3dbb4ecSPaulo Marques 			memmove(p2, p2 + 1, size);
543b3dbb4ecSPaulo Marques 			p1 = p2;
544b3dbb4ecSPaulo Marques 			len--;
545b3dbb4ecSPaulo Marques 
546b3dbb4ecSPaulo Marques 			if (size < 2) break;
547b3dbb4ecSPaulo Marques 
5481da177e4SLinus Torvalds 			/* find the token on the symbol */
5497c5d249aSPaulo Marques 			p2 = find_token(p1, size, str);
5501da177e4SLinus Torvalds 
551b3dbb4ecSPaulo Marques 		} while (p2);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 		table[i].len = len;
554b3dbb4ecSPaulo Marques 
555b3dbb4ecSPaulo Marques 		/* increase the counts for this symbol's new tokens */
5561da177e4SLinus Torvalds 		learn_symbol(table[i].sym, len);
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds /* search the token with the maximum profit */
561b3dbb4ecSPaulo Marques static int find_best_token(void)
5621da177e4SLinus Torvalds {
563b3dbb4ecSPaulo Marques 	int i, best, bestprofit;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	bestprofit=-10000;
566b3dbb4ecSPaulo Marques 	best = 0;
5671da177e4SLinus Torvalds 
568b3dbb4ecSPaulo Marques 	for (i = 0; i < 0x10000; i++) {
569b3dbb4ecSPaulo Marques 		if (token_profit[i] > bestprofit) {
570b3dbb4ecSPaulo Marques 			best = i;
571b3dbb4ecSPaulo Marques 			bestprofit = token_profit[i];
5721da177e4SLinus Torvalds 		}
5731da177e4SLinus Torvalds 	}
5741da177e4SLinus Torvalds 	return best;
5751da177e4SLinus Torvalds }
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */
5781da177e4SLinus Torvalds static void optimize_result(void)
5791da177e4SLinus Torvalds {
580b3dbb4ecSPaulo Marques 	int i, best;
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	/* using the '\0' symbol last allows compress_symbols to use standard
5831da177e4SLinus Torvalds 	 * fast string functions */
5841da177e4SLinus Torvalds 	for (i = 255; i >= 0; i--) {
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 		/* if this table slot is empty (it is not used by an actual
5871da177e4SLinus Torvalds 		 * original char code */
5881da177e4SLinus Torvalds 		if (!best_table_len[i]) {
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 			/* find the token with the breates profit value */
5911da177e4SLinus Torvalds 			best = find_best_token();
592e0a04b11SXiaochen Wang 			if (token_profit[best] == 0)
593e0a04b11SXiaochen Wang 				break;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 			/* place it in the "best" table */
596b3dbb4ecSPaulo Marques 			best_table_len[i] = 2;
597b3dbb4ecSPaulo Marques 			best_table[i][0] = best & 0xFF;
598b3dbb4ecSPaulo Marques 			best_table[i][1] = (best >> 8) & 0xFF;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 			/* replace this token in all the valid symbols */
601b3dbb4ecSPaulo Marques 			compress_symbols(best_table[i], i);
6021da177e4SLinus Torvalds 		}
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */
6071da177e4SLinus Torvalds static void insert_real_symbols_in_table(void)
6081da177e4SLinus Torvalds {
609b3dbb4ecSPaulo Marques 	unsigned int i, j, c;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	memset(best_table, 0, sizeof(best_table));
6121da177e4SLinus Torvalds 	memset(best_table_len, 0, sizeof(best_table_len));
6131da177e4SLinus Torvalds 
614b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
6151da177e4SLinus Torvalds 		for (j = 0; j < table[i].len; j++) {
6161da177e4SLinus Torvalds 			c = table[i].sym[j];
6171da177e4SLinus Torvalds 			best_table[c][0]=c;
6181da177e4SLinus Torvalds 			best_table_len[c]=1;
6191da177e4SLinus Torvalds 		}
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds }
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds static void optimize_token_table(void)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	build_initial_tok_table();
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	insert_real_symbols_in_table();
6281da177e4SLinus Torvalds 
62941f11a4fSYoshinori Sato 	/* When valid symbol is not registered, exit to error */
630b3dbb4ecSPaulo Marques 	if (!table_cnt) {
63141f11a4fSYoshinori Sato 		fprintf(stderr, "No valid symbol.\n");
63241f11a4fSYoshinori Sato 		exit(1);
63341f11a4fSYoshinori Sato 	}
63441f11a4fSYoshinori Sato 
6351da177e4SLinus Torvalds 	optimize_result();
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
638b478b782SLai Jiangshan /* guess for "linker script provide" symbol */
639b478b782SLai Jiangshan static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
640b478b782SLai Jiangshan {
641b478b782SLai Jiangshan 	const char *symbol = (char *)se->sym + 1;
642b478b782SLai Jiangshan 	int len = se->len - 1;
643b478b782SLai Jiangshan 
644b478b782SLai Jiangshan 	if (len < 8)
645b478b782SLai Jiangshan 		return 0;
646b478b782SLai Jiangshan 
647b478b782SLai Jiangshan 	if (symbol[0] != '_' || symbol[1] != '_')
648b478b782SLai Jiangshan 		return 0;
649b478b782SLai Jiangshan 
650b478b782SLai Jiangshan 	/* __start_XXXXX */
651b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "start_", 6))
652b478b782SLai Jiangshan 		return 1;
653b478b782SLai Jiangshan 
654b478b782SLai Jiangshan 	/* __stop_XXXXX */
655b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "stop_", 5))
656b478b782SLai Jiangshan 		return 1;
657b478b782SLai Jiangshan 
658b478b782SLai Jiangshan 	/* __end_XXXXX */
659b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "end_", 4))
660b478b782SLai Jiangshan 		return 1;
661b478b782SLai Jiangshan 
662b478b782SLai Jiangshan 	/* __XXXXX_start */
663b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 6, "_start", 6))
664b478b782SLai Jiangshan 		return 1;
665b478b782SLai Jiangshan 
666b478b782SLai Jiangshan 	/* __XXXXX_end */
667b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 4, "_end", 4))
668b478b782SLai Jiangshan 		return 1;
669b478b782SLai Jiangshan 
670b478b782SLai Jiangshan 	return 0;
671b478b782SLai Jiangshan }
672b478b782SLai Jiangshan 
673b478b782SLai Jiangshan static int prefix_underscores_count(const char *str)
674b478b782SLai Jiangshan {
675b478b782SLai Jiangshan 	const char *tail = str;
676b478b782SLai Jiangshan 
677a9ece53cSPaul Mundt 	while (*tail == '_')
678b478b782SLai Jiangshan 		tail++;
679b478b782SLai Jiangshan 
680b478b782SLai Jiangshan 	return tail - str;
681b478b782SLai Jiangshan }
682b478b782SLai Jiangshan 
683f2df3f65SPaulo Marques static int compare_symbols(const void *a, const void *b)
684f2df3f65SPaulo Marques {
685f2df3f65SPaulo Marques 	const struct sym_entry *sa;
686f2df3f65SPaulo Marques 	const struct sym_entry *sb;
687f2df3f65SPaulo Marques 	int wa, wb;
688f2df3f65SPaulo Marques 
689f2df3f65SPaulo Marques 	sa = a;
690f2df3f65SPaulo Marques 	sb = b;
691f2df3f65SPaulo Marques 
692f2df3f65SPaulo Marques 	/* sort by address first */
693f2df3f65SPaulo Marques 	if (sa->addr > sb->addr)
694f2df3f65SPaulo Marques 		return 1;
695f2df3f65SPaulo Marques 	if (sa->addr < sb->addr)
696f2df3f65SPaulo Marques 		return -1;
697f2df3f65SPaulo Marques 
698f2df3f65SPaulo Marques 	/* sort by "weakness" type */
699f2df3f65SPaulo Marques 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
700f2df3f65SPaulo Marques 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
701f2df3f65SPaulo Marques 	if (wa != wb)
702f2df3f65SPaulo Marques 		return wa - wb;
703f2df3f65SPaulo Marques 
704b478b782SLai Jiangshan 	/* sort by "linker script provide" type */
705b478b782SLai Jiangshan 	wa = may_be_linker_script_provide_symbol(sa);
706b478b782SLai Jiangshan 	wb = may_be_linker_script_provide_symbol(sb);
707b478b782SLai Jiangshan 	if (wa != wb)
708b478b782SLai Jiangshan 		return wa - wb;
709b478b782SLai Jiangshan 
710b478b782SLai Jiangshan 	/* sort by the number of prefix underscores */
711b478b782SLai Jiangshan 	wa = prefix_underscores_count((const char *)sa->sym + 1);
712b478b782SLai Jiangshan 	wb = prefix_underscores_count((const char *)sb->sym + 1);
713b478b782SLai Jiangshan 	if (wa != wb)
714b478b782SLai Jiangshan 		return wa - wb;
715b478b782SLai Jiangshan 
716f2df3f65SPaulo Marques 	/* sort by initial order, so that other symbols are left undisturbed */
717f2df3f65SPaulo Marques 	return sa->start_pos - sb->start_pos;
718f2df3f65SPaulo Marques }
719f2df3f65SPaulo Marques 
720f2df3f65SPaulo Marques static void sort_symbols(void)
721f2df3f65SPaulo Marques {
722f2df3f65SPaulo Marques 	qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
723f2df3f65SPaulo Marques }
7241da177e4SLinus Torvalds 
725c6bda7c9SRusty Russell static void make_percpus_absolute(void)
726c6bda7c9SRusty Russell {
727c6bda7c9SRusty Russell 	unsigned int i;
728c6bda7c9SRusty Russell 
729c6bda7c9SRusty Russell 	for (i = 0; i < table_cnt; i++)
7308c996940SArd Biesheuvel 		if (symbol_in_range(&table[i], &percpu_range, 1)) {
7318c996940SArd Biesheuvel 			/*
7328c996940SArd Biesheuvel 			 * Keep the 'A' override for percpu symbols to
7338c996940SArd Biesheuvel 			 * ensure consistent behavior compared to older
7348c996940SArd Biesheuvel 			 * versions of this tool.
7358c996940SArd Biesheuvel 			 */
736c6bda7c9SRusty Russell 			table[i].sym[0] = 'A';
7378c996940SArd Biesheuvel 			table[i].percpu_absolute = 1;
7388c996940SArd Biesheuvel 		}
739c6bda7c9SRusty Russell }
740c6bda7c9SRusty Russell 
741*2213e9a6SArd Biesheuvel /* find the minimum non-absolute symbol address */
742*2213e9a6SArd Biesheuvel static void record_relative_base(void)
743*2213e9a6SArd Biesheuvel {
744*2213e9a6SArd Biesheuvel 	unsigned int i;
745*2213e9a6SArd Biesheuvel 
746*2213e9a6SArd Biesheuvel 	relative_base = -1ULL;
747*2213e9a6SArd Biesheuvel 	for (i = 0; i < table_cnt; i++)
748*2213e9a6SArd Biesheuvel 		if (!symbol_absolute(&table[i]) &&
749*2213e9a6SArd Biesheuvel 		    table[i].addr < relative_base)
750*2213e9a6SArd Biesheuvel 			relative_base = table[i].addr;
751*2213e9a6SArd Biesheuvel }
752*2213e9a6SArd Biesheuvel 
753b3dbb4ecSPaulo Marques int main(int argc, char **argv)
7541da177e4SLinus Torvalds {
75541f11a4fSYoshinori Sato 	if (argc >= 2) {
75641f11a4fSYoshinori Sato 		int i;
75741f11a4fSYoshinori Sato 		for (i = 1; i < argc; i++) {
75841f11a4fSYoshinori Sato 			if(strcmp(argv[i], "--all-symbols") == 0)
7591da177e4SLinus Torvalds 				all_symbols = 1;
760c6bda7c9SRusty Russell 			else if (strcmp(argv[i], "--absolute-percpu") == 0)
761c6bda7c9SRusty Russell 				absolute_percpu = 1;
76241f11a4fSYoshinori Sato 			else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
76341f11a4fSYoshinori Sato 				char *p = &argv[i][16];
76441f11a4fSYoshinori Sato 				/* skip quote */
76541f11a4fSYoshinori Sato 				if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
76641f11a4fSYoshinori Sato 					p++;
76741f11a4fSYoshinori Sato 				symbol_prefix_char = *p;
768f6537f2fSMing Lei 			} else if (strncmp(argv[i], "--page-offset=", 14) == 0) {
769f6537f2fSMing Lei 				const char *p = &argv[i][14];
770f6537f2fSMing Lei 				kernel_start_addr = strtoull(p, NULL, 16);
771*2213e9a6SArd Biesheuvel 			} else if (strcmp(argv[i], "--base-relative") == 0)
772*2213e9a6SArd Biesheuvel 				base_relative = 1;
773*2213e9a6SArd Biesheuvel 			else
77441f11a4fSYoshinori Sato 				usage();
77541f11a4fSYoshinori Sato 		}
77641f11a4fSYoshinori Sato 	} else if (argc != 1)
7771da177e4SLinus Torvalds 		usage();
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	read_map(stdin);
780c6bda7c9SRusty Russell 	if (absolute_percpu)
781c6bda7c9SRusty Russell 		make_percpus_absolute();
782*2213e9a6SArd Biesheuvel 	if (base_relative)
783*2213e9a6SArd Biesheuvel 		record_relative_base();
784f2df3f65SPaulo Marques 	sort_symbols();
7851da177e4SLinus Torvalds 	optimize_token_table();
7861da177e4SLinus Torvalds 	write_src();
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	return 0;
7891da177e4SLinus Torvalds }
790