xref: /linux/scripts/kallsyms.c (revision b86406d42ae3c41ae0ce332ea24350829b88af51)
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 		"__kcfi_typeid_",	/* CFI type identifiers */
139 		NULL
140 	};
141 
142 	/* Symbol names that end with the following are ignored.*/
143 	static const char * const ignored_suffixes[] = {
144 		"_from_arm",		/* arm */
145 		"_from_thumb",		/* arm */
146 		"_veneer",		/* arm */
147 		NULL
148 	};
149 
150 	/* Symbol names that contain the following are ignored.*/
151 	static const char * const ignored_matches[] = {
152 		".long_branch.",	/* ppc stub */
153 		".plt_branch.",		/* ppc stub */
154 		NULL
155 	};
156 
157 	const char * const *p;
158 
159 	for (p = ignored_symbols; *p; p++)
160 		if (!strcmp(name, *p))
161 			return true;
162 
163 	for (p = ignored_prefixes; *p; p++)
164 		if (!strncmp(name, *p, strlen(*p)))
165 			return true;
166 
167 	for (p = ignored_suffixes; *p; p++) {
168 		int l = strlen(name) - strlen(*p);
169 
170 		if (l >= 0 && !strcmp(name + l, *p))
171 			return true;
172 	}
173 
174 	for (p = ignored_matches; *p; p++) {
175 		if (strstr(name, *p))
176 			return true;
177 	}
178 
179 	if (type == 'U' || type == 'u')
180 		return true;
181 	/* exclude debugging symbols */
182 	if (type == 'N' || type == 'n')
183 		return true;
184 
185 	if (toupper(type) == 'A') {
186 		/* Keep these useful absolute symbols */
187 		if (strcmp(name, "__kernel_syscall_via_break") &&
188 		    strcmp(name, "__kernel_syscall_via_epc") &&
189 		    strcmp(name, "__kernel_sigtramp") &&
190 		    strcmp(name, "__gp"))
191 			return true;
192 	}
193 
194 	return false;
195 }
196 
197 static void check_symbol_range(const char *sym, unsigned long long addr,
198 			       struct addr_range *ranges, int entries)
199 {
200 	size_t i;
201 	struct addr_range *ar;
202 
203 	for (i = 0; i < entries; ++i) {
204 		ar = &ranges[i];
205 
206 		if (strcmp(sym, ar->start_sym) == 0) {
207 			ar->start = addr;
208 			return;
209 		} else if (strcmp(sym, ar->end_sym) == 0) {
210 			ar->end = addr;
211 			return;
212 		}
213 	}
214 }
215 
216 static struct sym_entry *read_symbol(FILE *in)
217 {
218 	char name[KSYM_NAME_LEN_BUFFER+1], type;
219 	unsigned long long addr;
220 	unsigned int len;
221 	struct sym_entry *sym;
222 	int rc;
223 
224 	rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
225 	if (rc != 3) {
226 		if (rc != EOF && fgets(name, ARRAY_SIZE(name), in) == NULL)
227 			fprintf(stderr, "Read error or end of file.\n");
228 		return NULL;
229 	}
230 	if (strlen(name) >= KSYM_NAME_LEN) {
231 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
232 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
233 			name, strlen(name), KSYM_NAME_LEN);
234 		return NULL;
235 	}
236 
237 	if (strcmp(name, "_text") == 0)
238 		_text = addr;
239 
240 	/* Ignore most absolute/undefined (?) symbols. */
241 	if (is_ignored_symbol(name, type))
242 		return NULL;
243 
244 	check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
245 	check_symbol_range(name, addr, &percpu_range, 1);
246 
247 	/* include the type field in the symbol name, so that it gets
248 	 * compressed together */
249 
250 	len = strlen(name) + 1;
251 
252 	sym = malloc(sizeof(*sym) + len + 1);
253 	if (!sym) {
254 		fprintf(stderr, "kallsyms failure: "
255 			"unable to allocate required amount of memory\n");
256 		exit(EXIT_FAILURE);
257 	}
258 	sym->addr = addr;
259 	sym->len = len;
260 	sym->sym[0] = type;
261 	strcpy(sym_name(sym), name);
262 	sym->percpu_absolute = 0;
263 
264 	return sym;
265 }
266 
267 static int symbol_in_range(const struct sym_entry *s,
268 			   const struct addr_range *ranges, int entries)
269 {
270 	size_t i;
271 	const struct addr_range *ar;
272 
273 	for (i = 0; i < entries; ++i) {
274 		ar = &ranges[i];
275 
276 		if (s->addr >= ar->start && s->addr <= ar->end)
277 			return 1;
278 	}
279 
280 	return 0;
281 }
282 
283 static int symbol_valid(const struct sym_entry *s)
284 {
285 	const char *name = sym_name(s);
286 
287 	/* if --all-symbols is not specified, then symbols outside the text
288 	 * and inittext sections are discarded */
289 	if (!all_symbols) {
290 		if (symbol_in_range(s, text_ranges,
291 				    ARRAY_SIZE(text_ranges)) == 0)
292 			return 0;
293 		/* Corner case.  Discard any symbols with the same value as
294 		 * _etext _einittext; they can move between pass 1 and 2 when
295 		 * the kallsyms data are added.  If these symbols move then
296 		 * they may get dropped in pass 2, which breaks the kallsyms
297 		 * rules.
298 		 */
299 		if ((s->addr == text_range_text->end &&
300 		     strcmp(name, text_range_text->end_sym)) ||
301 		    (s->addr == text_range_inittext->end &&
302 		     strcmp(name, text_range_inittext->end_sym)))
303 			return 0;
304 	}
305 
306 	return 1;
307 }
308 
309 /* remove all the invalid symbols from the table */
310 static void shrink_table(void)
311 {
312 	unsigned int i, pos;
313 
314 	pos = 0;
315 	for (i = 0; i < table_cnt; i++) {
316 		if (symbol_valid(table[i])) {
317 			if (pos != i)
318 				table[pos] = table[i];
319 			pos++;
320 		} else {
321 			free(table[i]);
322 		}
323 	}
324 	table_cnt = pos;
325 
326 	/* When valid symbol is not registered, exit to error */
327 	if (!table_cnt) {
328 		fprintf(stderr, "No valid symbol.\n");
329 		exit(1);
330 	}
331 }
332 
333 static void read_map(FILE *in)
334 {
335 	struct sym_entry *sym;
336 
337 	while (!feof(in)) {
338 		sym = read_symbol(in);
339 		if (!sym)
340 			continue;
341 
342 		sym->start_pos = table_cnt;
343 
344 		if (table_cnt >= table_size) {
345 			table_size += 10000;
346 			table = realloc(table, sizeof(*table) * table_size);
347 			if (!table) {
348 				fprintf(stderr, "out of memory\n");
349 				exit (1);
350 			}
351 		}
352 
353 		table[table_cnt++] = sym;
354 	}
355 }
356 
357 static void output_label(const char *label)
358 {
359 	printf(".globl %s\n", label);
360 	printf("\tALGN\n");
361 	printf("%s:\n", label);
362 }
363 
364 /* Provide proper symbols relocatability by their '_text' relativeness. */
365 static void output_address(unsigned long long addr)
366 {
367 	if (_text <= addr)
368 		printf("\tPTR\t_text + %#llx\n", addr - _text);
369 	else
370 		printf("\tPTR\t_text - %#llx\n", _text - addr);
371 }
372 
373 /* uncompress a compressed symbol. When this function is called, the best table
374  * might still be compressed itself, so the function needs to be recursive */
375 static int expand_symbol(const unsigned char *data, int len, char *result)
376 {
377 	int c, rlen, total=0;
378 
379 	while (len) {
380 		c = *data;
381 		/* if the table holds a single char that is the same as the one
382 		 * we are looking for, then end the search */
383 		if (best_table[c][0]==c && best_table_len[c]==1) {
384 			*result++ = c;
385 			total++;
386 		} else {
387 			/* if not, recurse and expand */
388 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
389 			total += rlen;
390 			result += rlen;
391 		}
392 		data++;
393 		len--;
394 	}
395 	*result=0;
396 
397 	return total;
398 }
399 
400 static int symbol_absolute(const struct sym_entry *s)
401 {
402 	return s->percpu_absolute;
403 }
404 
405 static void write_src(void)
406 {
407 	unsigned int i, k, off;
408 	unsigned int best_idx[256];
409 	unsigned int *markers;
410 	char buf[KSYM_NAME_LEN];
411 
412 	printf("#include <asm/bitsperlong.h>\n");
413 	printf("#if BITS_PER_LONG == 64\n");
414 	printf("#define PTR .quad\n");
415 	printf("#define ALGN .balign 8\n");
416 	printf("#else\n");
417 	printf("#define PTR .long\n");
418 	printf("#define ALGN .balign 4\n");
419 	printf("#endif\n");
420 
421 	printf("\t.section .rodata, \"a\"\n");
422 
423 	if (!base_relative)
424 		output_label("kallsyms_addresses");
425 	else
426 		output_label("kallsyms_offsets");
427 
428 	for (i = 0; i < table_cnt; i++) {
429 		if (base_relative) {
430 			/*
431 			 * Use the offset relative to the lowest value
432 			 * encountered of all relative symbols, and emit
433 			 * non-relocatable fixed offsets that will be fixed
434 			 * up at runtime.
435 			 */
436 
437 			long long offset;
438 			int overflow;
439 
440 			if (!absolute_percpu) {
441 				offset = table[i]->addr - relative_base;
442 				overflow = (offset < 0 || offset > UINT_MAX);
443 			} else if (symbol_absolute(table[i])) {
444 				offset = table[i]->addr;
445 				overflow = (offset < 0 || offset > INT_MAX);
446 			} else {
447 				offset = relative_base - table[i]->addr - 1;
448 				overflow = (offset < INT_MIN || offset >= 0);
449 			}
450 			if (overflow) {
451 				fprintf(stderr, "kallsyms failure: "
452 					"%s symbol value %#llx out of range in relative mode\n",
453 					symbol_absolute(table[i]) ? "absolute" : "relative",
454 					table[i]->addr);
455 				exit(EXIT_FAILURE);
456 			}
457 			printf("\t.long\t%#x\n", (int)offset);
458 		} else if (!symbol_absolute(table[i])) {
459 			output_address(table[i]->addr);
460 		} else {
461 			printf("\tPTR\t%#llx\n", table[i]->addr);
462 		}
463 	}
464 	printf("\n");
465 
466 	if (base_relative) {
467 		output_label("kallsyms_relative_base");
468 		output_address(relative_base);
469 		printf("\n");
470 	}
471 
472 	output_label("kallsyms_num_syms");
473 	printf("\t.long\t%u\n", table_cnt);
474 	printf("\n");
475 
476 	/* table of offset markers, that give the offset in the compressed stream
477 	 * every 256 symbols */
478 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
479 	if (!markers) {
480 		fprintf(stderr, "kallsyms failure: "
481 			"unable to allocate required memory\n");
482 		exit(EXIT_FAILURE);
483 	}
484 
485 	output_label("kallsyms_names");
486 	off = 0;
487 	for (i = 0; i < table_cnt; i++) {
488 		if ((i & 0xFF) == 0)
489 			markers[i >> 8] = off;
490 
491 		/* There cannot be any symbol of length zero. */
492 		if (table[i]->len == 0) {
493 			fprintf(stderr, "kallsyms failure: "
494 				"unexpected zero symbol length\n");
495 			exit(EXIT_FAILURE);
496 		}
497 
498 		/* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
499 		if (table[i]->len > 0x3FFF) {
500 			fprintf(stderr, "kallsyms failure: "
501 				"unexpected huge symbol length\n");
502 			exit(EXIT_FAILURE);
503 		}
504 
505 		/* Encode length with ULEB128. */
506 		if (table[i]->len <= 0x7F) {
507 			/* Most symbols use a single byte for the length. */
508 			printf("\t.byte 0x%02x", table[i]->len);
509 			off += table[i]->len + 1;
510 		} else {
511 			/* "Big" symbols use two bytes. */
512 			printf("\t.byte 0x%02x, 0x%02x",
513 				(table[i]->len & 0x7F) | 0x80,
514 				(table[i]->len >> 7) & 0x7F);
515 			off += table[i]->len + 2;
516 		}
517 		for (k = 0; k < table[i]->len; k++)
518 			printf(", 0x%02x", table[i]->sym[k]);
519 		printf("\n");
520 	}
521 	printf("\n");
522 
523 	output_label("kallsyms_markers");
524 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
525 		printf("\t.long\t%u\n", markers[i]);
526 	printf("\n");
527 
528 	free(markers);
529 
530 	output_label("kallsyms_token_table");
531 	off = 0;
532 	for (i = 0; i < 256; i++) {
533 		best_idx[i] = off;
534 		expand_symbol(best_table[i], best_table_len[i], buf);
535 		printf("\t.asciz\t\"%s\"\n", buf);
536 		off += strlen(buf) + 1;
537 	}
538 	printf("\n");
539 
540 	output_label("kallsyms_token_index");
541 	for (i = 0; i < 256; i++)
542 		printf("\t.short\t%d\n", best_idx[i]);
543 	printf("\n");
544 }
545 
546 
547 /* table lookup compression functions */
548 
549 /* count all the possible tokens in a symbol */
550 static void learn_symbol(const unsigned char *symbol, int len)
551 {
552 	int i;
553 
554 	for (i = 0; i < len - 1; i++)
555 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
556 }
557 
558 /* decrease the count for all the possible tokens in a symbol */
559 static void forget_symbol(const unsigned char *symbol, int len)
560 {
561 	int i;
562 
563 	for (i = 0; i < len - 1; i++)
564 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
565 }
566 
567 /* do the initial token count */
568 static void build_initial_tok_table(void)
569 {
570 	unsigned int i;
571 
572 	for (i = 0; i < table_cnt; i++)
573 		learn_symbol(table[i]->sym, table[i]->len);
574 }
575 
576 static unsigned char *find_token(unsigned char *str, int len,
577 				 const unsigned char *token)
578 {
579 	int i;
580 
581 	for (i = 0; i < len - 1; i++) {
582 		if (str[i] == token[0] && str[i+1] == token[1])
583 			return &str[i];
584 	}
585 	return NULL;
586 }
587 
588 /* replace a given token in all the valid symbols. Use the sampled symbols
589  * to update the counts */
590 static void compress_symbols(const unsigned char *str, int idx)
591 {
592 	unsigned int i, len, size;
593 	unsigned char *p1, *p2;
594 
595 	for (i = 0; i < table_cnt; i++) {
596 
597 		len = table[i]->len;
598 		p1 = table[i]->sym;
599 
600 		/* find the token on the symbol */
601 		p2 = find_token(p1, len, str);
602 		if (!p2) continue;
603 
604 		/* decrease the counts for this symbol's tokens */
605 		forget_symbol(table[i]->sym, len);
606 
607 		size = len;
608 
609 		do {
610 			*p2 = idx;
611 			p2++;
612 			size -= (p2 - p1);
613 			memmove(p2, p2 + 1, size);
614 			p1 = p2;
615 			len--;
616 
617 			if (size < 2) break;
618 
619 			/* find the token on the symbol */
620 			p2 = find_token(p1, size, str);
621 
622 		} while (p2);
623 
624 		table[i]->len = len;
625 
626 		/* increase the counts for this symbol's new tokens */
627 		learn_symbol(table[i]->sym, len);
628 	}
629 }
630 
631 /* search the token with the maximum profit */
632 static int find_best_token(void)
633 {
634 	int i, best, bestprofit;
635 
636 	bestprofit=-10000;
637 	best = 0;
638 
639 	for (i = 0; i < 0x10000; i++) {
640 		if (token_profit[i] > bestprofit) {
641 			best = i;
642 			bestprofit = token_profit[i];
643 		}
644 	}
645 	return best;
646 }
647 
648 /* this is the core of the algorithm: calculate the "best" table */
649 static void optimize_result(void)
650 {
651 	int i, best;
652 
653 	/* using the '\0' symbol last allows compress_symbols to use standard
654 	 * fast string functions */
655 	for (i = 255; i >= 0; i--) {
656 
657 		/* if this table slot is empty (it is not used by an actual
658 		 * original char code */
659 		if (!best_table_len[i]) {
660 
661 			/* find the token with the best profit value */
662 			best = find_best_token();
663 			if (token_profit[best] == 0)
664 				break;
665 
666 			/* place it in the "best" table */
667 			best_table_len[i] = 2;
668 			best_table[i][0] = best & 0xFF;
669 			best_table[i][1] = (best >> 8) & 0xFF;
670 
671 			/* replace this token in all the valid symbols */
672 			compress_symbols(best_table[i], i);
673 		}
674 	}
675 }
676 
677 /* start by placing the symbols that are actually used on the table */
678 static void insert_real_symbols_in_table(void)
679 {
680 	unsigned int i, j, c;
681 
682 	for (i = 0; i < table_cnt; i++) {
683 		for (j = 0; j < table[i]->len; j++) {
684 			c = table[i]->sym[j];
685 			best_table[c][0]=c;
686 			best_table_len[c]=1;
687 		}
688 	}
689 }
690 
691 static void optimize_token_table(void)
692 {
693 	build_initial_tok_table();
694 
695 	insert_real_symbols_in_table();
696 
697 	optimize_result();
698 }
699 
700 /* guess for "linker script provide" symbol */
701 static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
702 {
703 	const char *symbol = sym_name(se);
704 	int len = se->len - 1;
705 
706 	if (len < 8)
707 		return 0;
708 
709 	if (symbol[0] != '_' || symbol[1] != '_')
710 		return 0;
711 
712 	/* __start_XXXXX */
713 	if (!memcmp(symbol + 2, "start_", 6))
714 		return 1;
715 
716 	/* __stop_XXXXX */
717 	if (!memcmp(symbol + 2, "stop_", 5))
718 		return 1;
719 
720 	/* __end_XXXXX */
721 	if (!memcmp(symbol + 2, "end_", 4))
722 		return 1;
723 
724 	/* __XXXXX_start */
725 	if (!memcmp(symbol + len - 6, "_start", 6))
726 		return 1;
727 
728 	/* __XXXXX_end */
729 	if (!memcmp(symbol + len - 4, "_end", 4))
730 		return 1;
731 
732 	return 0;
733 }
734 
735 static int compare_symbols(const void *a, const void *b)
736 {
737 	const struct sym_entry *sa = *(const struct sym_entry **)a;
738 	const struct sym_entry *sb = *(const struct sym_entry **)b;
739 	int wa, wb;
740 
741 	/* sort by address first */
742 	if (sa->addr > sb->addr)
743 		return 1;
744 	if (sa->addr < sb->addr)
745 		return -1;
746 
747 	/* sort by "weakness" type */
748 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
749 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
750 	if (wa != wb)
751 		return wa - wb;
752 
753 	/* sort by "linker script provide" type */
754 	wa = may_be_linker_script_provide_symbol(sa);
755 	wb = may_be_linker_script_provide_symbol(sb);
756 	if (wa != wb)
757 		return wa - wb;
758 
759 	/* sort by the number of prefix underscores */
760 	wa = strspn(sym_name(sa), "_");
761 	wb = strspn(sym_name(sb), "_");
762 	if (wa != wb)
763 		return wa - wb;
764 
765 	/* sort by initial order, so that other symbols are left undisturbed */
766 	return sa->start_pos - sb->start_pos;
767 }
768 
769 static void sort_symbols(void)
770 {
771 	qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
772 }
773 
774 static void make_percpus_absolute(void)
775 {
776 	unsigned int i;
777 
778 	for (i = 0; i < table_cnt; i++)
779 		if (symbol_in_range(table[i], &percpu_range, 1)) {
780 			/*
781 			 * Keep the 'A' override for percpu symbols to
782 			 * ensure consistent behavior compared to older
783 			 * versions of this tool.
784 			 */
785 			table[i]->sym[0] = 'A';
786 			table[i]->percpu_absolute = 1;
787 		}
788 }
789 
790 /* find the minimum non-absolute symbol address */
791 static void record_relative_base(void)
792 {
793 	unsigned int i;
794 
795 	for (i = 0; i < table_cnt; i++)
796 		if (!symbol_absolute(table[i])) {
797 			/*
798 			 * The table is sorted by address.
799 			 * Take the first non-absolute symbol value.
800 			 */
801 			relative_base = table[i]->addr;
802 			return;
803 		}
804 }
805 
806 int main(int argc, char **argv)
807 {
808 	if (argc >= 2) {
809 		int i;
810 		for (i = 1; i < argc; i++) {
811 			if(strcmp(argv[i], "--all-symbols") == 0)
812 				all_symbols = 1;
813 			else if (strcmp(argv[i], "--absolute-percpu") == 0)
814 				absolute_percpu = 1;
815 			else if (strcmp(argv[i], "--base-relative") == 0)
816 				base_relative = 1;
817 			else
818 				usage();
819 		}
820 	} else if (argc != 1)
821 		usage();
822 
823 	read_map(stdin);
824 	shrink_table();
825 	if (absolute_percpu)
826 		make_percpus_absolute();
827 	sort_symbols();
828 	if (base_relative)
829 		record_relative_base();
830 	optimize_token_table();
831 	write_src();
832 
833 	return 0;
834 }
835