xref: /linux/scripts/kallsyms.c (revision 534c9f2ec4c92adbe8791125e7ba66d5023ad51f)
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;
652213e9a6SArd Biesheuvel static int base_relative = 0;
661da177e4SLinus Torvalds 
67b3dbb4ecSPaulo Marques int token_profit[0x10000];
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* the table that holds the result of the compression */
70b3dbb4ecSPaulo Marques unsigned char best_table[256][2];
711da177e4SLinus Torvalds unsigned char best_table_len[256];
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 
74b3dbb4ecSPaulo Marques static void usage(void)
751da177e4SLinus Torvalds {
76f6537f2fSMing Lei 	fprintf(stderr, "Usage: kallsyms [--all-symbols] "
772213e9a6SArd Biesheuvel 			"[--base-relative] < in.map > out.S\n");
781da177e4SLinus Torvalds 	exit(1);
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * This ignores the intensely annoying "mapping symbols" found
831da177e4SLinus Torvalds  * in ARM ELF files: $a, $t and $d.
841da177e4SLinus Torvalds  */
85b3dbb4ecSPaulo Marques static inline int is_arm_mapping_symbol(const char *str)
861da177e4SLinus Torvalds {
876c34f1f5SKyle McMartin 	return str[0] == '$' && strchr("axtd", str[1])
881da177e4SLinus Torvalds 	       && (str[2] == '\0' || str[2] == '.');
891da177e4SLinus Torvalds }
901da177e4SLinus Torvalds 
9178eb7159SKees Cook static int check_symbol_range(const char *sym, unsigned long long addr,
9278eb7159SKees Cook 			      struct addr_range *ranges, int entries)
9317b1f0deSMike Frysinger {
9417b1f0deSMike Frysinger 	size_t i;
9578eb7159SKees Cook 	struct addr_range *ar;
9617b1f0deSMike Frysinger 
9778eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
9878eb7159SKees Cook 		ar = &ranges[i];
9917b1f0deSMike Frysinger 
10078eb7159SKees Cook 		if (strcmp(sym, ar->start_sym) == 0) {
10178eb7159SKees Cook 			ar->start = addr;
10217b1f0deSMike Frysinger 			return 0;
10378eb7159SKees Cook 		} else if (strcmp(sym, ar->end_sym) == 0) {
10478eb7159SKees Cook 			ar->end = addr;
10517b1f0deSMike Frysinger 			return 0;
10617b1f0deSMike Frysinger 		}
10717b1f0deSMike Frysinger 	}
10817b1f0deSMike Frysinger 
10917b1f0deSMike Frysinger 	return 1;
11017b1f0deSMike Frysinger }
11117b1f0deSMike Frysinger 
112b3dbb4ecSPaulo Marques static int read_symbol(FILE *in, struct sym_entry *s)
1131da177e4SLinus Torvalds {
114*534c9f2eSMasahiro Yamada 	char sym[500], stype;
1151da177e4SLinus Torvalds 	int rc;
1161da177e4SLinus Torvalds 
117*534c9f2eSMasahiro Yamada 	rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, sym);
1181da177e4SLinus Torvalds 	if (rc != 3) {
119*534c9f2eSMasahiro Yamada 		if (rc != EOF && fgets(sym, 500, in) == NULL)
120ef894870SJean Sacren 			fprintf(stderr, "Read error or end of file.\n");
1211da177e4SLinus Torvalds 		return -1;
1221da177e4SLinus Torvalds 	}
123*534c9f2eSMasahiro Yamada 	if (strlen(sym) > KSYM_NAME_LEN) {
1246f62259bSFabio Estevam 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu vs %d).\n"
125f3462aa9SAndi Kleen 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
126*534c9f2eSMasahiro Yamada 			sym, strlen(sym), KSYM_NAME_LEN);
127f3462aa9SAndi Kleen 		return -1;
128f3462aa9SAndi Kleen 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	/* Ignore most absolute/undefined (?) symbols. */
131fd593d12SEric W. Biederman 	if (strcmp(sym, "_text") == 0)
132fd593d12SEric W. Biederman 		_text = s->addr;
13378eb7159SKees Cook 	else if (check_symbol_range(sym, s->addr, text_ranges,
13478eb7159SKees Cook 				    ARRAY_SIZE(text_ranges)) == 0)
13517b1f0deSMike Frysinger 		/* nothing to do */;
136b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'A')
1371da177e4SLinus Torvalds 	{
1381da177e4SLinus Torvalds 		/* Keep these useful absolute symbols */
13941f11a4fSYoshinori Sato 		if (strcmp(sym, "__kernel_syscall_via_break") &&
14041f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_syscall_via_epc") &&
14141f11a4fSYoshinori Sato 		    strcmp(sym, "__kernel_sigtramp") &&
14241f11a4fSYoshinori Sato 		    strcmp(sym, "__gp"))
1431da177e4SLinus Torvalds 			return -1;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	}
146b3dbb4ecSPaulo Marques 	else if (toupper(stype) == 'U' ||
14741f11a4fSYoshinori Sato 		 is_arm_mapping_symbol(sym))
1481da177e4SLinus Torvalds 		return -1;
1496f00df24SRalf Baechle 	/* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
150*534c9f2eSMasahiro Yamada 	else if (sym[0] == '$')
1516f00df24SRalf Baechle 		return -1;
152aab34ac8SSam Ravnborg 	/* exclude debugging symbols */
15351962a9dSGuenter Roeck 	else if (stype == 'N' || stype == 'n')
154aab34ac8SSam Ravnborg 		return -1;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	/* include the type field in the symbol name, so that it gets
1571da177e4SLinus Torvalds 	 * compressed together */
158*534c9f2eSMasahiro Yamada 	s->len = strlen(sym) + 1;
159b3dbb4ecSPaulo Marques 	s->sym = malloc(s->len + 1);
160f1a136e0SJesper Juhl 	if (!s->sym) {
161f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
162f1a136e0SJesper Juhl 			"unable to allocate required amount of memory\n");
163f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
164f1a136e0SJesper Juhl 	}
165*534c9f2eSMasahiro Yamada 	strcpy((char *)s->sym + 1, sym);
166b3dbb4ecSPaulo Marques 	s->sym[0] = stype;
1671da177e4SLinus Torvalds 
1688c996940SArd Biesheuvel 	s->percpu_absolute = 0;
1698c996940SArd Biesheuvel 
170c6bda7c9SRusty Russell 	/* Record if we've found __per_cpu_start/end. */
171c6bda7c9SRusty Russell 	check_symbol_range(sym, s->addr, &percpu_range, 1);
172c6bda7c9SRusty Russell 
1731da177e4SLinus Torvalds 	return 0;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
17678eb7159SKees Cook static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
17778eb7159SKees Cook 			   int entries)
17817b1f0deSMike Frysinger {
17917b1f0deSMike Frysinger 	size_t i;
18078eb7159SKees Cook 	struct addr_range *ar;
18117b1f0deSMike Frysinger 
18278eb7159SKees Cook 	for (i = 0; i < entries; ++i) {
18378eb7159SKees Cook 		ar = &ranges[i];
18417b1f0deSMike Frysinger 
18578eb7159SKees Cook 		if (s->addr >= ar->start && s->addr <= ar->end)
186ac6ca5c8SMike Frysinger 			return 1;
18717b1f0deSMike Frysinger 	}
18817b1f0deSMike Frysinger 
189ac6ca5c8SMike Frysinger 	return 0;
19017b1f0deSMike Frysinger }
19117b1f0deSMike Frysinger 
192b3dbb4ecSPaulo Marques static int symbol_valid(struct sym_entry *s)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	/* Symbols which vary between passes.  Passes 1 and 2 must have
1952ea03891SSam Ravnborg 	 * identical symbol lists.  The kallsyms_* symbols below are only added
1962ea03891SSam Ravnborg 	 * after pass 1, they would be included in pass 2 when --all-symbols is
1972ea03891SSam Ravnborg 	 * specified so exclude them to get a stable symbol list.
1981da177e4SLinus Torvalds 	 */
1991da177e4SLinus Torvalds 	static char *special_symbols[] = {
2002ea03891SSam Ravnborg 		"kallsyms_addresses",
2012213e9a6SArd Biesheuvel 		"kallsyms_offsets",
2022213e9a6SArd Biesheuvel 		"kallsyms_relative_base",
2032ea03891SSam Ravnborg 		"kallsyms_num_syms",
2042ea03891SSam Ravnborg 		"kallsyms_names",
2052ea03891SSam Ravnborg 		"kallsyms_markers",
2062ea03891SSam Ravnborg 		"kallsyms_token_table",
2072ea03891SSam Ravnborg 		"kallsyms_token_index",
2082ea03891SSam Ravnborg 
2091da177e4SLinus Torvalds 	/* Exclude linker generated symbols which vary between passes */
2101da177e4SLinus Torvalds 		"_SDA_BASE_",		/* ppc */
2111da177e4SLinus Torvalds 		"_SDA2_BASE_",		/* ppc */
2121da177e4SLinus Torvalds 		NULL };
213bd8b22d2SArd Biesheuvel 
21456067812SArd Biesheuvel 	static char *special_prefixes[] = {
21556067812SArd Biesheuvel 		"__crc_",		/* modversions */
2161212f7a1SArd Biesheuvel 		"__efistub_",		/* arm64 EFI stub namespace */
21756067812SArd Biesheuvel 		NULL };
21856067812SArd Biesheuvel 
219bd8b22d2SArd Biesheuvel 	static char *special_suffixes[] = {
220bd8b22d2SArd Biesheuvel 		"_veneer",		/* arm */
221b9b74be1SArd Biesheuvel 		"_from_arm",		/* arm */
222b9b74be1SArd Biesheuvel 		"_from_thumb",		/* arm */
223bd8b22d2SArd Biesheuvel 		NULL };
224bd8b22d2SArd Biesheuvel 
2251da177e4SLinus Torvalds 	int i;
226bd8b22d2SArd Biesheuvel 	char *sym_name = (char *)s->sym + 1;
227bd8b22d2SArd Biesheuvel 
2281da177e4SLinus Torvalds 	/* if --all-symbols is not specified, then symbols outside the text
2291da177e4SLinus Torvalds 	 * and inittext sections are discarded */
2301da177e4SLinus Torvalds 	if (!all_symbols) {
23178eb7159SKees Cook 		if (symbol_in_range(s, text_ranges,
23278eb7159SKees Cook 				    ARRAY_SIZE(text_ranges)) == 0)
2331da177e4SLinus Torvalds 			return 0;
2341da177e4SLinus Torvalds 		/* Corner case.  Discard any symbols with the same value as
235a3b81113SRobin Getz 		 * _etext _einittext; they can move between pass 1 and 2 when
236a3b81113SRobin Getz 		 * the kallsyms data are added.  If these symbols move then
237a3b81113SRobin Getz 		 * they may get dropped in pass 2, which breaks the kallsyms
238a3b81113SRobin Getz 		 * rules.
2391da177e4SLinus Torvalds 		 */
24017b1f0deSMike Frysinger 		if ((s->addr == text_range_text->end &&
241bd8b22d2SArd Biesheuvel 				strcmp(sym_name,
24278eb7159SKees Cook 				       text_range_text->end_sym)) ||
24317b1f0deSMike Frysinger 		    (s->addr == text_range_inittext->end &&
244bd8b22d2SArd Biesheuvel 				strcmp(sym_name,
24578eb7159SKees Cook 				       text_range_inittext->end_sym)))
2461da177e4SLinus Torvalds 			return 0;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	/* Exclude symbols which vary between passes. */
250bd8b22d2SArd Biesheuvel 	for (i = 0; special_symbols[i]; i++)
251bd8b22d2SArd Biesheuvel 		if (strcmp(sym_name, special_symbols[i]) == 0)
2521da177e4SLinus Torvalds 			return 0;
2531da177e4SLinus Torvalds 
25456067812SArd Biesheuvel 	for (i = 0; special_prefixes[i]; i++) {
25556067812SArd Biesheuvel 		int l = strlen(special_prefixes[i]);
25656067812SArd Biesheuvel 
25756067812SArd Biesheuvel 		if (l <= strlen(sym_name) &&
25856067812SArd Biesheuvel 		    strncmp(sym_name, special_prefixes[i], l) == 0)
25956067812SArd Biesheuvel 			return 0;
26056067812SArd Biesheuvel 	}
26156067812SArd Biesheuvel 
262bd8b22d2SArd Biesheuvel 	for (i = 0; special_suffixes[i]; i++) {
263bd8b22d2SArd Biesheuvel 		int l = strlen(sym_name) - strlen(special_suffixes[i]);
264bd8b22d2SArd Biesheuvel 
265bd8b22d2SArd Biesheuvel 		if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0)
2661da177e4SLinus Torvalds 			return 0;
267bd8b22d2SArd Biesheuvel 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	return 1;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
272b3dbb4ecSPaulo Marques static void read_map(FILE *in)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	while (!feof(in)) {
275b3dbb4ecSPaulo Marques 		if (table_cnt >= table_size) {
276b3dbb4ecSPaulo Marques 			table_size += 10000;
277b3dbb4ecSPaulo Marques 			table = realloc(table, sizeof(*table) * table_size);
2781da177e4SLinus Torvalds 			if (!table) {
2791da177e4SLinus Torvalds 				fprintf(stderr, "out of memory\n");
2801da177e4SLinus Torvalds 				exit (1);
2811da177e4SLinus Torvalds 			}
2821da177e4SLinus Torvalds 		}
283f2df3f65SPaulo Marques 		if (read_symbol(in, &table[table_cnt]) == 0) {
284f2df3f65SPaulo Marques 			table[table_cnt].start_pos = table_cnt;
285b3dbb4ecSPaulo Marques 			table_cnt++;
2861da177e4SLinus Torvalds 		}
2871da177e4SLinus Torvalds 	}
288f2df3f65SPaulo Marques }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds static void output_label(char *label)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	printf(".globl %s\n", label);
2931da177e4SLinus Torvalds 	printf("\tALGN\n");
2941da177e4SLinus Torvalds 	printf("%s:\n", label);
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds /* uncompress a compressed symbol. When this function is called, the best table
2981da177e4SLinus Torvalds  * might still be compressed itself, so the function needs to be recursive */
2991da177e4SLinus Torvalds static int expand_symbol(unsigned char *data, int len, char *result)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	int c, rlen, total=0;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	while (len) {
3041da177e4SLinus Torvalds 		c = *data;
3051da177e4SLinus Torvalds 		/* if the table holds a single char that is the same as the one
3061da177e4SLinus Torvalds 		 * we are looking for, then end the search */
3071da177e4SLinus Torvalds 		if (best_table[c][0]==c && best_table_len[c]==1) {
3081da177e4SLinus Torvalds 			*result++ = c;
3091da177e4SLinus Torvalds 			total++;
3101da177e4SLinus Torvalds 		} else {
3111da177e4SLinus Torvalds 			/* if not, recurse and expand */
3121da177e4SLinus Torvalds 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
3131da177e4SLinus Torvalds 			total += rlen;
3141da177e4SLinus Torvalds 			result += rlen;
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 		data++;
3171da177e4SLinus Torvalds 		len--;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	*result=0;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	return total;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
32478eb7159SKees Cook static int symbol_absolute(struct sym_entry *s)
32578eb7159SKees Cook {
3268c996940SArd Biesheuvel 	return s->percpu_absolute;
32778eb7159SKees Cook }
32878eb7159SKees Cook 
329b3dbb4ecSPaulo Marques static void write_src(void)
3301da177e4SLinus Torvalds {
331b3dbb4ecSPaulo Marques 	unsigned int i, k, off;
3321da177e4SLinus Torvalds 	unsigned int best_idx[256];
3331da177e4SLinus Torvalds 	unsigned int *markers;
3349281aceaSTejun Heo 	char buf[KSYM_NAME_LEN];
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	printf("#include <asm/types.h>\n");
3371da177e4SLinus Torvalds 	printf("#if BITS_PER_LONG == 64\n");
3381da177e4SLinus Torvalds 	printf("#define PTR .quad\n");
3391da177e4SLinus Torvalds 	printf("#define ALGN .align 8\n");
3401da177e4SLinus Torvalds 	printf("#else\n");
3411da177e4SLinus Torvalds 	printf("#define PTR .long\n");
3421da177e4SLinus Torvalds 	printf("#define ALGN .align 4\n");
3431da177e4SLinus Torvalds 	printf("#endif\n");
3441da177e4SLinus Torvalds 
345aad09470SJan Beulich 	printf("\t.section .rodata, \"a\"\n");
3461da177e4SLinus Torvalds 
3472213e9a6SArd Biesheuvel 	/* Provide proper symbols relocatability by their relativeness
3482213e9a6SArd Biesheuvel 	 * to a fixed anchor point in the runtime image, either '_text'
3492213e9a6SArd Biesheuvel 	 * for absolute address tables, in which case the linker will
3502213e9a6SArd Biesheuvel 	 * emit the final addresses at build time. Otherwise, use the
3512213e9a6SArd Biesheuvel 	 * offset relative to the lowest value encountered of all relative
3522213e9a6SArd Biesheuvel 	 * symbols, and emit non-relocatable fixed offsets that will be fixed
3532213e9a6SArd Biesheuvel 	 * up at runtime.
3542213e9a6SArd Biesheuvel 	 *
3552213e9a6SArd Biesheuvel 	 * The symbol names cannot be used to construct normal symbol
3562213e9a6SArd Biesheuvel 	 * references as the list of symbols contains symbols that are
3572213e9a6SArd Biesheuvel 	 * declared static and are private to their .o files.  This prevents
3582213e9a6SArd Biesheuvel 	 * .tmp_kallsyms.o or any other object from referencing them.
359fd593d12SEric W. Biederman 	 */
3602213e9a6SArd Biesheuvel 	if (!base_relative)
3611da177e4SLinus Torvalds 		output_label("kallsyms_addresses");
3622213e9a6SArd Biesheuvel 	else
3632213e9a6SArd Biesheuvel 		output_label("kallsyms_offsets");
3642213e9a6SArd Biesheuvel 
365b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
3662213e9a6SArd Biesheuvel 		if (base_relative) {
3672213e9a6SArd Biesheuvel 			long long offset;
3682213e9a6SArd Biesheuvel 			int overflow;
3692213e9a6SArd Biesheuvel 
3702213e9a6SArd Biesheuvel 			if (!absolute_percpu) {
3712213e9a6SArd Biesheuvel 				offset = table[i].addr - relative_base;
3722213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > UINT_MAX);
3732213e9a6SArd Biesheuvel 			} else if (symbol_absolute(&table[i])) {
3742213e9a6SArd Biesheuvel 				offset = table[i].addr;
3752213e9a6SArd Biesheuvel 				overflow = (offset < 0 || offset > INT_MAX);
3762213e9a6SArd Biesheuvel 			} else {
3772213e9a6SArd Biesheuvel 				offset = relative_base - table[i].addr - 1;
3782213e9a6SArd Biesheuvel 				overflow = (offset < INT_MIN || offset >= 0);
3792213e9a6SArd Biesheuvel 			}
3802213e9a6SArd Biesheuvel 			if (overflow) {
3812213e9a6SArd Biesheuvel 				fprintf(stderr, "kallsyms failure: "
3822213e9a6SArd Biesheuvel 					"%s symbol value %#llx out of range in relative mode\n",
3832213e9a6SArd Biesheuvel 					symbol_absolute(&table[i]) ? "absolute" : "relative",
3842213e9a6SArd Biesheuvel 					table[i].addr);
3852213e9a6SArd Biesheuvel 				exit(EXIT_FAILURE);
3862213e9a6SArd Biesheuvel 			}
3872213e9a6SArd Biesheuvel 			printf("\t.long\t%#x\n", (int)offset);
3882213e9a6SArd Biesheuvel 		} else if (!symbol_absolute(&table[i])) {
3892c22d8baSVivek Goyal 			if (_text <= table[i].addr)
390fd593d12SEric W. Biederman 				printf("\tPTR\t_text + %#llx\n",
391fd593d12SEric W. Biederman 					table[i].addr - _text);
3922c22d8baSVivek Goyal 			else
3932930ffc7SAndrew Morton 				printf("\tPTR\t_text - %#llx\n",
3942930ffc7SAndrew Morton 					_text - table[i].addr);
395fd593d12SEric W. Biederman 		} else {
3961da177e4SLinus Torvalds 			printf("\tPTR\t%#llx\n", table[i].addr);
3971da177e4SLinus Torvalds 		}
398fd593d12SEric W. Biederman 	}
3991da177e4SLinus Torvalds 	printf("\n");
4001da177e4SLinus Torvalds 
4012213e9a6SArd Biesheuvel 	if (base_relative) {
4022213e9a6SArd Biesheuvel 		output_label("kallsyms_relative_base");
4032213e9a6SArd Biesheuvel 		printf("\tPTR\t_text - %#llx\n", _text - relative_base);
4042213e9a6SArd Biesheuvel 		printf("\n");
4052213e9a6SArd Biesheuvel 	}
4062213e9a6SArd Biesheuvel 
4071da177e4SLinus Torvalds 	output_label("kallsyms_num_syms");
408b3dbb4ecSPaulo Marques 	printf("\tPTR\t%d\n", table_cnt);
4091da177e4SLinus Torvalds 	printf("\n");
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	/* table of offset markers, that give the offset in the compressed stream
4121da177e4SLinus Torvalds 	 * every 256 symbols */
413f1a136e0SJesper Juhl 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
414f1a136e0SJesper Juhl 	if (!markers) {
415f1a136e0SJesper Juhl 		fprintf(stderr, "kallsyms failure: "
416f1a136e0SJesper Juhl 			"unable to allocate required memory\n");
417f1a136e0SJesper Juhl 		exit(EXIT_FAILURE);
418f1a136e0SJesper Juhl 	}
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	output_label("kallsyms_names");
4211da177e4SLinus Torvalds 	off = 0;
422b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
423b3dbb4ecSPaulo Marques 		if ((i & 0xFF) == 0)
424b3dbb4ecSPaulo Marques 			markers[i >> 8] = off;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 		printf("\t.byte 0x%02x", table[i].len);
4271da177e4SLinus Torvalds 		for (k = 0; k < table[i].len; k++)
4281da177e4SLinus Torvalds 			printf(", 0x%02x", table[i].sym[k]);
4291da177e4SLinus Torvalds 		printf("\n");
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		off += table[i].len + 1;
4321da177e4SLinus Torvalds 	}
4331da177e4SLinus Torvalds 	printf("\n");
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	output_label("kallsyms_markers");
436b3dbb4ecSPaulo Marques 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
4371da177e4SLinus Torvalds 		printf("\tPTR\t%d\n", markers[i]);
4381da177e4SLinus Torvalds 	printf("\n");
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	free(markers);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 	output_label("kallsyms_token_table");
4431da177e4SLinus Torvalds 	off = 0;
4441da177e4SLinus Torvalds 	for (i = 0; i < 256; i++) {
4451da177e4SLinus Torvalds 		best_idx[i] = off;
4461da177e4SLinus Torvalds 		expand_symbol(best_table[i], best_table_len[i], buf);
4471da177e4SLinus Torvalds 		printf("\t.asciz\t\"%s\"\n", buf);
4481da177e4SLinus Torvalds 		off += strlen(buf) + 1;
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	printf("\n");
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	output_label("kallsyms_token_index");
4531da177e4SLinus Torvalds 	for (i = 0; i < 256; i++)
4541da177e4SLinus Torvalds 		printf("\t.short\t%d\n", best_idx[i]);
4551da177e4SLinus Torvalds 	printf("\n");
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /* table lookup compression functions */
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds /* count all the possible tokens in a symbol */
4621da177e4SLinus Torvalds static void learn_symbol(unsigned char *symbol, int len)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	int i;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
467b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
4681da177e4SLinus Torvalds }
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds /* decrease the count for all the possible tokens in a symbol */
4711da177e4SLinus Torvalds static void forget_symbol(unsigned char *symbol, int len)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	int i;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	for (i = 0; i < len - 1; i++)
476b3dbb4ecSPaulo Marques 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
479b3dbb4ecSPaulo Marques /* remove all the invalid symbols from the table and do the initial token count */
4801da177e4SLinus Torvalds static void build_initial_tok_table(void)
4811da177e4SLinus Torvalds {
482b3dbb4ecSPaulo Marques 	unsigned int i, pos;
4831da177e4SLinus Torvalds 
484b3dbb4ecSPaulo Marques 	pos = 0;
485b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
4861da177e4SLinus Torvalds 		if ( symbol_valid(&table[i]) ) {
487b3dbb4ecSPaulo Marques 			if (pos != i)
488b3dbb4ecSPaulo Marques 				table[pos] = table[i];
489b3dbb4ecSPaulo Marques 			learn_symbol(table[pos].sym, table[pos].len);
490b3dbb4ecSPaulo Marques 			pos++;
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 	}
493b3dbb4ecSPaulo Marques 	table_cnt = pos;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4967c5d249aSPaulo Marques static void *find_token(unsigned char *str, int len, unsigned char *token)
4977c5d249aSPaulo Marques {
4987c5d249aSPaulo Marques 	int i;
4997c5d249aSPaulo Marques 
5007c5d249aSPaulo Marques 	for (i = 0; i < len - 1; i++) {
5017c5d249aSPaulo Marques 		if (str[i] == token[0] && str[i+1] == token[1])
5027c5d249aSPaulo Marques 			return &str[i];
5037c5d249aSPaulo Marques 	}
5047c5d249aSPaulo Marques 	return NULL;
5057c5d249aSPaulo Marques }
5067c5d249aSPaulo Marques 
5071da177e4SLinus Torvalds /* replace a given token in all the valid symbols. Use the sampled symbols
5081da177e4SLinus Torvalds  * to update the counts */
509b3dbb4ecSPaulo Marques static void compress_symbols(unsigned char *str, int idx)
5101da177e4SLinus Torvalds {
511b3dbb4ecSPaulo Marques 	unsigned int i, len, size;
512b3dbb4ecSPaulo Marques 	unsigned char *p1, *p2;
5131da177e4SLinus Torvalds 
514b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		len = table[i].len;
517b3dbb4ecSPaulo Marques 		p1 = table[i].sym;
518b3dbb4ecSPaulo Marques 
519b3dbb4ecSPaulo Marques 		/* find the token on the symbol */
5207c5d249aSPaulo Marques 		p2 = find_token(p1, len, str);
521b3dbb4ecSPaulo Marques 		if (!p2) continue;
522b3dbb4ecSPaulo Marques 
523b3dbb4ecSPaulo Marques 		/* decrease the counts for this symbol's tokens */
524b3dbb4ecSPaulo Marques 		forget_symbol(table[i].sym, len);
525b3dbb4ecSPaulo Marques 
526b3dbb4ecSPaulo Marques 		size = len;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 		do {
529b3dbb4ecSPaulo Marques 			*p2 = idx;
530b3dbb4ecSPaulo Marques 			p2++;
531b3dbb4ecSPaulo Marques 			size -= (p2 - p1);
532b3dbb4ecSPaulo Marques 			memmove(p2, p2 + 1, size);
533b3dbb4ecSPaulo Marques 			p1 = p2;
534b3dbb4ecSPaulo Marques 			len--;
535b3dbb4ecSPaulo Marques 
536b3dbb4ecSPaulo Marques 			if (size < 2) break;
537b3dbb4ecSPaulo Marques 
5381da177e4SLinus Torvalds 			/* find the token on the symbol */
5397c5d249aSPaulo Marques 			p2 = find_token(p1, size, str);
5401da177e4SLinus Torvalds 
541b3dbb4ecSPaulo Marques 		} while (p2);
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 		table[i].len = len;
544b3dbb4ecSPaulo Marques 
545b3dbb4ecSPaulo Marques 		/* increase the counts for this symbol's new tokens */
5461da177e4SLinus Torvalds 		learn_symbol(table[i].sym, len);
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /* search the token with the maximum profit */
551b3dbb4ecSPaulo Marques static int find_best_token(void)
5521da177e4SLinus Torvalds {
553b3dbb4ecSPaulo Marques 	int i, best, bestprofit;
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	bestprofit=-10000;
556b3dbb4ecSPaulo Marques 	best = 0;
5571da177e4SLinus Torvalds 
558b3dbb4ecSPaulo Marques 	for (i = 0; i < 0x10000; i++) {
559b3dbb4ecSPaulo Marques 		if (token_profit[i] > bestprofit) {
560b3dbb4ecSPaulo Marques 			best = i;
561b3dbb4ecSPaulo Marques 			bestprofit = token_profit[i];
5621da177e4SLinus Torvalds 		}
5631da177e4SLinus Torvalds 	}
5641da177e4SLinus Torvalds 	return best;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds /* this is the core of the algorithm: calculate the "best" table */
5681da177e4SLinus Torvalds static void optimize_result(void)
5691da177e4SLinus Torvalds {
570b3dbb4ecSPaulo Marques 	int i, best;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/* using the '\0' symbol last allows compress_symbols to use standard
5731da177e4SLinus Torvalds 	 * fast string functions */
5741da177e4SLinus Torvalds 	for (i = 255; i >= 0; i--) {
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 		/* if this table slot is empty (it is not used by an actual
5771da177e4SLinus Torvalds 		 * original char code */
5781da177e4SLinus Torvalds 		if (!best_table_len[i]) {
5791da177e4SLinus Torvalds 
580cbf7a90eSCao jin 			/* find the token with the best profit value */
5811da177e4SLinus Torvalds 			best = find_best_token();
582e0a04b11SXiaochen Wang 			if (token_profit[best] == 0)
583e0a04b11SXiaochen Wang 				break;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 			/* place it in the "best" table */
586b3dbb4ecSPaulo Marques 			best_table_len[i] = 2;
587b3dbb4ecSPaulo Marques 			best_table[i][0] = best & 0xFF;
588b3dbb4ecSPaulo Marques 			best_table[i][1] = (best >> 8) & 0xFF;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 			/* replace this token in all the valid symbols */
591b3dbb4ecSPaulo Marques 			compress_symbols(best_table[i], i);
5921da177e4SLinus Torvalds 		}
5931da177e4SLinus Torvalds 	}
5941da177e4SLinus Torvalds }
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds /* start by placing the symbols that are actually used on the table */
5971da177e4SLinus Torvalds static void insert_real_symbols_in_table(void)
5981da177e4SLinus Torvalds {
599b3dbb4ecSPaulo Marques 	unsigned int i, j, c;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	memset(best_table, 0, sizeof(best_table));
6021da177e4SLinus Torvalds 	memset(best_table_len, 0, sizeof(best_table_len));
6031da177e4SLinus Torvalds 
604b3dbb4ecSPaulo Marques 	for (i = 0; i < table_cnt; i++) {
6051da177e4SLinus Torvalds 		for (j = 0; j < table[i].len; j++) {
6061da177e4SLinus Torvalds 			c = table[i].sym[j];
6071da177e4SLinus Torvalds 			best_table[c][0]=c;
6081da177e4SLinus Torvalds 			best_table_len[c]=1;
6091da177e4SLinus Torvalds 		}
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds static void optimize_token_table(void)
6141da177e4SLinus Torvalds {
6151da177e4SLinus Torvalds 	build_initial_tok_table();
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	insert_real_symbols_in_table();
6181da177e4SLinus Torvalds 
61941f11a4fSYoshinori Sato 	/* When valid symbol is not registered, exit to error */
620b3dbb4ecSPaulo Marques 	if (!table_cnt) {
62141f11a4fSYoshinori Sato 		fprintf(stderr, "No valid symbol.\n");
62241f11a4fSYoshinori Sato 		exit(1);
62341f11a4fSYoshinori Sato 	}
62441f11a4fSYoshinori Sato 
6251da177e4SLinus Torvalds 	optimize_result();
6261da177e4SLinus Torvalds }
6271da177e4SLinus Torvalds 
628b478b782SLai Jiangshan /* guess for "linker script provide" symbol */
629b478b782SLai Jiangshan static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
630b478b782SLai Jiangshan {
631b478b782SLai Jiangshan 	const char *symbol = (char *)se->sym + 1;
632b478b782SLai Jiangshan 	int len = se->len - 1;
633b478b782SLai Jiangshan 
634b478b782SLai Jiangshan 	if (len < 8)
635b478b782SLai Jiangshan 		return 0;
636b478b782SLai Jiangshan 
637b478b782SLai Jiangshan 	if (symbol[0] != '_' || symbol[1] != '_')
638b478b782SLai Jiangshan 		return 0;
639b478b782SLai Jiangshan 
640b478b782SLai Jiangshan 	/* __start_XXXXX */
641b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "start_", 6))
642b478b782SLai Jiangshan 		return 1;
643b478b782SLai Jiangshan 
644b478b782SLai Jiangshan 	/* __stop_XXXXX */
645b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "stop_", 5))
646b478b782SLai Jiangshan 		return 1;
647b478b782SLai Jiangshan 
648b478b782SLai Jiangshan 	/* __end_XXXXX */
649b478b782SLai Jiangshan 	if (!memcmp(symbol + 2, "end_", 4))
650b478b782SLai Jiangshan 		return 1;
651b478b782SLai Jiangshan 
652b478b782SLai Jiangshan 	/* __XXXXX_start */
653b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 6, "_start", 6))
654b478b782SLai Jiangshan 		return 1;
655b478b782SLai Jiangshan 
656b478b782SLai Jiangshan 	/* __XXXXX_end */
657b478b782SLai Jiangshan 	if (!memcmp(symbol + len - 4, "_end", 4))
658b478b782SLai Jiangshan 		return 1;
659b478b782SLai Jiangshan 
660b478b782SLai Jiangshan 	return 0;
661b478b782SLai Jiangshan }
662b478b782SLai Jiangshan 
663b478b782SLai Jiangshan static int prefix_underscores_count(const char *str)
664b478b782SLai Jiangshan {
665b478b782SLai Jiangshan 	const char *tail = str;
666b478b782SLai Jiangshan 
667a9ece53cSPaul Mundt 	while (*tail == '_')
668b478b782SLai Jiangshan 		tail++;
669b478b782SLai Jiangshan 
670b478b782SLai Jiangshan 	return tail - str;
671b478b782SLai Jiangshan }
672b478b782SLai Jiangshan 
673f2df3f65SPaulo Marques static int compare_symbols(const void *a, const void *b)
674f2df3f65SPaulo Marques {
675f2df3f65SPaulo Marques 	const struct sym_entry *sa;
676f2df3f65SPaulo Marques 	const struct sym_entry *sb;
677f2df3f65SPaulo Marques 	int wa, wb;
678f2df3f65SPaulo Marques 
679f2df3f65SPaulo Marques 	sa = a;
680f2df3f65SPaulo Marques 	sb = b;
681f2df3f65SPaulo Marques 
682f2df3f65SPaulo Marques 	/* sort by address first */
683f2df3f65SPaulo Marques 	if (sa->addr > sb->addr)
684f2df3f65SPaulo Marques 		return 1;
685f2df3f65SPaulo Marques 	if (sa->addr < sb->addr)
686f2df3f65SPaulo Marques 		return -1;
687f2df3f65SPaulo Marques 
688f2df3f65SPaulo Marques 	/* sort by "weakness" type */
689f2df3f65SPaulo Marques 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
690f2df3f65SPaulo Marques 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
691f2df3f65SPaulo Marques 	if (wa != wb)
692f2df3f65SPaulo Marques 		return wa - wb;
693f2df3f65SPaulo Marques 
694b478b782SLai Jiangshan 	/* sort by "linker script provide" type */
695b478b782SLai Jiangshan 	wa = may_be_linker_script_provide_symbol(sa);
696b478b782SLai Jiangshan 	wb = may_be_linker_script_provide_symbol(sb);
697b478b782SLai Jiangshan 	if (wa != wb)
698b478b782SLai Jiangshan 		return wa - wb;
699b478b782SLai Jiangshan 
700b478b782SLai Jiangshan 	/* sort by the number of prefix underscores */
701b478b782SLai Jiangshan 	wa = prefix_underscores_count((const char *)sa->sym + 1);
702b478b782SLai Jiangshan 	wb = prefix_underscores_count((const char *)sb->sym + 1);
703b478b782SLai Jiangshan 	if (wa != wb)
704b478b782SLai Jiangshan 		return wa - wb;
705b478b782SLai Jiangshan 
706f2df3f65SPaulo Marques 	/* sort by initial order, so that other symbols are left undisturbed */
707f2df3f65SPaulo Marques 	return sa->start_pos - sb->start_pos;
708f2df3f65SPaulo Marques }
709f2df3f65SPaulo Marques 
710f2df3f65SPaulo Marques static void sort_symbols(void)
711f2df3f65SPaulo Marques {
712f2df3f65SPaulo Marques 	qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
713f2df3f65SPaulo Marques }
7141da177e4SLinus Torvalds 
715c6bda7c9SRusty Russell static void make_percpus_absolute(void)
716c6bda7c9SRusty Russell {
717c6bda7c9SRusty Russell 	unsigned int i;
718c6bda7c9SRusty Russell 
719c6bda7c9SRusty Russell 	for (i = 0; i < table_cnt; i++)
7208c996940SArd Biesheuvel 		if (symbol_in_range(&table[i], &percpu_range, 1)) {
7218c996940SArd Biesheuvel 			/*
7228c996940SArd Biesheuvel 			 * Keep the 'A' override for percpu symbols to
7238c996940SArd Biesheuvel 			 * ensure consistent behavior compared to older
7248c996940SArd Biesheuvel 			 * versions of this tool.
7258c996940SArd Biesheuvel 			 */
726c6bda7c9SRusty Russell 			table[i].sym[0] = 'A';
7278c996940SArd Biesheuvel 			table[i].percpu_absolute = 1;
7288c996940SArd Biesheuvel 		}
729c6bda7c9SRusty Russell }
730c6bda7c9SRusty Russell 
7312213e9a6SArd Biesheuvel /* find the minimum non-absolute symbol address */
7322213e9a6SArd Biesheuvel static void record_relative_base(void)
7332213e9a6SArd Biesheuvel {
7342213e9a6SArd Biesheuvel 	unsigned int i;
7352213e9a6SArd Biesheuvel 
7362213e9a6SArd Biesheuvel 	relative_base = -1ULL;
7372213e9a6SArd Biesheuvel 	for (i = 0; i < table_cnt; i++)
7382213e9a6SArd Biesheuvel 		if (!symbol_absolute(&table[i]) &&
7392213e9a6SArd Biesheuvel 		    table[i].addr < relative_base)
7402213e9a6SArd Biesheuvel 			relative_base = table[i].addr;
7412213e9a6SArd Biesheuvel }
7422213e9a6SArd Biesheuvel 
743b3dbb4ecSPaulo Marques int main(int argc, char **argv)
7441da177e4SLinus Torvalds {
74541f11a4fSYoshinori Sato 	if (argc >= 2) {
74641f11a4fSYoshinori Sato 		int i;
74741f11a4fSYoshinori Sato 		for (i = 1; i < argc; i++) {
74841f11a4fSYoshinori Sato 			if(strcmp(argv[i], "--all-symbols") == 0)
7491da177e4SLinus Torvalds 				all_symbols = 1;
750c6bda7c9SRusty Russell 			else if (strcmp(argv[i], "--absolute-percpu") == 0)
751c6bda7c9SRusty Russell 				absolute_percpu = 1;
752*534c9f2eSMasahiro Yamada 			else if (strcmp(argv[i], "--base-relative") == 0)
7532213e9a6SArd Biesheuvel 				base_relative = 1;
7542213e9a6SArd Biesheuvel 			else
75541f11a4fSYoshinori Sato 				usage();
75641f11a4fSYoshinori Sato 		}
75741f11a4fSYoshinori Sato 	} else if (argc != 1)
7581da177e4SLinus Torvalds 		usage();
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	read_map(stdin);
761c6bda7c9SRusty Russell 	if (absolute_percpu)
762c6bda7c9SRusty Russell 		make_percpus_absolute();
7632213e9a6SArd Biesheuvel 	if (base_relative)
7642213e9a6SArd Biesheuvel 		record_relative_base();
765f2df3f65SPaulo Marques 	sort_symbols();
7661da177e4SLinus Torvalds 	optimize_token_table();
7671da177e4SLinus Torvalds 	write_src();
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	return 0;
7701da177e4SLinus Torvalds }
771