blob: 056bde4365409ec4bbd59bedb52b6996a1f85970 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Torvalds1da177e2005-04-16 15:20:36 -070010 * 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 Biesheuvel2213e9a2016-03-15 14:58:19 -070025#include <limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040027#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040028
Tejun Heo9281ace2007-07-17 04:03:51 -070029#define KSYM_NAME_LEN 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct sym_entry {
32 unsigned long long addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070033 unsigned int len;
Paulo Marquesf2df3f62008-02-06 01:37:33 -080034 unsigned int start_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned char *sym;
Ard Biesheuvel8c996942016-03-15 14:58:15 -070036 unsigned int percpu_absolute;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Kees Cook78eb7152014-03-17 13:18:27 +103039struct addr_range {
40 const char *start_sym, *end_sym;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040041 unsigned long long start, end;
42};
43
44static unsigned long long _text;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070045static unsigned long long relative_base;
Kees Cook78eb7152014-03-17 13:18:27 +103046static struct addr_range text_ranges[] = {
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040047 { "_stext", "_etext" },
48 { "_sinittext", "_einittext" },
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040049};
50#define text_range_text (&text_ranges[0])
51#define text_range_inittext (&text_ranges[1])
52
Rusty Russellc6bda7c2014-03-17 14:05:46 +103053static struct addr_range percpu_range = {
54 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
55};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static struct sym_entry *table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070058static unsigned int table_size, table_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int all_symbols = 0;
Rusty Russellc6bda7c2014-03-17 14:05:46 +103060static int absolute_percpu = 0;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070061static int base_relative = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090063static int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* the table that holds the result of the compression */
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090066static unsigned char best_table[256][2];
67static unsigned char best_table_len[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070070static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Ming Leif6537f22013-11-02 09:11:33 +103072 fprintf(stderr, "Usage: kallsyms [--all-symbols] "
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070073 "[--base-relative] < in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 exit(1);
75}
76
Masahiro Yamada29e55ad2019-11-24 01:04:35 +090077static char *sym_name(const struct sym_entry *s)
78{
79 return (char *)s->sym + 1;
80}
81
Kees Cook78eb7152014-03-17 13:18:27 +103082static int check_symbol_range(const char *sym, unsigned long long addr,
83 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040084{
85 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +103086 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040087
Kees Cook78eb7152014-03-17 13:18:27 +103088 for (i = 0; i < entries; ++i) {
89 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040090
Kees Cook78eb7152014-03-17 13:18:27 +103091 if (strcmp(sym, ar->start_sym) == 0) {
92 ar->start = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040093 return 0;
Kees Cook78eb7152014-03-17 13:18:27 +103094 } else if (strcmp(sym, ar->end_sym) == 0) {
95 ar->end = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040096 return 0;
97 }
98 }
99
100 return 1;
101}
102
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700103static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900105 char sym[500], stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 int rc;
107
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900108 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (rc != 3) {
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900110 if (rc != EOF && fgets(sym, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600111 fprintf(stderr, "Read error or end of file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return -1;
113 }
Eugene Loh6db29832019-01-17 14:46:00 -0800114 if (strlen(sym) >= KSYM_NAME_LEN) {
115 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900116 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900117 sym, strlen(sym), KSYM_NAME_LEN);
Andi Kleenf3462aa2013-10-23 15:07:53 +0200118 return -1;
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100122 if (strcmp(sym, "_text") == 0)
123 _text = s->addr;
Kees Cook78eb7152014-03-17 13:18:27 +1030124 else if (check_symbol_range(sym, s->addr, text_ranges,
125 ARRAY_SIZE(text_ranges)) == 0)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400126 /* nothing to do */;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700127 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 {
129 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700130 if (strcmp(sym, "__kernel_syscall_via_break") &&
131 strcmp(sym, "__kernel_syscall_via_epc") &&
132 strcmp(sym, "__kernel_sigtramp") &&
133 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -1;
135
136 }
Masahiro Yamadae0109042019-11-24 01:04:33 +0900137 else if (toupper(stype) == 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -1;
Masahiro Yamadae0109042019-11-24 01:04:33 +0900139 /*
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 Yamada534c9f22018-05-09 16:23:47 +0900144 else if (sym[0] == '$')
Ralf Baechle6f00df22005-09-06 15:16:41 -0700145 return -1;
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200146 /* exclude debugging symbols */
Guenter Roeck51962a92017-10-13 15:57:58 -0700147 else if (stype == 'N' || stype == 'n')
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200148 return -1;
Vasily Gorbik33177f012019-06-28 19:22:47 +0200149 /* exclude s390 kasan local symbols */
150 else if (!strncmp(sym, ".LASANPC", 8))
151 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 /* include the type field in the symbol name, so that it gets
154 * compressed together */
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900155 s->len = strlen(sym) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700156 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800157 if (!s->sym) {
158 fprintf(stderr, "kallsyms failure: "
159 "unable to allocate required amount of memory\n");
160 exit(EXIT_FAILURE);
161 }
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900162 strcpy(sym_name(s), sym);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700163 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700165 s->percpu_absolute = 0;
166
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030167 /* Record if we've found __per_cpu_start/end. */
168 check_symbol_range(sym, s->addr, &percpu_range, 1);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171}
172
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900173static int symbol_in_range(const struct sym_entry *s,
174 const struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400175{
176 size_t i;
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900177 const struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400178
Kees Cook78eb7152014-03-17 13:18:27 +1030179 for (i = 0; i < entries; ++i) {
180 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400181
Kees Cook78eb7152014-03-17 13:18:27 +1030182 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400183 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400184 }
185
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400186 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400187}
188
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900189static int symbol_valid(const struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 /* Symbols which vary between passes. Passes 1 and 2 must have
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100192 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900196 static const char * const special_symbols[] = {
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100197 "kallsyms_addresses",
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700198 "kallsyms_offsets",
199 "kallsyms_relative_base",
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100200 "kallsyms_num_syms",
201 "kallsyms_names",
202 "kallsyms_markers",
203 "kallsyms_token_table",
204 "kallsyms_token_index",
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Exclude linker generated symbols which vary between passes */
207 "_SDA_BASE_", /* ppc */
208 "_SDA2_BASE_", /* ppc */
209 NULL };
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200210
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900211 static const char * const special_prefixes[] = {
Ard Biesheuvel56067812017-02-03 09:54:05 +0000212 "__crc_", /* modversions */
Ard Biesheuvel1212f7a2018-03-01 17:19:01 +0000213 "__efistub_", /* arm64 EFI stub namespace */
Ard Biesheuvel56067812017-02-03 09:54:05 +0000214 NULL };
215
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900216 static const char * const special_suffixes[] = {
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200217 "_veneer", /* arm */
Ard Biesheuvelb9b74be2016-04-01 14:33:48 +0100218 "_from_arm", /* arm */
219 "_from_thumb", /* arm */
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200220 NULL };
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int i;
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900223 const char *name = sym_name(s);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /* if --all-symbols is not specified, then symbols outside the text
226 * and inittext sections are discarded */
227 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030228 if (symbol_in_range(s, text_ranges,
229 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800232 * _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 Torvalds1da177e2005-04-16 15:20:36 -0700236 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400237 if ((s->addr == text_range_text->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900238 strcmp(name, text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400239 (s->addr == text_range_inittext->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900240 strcmp(name, text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
242 }
243
244 /* Exclude symbols which vary between passes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 for (i = 0; special_symbols[i]; i++)
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900246 if (strcmp(name, special_symbols[i]) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return 0;
248
Ard Biesheuvel56067812017-02-03 09:54:05 +0000249 for (i = 0; special_prefixes[i]; i++) {
250 int l = strlen(special_prefixes[i]);
251
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900252 if (strncmp(name, special_prefixes[i], l) == 0)
Ard Biesheuvel56067812017-02-03 09:54:05 +0000253 return 0;
254 }
255
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200256 for (i = 0; special_suffixes[i]; i++) {
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900257 int l = strlen(name) - strlen(special_suffixes[i]);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200258
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900259 if (l >= 0 && strcmp(name + l, special_suffixes[i]) == 0)
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200260 return 0;
261 }
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 1;
264}
265
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900266/* remove all the invalid symbols from the table */
267static 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 Marquesb3dbb4e2005-09-06 15:16:31 -0700290static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700293 if (table_cnt >= table_size) {
294 table_size += 10000;
295 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!table) {
297 fprintf(stderr, "out of memory\n");
298 exit (1);
299 }
300 }
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800301 if (read_symbol(in, &table[table_cnt]) == 0) {
302 table[table_cnt].start_pos = table_cnt;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700303 table_cnt++;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306}
307
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900308static void output_label(const char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900310 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 printf("\tALGN\n");
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900312 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
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 Yamada4bfe2b72019-11-24 01:04:38 +0900317static int expand_symbol(const unsigned char *data, int len, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
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 Yamada4bfe2b72019-11-24 01:04:38 +0900342static int symbol_absolute(const struct sym_entry *s)
Kees Cook78eb7152014-03-17 13:18:27 +1030343{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700344 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030345}
346
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700347static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700349 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 unsigned int best_idx[256];
351 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700352 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Masahiro Yamada500193e2019-02-04 10:53:18 +0900354 printf("#include <asm/bitsperlong.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 printf("#if BITS_PER_LONG == 64\n");
356 printf("#define PTR .quad\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100357 printf("#define ALGN .balign 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printf("#else\n");
359 printf("#define PTR .long\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100360 printf("#define ALGN .balign 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 printf("#endif\n");
362
Jan Beulichaad09472006-12-08 02:35:57 -0800363 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700365 /* 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. Biedermanfd593d12006-12-07 02:14:04 +0100377 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700378 if (!base_relative)
379 output_label("kallsyms_addresses");
380 else
381 output_label("kallsyms_offsets");
382
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700383 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700384 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 Goyal2c22d8b2006-12-07 02:14:10 +0100407 if (_text <= table[i].addr)
408 printf("\tPTR\t_text + %#llx\n",
409 table[i].addr - _text);
410 else
Andrew Morton2930ffc2014-03-10 15:49:48 -0700411 printf("\tPTR\t_text - %#llx\n",
412 _text - table[i].addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100413 } else {
414 printf("\tPTR\t%#llx\n", table[i].addr);
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 printf("\n");
418
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700419 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 Torvalds1da177e2005-04-16 15:20:36 -0700425 output_label("kallsyms_num_syms");
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600426 printf("\t.long\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 printf("\n");
428
429 /* table of offset markers, that give the offset in the compressed stream
430 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800431 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 Torvalds1da177e2005-04-16 15:20:36 -0700437
438 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700440 for (i = 0; i < table_cnt; i++) {
441 if ((i & 0xFF) == 0)
442 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
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 Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 printf("\n");
452
453 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700454 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600455 printf("\t.long\t%u\n", markers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 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 Marquesb3dbb4e2005-09-06 15:16:31 -0700464 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 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 Torvalds1da177e2005-04-16 15:20:36 -0700479/* count all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900480static void learn_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 int i;
483
484 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700485 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/* decrease the count for all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900489static void forget_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 int i;
492
493 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700494 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900497/* do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void build_initial_tok_table(void)
499{
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900500 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900502 for (i = 0; i < table_cnt; i++)
503 learn_symbol(table[i].sym, table[i].len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Masahiro Yamada2558c132019-11-24 01:04:37 +0900506static unsigned char *find_token(unsigned char *str, int len,
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900507 const unsigned char *token)
Paulo Marques7c5d2492007-06-20 18:09:00 +0100508{
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 Torvalds1da177e2005-04-16 15:20:36 -0700518/* replace a given token in all the valid symbols. Use the sampled symbols
519 * to update the counts */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900520static void compress_symbols(const unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700522 unsigned int i, len, size;
523 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700525 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700528 p1 = table[i].sym;
529
530 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100531 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700532 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 Torvalds1da177e2005-04-16 15:20:36 -0700538
539 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700540 *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 Torvalds1da177e2005-04-16 15:20:36 -0700549 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100550 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700552 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700554 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700556 /* increase the counts for this symbol's new tokens */
557 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559}
560
561/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700562static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700564 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700567 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700569 for (i = 0; i < 0x10000; i++) {
570 if (token_profit[i] > bestprofit) {
571 best = i;
572 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return best;
576}
577
578/* this is the core of the algorithm: calculate the "best" table */
579static void optimize_result(void)
580{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700581 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
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 jincbf7a902018-02-27 16:16:19 +0800591 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800593 if (token_profit[best] == 0)
594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700597 best_table_len[i] = 2;
598 best_table[i][0] = best & 0xFF;
599 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700602 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 }
605}
606
607/* start by placing the symbols that are actually used on the table */
608static void insert_real_symbols_in_table(void)
609{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700610 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700612 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 Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 }
619}
620
621static void optimize_token_table(void)
622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 build_initial_tok_table();
624
625 insert_real_symbols_in_table();
626
627 optimize_result();
628}
629
Lai Jiangshanb478b782009-03-13 15:10:26 +0800630/* guess for "linker script provide" symbol */
631static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
632{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900633 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800634 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 Marquesf2df3f62008-02-06 01:37:33 -0800665static 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 Jiangshanb478b782009-03-13 15:10:26 +0800686 /* 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 Yamadaaa915242019-11-24 01:04:36 +0900693 wa = strspn(sym_name(sa), "_");
694 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800695 if (wa != wb)
696 return wa - wb;
697
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800698 /* sort by initial order, so that other symbols are left undisturbed */
699 return sa->start_pos - sb->start_pos;
700}
701
702static void sort_symbols(void)
703{
704 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
705}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030707static void make_percpus_absolute(void)
708{
709 unsigned int i;
710
711 for (i = 0; i < table_cnt; i++)
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700712 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 Russellc6bda7c2014-03-17 14:05:46 +1030718 table[i].sym[0] = 'A';
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700719 table[i].percpu_absolute = 1;
720 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030721}
722
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700723/* find the minimum non-absolute symbol address */
724static void record_relative_base(void)
725{
726 unsigned int i;
727
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700728 for (i = 0; i < table_cnt; i++)
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900729 if (!symbol_absolute(&table[i])) {
730 /*
731 * The table is sorted by address.
732 * Take the first non-absolute symbol value.
733 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700734 relative_base = table[i].addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900735 return;
736 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700737}
738
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700739int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700741 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 Russellc6bda7c2014-03-17 14:05:46 +1030746 else if (strcmp(argv[i], "--absolute-percpu") == 0)
747 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900748 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700749 base_relative = 1;
750 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700751 usage();
752 }
753 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 usage();
755
756 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900757 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030758 if (absolute_percpu)
759 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900760 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700761 if (base_relative)
762 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100763 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 write_src();
765
766 return 0;
767}