xref: /openbmc/linux/scripts/kallsyms.c (revision b8a94bfb)
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		512
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	2048
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 		/* There cannot be any symbol of length zero. */
491 		if (table[i]->len == 0) {
492 			fprintf(stderr, "kallsyms failure: "
493 				"unexpected zero symbol length\n");
494 			exit(EXIT_FAILURE);
495 		}
496 
497 		/* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
498 		if (table[i]->len > 0x3FFF) {
499 			fprintf(stderr, "kallsyms failure: "
500 				"unexpected huge symbol length\n");
501 			exit(EXIT_FAILURE);
502 		}
503 
504 		/* Encode length with ULEB128. */
505 		if (table[i]->len <= 0x7F) {
506 			/* Most symbols use a single byte for the length. */
507 			printf("\t.byte 0x%02x", table[i]->len);
508 			off += table[i]->len + 1;
509 		} else {
510 			/* "Big" symbols use two bytes. */
511 			printf("\t.byte 0x%02x, 0x%02x",
512 				(table[i]->len & 0x7F) | 0x80,
513 				(table[i]->len >> 7) & 0x7F);
514 			off += table[i]->len + 2;
515 		}
516 		for (k = 0; k < table[i]->len; k++)
517 			printf(", 0x%02x", table[i]->sym[k]);
518 		printf("\n");
519 	}
520 	printf("\n");
521 
522 	output_label("kallsyms_markers");
523 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
524 		printf("\t.long\t%u\n", markers[i]);
525 	printf("\n");
526 
527 	free(markers);
528 
529 	output_label("kallsyms_token_table");
530 	off = 0;
531 	for (i = 0; i < 256; i++) {
532 		best_idx[i] = off;
533 		expand_symbol(best_table[i], best_table_len[i], buf);
534 		printf("\t.asciz\t\"%s\"\n", buf);
535 		off += strlen(buf) + 1;
536 	}
537 	printf("\n");
538 
539 	output_label("kallsyms_token_index");
540 	for (i = 0; i < 256; i++)
541 		printf("\t.short\t%d\n", best_idx[i]);
542 	printf("\n");
543 }
544 
545 
546 /* table lookup compression functions */
547 
548 /* count all the possible tokens in a symbol */
549 static void learn_symbol(const unsigned char *symbol, int len)
550 {
551 	int i;
552 
553 	for (i = 0; i < len - 1; i++)
554 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
555 }
556 
557 /* decrease the count for all the possible tokens in a symbol */
558 static void forget_symbol(const unsigned char *symbol, int len)
559 {
560 	int i;
561 
562 	for (i = 0; i < len - 1; i++)
563 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
564 }
565 
566 /* do the initial token count */
567 static void build_initial_tok_table(void)
568 {
569 	unsigned int i;
570 
571 	for (i = 0; i < table_cnt; i++)
572 		learn_symbol(table[i]->sym, table[i]->len);
573 }
574 
575 static unsigned char *find_token(unsigned char *str, int len,
576 				 const unsigned char *token)
577 {
578 	int i;
579 
580 	for (i = 0; i < len - 1; i++) {
581 		if (str[i] == token[0] && str[i+1] == token[1])
582 			return &str[i];
583 	}
584 	return NULL;
585 }
586 
587 /* replace a given token in all the valid symbols. Use the sampled symbols
588  * to update the counts */
589 static void compress_symbols(const unsigned char *str, int idx)
590 {
591 	unsigned int i, len, size;
592 	unsigned char *p1, *p2;
593 
594 	for (i = 0; i < table_cnt; i++) {
595 
596 		len = table[i]->len;
597 		p1 = table[i]->sym;
598 
599 		/* find the token on the symbol */
600 		p2 = find_token(p1, len, str);
601 		if (!p2) continue;
602 
603 		/* decrease the counts for this symbol's tokens */
604 		forget_symbol(table[i]->sym, len);
605 
606 		size = len;
607 
608 		do {
609 			*p2 = idx;
610 			p2++;
611 			size -= (p2 - p1);
612 			memmove(p2, p2 + 1, size);
613 			p1 = p2;
614 			len--;
615 
616 			if (size < 2) break;
617 
618 			/* find the token on the symbol */
619 			p2 = find_token(p1, size, str);
620 
621 		} while (p2);
622 
623 		table[i]->len = len;
624 
625 		/* increase the counts for this symbol's new tokens */
626 		learn_symbol(table[i]->sym, len);
627 	}
628 }
629 
630 /* search the token with the maximum profit */
631 static int find_best_token(void)
632 {
633 	int i, best, bestprofit;
634 
635 	bestprofit=-10000;
636 	best = 0;
637 
638 	for (i = 0; i < 0x10000; i++) {
639 		if (token_profit[i] > bestprofit) {
640 			best = i;
641 			bestprofit = token_profit[i];
642 		}
643 	}
644 	return best;
645 }
646 
647 /* this is the core of the algorithm: calculate the "best" table */
648 static void optimize_result(void)
649 {
650 	int i, best;
651 
652 	/* using the '\0' symbol last allows compress_symbols to use standard
653 	 * fast string functions */
654 	for (i = 255; i >= 0; i--) {
655 
656 		/* if this table slot is empty (it is not used by an actual
657 		 * original char code */
658 		if (!best_table_len[i]) {
659 
660 			/* find the token with the best profit value */
661 			best = find_best_token();
662 			if (token_profit[best] == 0)
663 				break;
664 
665 			/* place it in the "best" table */
666 			best_table_len[i] = 2;
667 			best_table[i][0] = best & 0xFF;
668 			best_table[i][1] = (best >> 8) & 0xFF;
669 
670 			/* replace this token in all the valid symbols */
671 			compress_symbols(best_table[i], i);
672 		}
673 	}
674 }
675 
676 /* start by placing the symbols that are actually used on the table */
677 static void insert_real_symbols_in_table(void)
678 {
679 	unsigned int i, j, c;
680 
681 	for (i = 0; i < table_cnt; i++) {
682 		for (j = 0; j < table[i]->len; j++) {
683 			c = table[i]->sym[j];
684 			best_table[c][0]=c;
685 			best_table_len[c]=1;
686 		}
687 	}
688 }
689 
690 static void optimize_token_table(void)
691 {
692 	build_initial_tok_table();
693 
694 	insert_real_symbols_in_table();
695 
696 	optimize_result();
697 }
698 
699 /* guess for "linker script provide" symbol */
700 static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
701 {
702 	const char *symbol = sym_name(se);
703 	int len = se->len - 1;
704 
705 	if (len < 8)
706 		return 0;
707 
708 	if (symbol[0] != '_' || symbol[1] != '_')
709 		return 0;
710 
711 	/* __start_XXXXX */
712 	if (!memcmp(symbol + 2, "start_", 6))
713 		return 1;
714 
715 	/* __stop_XXXXX */
716 	if (!memcmp(symbol + 2, "stop_", 5))
717 		return 1;
718 
719 	/* __end_XXXXX */
720 	if (!memcmp(symbol + 2, "end_", 4))
721 		return 1;
722 
723 	/* __XXXXX_start */
724 	if (!memcmp(symbol + len - 6, "_start", 6))
725 		return 1;
726 
727 	/* __XXXXX_end */
728 	if (!memcmp(symbol + len - 4, "_end", 4))
729 		return 1;
730 
731 	return 0;
732 }
733 
734 static int compare_symbols(const void *a, const void *b)
735 {
736 	const struct sym_entry *sa = *(const struct sym_entry **)a;
737 	const struct sym_entry *sb = *(const struct sym_entry **)b;
738 	int wa, wb;
739 
740 	/* sort by address first */
741 	if (sa->addr > sb->addr)
742 		return 1;
743 	if (sa->addr < sb->addr)
744 		return -1;
745 
746 	/* sort by "weakness" type */
747 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
748 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
749 	if (wa != wb)
750 		return wa - wb;
751 
752 	/* sort by "linker script provide" type */
753 	wa = may_be_linker_script_provide_symbol(sa);
754 	wb = may_be_linker_script_provide_symbol(sb);
755 	if (wa != wb)
756 		return wa - wb;
757 
758 	/* sort by the number of prefix underscores */
759 	wa = strspn(sym_name(sa), "_");
760 	wb = strspn(sym_name(sb), "_");
761 	if (wa != wb)
762 		return wa - wb;
763 
764 	/* sort by initial order, so that other symbols are left undisturbed */
765 	return sa->start_pos - sb->start_pos;
766 }
767 
768 static void sort_symbols(void)
769 {
770 	qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
771 }
772 
773 static void make_percpus_absolute(void)
774 {
775 	unsigned int i;
776 
777 	for (i = 0; i < table_cnt; i++)
778 		if (symbol_in_range(table[i], &percpu_range, 1)) {
779 			/*
780 			 * Keep the 'A' override for percpu symbols to
781 			 * ensure consistent behavior compared to older
782 			 * versions of this tool.
783 			 */
784 			table[i]->sym[0] = 'A';
785 			table[i]->percpu_absolute = 1;
786 		}
787 }
788 
789 /* find the minimum non-absolute symbol address */
790 static void record_relative_base(void)
791 {
792 	unsigned int i;
793 
794 	for (i = 0; i < table_cnt; i++)
795 		if (!symbol_absolute(table[i])) {
796 			/*
797 			 * The table is sorted by address.
798 			 * Take the first non-absolute symbol value.
799 			 */
800 			relative_base = table[i]->addr;
801 			return;
802 		}
803 }
804 
805 int main(int argc, char **argv)
806 {
807 	if (argc >= 2) {
808 		int i;
809 		for (i = 1; i < argc; i++) {
810 			if(strcmp(argv[i], "--all-symbols") == 0)
811 				all_symbols = 1;
812 			else if (strcmp(argv[i], "--absolute-percpu") == 0)
813 				absolute_percpu = 1;
814 			else if (strcmp(argv[i], "--base-relative") == 0)
815 				base_relative = 1;
816 			else
817 				usage();
818 		}
819 	} else if (argc != 1)
820 		usage();
821 
822 	read_map(stdin);
823 	shrink_table();
824 	if (absolute_percpu)
825 		make_percpus_absolute();
826 	sort_symbols();
827 	if (base_relative)
828 		record_relative_base();
829 	optimize_token_table();
830 	write_src();
831 
832 	return 0;
833 }
834