OSDN Git Service

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