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