xref: /linux/scripts/kallsyms.c (revision 51962a9d437f0d580c04cd2c4abc2bd417200da2)
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>
252213e9a6SArd 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;
472213e9a6SArd 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';
662213e9a6SArd Biesheuvel static int base_relative = 0;
671da177e4SLinus Torvalds 
68b3dbb4ecSPaulo Marques int token_profit[0x10000];
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /* the table that holds the result of the compression */
71b3dbb4ecSPaulo Marques unsigned char best_table[256][2];
721da177e4SLinus Torvalds unsigned char best_table_len[256];
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds 
75b3dbb4ecSPaulo Marques static void usage(void)
761da177e4SLinus Torvalds {
77f6537f2fSMing Lei 	fprintf(stderr, "Usage: kallsyms [--all-symbols] "
78f6537f2fSMing Lei 			"[--symbol-prefix=<prefix char>] "
792213e9a6SArd Biesheuvel 			"[--base-relative] < in.map > out.S\n");
801da177e4SLinus Torvalds 	exit(1);
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * This ignores the intensely annoying "mapping symbols" found
851da177e4SLinus Torvalds  * in ARM ELF files: $a, $t and $d.
861da177e4SLinus Torvalds  */
87b3dbb4ecSPaulo Marques static inline int is_arm_mapping_symbol(const char *str)
881da177e4SLinus Torvalds {
896c34f1f5SKyle McMartin 	return str[0] == '$' && strchr("axtd", str[1])
901da177e4SLinus Torvalds 	       && (str[2] == '\0' || str[2] == '.');
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds 
9378eb7159SKees Cook static int check_symbol_range(const char *sym, unsigned long long addr,
9478eb7159SKees Cook 			      struct addr_range *ranges, int entries)
9517b1f0deSMike Frysinger {
9617b1f0deSMike Frysinger 	size_t i;
9778eb7159SKees Cook 	struct addr_range *ar;
9817b1f0deSMike Frysinger 
9978eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
10078eb7159SKees Cook 		ar = &ranges[i];
10117b1f0deSMike Frysinger 
10278eb7159SKees Cook 		if (strcmp(sym, ar->start_sym) == 0) {
10378eb7159SKees Cook 			ar->start = addr;
10417b1f0deSMike Frysinger 			return 0;
10578eb7159SKees Cook 		} else if (strcmp(sym, ar->end_sym) == 0) {
10678eb7159SKees Cook 			ar->end = addr;
10717b1f0deSMike Frysinger 			return 0;
10817b1f0deSMike Frysinger 		}
10917b1f0deSMike Frysinger 	}
11017b1f0deSMike Frysinger 
11117b1f0deSMike Frysinger 	return 1;
11217b1f0deSMike Frysinger }
11317b1f0deSMike Frysinger 
114b3dbb4ecSPaulo Marques static int read_symbol(FILE *in, struct sym_entry *s)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	char str[500];
117b3dbb4ecSPaulo Marques 	char *sym, stype;
1181da177e4SLinus Torvalds 	int rc;
1191da177e4SLinus Torvalds 
120b3dbb4ecSPaulo Marques 	rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
1211da177e4SLinus Torvalds 	if (rc != 3) {
122ef894870SJean Sacren 		if (rc != EOF && fgets(str, 500, in) == NULL)
123ef894870SJean Sacren 			fprintf(stderr, "Read error or end of file.\n");
1241da177e4SLinus Torvalds 		return -1;
1251da177e4SLinus Torvalds 	}
126f3462aa9SAndi Kleen 	if (strlen(str) > KSYM_NAME_LEN) {
1276f62259bSFabio Estevam 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu vs %d).\n"
128f3462aa9SAndi Kleen 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
129f3462aa9SAndi Kleen 			str, strlen(str), KSYM_NAME_LEN);
130f3462aa9SAndi Kleen 		return -1;
131f3462aa9SAndi Kleen 	}
1321da177e4SLinus Torvalds 
13341f11a4fSYoshinori Sato 	sym = str;
13441f11a4fSYoshinori Sato 	/* skip prefix char */
13541f11a4fSYoshinori Sato 	if (symbol_prefix_char && str[0] == symbol_prefix_char)
13641f11a4fSYoshinori Sato 		sym++;
13741f11a4fSYoshinori Sato 
1381da177e4SLinus Torvalds 	/* Ignore most absolute/undefined (?) symbols. */
139fd593d12SEric W. Biederman 	if (strcmp(sym, "_text") == 0)
140fd593d12SEric W. Biederman 		_text = s->addr;
14178eb7159SKees Cook 	else if (check_symbol_range(sym, s->addr, text_ranges,
14278eb7159SKees Cook 				    ARRAY_SIZE(text_ranges)) == 0)
14317b1f0deSMike Frysinger 		/* nothing to do */;
144b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'A')
1451da177e4SLinus Torvalds 	{
1461da177e4SLinus Torvalds 		/* Keep these useful absolute symbols */
14741f11a4fSYoshinori Sato 		if (strcmp(sym, "__kernel_syscall_via_break") &&
14841f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_syscall_via_epc") &&
14941f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_sigtramp") &&
15041f11a4fSYoshinori Sato 		    strcmp(sym, "__gp"))
1511da177e4SLinus Torvalds 			return -1;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	}
154b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'U' ||
15541f11a4fSYoshinori Sato 		 is_arm_mapping_symbol(sym))
1561da177e4SLinus Torvalds 		return -1;
1576f00df24SRalf Baechle 	/* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
1586f00df24SRalf Baechle 	else if (str[0] == '$')
1596f00df24SRalf Baechle 		return -1;
160aab34ac8SSam Ravnborg 	/* exclude debugging symbols */
161*51962a9dSGuenter Roeck 	else if (stype == 'N' || stype == 'n')
162aab34ac8SSam Ravnborg 		return -1;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	/* include the type field in the symbol name, so that it gets
1651da177e4SLinus Torvalds 	 * compressed together */
1661da177e4SLinus Torvalds 	s->len = strlen(str) + 1;
167b3dbb4ecSPaulo Marques 	s->sym = malloc(s->len + 1);
168f1a136e0SJesper Juhl 	if (!s->sym) {
169f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
170f1a136e0SJesper Juhl 			"unable to allocate required amount of memory\n");
171f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
172f1a136e0SJesper Juhl 	}
173b3dbb4ecSPaulo Marques 	strcpy((char *)s->sym + 1, str);
174b3dbb4ecSPaulo Marques 	s->sym[0] = stype;
1751da177e4SLinus Torvalds 
1768c996940SArd Biesheuvel 	s->percpu_absolute = 0;
1778c996940SArd Biesheuvel 
178c6bda7c9SRusty Russell 	/* Record if we've found __per_cpu_start/end. */
179c6bda7c9SRusty Russell 	check_symbol_range(sym, s->addr, &percpu_range, 1);
180c6bda7c9SRusty Russell 
1811da177e4SLinus Torvalds 	return 0;
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
18478eb7159SKees Cook static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
18578eb7159SKees Cook 			   int entries)
18617b1f0deSMike Frysinger {
18717b1f0deSMike Frysinger 	size_t i;
18878eb7159SKees Cook 	struct addr_range *ar;
18917b1f0deSMike Frysinger 
19078eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
19178eb7159SKees Cook 		ar = &ranges[i];
19217b1f0deSMike Frysinger 
19378eb7159SKees Cook 		if (s->addr >= ar->start && s->addr <= ar->end)
194ac6ca5c8SMike Frysinger 			return 1;
19517b1f0deSMike Frysinger 	}
19617b1f0deSMike Frysinger 
197ac6ca5c8SMike Frysinger 	return 0;
19817b1f0deSMike Frysinger }
19917b1f0deSMike Frysinger 
200b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	/* Symbols which vary between passes.  Passes 1 and 2 must have
2032ea03891SSam Ravnborg 	 * identical symbol lists.  The kallsyms_* symbols below are only added
2042ea03891SSam Ravnborg 	 * after pass 1, they would be included in pass 2 when --all-symbols is
2052ea03891SSam Ravnborg 	 * specified so exclude them to get a stable symbol list.
2061da177e4SLinus Torvalds 	 */
2071da177e4SLinus Torvalds 	static char *special_symbols[] = {
2082ea03891SSam Ravnborg 		"kallsyms_addresses",
2092213e9a6SArd Biesheuvel 		"kallsyms_offsets",
2102213e9a6SArd Biesheuvel 		"kallsyms_relative_base",
2112ea03891SSam Ravnborg 		"kallsyms_num_syms",
2122ea03891SSam Ravnborg 		"kallsyms_names",
2132ea03891SSam Ravnborg 		"kallsyms_markers",
2142ea03891SSam Ravnborg 		"kallsyms_token_table",
2152ea03891SSam Ravnborg 		"kallsyms_token_index",
2162ea03891SSam Ravnborg 
2171da177e4SLinus Torvalds 	/* Exclude linker generated symbols which vary between passes */
2181da177e4SLinus Torvalds 		"_SDA_BASE_",		/* ppc */
2191da177e4SLinus Torvalds 		"_SDA2_BASE_",		/* ppc */
2201da177e4SLinus Torvalds 		NULL };
221bd8b22d2SArd Biesheuvel 
22256067812SArd Biesheuvel 	static char *special_prefixes[] = {
22356067812SArd Biesheuvel 		"__crc_",		/* modversions */
22456067812SArd Biesheuvel 		NULL };
22556067812SArd Biesheuvel 
226bd8b22d2SArd Biesheuvel 	static char *special_suffixes[] = {
227bd8b22d2SArd Biesheuvel 		"_veneer",		/* arm */
228b9b74be1SArd Biesheuvel 		"_from_arm",		/* arm */
229b9b74be1SArd Biesheuvel 		"_from_thumb",		/* arm */
230bd8b22d2SArd Biesheuvel 		NULL };
231bd8b22d2SArd Biesheuvel 
2321da177e4SLinus Torvalds 	int i;
233bd8b22d2SArd Biesheuvel 	char *sym_name = (char *)s->sym + 1;
234bd8b22d2SArd Biesheuvel 
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 
26656067812SArd Biesheuvel 	for (i = 0; special_prefixes[i]; i++) {
26756067812SArd Biesheuvel 		int l = strlen(special_prefixes[i]);
26856067812SArd Biesheuvel 
26956067812SArd Biesheuvel 		if (l <= strlen(sym_name) &&
27056067812SArd Biesheuvel 		    strncmp(sym_name, special_prefixes[i], l) == 0)
27156067812SArd Biesheuvel 			return 0;
27256067812SArd Biesheuvel 	}
27356067812SArd Biesheuvel 
274bd8b22d2SArd Biesheuvel 	for (i = 0; special_suffixes[i]; i++) {
275bd8b22d2SArd Biesheuvel 		int l = strlen(sym_name) - strlen(special_suffixes[i]);
276bd8b22d2SArd Biesheuvel 
277bd8b22d2SArd Biesheuvel 		if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0)
2781da177e4SLinus Torvalds 			return 0;
279bd8b22d2SArd Biesheuvel 	}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	return 1;
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
284b3dbb4ecSPaulo Marques static void read_map(FILE *in)
2851da177e4SLinus Torvalds {
2861da177e4SLinus Torvalds 	while (!feof(in)) {
287b3dbb4ecSPaulo Marques 		if (table_cnt >= table_size) {
288b3dbb4ecSPaulo Marques 			table_size += 10000;
289b3dbb4ecSPaulo Marques 			table = realloc(table, sizeof(*table) * table_size);
2901da177e4SLinus Torvalds 			if (!table) {
2911da177e4SLinus Torvalds 				fprintf(stderr, "out of memory\n");
2921da177e4SLinus Torvalds 				exit (1);
2931da177e4SLinus Torvalds 			}
2941da177e4SLinus Torvalds 		}
295f2df3f65SPaulo Marques 		if (read_symbol(in, &table[table_cnt]) == 0) {
296f2df3f65SPaulo Marques 			table[table_cnt].start_pos = table_cnt;
297b3dbb4ecSPaulo Marques 			table_cnt++;
2981da177e4SLinus Torvalds 		}
2991da177e4SLinus Torvalds 	}
300f2df3f65SPaulo Marques }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds static void output_label(char *label)
3031da177e4SLinus Torvalds {
30441f11a4fSYoshinori Sato 	if (symbol_prefix_char)
30541f11a4fSYoshinori Sato 		printf(".globl %c%s\n", symbol_prefix_char, label);
30641f11a4fSYoshinori Sato 	else
3071da177e4SLinus Torvalds 		printf(".globl %s\n", label);
3081da177e4SLinus Torvalds 	printf("\tALGN\n");
30941f11a4fSYoshinori Sato 	if (symbol_prefix_char)
31041f11a4fSYoshinori Sato 		printf("%c%s:\n", symbol_prefix_char, label);
31141f11a4fSYoshinori Sato 	else
3121da177e4SLinus Torvalds 		printf("%s:\n", label);
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table
3161da177e4SLinus Torvalds  * might still be compressed itself, so the function needs to be recursive */
3171da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	int c, rlen, total=0;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	while (len) {
3221da177e4SLinus Torvalds 		c = *data;
3231da177e4SLinus Torvalds 		/* if the table holds a single char that is the same as the one
3241da177e4SLinus Torvalds 		 * we are looking for, then end the search */
3251da177e4SLinus Torvalds 		if (best_table[c][0]==c && best_table_len[c]==1) {
3261da177e4SLinus Torvalds 			*result++ = c;
3271da177e4SLinus Torvalds 			total++;
3281da177e4SLinus Torvalds 		} else {
3291da177e4SLinus Torvalds 			/* if not, recurse and expand */
3301da177e4SLinus Torvalds 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
3311da177e4SLinus Torvalds 			total += rlen;
3321da177e4SLinus Torvalds 			result += rlen;
3331da177e4SLinus Torvalds 		}
3341da177e4SLinus Torvalds 		data++;
3351da177e4SLinus Torvalds 		len--;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 	*result=0;
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	return total;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
34278eb7159SKees Cook static int symbol_absolute(struct sym_entry *s)
34378eb7159SKees Cook {
3448c996940SArd Biesheuvel 	return s->percpu_absolute;
34578eb7159SKees Cook }
34678eb7159SKees Cook 
347b3dbb4ecSPaulo Marques static void write_src(void)
3481da177e4SLinus Torvalds {
349b3dbb4ecSPaulo Marques 	unsigned int i, k, off;
3501da177e4SLinus Torvalds 	unsigned int best_idx[256];
3511da177e4SLinus Torvalds 	unsigned int *markers;
3529281aceaSTejun Heo 	char buf[KSYM_NAME_LEN];
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	printf("#include <asm/types.h>\n");
3551da177e4SLinus Torvalds 	printf("#if BITS_PER_LONG == 64\n");
3561da177e4SLinus Torvalds 	printf("#define PTR .quad\n");
3571da177e4SLinus Torvalds 	printf("#define ALGN .align 8\n");
3581da177e4SLinus Torvalds 	printf("#else\n");
3591da177e4SLinus Torvalds 	printf("#define PTR .long\n");
3601da177e4SLinus Torvalds 	printf("#define ALGN .align 4\n");
3611da177e4SLinus Torvalds 	printf("#endif\n");
3621da177e4SLinus Torvalds 
363aad09470SJan Beulich 	printf("\t.section .rodata, \"a\"\n");
3641da177e4SLinus Torvalds 
3652213e9a6SArd Biesheuvel 	/* Provide proper symbols relocatability by their relativeness
3662213e9a6SArd Biesheuvel 	 * to a fixed anchor point in the runtime image, either '_text'
3672213e9a6SArd Biesheuvel 	 * for absolute address tables, in which case the linker will
3682213e9a6SArd Biesheuvel 	 * emit the final addresses at build time. Otherwise, use the
3692213e9a6SArd Biesheuvel 	 * offset relative to the lowest value encountered of all relative
3702213e9a6SArd Biesheuvel 	 * symbols, and emit non-relocatable fixed offsets that will be fixed
3712213e9a6SArd Biesheuvel 	 * up at runtime.
3722213e9a6SArd Biesheuvel 	 *
3732213e9a6SArd Biesheuvel 	 * The symbol names cannot be used to construct normal symbol
3742213e9a6SArd Biesheuvel 	 * references as the list of symbols contains symbols that are
3752213e9a6SArd Biesheuvel 	 * declared static and are private to their .o files.  This prevents
3762213e9a6SArd Biesheuvel 	 * .tmp_kallsyms.o or any other object from referencing them.
377fd593d12SEric W. Biederman 	 */
3782213e9a6SArd Biesheuvel 	if (!base_relative)
3791da177e4SLinus Torvalds 		output_label("kallsyms_addresses");
3802213e9a6SArd Biesheuvel 	else
3812213e9a6SArd Biesheuvel 		output_label("kallsyms_offsets");
3822213e9a6SArd Biesheuvel 
383b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
3842213e9a6SArd Biesheuvel 		if (base_relative) {
3852213e9a6SArd Biesheuvel 			long long offset;
3862213e9a6SArd Biesheuvel 			int overflow;
3872213e9a6SArd Biesheuvel 
3882213e9a6SArd Biesheuvel 			if (!absolute_percpu) {
3892213e9a6SArd Biesheuvel 				offset = table[i].addr - relative_base;
3902213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > UINT_MAX);
3912213e9a6SArd Biesheuvel 			} else if (symbol_absolute(&table[i])) {
3922213e9a6SArd Biesheuvel 				offset = table[i].addr;
3932213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > INT_MAX);
3942213e9a6SArd Biesheuvel 			} else {
3952213e9a6SArd Biesheuvel 				offset = relative_base - table[i].addr - 1;
3962213e9a6SArd Biesheuvel 				overflow = (offset < INT_MIN || offset >= 0);
3972213e9a6SArd Biesheuvel 			}
3982213e9a6SArd Biesheuvel 			if (overflow) {
3992213e9a6SArd Biesheuvel 				fprintf(stderr, "kallsyms failure: "
4002213e9a6SArd Biesheuvel 					"%s symbol value %#llx out of range in relative mode\n",
4012213e9a6SArd Biesheuvel 					symbol_absolute(&table[i]) ? "absolute" : "relative",
4022213e9a6SArd Biesheuvel 					table[i].addr);
4032213e9a6SArd Biesheuvel 				exit(EXIT_FAILURE);
4042213e9a6SArd Biesheuvel 			}
4052213e9a6SArd Biesheuvel 			printf("\t.long\t%#x\n", (int)offset);
4062213e9a6SArd Biesheuvel 		} else if (!symbol_absolute(&table[i])) {
4072c22d8baSVivek Goyal 			if (_text <= table[i].addr)
408fd593d12SEric W. Biederman 				printf("\tPTR\t_text + %#llx\n",
409fd593d12SEric W. Biederman 					table[i].addr - _text);
4102c22d8baSVivek Goyal 			else
4112930ffc7SAndrew Morton 				printf("\tPTR\t_text - %#llx\n",
4122930ffc7SAndrew Morton 					_text - table[i].addr);
413fd593d12SEric W. Biederman 		} else {
4141da177e4SLinus Torvalds 			printf("\tPTR\t%#llx\n", table[i].addr);
4151da177e4SLinus Torvalds 		}
416fd593d12SEric W. Biederman 	}
4171da177e4SLinus Torvalds 	printf("\n");
4181da177e4SLinus Torvalds 
4192213e9a6SArd Biesheuvel 	if (base_relative) {
4202213e9a6SArd Biesheuvel 		output_label("kallsyms_relative_base");
4212213e9a6SArd Biesheuvel 		printf("\tPTR\t_text - %#llx\n", _text - relative_base);
4222213e9a6SArd Biesheuvel 		printf("\n");
4232213e9a6SArd Biesheuvel 	}
4242213e9a6SArd Biesheuvel 
4251da177e4SLinus Torvalds 	output_label("kallsyms_num_syms");
426b3dbb4ecSPaulo Marques 	printf("\tPTR\t%d\n", table_cnt);
4271da177e4SLinus Torvalds 	printf("\n");
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	/* table of offset markers, that give the offset in the compressed stream
4301da177e4SLinus Torvalds 	 * every 256 symbols */
431f1a136e0SJesper Juhl 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
432f1a136e0SJesper Juhl 	if (!markers) {
433f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
434f1a136e0SJesper Juhl 			"unable to allocate required memory\n");
435f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
436f1a136e0SJesper Juhl 	}
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	output_label("kallsyms_names");
4391da177e4SLinus Torvalds 	off = 0;
440b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
441b3dbb4ecSPaulo Marques 		if ((i & 0xFF) == 0)
442b3dbb4ecSPaulo Marques 			markers[i >> 8] = off;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 		printf("\t.byte 0x%02x", table[i].len);
4451da177e4SLinus Torvalds 		for (k = 0; k < table[i].len; k++)
4461da177e4SLinus Torvalds 			printf(", 0x%02x", table[i].sym[k]);
4471da177e4SLinus Torvalds 		printf("\n");
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 		off += table[i].len + 1;
4501da177e4SLinus Torvalds 	}
4511da177e4SLinus Torvalds 	printf("\n");
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	output_label("kallsyms_markers");
454b3dbb4ecSPaulo Marques 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
4551da177e4SLinus Torvalds 		printf("\tPTR\t%d\n", markers[i]);
4561da177e4SLinus Torvalds 	printf("\n");
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	free(markers);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	output_label("kallsyms_token_table");
4611da177e4SLinus Torvalds 	off = 0;
4621da177e4SLinus Torvalds 	for (i = 0; i < 256; i++) {
4631da177e4SLinus Torvalds 		best_idx[i] = off;
4641da177e4SLinus Torvalds 		expand_symbol(best_table[i], best_table_len[i], buf);
4651da177e4SLinus Torvalds 		printf("\t.asciz\t\"%s\"\n", buf);
4661da177e4SLinus Torvalds 		off += strlen(buf) + 1;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 	printf("\n");
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	output_label("kallsyms_token_index");
4711da177e4SLinus Torvalds 	for (i = 0; i < 256; i++)
4721da177e4SLinus Torvalds 		printf("\t.short\t%d\n", best_idx[i]);
4731da177e4SLinus Torvalds 	printf("\n");
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* table lookup compression functions */
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /* count all the possible tokens in a symbol */
4801da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	int i;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
485b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */
4891da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	int i;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
494b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
497b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */
4981da177e4SLinus Torvalds static void build_initial_tok_table(void)
4991da177e4SLinus Torvalds {
500b3dbb4ecSPaulo Marques 	unsigned int i, pos;
5011da177e4SLinus Torvalds 
502b3dbb4ecSPaulo Marques 	pos = 0;
503b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
5041da177e4SLinus Torvalds 		if ( symbol_valid(&table[i]) ) {
505b3dbb4ecSPaulo Marques 			if (pos != i)
506b3dbb4ecSPaulo Marques 				table[pos] = table[i];
507b3dbb4ecSPaulo Marques 			learn_symbol(table[pos].sym, table[pos].len);
508b3dbb4ecSPaulo Marques 			pos++;
5091da177e4SLinus Torvalds 		}
5101da177e4SLinus Torvalds 	}
511b3dbb4ecSPaulo Marques 	table_cnt = pos;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5147c5d249aSPaulo Marques static void *find_token(unsigned char *str, int len, unsigned char *token)
5157c5d249aSPaulo Marques {
5167c5d249aSPaulo Marques 	int i;
5177c5d249aSPaulo Marques 
5187c5d249aSPaulo Marques 	for (i = 0; i < len - 1; i++) {
5197c5d249aSPaulo Marques 		if (str[i] == token[0] && str[i+1] == token[1])
5207c5d249aSPaulo Marques 			return &str[i];
5217c5d249aSPaulo Marques 	}
5227c5d249aSPaulo Marques 	return NULL;
5237c5d249aSPaulo Marques }
5247c5d249aSPaulo Marques 
5251da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols
5261da177e4SLinus Torvalds  * to update the counts */
527b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx)
5281da177e4SLinus Torvalds {
529b3dbb4ecSPaulo Marques 	unsigned int i, len, size;
530b3dbb4ecSPaulo Marques 	unsigned char *p1, *p2;
5311da177e4SLinus Torvalds 
532b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 		len = table[i].len;
535b3dbb4ecSPaulo Marques 		p1 = table[i].sym;
536b3dbb4ecSPaulo Marques 
537b3dbb4ecSPaulo Marques 		/* find the token on the symbol */
5387c5d249aSPaulo Marques 		p2 = find_token(p1, len, str);
539b3dbb4ecSPaulo Marques 		if (!p2) continue;
540b3dbb4ecSPaulo Marques 
541b3dbb4ecSPaulo Marques 		/* decrease the counts for this symbol's tokens */
542b3dbb4ecSPaulo Marques 		forget_symbol(table[i].sym, len);
543b3dbb4ecSPaulo Marques 
544b3dbb4ecSPaulo Marques 		size = len;
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 		do {
547b3dbb4ecSPaulo Marques 			*p2 = idx;
548b3dbb4ecSPaulo Marques 			p2++;
549b3dbb4ecSPaulo Marques 			size -= (p2 - p1);
550b3dbb4ecSPaulo Marques 			memmove(p2, p2 + 1, size);
551b3dbb4ecSPaulo Marques 			p1 = p2;
552b3dbb4ecSPaulo Marques 			len--;
553b3dbb4ecSPaulo Marques 
554b3dbb4ecSPaulo Marques 			if (size < 2) break;
555b3dbb4ecSPaulo Marques 
5561da177e4SLinus Torvalds 			/* find the token on the symbol */
5577c5d249aSPaulo Marques 			p2 = find_token(p1, size, str);
5581da177e4SLinus Torvalds 
559b3dbb4ecSPaulo Marques 		} while (p2);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 		table[i].len = len;
562b3dbb4ecSPaulo Marques 
563b3dbb4ecSPaulo Marques 		/* increase the counts for this symbol's new tokens */
5641da177e4SLinus Torvalds 		learn_symbol(table[i].sym, len);
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds /* search the token with the maximum profit */
569b3dbb4ecSPaulo Marques static int find_best_token(void)
5701da177e4SLinus Torvalds {
571b3dbb4ecSPaulo Marques 	int i, best, bestprofit;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	bestprofit=-10000;
574b3dbb4ecSPaulo Marques 	best = 0;
5751da177e4SLinus Torvalds 
576b3dbb4ecSPaulo Marques 	for (i = 0; i < 0x10000; i++) {
577b3dbb4ecSPaulo Marques 		if (token_profit[i] > bestprofit) {
578b3dbb4ecSPaulo Marques 			best = i;
579b3dbb4ecSPaulo Marques 			bestprofit = token_profit[i];
5801da177e4SLinus Torvalds 		}
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 	return best;
5831da177e4SLinus Torvalds }
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */
5861da177e4SLinus Torvalds static void optimize_result(void)
5871da177e4SLinus Torvalds {
588b3dbb4ecSPaulo Marques 	int i, best;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/* using the '\0' symbol last allows compress_symbols to use standard
5911da177e4SLinus Torvalds 	 * fast string functions */
5921da177e4SLinus Torvalds 	for (i = 255; i >= 0; i--) {
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 		/* if this table slot is empty (it is not used by an actual
5951da177e4SLinus Torvalds 		 * original char code */
5961da177e4SLinus Torvalds 		if (!best_table_len[i]) {
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 			/* find the token with the breates profit value */
5991da177e4SLinus Torvalds 			best = find_best_token();
600e0a04b11SXiaochen Wang 			if (token_profit[best] == 0)
601e0a04b11SXiaochen Wang 				break;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 			/* place it in the "best" table */
604b3dbb4ecSPaulo Marques 			best_table_len[i] = 2;
605b3dbb4ecSPaulo Marques 			best_table[i][0] = best & 0xFF;
606b3dbb4ecSPaulo Marques 			best_table[i][1] = (best >> 8) & 0xFF;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 			/* replace this token in all the valid symbols */
609b3dbb4ecSPaulo Marques 			compress_symbols(best_table[i], i);
6101da177e4SLinus Torvalds 		}
6111da177e4SLinus Torvalds 	}
6121da177e4SLinus Torvalds }
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */
6151da177e4SLinus Torvalds static void insert_real_symbols_in_table(void)
6161da177e4SLinus Torvalds {
617b3dbb4ecSPaulo Marques 	unsigned int i, j, c;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	memset(best_table, 0, sizeof(best_table));
6201da177e4SLinus Torvalds 	memset(best_table_len, 0, sizeof(best_table_len));
6211da177e4SLinus Torvalds 
622b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
6231da177e4SLinus Torvalds 		for (j = 0; j < table[i].len; j++) {
6241da177e4SLinus Torvalds 			c = table[i].sym[j];
6251da177e4SLinus Torvalds 			best_table[c][0]=c;
6261da177e4SLinus Torvalds 			best_table_len[c]=1;
6271da177e4SLinus Torvalds 		}
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds static void optimize_token_table(void)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	build_initial_tok_table();
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	insert_real_symbols_in_table();
6361da177e4SLinus Torvalds 
63741f11a4fSYoshinori Sato 	/* When valid symbol is not registered, exit to error */
638b3dbb4ecSPaulo Marques 	if (!table_cnt) {
63941f11a4fSYoshinori Sato 		fprintf(stderr, "No valid symbol.\n");
64041f11a4fSYoshinori Sato 		exit(1);
64141f11a4fSYoshinori Sato 	}
64241f11a4fSYoshinori Sato 
6431da177e4SLinus Torvalds 	optimize_result();
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
646b478b782SLai Jiangshan /* guess for "linker script provide" symbol */
647b478b782SLai Jiangshan static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
648b478b782SLai Jiangshan {
649b478b782SLai Jiangshan 	const char *symbol = (char *)se->sym + 1;
650b478b782SLai Jiangshan 	int len = se->len - 1;
651b478b782SLai Jiangshan 
652b478b782SLai Jiangshan 	if (len < 8)
653b478b782SLai Jiangshan 		return 0;
654b478b782SLai Jiangshan 
655b478b782SLai Jiangshan 	if (symbol[0] != '_' || symbol[1] != '_')
656b478b782SLai Jiangshan 		return 0;
657b478b782SLai Jiangshan 
658b478b782SLai Jiangshan 	/* __start_XXXXX */
659b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "start_", 6))
660b478b782SLai Jiangshan 		return 1;
661b478b782SLai Jiangshan 
662b478b782SLai Jiangshan 	/* __stop_XXXXX */
663b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "stop_", 5))
664b478b782SLai Jiangshan 		return 1;
665b478b782SLai Jiangshan 
666b478b782SLai Jiangshan 	/* __end_XXXXX */
667b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "end_", 4))
668b478b782SLai Jiangshan 		return 1;
669b478b782SLai Jiangshan 
670b478b782SLai Jiangshan 	/* __XXXXX_start */
671b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 6, "_start", 6))
672b478b782SLai Jiangshan 		return 1;
673b478b782SLai Jiangshan 
674b478b782SLai Jiangshan 	/* __XXXXX_end */
675b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 4, "_end", 4))
676b478b782SLai Jiangshan 		return 1;
677b478b782SLai Jiangshan 
678b478b782SLai Jiangshan 	return 0;
679b478b782SLai Jiangshan }
680b478b782SLai Jiangshan 
681b478b782SLai Jiangshan static int prefix_underscores_count(const char *str)
682b478b782SLai Jiangshan {
683b478b782SLai Jiangshan 	const char *tail = str;
684b478b782SLai Jiangshan 
685a9ece53cSPaul Mundt 	while (*tail == '_')
686b478b782SLai Jiangshan 		tail++;
687b478b782SLai Jiangshan 
688b478b782SLai Jiangshan 	return tail - str;
689b478b782SLai Jiangshan }
690b478b782SLai Jiangshan 
691f2df3f65SPaulo Marques static int compare_symbols(const void *a, const void *b)
692f2df3f65SPaulo Marques {
693f2df3f65SPaulo Marques 	const struct sym_entry *sa;
694f2df3f65SPaulo Marques 	const struct sym_entry *sb;
695f2df3f65SPaulo Marques 	int wa, wb;
696f2df3f65SPaulo Marques 
697f2df3f65SPaulo Marques 	sa = a;
698f2df3f65SPaulo Marques 	sb = b;
699f2df3f65SPaulo Marques 
700f2df3f65SPaulo Marques 	/* sort by address first */
701f2df3f65SPaulo Marques 	if (sa->addr > sb->addr)
702f2df3f65SPaulo Marques 		return 1;
703f2df3f65SPaulo Marques 	if (sa->addr < sb->addr)
704f2df3f65SPaulo Marques 		return -1;
705f2df3f65SPaulo Marques 
706f2df3f65SPaulo Marques 	/* sort by "weakness" type */
707f2df3f65SPaulo Marques 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
708f2df3f65SPaulo Marques 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
709f2df3f65SPaulo Marques 	if (wa != wb)
710f2df3f65SPaulo Marques 		return wa - wb;
711f2df3f65SPaulo Marques 
712b478b782SLai Jiangshan 	/* sort by "linker script provide" type */
713b478b782SLai Jiangshan 	wa = may_be_linker_script_provide_symbol(sa);
714b478b782SLai Jiangshan 	wb = may_be_linker_script_provide_symbol(sb);
715b478b782SLai Jiangshan 	if (wa != wb)
716b478b782SLai Jiangshan 		return wa - wb;
717b478b782SLai Jiangshan 
718b478b782SLai Jiangshan 	/* sort by the number of prefix underscores */
719b478b782SLai Jiangshan 	wa = prefix_underscores_count((const char *)sa->sym + 1);
720b478b782SLai Jiangshan 	wb = prefix_underscores_count((const char *)sb->sym + 1);
721b478b782SLai Jiangshan 	if (wa != wb)
722b478b782SLai Jiangshan 		return wa - wb;
723b478b782SLai Jiangshan 
724f2df3f65SPaulo Marques 	/* sort by initial order, so that other symbols are left undisturbed */
725f2df3f65SPaulo Marques 	return sa->start_pos - sb->start_pos;
726f2df3f65SPaulo Marques }
727f2df3f65SPaulo Marques 
728f2df3f65SPaulo Marques static void sort_symbols(void)
729f2df3f65SPaulo Marques {
730f2df3f65SPaulo Marques 	qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
731f2df3f65SPaulo Marques }
7321da177e4SLinus Torvalds 
733c6bda7c9SRusty Russell static void make_percpus_absolute(void)
734c6bda7c9SRusty Russell {
735c6bda7c9SRusty Russell 	unsigned int i;
736c6bda7c9SRusty Russell 
737c6bda7c9SRusty Russell 	for (i = 0; i < table_cnt; i++)
7388c996940SArd Biesheuvel 		if (symbol_in_range(&table[i], &percpu_range, 1)) {
7398c996940SArd Biesheuvel 			/*
7408c996940SArd Biesheuvel 			 * Keep the 'A' override for percpu symbols to
7418c996940SArd Biesheuvel 			 * ensure consistent behavior compared to older
7428c996940SArd Biesheuvel 			 * versions of this tool.
7438c996940SArd Biesheuvel 			 */
744c6bda7c9SRusty Russell 			table[i].sym[0] = 'A';
7458c996940SArd Biesheuvel 			table[i].percpu_absolute = 1;
7468c996940SArd Biesheuvel 		}
747c6bda7c9SRusty Russell }
748c6bda7c9SRusty Russell 
7492213e9a6SArd Biesheuvel /* find the minimum non-absolute symbol address */
7502213e9a6SArd Biesheuvel static void record_relative_base(void)
7512213e9a6SArd Biesheuvel {
7522213e9a6SArd Biesheuvel 	unsigned int i;
7532213e9a6SArd Biesheuvel 
7542213e9a6SArd Biesheuvel 	relative_base = -1ULL;
7552213e9a6SArd Biesheuvel 	for (i = 0; i < table_cnt; i++)
7562213e9a6SArd Biesheuvel 		if (!symbol_absolute(&table[i]) &&
7572213e9a6SArd Biesheuvel 		    table[i].addr < relative_base)
7582213e9a6SArd Biesheuvel 			relative_base = table[i].addr;
7592213e9a6SArd Biesheuvel }
7602213e9a6SArd Biesheuvel 
761b3dbb4ecSPaulo Marques int main(int argc, char **argv)
7621da177e4SLinus Torvalds {
76341f11a4fSYoshinori Sato 	if (argc >= 2) {
76441f11a4fSYoshinori Sato 		int i;
76541f11a4fSYoshinori Sato 		for (i = 1; i < argc; i++) {
76641f11a4fSYoshinori Sato 			if(strcmp(argv[i], "--all-symbols") == 0)
7671da177e4SLinus Torvalds 				all_symbols = 1;
768c6bda7c9SRusty Russell 			else if (strcmp(argv[i], "--absolute-percpu") == 0)
769c6bda7c9SRusty Russell 				absolute_percpu = 1;
77041f11a4fSYoshinori Sato 			else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
77141f11a4fSYoshinori Sato 				char *p = &argv[i][16];
77241f11a4fSYoshinori Sato 				/* skip quote */
77341f11a4fSYoshinori Sato 				if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
77441f11a4fSYoshinori Sato 					p++;
77541f11a4fSYoshinori Sato 				symbol_prefix_char = *p;
7762213e9a6SArd Biesheuvel 			} else if (strcmp(argv[i], "--base-relative") == 0)
7772213e9a6SArd Biesheuvel 				base_relative = 1;
7782213e9a6SArd Biesheuvel 			else
77941f11a4fSYoshinori Sato 				usage();
78041f11a4fSYoshinori Sato 		}
78141f11a4fSYoshinori Sato 	} else if (argc != 1)
7821da177e4SLinus Torvalds 		usage();
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	read_map(stdin);
785c6bda7c9SRusty Russell 	if (absolute_percpu)
786c6bda7c9SRusty Russell 		make_percpus_absolute();
7872213e9a6SArd Biesheuvel 	if (base_relative)
7882213e9a6SArd Biesheuvel 		record_relative_base();
789f2df3f65SPaulo Marques 	sort_symbols();
7901da177e4SLinus Torvalds 	optimize_token_table();
7911da177e4SLinus Torvalds 	write_src();
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	return 0;
7941da177e4SLinus Torvalds }
795