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