blob: 4b2711c23f4ed9ac52d091b330bc82b4f6403027 [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#ifndef ARRAY_SIZE
28#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
29#endif
30
Tejun Heo9281ace2007-07-17 04:03:51 -070031#define KSYM_NAME_LEN 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033struct sym_entry {
34 unsigned long long addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070035 unsigned int len;
Paulo Marquesf2df3f62008-02-06 01:37:33 -080036 unsigned int start_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 unsigned char *sym;
Ard Biesheuvel8c996942016-03-15 14:58:15 -070038 unsigned int percpu_absolute;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Kees Cook78eb7152014-03-17 13:18:27 +103041struct addr_range {
42 const char *start_sym, *end_sym;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040043 unsigned long long start, end;
44};
45
46static unsigned long long _text;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070047static unsigned long long relative_base;
Kees Cook78eb7152014-03-17 13:18:27 +103048static struct addr_range text_ranges[] = {
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040049 { "_stext", "_etext" },
50 { "_sinittext", "_einittext" },
51 { "_stext_l1", "_etext_l1" }, /* Blackfin on-chip L1 inst SRAM */
52 { "_stext_l2", "_etext_l2" }, /* Blackfin on-chip L2 SRAM */
53};
54#define text_range_text (&text_ranges[0])
55#define text_range_inittext (&text_ranges[1])
56
Rusty Russellc6bda7c2014-03-17 14:05:46 +103057static struct addr_range percpu_range = {
58 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static struct sym_entry *table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070062static unsigned int table_size, table_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int all_symbols = 0;
Rusty Russellc6bda7c2014-03-17 14:05:46 +103064static int absolute_percpu = 0;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070065static int base_relative = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070067int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* the table that holds the result of the compression */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070070unsigned char best_table[256][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070071unsigned char best_table_len[256];
72
73
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070074static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Ming Leif6537f22013-11-02 09:11:33 +103076 fprintf(stderr, "Usage: kallsyms [--all-symbols] "
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070077 "[--base-relative] < in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 exit(1);
79}
80
81/*
82 * This ignores the intensely annoying "mapping symbols" found
83 * in ARM ELF files: $a, $t and $d.
84 */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070085static inline int is_arm_mapping_symbol(const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Kyle McMartin6c34f1f2014-09-16 22:37:18 +010087 return str[0] == '$' && strchr("axtd", str[1])
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 && (str[2] == '\0' || str[2] == '.');
89}
90
Kees Cook78eb7152014-03-17 13:18:27 +103091static int check_symbol_range(const char *sym, unsigned long long addr,
92 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040093{
94 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +103095 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040096
Kees Cook78eb7152014-03-17 13:18:27 +103097 for (i = 0; i < entries; ++i) {
98 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040099
Kees Cook78eb7152014-03-17 13:18:27 +1030100 if (strcmp(sym, ar->start_sym) == 0) {
101 ar->start = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400102 return 0;
Kees Cook78eb7152014-03-17 13:18:27 +1030103 } else if (strcmp(sym, ar->end_sym) == 0) {
104 ar->end = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400105 return 0;
106 }
107 }
108
109 return 1;
110}
111
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700112static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900114 char sym[500], stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int rc;
116
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900117 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (rc != 3) {
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900119 if (rc != EOF && fgets(sym, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600120 fprintf(stderr, "Read error or end of file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -1;
122 }
Eugene Lohcacf3c02019-01-17 14:46:00 -0800123 if (strlen(sym) >= KSYM_NAME_LEN) {
124 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900125 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900126 sym, strlen(sym), KSYM_NAME_LEN);
Andi Kleenf3462aa2013-10-23 15:07:53 +0200127 return -1;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100131 if (strcmp(sym, "_text") == 0)
132 _text = s->addr;
Kees Cook78eb7152014-03-17 13:18:27 +1030133 else if (check_symbol_range(sym, s->addr, text_ranges,
134 ARRAY_SIZE(text_ranges)) == 0)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400135 /* nothing to do */;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700136 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 {
138 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700139 if (strcmp(sym, "__kernel_syscall_via_break") &&
140 strcmp(sym, "__kernel_syscall_via_epc") &&
141 strcmp(sym, "__kernel_sigtramp") &&
142 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -1;
144
145 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700146 else if (toupper(stype) == 'U' ||
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700147 is_arm_mapping_symbol(sym))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -1;
Ralf Baechle6f00df22005-09-06 15:16:41 -0700149 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900150 else if (sym[0] == '$')
Ralf Baechle6f00df22005-09-06 15:16:41 -0700151 return -1;
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200152 /* exclude debugging symbols */
Guenter Roeck51962a92017-10-13 15:57:58 -0700153 else if (stype == 'N' || stype == 'n')
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200154 return -1;
Vasily Gorbik9eb4f282019-06-28 19:22:47 +0200155 /* exclude s390 kasan local symbols */
156 else if (!strncmp(sym, ".LASANPC", 8))
157 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 /* include the type field in the symbol name, so that it gets
160 * compressed together */
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900161 s->len = strlen(sym) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700162 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800163 if (!s->sym) {
164 fprintf(stderr, "kallsyms failure: "
165 "unable to allocate required amount of memory\n");
166 exit(EXIT_FAILURE);
167 }
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900168 strcpy((char *)s->sym + 1, sym);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700169 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700171 s->percpu_absolute = 0;
172
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030173 /* Record if we've found __per_cpu_start/end. */
174 check_symbol_range(sym, s->addr, &percpu_range, 1);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return 0;
177}
178
Kees Cook78eb7152014-03-17 13:18:27 +1030179static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
180 int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400181{
182 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030183 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400184
Kees Cook78eb7152014-03-17 13:18:27 +1030185 for (i = 0; i < entries; ++i) {
186 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400187
Kees Cook78eb7152014-03-17 13:18:27 +1030188 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400189 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400190 }
191
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400192 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400193}
194
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700195static int symbol_valid(struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 /* Symbols which vary between passes. Passes 1 and 2 must have
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100198 * identical symbol lists. The kallsyms_* symbols below are only added
199 * after pass 1, they would be included in pass 2 when --all-symbols is
200 * specified so exclude them to get a stable symbol list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
202 static char *special_symbols[] = {
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100203 "kallsyms_addresses",
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700204 "kallsyms_offsets",
205 "kallsyms_relative_base",
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100206 "kallsyms_num_syms",
207 "kallsyms_names",
208 "kallsyms_markers",
209 "kallsyms_token_table",
210 "kallsyms_token_index",
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Exclude linker generated symbols which vary between passes */
213 "_SDA_BASE_", /* ppc */
214 "_SDA2_BASE_", /* ppc */
215 NULL };
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200216
Ard Biesheuvel56067812017-02-03 09:54:05 +0000217 static char *special_prefixes[] = {
218 "__crc_", /* modversions */
Ard Biesheuvel1212f7a2018-03-01 17:19:01 +0000219 "__efistub_", /* arm64 EFI stub namespace */
Ard Biesheuvel56067812017-02-03 09:54:05 +0000220 NULL };
221
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200222 static char *special_suffixes[] = {
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200223 "_veneer", /* arm */
Ard Biesheuvelb9b74be2016-04-01 14:33:48 +0100224 "_from_arm", /* arm */
225 "_from_thumb", /* arm */
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200226 NULL };
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int i;
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200229 char *sym_name = (char *)s->sym + 1;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* if --all-symbols is not specified, then symbols outside the text
232 * and inittext sections are discarded */
233 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030234 if (symbol_in_range(s, text_ranges,
235 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return 0;
237 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800238 * _etext _einittext; they can move between pass 1 and 2 when
239 * the kallsyms data are added. If these symbols move then
240 * they may get dropped in pass 2, which breaks the kallsyms
241 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400243 if ((s->addr == text_range_text->end &&
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200244 strcmp(sym_name,
Kees Cook78eb7152014-03-17 13:18:27 +1030245 text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400246 (s->addr == text_range_inittext->end &&
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200247 strcmp(sym_name,
Kees Cook78eb7152014-03-17 13:18:27 +1030248 text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250 }
251
252 /* Exclude symbols which vary between passes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 for (i = 0; special_symbols[i]; i++)
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200254 if (strcmp(sym_name, special_symbols[i]) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
256
Ard Biesheuvel56067812017-02-03 09:54:05 +0000257 for (i = 0; special_prefixes[i]; i++) {
258 int l = strlen(special_prefixes[i]);
259
260 if (l <= strlen(sym_name) &&
261 strncmp(sym_name, special_prefixes[i], l) == 0)
262 return 0;
263 }
264
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200265 for (i = 0; special_suffixes[i]; i++) {
266 int l = strlen(sym_name) - strlen(special_suffixes[i]);
267
268 if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0)
269 return 0;
270 }
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 1;
273}
274
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700275static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700278 if (table_cnt >= table_size) {
279 table_size += 10000;
280 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (!table) {
282 fprintf(stderr, "out of memory\n");
283 exit (1);
284 }
285 }
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800286 if (read_symbol(in, &table[table_cnt]) == 0) {
287 table[table_cnt].start_pos = table_cnt;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700288 table_cnt++;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
292
293static void output_label(char *label)
294{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900295 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 printf("\tALGN\n");
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900297 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300/* uncompress a compressed symbol. When this function is called, the best table
301 * might still be compressed itself, so the function needs to be recursive */
302static int expand_symbol(unsigned char *data, int len, char *result)
303{
304 int c, rlen, total=0;
305
306 while (len) {
307 c = *data;
308 /* if the table holds a single char that is the same as the one
309 * we are looking for, then end the search */
310 if (best_table[c][0]==c && best_table_len[c]==1) {
311 *result++ = c;
312 total++;
313 } else {
314 /* if not, recurse and expand */
315 rlen = expand_symbol(best_table[c], best_table_len[c], result);
316 total += rlen;
317 result += rlen;
318 }
319 data++;
320 len--;
321 }
322 *result=0;
323
324 return total;
325}
326
Kees Cook78eb7152014-03-17 13:18:27 +1030327static int symbol_absolute(struct sym_entry *s)
328{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700329 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030330}
331
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700332static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700334 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unsigned int best_idx[256];
336 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700337 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 printf("#include <asm/types.h>\n");
340 printf("#if BITS_PER_LONG == 64\n");
341 printf("#define PTR .quad\n");
342 printf("#define ALGN .align 8\n");
343 printf("#else\n");
344 printf("#define PTR .long\n");
345 printf("#define ALGN .align 4\n");
346 printf("#endif\n");
347
Jan Beulichaad09472006-12-08 02:35:57 -0800348 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700350 /* Provide proper symbols relocatability by their relativeness
351 * to a fixed anchor point in the runtime image, either '_text'
352 * for absolute address tables, in which case the linker will
353 * emit the final addresses at build time. Otherwise, use the
354 * offset relative to the lowest value encountered of all relative
355 * symbols, and emit non-relocatable fixed offsets that will be fixed
356 * up at runtime.
357 *
358 * The symbol names cannot be used to construct normal symbol
359 * references as the list of symbols contains symbols that are
360 * declared static and are private to their .o files. This prevents
361 * .tmp_kallsyms.o or any other object from referencing them.
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100362 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700363 if (!base_relative)
364 output_label("kallsyms_addresses");
365 else
366 output_label("kallsyms_offsets");
367
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700368 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700369 if (base_relative) {
370 long long offset;
371 int overflow;
372
373 if (!absolute_percpu) {
374 offset = table[i].addr - relative_base;
375 overflow = (offset < 0 || offset > UINT_MAX);
376 } else if (symbol_absolute(&table[i])) {
377 offset = table[i].addr;
378 overflow = (offset < 0 || offset > INT_MAX);
379 } else {
380 offset = relative_base - table[i].addr - 1;
381 overflow = (offset < INT_MIN || offset >= 0);
382 }
383 if (overflow) {
384 fprintf(stderr, "kallsyms failure: "
385 "%s symbol value %#llx out of range in relative mode\n",
386 symbol_absolute(&table[i]) ? "absolute" : "relative",
387 table[i].addr);
388 exit(EXIT_FAILURE);
389 }
390 printf("\t.long\t%#x\n", (int)offset);
391 } else if (!symbol_absolute(&table[i])) {
Vivek Goyal2c22d8b2006-12-07 02:14:10 +0100392 if (_text <= table[i].addr)
393 printf("\tPTR\t_text + %#llx\n",
394 table[i].addr - _text);
395 else
Andrew Morton2930ffc2014-03-10 15:49:48 -0700396 printf("\tPTR\t_text - %#llx\n",
397 _text - table[i].addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100398 } else {
399 printf("\tPTR\t%#llx\n", table[i].addr);
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 printf("\n");
403
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700404 if (base_relative) {
405 output_label("kallsyms_relative_base");
406 printf("\tPTR\t_text - %#llx\n", _text - relative_base);
407 printf("\n");
408 }
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 output_label("kallsyms_num_syms");
nixiaomingac5db1f2018-05-24 11:16:12 +0800411 printf("\tPTR\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 printf("\n");
413
414 /* table of offset markers, that give the offset in the compressed stream
415 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800416 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
417 if (!markers) {
418 fprintf(stderr, "kallsyms failure: "
419 "unable to allocate required memory\n");
420 exit(EXIT_FAILURE);
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700425 for (i = 0; i < table_cnt; i++) {
426 if ((i & 0xFF) == 0)
427 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 printf("\t.byte 0x%02x", table[i].len);
430 for (k = 0; k < table[i].len; k++)
431 printf(", 0x%02x", table[i].sym[k]);
432 printf("\n");
433
434 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 printf("\n");
437
438 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700439 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 printf("\tPTR\t%d\n", markers[i]);
441 printf("\n");
442
443 free(markers);
444
445 output_label("kallsyms_token_table");
446 off = 0;
447 for (i = 0; i < 256; i++) {
448 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700449 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 printf("\t.asciz\t\"%s\"\n", buf);
451 off += strlen(buf) + 1;
452 }
453 printf("\n");
454
455 output_label("kallsyms_token_index");
456 for (i = 0; i < 256; i++)
457 printf("\t.short\t%d\n", best_idx[i]);
458 printf("\n");
459}
460
461
462/* table lookup compression functions */
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* count all the possible tokens in a symbol */
465static void learn_symbol(unsigned char *symbol, int len)
466{
467 int i;
468
469 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700470 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
473/* decrease the count for all the possible tokens in a symbol */
474static void forget_symbol(unsigned char *symbol, int len)
475{
476 int i;
477
478 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700479 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700482/* remove all the invalid symbols from the table and do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static void build_initial_tok_table(void)
484{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700485 unsigned int i, pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700487 pos = 0;
488 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if ( symbol_valid(&table[i]) ) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700490 if (pos != i)
491 table[pos] = table[i];
492 learn_symbol(table[pos].sym, table[pos].len);
493 pos++;
Masahiro Yamadae9bcb922019-11-24 01:04:30 +0900494 } else {
495 free(table[i].sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700498 table_cnt = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Paulo Marques7c5d2492007-06-20 18:09:00 +0100501static void *find_token(unsigned char *str, int len, unsigned char *token)
502{
503 int i;
504
505 for (i = 0; i < len - 1; i++) {
506 if (str[i] == token[0] && str[i+1] == token[1])
507 return &str[i];
508 }
509 return NULL;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/* replace a given token in all the valid symbols. Use the sampled symbols
513 * to update the counts */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700514static void compress_symbols(unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700516 unsigned int i, len, size;
517 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700519 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700522 p1 = table[i].sym;
523
524 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100525 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700526 if (!p2) continue;
527
528 /* decrease the counts for this symbol's tokens */
529 forget_symbol(table[i].sym, len);
530
531 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700534 *p2 = idx;
535 p2++;
536 size -= (p2 - p1);
537 memmove(p2, p2 + 1, size);
538 p1 = p2;
539 len--;
540
541 if (size < 2) break;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100544 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700546 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700548 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700550 /* increase the counts for this symbol's new tokens */
551 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553}
554
555/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700556static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700558 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700561 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700563 for (i = 0; i < 0x10000; i++) {
564 if (token_profit[i] > bestprofit) {
565 best = i;
566 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return best;
570}
571
572/* this is the core of the algorithm: calculate the "best" table */
573static void optimize_result(void)
574{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700575 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* using the '\0' symbol last allows compress_symbols to use standard
578 * fast string functions */
579 for (i = 255; i >= 0; i--) {
580
581 /* if this table slot is empty (it is not used by an actual
582 * original char code */
583 if (!best_table_len[i]) {
584
Cao jincbf7a902018-02-27 16:16:19 +0800585 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800587 if (token_profit[best] == 0)
588 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700591 best_table_len[i] = 2;
592 best_table[i][0] = best & 0xFF;
593 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700596 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598 }
599}
600
601/* start by placing the symbols that are actually used on the table */
602static void insert_real_symbols_in_table(void)
603{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700604 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 memset(best_table, 0, sizeof(best_table));
607 memset(best_table_len, 0, sizeof(best_table_len));
608
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700609 for (i = 0; i < table_cnt; i++) {
610 for (j = 0; j < table[i].len; j++) {
611 c = table[i].sym[j];
612 best_table[c][0]=c;
613 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 }
616}
617
618static void optimize_token_table(void)
619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 build_initial_tok_table();
621
622 insert_real_symbols_in_table();
623
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700624 /* When valid symbol is not registered, exit to error */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700625 if (!table_cnt) {
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700626 fprintf(stderr, "No valid symbol.\n");
627 exit(1);
628 }
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 optimize_result();
631}
632
Lai Jiangshanb478b7822009-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{
636 const char *symbol = (char *)se->sym + 1;
637 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
668static int prefix_underscores_count(const char *str)
669{
670 const char *tail = str;
671
Paul Mundta9ece532009-09-22 16:44:12 -0700672 while (*tail == '_')
Lai Jiangshanb478b7822009-03-13 15:10:26 +0800673 tail++;
674
675 return tail - str;
676}
677
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800678static int compare_symbols(const void *a, const void *b)
679{
680 const struct sym_entry *sa;
681 const struct sym_entry *sb;
682 int wa, wb;
683
684 sa = a;
685 sb = b;
686
687 /* sort by address first */
688 if (sa->addr > sb->addr)
689 return 1;
690 if (sa->addr < sb->addr)
691 return -1;
692
693 /* sort by "weakness" type */
694 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
695 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
696 if (wa != wb)
697 return wa - wb;
698
Lai Jiangshanb478b7822009-03-13 15:10:26 +0800699 /* sort by "linker script provide" type */
700 wa = may_be_linker_script_provide_symbol(sa);
701 wb = may_be_linker_script_provide_symbol(sb);
702 if (wa != wb)
703 return wa - wb;
704
705 /* sort by the number of prefix underscores */
706 wa = prefix_underscores_count((const char *)sa->sym + 1);
707 wb = prefix_underscores_count((const char *)sb->sym + 1);
708 if (wa != wb)
709 return wa - wb;
710
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800711 /* sort by initial order, so that other symbols are left undisturbed */
712 return sa->start_pos - sb->start_pos;
713}
714
715static void sort_symbols(void)
716{
717 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
718}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030720static void make_percpus_absolute(void)
721{
722 unsigned int i;
723
724 for (i = 0; i < table_cnt; i++)
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700725 if (symbol_in_range(&table[i], &percpu_range, 1)) {
726 /*
727 * Keep the 'A' override for percpu symbols to
728 * ensure consistent behavior compared to older
729 * versions of this tool.
730 */
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030731 table[i].sym[0] = 'A';
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700732 table[i].percpu_absolute = 1;
733 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030734}
735
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700736/* find the minimum non-absolute symbol address */
737static void record_relative_base(void)
738{
739 unsigned int i;
740
741 relative_base = -1ULL;
742 for (i = 0; i < table_cnt; i++)
743 if (!symbol_absolute(&table[i]) &&
744 table[i].addr < relative_base)
745 relative_base = table[i].addr;
746}
747
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700748int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700750 if (argc >= 2) {
751 int i;
752 for (i = 1; i < argc; i++) {
753 if(strcmp(argv[i], "--all-symbols") == 0)
754 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030755 else if (strcmp(argv[i], "--absolute-percpu") == 0)
756 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900757 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700758 base_relative = 1;
759 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700760 usage();
761 }
762 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 usage();
764
765 read_map(stdin);
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030766 if (absolute_percpu)
767 make_percpus_absolute();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700768 if (base_relative)
769 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100770 sort_symbols();
771 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 write_src();
773
774 return 0;
775}