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