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