xref: /linux/scripts/kallsyms.c (revision 6e8c5bbd5e83e649251c198e743c8b9e7c48372b)
1 /* Generate assembler source containing symbol information
2  *
3  * Copyright 2002       by Kai Germaschewski
4  *
5  * This software may be used and distributed according to the terms
6  * of the GNU General Public License, incorporated herein by reference.
7  *
8  * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
9  *
10  *      Table compression uses all the unused char codes on the symbols and
11  *  maps these to the most used substrings (tokens). For instance, it might
12  *  map char code 0xF7 to represent "write_" and then in every symbol where
13  *  "write_" appears it can be replaced by 0xF7, saving 5 bytes.
14  *      The used codes themselves are also placed in the table so that the
15  *  decompresion can work without "special cases".
16  *      Applied to kernel symbols, this usually produces a compression ratio
17  *  of about 50%.
18  *
19  */
20 
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <limits.h>
27 
28 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
29 
30 #define _stringify_1(x)	#x
31 #define _stringify(x)	_stringify_1(x)
32 
33 #define KSYM_NAME_LEN		128
34 
35 /*
36  * A substantially bigger size than the current maximum.
37  *
38  * It cannot be defined as an expression because it gets stringified
39  * for the fscanf() format string. Therefore, a _Static_assert() is
40  * used instead to maintain the relationship with KSYM_NAME_LEN.
41  */
42 #define KSYM_NAME_LEN_BUFFER	512
43 _Static_assert(
44 	KSYM_NAME_LEN_BUFFER == KSYM_NAME_LEN * 4,
45 	"Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN"
46 );
47 
48 struct sym_entry {
49 	unsigned long long addr;
50 	unsigned int len;
51 	unsigned int start_pos;
52 	unsigned int percpu_absolute;
53 	unsigned char sym[];
54 };
55 
56 struct addr_range {
57 	const char *start_sym, *end_sym;
58 	unsigned long long start, end;
59 };
60 
61 static unsigned long long _text;
62 static unsigned long long relative_base;
63 static struct addr_range text_ranges[] = {
64 	{ "_stext",     "_etext"     },
65 	{ "_sinittext", "_einittext" },
66 };
67 #define text_range_text     (&text_ranges[0])
68 #define text_range_inittext (&text_ranges[1])
69 
70 static struct addr_range percpu_range = {
71 	"__per_cpu_start", "__per_cpu_end", -1ULL, 0
72 };
73 
74 static struct sym_entry **table;
75 static unsigned int table_size, table_cnt;
76 static int all_symbols;
77 static int absolute_percpu;
78 static int base_relative;
79 
80 static int token_profit[0x10000];
81 
82 /* the table that holds the result of the compression */
83 static unsigned char best_table[256][2];
84 static unsigned char best_table_len[256];
85 
86 
87 static void usage(void)
88 {
89 	fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
90 			"[--base-relative] < in.map > out.S\n");
91 	exit(1);
92 }
93 
94 static char *sym_name(const struct sym_entry *s)
95 {
96 	return (char *)s->sym + 1;
97 }
98 
99 static bool is_ignored_symbol(const char *name, char type)
100 {
101 	/* Symbol names that exactly match to the following are ignored.*/
102 	static const char * const ignored_symbols[] = {
103 		/*
104 		 * Symbols which vary between passes. Passes 1 and 2 must have
105 		 * identical symbol lists. The kallsyms_* symbols below are
106 		 * only added after pass 1, they would be included in pass 2
107 		 * when --all-symbols is specified so exclude them to get a
108 		 * stable symbol list.
109 		 */
110 		"kallsyms_addresses",
111 		"kallsyms_offsets",
112 		"kallsyms_relative_base",
113 		"kallsyms_num_syms",
114 		"kallsyms_names",
115 		"kallsyms_markers",
116 		"kallsyms_token_table",
117 		"kallsyms_token_index",
118 		/* Exclude linker generated symbols which vary between passes */
119 		"_SDA_BASE_",		/* ppc */
120 		"_SDA2_BASE_",		/* ppc */
121 		NULL
122 	};
123 
124 	/* Symbol names that begin with the following are ignored.*/
125 	static const char * const ignored_prefixes[] = {
126 		"$",			/* local symbols for ARM, MIPS, etc. */
127 		".L",			/* local labels, .LBB,.Ltmpxxx,.L__unnamed_xx,.LASANPC, etc. */
128 		"__crc_",		/* modversions */
129 		"__efistub_",		/* arm64 EFI stub namespace */
130 		"__kvm_nvhe_$",		/* arm64 local symbols in non-VHE KVM namespace */
131 		"__kvm_nvhe_.L",	/* arm64 local symbols in non-VHE KVM namespace */
132 		"__AArch64ADRPThunk_",	/* arm64 lld */
133 		"__ARMV5PILongThunk_",	/* arm lld */
134 		"__ARMV7PILongThunk_",
135 		"__ThumbV7PILongThunk_",
136 		"__LA25Thunk_",		/* mips lld */
137 		"__microLA25Thunk_",
138 		NULL
139 	};
140 
141 	/* Symbol names that end with the following are ignored.*/
142 	static const char * const ignored_suffixes[] = {
143 		"_from_arm",		/* arm */
144 		"_from_thumb",		/* arm */
145 		"_veneer",		/* arm */
146 		NULL
147 	};
148 
149 	/* Symbol names that contain the following are ignored.*/
150 	static const char * const ignored_matches[] = {
151 		".long_branch.",	/* ppc stub */
152 		".plt_branch.",		/* ppc stub */
153 		NULL
154 	};
155 
156 	const char * const *p;
157 
158 	for (p = ignored_symbols; *p; p++)
159 		if (!strcmp(name, *p))
160 			return true;
161 
162 	for (p = ignored_prefixes; *p; p++)
163 		if (!strncmp(name, *p, strlen(*p)))
164 			return true;
165 
166 	for (p = ignored_suffixes; *p; p++) {
167 		int l = strlen(name) - strlen(*p);
168 
169 		if (l >= 0 && !strcmp(name + l, *p))
170 			return true;
171 	}
172 
173 	for (p = ignored_matches; *p; p++) {
174 		if (strstr(name, *p))
175 			return true;
176 	}
177 
178 	if (type == 'U' || type == 'u')
179 		return true;
180 	/* exclude debugging symbols */
181 	if (type == 'N' || type == 'n')
182 		return true;
183 
184 	if (toupper(type) == 'A') {
185 		/* Keep these useful absolute symbols */
186 		if (strcmp(name, "__kernel_syscall_via_break") &&
187 		    strcmp(name, "__kernel_syscall_via_epc") &&
188 		    strcmp(name, "__kernel_sigtramp") &&
189 		    strcmp(name, "__gp"))
190 			return true;
191 	}
192 
193 	return false;
194 }
195 
196 static void check_symbol_range(const char *sym, unsigned long long addr,
197 			       struct addr_range *ranges, int entries)
198 {
199 	size_t i;
200 	struct addr_range *ar;
201 
202 	for (i = 0; i < entries; ++i) {
203 		ar = &ranges[i];
204 
205 		if (strcmp(sym, ar->start_sym) == 0) {
206 			ar->start = addr;
207 			return;
208 		} else if (strcmp(sym, ar->end_sym) == 0) {
209 			ar->end = addr;
210 			return;
211 		}
212 	}
213 }
214 
215 static struct sym_entry *read_symbol(FILE *in)
216 {
217 	char name[KSYM_NAME_LEN_BUFFER+1], type;
218 	unsigned long long addr;
219 	unsigned int len;
220 	struct sym_entry *sym;
221 	int rc;
222 
223 	rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
224 	if (rc != 3) {
225 		if (rc != EOF && fgets(name, ARRAY_SIZE(name), in) == NULL)
226 			fprintf(stderr, "Read error or end of file.\n");
227 		return NULL;
228 	}
229 	if (strlen(name) >= KSYM_NAME_LEN) {
230 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
231 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
232 			name, strlen(name), KSYM_NAME_LEN);
233 		return NULL;
234 	}
235 
236 	if (strcmp(name, "_text") == 0)
237 		_text = addr;
238 
239 	/* Ignore most absolute/undefined (?) symbols. */
240 	if (is_ignored_symbol(name, type))
241 		return NULL;
242 
243 	check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
244 	check_symbol_range(name, addr, &percpu_range, 1);
245 
246 	/* include the type field in the symbol name, so that it gets
247 	 * compressed together */
248 
249 	len = strlen(name) + 1;
250 
251 	sym = malloc(sizeof(*sym) + len + 1);
252 	if (!sym) {
253 		fprintf(stderr, "kallsyms failure: "
254 			"unable to allocate required amount of memory\n");
255 		exit(EXIT_FAILURE);
256 	}
257 	sym->addr = addr;
258 	sym->len = len;
259 	sym->sym[0] = type;
260 	strcpy(sym_name(sym), name);
261 	sym->percpu_absolute = 0;
262 
263 	return sym;
264 }
265 
266 static int symbol_in_range(const struct sym_entry *s,
267 			   const struct addr_range *ranges, int entries)
268 {
269 	size_t i;
270 	const struct addr_range *ar;
271 
272 	for (i = 0; i < entries; ++i) {
273 		ar = &ranges[i];
274 
275 		if (s->addr >= ar->start && s->addr <= ar->end)
276 			return 1;
277 	}
278 
279 	return 0;
280 }
281 
282 static int symbol_valid(const struct sym_entry *s)
283 {
284 	const char *name = sym_name(s);
285 
286 	/* if --all-symbols is not specified, then symbols outside the text
287 	 * and inittext sections are discarded */
288 	if (!all_symbols) {
289 		if (symbol_in_range(s, text_ranges,
290 				    ARRAY_SIZE(text_ranges)) == 0)
291 			return 0;
292 		/* Corner case.  Discard any symbols with the same value as
293 		 * _etext _einittext; they can move between pass 1 and 2 when
294 		 * the kallsyms data are added.  If these symbols move then
295 		 * they may get dropped in pass 2, which breaks the kallsyms
296 		 * rules.
297 		 */
298 		if ((s->addr == text_range_text->end &&
299 		     strcmp(name, text_range_text->end_sym)) ||
300 		    (s->addr == text_range_inittext->end &&
301 		     strcmp(name, text_range_inittext->end_sym)))
302 			return 0;
303 	}
304 
305 	return 1;
306 }
307 
308 /* remove all the invalid symbols from the table */
309 static void shrink_table(void)
310 {
311 	unsigned int i, pos;
312 
313 	pos = 0;
314 	for (i = 0; i < table_cnt; i++) {
315 		if (symbol_valid(table[i])) {
316 			if (pos != i)
317 				table[pos] = table[i];
318 			pos++;
319 		} else {
320 			free(table[i]);
321 		}
322 	}
323 	table_cnt = pos;
324 
325 	/* When valid symbol is not registered, exit to error */
326 	if (!table_cnt) {
327 		fprintf(stderr, "No valid symbol.\n");
328 		exit(1);
329 	}
330 }
331 
332 static void read_map(FILE *in)
333 {
334 	struct sym_entry *sym;
335 
336 	while (!feof(in)) {
337 		sym = read_symbol(in);
338 		if (!sym)
339 			continue;
340 
341 		sym->start_pos = table_cnt;
342 
343 		if (table_cnt >= table_size) {
344 			table_size += 10000;
345 			table = realloc(table, sizeof(*table) * table_size);
346 			if (!table) {
347 				fprintf(stderr, "out of memory\n");
348 				exit (1);
349 			}
350 		}
351 
352 		table[table_cnt++] = sym;
353 	}
354 }
355 
356 static void output_label(const char *label)
357 {
358 	printf(".globl %s\n", label);
359 	printf("\tALGN\n");
360 	printf("%s:\n", label);
361 }
362 
363 /* Provide proper symbols relocatability by their '_text' relativeness. */
364 static void output_address(unsigned long long addr)
365 {
366 	if (_text <= addr)
367 		printf("\tPTR\t_text + %#llx\n", addr - _text);
368 	else
369 		printf("\tPTR\t_text - %#llx\n", _text - addr);
370 }
371 
372 /* uncompress a compressed symbol. When this function is called, the best table
373  * might still be compressed itself, so the function needs to be recursive */
374 static int expand_symbol(const unsigned char *data, int len, char *result)
375 {
376 	int c, rlen, total=0;
377 
378 	while (len) {
379 		c = *data;
380 		/* if the table holds a single char that is the same as the one
381 		 * we are looking for, then end the search */
382 		if (best_table[c][0]==c && best_table_len[c]==1) {
383 			*result++ = c;
384 			total++;
385 		} else {
386 			/* if not, recurse and expand */
387 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
388 			total += rlen;
389 			result += rlen;
390 		}
391 		data++;
392 		len--;
393 	}
394 	*result=0;
395 
396 	return total;
397 }
398 
399 static int symbol_absolute(const struct sym_entry *s)
400 {
401 	return s->percpu_absolute;
402 }
403 
404 static void write_src(void)
405 {
406 	unsigned int i, k, off;
407 	unsigned int best_idx[256];
408 	unsigned int *markers;
409 	char buf[KSYM_NAME_LEN];
410 
411 	printf("#include <asm/bitsperlong.h>\n");
412 	printf("#if BITS_PER_LONG == 64\n");
413 	printf("#define PTR .quad\n");
414 	printf("#define ALGN .balign 8\n");
415 	printf("#else\n");
416 	printf("#define PTR .long\n");
417 	printf("#define ALGN .balign 4\n");
418 	printf("#endif\n");
419 
420 	printf("\t.section .rodata, \"a\"\n");
421 
422 	if (!base_relative)
423 		output_label("kallsyms_addresses");
424 	else
425 		output_label("kallsyms_offsets");
426 
427 	for (i = 0; i < table_cnt; i++) {
428 		if (base_relative) {
429 			/*
430 			 * Use the offset relative to the lowest value
431 			 * encountered of all relative symbols, and emit
432 			 * non-relocatable fixed offsets that will be fixed
433 			 * up at runtime.
434 			 */
435 
436 			long long offset;
437 			int overflow;
438 
439 			if (!absolute_percpu) {
440 				offset = table[i]->addr - relative_base;
441 				overflow = (offset < 0 || offset > UINT_MAX);
442 			} else if (symbol_absolute(table[i])) {
443 				offset = table[i]->addr;
444 				overflow = (offset < 0 || offset > INT_MAX);
445 			} else {
446 				offset = relative_base - table[i]->addr - 1;
447 				overflow = (offset < INT_MIN || offset >= 0);
448 			}
449 			if (overflow) {
450 				fprintf(stderr, "kallsyms failure: "
451 					"%s symbol value %#llx out of range in relative mode\n",
452 					symbol_absolute(table[i]) ? "absolute" : "relative",
453 					table[i]->addr);
454 				exit(EXIT_FAILURE);
455 			}
456 			printf("\t.long\t%#x\n", (int)offset);
457 		} else if (!symbol_absolute(table[i])) {
458 			output_address(table[i]->addr);
459 		} else {
460 			printf("\tPTR\t%#llx\n", table[i]->addr);
461 		}
462 	}
463 	printf("\n");
464 
465 	if (base_relative) {
466 		output_label("kallsyms_relative_base");
467 		output_address(relative_base);
468 		printf("\n");
469 	}
470 
471 	output_label("kallsyms_num_syms");
472 	printf("\t.long\t%u\n", table_cnt);
473 	printf("\n");
474 
475 	/* table of offset markers, that give the offset in the compressed stream
476 	 * every 256 symbols */
477 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
478 	if (!markers) {
479 		fprintf(stderr, "kallsyms failure: "
480 			"unable to allocate required memory\n");
481 		exit(EXIT_FAILURE);
482 	}
483 
484 	output_label("kallsyms_names");
485 	off = 0;
486 	for (i = 0; i < table_cnt; i++) {
487 		if ((i & 0xFF) == 0)
488 			markers[i >> 8] = off;
489 
490 		printf("\t.byte 0x%02x", table[i]->len);
491 		for (k = 0; k < table[i]->len; k++)
492 			printf(", 0x%02x", table[i]->sym[k]);
493 		printf("\n");
494 
495 		off += table[i]->len + 1;
496 	}
497 	printf("\n");
498 
499 	output_label("kallsyms_markers");
500 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
501 		printf("\t.long\t%u\n", markers[i]);
502 	printf("\n");
503 
504 	free(markers);
505 
506 	output_label("kallsyms_token_table");
507 	off = 0;
508 	for (i = 0; i < 256; i++) {
509 		best_idx[i] = off;
510 		expand_symbol(best_table[i], best_table_len[i], buf);
511 		printf("\t.asciz\t\"%s\"\n", buf);
512 		off += strlen(buf) + 1;
513 	}
514 	printf("\n");
515 
516 	output_label("kallsyms_token_index");
517 	for (i = 0; i < 256; i++)
518 		printf("\t.short\t%d\n", best_idx[i]);
519 	printf("\n");
520 }
521 
522 
523 /* table lookup compression functions */
524 
525 /* count all the possible tokens in a symbol */
526 static void learn_symbol(const unsigned char *symbol, int len)
527 {
528 	int i;
529 
530 	for (i = 0; i < len - 1; i++)
531 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
532 }
533 
534 /* decrease the count for all the possible tokens in a symbol */
535 static void forget_symbol(const unsigned char *symbol, int len)
536 {
537 	int i;
538 
539 	for (i = 0; i < len - 1; i++)
540 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
541 }
542 
543 /* do the initial token count */
544 static void build_initial_tok_table(void)
545 {
546 	unsigned int i;
547 
548 	for (i = 0; i < table_cnt; i++)
549 		learn_symbol(table[i]->sym, table[i]->len);
550 }
551 
552 static unsigned char *find_token(unsigned char *str, int len,
553 				 const unsigned char *token)
554 {
555 	int i;
556 
557 	for (i = 0; i < len - 1; i++) {
558 		if (str[i] == token[0] && str[i+1] == token[1])
559 			return &str[i];
560 	}
561 	return NULL;
562 }
563 
564 /* replace a given token in all the valid symbols. Use the sampled symbols
565  * to update the counts */
566 static void compress_symbols(const unsigned char *str, int idx)
567 {
568 	unsigned int i, len, size;
569 	unsigned char *p1, *p2;
570 
571 	for (i = 0; i < table_cnt; i++) {
572 
573 		len = table[i]->len;
574 		p1 = table[i]->sym;
575 
576 		/* find the token on the symbol */
577 		p2 = find_token(p1, len, str);
578 		if (!p2) continue;
579 
580 		/* decrease the counts for this symbol's tokens */
581 		forget_symbol(table[i]->sym, len);
582 
583 		size = len;
584 
585 		do {
586 			*p2 = idx;
587 			p2++;
588 			size -= (p2 - p1);
589 			memmove(p2, p2 + 1, size);
590 			p1 = p2;
591 			len--;
592 
593 			if (size < 2) break;
594 
595 			/* find the token on the symbol */
596 			p2 = find_token(p1, size, str);
597 
598 		} while (p2);
599 
600 		table[i]->len = len;
601 
602 		/* increase the counts for this symbol's new tokens */
603 		learn_symbol(table[i]->sym, len);
604 	}
605 }
606 
607 /* search the token with the maximum profit */
608 static int find_best_token(void)
609 {
610 	int i, best, bestprofit;
611 
612 	bestprofit=-10000;
613 	best = 0;
614 
615 	for (i = 0; i < 0x10000; i++) {
616 		if (token_profit[i] > bestprofit) {
617 			best = i;
618 			bestprofit = token_profit[i];
619 		}
620 	}
621 	return best;
622 }
623 
624 /* this is the core of the algorithm: calculate the "best" table */
625 static void optimize_result(void)
626 {
627 	int i, best;
628 
629 	/* using the '\0' symbol last allows compress_symbols to use standard
630 	 * fast string functions */
631 	for (i = 255; i >= 0; i--) {
632 
633 		/* if this table slot is empty (it is not used by an actual
634 		 * original char code */
635 		if (!best_table_len[i]) {
636 
637 			/* find the token with the best profit value */
638 			best = find_best_token();
639 			if (token_profit[best] == 0)
640 				break;
641 
642 			/* place it in the "best" table */
643 			best_table_len[i] = 2;
644 			best_table[i][0] = best & 0xFF;
645 			best_table[i][1] = (best >> 8) & 0xFF;
646 
647 			/* replace this token in all the valid symbols */
648 			compress_symbols(best_table[i], i);
649 		}
650 	}
651 }
652 
653 /* start by placing the symbols that are actually used on the table */
654 static void insert_real_symbols_in_table(void)
655 {
656 	unsigned int i, j, c;
657 
658 	for (i = 0; i < table_cnt; i++) {
659 		for (j = 0; j < table[i]->len; j++) {
660 			c = table[i]->sym[j];
661 			best_table[c][0]=c;
662 			best_table_len[c]=1;
663 		}
664 	}
665 }
666 
667 static void optimize_token_table(void)
668 {
669 	build_initial_tok_table();
670 
671 	insert_real_symbols_in_table();
672 
673 	optimize_result();
674 }
675 
676 /* guess for "linker script provide" symbol */
677 static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
678 {
679 	const char *symbol = sym_name(se);
680 	int len = se->len - 1;
681 
682 	if (len < 8)
683 		return 0;
684 
685 	if (symbol[0] != '_' || symbol[1] != '_')
686 		return 0;
687 
688 	/* __start_XXXXX */
689 	if (!memcmp(symbol + 2, "start_", 6))
690 		return 1;
691 
692 	/* __stop_XXXXX */
693 	if (!memcmp(symbol + 2, "stop_", 5))
694 		return 1;
695 
696 	/* __end_XXXXX */
697 	if (!memcmp(symbol + 2, "end_", 4))
698 		return 1;
699 
700 	/* __XXXXX_start */
701 	if (!memcmp(symbol + len - 6, "_start", 6))
702 		return 1;
703 
704 	/* __XXXXX_end */
705 	if (!memcmp(symbol + len - 4, "_end", 4))
706 		return 1;
707 
708 	return 0;
709 }
710 
711 static int compare_symbols(const void *a, const void *b)
712 {
713 	const struct sym_entry *sa = *(const struct sym_entry **)a;
714 	const struct sym_entry *sb = *(const struct sym_entry **)b;
715 	int wa, wb;
716 
717 	/* sort by address first */
718 	if (sa->addr > sb->addr)
719 		return 1;
720 	if (sa->addr < sb->addr)
721 		return -1;
722 
723 	/* sort by "weakness" type */
724 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
725 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
726 	if (wa != wb)
727 		return wa - wb;
728 
729 	/* sort by "linker script provide" type */
730 	wa = may_be_linker_script_provide_symbol(sa);
731 	wb = may_be_linker_script_provide_symbol(sb);
732 	if (wa != wb)
733 		return wa - wb;
734 
735 	/* sort by the number of prefix underscores */
736 	wa = strspn(sym_name(sa), "_");
737 	wb = strspn(sym_name(sb), "_");
738 	if (wa != wb)
739 		return wa - wb;
740 
741 	/* sort by initial order, so that other symbols are left undisturbed */
742 	return sa->start_pos - sb->start_pos;
743 }
744 
745 static void sort_symbols(void)
746 {
747 	qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
748 }
749 
750 static void make_percpus_absolute(void)
751 {
752 	unsigned int i;
753 
754 	for (i = 0; i < table_cnt; i++)
755 		if (symbol_in_range(table[i], &percpu_range, 1)) {
756 			/*
757 			 * Keep the 'A' override for percpu symbols to
758 			 * ensure consistent behavior compared to older
759 			 * versions of this tool.
760 			 */
761 			table[i]->sym[0] = 'A';
762 			table[i]->percpu_absolute = 1;
763 		}
764 }
765 
766 /* find the minimum non-absolute symbol address */
767 static void record_relative_base(void)
768 {
769 	unsigned int i;
770 
771 	for (i = 0; i < table_cnt; i++)
772 		if (!symbol_absolute(table[i])) {
773 			/*
774 			 * The table is sorted by address.
775 			 * Take the first non-absolute symbol value.
776 			 */
777 			relative_base = table[i]->addr;
778 			return;
779 		}
780 }
781 
782 int main(int argc, char **argv)
783 {
784 	if (argc >= 2) {
785 		int i;
786 		for (i = 1; i < argc; i++) {
787 			if(strcmp(argv[i], "--all-symbols") == 0)
788 				all_symbols = 1;
789 			else if (strcmp(argv[i], "--absolute-percpu") == 0)
790 				absolute_percpu = 1;
791 			else if (strcmp(argv[i], "--base-relative") == 0)
792 				base_relative = 1;
793 			else
794 				usage();
795 		}
796 	} else if (argc != 1)
797 		usage();
798 
799 	read_map(stdin);
800 	shrink_table();
801 	if (absolute_percpu)
802 		make_percpus_absolute();
803 	sort_symbols();
804 	if (base_relative)
805 		record_relative_base();
806 	optimize_token_table();
807 	write_src();
808 
809 	return 0;
810 }
811