zram: use __bio_add_page for adding single page to bio
[linux-block.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 *
adc40221 8 * Usage: kallsyms [--all-symbols] [--absolute-percpu]
79549da6 9 * [--base-relative] [--lto-clang] in.map > out.S
1da177e4 10 *
1da177e4
LT
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
aa221f2e 22#include <getopt.h>
a41333e0 23#include <stdbool.h>
1da177e4
LT
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <ctype.h>
2213e9a6 28#include <limits.h>
1da177e4 29
17b1f0de 30#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
17b1f0de 31
b471927e
BF
32#define _stringify_1(x) #x
33#define _stringify(x) _stringify_1(x)
34
b8a94bfb 35#define KSYM_NAME_LEN 512
1da177e4 36
6e8c5bbd
MO
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 */
b8a94bfb 44#define KSYM_NAME_LEN_BUFFER 2048
6e8c5bbd
MO
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);
b471927e 49
1da177e4
LT
50struct sym_entry {
51 unsigned long long addr;
b3dbb4ec 52 unsigned int len;
60443c88 53 unsigned int seq;
f2df3f65 54 unsigned int start_pos;
8c996940 55 unsigned int percpu_absolute;
9d82973e 56 unsigned char sym[];
1da177e4
LT
57};
58
78eb7159
KC
59struct addr_range {
60 const char *start_sym, *end_sym;
17b1f0de
MF
61 unsigned long long start, end;
62};
63
64static unsigned long long _text;
2213e9a6 65static unsigned long long relative_base;
78eb7159 66static struct addr_range text_ranges[] = {
17b1f0de
MF
67 { "_stext", "_etext" },
68 { "_sinittext", "_einittext" },
17b1f0de
MF
69};
70#define text_range_text (&text_ranges[0])
71#define text_range_inittext (&text_ranges[1])
72
c6bda7c9
RR
73static struct addr_range percpu_range = {
74 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
75};
76
8d605269 77static struct sym_entry **table;
b3dbb4ec 78static unsigned int table_size, table_cnt;
831362fc
MY
79static int all_symbols;
80static int absolute_percpu;
81static int base_relative;
010a0aad 82static int lto_clang;
1da177e4 83
f43e9daa 84static int token_profit[0x10000];
1da177e4
LT
85
86/* the table that holds the result of the compression */
f43e9daa
MY
87static unsigned char best_table[256][2];
88static unsigned char best_table_len[256];
1da177e4
LT
89
90
b3dbb4ec 91static void usage(void)
1da177e4 92{
8d3a7507 93 fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] "
010a0aad 94 "[--base-relative] [--lto-clang] in.map > out.S\n");
1da177e4
LT
95 exit(1);
96}
97
29e55ad3
MY
98static char *sym_name(const struct sym_entry *s)
99{
100 return (char *)s->sym + 1;
101}
102
a41333e0
MY
103static bool is_ignored_symbol(const char *name, char type)
104{
a7b00a18 105 if (type == 'u' || type == 'n')
887df76d
MY
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
a41333e0
MY
117 return false;
118}
119
b6233d0d
MY
120static void check_symbol_range(const char *sym, unsigned long long addr,
121 struct addr_range *ranges, int entries)
17b1f0de
MF
122{
123 size_t i;
78eb7159 124 struct addr_range *ar;
17b1f0de 125
78eb7159
KC
126 for (i = 0; i < entries; ++i) {
127 ar = &ranges[i];
17b1f0de 128
78eb7159
KC
129 if (strcmp(sym, ar->start_sym) == 0) {
130 ar->start = addr;
b6233d0d 131 return;
78eb7159
KC
132 } else if (strcmp(sym, ar->end_sym) == 0) {
133 ar->end = addr;
b6233d0d 134 return;
17b1f0de
MF
135 }
136 }
17b1f0de
MF
137}
138
8d605269 139static struct sym_entry *read_symbol(FILE *in)
1da177e4 140{
b471927e 141 char name[KSYM_NAME_LEN_BUFFER+1], type;
8d605269
MY
142 unsigned long long addr;
143 unsigned int len;
144 struct sym_entry *sym;
1da177e4
LT
145 int rc;
146
b471927e 147 rc = fscanf(in, "%llx %c %" _stringify(KSYM_NAME_LEN_BUFFER) "s\n", &addr, &type, name);
1da177e4 148 if (rc != 3) {
b66c874f 149 if (rc != EOF && fgets(name, ARRAY_SIZE(name), in) == NULL)
ef894870 150 fprintf(stderr, "Read error or end of file.\n");
8d605269 151 return NULL;
1da177e4 152 }
be9f6133 153 if (strlen(name) >= KSYM_NAME_LEN) {
6db2983c 154 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
bb66fc67 155 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
be9f6133 156 name, strlen(name), KSYM_NAME_LEN);
8d605269 157 return NULL;
f3462aa9 158 }
1da177e4 159
be9f6133 160 if (strcmp(name, "_text") == 0)
8d605269 161 _text = addr;
b6233d0d 162
7883a143
MP
163 /* Ignore most absolute/undefined (?) symbols. */
164 if (is_ignored_symbol(name, type))
165 return NULL;
166
8d605269
MY
167 check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
168 check_symbol_range(name, addr, &percpu_range, 1);
1da177e4
LT
169
170 /* include the type field in the symbol name, so that it gets
171 * compressed together */
8d605269
MY
172
173 len = strlen(name) + 1;
174
9d1b3895 175 sym = malloc(sizeof(*sym) + len + 1);
8d605269 176 if (!sym) {
f1a136e0
JJ
177 fprintf(stderr, "kallsyms failure: "
178 "unable to allocate required amount of memory\n");
179 exit(EXIT_FAILURE);
180 }
8d605269
MY
181 sym->addr = addr;
182 sym->len = len;
183 sym->sym[0] = type;
9d1b3895 184 strcpy(sym_name(sym), name);
8d605269 185 sym->percpu_absolute = 0;
8c996940 186
8d605269 187 return sym;
1da177e4
LT
188}
189
4bfe2b78
MY
190static int symbol_in_range(const struct sym_entry *s,
191 const struct addr_range *ranges, int entries)
17b1f0de
MF
192{
193 size_t i;
4bfe2b78 194 const struct addr_range *ar;
17b1f0de 195
78eb7159
KC
196 for (i = 0; i < entries; ++i) {
197 ar = &ranges[i];
17b1f0de 198
78eb7159 199 if (s->addr >= ar->start && s->addr <= ar->end)
ac6ca5c8 200 return 1;
17b1f0de
MF
201 }
202
ac6ca5c8 203 return 0;
17b1f0de
MF
204}
205
4bfe2b78 206static int symbol_valid(const struct sym_entry *s)
1da177e4 207{
29e55ad3 208 const char *name = sym_name(s);
bd8b22d2 209
1da177e4
LT
210 /* if --all-symbols is not specified, then symbols outside the text
211 * and inittext sections are discarded */
212 if (!all_symbols) {
78eb7159
KC
213 if (symbol_in_range(s, text_ranges,
214 ARRAY_SIZE(text_ranges)) == 0)
1da177e4
LT
215 return 0;
216 /* Corner case. Discard any symbols with the same value as
a3b81113
RG
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.
1da177e4 221 */
17b1f0de 222 if ((s->addr == text_range_text->end &&
29e55ad3 223 strcmp(name, text_range_text->end_sym)) ||
17b1f0de 224 (s->addr == text_range_inittext->end &&
29e55ad3 225 strcmp(name, text_range_inittext->end_sym)))
1da177e4
LT
226 return 0;
227 }
228
1da177e4
LT
229 return 1;
230}
231
5e5c4fa7
MY
232/* remove all the invalid symbols from the table */
233static void shrink_table(void)
234{
235 unsigned int i, pos;
236
237 pos = 0;
238 for (i = 0; i < table_cnt; i++) {
8d605269 239 if (symbol_valid(table[i])) {
5e5c4fa7
MY
240 if (pos != i)
241 table[pos] = table[i];
242 pos++;
243 } else {
8d605269 244 free(table[i]);
5e5c4fa7
MY
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
aa221f2e 256static void read_map(const char *in)
1da177e4 257{
aa221f2e 258 FILE *fp;
8d605269
MY
259 struct sym_entry *sym;
260
aa221f2e
MY
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);
8d605269
MY
269 if (!sym)
270 continue;
271
272 sym->start_pos = table_cnt;
273
b3dbb4ec
PM
274 if (table_cnt >= table_size) {
275 table_size += 10000;
276 table = realloc(table, sizeof(*table) * table_size);
1da177e4
LT
277 if (!table) {
278 fprintf(stderr, "out of memory\n");
aa221f2e 279 fclose(fp);
1da177e4
LT
280 exit (1);
281 }
282 }
8d605269
MY
283
284 table[table_cnt++] = sym;
1da177e4 285 }
aa221f2e
MY
286
287 fclose(fp);
1da177e4
LT
288}
289
4bfe2b78 290static void output_label(const char *label)
1da177e4 291{
534c9f2e 292 printf(".globl %s\n", label);
1da177e4 293 printf("\tALGN\n");
534c9f2e 294 printf("%s:\n", label);
1da177e4
LT
295}
296
fd2ab2f6
MY
297/* Provide proper symbols relocatability by their '_text' relativeness. */
298static 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
1da177e4
LT
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 */
4bfe2b78 308static int expand_symbol(const unsigned char *data, int len, char *result)
1da177e4
LT
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
4bfe2b78 333static int symbol_absolute(const struct sym_entry *s)
78eb7159 334{
8c996940 335 return s->percpu_absolute;
78eb7159
KC
336}
337
010a0aad
ZL
338static void cleanup_symbol_name(char *s)
339{
340 char *p;
341
010a0aad
ZL
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
60443c88
ZL
357static int compare_names(const void *a, const void *b)
358{
359 int ret;
60443c88
ZL
360 const struct sym_entry *sa = *(const struct sym_entry **)a;
361 const struct sym_entry *sb = *(const struct sym_entry **)b;
362
dd1553b8 363 ret = strcmp(sym_name(sa), sym_name(sb));
60443c88
ZL
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
377static void sort_symbols_by_name(void)
378{
379 qsort(table, table_cnt, sizeof(table[0]), compare_names);
380}
381
b3dbb4ec 382static void write_src(void)
1da177e4 383{
b3dbb4ec 384 unsigned int i, k, off;
1da177e4
LT
385 unsigned int best_idx[256];
386 unsigned int *markers;
9281acea 387 char buf[KSYM_NAME_LEN];
1da177e4 388
500193ec 389 printf("#include <asm/bitsperlong.h>\n");
1da177e4
LT
390 printf("#if BITS_PER_LONG == 64\n");
391 printf("#define PTR .quad\n");
72d3ebb9 392 printf("#define ALGN .balign 8\n");
1da177e4
LT
393 printf("#else\n");
394 printf("#define PTR .long\n");
72d3ebb9 395 printf("#define ALGN .balign 4\n");
1da177e4
LT
396 printf("#endif\n");
397
aad09470 398 printf("\t.section .rodata, \"a\"\n");
1da177e4 399
1da177e4 400 output_label("kallsyms_num_syms");
80ffbaa5 401 printf("\t.long\t%u\n", table_cnt);
1da177e4
LT
402 printf("\n");
403
404 /* table of offset markers, that give the offset in the compressed stream
405 * every 256 symbols */
f1a136e0
JJ
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 }
1da177e4
LT
412
413 output_label("kallsyms_names");
1da177e4 414 off = 0;
b3dbb4ec
PM
415 for (i = 0; i < table_cnt; i++) {
416 if ((i & 0xFF) == 0)
417 markers[i >> 8] = off;
60443c88 418 table[i]->seq = i;
1da177e4 419
73bbb944
MO
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 }
8d605269
MY
446 for (k = 0; k < table[i]->len; k++)
447 printf(", 0x%02x", table[i]->sym[k]);
1da177e4 448 printf("\n");
1da177e4
LT
449 }
450 printf("\n");
451
dd1553b8
MY
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
1da177e4 461 output_label("kallsyms_markers");
b3dbb4ec 462 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
80ffbaa5 463 printf("\t.long\t%u\n", markers[i]);
1da177e4
LT
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;
b3dbb4ec 472 expand_symbol(best_table[i], best_table_len[i], buf);
1da177e4
LT
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");
404bad70
MY
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 }
dd1553b8 517 printf("\t.long\t%#x /* %s */\n", (int)offset, table[i]->sym);
404bad70
MY
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
dd1553b8
MY
532 if (lto_clang)
533 for (i = 0; i < table_cnt; i++)
534 cleanup_symbol_name((char *)table[i]->sym);
535
404bad70
MY
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");
1da177e4
LT
544}
545
546
547/* table lookup compression functions */
548
1da177e4 549/* count all the possible tokens in a symbol */
4bfe2b78 550static void learn_symbol(const unsigned char *symbol, int len)
1da177e4
LT
551{
552 int i;
553
554 for (i = 0; i < len - 1; i++)
b3dbb4ec 555 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
1da177e4
LT
556}
557
558/* decrease the count for all the possible tokens in a symbol */
4bfe2b78 559static void forget_symbol(const unsigned char *symbol, int len)
1da177e4
LT
560{
561 int i;
562
563 for (i = 0; i < len - 1; i++)
b3dbb4ec 564 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
1da177e4
LT
565}
566
5e5c4fa7 567/* do the initial token count */
fcdf7197 568static void build_initial_token_table(void)
1da177e4 569{
5e5c4fa7 570 unsigned int i;
1da177e4 571
5e5c4fa7 572 for (i = 0; i < table_cnt; i++)
8d605269 573 learn_symbol(table[i]->sym, table[i]->len);
1da177e4
LT
574}
575
2558c138 576static unsigned char *find_token(unsigned char *str, int len,
4bfe2b78 577 const unsigned char *token)
7c5d249a
PM
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
1da177e4
LT
588/* replace a given token in all the valid symbols. Use the sampled symbols
589 * to update the counts */
4bfe2b78 590static void compress_symbols(const unsigned char *str, int idx)
1da177e4 591{
b3dbb4ec
PM
592 unsigned int i, len, size;
593 unsigned char *p1, *p2;
1da177e4 594
b3dbb4ec 595 for (i = 0; i < table_cnt; i++) {
1da177e4 596
8d605269
MY
597 len = table[i]->len;
598 p1 = table[i]->sym;
b3dbb4ec
PM
599
600 /* find the token on the symbol */
7c5d249a 601 p2 = find_token(p1, len, str);
b3dbb4ec
PM
602 if (!p2) continue;
603
604 /* decrease the counts for this symbol's tokens */
8d605269 605 forget_symbol(table[i]->sym, len);
b3dbb4ec
PM
606
607 size = len;
1da177e4
LT
608
609 do {
b3dbb4ec
PM
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
1da177e4 619 /* find the token on the symbol */
7c5d249a 620 p2 = find_token(p1, size, str);
1da177e4 621
b3dbb4ec 622 } while (p2);
1da177e4 623
8d605269 624 table[i]->len = len;
1da177e4 625
b3dbb4ec 626 /* increase the counts for this symbol's new tokens */
8d605269 627 learn_symbol(table[i]->sym, len);
1da177e4
LT
628 }
629}
630
631/* search the token with the maximum profit */
b3dbb4ec 632static int find_best_token(void)
1da177e4 633{
b3dbb4ec 634 int i, best, bestprofit;
1da177e4
LT
635
636 bestprofit=-10000;
b3dbb4ec 637 best = 0;
1da177e4 638
b3dbb4ec
PM
639 for (i = 0; i < 0x10000; i++) {
640 if (token_profit[i] > bestprofit) {
641 best = i;
642 bestprofit = token_profit[i];
1da177e4 643 }
1da177e4 644 }
1da177e4
LT
645 return best;
646}
647
648/* this is the core of the algorithm: calculate the "best" table */
649static void optimize_result(void)
650{
b3dbb4ec 651 int i, best;
1da177e4
LT
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
cbf7a90e 661 /* find the token with the best profit value */
1da177e4 662 best = find_best_token();
e0a04b11
XW
663 if (token_profit[best] == 0)
664 break;
1da177e4
LT
665
666 /* place it in the "best" table */
b3dbb4ec
PM
667 best_table_len[i] = 2;
668 best_table[i][0] = best & 0xFF;
669 best_table[i][1] = (best >> 8) & 0xFF;
1da177e4
LT
670
671 /* replace this token in all the valid symbols */
b3dbb4ec 672 compress_symbols(best_table[i], i);
1da177e4
LT
673 }
674 }
675}
676
677/* start by placing the symbols that are actually used on the table */
678static void insert_real_symbols_in_table(void)
679{
b3dbb4ec 680 unsigned int i, j, c;
1da177e4 681
b3dbb4ec 682 for (i = 0; i < table_cnt; i++) {
8d605269
MY
683 for (j = 0; j < table[i]->len; j++) {
684 c = table[i]->sym[j];
b3dbb4ec
PM
685 best_table[c][0]=c;
686 best_table_len[c]=1;
1da177e4
LT
687 }
688 }
689}
690
691static void optimize_token_table(void)
692{
fcdf7197 693 build_initial_token_table();
1da177e4
LT
694
695 insert_real_symbols_in_table();
696
697 optimize_result();
698}
699
b478b782
LJ
700/* guess for "linker script provide" symbol */
701static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
702{
29e55ad3 703 const char *symbol = sym_name(se);
b478b782
LJ
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
f2df3f65
PM
735static int compare_symbols(const void *a, const void *b)
736{
8d605269
MY
737 const struct sym_entry *sa = *(const struct sym_entry **)a;
738 const struct sym_entry *sb = *(const struct sym_entry **)b;
f2df3f65
PM
739 int wa, wb;
740
f2df3f65
PM
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
b478b782
LJ
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 */
aa915245
MY
760 wa = strspn(sym_name(sa), "_");
761 wb = strspn(sym_name(sb), "_");
b478b782
LJ
762 if (wa != wb)
763 return wa - wb;
764
f2df3f65
PM
765 /* sort by initial order, so that other symbols are left undisturbed */
766 return sa->start_pos - sb->start_pos;
767}
768
769static void sort_symbols(void)
770{
8d605269 771 qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
f2df3f65 772}
1da177e4 773
c6bda7c9
RR
774static void make_percpus_absolute(void)
775{
776 unsigned int i;
777
778 for (i = 0; i < table_cnt; i++)
8d605269 779 if (symbol_in_range(table[i], &percpu_range, 1)) {
8c996940
AB
780 /*
781 * Keep the 'A' override for percpu symbols to
782 * ensure consistent behavior compared to older
783 * versions of this tool.
784 */
8d605269
MY
785 table[i]->sym[0] = 'A';
786 table[i]->percpu_absolute = 1;
8c996940 787 }
c6bda7c9
RR
788}
789
2213e9a6
AB
790/* find the minimum non-absolute symbol address */
791static void record_relative_base(void)
792{
793 unsigned int i;
794
2213e9a6 795 for (i = 0; i < table_cnt; i++)
8d605269 796 if (!symbol_absolute(table[i])) {
f34ea029
MY
797 /*
798 * The table is sorted by address.
799 * Take the first non-absolute symbol value.
800 */
8d605269 801 relative_base = table[i]->addr;
f34ea029
MY
802 return;
803 }
2213e9a6
AB
804}
805
b3dbb4ec 806int main(int argc, char **argv)
1da177e4 807{
aa221f2e
MY
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},
010a0aad 813 {"lto-clang", no_argument, &lto_clang, 1},
aa221f2e
MY
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)
1da177e4
LT
826 usage();
827
aa221f2e 828 read_map(argv[optind]);
5e5c4fa7 829 shrink_table();
c6bda7c9
RR
830 if (absolute_percpu)
831 make_percpus_absolute();
f34ea029 832 sort_symbols();
2213e9a6
AB
833 if (base_relative)
834 record_relative_base();
2ea03891 835 optimize_token_table();
1da177e4
LT
836 write_src();
837
838 return 0;
839}