xref: /linux/scripts/kallsyms.c (revision 6db2983cd8064808141ccefd75218f5b4345ffae)
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 };
5217b1f0deSMike Frysinger #define text_range_text     (&text_ranges[0])
5317b1f0deSMike Frysinger #define text_range_inittext (&text_ranges[1])
5417b1f0deSMike Frysinger 
55c6bda7c9SRusty Russell static struct addr_range percpu_range = {
56c6bda7c9SRusty Russell 	"__per_cpu_start", "__per_cpu_end", -1ULL, 0
57c6bda7c9SRusty Russell };
58c6bda7c9SRusty Russell 
591da177e4SLinus Torvalds static struct sym_entry *table;
60b3dbb4ecSPaulo Marques static unsigned int table_size, table_cnt;
611da177e4SLinus Torvalds static int all_symbols = 0;
62c6bda7c9SRusty Russell static int absolute_percpu = 0;
632213e9a6SArd Biesheuvel static int base_relative = 0;
641da177e4SLinus Torvalds 
65b3dbb4ecSPaulo Marques int token_profit[0x10000];
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* the table that holds the result of the compression */
68b3dbb4ecSPaulo Marques unsigned char best_table[256][2];
691da177e4SLinus Torvalds unsigned char best_table_len[256];
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 
72b3dbb4ecSPaulo Marques static void usage(void)
731da177e4SLinus Torvalds {
74f6537f2fSMing Lei 	fprintf(stderr, "Usage: kallsyms [--all-symbols] "
752213e9a6SArd Biesheuvel 			"[--base-relative] < in.map > out.S\n");
761da177e4SLinus Torvalds 	exit(1);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  * This ignores the intensely annoying "mapping symbols" found
811da177e4SLinus Torvalds  * in ARM ELF files: $a, $t and $d.
821da177e4SLinus Torvalds  */
83b3dbb4ecSPaulo Marques static inline int is_arm_mapping_symbol(const char *str)
841da177e4SLinus Torvalds {
856c34f1f5SKyle McMartin 	return str[0] == '$' && strchr("axtd", str[1])
861da177e4SLinus Torvalds 	       && (str[2] == '\0' || str[2] == '.');
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
8978eb7159SKees Cook static int check_symbol_range(const char *sym, unsigned long long addr,
9078eb7159SKees Cook 			      struct addr_range *ranges, int entries)
9117b1f0deSMike Frysinger {
9217b1f0deSMike Frysinger 	size_t i;
9378eb7159SKees Cook 	struct addr_range *ar;
9417b1f0deSMike Frysinger 
9578eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
9678eb7159SKees Cook 		ar = &ranges[i];
9717b1f0deSMike Frysinger 
9878eb7159SKees Cook 		if (strcmp(sym, ar->start_sym) == 0) {
9978eb7159SKees Cook 			ar->start = addr;
10017b1f0deSMike Frysinger 			return 0;
10178eb7159SKees Cook 		} else if (strcmp(sym, ar->end_sym) == 0) {
10278eb7159SKees Cook 			ar->end = addr;
10317b1f0deSMike Frysinger 			return 0;
10417b1f0deSMike Frysinger 		}
10517b1f0deSMike Frysinger 	}
10617b1f0deSMike Frysinger 
10717b1f0deSMike Frysinger 	return 1;
10817b1f0deSMike Frysinger }
10917b1f0deSMike Frysinger 
110b3dbb4ecSPaulo Marques static int read_symbol(FILE *in, struct sym_entry *s)
1111da177e4SLinus Torvalds {
112534c9f2eSMasahiro Yamada 	char sym[500], stype;
1131da177e4SLinus Torvalds 	int rc;
1141da177e4SLinus Torvalds 
115534c9f2eSMasahiro Yamada 	rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, sym);
1161da177e4SLinus Torvalds 	if (rc != 3) {
117534c9f2eSMasahiro Yamada 		if (rc != EOF && fgets(sym, 500, in) == NULL)
118ef894870SJean Sacren 			fprintf(stderr, "Read error or end of file.\n");
1191da177e4SLinus Torvalds 		return -1;
1201da177e4SLinus Torvalds 	}
121*6db2983cSEugene Loh 	if (strlen(sym) >= KSYM_NAME_LEN) {
122*6db2983cSEugene Loh 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
123f3462aa9SAndi Kleen 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
124534c9f2eSMasahiro Yamada 			sym, strlen(sym), KSYM_NAME_LEN);
125f3462aa9SAndi Kleen 		return -1;
126f3462aa9SAndi Kleen 	}
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	/* Ignore most absolute/undefined (?) symbols. */
129fd593d12SEric W. Biederman 	if (strcmp(sym, "_text") == 0)
130fd593d12SEric W. Biederman 		_text = s->addr;
13178eb7159SKees Cook 	else if (check_symbol_range(sym, s->addr, text_ranges,
13278eb7159SKees Cook 				    ARRAY_SIZE(text_ranges)) == 0)
13317b1f0deSMike Frysinger 		/* nothing to do */;
134b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'A')
1351da177e4SLinus Torvalds 	{
1361da177e4SLinus Torvalds 		/* Keep these useful absolute symbols */
13741f11a4fSYoshinori Sato 		if (strcmp(sym, "__kernel_syscall_via_break") &&
13841f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_syscall_via_epc") &&
13941f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_sigtramp") &&
14041f11a4fSYoshinori Sato 		    strcmp(sym, "__gp"))
1411da177e4SLinus Torvalds 			return -1;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	}
144b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'U' ||
14541f11a4fSYoshinori Sato 		 is_arm_mapping_symbol(sym))
1461da177e4SLinus Torvalds 		return -1;
1476f00df24SRalf Baechle 	/* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
148534c9f2eSMasahiro Yamada 	else if (sym[0] == '$')
1496f00df24SRalf Baechle 		return -1;
150aab34ac8SSam Ravnborg 	/* exclude debugging symbols */
15151962a9dSGuenter Roeck 	else if (stype == 'N' || stype == 'n')
152aab34ac8SSam Ravnborg 		return -1;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	/* include the type field in the symbol name, so that it gets
1551da177e4SLinus Torvalds 	 * compressed together */
156534c9f2eSMasahiro Yamada 	s->len = strlen(sym) + 1;
157b3dbb4ecSPaulo Marques 	s->sym = malloc(s->len + 1);
158f1a136e0SJesper Juhl 	if (!s->sym) {
159f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
160f1a136e0SJesper Juhl 			"unable to allocate required amount of memory\n");
161f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
162f1a136e0SJesper Juhl 	}
163534c9f2eSMasahiro Yamada 	strcpy((char *)s->sym + 1, sym);
164b3dbb4ecSPaulo Marques 	s->sym[0] = stype;
1651da177e4SLinus Torvalds 
1668c996940SArd Biesheuvel 	s->percpu_absolute = 0;
1678c996940SArd Biesheuvel 
168c6bda7c9SRusty Russell 	/* Record if we've found __per_cpu_start/end. */
169c6bda7c9SRusty Russell 	check_symbol_range(sym, s->addr, &percpu_range, 1);
170c6bda7c9SRusty Russell 
1711da177e4SLinus Torvalds 	return 0;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
17478eb7159SKees Cook static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
17578eb7159SKees Cook 			   int entries)
17617b1f0deSMike Frysinger {
17717b1f0deSMike Frysinger 	size_t i;
17878eb7159SKees Cook 	struct addr_range *ar;
17917b1f0deSMike Frysinger 
18078eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
18178eb7159SKees Cook 		ar = &ranges[i];
18217b1f0deSMike Frysinger 
18378eb7159SKees Cook 		if (s->addr >= ar->start && s->addr <= ar->end)
184ac6ca5c8SMike Frysinger 			return 1;
18517b1f0deSMike Frysinger 	}
18617b1f0deSMike Frysinger 
187ac6ca5c8SMike Frysinger 	return 0;
18817b1f0deSMike Frysinger }
18917b1f0deSMike Frysinger 
190b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	/* Symbols which vary between passes.  Passes 1 and 2 must have
1932ea03891SSam Ravnborg 	 * identical symbol lists.  The kallsyms_* symbols below are only added
1942ea03891SSam Ravnborg 	 * after pass 1, they would be included in pass 2 when --all-symbols is
1952ea03891SSam Ravnborg 	 * specified so exclude them to get a stable symbol list.
1961da177e4SLinus Torvalds 	 */
1971da177e4SLinus Torvalds 	static char *special_symbols[] = {
1982ea03891SSam Ravnborg 		"kallsyms_addresses",
1992213e9a6SArd Biesheuvel 		"kallsyms_offsets",
2002213e9a6SArd Biesheuvel 		"kallsyms_relative_base",
2012ea03891SSam Ravnborg 		"kallsyms_num_syms",
2022ea03891SSam Ravnborg 		"kallsyms_names",
2032ea03891SSam Ravnborg 		"kallsyms_markers",
2042ea03891SSam Ravnborg 		"kallsyms_token_table",
2052ea03891SSam Ravnborg 		"kallsyms_token_index",
2062ea03891SSam Ravnborg 
2071da177e4SLinus Torvalds 	/* Exclude linker generated symbols which vary between passes */
2081da177e4SLinus Torvalds 		"_SDA_BASE_",		/* ppc */
2091da177e4SLinus Torvalds 		"_SDA2_BASE_",		/* ppc */
2101da177e4SLinus Torvalds 		NULL };
211bd8b22d2SArd Biesheuvel 
21256067812SArd Biesheuvel 	static char *special_prefixes[] = {
21356067812SArd Biesheuvel 		"__crc_",		/* modversions */
2141212f7a1SArd Biesheuvel 		"__efistub_",		/* arm64 EFI stub namespace */
21556067812SArd Biesheuvel 		NULL };
21656067812SArd Biesheuvel 
217bd8b22d2SArd Biesheuvel 	static char *special_suffixes[] = {
218bd8b22d2SArd Biesheuvel 		"_veneer",		/* arm */
219b9b74be1SArd Biesheuvel 		"_from_arm",		/* arm */
220b9b74be1SArd Biesheuvel 		"_from_thumb",		/* arm */
221bd8b22d2SArd Biesheuvel 		NULL };
222bd8b22d2SArd Biesheuvel 
2231da177e4SLinus Torvalds 	int i;
224bd8b22d2SArd Biesheuvel 	char *sym_name = (char *)s->sym + 1;
225bd8b22d2SArd Biesheuvel 
2261da177e4SLinus Torvalds 	/* if --all-symbols is not specified, then symbols outside the text
2271da177e4SLinus Torvalds 	 * and inittext sections are discarded */
2281da177e4SLinus Torvalds 	if (!all_symbols) {
22978eb7159SKees Cook 		if (symbol_in_range(s, text_ranges,
23078eb7159SKees Cook 				    ARRAY_SIZE(text_ranges)) == 0)
2311da177e4SLinus Torvalds 			return 0;
2321da177e4SLinus Torvalds 		/* Corner case.  Discard any symbols with the same value as
233a3b81113SRobin Getz 		 * _etext _einittext; they can move between pass 1 and 2 when
234a3b81113SRobin Getz 		 * the kallsyms data are added.  If these symbols move then
235a3b81113SRobin Getz 		 * they may get dropped in pass 2, which breaks the kallsyms
236a3b81113SRobin Getz 		 * rules.
2371da177e4SLinus Torvalds 		 */
23817b1f0deSMike Frysinger 		if ((s->addr == text_range_text->end &&
239bd8b22d2SArd Biesheuvel 				strcmp(sym_name,
24078eb7159SKees Cook 				       text_range_text->end_sym)) ||
24117b1f0deSMike Frysinger 		    (s->addr == text_range_inittext->end &&
242bd8b22d2SArd Biesheuvel 				strcmp(sym_name,
24378eb7159SKees Cook 				       text_range_inittext->end_sym)))
2441da177e4SLinus Torvalds 			return 0;
2451da177e4SLinus Torvalds 	}
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 	/* Exclude symbols which vary between passes. */
248bd8b22d2SArd Biesheuvel 	for (i = 0; special_symbols[i]; i++)
249bd8b22d2SArd Biesheuvel 		if (strcmp(sym_name, special_symbols[i]) == 0)
2501da177e4SLinus Torvalds 			return 0;
2511da177e4SLinus Torvalds 
25256067812SArd Biesheuvel 	for (i = 0; special_prefixes[i]; i++) {
25356067812SArd Biesheuvel 		int l = strlen(special_prefixes[i]);
25456067812SArd Biesheuvel 
25556067812SArd Biesheuvel 		if (l <= strlen(sym_name) &&
25656067812SArd Biesheuvel 		    strncmp(sym_name, special_prefixes[i], l) == 0)
25756067812SArd Biesheuvel 			return 0;
25856067812SArd Biesheuvel 	}
25956067812SArd Biesheuvel 
260bd8b22d2SArd Biesheuvel 	for (i = 0; special_suffixes[i]; i++) {
261bd8b22d2SArd Biesheuvel 		int l = strlen(sym_name) - strlen(special_suffixes[i]);
262bd8b22d2SArd Biesheuvel 
263bd8b22d2SArd Biesheuvel 		if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0)
2641da177e4SLinus Torvalds 			return 0;
265bd8b22d2SArd Biesheuvel 	}
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	return 1;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
270b3dbb4ecSPaulo Marques static void read_map(FILE *in)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	while (!feof(in)) {
273b3dbb4ecSPaulo Marques 		if (table_cnt >= table_size) {
274b3dbb4ecSPaulo Marques 			table_size += 10000;
275b3dbb4ecSPaulo Marques 			table = realloc(table, sizeof(*table) * table_size);
2761da177e4SLinus Torvalds 			if (!table) {
2771da177e4SLinus Torvalds 				fprintf(stderr, "out of memory\n");
2781da177e4SLinus Torvalds 				exit (1);
2791da177e4SLinus Torvalds 			}
2801da177e4SLinus Torvalds 		}
281f2df3f65SPaulo Marques 		if (read_symbol(in, &table[table_cnt]) == 0) {
282f2df3f65SPaulo Marques 			table[table_cnt].start_pos = table_cnt;
283b3dbb4ecSPaulo Marques 			table_cnt++;
2841da177e4SLinus Torvalds 		}
2851da177e4SLinus Torvalds 	}
286f2df3f65SPaulo Marques }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds static void output_label(char *label)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	printf(".globl %s\n", label);
2911da177e4SLinus Torvalds 	printf("\tALGN\n");
2921da177e4SLinus Torvalds 	printf("%s:\n", label);
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table
2961da177e4SLinus Torvalds  * might still be compressed itself, so the function needs to be recursive */
2971da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result)
2981da177e4SLinus Torvalds {
2991da177e4SLinus Torvalds 	int c, rlen, total=0;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	while (len) {
3021da177e4SLinus Torvalds 		c = *data;
3031da177e4SLinus Torvalds 		/* if the table holds a single char that is the same as the one
3041da177e4SLinus Torvalds 		 * we are looking for, then end the search */
3051da177e4SLinus Torvalds 		if (best_table[c][0]==c && best_table_len[c]==1) {
3061da177e4SLinus Torvalds 			*result++ = c;
3071da177e4SLinus Torvalds 			total++;
3081da177e4SLinus Torvalds 		} else {
3091da177e4SLinus Torvalds 			/* if not, recurse and expand */
3101da177e4SLinus Torvalds 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
3111da177e4SLinus Torvalds 			total += rlen;
3121da177e4SLinus Torvalds 			result += rlen;
3131da177e4SLinus Torvalds 		}
3141da177e4SLinus Torvalds 		data++;
3151da177e4SLinus Torvalds 		len--;
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds 	*result=0;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	return total;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
32278eb7159SKees Cook static int symbol_absolute(struct sym_entry *s)
32378eb7159SKees Cook {
3248c996940SArd Biesheuvel 	return s->percpu_absolute;
32578eb7159SKees Cook }
32678eb7159SKees Cook 
327b3dbb4ecSPaulo Marques static void write_src(void)
3281da177e4SLinus Torvalds {
329b3dbb4ecSPaulo Marques 	unsigned int i, k, off;
3301da177e4SLinus Torvalds 	unsigned int best_idx[256];
3311da177e4SLinus Torvalds 	unsigned int *markers;
3329281aceaSTejun Heo 	char buf[KSYM_NAME_LEN];
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	printf("#include <asm/types.h>\n");
3351da177e4SLinus Torvalds 	printf("#if BITS_PER_LONG == 64\n");
3361da177e4SLinus Torvalds 	printf("#define PTR .quad\n");
33772d3ebb9SMathias Krause 	printf("#define ALGN .balign 8\n");
3381da177e4SLinus Torvalds 	printf("#else\n");
3391da177e4SLinus Torvalds 	printf("#define PTR .long\n");
34072d3ebb9SMathias Krause 	printf("#define ALGN .balign 4\n");
3411da177e4SLinus Torvalds 	printf("#endif\n");
3421da177e4SLinus Torvalds 
343aad09470SJan Beulich 	printf("\t.section .rodata, \"a\"\n");
3441da177e4SLinus Torvalds 
3452213e9a6SArd Biesheuvel 	/* Provide proper symbols relocatability by their relativeness
3462213e9a6SArd Biesheuvel 	 * to a fixed anchor point in the runtime image, either '_text'
3472213e9a6SArd Biesheuvel 	 * for absolute address tables, in which case the linker will
3482213e9a6SArd Biesheuvel 	 * emit the final addresses at build time. Otherwise, use the
3492213e9a6SArd Biesheuvel 	 * offset relative to the lowest value encountered of all relative
3502213e9a6SArd Biesheuvel 	 * symbols, and emit non-relocatable fixed offsets that will be fixed
3512213e9a6SArd Biesheuvel 	 * up at runtime.
3522213e9a6SArd Biesheuvel 	 *
3532213e9a6SArd Biesheuvel 	 * The symbol names cannot be used to construct normal symbol
3542213e9a6SArd Biesheuvel 	 * references as the list of symbols contains symbols that are
3552213e9a6SArd Biesheuvel 	 * declared static and are private to their .o files.  This prevents
3562213e9a6SArd Biesheuvel 	 * .tmp_kallsyms.o or any other object from referencing them.
357fd593d12SEric W. Biederman 	 */
3582213e9a6SArd Biesheuvel 	if (!base_relative)
3591da177e4SLinus Torvalds 		output_label("kallsyms_addresses");
3602213e9a6SArd Biesheuvel 	else
3612213e9a6SArd Biesheuvel 		output_label("kallsyms_offsets");
3622213e9a6SArd Biesheuvel 
363b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
3642213e9a6SArd Biesheuvel 		if (base_relative) {
3652213e9a6SArd Biesheuvel 			long long offset;
3662213e9a6SArd Biesheuvel 			int overflow;
3672213e9a6SArd Biesheuvel 
3682213e9a6SArd Biesheuvel 			if (!absolute_percpu) {
3692213e9a6SArd Biesheuvel 				offset = table[i].addr - relative_base;
3702213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > UINT_MAX);
3712213e9a6SArd Biesheuvel 			} else if (symbol_absolute(&table[i])) {
3722213e9a6SArd Biesheuvel 				offset = table[i].addr;
3732213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > INT_MAX);
3742213e9a6SArd Biesheuvel 			} else {
3752213e9a6SArd Biesheuvel 				offset = relative_base - table[i].addr - 1;
3762213e9a6SArd Biesheuvel 				overflow = (offset < INT_MIN || offset >= 0);
3772213e9a6SArd Biesheuvel 			}
3782213e9a6SArd Biesheuvel 			if (overflow) {
3792213e9a6SArd Biesheuvel 				fprintf(stderr, "kallsyms failure: "
3802213e9a6SArd Biesheuvel 					"%s symbol value %#llx out of range in relative mode\n",
3812213e9a6SArd Biesheuvel 					symbol_absolute(&table[i]) ? "absolute" : "relative",
3822213e9a6SArd Biesheuvel 					table[i].addr);
3832213e9a6SArd Biesheuvel 				exit(EXIT_FAILURE);
3842213e9a6SArd Biesheuvel 			}
3852213e9a6SArd Biesheuvel 			printf("\t.long\t%#x\n", (int)offset);
3862213e9a6SArd Biesheuvel 		} else if (!symbol_absolute(&table[i])) {
3872c22d8baSVivek Goyal 			if (_text <= table[i].addr)
388fd593d12SEric W. Biederman 				printf("\tPTR\t_text + %#llx\n",
389fd593d12SEric W. Biederman 					table[i].addr - _text);
3902c22d8baSVivek Goyal 			else
3912930ffc7SAndrew Morton 				printf("\tPTR\t_text - %#llx\n",
3922930ffc7SAndrew Morton 					_text - table[i].addr);
393fd593d12SEric W. Biederman 		} else {
3941da177e4SLinus Torvalds 			printf("\tPTR\t%#llx\n", table[i].addr);
3951da177e4SLinus Torvalds 		}
396fd593d12SEric W. Biederman 	}
3971da177e4SLinus Torvalds 	printf("\n");
3981da177e4SLinus Torvalds 
3992213e9a6SArd Biesheuvel 	if (base_relative) {
4002213e9a6SArd Biesheuvel 		output_label("kallsyms_relative_base");
4012213e9a6SArd Biesheuvel 		printf("\tPTR\t_text - %#llx\n", _text - relative_base);
4022213e9a6SArd Biesheuvel 		printf("\n");
4032213e9a6SArd Biesheuvel 	}
4042213e9a6SArd Biesheuvel 
4051da177e4SLinus Torvalds 	output_label("kallsyms_num_syms");
40680ffbaa5SJan Beulich 	printf("\t.long\t%u\n", table_cnt);
4071da177e4SLinus Torvalds 	printf("\n");
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	/* table of offset markers, that give the offset in the compressed stream
4101da177e4SLinus Torvalds 	 * every 256 symbols */
411f1a136e0SJesper Juhl 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
412f1a136e0SJesper Juhl 	if (!markers) {
413f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
414f1a136e0SJesper Juhl 			"unable to allocate required memory\n");
415f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
416f1a136e0SJesper Juhl 	}
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	output_label("kallsyms_names");
4191da177e4SLinus Torvalds 	off = 0;
420b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
421b3dbb4ecSPaulo Marques 		if ((i & 0xFF) == 0)
422b3dbb4ecSPaulo Marques 			markers[i >> 8] = off;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 		printf("\t.byte 0x%02x", table[i].len);
4251da177e4SLinus Torvalds 		for (k = 0; k < table[i].len; k++)
4261da177e4SLinus Torvalds 			printf(", 0x%02x", table[i].sym[k]);
4271da177e4SLinus Torvalds 		printf("\n");
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 		off += table[i].len + 1;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	printf("\n");
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	output_label("kallsyms_markers");
434b3dbb4ecSPaulo Marques 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
43580ffbaa5SJan Beulich 		printf("\t.long\t%u\n", markers[i]);
4361da177e4SLinus Torvalds 	printf("\n");
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	free(markers);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	output_label("kallsyms_token_table");
4411da177e4SLinus Torvalds 	off = 0;
4421da177e4SLinus Torvalds 	for (i = 0; i < 256; i++) {
4431da177e4SLinus Torvalds 		best_idx[i] = off;
4441da177e4SLinus Torvalds 		expand_symbol(best_table[i], best_table_len[i], buf);
4451da177e4SLinus Torvalds 		printf("\t.asciz\t\"%s\"\n", buf);
4461da177e4SLinus Torvalds 		off += strlen(buf) + 1;
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 	printf("\n");
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	output_label("kallsyms_token_index");
4511da177e4SLinus Torvalds 	for (i = 0; i < 256; i++)
4521da177e4SLinus Torvalds 		printf("\t.short\t%d\n", best_idx[i]);
4531da177e4SLinus Torvalds 	printf("\n");
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds /* table lookup compression functions */
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /* count all the possible tokens in a symbol */
4601da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	int i;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
465b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */
4691da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	int i;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
474b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
477b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */
4781da177e4SLinus Torvalds static void build_initial_tok_table(void)
4791da177e4SLinus Torvalds {
480b3dbb4ecSPaulo Marques 	unsigned int i, pos;
4811da177e4SLinus Torvalds 
482b3dbb4ecSPaulo Marques 	pos = 0;
483b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
4841da177e4SLinus Torvalds 		if ( symbol_valid(&table[i]) ) {
485b3dbb4ecSPaulo Marques 			if (pos != i)
486b3dbb4ecSPaulo Marques 				table[pos] = table[i];
487b3dbb4ecSPaulo Marques 			learn_symbol(table[pos].sym, table[pos].len);
488b3dbb4ecSPaulo Marques 			pos++;
4891da177e4SLinus Torvalds 		}
4901da177e4SLinus Torvalds 	}
491b3dbb4ecSPaulo Marques 	table_cnt = pos;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4947c5d249aSPaulo Marques static void *find_token(unsigned char *str, int len, unsigned char *token)
4957c5d249aSPaulo Marques {
4967c5d249aSPaulo Marques 	int i;
4977c5d249aSPaulo Marques 
4987c5d249aSPaulo Marques 	for (i = 0; i < len - 1; i++) {
4997c5d249aSPaulo Marques 		if (str[i] == token[0] && str[i+1] == token[1])
5007c5d249aSPaulo Marques 			return &str[i];
5017c5d249aSPaulo Marques 	}
5027c5d249aSPaulo Marques 	return NULL;
5037c5d249aSPaulo Marques }
5047c5d249aSPaulo Marques 
5051da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols
5061da177e4SLinus Torvalds  * to update the counts */
507b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx)
5081da177e4SLinus Torvalds {
509b3dbb4ecSPaulo Marques 	unsigned int i, len, size;
510b3dbb4ecSPaulo Marques 	unsigned char *p1, *p2;
5111da177e4SLinus Torvalds 
512b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 		len = table[i].len;
515b3dbb4ecSPaulo Marques 		p1 = table[i].sym;
516b3dbb4ecSPaulo Marques 
517b3dbb4ecSPaulo Marques 		/* find the token on the symbol */
5187c5d249aSPaulo Marques 		p2 = find_token(p1, len, str);
519b3dbb4ecSPaulo Marques 		if (!p2) continue;
520b3dbb4ecSPaulo Marques 
521b3dbb4ecSPaulo Marques 		/* decrease the counts for this symbol's tokens */
522b3dbb4ecSPaulo Marques 		forget_symbol(table[i].sym, len);
523b3dbb4ecSPaulo Marques 
524b3dbb4ecSPaulo Marques 		size = len;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 		do {
527b3dbb4ecSPaulo Marques 			*p2 = idx;
528b3dbb4ecSPaulo Marques 			p2++;
529b3dbb4ecSPaulo Marques 			size -= (p2 - p1);
530b3dbb4ecSPaulo Marques 			memmove(p2, p2 + 1, size);
531b3dbb4ecSPaulo Marques 			p1 = p2;
532b3dbb4ecSPaulo Marques 			len--;
533b3dbb4ecSPaulo Marques 
534b3dbb4ecSPaulo Marques 			if (size < 2) break;
535b3dbb4ecSPaulo Marques 
5361da177e4SLinus Torvalds 			/* find the token on the symbol */
5377c5d249aSPaulo Marques 			p2 = find_token(p1, size, str);
5381da177e4SLinus Torvalds 
539b3dbb4ecSPaulo Marques 		} while (p2);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 		table[i].len = len;
542b3dbb4ecSPaulo Marques 
543b3dbb4ecSPaulo Marques 		/* increase the counts for this symbol's new tokens */
5441da177e4SLinus Torvalds 		learn_symbol(table[i].sym, len);
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds /* search the token with the maximum profit */
549b3dbb4ecSPaulo Marques static int find_best_token(void)
5501da177e4SLinus Torvalds {
551b3dbb4ecSPaulo Marques 	int i, best, bestprofit;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	bestprofit=-10000;
554b3dbb4ecSPaulo Marques 	best = 0;
5551da177e4SLinus Torvalds 
556b3dbb4ecSPaulo Marques 	for (i = 0; i < 0x10000; i++) {
557b3dbb4ecSPaulo Marques 		if (token_profit[i] > bestprofit) {
558b3dbb4ecSPaulo Marques 			best = i;
559b3dbb4ecSPaulo Marques 			bestprofit = token_profit[i];
5601da177e4SLinus Torvalds 		}
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 	return best;
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */
5661da177e4SLinus Torvalds static void optimize_result(void)
5671da177e4SLinus Torvalds {
568b3dbb4ecSPaulo Marques 	int i, best;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	/* using the '\0' symbol last allows compress_symbols to use standard
5711da177e4SLinus Torvalds 	 * fast string functions */
5721da177e4SLinus Torvalds 	for (i = 255; i >= 0; i--) {
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 		/* if this table slot is empty (it is not used by an actual
5751da177e4SLinus Torvalds 		 * original char code */
5761da177e4SLinus Torvalds 		if (!best_table_len[i]) {
5771da177e4SLinus Torvalds 
578cbf7a90eSCao jin 			/* find the token with the best profit value */
5791da177e4SLinus Torvalds 			best = find_best_token();
580e0a04b11SXiaochen Wang 			if (token_profit[best] == 0)
581e0a04b11SXiaochen Wang 				break;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 			/* place it in the "best" table */
584b3dbb4ecSPaulo Marques 			best_table_len[i] = 2;
585b3dbb4ecSPaulo Marques 			best_table[i][0] = best & 0xFF;
586b3dbb4ecSPaulo Marques 			best_table[i][1] = (best >> 8) & 0xFF;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 			/* replace this token in all the valid symbols */
589b3dbb4ecSPaulo Marques 			compress_symbols(best_table[i], i);
5901da177e4SLinus Torvalds 		}
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */
5951da177e4SLinus Torvalds static void insert_real_symbols_in_table(void)
5961da177e4SLinus Torvalds {
597b3dbb4ecSPaulo Marques 	unsigned int i, j, c;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	memset(best_table, 0, sizeof(best_table));
6001da177e4SLinus Torvalds 	memset(best_table_len, 0, sizeof(best_table_len));
6011da177e4SLinus Torvalds 
602b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
6031da177e4SLinus Torvalds 		for (j = 0; j < table[i].len; j++) {
6041da177e4SLinus Torvalds 			c = table[i].sym[j];
6051da177e4SLinus Torvalds 			best_table[c][0]=c;
6061da177e4SLinus Torvalds 			best_table_len[c]=1;
6071da177e4SLinus Torvalds 		}
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds static void optimize_token_table(void)
6121da177e4SLinus Torvalds {
6131da177e4SLinus Torvalds 	build_initial_tok_table();
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	insert_real_symbols_in_table();
6161da177e4SLinus Torvalds 
61741f11a4fSYoshinori Sato 	/* When valid symbol is not registered, exit to error */
618b3dbb4ecSPaulo Marques 	if (!table_cnt) {
61941f11a4fSYoshinori Sato 		fprintf(stderr, "No valid symbol.\n");
62041f11a4fSYoshinori Sato 		exit(1);
62141f11a4fSYoshinori Sato 	}
62241f11a4fSYoshinori Sato 
6231da177e4SLinus Torvalds 	optimize_result();
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds 
626b478b782SLai Jiangshan /* guess for "linker script provide" symbol */
627b478b782SLai Jiangshan static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
628b478b782SLai Jiangshan {
629b478b782SLai Jiangshan 	const char *symbol = (char *)se->sym + 1;
630b478b782SLai Jiangshan 	int len = se->len - 1;
631b478b782SLai Jiangshan 
632b478b782SLai Jiangshan 	if (len < 8)
633b478b782SLai Jiangshan 		return 0;
634b478b782SLai Jiangshan 
635b478b782SLai Jiangshan 	if (symbol[0] != '_' || symbol[1] != '_')
636b478b782SLai Jiangshan 		return 0;
637b478b782SLai Jiangshan 
638b478b782SLai Jiangshan 	/* __start_XXXXX */
639b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "start_", 6))
640b478b782SLai Jiangshan 		return 1;
641b478b782SLai Jiangshan 
642b478b782SLai Jiangshan 	/* __stop_XXXXX */
643b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "stop_", 5))
644b478b782SLai Jiangshan 		return 1;
645b478b782SLai Jiangshan 
646b478b782SLai Jiangshan 	/* __end_XXXXX */
647b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "end_", 4))
648b478b782SLai Jiangshan 		return 1;
649b478b782SLai Jiangshan 
650b478b782SLai Jiangshan 	/* __XXXXX_start */
651b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 6, "_start", 6))
652b478b782SLai Jiangshan 		return 1;
653b478b782SLai Jiangshan 
654b478b782SLai Jiangshan 	/* __XXXXX_end */
655b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 4, "_end", 4))
656b478b782SLai Jiangshan 		return 1;
657b478b782SLai Jiangshan 
658b478b782SLai Jiangshan 	return 0;
659b478b782SLai Jiangshan }
660b478b782SLai Jiangshan 
661b478b782SLai Jiangshan static int prefix_underscores_count(const char *str)
662b478b782SLai Jiangshan {
663b478b782SLai Jiangshan 	const char *tail = str;
664b478b782SLai Jiangshan 
665a9ece53cSPaul Mundt 	while (*tail == '_')
666b478b782SLai Jiangshan 		tail++;
667b478b782SLai Jiangshan 
668b478b782SLai Jiangshan 	return tail - str;
669b478b782SLai Jiangshan }
670b478b782SLai Jiangshan 
671f2df3f65SPaulo Marques static int compare_symbols(const void *a, const void *b)
672f2df3f65SPaulo Marques {
673f2df3f65SPaulo Marques 	const struct sym_entry *sa;
674f2df3f65SPaulo Marques 	const struct sym_entry *sb;
675f2df3f65SPaulo Marques 	int wa, wb;
676f2df3f65SPaulo Marques 
677f2df3f65SPaulo Marques 	sa = a;
678f2df3f65SPaulo Marques 	sb = b;
679f2df3f65SPaulo Marques 
680f2df3f65SPaulo Marques 	/* sort by address first */
681f2df3f65SPaulo Marques 	if (sa->addr > sb->addr)
682f2df3f65SPaulo Marques 		return 1;
683f2df3f65SPaulo Marques 	if (sa->addr < sb->addr)
684f2df3f65SPaulo Marques 		return -1;
685f2df3f65SPaulo Marques 
686f2df3f65SPaulo Marques 	/* sort by "weakness" type */
687f2df3f65SPaulo Marques 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
688f2df3f65SPaulo Marques 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
689f2df3f65SPaulo Marques 	if (wa != wb)
690f2df3f65SPaulo Marques 		return wa - wb;
691f2df3f65SPaulo Marques 
692b478b782SLai Jiangshan 	/* sort by "linker script provide" type */
693b478b782SLai Jiangshan 	wa = may_be_linker_script_provide_symbol(sa);
694b478b782SLai Jiangshan 	wb = may_be_linker_script_provide_symbol(sb);
695b478b782SLai Jiangshan 	if (wa != wb)
696b478b782SLai Jiangshan 		return wa - wb;
697b478b782SLai Jiangshan 
698b478b782SLai Jiangshan 	/* sort by the number of prefix underscores */
699b478b782SLai Jiangshan 	wa = prefix_underscores_count((const char *)sa->sym + 1);
700b478b782SLai Jiangshan 	wb = prefix_underscores_count((const char *)sb->sym + 1);
701b478b782SLai Jiangshan 	if (wa != wb)
702b478b782SLai Jiangshan 		return wa - wb;
703b478b782SLai Jiangshan 
704f2df3f65SPaulo Marques 	/* sort by initial order, so that other symbols are left undisturbed */
705f2df3f65SPaulo Marques 	return sa->start_pos - sb->start_pos;
706f2df3f65SPaulo Marques }
707f2df3f65SPaulo Marques 
708f2df3f65SPaulo Marques static void sort_symbols(void)
709f2df3f65SPaulo Marques {
710f2df3f65SPaulo Marques 	qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
711f2df3f65SPaulo Marques }
7121da177e4SLinus Torvalds 
713c6bda7c9SRusty Russell static void make_percpus_absolute(void)
714c6bda7c9SRusty Russell {
715c6bda7c9SRusty Russell 	unsigned int i;
716c6bda7c9SRusty Russell 
717c6bda7c9SRusty Russell 	for (i = 0; i < table_cnt; i++)
7188c996940SArd Biesheuvel 		if (symbol_in_range(&table[i], &percpu_range, 1)) {
7198c996940SArd Biesheuvel 			/*
7208c996940SArd Biesheuvel 			 * Keep the 'A' override for percpu symbols to
7218c996940SArd Biesheuvel 			 * ensure consistent behavior compared to older
7228c996940SArd Biesheuvel 			 * versions of this tool.
7238c996940SArd Biesheuvel 			 */
724c6bda7c9SRusty Russell 			table[i].sym[0] = 'A';
7258c996940SArd Biesheuvel 			table[i].percpu_absolute = 1;
7268c996940SArd Biesheuvel 		}
727c6bda7c9SRusty Russell }
728c6bda7c9SRusty Russell 
7292213e9a6SArd Biesheuvel /* find the minimum non-absolute symbol address */
7302213e9a6SArd Biesheuvel static void record_relative_base(void)
7312213e9a6SArd Biesheuvel {
7322213e9a6SArd Biesheuvel 	unsigned int i;
7332213e9a6SArd Biesheuvel 
7342213e9a6SArd Biesheuvel 	relative_base = -1ULL;
7352213e9a6SArd Biesheuvel 	for (i = 0; i < table_cnt; i++)
7362213e9a6SArd Biesheuvel 		if (!symbol_absolute(&table[i]) &&
7372213e9a6SArd Biesheuvel 		    table[i].addr < relative_base)
7382213e9a6SArd Biesheuvel 			relative_base = table[i].addr;
7392213e9a6SArd Biesheuvel }
7402213e9a6SArd Biesheuvel 
741b3dbb4ecSPaulo Marques int main(int argc, char **argv)
7421da177e4SLinus Torvalds {
74341f11a4fSYoshinori Sato 	if (argc >= 2) {
74441f11a4fSYoshinori Sato 		int i;
74541f11a4fSYoshinori Sato 		for (i = 1; i < argc; i++) {
74641f11a4fSYoshinori Sato 			if(strcmp(argv[i], "--all-symbols") == 0)
7471da177e4SLinus Torvalds 				all_symbols = 1;
748c6bda7c9SRusty Russell 			else if (strcmp(argv[i], "--absolute-percpu") == 0)
749c6bda7c9SRusty Russell 				absolute_percpu = 1;
750534c9f2eSMasahiro Yamada 			else if (strcmp(argv[i], "--base-relative") == 0)
7512213e9a6SArd Biesheuvel 				base_relative = 1;
7522213e9a6SArd Biesheuvel 			else
75341f11a4fSYoshinori Sato 				usage();
75441f11a4fSYoshinori Sato 		}
75541f11a4fSYoshinori Sato 	} else if (argc != 1)
7561da177e4SLinus Torvalds 		usage();
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	read_map(stdin);
759c6bda7c9SRusty Russell 	if (absolute_percpu)
760c6bda7c9SRusty Russell 		make_percpus_absolute();
7612213e9a6SArd Biesheuvel 	if (base_relative)
7622213e9a6SArd Biesheuvel 		record_relative_base();
763f2df3f65SPaulo Marques 	sort_symbols();
7641da177e4SLinus Torvalds 	optimize_token_table();
7651da177e4SLinus Torvalds 	write_src();
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	return 0;
7681da177e4SLinus Torvalds }
769