blob: 89cc7c098c5187326f17d639ea10183d8c0f3da0 [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
Kees Cook78eb7152014-03-17 13:18:27 +1030173static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
174 int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400175{
176 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030177 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
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700189static int symbol_valid(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 */
196 static char *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
Ard Biesheuvel56067812017-02-03 09:54:05 +0000211 static char *special_prefixes[] = {
212 "__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
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200216 static char *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
308static void output_label(char *label)
309{
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 */
317static int expand_symbol(unsigned char *data, int len, char *result)
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
Kees Cook78eb7152014-03-17 13:18:27 +1030342static int symbol_absolute(struct sym_entry *s)
343{
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 */
480static void learn_symbol(unsigned char *symbol, int len)
481{
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 */
489static void forget_symbol(unsigned char *symbol, int len)
490{
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
Paulo Marques7c5d2492007-06-20 18:09:00 +0100506static void *find_token(unsigned char *str, int len, unsigned char *token)
507{
508 int i;
509
510 for (i = 0; i < len - 1; i++) {
511 if (str[i] == token[0] && str[i+1] == token[1])
512 return &str[i];
513 }
514 return NULL;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/* replace a given token in all the valid symbols. Use the sampled symbols
518 * to update the counts */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700519static void compress_symbols(unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700521 unsigned int i, len, size;
522 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700524 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700527 p1 = table[i].sym;
528
529 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100530 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700531 if (!p2) continue;
532
533 /* decrease the counts for this symbol's tokens */
534 forget_symbol(table[i].sym, len);
535
536 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700539 *p2 = idx;
540 p2++;
541 size -= (p2 - p1);
542 memmove(p2, p2 + 1, size);
543 p1 = p2;
544 len--;
545
546 if (size < 2) break;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100549 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700551 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700553 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700555 /* increase the counts for this symbol's new tokens */
556 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558}
559
560/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700561static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700563 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700566 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700568 for (i = 0; i < 0x10000; i++) {
569 if (token_profit[i] > bestprofit) {
570 best = i;
571 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return best;
575}
576
577/* this is the core of the algorithm: calculate the "best" table */
578static void optimize_result(void)
579{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700580 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* using the '\0' symbol last allows compress_symbols to use standard
583 * fast string functions */
584 for (i = 255; i >= 0; i--) {
585
586 /* if this table slot is empty (it is not used by an actual
587 * original char code */
588 if (!best_table_len[i]) {
589
Cao jincbf7a902018-02-27 16:16:19 +0800590 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800592 if (token_profit[best] == 0)
593 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700596 best_table_len[i] = 2;
597 best_table[i][0] = best & 0xFF;
598 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700601 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604}
605
606/* start by placing the symbols that are actually used on the table */
607static void insert_real_symbols_in_table(void)
608{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700609 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700611 for (i = 0; i < table_cnt; i++) {
612 for (j = 0; j < table[i].len; j++) {
613 c = table[i].sym[j];
614 best_table[c][0]=c;
615 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617 }
618}
619
620static void optimize_token_table(void)
621{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 build_initial_tok_table();
623
624 insert_real_symbols_in_table();
625
626 optimize_result();
627}
628
Lai Jiangshanb478b782009-03-13 15:10:26 +0800629/* guess for "linker script provide" symbol */
630static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
631{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900632 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800633 int len = se->len - 1;
634
635 if (len < 8)
636 return 0;
637
638 if (symbol[0] != '_' || symbol[1] != '_')
639 return 0;
640
641 /* __start_XXXXX */
642 if (!memcmp(symbol + 2, "start_", 6))
643 return 1;
644
645 /* __stop_XXXXX */
646 if (!memcmp(symbol + 2, "stop_", 5))
647 return 1;
648
649 /* __end_XXXXX */
650 if (!memcmp(symbol + 2, "end_", 4))
651 return 1;
652
653 /* __XXXXX_start */
654 if (!memcmp(symbol + len - 6, "_start", 6))
655 return 1;
656
657 /* __XXXXX_end */
658 if (!memcmp(symbol + len - 4, "_end", 4))
659 return 1;
660
661 return 0;
662}
663
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800664static int compare_symbols(const void *a, const void *b)
665{
666 const struct sym_entry *sa;
667 const struct sym_entry *sb;
668 int wa, wb;
669
670 sa = a;
671 sb = b;
672
673 /* sort by address first */
674 if (sa->addr > sb->addr)
675 return 1;
676 if (sa->addr < sb->addr)
677 return -1;
678
679 /* sort by "weakness" type */
680 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
681 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
682 if (wa != wb)
683 return wa - wb;
684
Lai Jiangshanb478b782009-03-13 15:10:26 +0800685 /* sort by "linker script provide" type */
686 wa = may_be_linker_script_provide_symbol(sa);
687 wb = may_be_linker_script_provide_symbol(sb);
688 if (wa != wb)
689 return wa - wb;
690
691 /* sort by the number of prefix underscores */
Masahiro Yamadaaa915242019-11-24 01:04:36 +0900692 wa = strspn(sym_name(sa), "_");
693 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800694 if (wa != wb)
695 return wa - wb;
696
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800697 /* sort by initial order, so that other symbols are left undisturbed */
698 return sa->start_pos - sb->start_pos;
699}
700
701static void sort_symbols(void)
702{
703 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
704}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030706static void make_percpus_absolute(void)
707{
708 unsigned int i;
709
710 for (i = 0; i < table_cnt; i++)
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700711 if (symbol_in_range(&table[i], &percpu_range, 1)) {
712 /*
713 * Keep the 'A' override for percpu symbols to
714 * ensure consistent behavior compared to older
715 * versions of this tool.
716 */
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030717 table[i].sym[0] = 'A';
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700718 table[i].percpu_absolute = 1;
719 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030720}
721
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700722/* find the minimum non-absolute symbol address */
723static void record_relative_base(void)
724{
725 unsigned int i;
726
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700727 for (i = 0; i < table_cnt; i++)
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900728 if (!symbol_absolute(&table[i])) {
729 /*
730 * The table is sorted by address.
731 * Take the first non-absolute symbol value.
732 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700733 relative_base = table[i].addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900734 return;
735 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700736}
737
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700738int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700740 if (argc >= 2) {
741 int i;
742 for (i = 1; i < argc; i++) {
743 if(strcmp(argv[i], "--all-symbols") == 0)
744 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030745 else if (strcmp(argv[i], "--absolute-percpu") == 0)
746 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900747 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700748 base_relative = 1;
749 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700750 usage();
751 }
752 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 usage();
754
755 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900756 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030757 if (absolute_percpu)
758 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900759 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700760 if (base_relative)
761 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100762 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 write_src();
764
765 return 0;
766}