blob: f4d5f131556d1f4478e685bbc5ee9dcca82ec468 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Generate assembler source containing symbol information
2 *
3 * Copyright 2002 by Kai Germaschewski
4 *
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
7 *
8 * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Table compression uses all the unused char codes on the symbols and
11 * maps these to the most used substrings (tokens). For instance, it might
12 * map char code 0xF7 to represent "write_" and then in every symbol where
13 * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
14 * The used codes themselves are also placed in the table so that the
15 * decompresion can work without "special cases".
16 * Applied to kernel symbols, this usually produces a compression ratio
17 * of about 50%.
18 *
19 */
20
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090021#include <stdbool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070026#include <limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040028#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040029
Tejun Heo9281ace2007-07-17 04:03:51 -070030#define KSYM_NAME_LEN 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct sym_entry {
33 unsigned long long addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070034 unsigned int len;
Paulo Marquesf2df3f62008-02-06 01:37:33 -080035 unsigned int start_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 unsigned char *sym;
Ard Biesheuvel8c996942016-03-15 14:58:15 -070037 unsigned int percpu_absolute;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
Kees Cook78eb7152014-03-17 13:18:27 +103040struct addr_range {
41 const char *start_sym, *end_sym;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040042 unsigned long long start, end;
43};
44
45static unsigned long long _text;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070046static unsigned long long relative_base;
Kees Cook78eb7152014-03-17 13:18:27 +103047static struct addr_range text_ranges[] = {
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040048 { "_stext", "_etext" },
49 { "_sinittext", "_einittext" },
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040050};
51#define text_range_text (&text_ranges[0])
52#define text_range_inittext (&text_ranges[1])
53
Rusty Russellc6bda7c2014-03-17 14:05:46 +103054static struct addr_range percpu_range = {
55 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
56};
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static struct sym_entry *table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070059static unsigned int table_size, table_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static int all_symbols = 0;
Rusty Russellc6bda7c2014-03-17 14:05:46 +103061static int absolute_percpu = 0;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070062static int base_relative = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090064static int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* the table that holds the result of the compression */
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090067static unsigned char best_table[256][2];
68static unsigned char best_table_len[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070071static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Ming Leif6537f22013-11-02 09:11:33 +103073 fprintf(stderr, "Usage: kallsyms [--all-symbols] "
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070074 "[--base-relative] < in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 exit(1);
76}
77
Masahiro Yamada29e55ad2019-11-24 01:04:35 +090078static char *sym_name(const struct sym_entry *s)
79{
80 return (char *)s->sym + 1;
81}
82
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090083static bool is_ignored_symbol(const char *name, char type)
84{
85 static const char * const ignored_symbols[] = {
86 /*
87 * Symbols which vary between passes. Passes 1 and 2 must have
88 * identical symbol lists. The kallsyms_* symbols below are
89 * only added after pass 1, they would be included in pass 2
90 * when --all-symbols is specified so exclude them to get a
91 * stable symbol list.
92 */
93 "kallsyms_addresses",
94 "kallsyms_offsets",
95 "kallsyms_relative_base",
96 "kallsyms_num_syms",
97 "kallsyms_names",
98 "kallsyms_markers",
99 "kallsyms_token_table",
100 "kallsyms_token_index",
101 /* Exclude linker generated symbols which vary between passes */
102 "_SDA_BASE_", /* ppc */
103 "_SDA2_BASE_", /* ppc */
104 NULL
105 };
106
107 static const char * const ignored_prefixes[] = {
Masahiro Yamada97261e12019-11-24 01:04:40 +0900108 "$", /* local symbols for ARM, MIPS, etc. */
109 ".LASANPC", /* s390 kasan local symbols */
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900110 "__crc_", /* modversions */
111 "__efistub_", /* arm64 EFI stub namespace */
112 NULL
113 };
114
115 static const char * const ignored_suffixes[] = {
116 "_from_arm", /* arm */
117 "_from_thumb", /* arm */
118 "_veneer", /* arm */
119 NULL
120 };
121
122 const char * const *p;
123
124 /* Exclude symbols which vary between passes. */
125 for (p = ignored_symbols; *p; p++)
126 if (!strcmp(name, *p))
127 return true;
128
129 for (p = ignored_prefixes; *p; p++)
130 if (!strncmp(name, *p, strlen(*p)))
131 return true;
132
133 for (p = ignored_suffixes; *p; p++) {
134 int l = strlen(name) - strlen(*p);
135
136 if (l >= 0 && !strcmp(name + l, *p))
137 return true;
138 }
139
Masahiro Yamada887df762019-11-24 01:04:41 +0900140 if (type == 'U' || type == 'u')
141 return true;
142 /* exclude debugging symbols */
143 if (type == 'N' || type == 'n')
144 return true;
145
146 if (toupper(type) == 'A') {
147 /* Keep these useful absolute symbols */
148 if (strcmp(name, "__kernel_syscall_via_break") &&
149 strcmp(name, "__kernel_syscall_via_epc") &&
150 strcmp(name, "__kernel_sigtramp") &&
151 strcmp(name, "__gp"))
152 return true;
153 }
154
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900155 return false;
156}
157
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900158static void check_symbol_range(const char *sym, unsigned long long addr,
159 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400160{
161 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030162 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400163
Kees Cook78eb7152014-03-17 13:18:27 +1030164 for (i = 0; i < entries; ++i) {
165 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400166
Kees Cook78eb7152014-03-17 13:18:27 +1030167 if (strcmp(sym, ar->start_sym) == 0) {
168 ar->start = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900169 return;
Kees Cook78eb7152014-03-17 13:18:27 +1030170 } else if (strcmp(sym, ar->end_sym) == 0) {
171 ar->end = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900172 return;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400173 }
174 }
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400175}
176
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700177static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900179 char sym[500], stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int rc;
181
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900182 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (rc != 3) {
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900184 if (rc != EOF && fgets(sym, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600185 fprintf(stderr, "Read error or end of file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -1;
187 }
Eugene Loh6db29832019-01-17 14:46:00 -0800188 if (strlen(sym) >= KSYM_NAME_LEN) {
189 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900190 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900191 sym, strlen(sym), KSYM_NAME_LEN);
Andi Kleenf3462aa2013-10-23 15:07:53 +0200192 return -1;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900195 if (is_ignored_symbol(sym, stype))
196 return -1;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100199 if (strcmp(sym, "_text") == 0)
200 _text = s->addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900201
202 check_symbol_range(sym, s->addr, text_ranges, ARRAY_SIZE(text_ranges));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 /* include the type field in the symbol name, so that it gets
205 * compressed together */
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900206 s->len = strlen(sym) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700207 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800208 if (!s->sym) {
209 fprintf(stderr, "kallsyms failure: "
210 "unable to allocate required amount of memory\n");
211 exit(EXIT_FAILURE);
212 }
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900213 strcpy(sym_name(s), sym);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700214 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700216 s->percpu_absolute = 0;
217
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030218 /* Record if we've found __per_cpu_start/end. */
219 check_symbol_range(sym, s->addr, &percpu_range, 1);
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return 0;
222}
223
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900224static int symbol_in_range(const struct sym_entry *s,
225 const struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400226{
227 size_t i;
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900228 const struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400229
Kees Cook78eb7152014-03-17 13:18:27 +1030230 for (i = 0; i < entries; ++i) {
231 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400232
Kees Cook78eb7152014-03-17 13:18:27 +1030233 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400234 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400235 }
236
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400237 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400238}
239
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900240static int symbol_valid(const struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900242 const char *name = sym_name(s);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* if --all-symbols is not specified, then symbols outside the text
245 * and inittext sections are discarded */
246 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030247 if (symbol_in_range(s, text_ranges,
248 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return 0;
250 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800251 * _etext _einittext; they can move between pass 1 and 2 when
252 * the kallsyms data are added. If these symbols move then
253 * they may get dropped in pass 2, which breaks the kallsyms
254 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400256 if ((s->addr == text_range_text->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900257 strcmp(name, text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400258 (s->addr == text_range_inittext->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900259 strcmp(name, text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261 }
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 1;
264}
265
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900266/* remove all the invalid symbols from the table */
267static void shrink_table(void)
268{
269 unsigned int i, pos;
270
271 pos = 0;
272 for (i = 0; i < table_cnt; i++) {
273 if (symbol_valid(&table[i])) {
274 if (pos != i)
275 table[pos] = table[i];
276 pos++;
277 } else {
278 free(table[i].sym);
279 }
280 }
281 table_cnt = pos;
282
283 /* When valid symbol is not registered, exit to error */
284 if (!table_cnt) {
285 fprintf(stderr, "No valid symbol.\n");
286 exit(1);
287 }
288}
289
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700290static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700293 if (table_cnt >= table_size) {
294 table_size += 10000;
295 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!table) {
297 fprintf(stderr, "out of memory\n");
298 exit (1);
299 }
300 }
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800301 if (read_symbol(in, &table[table_cnt]) == 0) {
302 table[table_cnt].start_pos = table_cnt;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700303 table_cnt++;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306}
307
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900308static void output_label(const char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900310 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 printf("\tALGN\n");
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900312 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/* uncompress a compressed symbol. When this function is called, the best table
316 * might still be compressed itself, so the function needs to be recursive */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900317static int expand_symbol(const unsigned char *data, int len, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int c, rlen, total=0;
320
321 while (len) {
322 c = *data;
323 /* if the table holds a single char that is the same as the one
324 * we are looking for, then end the search */
325 if (best_table[c][0]==c && best_table_len[c]==1) {
326 *result++ = c;
327 total++;
328 } else {
329 /* if not, recurse and expand */
330 rlen = expand_symbol(best_table[c], best_table_len[c], result);
331 total += rlen;
332 result += rlen;
333 }
334 data++;
335 len--;
336 }
337 *result=0;
338
339 return total;
340}
341
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900342static int symbol_absolute(const struct sym_entry *s)
Kees Cook78eb7152014-03-17 13:18:27 +1030343{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700344 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030345}
346
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700347static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700349 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 unsigned int best_idx[256];
351 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700352 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Masahiro Yamada500193e2019-02-04 10:53:18 +0900354 printf("#include <asm/bitsperlong.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 printf("#if BITS_PER_LONG == 64\n");
356 printf("#define PTR .quad\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100357 printf("#define ALGN .balign 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 printf("#else\n");
359 printf("#define PTR .long\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100360 printf("#define ALGN .balign 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 printf("#endif\n");
362
Jan Beulichaad09472006-12-08 02:35:57 -0800363 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700365 /* Provide proper symbols relocatability by their relativeness
366 * to a fixed anchor point in the runtime image, either '_text'
367 * for absolute address tables, in which case the linker will
368 * emit the final addresses at build time. Otherwise, use the
369 * offset relative to the lowest value encountered of all relative
370 * symbols, and emit non-relocatable fixed offsets that will be fixed
371 * up at runtime.
372 *
373 * The symbol names cannot be used to construct normal symbol
374 * references as the list of symbols contains symbols that are
375 * declared static and are private to their .o files. This prevents
376 * .tmp_kallsyms.o or any other object from referencing them.
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100377 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700378 if (!base_relative)
379 output_label("kallsyms_addresses");
380 else
381 output_label("kallsyms_offsets");
382
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700383 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700384 if (base_relative) {
385 long long offset;
386 int overflow;
387
388 if (!absolute_percpu) {
389 offset = table[i].addr - relative_base;
390 overflow = (offset < 0 || offset > UINT_MAX);
391 } else if (symbol_absolute(&table[i])) {
392 offset = table[i].addr;
393 overflow = (offset < 0 || offset > INT_MAX);
394 } else {
395 offset = relative_base - table[i].addr - 1;
396 overflow = (offset < INT_MIN || offset >= 0);
397 }
398 if (overflow) {
399 fprintf(stderr, "kallsyms failure: "
400 "%s symbol value %#llx out of range in relative mode\n",
401 symbol_absolute(&table[i]) ? "absolute" : "relative",
402 table[i].addr);
403 exit(EXIT_FAILURE);
404 }
405 printf("\t.long\t%#x\n", (int)offset);
406 } else if (!symbol_absolute(&table[i])) {
Vivek Goyal2c22d8b2006-12-07 02:14:10 +0100407 if (_text <= table[i].addr)
408 printf("\tPTR\t_text + %#llx\n",
409 table[i].addr - _text);
410 else
Andrew Morton2930ffc2014-03-10 15:49:48 -0700411 printf("\tPTR\t_text - %#llx\n",
412 _text - table[i].addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100413 } else {
414 printf("\tPTR\t%#llx\n", table[i].addr);
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 printf("\n");
418
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700419 if (base_relative) {
420 output_label("kallsyms_relative_base");
421 printf("\tPTR\t_text - %#llx\n", _text - relative_base);
422 printf("\n");
423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 output_label("kallsyms_num_syms");
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600426 printf("\t.long\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 printf("\n");
428
429 /* table of offset markers, that give the offset in the compressed stream
430 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800431 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
432 if (!markers) {
433 fprintf(stderr, "kallsyms failure: "
434 "unable to allocate required memory\n");
435 exit(EXIT_FAILURE);
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700440 for (i = 0; i < table_cnt; i++) {
441 if ((i & 0xFF) == 0)
442 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 printf("\t.byte 0x%02x", table[i].len);
445 for (k = 0; k < table[i].len; k++)
446 printf(", 0x%02x", table[i].sym[k]);
447 printf("\n");
448
449 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 printf("\n");
452
453 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700454 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600455 printf("\t.long\t%u\n", markers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 printf("\n");
457
458 free(markers);
459
460 output_label("kallsyms_token_table");
461 off = 0;
462 for (i = 0; i < 256; i++) {
463 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700464 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 printf("\t.asciz\t\"%s\"\n", buf);
466 off += strlen(buf) + 1;
467 }
468 printf("\n");
469
470 output_label("kallsyms_token_index");
471 for (i = 0; i < 256; i++)
472 printf("\t.short\t%d\n", best_idx[i]);
473 printf("\n");
474}
475
476
477/* table lookup compression functions */
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/* count all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900480static void learn_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 int i;
483
484 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700485 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/* decrease the count for all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900489static void forget_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
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900497/* do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static void build_initial_tok_table(void)
499{
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900500 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900502 for (i = 0; i < table_cnt; i++)
503 learn_symbol(table[i].sym, table[i].len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Masahiro Yamada2558c132019-11-24 01:04:37 +0900506static unsigned char *find_token(unsigned char *str, int len,
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900507 const unsigned char *token)
Paulo Marques7c5d2492007-06-20 18:09:00 +0100508{
509 int i;
510
511 for (i = 0; i < len - 1; i++) {
512 if (str[i] == token[0] && str[i+1] == token[1])
513 return &str[i];
514 }
515 return NULL;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/* replace a given token in all the valid symbols. Use the sampled symbols
519 * to update the counts */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900520static void compress_symbols(const unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700522 unsigned int i, len, size;
523 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700525 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700528 p1 = table[i].sym;
529
530 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100531 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700532 if (!p2) continue;
533
534 /* decrease the counts for this symbol's tokens */
535 forget_symbol(table[i].sym, len);
536
537 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700540 *p2 = idx;
541 p2++;
542 size -= (p2 - p1);
543 memmove(p2, p2 + 1, size);
544 p1 = p2;
545 len--;
546
547 if (size < 2) break;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100550 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700552 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700554 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700556 /* increase the counts for this symbol's new tokens */
557 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559}
560
561/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700562static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700564 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700567 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700569 for (i = 0; i < 0x10000; i++) {
570 if (token_profit[i] > bestprofit) {
571 best = i;
572 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return best;
576}
577
578/* this is the core of the algorithm: calculate the "best" table */
579static void optimize_result(void)
580{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700581 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 /* using the '\0' symbol last allows compress_symbols to use standard
584 * fast string functions */
585 for (i = 255; i >= 0; i--) {
586
587 /* if this table slot is empty (it is not used by an actual
588 * original char code */
589 if (!best_table_len[i]) {
590
Cao jincbf7a902018-02-27 16:16:19 +0800591 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800593 if (token_profit[best] == 0)
594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700597 best_table_len[i] = 2;
598 best_table[i][0] = best & 0xFF;
599 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700602 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 }
605}
606
607/* start by placing the symbols that are actually used on the table */
608static void insert_real_symbols_in_table(void)
609{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700610 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700612 for (i = 0; i < table_cnt; i++) {
613 for (j = 0; j < table[i].len; j++) {
614 c = table[i].sym[j];
615 best_table[c][0]=c;
616 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 }
619}
620
621static void optimize_token_table(void)
622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 build_initial_tok_table();
624
625 insert_real_symbols_in_table();
626
627 optimize_result();
628}
629
Lai Jiangshanb478b782009-03-13 15:10:26 +0800630/* guess for "linker script provide" symbol */
631static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
632{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900633 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800634 int len = se->len - 1;
635
636 if (len < 8)
637 return 0;
638
639 if (symbol[0] != '_' || symbol[1] != '_')
640 return 0;
641
642 /* __start_XXXXX */
643 if (!memcmp(symbol + 2, "start_", 6))
644 return 1;
645
646 /* __stop_XXXXX */
647 if (!memcmp(symbol + 2, "stop_", 5))
648 return 1;
649
650 /* __end_XXXXX */
651 if (!memcmp(symbol + 2, "end_", 4))
652 return 1;
653
654 /* __XXXXX_start */
655 if (!memcmp(symbol + len - 6, "_start", 6))
656 return 1;
657
658 /* __XXXXX_end */
659 if (!memcmp(symbol + len - 4, "_end", 4))
660 return 1;
661
662 return 0;
663}
664
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800665static int compare_symbols(const void *a, const void *b)
666{
667 const struct sym_entry *sa;
668 const struct sym_entry *sb;
669 int wa, wb;
670
671 sa = a;
672 sb = b;
673
674 /* sort by address first */
675 if (sa->addr > sb->addr)
676 return 1;
677 if (sa->addr < sb->addr)
678 return -1;
679
680 /* sort by "weakness" type */
681 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
682 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
683 if (wa != wb)
684 return wa - wb;
685
Lai Jiangshanb478b782009-03-13 15:10:26 +0800686 /* sort by "linker script provide" type */
687 wa = may_be_linker_script_provide_symbol(sa);
688 wb = may_be_linker_script_provide_symbol(sb);
689 if (wa != wb)
690 return wa - wb;
691
692 /* sort by the number of prefix underscores */
Masahiro Yamadaaa915242019-11-24 01:04:36 +0900693 wa = strspn(sym_name(sa), "_");
694 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800695 if (wa != wb)
696 return wa - wb;
697
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800698 /* sort by initial order, so that other symbols are left undisturbed */
699 return sa->start_pos - sb->start_pos;
700}
701
702static void sort_symbols(void)
703{
704 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
705}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030707static void make_percpus_absolute(void)
708{
709 unsigned int i;
710
711 for (i = 0; i < table_cnt; i++)
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700712 if (symbol_in_range(&table[i], &percpu_range, 1)) {
713 /*
714 * Keep the 'A' override for percpu symbols to
715 * ensure consistent behavior compared to older
716 * versions of this tool.
717 */
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030718 table[i].sym[0] = 'A';
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700719 table[i].percpu_absolute = 1;
720 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030721}
722
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700723/* find the minimum non-absolute symbol address */
724static void record_relative_base(void)
725{
726 unsigned int i;
727
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700728 for (i = 0; i < table_cnt; i++)
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900729 if (!symbol_absolute(&table[i])) {
730 /*
731 * The table is sorted by address.
732 * Take the first non-absolute symbol value.
733 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700734 relative_base = table[i].addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900735 return;
736 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700737}
738
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700739int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700741 if (argc >= 2) {
742 int i;
743 for (i = 1; i < argc; i++) {
744 if(strcmp(argv[i], "--all-symbols") == 0)
745 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030746 else if (strcmp(argv[i], "--absolute-percpu") == 0)
747 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900748 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700749 base_relative = 1;
750 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700751 usage();
752 }
753 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 usage();
755
756 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900757 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030758 if (absolute_percpu)
759 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900760 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700761 if (base_relative)
762 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100763 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 write_src();
765
766 return 0;
767}