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