xref: /openbmc/linux/scripts/kallsyms.c (revision 68f436a8)
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 <errno.h>
23 #include <getopt.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <limits.h>
30 
31 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
32 
33 #define KSYM_NAME_LEN		512
34 
35 struct sym_entry {
36 	unsigned long long addr;
37 	unsigned int len;
38 	unsigned int seq;
39 	unsigned int start_pos;
40 	unsigned int percpu_absolute;
41 	unsigned char sym[];
42 };
43 
44 struct addr_range {
45 	const char *start_sym, *end_sym;
46 	unsigned long long start, end;
47 };
48 
49 static unsigned long long _text;
50 static unsigned long long relative_base;
51 static struct addr_range text_ranges[] = {
52 	{ "_stext",     "_etext"     },
53 	{ "_sinittext", "_einittext" },
54 };
55 #define text_range_text     (&text_ranges[0])
56 #define text_range_inittext (&text_ranges[1])
57 
58 static struct addr_range percpu_range = {
59 	"__per_cpu_start", "__per_cpu_end", -1ULL, 0
60 };
61 
62 static struct sym_entry **table;
63 static unsigned int table_size, table_cnt;
64 static int all_symbols;
65 static int absolute_percpu;
66 static int base_relative;
67 static int lto_clang;
68 
69 static int token_profit[0x10000];
70 
71 /* the table that holds the result of the compression */
72 static unsigned char best_table[256][2];
73 static unsigned char best_table_len[256];
74 
75 
76 static void usage(void)
77 {
78 	fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
79 			"[--base-relative] [--lto-clang] in.map > out.S\n");
80 	exit(1);
81 }
82 
83 static char *sym_name(const struct sym_entry *s)
84 {
85 	return (char *)s->sym + 1;
86 }
87 
88 static bool is_ignored_symbol(const char *name, char type)
89 {
90 	if (type == 'u' || type == 'n')
91 		return true;
92 
93 	if (toupper(type) == 'A') {
94 		/* Keep these useful absolute symbols */
95 		if (strcmp(name, "__kernel_syscall_via_break") &&
96 		    strcmp(name, "__kernel_syscall_via_epc") &&
97 		    strcmp(name, "__kernel_sigtramp") &&
98 		    strcmp(name, "__gp"))
99 			return true;
100 	}
101 
102 	return false;
103 }
104 
105 static void check_symbol_range(const char *sym, unsigned long long addr,
106 			       struct addr_range *ranges, int entries)
107 {
108 	size_t i;
109 	struct addr_range *ar;
110 
111 	for (i = 0; i < entries; ++i) {
112 		ar = &ranges[i];
113 
114 		if (strcmp(sym, ar->start_sym) == 0) {
115 			ar->start = addr;
116 			return;
117 		} else if (strcmp(sym, ar->end_sym) == 0) {
118 			ar->end = addr;
119 			return;
120 		}
121 	}
122 }
123 
124 static struct sym_entry *read_symbol(FILE *in, char **buf, size_t *buf_len)
125 {
126 	char *name, type, *p;
127 	unsigned long long addr;
128 	size_t len;
129 	ssize_t readlen;
130 	struct sym_entry *sym;
131 
132 	readlen = getline(buf, buf_len, in);
133 	if (readlen < 0) {
134 		if (errno) {
135 			perror("read_symbol");
136 			exit(EXIT_FAILURE);
137 		}
138 		return NULL;
139 	}
140 
141 	if ((*buf)[readlen - 1] == '\n')
142 		(*buf)[readlen - 1] = 0;
143 
144 	addr = strtoull(*buf, &p, 16);
145 
146 	if (*buf == p || *p++ != ' ' || !isascii((type = *p++)) || *p++ != ' ') {
147 		fprintf(stderr, "line format error\n");
148 		exit(EXIT_FAILURE);
149 	}
150 
151 	name = p;
152 	len = strlen(name);
153 
154 	if (len >= KSYM_NAME_LEN) {
155 		fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
156 				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
157 			name, len, KSYM_NAME_LEN);
158 		return NULL;
159 	}
160 
161 	if (strcmp(name, "_text") == 0)
162 		_text = addr;
163 
164 	/* Ignore most absolute/undefined (?) symbols. */
165 	if (is_ignored_symbol(name, type))
166 		return NULL;
167 
168 	check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
169 	check_symbol_range(name, addr, &percpu_range, 1);
170 
171 	/* include the type field in the symbol name, so that it gets
172 	 * compressed together */
173 	len++;
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 	char *buf = NULL;
261 	size_t buflen = 0;
262 
263 	fp = fopen(in, "r");
264 	if (!fp) {
265 		perror(in);
266 		exit(1);
267 	}
268 
269 	while (!feof(fp)) {
270 		sym = read_symbol(fp, &buf, &buflen);
271 		if (!sym)
272 			continue;
273 
274 		sym->start_pos = table_cnt;
275 
276 		if (table_cnt >= table_size) {
277 			table_size += 10000;
278 			table = realloc(table, sizeof(*table) * table_size);
279 			if (!table) {
280 				fprintf(stderr, "out of memory\n");
281 				fclose(fp);
282 				exit (1);
283 			}
284 		}
285 
286 		table[table_cnt++] = sym;
287 	}
288 
289 	free(buf);
290 	fclose(fp);
291 }
292 
293 static void output_label(const char *label)
294 {
295 	printf(".globl %s\n", label);
296 	printf("\tALGN\n");
297 	printf("%s:\n", label);
298 }
299 
300 /* Provide proper symbols relocatability by their '_text' relativeness. */
301 static void output_address(unsigned long long addr)
302 {
303 	if (_text <= addr)
304 		printf("\tPTR\t_text + %#llx\n", addr - _text);
305 	else
306 		printf("\tPTR\t_text - %#llx\n", _text - addr);
307 }
308 
309 /* uncompress a compressed symbol. When this function is called, the best table
310  * might still be compressed itself, so the function needs to be recursive */
311 static int expand_symbol(const unsigned char *data, int len, char *result)
312 {
313 	int c, rlen, total=0;
314 
315 	while (len) {
316 		c = *data;
317 		/* if the table holds a single char that is the same as the one
318 		 * we are looking for, then end the search */
319 		if (best_table[c][0]==c && best_table_len[c]==1) {
320 			*result++ = c;
321 			total++;
322 		} else {
323 			/* if not, recurse and expand */
324 			rlen = expand_symbol(best_table[c], best_table_len[c], result);
325 			total += rlen;
326 			result += rlen;
327 		}
328 		data++;
329 		len--;
330 	}
331 	*result=0;
332 
333 	return total;
334 }
335 
336 static int symbol_absolute(const struct sym_entry *s)
337 {
338 	return s->percpu_absolute;
339 }
340 
341 static void cleanup_symbol_name(char *s)
342 {
343 	char *p;
344 
345 	/*
346 	 * ASCII[.]   = 2e
347 	 * ASCII[0-9] = 30,39
348 	 * ASCII[A-Z] = 41,5a
349 	 * ASCII[_]   = 5f
350 	 * ASCII[a-z] = 61,7a
351 	 *
352 	 * As above, replacing the first '.' in ".llvm." with '\0' does not
353 	 * affect the main sorting, but it helps us with subsorting.
354 	 */
355 	p = strstr(s, ".llvm.");
356 	if (p)
357 		*p = '\0';
358 }
359 
360 static int compare_names(const void *a, const void *b)
361 {
362 	int ret;
363 	const struct sym_entry *sa = *(const struct sym_entry **)a;
364 	const struct sym_entry *sb = *(const struct sym_entry **)b;
365 
366 	ret = strcmp(sym_name(sa), sym_name(sb));
367 	if (!ret) {
368 		if (sa->addr > sb->addr)
369 			return 1;
370 		else if (sa->addr < sb->addr)
371 			return -1;
372 
373 		/* keep old order */
374 		return (int)(sa->seq - sb->seq);
375 	}
376 
377 	return ret;
378 }
379 
380 static void sort_symbols_by_name(void)
381 {
382 	qsort(table, table_cnt, sizeof(table[0]), compare_names);
383 }
384 
385 static void write_src(void)
386 {
387 	unsigned int i, k, off;
388 	unsigned int best_idx[256];
389 	unsigned int *markers;
390 	char buf[KSYM_NAME_LEN];
391 
392 	printf("#include <asm/bitsperlong.h>\n");
393 	printf("#if BITS_PER_LONG == 64\n");
394 	printf("#define PTR .quad\n");
395 	printf("#define ALGN .balign 8\n");
396 	printf("#else\n");
397 	printf("#define PTR .long\n");
398 	printf("#define ALGN .balign 4\n");
399 	printf("#endif\n");
400 
401 	printf("\t.section .rodata, \"a\"\n");
402 
403 	output_label("kallsyms_num_syms");
404 	printf("\t.long\t%u\n", table_cnt);
405 	printf("\n");
406 
407 	/* table of offset markers, that give the offset in the compressed stream
408 	 * every 256 symbols */
409 	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
410 	if (!markers) {
411 		fprintf(stderr, "kallsyms failure: "
412 			"unable to allocate required memory\n");
413 		exit(EXIT_FAILURE);
414 	}
415 
416 	output_label("kallsyms_names");
417 	off = 0;
418 	for (i = 0; i < table_cnt; i++) {
419 		if ((i & 0xFF) == 0)
420 			markers[i >> 8] = off;
421 		table[i]->seq = i;
422 
423 		/* There cannot be any symbol of length zero. */
424 		if (table[i]->len == 0) {
425 			fprintf(stderr, "kallsyms failure: "
426 				"unexpected zero symbol length\n");
427 			exit(EXIT_FAILURE);
428 		}
429 
430 		/* Only lengths that fit in up-to-two-byte ULEB128 are supported. */
431 		if (table[i]->len > 0x3FFF) {
432 			fprintf(stderr, "kallsyms failure: "
433 				"unexpected huge symbol length\n");
434 			exit(EXIT_FAILURE);
435 		}
436 
437 		/* Encode length with ULEB128. */
438 		if (table[i]->len <= 0x7F) {
439 			/* Most symbols use a single byte for the length. */
440 			printf("\t.byte 0x%02x", table[i]->len);
441 			off += table[i]->len + 1;
442 		} else {
443 			/* "Big" symbols use two bytes. */
444 			printf("\t.byte 0x%02x, 0x%02x",
445 				(table[i]->len & 0x7F) | 0x80,
446 				(table[i]->len >> 7) & 0x7F);
447 			off += table[i]->len + 2;
448 		}
449 		for (k = 0; k < table[i]->len; k++)
450 			printf(", 0x%02x", table[i]->sym[k]);
451 		printf("\n");
452 	}
453 	printf("\n");
454 
455 	/*
456 	 * Now that we wrote out the compressed symbol names, restore the
457 	 * original names, which are needed in some of the later steps.
458 	 */
459 	for (i = 0; i < table_cnt; i++) {
460 		expand_symbol(table[i]->sym, table[i]->len, buf);
461 		strcpy((char *)table[i]->sym, buf);
462 	}
463 
464 	output_label("kallsyms_markers");
465 	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
466 		printf("\t.long\t%u\n", markers[i]);
467 	printf("\n");
468 
469 	free(markers);
470 
471 	output_label("kallsyms_token_table");
472 	off = 0;
473 	for (i = 0; i < 256; i++) {
474 		best_idx[i] = off;
475 		expand_symbol(best_table[i], best_table_len[i], buf);
476 		printf("\t.asciz\t\"%s\"\n", buf);
477 		off += strlen(buf) + 1;
478 	}
479 	printf("\n");
480 
481 	output_label("kallsyms_token_index");
482 	for (i = 0; i < 256; i++)
483 		printf("\t.short\t%d\n", best_idx[i]);
484 	printf("\n");
485 
486 	if (!base_relative)
487 		output_label("kallsyms_addresses");
488 	else
489 		output_label("kallsyms_offsets");
490 
491 	for (i = 0; i < table_cnt; i++) {
492 		if (base_relative) {
493 			/*
494 			 * Use the offset relative to the lowest value
495 			 * encountered of all relative symbols, and emit
496 			 * non-relocatable fixed offsets that will be fixed
497 			 * up at runtime.
498 			 */
499 
500 			long long offset;
501 			int overflow;
502 
503 			if (!absolute_percpu) {
504 				offset = table[i]->addr - relative_base;
505 				overflow = (offset < 0 || offset > UINT_MAX);
506 			} else if (symbol_absolute(table[i])) {
507 				offset = table[i]->addr;
508 				overflow = (offset < 0 || offset > INT_MAX);
509 			} else {
510 				offset = relative_base - table[i]->addr - 1;
511 				overflow = (offset < INT_MIN || offset >= 0);
512 			}
513 			if (overflow) {
514 				fprintf(stderr, "kallsyms failure: "
515 					"%s symbol value %#llx out of range in relative mode\n",
516 					symbol_absolute(table[i]) ? "absolute" : "relative",
517 					table[i]->addr);
518 				exit(EXIT_FAILURE);
519 			}
520 			printf("\t.long\t%#x	/* %s */\n", (int)offset, table[i]->sym);
521 		} else if (!symbol_absolute(table[i])) {
522 			output_address(table[i]->addr);
523 		} else {
524 			printf("\tPTR\t%#llx\n", table[i]->addr);
525 		}
526 	}
527 	printf("\n");
528 
529 	if (base_relative) {
530 		output_label("kallsyms_relative_base");
531 		output_address(relative_base);
532 		printf("\n");
533 	}
534 
535 	if (lto_clang)
536 		for (i = 0; i < table_cnt; i++)
537 			cleanup_symbol_name((char *)table[i]->sym);
538 
539 	sort_symbols_by_name();
540 	output_label("kallsyms_seqs_of_names");
541 	for (i = 0; i < table_cnt; i++)
542 		printf("\t.byte 0x%02x, 0x%02x, 0x%02x\n",
543 			(unsigned char)(table[i]->seq >> 16),
544 			(unsigned char)(table[i]->seq >> 8),
545 			(unsigned char)(table[i]->seq >> 0));
546 	printf("\n");
547 }
548 
549 
550 /* table lookup compression functions */
551 
552 /* count all the possible tokens in a symbol */
553 static void learn_symbol(const unsigned char *symbol, int len)
554 {
555 	int i;
556 
557 	for (i = 0; i < len - 1; i++)
558 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
559 }
560 
561 /* decrease the count for all the possible tokens in a symbol */
562 static void forget_symbol(const unsigned char *symbol, int len)
563 {
564 	int i;
565 
566 	for (i = 0; i < len - 1; i++)
567 		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
568 }
569 
570 /* do the initial token count */
571 static void build_initial_token_table(void)
572 {
573 	unsigned int i;
574 
575 	for (i = 0; i < table_cnt; i++)
576 		learn_symbol(table[i]->sym, table[i]->len);
577 }
578 
579 static unsigned char *find_token(unsigned char *str, int len,
580 				 const unsigned char *token)
581 {
582 	int i;
583 
584 	for (i = 0; i < len - 1; i++) {
585 		if (str[i] == token[0] && str[i+1] == token[1])
586 			return &str[i];
587 	}
588 	return NULL;
589 }
590 
591 /* replace a given token in all the valid symbols. Use the sampled symbols
592  * to update the counts */
593 static void compress_symbols(const unsigned char *str, int idx)
594 {
595 	unsigned int i, len, size;
596 	unsigned char *p1, *p2;
597 
598 	for (i = 0; i < table_cnt; i++) {
599 
600 		len = table[i]->len;
601 		p1 = table[i]->sym;
602 
603 		/* find the token on the symbol */
604 		p2 = find_token(p1, len, str);
605 		if (!p2) continue;
606 
607 		/* decrease the counts for this symbol's tokens */
608 		forget_symbol(table[i]->sym, len);
609 
610 		size = len;
611 
612 		do {
613 			*p2 = idx;
614 			p2++;
615 			size -= (p2 - p1);
616 			memmove(p2, p2 + 1, size);
617 			p1 = p2;
618 			len--;
619 
620 			if (size < 2) break;
621 
622 			/* find the token on the symbol */
623 			p2 = find_token(p1, size, str);
624 
625 		} while (p2);
626 
627 		table[i]->len = len;
628 
629 		/* increase the counts for this symbol's new tokens */
630 		learn_symbol(table[i]->sym, len);
631 	}
632 }
633 
634 /* search the token with the maximum profit */
635 static int find_best_token(void)
636 {
637 	int i, best, bestprofit;
638 
639 	bestprofit=-10000;
640 	best = 0;
641 
642 	for (i = 0; i < 0x10000; i++) {
643 		if (token_profit[i] > bestprofit) {
644 			best = i;
645 			bestprofit = token_profit[i];
646 		}
647 	}
648 	return best;
649 }
650 
651 /* this is the core of the algorithm: calculate the "best" table */
652 static void optimize_result(void)
653 {
654 	int i, best;
655 
656 	/* using the '\0' symbol last allows compress_symbols to use standard
657 	 * fast string functions */
658 	for (i = 255; i >= 0; i--) {
659 
660 		/* if this table slot is empty (it is not used by an actual
661 		 * original char code */
662 		if (!best_table_len[i]) {
663 
664 			/* find the token with the best profit value */
665 			best = find_best_token();
666 			if (token_profit[best] == 0)
667 				break;
668 
669 			/* place it in the "best" table */
670 			best_table_len[i] = 2;
671 			best_table[i][0] = best & 0xFF;
672 			best_table[i][1] = (best >> 8) & 0xFF;
673 
674 			/* replace this token in all the valid symbols */
675 			compress_symbols(best_table[i], i);
676 		}
677 	}
678 }
679 
680 /* start by placing the symbols that are actually used on the table */
681 static void insert_real_symbols_in_table(void)
682 {
683 	unsigned int i, j, c;
684 
685 	for (i = 0; i < table_cnt; i++) {
686 		for (j = 0; j < table[i]->len; j++) {
687 			c = table[i]->sym[j];
688 			best_table[c][0]=c;
689 			best_table_len[c]=1;
690 		}
691 	}
692 }
693 
694 static void optimize_token_table(void)
695 {
696 	build_initial_token_table();
697 
698 	insert_real_symbols_in_table();
699 
700 	optimize_result();
701 }
702 
703 /* guess for "linker script provide" symbol */
704 static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
705 {
706 	const char *symbol = sym_name(se);
707 	int len = se->len - 1;
708 
709 	if (len < 8)
710 		return 0;
711 
712 	if (symbol[0] != '_' || symbol[1] != '_')
713 		return 0;
714 
715 	/* __start_XXXXX */
716 	if (!memcmp(symbol + 2, "start_", 6))
717 		return 1;
718 
719 	/* __stop_XXXXX */
720 	if (!memcmp(symbol + 2, "stop_", 5))
721 		return 1;
722 
723 	/* __end_XXXXX */
724 	if (!memcmp(symbol + 2, "end_", 4))
725 		return 1;
726 
727 	/* __XXXXX_start */
728 	if (!memcmp(symbol + len - 6, "_start", 6))
729 		return 1;
730 
731 	/* __XXXXX_end */
732 	if (!memcmp(symbol + len - 4, "_end", 4))
733 		return 1;
734 
735 	return 0;
736 }
737 
738 static int compare_symbols(const void *a, const void *b)
739 {
740 	const struct sym_entry *sa = *(const struct sym_entry **)a;
741 	const struct sym_entry *sb = *(const struct sym_entry **)b;
742 	int wa, wb;
743 
744 	/* sort by address first */
745 	if (sa->addr > sb->addr)
746 		return 1;
747 	if (sa->addr < sb->addr)
748 		return -1;
749 
750 	/* sort by "weakness" type */
751 	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
752 	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
753 	if (wa != wb)
754 		return wa - wb;
755 
756 	/* sort by "linker script provide" type */
757 	wa = may_be_linker_script_provide_symbol(sa);
758 	wb = may_be_linker_script_provide_symbol(sb);
759 	if (wa != wb)
760 		return wa - wb;
761 
762 	/* sort by the number of prefix underscores */
763 	wa = strspn(sym_name(sa), "_");
764 	wb = strspn(sym_name(sb), "_");
765 	if (wa != wb)
766 		return wa - wb;
767 
768 	/* sort by initial order, so that other symbols are left undisturbed */
769 	return sa->start_pos - sb->start_pos;
770 }
771 
772 static void sort_symbols(void)
773 {
774 	qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
775 }
776 
777 static void make_percpus_absolute(void)
778 {
779 	unsigned int i;
780 
781 	for (i = 0; i < table_cnt; i++)
782 		if (symbol_in_range(table[i], &percpu_range, 1)) {
783 			/*
784 			 * Keep the 'A' override for percpu symbols to
785 			 * ensure consistent behavior compared to older
786 			 * versions of this tool.
787 			 */
788 			table[i]->sym[0] = 'A';
789 			table[i]->percpu_absolute = 1;
790 		}
791 }
792 
793 /* find the minimum non-absolute symbol address */
794 static void record_relative_base(void)
795 {
796 	unsigned int i;
797 
798 	for (i = 0; i < table_cnt; i++)
799 		if (!symbol_absolute(table[i])) {
800 			/*
801 			 * The table is sorted by address.
802 			 * Take the first non-absolute symbol value.
803 			 */
804 			relative_base = table[i]->addr;
805 			return;
806 		}
807 }
808 
809 int main(int argc, char **argv)
810 {
811 	while (1) {
812 		static const struct option long_options[] = {
813 			{"all-symbols",     no_argument, &all_symbols,     1},
814 			{"absolute-percpu", no_argument, &absolute_percpu, 1},
815 			{"base-relative",   no_argument, &base_relative,   1},
816 			{"lto-clang",       no_argument, &lto_clang,       1},
817 			{},
818 		};
819 
820 		int c = getopt_long(argc, argv, "", long_options, NULL);
821 
822 		if (c == -1)
823 			break;
824 		if (c != 0)
825 			usage();
826 	}
827 
828 	if (optind >= argc)
829 		usage();
830 
831 	read_map(argv[optind]);
832 	shrink_table();
833 	if (absolute_percpu)
834 		make_percpus_absolute();
835 	sort_symbols();
836 	if (base_relative)
837 		record_relative_base();
838 	optimize_token_table();
839 	write_src();
840 
841 	return 0;
842 }
843