blob: 04a1dd16edcfea5a35b0c0620aeb63ffdcede1e6 [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
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090021#include <stdbool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070026#include <limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040028#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040029
Tejun Heo9281ace2007-07-17 04:03:51 -070030#define KSYM_NAME_LEN 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct sym_entry {
33 unsigned long long addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070034 unsigned int len;
Paulo Marquesf2df3f62008-02-06 01:37:33 -080035 unsigned int start_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 unsigned char *sym;
Ard Biesheuvel8c996942016-03-15 14:58:15 -070037 unsigned int percpu_absolute;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
Kees Cook78eb7152014-03-17 13:18:27 +103040struct addr_range {
41 const char *start_sym, *end_sym;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040042 unsigned long long start, end;
43};
44
45static unsigned long long _text;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070046static unsigned long long relative_base;
Kees Cook78eb7152014-03-17 13:18:27 +103047static struct addr_range text_ranges[] = {
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040048 { "_stext", "_etext" },
49 { "_sinittext", "_einittext" },
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040050};
51#define text_range_text (&text_ranges[0])
52#define text_range_inittext (&text_ranges[1])
53
Rusty Russellc6bda7c2014-03-17 14:05:46 +103054static struct addr_range percpu_range = {
55 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
56};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct sym_entry *table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070059static unsigned int table_size, table_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int all_symbols = 0;
Rusty Russellc6bda7c2014-03-17 14:05:46 +103061static int absolute_percpu = 0;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070062static int base_relative = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090064static int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* the table that holds the result of the compression */
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090067static unsigned char best_table[256][2];
68static unsigned char best_table_len[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070071static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Ming Leif6537f22013-11-02 09:11:33 +103073 fprintf(stderr, "Usage: kallsyms [--all-symbols] "
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070074 "[--base-relative] < in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 exit(1);
76}
77
Masahiro Yamada29e55ad2019-11-24 01:04:35 +090078static char *sym_name(const struct sym_entry *s)
79{
80 return (char *)s->sym + 1;
81}
82
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090083static bool is_ignored_symbol(const char *name, char type)
84{
85 static const char * const ignored_symbols[] = {
86 /*
87 * Symbols which vary between passes. Passes 1 and 2 must have
88 * identical symbol lists. The kallsyms_* symbols below are
89 * only added after pass 1, they would be included in pass 2
90 * when --all-symbols is specified so exclude them to get a
91 * stable symbol list.
92 */
93 "kallsyms_addresses",
94 "kallsyms_offsets",
95 "kallsyms_relative_base",
96 "kallsyms_num_syms",
97 "kallsyms_names",
98 "kallsyms_markers",
99 "kallsyms_token_table",
100 "kallsyms_token_index",
101 /* Exclude linker generated symbols which vary between passes */
102 "_SDA_BASE_", /* ppc */
103 "_SDA2_BASE_", /* ppc */
104 NULL
105 };
106
107 static const char * const ignored_prefixes[] = {
Masahiro Yamada97261e12019-11-24 01:04:40 +0900108 "$", /* local symbols for ARM, MIPS, etc. */
109 ".LASANPC", /* s390 kasan local symbols */
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900110 "__crc_", /* modversions */
111 "__efistub_", /* arm64 EFI stub namespace */
112 NULL
113 };
114
115 static const char * const ignored_suffixes[] = {
116 "_from_arm", /* arm */
117 "_from_thumb", /* arm */
118 "_veneer", /* arm */
119 NULL
120 };
121
122 const char * const *p;
123
124 /* Exclude symbols which vary between passes. */
125 for (p = ignored_symbols; *p; p++)
126 if (!strcmp(name, *p))
127 return true;
128
129 for (p = ignored_prefixes; *p; p++)
130 if (!strncmp(name, *p, strlen(*p)))
131 return true;
132
133 for (p = ignored_suffixes; *p; p++) {
134 int l = strlen(name) - strlen(*p);
135
136 if (l >= 0 && !strcmp(name + l, *p))
137 return true;
138 }
139
140 return false;
141}
142
Kees Cook78eb7152014-03-17 13:18:27 +1030143static int check_symbol_range(const char *sym, unsigned long long addr,
144 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400145{
146 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030147 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400148
Kees Cook78eb7152014-03-17 13:18:27 +1030149 for (i = 0; i < entries; ++i) {
150 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400151
Kees Cook78eb7152014-03-17 13:18:27 +1030152 if (strcmp(sym, ar->start_sym) == 0) {
153 ar->start = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400154 return 0;
Kees Cook78eb7152014-03-17 13:18:27 +1030155 } else if (strcmp(sym, ar->end_sym) == 0) {
156 ar->end = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400157 return 0;
158 }
159 }
160
161 return 1;
162}
163
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700164static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900166 char sym[500], stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int rc;
168
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900169 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (rc != 3) {
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900171 if (rc != EOF && fgets(sym, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600172 fprintf(stderr, "Read error or end of file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -1;
174 }
Eugene Loh6db29832019-01-17 14:46:00 -0800175 if (strlen(sym) >= KSYM_NAME_LEN) {
176 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900177 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900178 sym, strlen(sym), KSYM_NAME_LEN);
Andi Kleenf3462aa2013-10-23 15:07:53 +0200179 return -1;
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900182 if (is_ignored_symbol(sym, stype))
183 return -1;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100186 if (strcmp(sym, "_text") == 0)
187 _text = s->addr;
Kees Cook78eb7152014-03-17 13:18:27 +1030188 else if (check_symbol_range(sym, s->addr, text_ranges,
189 ARRAY_SIZE(text_ranges)) == 0)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400190 /* nothing to do */;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700191 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 {
193 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700194 if (strcmp(sym, "__kernel_syscall_via_break") &&
195 strcmp(sym, "__kernel_syscall_via_epc") &&
196 strcmp(sym, "__kernel_sigtramp") &&
197 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return -1;
199
200 }
Masahiro Yamadae0109042019-11-24 01:04:33 +0900201 else if (toupper(stype) == 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return -1;
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200203 /* exclude debugging symbols */
Guenter Roeck51962a92017-10-13 15:57:58 -0700204 else if (stype == 'N' || stype == 'n')
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200205 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* include the type field in the symbol name, so that it gets
208 * compressed together */
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900209 s->len = strlen(sym) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700210 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800211 if (!s->sym) {
212 fprintf(stderr, "kallsyms failure: "
213 "unable to allocate required amount of memory\n");
214 exit(EXIT_FAILURE);
215 }
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900216 strcpy(sym_name(s), sym);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700217 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700219 s->percpu_absolute = 0;
220
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030221 /* Record if we've found __per_cpu_start/end. */
222 check_symbol_range(sym, s->addr, &percpu_range, 1);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225}
226
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900227static int symbol_in_range(const struct sym_entry *s,
228 const struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400229{
230 size_t i;
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900231 const struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400232
Kees Cook78eb7152014-03-17 13:18:27 +1030233 for (i = 0; i < entries; ++i) {
234 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400235
Kees Cook78eb7152014-03-17 13:18:27 +1030236 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400237 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400238 }
239
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400240 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400241}
242
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900243static int symbol_valid(const struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900245 const char *name = sym_name(s);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* if --all-symbols is not specified, then symbols outside the text
248 * and inittext sections are discarded */
249 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030250 if (symbol_in_range(s, text_ranges,
251 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return 0;
253 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800254 * _etext _einittext; they can move between pass 1 and 2 when
255 * the kallsyms data are added. If these symbols move then
256 * they may get dropped in pass 2, which breaks the kallsyms
257 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400259 if ((s->addr == text_range_text->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900260 strcmp(name, text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400261 (s->addr == text_range_inittext->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900262 strcmp(name, text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 1;
267}
268
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900269/* remove all the invalid symbols from the table */
270static void shrink_table(void)
271{
272 unsigned int i, pos;
273
274 pos = 0;
275 for (i = 0; i < table_cnt; i++) {
276 if (symbol_valid(&table[i])) {
277 if (pos != i)
278 table[pos] = table[i];
279 pos++;
280 } else {
281 free(table[i].sym);
282 }
283 }
284 table_cnt = pos;
285
286 /* When valid symbol is not registered, exit to error */
287 if (!table_cnt) {
288 fprintf(stderr, "No valid symbol.\n");
289 exit(1);
290 }
291}
292
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700293static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700296 if (table_cnt >= table_size) {
297 table_size += 10000;
298 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!table) {
300 fprintf(stderr, "out of memory\n");
301 exit (1);
302 }
303 }
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800304 if (read_symbol(in, &table[table_cnt]) == 0) {
305 table[table_cnt].start_pos = table_cnt;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700306 table_cnt++;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309}
310
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900311static void output_label(const char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900313 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 printf("\tALGN\n");
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900315 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318/* uncompress a compressed symbol. When this function is called, the best table
319 * might still be compressed itself, so the function needs to be recursive */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900320static int expand_symbol(const unsigned char *data, int len, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 int c, rlen, total=0;
323
324 while (len) {
325 c = *data;
326 /* if the table holds a single char that is the same as the one
327 * we are looking for, then end the search */
328 if (best_table[c][0]==c && best_table_len[c]==1) {
329 *result++ = c;
330 total++;
331 } else {
332 /* if not, recurse and expand */
333 rlen = expand_symbol(best_table[c], best_table_len[c], result);
334 total += rlen;
335 result += rlen;
336 }
337 data++;
338 len--;
339 }
340 *result=0;
341
342 return total;
343}
344
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900345static int symbol_absolute(const struct sym_entry *s)
Kees Cook78eb7152014-03-17 13:18:27 +1030346{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700347 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030348}
349
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700350static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700352 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 unsigned int best_idx[256];
354 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700355 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Masahiro Yamada500193e2019-02-04 10:53:18 +0900357 printf("#include <asm/bitsperlong.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printf("#if BITS_PER_LONG == 64\n");
359 printf("#define PTR .quad\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100360 printf("#define ALGN .balign 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 printf("#else\n");
362 printf("#define PTR .long\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100363 printf("#define ALGN .balign 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 printf("#endif\n");
365
Jan Beulichaad09472006-12-08 02:35:57 -0800366 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700368 /* Provide proper symbols relocatability by their relativeness
369 * to a fixed anchor point in the runtime image, either '_text'
370 * for absolute address tables, in which case the linker will
371 * emit the final addresses at build time. Otherwise, use the
372 * offset relative to the lowest value encountered of all relative
373 * symbols, and emit non-relocatable fixed offsets that will be fixed
374 * up at runtime.
375 *
376 * The symbol names cannot be used to construct normal symbol
377 * references as the list of symbols contains symbols that are
378 * declared static and are private to their .o files. This prevents
379 * .tmp_kallsyms.o or any other object from referencing them.
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100380 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700381 if (!base_relative)
382 output_label("kallsyms_addresses");
383 else
384 output_label("kallsyms_offsets");
385
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700386 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700387 if (base_relative) {
388 long long offset;
389 int overflow;
390
391 if (!absolute_percpu) {
392 offset = table[i].addr - relative_base;
393 overflow = (offset < 0 || offset > UINT_MAX);
394 } else if (symbol_absolute(&table[i])) {
395 offset = table[i].addr;
396 overflow = (offset < 0 || offset > INT_MAX);
397 } else {
398 offset = relative_base - table[i].addr - 1;
399 overflow = (offset < INT_MIN || offset >= 0);
400 }
401 if (overflow) {
402 fprintf(stderr, "kallsyms failure: "
403 "%s symbol value %#llx out of range in relative mode\n",
404 symbol_absolute(&table[i]) ? "absolute" : "relative",
405 table[i].addr);
406 exit(EXIT_FAILURE);
407 }
408 printf("\t.long\t%#x\n", (int)offset);
409 } else if (!symbol_absolute(&table[i])) {
Vivek Goyal2c22d8b2006-12-07 02:14:10 +0100410 if (_text <= table[i].addr)
411 printf("\tPTR\t_text + %#llx\n",
412 table[i].addr - _text);
413 else
Andrew Morton2930ffc2014-03-10 15:49:48 -0700414 printf("\tPTR\t_text - %#llx\n",
415 _text - table[i].addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100416 } else {
417 printf("\tPTR\t%#llx\n", table[i].addr);
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 printf("\n");
421
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700422 if (base_relative) {
423 output_label("kallsyms_relative_base");
424 printf("\tPTR\t_text - %#llx\n", _text - relative_base);
425 printf("\n");
426 }
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 output_label("kallsyms_num_syms");
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600429 printf("\t.long\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 printf("\n");
431
432 /* table of offset markers, that give the offset in the compressed stream
433 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800434 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
435 if (!markers) {
436 fprintf(stderr, "kallsyms failure: "
437 "unable to allocate required memory\n");
438 exit(EXIT_FAILURE);
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700443 for (i = 0; i < table_cnt; i++) {
444 if ((i & 0xFF) == 0)
445 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 printf("\t.byte 0x%02x", table[i].len);
448 for (k = 0; k < table[i].len; k++)
449 printf(", 0x%02x", table[i].sym[k]);
450 printf("\n");
451
452 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 printf("\n");
455
456 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700457 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600458 printf("\t.long\t%u\n", markers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 printf("\n");
460
461 free(markers);
462
463 output_label("kallsyms_token_table");
464 off = 0;
465 for (i = 0; i < 256; i++) {
466 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700467 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 printf("\t.asciz\t\"%s\"\n", buf);
469 off += strlen(buf) + 1;
470 }
471 printf("\n");
472
473 output_label("kallsyms_token_index");
474 for (i = 0; i < 256; i++)
475 printf("\t.short\t%d\n", best_idx[i]);
476 printf("\n");
477}
478
479
480/* table lookup compression functions */
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/* count all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900483static void learn_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 int i;
486
487 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700488 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/* decrease the count for all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900492static void forget_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 int i;
495
496 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700497 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900500/* do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static void build_initial_tok_table(void)
502{
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900503 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900505 for (i = 0; i < table_cnt; i++)
506 learn_symbol(table[i].sym, table[i].len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Masahiro Yamada2558c132019-11-24 01:04:37 +0900509static unsigned char *find_token(unsigned char *str, int len,
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900510 const unsigned char *token)
Paulo Marques7c5d2492007-06-20 18:09:00 +0100511{
512 int i;
513
514 for (i = 0; i < len - 1; i++) {
515 if (str[i] == token[0] && str[i+1] == token[1])
516 return &str[i];
517 }
518 return NULL;
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/* replace a given token in all the valid symbols. Use the sampled symbols
522 * to update the counts */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900523static void compress_symbols(const unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700525 unsigned int i, len, size;
526 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700528 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700531 p1 = table[i].sym;
532
533 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100534 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700535 if (!p2) continue;
536
537 /* decrease the counts for this symbol's tokens */
538 forget_symbol(table[i].sym, len);
539
540 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700543 *p2 = idx;
544 p2++;
545 size -= (p2 - p1);
546 memmove(p2, p2 + 1, size);
547 p1 = p2;
548 len--;
549
550 if (size < 2) break;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100553 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700555 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700557 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700559 /* increase the counts for this symbol's new tokens */
560 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562}
563
564/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700565static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700567 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700570 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700572 for (i = 0; i < 0x10000; i++) {
573 if (token_profit[i] > bestprofit) {
574 best = i;
575 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return best;
579}
580
581/* this is the core of the algorithm: calculate the "best" table */
582static void optimize_result(void)
583{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700584 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 /* using the '\0' symbol last allows compress_symbols to use standard
587 * fast string functions */
588 for (i = 255; i >= 0; i--) {
589
590 /* if this table slot is empty (it is not used by an actual
591 * original char code */
592 if (!best_table_len[i]) {
593
Cao jincbf7a902018-02-27 16:16:19 +0800594 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800596 if (token_profit[best] == 0)
597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700600 best_table_len[i] = 2;
601 best_table[i][0] = best & 0xFF;
602 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700605 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 }
608}
609
610/* start by placing the symbols that are actually used on the table */
611static void insert_real_symbols_in_table(void)
612{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700613 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700615 for (i = 0; i < table_cnt; i++) {
616 for (j = 0; j < table[i].len; j++) {
617 c = table[i].sym[j];
618 best_table[c][0]=c;
619 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 }
622}
623
624static void optimize_token_table(void)
625{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 build_initial_tok_table();
627
628 insert_real_symbols_in_table();
629
630 optimize_result();
631}
632
Lai Jiangshanb478b782009-03-13 15:10:26 +0800633/* guess for "linker script provide" symbol */
634static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
635{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900636 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800637 int len = se->len - 1;
638
639 if (len < 8)
640 return 0;
641
642 if (symbol[0] != '_' || symbol[1] != '_')
643 return 0;
644
645 /* __start_XXXXX */
646 if (!memcmp(symbol + 2, "start_", 6))
647 return 1;
648
649 /* __stop_XXXXX */
650 if (!memcmp(symbol + 2, "stop_", 5))
651 return 1;
652
653 /* __end_XXXXX */
654 if (!memcmp(symbol + 2, "end_", 4))
655 return 1;
656
657 /* __XXXXX_start */
658 if (!memcmp(symbol + len - 6, "_start", 6))
659 return 1;
660
661 /* __XXXXX_end */
662 if (!memcmp(symbol + len - 4, "_end", 4))
663 return 1;
664
665 return 0;
666}
667
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800668static int compare_symbols(const void *a, const void *b)
669{
670 const struct sym_entry *sa;
671 const struct sym_entry *sb;
672 int wa, wb;
673
674 sa = a;
675 sb = b;
676
677 /* sort by address first */
678 if (sa->addr > sb->addr)
679 return 1;
680 if (sa->addr < sb->addr)
681 return -1;
682
683 /* sort by "weakness" type */
684 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
685 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
686 if (wa != wb)
687 return wa - wb;
688
Lai Jiangshanb478b782009-03-13 15:10:26 +0800689 /* sort by "linker script provide" type */
690 wa = may_be_linker_script_provide_symbol(sa);
691 wb = may_be_linker_script_provide_symbol(sb);
692 if (wa != wb)
693 return wa - wb;
694
695 /* sort by the number of prefix underscores */
Masahiro Yamadaaa915242019-11-24 01:04:36 +0900696 wa = strspn(sym_name(sa), "_");
697 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800698 if (wa != wb)
699 return wa - wb;
700
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800701 /* sort by initial order, so that other symbols are left undisturbed */
702 return sa->start_pos - sb->start_pos;
703}
704
705static void sort_symbols(void)
706{
707 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030710static void make_percpus_absolute(void)
711{
712 unsigned int i;
713
714 for (i = 0; i < table_cnt; i++)
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700715 if (symbol_in_range(&table[i], &percpu_range, 1)) {
716 /*
717 * Keep the 'A' override for percpu symbols to
718 * ensure consistent behavior compared to older
719 * versions of this tool.
720 */
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030721 table[i].sym[0] = 'A';
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700722 table[i].percpu_absolute = 1;
723 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030724}
725
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700726/* find the minimum non-absolute symbol address */
727static void record_relative_base(void)
728{
729 unsigned int i;
730
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700731 for (i = 0; i < table_cnt; i++)
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900732 if (!symbol_absolute(&table[i])) {
733 /*
734 * The table is sorted by address.
735 * Take the first non-absolute symbol value.
736 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700737 relative_base = table[i].addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900738 return;
739 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700740}
741
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700742int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700744 if (argc >= 2) {
745 int i;
746 for (i = 1; i < argc; i++) {
747 if(strcmp(argv[i], "--all-symbols") == 0)
748 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030749 else if (strcmp(argv[i], "--absolute-percpu") == 0)
750 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900751 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700752 base_relative = 1;
753 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700754 usage();
755 }
756 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 usage();
758
759 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900760 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030761 if (absolute_percpu)
762 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900763 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700764 if (base_relative)
765 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100766 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 write_src();
768
769 return 0;
770}