blob: 54ad86d13784995cfb8eb57bd0827204c6c36886 [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;
Ard Biesheuvel8c996942016-03-15 14:58:15 -070036 unsigned int percpu_absolute;
Linus Torvalds9d829732020-05-04 09:16:37 -070037 unsigned char sym[];
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
Masahiro Yamada8d605262020-02-02 14:09:21 +090058static struct sym_entry **table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070059static unsigned int table_size, table_cnt;
Masahiro Yamada831362f2019-11-24 01:04:44 +090060static int all_symbols;
61static int absolute_percpu;
62static int base_relative;
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{
Masahiro Yamada516d9802020-09-23 02:48:56 +090085 /* Symbol names that exactly match to the following are ignored.*/
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090086 static const char * const ignored_symbols[] = {
87 /*
88 * Symbols which vary between passes. Passes 1 and 2 must have
89 * identical symbol lists. The kallsyms_* symbols below are
90 * only added after pass 1, they would be included in pass 2
91 * when --all-symbols is specified so exclude them to get a
92 * stable symbol list.
93 */
94 "kallsyms_addresses",
95 "kallsyms_offsets",
96 "kallsyms_relative_base",
97 "kallsyms_num_syms",
98 "kallsyms_names",
99 "kallsyms_markers",
100 "kallsyms_token_table",
101 "kallsyms_token_index",
102 /* Exclude linker generated symbols which vary between passes */
103 "_SDA_BASE_", /* ppc */
104 "_SDA2_BASE_", /* ppc */
105 NULL
106 };
107
Masahiro Yamada516d9802020-09-23 02:48:56 +0900108 /* Symbol names that begin with the following are ignored.*/
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900109 static const char * const ignored_prefixes[] = {
Masahiro Yamada97261e12019-11-24 01:04:40 +0900110 "$", /* local symbols for ARM, MIPS, etc. */
111 ".LASANPC", /* s390 kasan local symbols */
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900112 "__crc_", /* modversions */
113 "__efistub_", /* arm64 EFI stub namespace */
David Brazdil76217122020-06-25 14:14:08 +0100114 "__kvm_nvhe_", /* arm64 non-VHE KVM namespace */
Arnd Bergmann3dc2ba42021-02-04 16:29:47 +0100115 "__AArch64ADRPThunk_", /* arm64 lld */
116 "__ARMV5PILongThunk_", /* arm lld */
117 "__ARMV7PILongThunk_",
118 "__ThumbV7PILongThunk_",
119 "__LA25Thunk_", /* mips lld */
120 "__microLA25Thunk_",
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900121 NULL
122 };
123
Masahiro Yamada516d9802020-09-23 02:48:56 +0900124 /* Symbol names that end with the following are ignored.*/
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900125 static const char * const ignored_suffixes[] = {
126 "_from_arm", /* arm */
127 "_from_thumb", /* arm */
128 "_veneer", /* arm */
129 NULL
130 };
131
Masahiro Yamada516d9802020-09-23 02:48:56 +0900132 /* Symbol names that contain the following are ignored.*/
133 static const char * const ignored_matches[] = {
134 ".long_branch.", /* ppc stub */
135 ".plt_branch.", /* ppc stub */
136 NULL
137 };
138
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900139 const char * const *p;
140
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900141 for (p = ignored_symbols; *p; p++)
142 if (!strcmp(name, *p))
143 return true;
144
145 for (p = ignored_prefixes; *p; p++)
146 if (!strncmp(name, *p, strlen(*p)))
147 return true;
148
149 for (p = ignored_suffixes; *p; p++) {
150 int l = strlen(name) - strlen(*p);
151
152 if (l >= 0 && !strcmp(name + l, *p))
153 return true;
154 }
155
Masahiro Yamada516d9802020-09-23 02:48:56 +0900156 for (p = ignored_matches; *p; p++) {
157 if (strstr(name, *p))
158 return true;
159 }
160
Masahiro Yamada887df762019-11-24 01:04:41 +0900161 if (type == 'U' || type == 'u')
162 return true;
163 /* exclude debugging symbols */
164 if (type == 'N' || type == 'n')
165 return true;
166
167 if (toupper(type) == 'A') {
168 /* Keep these useful absolute symbols */
169 if (strcmp(name, "__kernel_syscall_via_break") &&
170 strcmp(name, "__kernel_syscall_via_epc") &&
171 strcmp(name, "__kernel_sigtramp") &&
172 strcmp(name, "__gp"))
173 return true;
174 }
175
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900176 return false;
177}
178
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900179static void check_symbol_range(const char *sym, unsigned long long addr,
180 struct addr_range *ranges, 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 (strcmp(sym, ar->start_sym) == 0) {
189 ar->start = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900190 return;
Kees Cook78eb7152014-03-17 13:18:27 +1030191 } else if (strcmp(sym, ar->end_sym) == 0) {
192 ar->end = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900193 return;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400194 }
195 }
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400196}
197
Masahiro Yamada8d605262020-02-02 14:09:21 +0900198static struct sym_entry *read_symbol(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900200 char name[500], type;
Masahiro Yamada8d605262020-02-02 14:09:21 +0900201 unsigned long long addr;
202 unsigned int len;
203 struct sym_entry *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 int rc;
205
Masahiro Yamada8d605262020-02-02 14:09:21 +0900206 rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (rc != 3) {
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900208 if (rc != EOF && fgets(name, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600209 fprintf(stderr, "Read error or end of file.\n");
Masahiro Yamada8d605262020-02-02 14:09:21 +0900210 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900212 if (strlen(name) >= KSYM_NAME_LEN) {
Eugene Loh6db29832019-01-17 14:46:00 -0800213 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900214 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900215 name, strlen(name), KSYM_NAME_LEN);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900216 return NULL;
Andi Kleenf3462aa2013-10-23 15:07:53 +0200217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900219 if (strcmp(name, "_text") == 0)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900220 _text = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900221
Mikhail Petrov7883a142020-03-11 23:37:09 +0300222 /* Ignore most absolute/undefined (?) symbols. */
223 if (is_ignored_symbol(name, type))
224 return NULL;
225
Masahiro Yamada8d605262020-02-02 14:09:21 +0900226 check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
227 check_symbol_range(name, addr, &percpu_range, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* include the type field in the symbol name, so that it gets
230 * compressed together */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900231
232 len = strlen(name) + 1;
233
Masahiro Yamada9d1b3892020-02-11 01:18:52 +0900234 sym = malloc(sizeof(*sym) + len + 1);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900235 if (!sym) {
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800236 fprintf(stderr, "kallsyms failure: "
237 "unable to allocate required amount of memory\n");
238 exit(EXIT_FAILURE);
239 }
Masahiro Yamada8d605262020-02-02 14:09:21 +0900240 sym->addr = addr;
241 sym->len = len;
242 sym->sym[0] = type;
Masahiro Yamada9d1b3892020-02-11 01:18:52 +0900243 strcpy(sym_name(sym), name);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900244 sym->percpu_absolute = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Masahiro Yamada8d605262020-02-02 14:09:21 +0900246 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900249static int symbol_in_range(const struct sym_entry *s,
250 const struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400251{
252 size_t i;
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900253 const struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400254
Kees Cook78eb7152014-03-17 13:18:27 +1030255 for (i = 0; i < entries; ++i) {
256 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400257
Kees Cook78eb7152014-03-17 13:18:27 +1030258 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400259 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400260 }
261
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400262 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400263}
264
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900265static int symbol_valid(const struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900267 const char *name = sym_name(s);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* if --all-symbols is not specified, then symbols outside the text
270 * and inittext sections are discarded */
271 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030272 if (symbol_in_range(s, text_ranges,
273 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
275 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800276 * _etext _einittext; they can move between pass 1 and 2 when
277 * the kallsyms data are added. If these symbols move then
278 * they may get dropped in pass 2, which breaks the kallsyms
279 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400281 if ((s->addr == text_range_text->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900282 strcmp(name, text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400283 (s->addr == text_range_inittext->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900284 strcmp(name, text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286 }
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 1;
289}
290
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900291/* remove all the invalid symbols from the table */
292static void shrink_table(void)
293{
294 unsigned int i, pos;
295
296 pos = 0;
297 for (i = 0; i < table_cnt; i++) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900298 if (symbol_valid(table[i])) {
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900299 if (pos != i)
300 table[pos] = table[i];
301 pos++;
302 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900303 free(table[i]);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900304 }
305 }
306 table_cnt = pos;
307
308 /* When valid symbol is not registered, exit to error */
309 if (!table_cnt) {
310 fprintf(stderr, "No valid symbol.\n");
311 exit(1);
312 }
313}
314
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700315static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900317 struct sym_entry *sym;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 while (!feof(in)) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900320 sym = read_symbol(in);
321 if (!sym)
322 continue;
323
324 sym->start_pos = table_cnt;
325
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700326 if (table_cnt >= table_size) {
327 table_size += 10000;
328 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!table) {
330 fprintf(stderr, "out of memory\n");
331 exit (1);
332 }
333 }
Masahiro Yamada8d605262020-02-02 14:09:21 +0900334
335 table[table_cnt++] = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337}
338
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900339static void output_label(const char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900341 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 printf("\tALGN\n");
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900343 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900346/* Provide proper symbols relocatability by their '_text' relativeness. */
347static void output_address(unsigned long long addr)
348{
349 if (_text <= addr)
350 printf("\tPTR\t_text + %#llx\n", addr - _text);
351 else
352 printf("\tPTR\t_text - %#llx\n", _text - addr);
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/* uncompress a compressed symbol. When this function is called, the best table
356 * might still be compressed itself, so the function needs to be recursive */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900357static int expand_symbol(const unsigned char *data, int len, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 int c, rlen, total=0;
360
361 while (len) {
362 c = *data;
363 /* if the table holds a single char that is the same as the one
364 * we are looking for, then end the search */
365 if (best_table[c][0]==c && best_table_len[c]==1) {
366 *result++ = c;
367 total++;
368 } else {
369 /* if not, recurse and expand */
370 rlen = expand_symbol(best_table[c], best_table_len[c], result);
371 total += rlen;
372 result += rlen;
373 }
374 data++;
375 len--;
376 }
377 *result=0;
378
379 return total;
380}
381
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900382static int symbol_absolute(const struct sym_entry *s)
Kees Cook78eb7152014-03-17 13:18:27 +1030383{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700384 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030385}
386
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700387static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700389 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned int best_idx[256];
391 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700392 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Masahiro Yamada500193e2019-02-04 10:53:18 +0900394 printf("#include <asm/bitsperlong.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 printf("#if BITS_PER_LONG == 64\n");
396 printf("#define PTR .quad\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100397 printf("#define ALGN .balign 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 printf("#else\n");
399 printf("#define PTR .long\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100400 printf("#define ALGN .balign 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 printf("#endif\n");
402
Jan Beulichaad09472006-12-08 02:35:57 -0800403 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700405 if (!base_relative)
406 output_label("kallsyms_addresses");
407 else
408 output_label("kallsyms_offsets");
409
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700410 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700411 if (base_relative) {
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900412 /*
413 * Use the offset relative to the lowest value
414 * encountered of all relative symbols, and emit
415 * non-relocatable fixed offsets that will be fixed
416 * up at runtime.
417 */
418
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700419 long long offset;
420 int overflow;
421
422 if (!absolute_percpu) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900423 offset = table[i]->addr - relative_base;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700424 overflow = (offset < 0 || offset > UINT_MAX);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900425 } else if (symbol_absolute(table[i])) {
426 offset = table[i]->addr;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700427 overflow = (offset < 0 || offset > INT_MAX);
428 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900429 offset = relative_base - table[i]->addr - 1;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700430 overflow = (offset < INT_MIN || offset >= 0);
431 }
432 if (overflow) {
433 fprintf(stderr, "kallsyms failure: "
434 "%s symbol value %#llx out of range in relative mode\n",
Masahiro Yamada8d605262020-02-02 14:09:21 +0900435 symbol_absolute(table[i]) ? "absolute" : "relative",
436 table[i]->addr);
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700437 exit(EXIT_FAILURE);
438 }
439 printf("\t.long\t%#x\n", (int)offset);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900440 } else if (!symbol_absolute(table[i])) {
441 output_address(table[i]->addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100442 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900443 printf("\tPTR\t%#llx\n", table[i]->addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446 printf("\n");
447
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700448 if (base_relative) {
449 output_label("kallsyms_relative_base");
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900450 output_address(relative_base);
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700451 printf("\n");
452 }
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 output_label("kallsyms_num_syms");
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600455 printf("\t.long\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printf("\n");
457
458 /* table of offset markers, that give the offset in the compressed stream
459 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800460 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
461 if (!markers) {
462 fprintf(stderr, "kallsyms failure: "
463 "unable to allocate required memory\n");
464 exit(EXIT_FAILURE);
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700469 for (i = 0; i < table_cnt; i++) {
470 if ((i & 0xFF) == 0)
471 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Masahiro Yamada8d605262020-02-02 14:09:21 +0900473 printf("\t.byte 0x%02x", table[i]->len);
474 for (k = 0; k < table[i]->len; k++)
475 printf(", 0x%02x", table[i]->sym[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 printf("\n");
477
Masahiro Yamada8d605262020-02-02 14:09:21 +0900478 off += table[i]->len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 printf("\n");
481
482 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700483 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600484 printf("\t.long\t%u\n", markers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 printf("\n");
486
487 free(markers);
488
489 output_label("kallsyms_token_table");
490 off = 0;
491 for (i = 0; i < 256; i++) {
492 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700493 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 printf("\t.asciz\t\"%s\"\n", buf);
495 off += strlen(buf) + 1;
496 }
497 printf("\n");
498
499 output_label("kallsyms_token_index");
500 for (i = 0; i < 256; i++)
501 printf("\t.short\t%d\n", best_idx[i]);
502 printf("\n");
503}
504
505
506/* table lookup compression functions */
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/* count all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900509static void learn_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int i;
512
513 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700514 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
517/* decrease the count for all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900518static void forget_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int i;
521
522 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700523 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900526/* do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static void build_initial_tok_table(void)
528{
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900529 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900531 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900532 learn_symbol(table[i]->sym, table[i]->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Masahiro Yamada2558c132019-11-24 01:04:37 +0900535static unsigned char *find_token(unsigned char *str, int len,
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900536 const unsigned char *token)
Paulo Marques7c5d2492007-06-20 18:09:00 +0100537{
538 int i;
539
540 for (i = 0; i < len - 1; i++) {
541 if (str[i] == token[0] && str[i+1] == token[1])
542 return &str[i];
543 }
544 return NULL;
545}
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547/* replace a given token in all the valid symbols. Use the sampled symbols
548 * to update the counts */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900549static void compress_symbols(const unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700551 unsigned int i, len, size;
552 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700554 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Masahiro Yamada8d605262020-02-02 14:09:21 +0900556 len = table[i]->len;
557 p1 = table[i]->sym;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700558
559 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100560 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700561 if (!p2) continue;
562
563 /* decrease the counts for this symbol's tokens */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900564 forget_symbol(table[i]->sym, len);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700565
566 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700569 *p2 = idx;
570 p2++;
571 size -= (p2 - p1);
572 memmove(p2, p2 + 1, size);
573 p1 = p2;
574 len--;
575
576 if (size < 2) break;
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100579 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700581 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Masahiro Yamada8d605262020-02-02 14:09:21 +0900583 table[i]->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700585 /* increase the counts for this symbol's new tokens */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900586 learn_symbol(table[i]->sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588}
589
590/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700591static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700593 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700596 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700598 for (i = 0; i < 0x10000; i++) {
599 if (token_profit[i] > bestprofit) {
600 best = i;
601 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return best;
605}
606
607/* this is the core of the algorithm: calculate the "best" table */
608static void optimize_result(void)
609{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700610 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* using the '\0' symbol last allows compress_symbols to use standard
613 * fast string functions */
614 for (i = 255; i >= 0; i--) {
615
616 /* if this table slot is empty (it is not used by an actual
617 * original char code */
618 if (!best_table_len[i]) {
619
Cao jincbf7a902018-02-27 16:16:19 +0800620 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800622 if (token_profit[best] == 0)
623 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700626 best_table_len[i] = 2;
627 best_table[i][0] = best & 0xFF;
628 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700631 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
634}
635
636/* start by placing the symbols that are actually used on the table */
637static void insert_real_symbols_in_table(void)
638{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700639 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700641 for (i = 0; i < table_cnt; i++) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900642 for (j = 0; j < table[i]->len; j++) {
643 c = table[i]->sym[j];
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700644 best_table[c][0]=c;
645 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 }
648}
649
650static void optimize_token_table(void)
651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 build_initial_tok_table();
653
654 insert_real_symbols_in_table();
655
656 optimize_result();
657}
658
Lai Jiangshanb478b782009-03-13 15:10:26 +0800659/* guess for "linker script provide" symbol */
660static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
661{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900662 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800663 int len = se->len - 1;
664
665 if (len < 8)
666 return 0;
667
668 if (symbol[0] != '_' || symbol[1] != '_')
669 return 0;
670
671 /* __start_XXXXX */
672 if (!memcmp(symbol + 2, "start_", 6))
673 return 1;
674
675 /* __stop_XXXXX */
676 if (!memcmp(symbol + 2, "stop_", 5))
677 return 1;
678
679 /* __end_XXXXX */
680 if (!memcmp(symbol + 2, "end_", 4))
681 return 1;
682
683 /* __XXXXX_start */
684 if (!memcmp(symbol + len - 6, "_start", 6))
685 return 1;
686
687 /* __XXXXX_end */
688 if (!memcmp(symbol + len - 4, "_end", 4))
689 return 1;
690
691 return 0;
692}
693
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800694static int compare_symbols(const void *a, const void *b)
695{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900696 const struct sym_entry *sa = *(const struct sym_entry **)a;
697 const struct sym_entry *sb = *(const struct sym_entry **)b;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800698 int wa, wb;
699
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800700 /* sort by address first */
701 if (sa->addr > sb->addr)
702 return 1;
703 if (sa->addr < sb->addr)
704 return -1;
705
706 /* sort by "weakness" type */
707 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
708 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
709 if (wa != wb)
710 return wa - wb;
711
Lai Jiangshanb478b782009-03-13 15:10:26 +0800712 /* sort by "linker script provide" type */
713 wa = may_be_linker_script_provide_symbol(sa);
714 wb = may_be_linker_script_provide_symbol(sb);
715 if (wa != wb)
716 return wa - wb;
717
718 /* sort by the number of prefix underscores */
Masahiro Yamadaaa915242019-11-24 01:04:36 +0900719 wa = strspn(sym_name(sa), "_");
720 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800721 if (wa != wb)
722 return wa - wb;
723
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800724 /* sort by initial order, so that other symbols are left undisturbed */
725 return sa->start_pos - sb->start_pos;
726}
727
728static void sort_symbols(void)
729{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900730 qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800731}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030733static void make_percpus_absolute(void)
734{
735 unsigned int i;
736
737 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900738 if (symbol_in_range(table[i], &percpu_range, 1)) {
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700739 /*
740 * Keep the 'A' override for percpu symbols to
741 * ensure consistent behavior compared to older
742 * versions of this tool.
743 */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900744 table[i]->sym[0] = 'A';
745 table[i]->percpu_absolute = 1;
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700746 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030747}
748
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700749/* find the minimum non-absolute symbol address */
750static void record_relative_base(void)
751{
752 unsigned int i;
753
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700754 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900755 if (!symbol_absolute(table[i])) {
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900756 /*
757 * The table is sorted by address.
758 * Take the first non-absolute symbol value.
759 */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900760 relative_base = table[i]->addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900761 return;
762 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700763}
764
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700765int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700767 if (argc >= 2) {
768 int i;
769 for (i = 1; i < argc; i++) {
770 if(strcmp(argv[i], "--all-symbols") == 0)
771 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030772 else if (strcmp(argv[i], "--absolute-percpu") == 0)
773 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900774 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700775 base_relative = 1;
776 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700777 usage();
778 }
779 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 usage();
781
782 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900783 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030784 if (absolute_percpu)
785 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900786 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700787 if (base_relative)
788 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100789 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 write_src();
791
792 return 0;
793}