blob: 0096cd9653327584fe62ce56ba158c68875c5067 [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{
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 */
David Brazdil76217122020-06-25 14:14:08 +0100112 "__kvm_nvhe_", /* arm64 non-VHE KVM namespace */
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900113 NULL
114 };
115
116 static const char * const ignored_suffixes[] = {
117 "_from_arm", /* arm */
118 "_from_thumb", /* arm */
119 "_veneer", /* arm */
120 NULL
121 };
122
123 const char * const *p;
124
125 /* Exclude symbols which vary between passes. */
126 for (p = ignored_symbols; *p; p++)
127 if (!strcmp(name, *p))
128 return true;
129
130 for (p = ignored_prefixes; *p; p++)
131 if (!strncmp(name, *p, strlen(*p)))
132 return true;
133
134 for (p = ignored_suffixes; *p; p++) {
135 int l = strlen(name) - strlen(*p);
136
137 if (l >= 0 && !strcmp(name + l, *p))
138 return true;
139 }
140
Masahiro Yamada887df762019-11-24 01:04:41 +0900141 if (type == 'U' || type == 'u')
142 return true;
143 /* exclude debugging symbols */
144 if (type == 'N' || type == 'n')
145 return true;
146
147 if (toupper(type) == 'A') {
148 /* Keep these useful absolute symbols */
149 if (strcmp(name, "__kernel_syscall_via_break") &&
150 strcmp(name, "__kernel_syscall_via_epc") &&
151 strcmp(name, "__kernel_sigtramp") &&
152 strcmp(name, "__gp"))
153 return true;
154 }
155
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900156 return false;
157}
158
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900159static void check_symbol_range(const char *sym, unsigned long long addr,
160 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400161{
162 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030163 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400164
Kees Cook78eb7152014-03-17 13:18:27 +1030165 for (i = 0; i < entries; ++i) {
166 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400167
Kees Cook78eb7152014-03-17 13:18:27 +1030168 if (strcmp(sym, ar->start_sym) == 0) {
169 ar->start = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900170 return;
Kees Cook78eb7152014-03-17 13:18:27 +1030171 } else if (strcmp(sym, ar->end_sym) == 0) {
172 ar->end = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900173 return;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400174 }
175 }
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400176}
177
Masahiro Yamada8d605262020-02-02 14:09:21 +0900178static struct sym_entry *read_symbol(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900180 char name[500], type;
Masahiro Yamada8d605262020-02-02 14:09:21 +0900181 unsigned long long addr;
182 unsigned int len;
183 struct sym_entry *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int rc;
185
Masahiro Yamada8d605262020-02-02 14:09:21 +0900186 rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (rc != 3) {
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900188 if (rc != EOF && fgets(name, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600189 fprintf(stderr, "Read error or end of file.\n");
Masahiro Yamada8d605262020-02-02 14:09:21 +0900190 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900192 if (strlen(name) >= KSYM_NAME_LEN) {
Eugene Loh6db29832019-01-17 14:46:00 -0800193 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900194 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900195 name, strlen(name), KSYM_NAME_LEN);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900196 return NULL;
Andi Kleenf3462aa2013-10-23 15:07:53 +0200197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900199 if (strcmp(name, "_text") == 0)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900200 _text = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900201
Mikhail Petrov7883a142020-03-11 23:37:09 +0300202 /* Ignore most absolute/undefined (?) symbols. */
203 if (is_ignored_symbol(name, type))
204 return NULL;
205
Masahiro Yamada8d605262020-02-02 14:09:21 +0900206 check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
207 check_symbol_range(name, addr, &percpu_range, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* include the type field in the symbol name, so that it gets
210 * compressed together */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900211
212 len = strlen(name) + 1;
213
Masahiro Yamada9d1b3892020-02-11 01:18:52 +0900214 sym = malloc(sizeof(*sym) + len + 1);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900215 if (!sym) {
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800216 fprintf(stderr, "kallsyms failure: "
217 "unable to allocate required amount of memory\n");
218 exit(EXIT_FAILURE);
219 }
Masahiro Yamada8d605262020-02-02 14:09:21 +0900220 sym->addr = addr;
221 sym->len = len;
222 sym->sym[0] = type;
Masahiro Yamada9d1b3892020-02-11 01:18:52 +0900223 strcpy(sym_name(sym), name);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900224 sym->percpu_absolute = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Masahiro Yamada8d605262020-02-02 14:09:21 +0900226 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900229static int symbol_in_range(const struct sym_entry *s,
230 const struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400231{
232 size_t i;
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900233 const struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400234
Kees Cook78eb7152014-03-17 13:18:27 +1030235 for (i = 0; i < entries; ++i) {
236 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400237
Kees Cook78eb7152014-03-17 13:18:27 +1030238 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400239 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400240 }
241
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400242 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400243}
244
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900245static int symbol_valid(const struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900247 const char *name = sym_name(s);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* if --all-symbols is not specified, then symbols outside the text
250 * and inittext sections are discarded */
251 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030252 if (symbol_in_range(s, text_ranges,
253 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800256 * _etext _einittext; they can move between pass 1 and 2 when
257 * the kallsyms data are added. If these symbols move then
258 * they may get dropped in pass 2, which breaks the kallsyms
259 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400261 if ((s->addr == text_range_text->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900262 strcmp(name, text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400263 (s->addr == text_range_inittext->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900264 strcmp(name, text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return 0;
266 }
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 1;
269}
270
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900271/* remove all the invalid symbols from the table */
272static void shrink_table(void)
273{
274 unsigned int i, pos;
275
276 pos = 0;
277 for (i = 0; i < table_cnt; i++) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900278 if (symbol_valid(table[i])) {
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900279 if (pos != i)
280 table[pos] = table[i];
281 pos++;
282 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900283 free(table[i]);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900284 }
285 }
286 table_cnt = pos;
287
288 /* When valid symbol is not registered, exit to error */
289 if (!table_cnt) {
290 fprintf(stderr, "No valid symbol.\n");
291 exit(1);
292 }
293}
294
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700295static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900297 struct sym_entry *sym;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 while (!feof(in)) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900300 sym = read_symbol(in);
301 if (!sym)
302 continue;
303
304 sym->start_pos = table_cnt;
305
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700306 if (table_cnt >= table_size) {
307 table_size += 10000;
308 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!table) {
310 fprintf(stderr, "out of memory\n");
311 exit (1);
312 }
313 }
Masahiro Yamada8d605262020-02-02 14:09:21 +0900314
315 table[table_cnt++] = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317}
318
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900319static void output_label(const char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900321 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 printf("\tALGN\n");
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900323 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900326/* Provide proper symbols relocatability by their '_text' relativeness. */
327static void output_address(unsigned long long addr)
328{
329 if (_text <= addr)
330 printf("\tPTR\t_text + %#llx\n", addr - _text);
331 else
332 printf("\tPTR\t_text - %#llx\n", _text - addr);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/* uncompress a compressed symbol. When this function is called, the best table
336 * might still be compressed itself, so the function needs to be recursive */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900337static int expand_symbol(const unsigned char *data, int len, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 int c, rlen, total=0;
340
341 while (len) {
342 c = *data;
343 /* if the table holds a single char that is the same as the one
344 * we are looking for, then end the search */
345 if (best_table[c][0]==c && best_table_len[c]==1) {
346 *result++ = c;
347 total++;
348 } else {
349 /* if not, recurse and expand */
350 rlen = expand_symbol(best_table[c], best_table_len[c], result);
351 total += rlen;
352 result += rlen;
353 }
354 data++;
355 len--;
356 }
357 *result=0;
358
359 return total;
360}
361
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900362static int symbol_absolute(const struct sym_entry *s)
Kees Cook78eb7152014-03-17 13:18:27 +1030363{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700364 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030365}
366
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700367static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700369 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int best_idx[256];
371 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700372 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Masahiro Yamada500193e2019-02-04 10:53:18 +0900374 printf("#include <asm/bitsperlong.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 printf("#if BITS_PER_LONG == 64\n");
376 printf("#define PTR .quad\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100377 printf("#define ALGN .balign 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 printf("#else\n");
379 printf("#define PTR .long\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100380 printf("#define ALGN .balign 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 printf("#endif\n");
382
Jan Beulichaad09472006-12-08 02:35:57 -0800383 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700385 if (!base_relative)
386 output_label("kallsyms_addresses");
387 else
388 output_label("kallsyms_offsets");
389
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700390 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700391 if (base_relative) {
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900392 /*
393 * Use the offset relative to the lowest value
394 * encountered of all relative symbols, and emit
395 * non-relocatable fixed offsets that will be fixed
396 * up at runtime.
397 */
398
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700399 long long offset;
400 int overflow;
401
402 if (!absolute_percpu) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900403 offset = table[i]->addr - relative_base;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700404 overflow = (offset < 0 || offset > UINT_MAX);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900405 } else if (symbol_absolute(table[i])) {
406 offset = table[i]->addr;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700407 overflow = (offset < 0 || offset > INT_MAX);
408 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900409 offset = relative_base - table[i]->addr - 1;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700410 overflow = (offset < INT_MIN || offset >= 0);
411 }
412 if (overflow) {
413 fprintf(stderr, "kallsyms failure: "
414 "%s symbol value %#llx out of range in relative mode\n",
Masahiro Yamada8d605262020-02-02 14:09:21 +0900415 symbol_absolute(table[i]) ? "absolute" : "relative",
416 table[i]->addr);
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700417 exit(EXIT_FAILURE);
418 }
419 printf("\t.long\t%#x\n", (int)offset);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900420 } else if (!symbol_absolute(table[i])) {
421 output_address(table[i]->addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100422 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900423 printf("\tPTR\t%#llx\n", table[i]->addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 printf("\n");
427
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700428 if (base_relative) {
429 output_label("kallsyms_relative_base");
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900430 output_address(relative_base);
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700431 printf("\n");
432 }
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 output_label("kallsyms_num_syms");
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600435 printf("\t.long\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 printf("\n");
437
438 /* table of offset markers, that give the offset in the compressed stream
439 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800440 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
441 if (!markers) {
442 fprintf(stderr, "kallsyms failure: "
443 "unable to allocate required memory\n");
444 exit(EXIT_FAILURE);
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700449 for (i = 0; i < table_cnt; i++) {
450 if ((i & 0xFF) == 0)
451 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Masahiro Yamada8d605262020-02-02 14:09:21 +0900453 printf("\t.byte 0x%02x", table[i]->len);
454 for (k = 0; k < table[i]->len; k++)
455 printf(", 0x%02x", table[i]->sym[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printf("\n");
457
Masahiro Yamada8d605262020-02-02 14:09:21 +0900458 off += table[i]->len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 printf("\n");
461
462 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700463 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600464 printf("\t.long\t%u\n", markers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 printf("\n");
466
467 free(markers);
468
469 output_label("kallsyms_token_table");
470 off = 0;
471 for (i = 0; i < 256; i++) {
472 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700473 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 printf("\t.asciz\t\"%s\"\n", buf);
475 off += strlen(buf) + 1;
476 }
477 printf("\n");
478
479 output_label("kallsyms_token_index");
480 for (i = 0; i < 256; i++)
481 printf("\t.short\t%d\n", best_idx[i]);
482 printf("\n");
483}
484
485
486/* table lookup compression functions */
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/* count all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900489static void learn_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 int i;
492
493 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700494 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/* decrease the count for all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900498static void forget_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 int i;
501
502 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700503 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900506/* do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static void build_initial_tok_table(void)
508{
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900509 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900511 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900512 learn_symbol(table[i]->sym, table[i]->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Masahiro Yamada2558c132019-11-24 01:04:37 +0900515static unsigned char *find_token(unsigned char *str, int len,
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900516 const unsigned char *token)
Paulo Marques7c5d2492007-06-20 18:09:00 +0100517{
518 int i;
519
520 for (i = 0; i < len - 1; i++) {
521 if (str[i] == token[0] && str[i+1] == token[1])
522 return &str[i];
523 }
524 return NULL;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/* replace a given token in all the valid symbols. Use the sampled symbols
528 * to update the counts */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900529static void compress_symbols(const unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700531 unsigned int i, len, size;
532 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700534 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Masahiro Yamada8d605262020-02-02 14:09:21 +0900536 len = table[i]->len;
537 p1 = table[i]->sym;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700538
539 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100540 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700541 if (!p2) continue;
542
543 /* decrease the counts for this symbol's tokens */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900544 forget_symbol(table[i]->sym, len);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700545
546 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700549 *p2 = idx;
550 p2++;
551 size -= (p2 - p1);
552 memmove(p2, p2 + 1, size);
553 p1 = p2;
554 len--;
555
556 if (size < 2) break;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100559 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700561 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Masahiro Yamada8d605262020-02-02 14:09:21 +0900563 table[i]->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700565 /* increase the counts for this symbol's new tokens */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900566 learn_symbol(table[i]->sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568}
569
570/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700571static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700573 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700576 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700578 for (i = 0; i < 0x10000; i++) {
579 if (token_profit[i] > bestprofit) {
580 best = i;
581 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return best;
585}
586
587/* this is the core of the algorithm: calculate the "best" table */
588static void optimize_result(void)
589{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700590 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* using the '\0' symbol last allows compress_symbols to use standard
593 * fast string functions */
594 for (i = 255; i >= 0; i--) {
595
596 /* if this table slot is empty (it is not used by an actual
597 * original char code */
598 if (!best_table_len[i]) {
599
Cao jincbf7a902018-02-27 16:16:19 +0800600 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800602 if (token_profit[best] == 0)
603 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700606 best_table_len[i] = 2;
607 best_table[i][0] = best & 0xFF;
608 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700611 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 }
614}
615
616/* start by placing the symbols that are actually used on the table */
617static void insert_real_symbols_in_table(void)
618{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700619 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700621 for (i = 0; i < table_cnt; i++) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900622 for (j = 0; j < table[i]->len; j++) {
623 c = table[i]->sym[j];
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700624 best_table[c][0]=c;
625 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 }
628}
629
630static void optimize_token_table(void)
631{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 build_initial_tok_table();
633
634 insert_real_symbols_in_table();
635
636 optimize_result();
637}
638
Lai Jiangshanb478b782009-03-13 15:10:26 +0800639/* guess for "linker script provide" symbol */
640static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
641{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900642 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800643 int len = se->len - 1;
644
645 if (len < 8)
646 return 0;
647
648 if (symbol[0] != '_' || symbol[1] != '_')
649 return 0;
650
651 /* __start_XXXXX */
652 if (!memcmp(symbol + 2, "start_", 6))
653 return 1;
654
655 /* __stop_XXXXX */
656 if (!memcmp(symbol + 2, "stop_", 5))
657 return 1;
658
659 /* __end_XXXXX */
660 if (!memcmp(symbol + 2, "end_", 4))
661 return 1;
662
663 /* __XXXXX_start */
664 if (!memcmp(symbol + len - 6, "_start", 6))
665 return 1;
666
667 /* __XXXXX_end */
668 if (!memcmp(symbol + len - 4, "_end", 4))
669 return 1;
670
671 return 0;
672}
673
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800674static int compare_symbols(const void *a, const void *b)
675{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900676 const struct sym_entry *sa = *(const struct sym_entry **)a;
677 const struct sym_entry *sb = *(const struct sym_entry **)b;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800678 int wa, wb;
679
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800680 /* sort by address first */
681 if (sa->addr > sb->addr)
682 return 1;
683 if (sa->addr < sb->addr)
684 return -1;
685
686 /* sort by "weakness" type */
687 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
688 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
689 if (wa != wb)
690 return wa - wb;
691
Lai Jiangshanb478b782009-03-13 15:10:26 +0800692 /* sort by "linker script provide" type */
693 wa = may_be_linker_script_provide_symbol(sa);
694 wb = may_be_linker_script_provide_symbol(sb);
695 if (wa != wb)
696 return wa - wb;
697
698 /* sort by the number of prefix underscores */
Masahiro Yamadaaa915242019-11-24 01:04:36 +0900699 wa = strspn(sym_name(sa), "_");
700 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800701 if (wa != wb)
702 return wa - wb;
703
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800704 /* sort by initial order, so that other symbols are left undisturbed */
705 return sa->start_pos - sb->start_pos;
706}
707
708static void sort_symbols(void)
709{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900710 qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030713static void make_percpus_absolute(void)
714{
715 unsigned int i;
716
717 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900718 if (symbol_in_range(table[i], &percpu_range, 1)) {
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700719 /*
720 * Keep the 'A' override for percpu symbols to
721 * ensure consistent behavior compared to older
722 * versions of this tool.
723 */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900724 table[i]->sym[0] = 'A';
725 table[i]->percpu_absolute = 1;
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700726 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030727}
728
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700729/* find the minimum non-absolute symbol address */
730static void record_relative_base(void)
731{
732 unsigned int i;
733
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700734 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900735 if (!symbol_absolute(table[i])) {
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900736 /*
737 * The table is sorted by address.
738 * Take the first non-absolute symbol value.
739 */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900740 relative_base = table[i]->addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900741 return;
742 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700743}
744
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700745int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700747 if (argc >= 2) {
748 int i;
749 for (i = 1; i < argc; i++) {
750 if(strcmp(argv[i], "--all-symbols") == 0)
751 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030752 else if (strcmp(argv[i], "--absolute-percpu") == 0)
753 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900754 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700755 base_relative = 1;
756 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700757 usage();
758 }
759 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 usage();
761
762 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900763 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030764 if (absolute_percpu)
765 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900766 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700767 if (base_relative)
768 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100769 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 write_src();
771
772 return 0;
773}