xref: /openbmc/linux/scripts/kallsyms.c (revision 12109610)
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: kallsyms [--all-symbols] [--absolute-percpu]
9  *                         [--base-relative] [--lto-clang] in.map > out.S
10  *
11  *      Table compression uses all the unused char codes on the symbols and
12  *  maps these to the most used substrings (tokens). For instance, it might
13  *  map char code 0xF7 to represent "write_" and then in every symbol where
14  *  "write_" appears it can be replaced by 0xF7, saving 5 bytes.
15  *      The used codes themselves are also placed in the table so that the
16  *  decompresion can work without "special cases".
17  *      Applied to kernel symbols, this usually produces a compression ratio
18  *  of about 50%.
19  *
20  */
21 
22 #include <getopt.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <limits.h>
29 
30 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
31 
32 #define _stringify_1(x)	#x
33 #define _stringify(x)	_stringify_1(x)
34 
35 #define KSYM_NAME_LEN		512
36 
37 /*
38  * A substantially bigger size than the current maximum.
39  *
40  * It cannot be defined as an expression because it gets stringified
41  * for the fscanf() format string. Therefore, a _Static_assert() is
42  * used instead to maintain the relationship with KSYM_NAME_LEN.
43  */
44 #define KSYM_NAME_LEN_BUFFER	2048
45 _Static_assert(
46 	KSYM_NAME_LEN_BUFFER == KSYM_NAME_LEN * 4,
47 	"Please keep KSYM_NAME_LEN_BUFFER in sync with KSYM_NAME_LEN"
48 );
49 
50 struct sym_entry {
51 	unsigned long long addr;
52 	unsigned int len;
53 	unsigned int seq;
54 	unsigned int start_pos;
55 	unsigned int percpu_absolute;
56 	unsigned char sym[];
57 };
58 
59 struct addr_range {
60 	const char *start_sym, *end_sym;
61 	unsigned long long start, end;
62 };
63 
64 static unsigned long long _text;
65 static unsigned long long relative_base;
66 static struct addr_range text_ranges[] = {
67 	{ "_stext",     "_etext"     },
68 	{ "_sinittext", "_einittext" },
69 };
70 #define text_range_text     (&text_ranges[0])
71 #define text_range_inittext (&text_ranges[1])
72 
73 static struct addr_range percpu_range = {
74 	"__per_cpu_start", "__per_cpu_end", -1ULL, 0
75 };
76 
77 static struct sym_entry **table;
78 static unsigned int table_size, table_cnt;
79 static int all_symbols;
80 static int absolute_percpu;
81 static int base_relative;
82 static int lto_clang;
83 
84 static int token_profit[0x10000];
85 
86 /* the table that holds the result of the compression */
87 static unsigned char best_table[256][2];
88 static unsigned char best_table_len[256];
89 
90 
91 static void usage(void)
92 {
93 	fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
94 			"[--base-relative] [--lto-clang] in.map > out.S\n");
95 	exit(1);
96 }
97 
98 static char *sym_name(const struct sym_entry *s)
99 {
100 	return (char *)s->sym + 1;
101 }
102 
103 static bool is_ignored_symbol(const char *name, char type)
104 {
105 	if (type == 'u' || type == 'n')
106 		return true;
107 
108 	if (toupper(type) == 'A') {
109 		/* Keep these useful absolute symbols */
110 		if (strcmp(name, "__kernel_syscall_via_break") &&
111 		    strcmp(name, "__kernel_syscall_via_epc") &&
112 		    strcmp(name, "__kernel_sigtramp") &&
113 		    strcmp(name, "__gp"))
114 			return true;
115 	}
116 
117 	return false;
118 }
119 
120 static void check_symbol_range(const char *sym, unsigned long long addr,
121 			       struct addr_range *ranges, int entries)
122 {
123 	size_t i;
124 	struct addr_range *ar;
125 
126 	for (i = 0; i < entries; ++i) {
127 		ar = &ranges[i];
128 
129 		if (strcmp(sym, ar->start_sym) == 0) {
130 			ar->start = addr;
131 			return;
132 		} else if (strcmp(sym, ar->end_sym) == 0) {
133 			ar->end = addr;
134 			return;
135 		}
136 	}
137 }
138 
139 static struct sym_entry *read_symbol(FILE *in)
140 {
141 	char name[KSYM_NAME_LEN_BUFFER+1], type;
142 	unsigned long long addr;
143 	unsigned int len;
144 	struct sym_entry *sym;
145 	int rc;
146 
147 	rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
148 	if (rc != 3) {
149 		if (rc != EOF && fgets(name, ARRAY_SIZE(name), in) == NULL)
150 			fprintf(stderr, "Read error or end of file.\n");
151 		return NULL;
152 	}
153 	if (strlen(name) >= KSYM_NAME_LEN) {
154 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
155 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
156 			name, strlen(name), KSYM_NAME_LEN);
157 		return NULL;
158 	}
159 
160 	if (strcmp(name, "_text") == 0)
161 		_text = addr;
162 
163 	/* Ignore most absolute/undefined (?) symbols. */
164 	if (is_ignored_symbol(name, type))
165 		return NULL;
166 
167 	check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
168 	check_symbol_range(name, addr, &percpu_range, 1);
169 
170 	/* include the type field in the symbol name, so that it gets
171 	 * compressed together */
172 
173 	len = strlen(name) + 1;
174 
175 	sym = malloc(sizeof(*sym) + len + 1);
176 	if (!sym) {
177 		fprintf(stderr, "kallsyms failure: "
178 			"unable to allocate required amount of memory\n");
179 		exit(EXIT_FAILURE);
180 	}
181 	sym->addr = addr;
182 	sym->len = len;
183 	sym->sym[0] = type;
184 	strcpy(sym_name(sym), name);
185 	sym->percpu_absolute = 0;
186 
187 	return sym;
188 }
189 
190 static int symbol_in_range(const struct sym_entry *s,
191 			   const struct addr_range *ranges, int entries)
192 {
193 	size_t i;
194 	const struct addr_range *ar;
195 
196 	for (i = 0; i < entries; ++i) {
197 		ar = &ranges[i];
198 
199 		if (s->addr >= ar->start && s->addr <= ar->end)
200 			return 1;
201 	}
202 
203 	return 0;
204 }
205 
206 static int symbol_valid(const struct sym_entry *s)
207 {
208 	const char *name = sym_name(s);
209 
210 	/* if --all-symbols is not specified, then symbols outside the text
211 	 * and inittext sections are discarded */
212 	if (!all_symbols) {
213 		if (symbol_in_range(s, text_ranges,
214 				    ARRAY_SIZE(text_ranges)) == 0)
215 			return 0;
216 		/* Corner case.  Discard any symbols with the same value as
217 		 * _etext _einittext; they can move between pass 1 and 2 when
218 		 * the kallsyms data are added.  If these symbols move then
219 		 * they may get dropped in pass 2, which breaks the kallsyms
220 		 * rules.
221 		 */
222 		if ((s->addr == text_range_text->end &&
223 		     strcmp(name, text_range_text->end_sym)) ||
224 		    (s->addr == text_range_inittext->end &&
225 		     strcmp(name, text_range_inittext->end_sym)))
226 			return 0;
227 	}
228 
229 	return 1;
230 }
231 
232 /* remove all the invalid symbols from the table */
233 static void shrink_table(void)
234 {
235 	unsigned int i, pos;
236 
237 	pos = 0;
238 	for (i = 0; i < table_cnt; i++) {
239 		if (symbol_valid(table[i])) {
240 			if (pos != i)
241 				table[pos] = table[i];
242 			pos++;
243 		} else {
244 			free(table[i]);
245 		}
246 	}
247 	table_cnt = pos;
248 
249 	/* When valid symbol is not registered, exit to error */
250 	if (!table_cnt) {
251 		fprintf(stderr, "No valid symbol.\n");
252 		exit(1);
253 	}
254 }
255 
256 static void read_map(const char *in)
257 {
258 	FILE *fp;
259 	struct sym_entry *sym;
260 
261 	fp = fopen(in, "r");
262 	if (!fp) {
263 		perror(in);
264 		exit(1);
265 	}
266 
267 	while (!feof(fp)) {
268 		sym = read_symbol(fp);
269 		if (!sym)
270 			continue;
271 
272 		sym->start_pos = table_cnt;
273 
274 		if (table_cnt >= table_size) {
275 			table_size += 10000;
276 			table = realloc(table, sizeof(*table) * table_size);
277 			if (!table) {
278 				fprintf(stderr, "out of memory\n");
279 				fclose(fp);
280 				exit (1);
281 			}
282 		}
283 
284 		table[table_cnt++] = sym;
285 	}
286 
287 	fclose(fp);
288 }
289 
290 static void output_label(const char *label)
291 {
292 	printf(".globl %s\n", label);
293 	printf("\tALGN\n");
294 	printf("%s:\n", label);
295 }
296 
297 /* Provide proper symbols relocatability by their '_text' relativeness. */
298 static void output_address(unsigned long long addr)
299 {
300 	if (_text <= addr)
301 		printf("\tPTR\t_text + %#llx\n", addr - _text);
302 	else
303 		printf("\tPTR\t_text - %#llx\n", _text - addr);
304 }
305 
306 /* uncompress a compressed symbol. When this function is called, the best table
307  * might still be compressed itself, so the function needs to be recursive */
308 static int expand_symbol(const unsigned char *data, int len, char *result)
309 {
310 	int c, rlen, total=0;
311 
312 	while (len) {
313 		c = *data;
314 		/* if the table holds a single char that is the same as the one
315 		 * we are looking for, then end the search */
316 		if (best_table[c][0]==c && best_table_len[c]==1) {
317 			*result++ = c;
318 			total++;
319 		} else {
320 			/* if not, recurse and expand */
321 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
322 			total += rlen;
323 			result += rlen;
324 		}
325 		data++;
326 		len--;
327 	}
328 	*result=0;
329 
330 	return total;
331 }
332 
333 static int symbol_absolute(const struct sym_entry *s)
334 {
335 	return s->percpu_absolute;
336 }
337 
338 static void cleanup_symbol_name(char *s)
339 {
340 	char *p;
341 
342 	/*
343 	 * ASCII[.]   = 2e
344 	 * ASCII[0-9] = 30,39
345 	 * ASCII[A-Z] = 41,5a
346 	 * ASCII[_]   = 5f
347 	 * ASCII[a-z] = 61,7a
348 	 *
349 	 * As above, replacing '.' with '\0' does not affect the main sorting,
350 	 * but it helps us with subsorting.
351 	 */
352 	p = strchr(s, '.');
353 	if (p)
354 		*p = '\0';
355 }
356 
357 static int compare_names(const void *a, const void *b)
358 {
359 	int ret;
360 	const struct sym_entry *sa = *(const struct sym_entry **)a;
361 	const struct sym_entry *sb = *(const struct sym_entry **)b;
362 
363 	ret = strcmp(sym_name(sa), sym_name(sb));
364 	if (!ret) {
365 		if (sa->addr > sb->addr)
366 			return 1;
367 		else if (sa->addr < sb->addr)
368 			return -1;
369 
370 		/* keep old order */
371 		return (int)(sa->seq - sb->seq);
372 	}
373 
374 	return ret;
375 }
376 
377 static void sort_symbols_by_name(void)
378 {
379 	qsort(table, table_cnt, sizeof(table[0]), compare_names);
380 }
381 
382 static void write_src(void)
383 {
384 	unsigned int i, k, off;
385 	unsigned int best_idx[256];
386 	unsigned int *markers;
387 	char buf[KSYM_NAME_LEN];
388 
389 	printf("#include <asm/bitsperlong.h>\n");
390 	printf("#if BITS_PER_LONG == 64\n");
391 	printf("#define PTR .quad\n");
392 	printf("#define ALGN .balign 8\n");
393 	printf("#else\n");
394 	printf("#define PTR .long\n");
395 	printf("#define ALGN .balign 4\n");
396 	printf("#endif\n");
397 
398 	printf("\t.section .rodata, \"a\"\n");
399 
400 	output_label("kallsyms_num_syms");
401 	printf("\t.long\t%u\n", table_cnt);
402 	printf("\n");
403 
404 	/* table of offset markers, that give the offset in the compressed stream
405 	 * every 256 symbols */
406 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
407 	if (!markers) {
408 		fprintf(stderr, "kallsyms failure: "
409 			"unable to allocate required memory\n");
410 		exit(EXIT_FAILURE);
411 	}
412 
413 	output_label("kallsyms_names");
414 	off = 0;
415 	for (i = 0; i < table_cnt; i++) {
416 		if ((i & 0xFF) == 0)
417 			markers[i >> 8] = off;
418 		table[i]->seq = i;
419 
420 		/* There cannot be any symbol of length zero. */
421 		if (table[i]->len == 0) {
422 			fprintf(stderr, "kallsyms failure: "
423 				"unexpected zero symbol length\n");
424 			exit(EXIT_FAILURE);
425 		}
426 
427 		/* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
428 		if (table[i]->len > 0x3FFF) {
429 			fprintf(stderr, "kallsyms failure: "
430 				"unexpected huge symbol length\n");
431 			exit(EXIT_FAILURE);
432 		}
433 
434 		/* Encode length with ULEB128. */
435 		if (table[i]->len <= 0x7F) {
436 			/* Most symbols use a single byte for the length. */
437 			printf("\t.byte 0x%02x", table[i]->len);
438 			off += table[i]->len + 1;
439 		} else {
440 			/* "Big" symbols use two bytes. */
441 			printf("\t.byte 0x%02x, 0x%02x",
442 				(table[i]->len & 0x7F) | 0x80,
443 				(table[i]->len >> 7) & 0x7F);
444 			off += table[i]->len + 2;
445 		}
446 		for (k = 0; k < table[i]->len; k++)
447 			printf(", 0x%02x", table[i]->sym[k]);
448 		printf("\n");
449 	}
450 	printf("\n");
451 
452 	/*
453 	 * Now that we wrote out the compressed symbol names, restore the
454 	 * original names, which are needed in some of the later steps.
455 	 */
456 	for (i = 0; i < table_cnt; i++) {
457 		expand_symbol(table[i]->sym, table[i]->len, buf);
458 		strcpy((char *)table[i]->sym, buf);
459 	}
460 
461 	output_label("kallsyms_markers");
462 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
463 		printf("\t.long\t%u\n", markers[i]);
464 	printf("\n");
465 
466 	free(markers);
467 
468 	output_label("kallsyms_token_table");
469 	off = 0;
470 	for (i = 0; i < 256; i++) {
471 		best_idx[i] = off;
472 		expand_symbol(best_table[i], best_table_len[i], buf);
473 		printf("\t.asciz\t\"%s\"\n", buf);
474 		off += strlen(buf) + 1;
475 	}
476 	printf("\n");
477 
478 	output_label("kallsyms_token_index");
479 	for (i = 0; i < 256; i++)
480 		printf("\t.short\t%d\n", best_idx[i]);
481 	printf("\n");
482 
483 	if (!base_relative)
484 		output_label("kallsyms_addresses");
485 	else
486 		output_label("kallsyms_offsets");
487 
488 	for (i = 0; i < table_cnt; i++) {
489 		if (base_relative) {
490 			/*
491 			 * Use the offset relative to the lowest value
492 			 * encountered of all relative symbols, and emit
493 			 * non-relocatable fixed offsets that will be fixed
494 			 * up at runtime.
495 			 */
496 
497 			long long offset;
498 			int overflow;
499 
500 			if (!absolute_percpu) {
501 				offset = table[i]->addr - relative_base;
502 				overflow = (offset < 0 || offset > UINT_MAX);
503 			} else if (symbol_absolute(table[i])) {
504 				offset = table[i]->addr;
505 				overflow = (offset < 0 || offset > INT_MAX);
506 			} else {
507 				offset = relative_base - table[i]->addr - 1;
508 				overflow = (offset < INT_MIN || offset >= 0);
509 			}
510 			if (overflow) {
511 				fprintf(stderr, "kallsyms failure: "
512 					"%s symbol value %#llx out of range in relative mode\n",
513 					symbol_absolute(table[i]) ? "absolute" : "relative",
514 					table[i]->addr);
515 				exit(EXIT_FAILURE);
516 			}
517 			printf("\t.long\t%#x	/* %s */\n", (int)offset, table[i]->sym);
518 		} else if (!symbol_absolute(table[i])) {
519 			output_address(table[i]->addr);
520 		} else {
521 			printf("\tPTR\t%#llx\n", table[i]->addr);
522 		}
523 	}
524 	printf("\n");
525 
526 	if (base_relative) {
527 		output_label("kallsyms_relative_base");
528 		output_address(relative_base);
529 		printf("\n");
530 	}
531 
532 	if (lto_clang)
533 		for (i = 0; i < table_cnt; i++)
534 			cleanup_symbol_name((char *)table[i]->sym);
535 
536 	sort_symbols_by_name();
537 	output_label("kallsyms_seqs_of_names");
538 	for (i = 0; i < table_cnt; i++)
539 		printf("\t.byte 0x%02x, 0x%02x, 0x%02x\n",
540 			(unsigned char)(table[i]->seq >> 16),
541 			(unsigned char)(table[i]->seq >> 8),
542 			(unsigned char)(table[i]->seq >> 0));
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_token_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_token_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 	while (1) {
809 		static struct option long_options[] = {
810 			{"all-symbols",     no_argument, &all_symbols,     1},
811 			{"absolute-percpu", no_argument, &absolute_percpu, 1},
812 			{"base-relative",   no_argument, &base_relative,   1},
813 			{"lto-clang",       no_argument, &lto_clang,       1},
814 			{},
815 		};
816 
817 		int c = getopt_long(argc, argv, "", long_options, NULL);
818 
819 		if (c == -1)
820 			break;
821 		if (c != 0)
822 			usage();
823 	}
824 
825 	if (optind >= argc)
826 		usage();
827 
828 	read_map(argv[optind]);
829 	shrink_table();
830 	if (absolute_percpu)
831 		make_percpus_absolute();
832 	sort_symbols();
833 	if (base_relative)
834 		record_relative_base();
835 	optimize_token_table();
836 	write_src();
837 
838 	return 0;
839 }
840