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