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