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