blob: 843615c1d384e1067973af38073d5986c24ab568 [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[] = {
108 "__crc_", /* modversions */
109 "__efistub_", /* arm64 EFI stub namespace */
110 NULL
111 };
112
113 static const char * const ignored_suffixes[] = {
114 "_from_arm", /* arm */
115 "_from_thumb", /* arm */
116 "_veneer", /* arm */
117 NULL
118 };
119
120 const char * const *p;
121
122 /* Exclude symbols which vary between passes. */
123 for (p = ignored_symbols; *p; p++)
124 if (!strcmp(name, *p))
125 return true;
126
127 for (p = ignored_prefixes; *p; p++)
128 if (!strncmp(name, *p, strlen(*p)))
129 return true;
130
131 for (p = ignored_suffixes; *p; p++) {
132 int l = strlen(name) - strlen(*p);
133
134 if (l >= 0 && !strcmp(name + l, *p))
135 return true;
136 }
137
138 return false;
139}
140
Kees Cook78eb7152014-03-17 13:18:27 +1030141static int check_symbol_range(const char *sym, unsigned long long addr,
142 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400143{
144 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030145 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400146
Kees Cook78eb7152014-03-17 13:18:27 +1030147 for (i = 0; i < entries; ++i) {
148 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400149
Kees Cook78eb7152014-03-17 13:18:27 +1030150 if (strcmp(sym, ar->start_sym) == 0) {
151 ar->start = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400152 return 0;
Kees Cook78eb7152014-03-17 13:18:27 +1030153 } else if (strcmp(sym, ar->end_sym) == 0) {
154 ar->end = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400155 return 0;
156 }
157 }
158
159 return 1;
160}
161
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700162static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900164 char sym[500], stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int rc;
166
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900167 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, sym);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (rc != 3) {
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900169 if (rc != EOF && fgets(sym, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600170 fprintf(stderr, "Read error or end of file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -1;
172 }
Eugene Loh6db29832019-01-17 14:46:00 -0800173 if (strlen(sym) >= KSYM_NAME_LEN) {
174 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900175 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900176 sym, strlen(sym), KSYM_NAME_LEN);
Andi Kleenf3462aa2013-10-23 15:07:53 +0200177 return -1;
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900180 if (is_ignored_symbol(sym, stype))
181 return -1;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100184 if (strcmp(sym, "_text") == 0)
185 _text = s->addr;
Kees Cook78eb7152014-03-17 13:18:27 +1030186 else if (check_symbol_range(sym, s->addr, text_ranges,
187 ARRAY_SIZE(text_ranges)) == 0)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400188 /* nothing to do */;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700189 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 {
191 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700192 if (strcmp(sym, "__kernel_syscall_via_break") &&
193 strcmp(sym, "__kernel_syscall_via_epc") &&
194 strcmp(sym, "__kernel_sigtramp") &&
195 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return -1;
197
198 }
Masahiro Yamadae0109042019-11-24 01:04:33 +0900199 else if (toupper(stype) == 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -1;
Masahiro Yamadae0109042019-11-24 01:04:33 +0900201 /*
202 * Ignore generated symbols such as:
203 * - mapping symbols in ARM ELF files ($a, $t, and $d)
204 * - MIPS ELF local symbols ($L123 instead of .L123)
205 */
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900206 else if (sym[0] == '$')
Ralf Baechle6f00df22005-09-06 15:16:41 -0700207 return -1;
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200208 /* exclude debugging symbols */
Guenter Roeck51962a92017-10-13 15:57:58 -0700209 else if (stype == 'N' || stype == 'n')
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200210 return -1;
Vasily Gorbik33177f012019-06-28 19:22:47 +0200211 /* exclude s390 kasan local symbols */
212 else if (!strncmp(sym, ".LASANPC", 8))
213 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* include the type field in the symbol name, so that it gets
216 * compressed together */
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900217 s->len = strlen(sym) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700218 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800219 if (!s->sym) {
220 fprintf(stderr, "kallsyms failure: "
221 "unable to allocate required amount of memory\n");
222 exit(EXIT_FAILURE);
223 }
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900224 strcpy(sym_name(s), sym);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700225 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700227 s->percpu_absolute = 0;
228
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030229 /* Record if we've found __per_cpu_start/end. */
230 check_symbol_range(sym, s->addr, &percpu_range, 1);
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233}
234
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900235static int symbol_in_range(const struct sym_entry *s,
236 const struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400237{
238 size_t i;
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900239 const struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400240
Kees Cook78eb7152014-03-17 13:18:27 +1030241 for (i = 0; i < entries; ++i) {
242 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400243
Kees Cook78eb7152014-03-17 13:18:27 +1030244 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400245 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400246 }
247
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400248 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400249}
250
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900251static int symbol_valid(const struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900253 const char *name = sym_name(s);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* if --all-symbols is not specified, then symbols outside the text
256 * and inittext sections are discarded */
257 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030258 if (symbol_in_range(s, text_ranges,
259 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
261 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800262 * _etext _einittext; they can move between pass 1 and 2 when
263 * the kallsyms data are added. If these symbols move then
264 * they may get dropped in pass 2, which breaks the kallsyms
265 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400267 if ((s->addr == text_range_text->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900268 strcmp(name, text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400269 (s->addr == text_range_inittext->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900270 strcmp(name, text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
272 }
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 1;
275}
276
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900277/* remove all the invalid symbols from the table */
278static void shrink_table(void)
279{
280 unsigned int i, pos;
281
282 pos = 0;
283 for (i = 0; i < table_cnt; i++) {
284 if (symbol_valid(&table[i])) {
285 if (pos != i)
286 table[pos] = table[i];
287 pos++;
288 } else {
289 free(table[i].sym);
290 }
291 }
292 table_cnt = pos;
293
294 /* When valid symbol is not registered, exit to error */
295 if (!table_cnt) {
296 fprintf(stderr, "No valid symbol.\n");
297 exit(1);
298 }
299}
300
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700301static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700304 if (table_cnt >= table_size) {
305 table_size += 10000;
306 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!table) {
308 fprintf(stderr, "out of memory\n");
309 exit (1);
310 }
311 }
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800312 if (read_symbol(in, &table[table_cnt]) == 0) {
313 table[table_cnt].start_pos = table_cnt;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700314 table_cnt++;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800315 }
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
326/* uncompress a compressed symbol. When this function is called, the best table
327 * might still be compressed itself, so the function needs to be recursive */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900328static int expand_symbol(const unsigned char *data, int len, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int c, rlen, total=0;
331
332 while (len) {
333 c = *data;
334 /* if the table holds a single char that is the same as the one
335 * we are looking for, then end the search */
336 if (best_table[c][0]==c && best_table_len[c]==1) {
337 *result++ = c;
338 total++;
339 } else {
340 /* if not, recurse and expand */
341 rlen = expand_symbol(best_table[c], best_table_len[c], result);
342 total += rlen;
343 result += rlen;
344 }
345 data++;
346 len--;
347 }
348 *result=0;
349
350 return total;
351}
352
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900353static int symbol_absolute(const struct sym_entry *s)
Kees Cook78eb7152014-03-17 13:18:27 +1030354{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700355 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030356}
357
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700358static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700360 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 unsigned int best_idx[256];
362 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700363 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Masahiro Yamada500193e2019-02-04 10:53:18 +0900365 printf("#include <asm/bitsperlong.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 printf("#if BITS_PER_LONG == 64\n");
367 printf("#define PTR .quad\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100368 printf("#define ALGN .balign 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 printf("#else\n");
370 printf("#define PTR .long\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100371 printf("#define ALGN .balign 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 printf("#endif\n");
373
Jan Beulichaad09472006-12-08 02:35:57 -0800374 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700376 /* Provide proper symbols relocatability by their relativeness
377 * to a fixed anchor point in the runtime image, either '_text'
378 * for absolute address tables, in which case the linker will
379 * emit the final addresses at build time. Otherwise, use the
380 * offset relative to the lowest value encountered of all relative
381 * symbols, and emit non-relocatable fixed offsets that will be fixed
382 * up at runtime.
383 *
384 * The symbol names cannot be used to construct normal symbol
385 * references as the list of symbols contains symbols that are
386 * declared static and are private to their .o files. This prevents
387 * .tmp_kallsyms.o or any other object from referencing them.
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100388 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700389 if (!base_relative)
390 output_label("kallsyms_addresses");
391 else
392 output_label("kallsyms_offsets");
393
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700394 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700395 if (base_relative) {
396 long long offset;
397 int overflow;
398
399 if (!absolute_percpu) {
400 offset = table[i].addr - relative_base;
401 overflow = (offset < 0 || offset > UINT_MAX);
402 } else if (symbol_absolute(&table[i])) {
403 offset = table[i].addr;
404 overflow = (offset < 0 || offset > INT_MAX);
405 } else {
406 offset = relative_base - table[i].addr - 1;
407 overflow = (offset < INT_MIN || offset >= 0);
408 }
409 if (overflow) {
410 fprintf(stderr, "kallsyms failure: "
411 "%s symbol value %#llx out of range in relative mode\n",
412 symbol_absolute(&table[i]) ? "absolute" : "relative",
413 table[i].addr);
414 exit(EXIT_FAILURE);
415 }
416 printf("\t.long\t%#x\n", (int)offset);
417 } else if (!symbol_absolute(&table[i])) {
Vivek Goyal2c22d8b2006-12-07 02:14:10 +0100418 if (_text <= table[i].addr)
419 printf("\tPTR\t_text + %#llx\n",
420 table[i].addr - _text);
421 else
Andrew Morton2930ffc2014-03-10 15:49:48 -0700422 printf("\tPTR\t_text - %#llx\n",
423 _text - table[i].addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100424 } else {
425 printf("\tPTR\t%#llx\n", table[i].addr);
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428 printf("\n");
429
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700430 if (base_relative) {
431 output_label("kallsyms_relative_base");
432 printf("\tPTR\t_text - %#llx\n", _text - relative_base);
433 printf("\n");
434 }
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 output_label("kallsyms_num_syms");
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600437 printf("\t.long\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 printf("\n");
439
440 /* table of offset markers, that give the offset in the compressed stream
441 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800442 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
443 if (!markers) {
444 fprintf(stderr, "kallsyms failure: "
445 "unable to allocate required memory\n");
446 exit(EXIT_FAILURE);
447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700451 for (i = 0; i < table_cnt; i++) {
452 if ((i & 0xFF) == 0)
453 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 printf("\t.byte 0x%02x", table[i].len);
456 for (k = 0; k < table[i].len; k++)
457 printf(", 0x%02x", table[i].sym[k]);
458 printf("\n");
459
460 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 printf("\n");
463
464 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700465 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600466 printf("\t.long\t%u\n", markers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 printf("\n");
468
469 free(markers);
470
471 output_label("kallsyms_token_table");
472 off = 0;
473 for (i = 0; i < 256; i++) {
474 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700475 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 printf("\t.asciz\t\"%s\"\n", buf);
477 off += strlen(buf) + 1;
478 }
479 printf("\n");
480
481 output_label("kallsyms_token_index");
482 for (i = 0; i < 256; i++)
483 printf("\t.short\t%d\n", best_idx[i]);
484 printf("\n");
485}
486
487
488/* table lookup compression functions */
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490/* count all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900491static void learn_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 int i;
494
495 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700496 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/* decrease the count for all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900500static void forget_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int i;
503
504 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700505 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900508/* do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509static void build_initial_tok_table(void)
510{
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900511 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900513 for (i = 0; i < table_cnt; i++)
514 learn_symbol(table[i].sym, table[i].len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Masahiro Yamada2558c132019-11-24 01:04:37 +0900517static unsigned char *find_token(unsigned char *str, int len,
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900518 const unsigned char *token)
Paulo Marques7c5d2492007-06-20 18:09:00 +0100519{
520 int i;
521
522 for (i = 0; i < len - 1; i++) {
523 if (str[i] == token[0] && str[i+1] == token[1])
524 return &str[i];
525 }
526 return NULL;
527}
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/* replace a given token in all the valid symbols. Use the sampled symbols
530 * to update the counts */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900531static void compress_symbols(const unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700533 unsigned int i, len, size;
534 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700536 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700539 p1 = table[i].sym;
540
541 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100542 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700543 if (!p2) continue;
544
545 /* decrease the counts for this symbol's tokens */
546 forget_symbol(table[i].sym, len);
547
548 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700551 *p2 = idx;
552 p2++;
553 size -= (p2 - p1);
554 memmove(p2, p2 + 1, size);
555 p1 = p2;
556 len--;
557
558 if (size < 2) break;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100561 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700563 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700565 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700567 /* increase the counts for this symbol's new tokens */
568 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570}
571
572/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700573static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700575 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700578 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700580 for (i = 0; i < 0x10000; i++) {
581 if (token_profit[i] > bestprofit) {
582 best = i;
583 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return best;
587}
588
589/* this is the core of the algorithm: calculate the "best" table */
590static void optimize_result(void)
591{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700592 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* using the '\0' symbol last allows compress_symbols to use standard
595 * fast string functions */
596 for (i = 255; i >= 0; i--) {
597
598 /* if this table slot is empty (it is not used by an actual
599 * original char code */
600 if (!best_table_len[i]) {
601
Cao jincbf7a902018-02-27 16:16:19 +0800602 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800604 if (token_profit[best] == 0)
605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700608 best_table_len[i] = 2;
609 best_table[i][0] = best & 0xFF;
610 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700613 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615 }
616}
617
618/* start by placing the symbols that are actually used on the table */
619static void insert_real_symbols_in_table(void)
620{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700621 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700623 for (i = 0; i < table_cnt; i++) {
624 for (j = 0; j < table[i].len; j++) {
625 c = table[i].sym[j];
626 best_table[c][0]=c;
627 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
630}
631
632static void optimize_token_table(void)
633{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 build_initial_tok_table();
635
636 insert_real_symbols_in_table();
637
638 optimize_result();
639}
640
Lai Jiangshanb478b782009-03-13 15:10:26 +0800641/* guess for "linker script provide" symbol */
642static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
643{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900644 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800645 int len = se->len - 1;
646
647 if (len < 8)
648 return 0;
649
650 if (symbol[0] != '_' || symbol[1] != '_')
651 return 0;
652
653 /* __start_XXXXX */
654 if (!memcmp(symbol + 2, "start_", 6))
655 return 1;
656
657 /* __stop_XXXXX */
658 if (!memcmp(symbol + 2, "stop_", 5))
659 return 1;
660
661 /* __end_XXXXX */
662 if (!memcmp(symbol + 2, "end_", 4))
663 return 1;
664
665 /* __XXXXX_start */
666 if (!memcmp(symbol + len - 6, "_start", 6))
667 return 1;
668
669 /* __XXXXX_end */
670 if (!memcmp(symbol + len - 4, "_end", 4))
671 return 1;
672
673 return 0;
674}
675
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800676static int compare_symbols(const void *a, const void *b)
677{
678 const struct sym_entry *sa;
679 const struct sym_entry *sb;
680 int wa, wb;
681
682 sa = a;
683 sb = b;
684
685 /* sort by address first */
686 if (sa->addr > sb->addr)
687 return 1;
688 if (sa->addr < sb->addr)
689 return -1;
690
691 /* sort by "weakness" type */
692 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
693 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
694 if (wa != wb)
695 return wa - wb;
696
Lai Jiangshanb478b782009-03-13 15:10:26 +0800697 /* sort by "linker script provide" type */
698 wa = may_be_linker_script_provide_symbol(sa);
699 wb = may_be_linker_script_provide_symbol(sb);
700 if (wa != wb)
701 return wa - wb;
702
703 /* sort by the number of prefix underscores */
Masahiro Yamadaaa915242019-11-24 01:04:36 +0900704 wa = strspn(sym_name(sa), "_");
705 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800706 if (wa != wb)
707 return wa - wb;
708
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800709 /* sort by initial order, so that other symbols are left undisturbed */
710 return sa->start_pos - sb->start_pos;
711}
712
713static void sort_symbols(void)
714{
715 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
716}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030718static void make_percpus_absolute(void)
719{
720 unsigned int i;
721
722 for (i = 0; i < table_cnt; i++)
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700723 if (symbol_in_range(&table[i], &percpu_range, 1)) {
724 /*
725 * Keep the 'A' override for percpu symbols to
726 * ensure consistent behavior compared to older
727 * versions of this tool.
728 */
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030729 table[i].sym[0] = 'A';
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700730 table[i].percpu_absolute = 1;
731 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030732}
733
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700734/* find the minimum non-absolute symbol address */
735static void record_relative_base(void)
736{
737 unsigned int i;
738
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700739 for (i = 0; i < table_cnt; i++)
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900740 if (!symbol_absolute(&table[i])) {
741 /*
742 * The table is sorted by address.
743 * Take the first non-absolute symbol value.
744 */
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700745 relative_base = table[i].addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900746 return;
747 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700748}
749
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700750int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700752 if (argc >= 2) {
753 int i;
754 for (i = 1; i < argc; i++) {
755 if(strcmp(argv[i], "--all-symbols") == 0)
756 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030757 else if (strcmp(argv[i], "--absolute-percpu") == 0)
758 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900759 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700760 base_relative = 1;
761 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700762 usage();
763 }
764 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 usage();
766
767 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900768 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030769 if (absolute_percpu)
770 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900771 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700772 if (base_relative)
773 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100774 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 write_src();
776
777 return 0;
778}