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