blob: bfac62a73f82f805cb47066187db89f2ca077322 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Generate assembler source containing symbol information
2 *
3 * Copyright 2002 by Kai Germaschewski
4 *
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
7 *
8 * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Table compression uses all the unused char codes on the symbols and
11 * maps these to the most used substrings (tokens). For instance, it might
12 * map char code 0xF7 to represent "write_" and then in every symbol where
13 * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
14 * The used codes themselves are also placed in the table so that the
15 * decompresion can work without "special cases".
16 * Applied to kernel symbols, this usually produces a compression ratio
17 * of about 50%.
18 *
19 */
20
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090021#include <stdbool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070026#include <limits.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040028#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040029
Tejun Heo9281ace2007-07-17 04:03:51 -070030#define KSYM_NAME_LEN 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct sym_entry {
33 unsigned long long addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070034 unsigned int len;
Paulo Marquesf2df3f62008-02-06 01:37:33 -080035 unsigned int start_pos;
Ard Biesheuvel8c996942016-03-15 14:58:15 -070036 unsigned int percpu_absolute;
Linus Torvalds9d829732020-05-04 09:16:37 -070037 unsigned char sym[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
Kees Cook78eb7152014-03-17 13:18:27 +103040struct addr_range {
41 const char *start_sym, *end_sym;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040042 unsigned long long start, end;
43};
44
45static unsigned long long _text;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070046static unsigned long long relative_base;
Kees Cook78eb7152014-03-17 13:18:27 +103047static struct addr_range text_ranges[] = {
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040048 { "_stext", "_etext" },
49 { "_sinittext", "_einittext" },
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040050};
51#define text_range_text (&text_ranges[0])
52#define text_range_inittext (&text_ranges[1])
53
Rusty Russellc6bda7c2014-03-17 14:05:46 +103054static struct addr_range percpu_range = {
55 "__per_cpu_start", "__per_cpu_end", -1ULL, 0
56};
57
Masahiro Yamada8d605262020-02-02 14:09:21 +090058static struct sym_entry **table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070059static unsigned int table_size, table_cnt;
Masahiro Yamada831362f2019-11-24 01:04:44 +090060static int all_symbols;
61static int absolute_percpu;
62static int base_relative;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090064static int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* the table that holds the result of the compression */
Masahiro Yamadaf43e9da2019-02-04 10:53:16 +090067static unsigned char best_table[256][2];
68static unsigned char best_table_len[256];
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070071static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Ming Leif6537f22013-11-02 09:11:33 +103073 fprintf(stderr, "Usage: kallsyms [--all-symbols] "
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -070074 "[--base-relative] < in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 exit(1);
76}
77
Masahiro Yamada29e55ad2019-11-24 01:04:35 +090078static char *sym_name(const struct sym_entry *s)
79{
80 return (char *)s->sym + 1;
81}
82
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090083static bool is_ignored_symbol(const char *name, char type)
84{
Masahiro Yamada516d9802020-09-23 02:48:56 +090085 /* Symbol names that exactly match to the following are ignored.*/
Masahiro Yamadaa41333e2019-11-24 01:04:39 +090086 static const char * const ignored_symbols[] = {
87 /*
88 * Symbols which vary between passes. Passes 1 and 2 must have
89 * identical symbol lists. The kallsyms_* symbols below are
90 * only added after pass 1, they would be included in pass 2
91 * when --all-symbols is specified so exclude them to get a
92 * stable symbol list.
93 */
94 "kallsyms_addresses",
95 "kallsyms_offsets",
96 "kallsyms_relative_base",
97 "kallsyms_num_syms",
98 "kallsyms_names",
99 "kallsyms_markers",
100 "kallsyms_token_table",
101 "kallsyms_token_index",
102 /* Exclude linker generated symbols which vary between passes */
103 "_SDA_BASE_", /* ppc */
104 "_SDA2_BASE_", /* ppc */
105 NULL
106 };
107
Masahiro Yamada516d9802020-09-23 02:48:56 +0900108 /* Symbol names that begin with the following are ignored.*/
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900109 static const char * const ignored_prefixes[] = {
Masahiro Yamada97261e12019-11-24 01:04:40 +0900110 "$", /* local symbols for ARM, MIPS, etc. */
111 ".LASANPC", /* s390 kasan local symbols */
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900112 "__crc_", /* modversions */
113 "__efistub_", /* arm64 EFI stub namespace */
Kalesh Singh7c00e812022-04-20 14:42:57 -0700114 "__kvm_nvhe_$", /* arm64 local symbols in non-VHE KVM namespace */
115 "__kvm_nvhe_.L", /* arm64 local symbols in non-VHE KVM namespace */
Arnd Bergmannefe6e302021-02-04 16:29:47 +0100116 "__AArch64ADRPThunk_", /* arm64 lld */
117 "__ARMV5PILongThunk_", /* arm lld */
118 "__ARMV7PILongThunk_",
119 "__ThumbV7PILongThunk_",
120 "__LA25Thunk_", /* mips lld */
121 "__microLA25Thunk_",
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900122 NULL
123 };
124
Masahiro Yamada516d9802020-09-23 02:48:56 +0900125 /* Symbol names that end with the following are ignored.*/
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900126 static const char * const ignored_suffixes[] = {
127 "_from_arm", /* arm */
128 "_from_thumb", /* arm */
129 "_veneer", /* arm */
130 NULL
131 };
132
Masahiro Yamada516d9802020-09-23 02:48:56 +0900133 /* Symbol names that contain the following are ignored.*/
134 static const char * const ignored_matches[] = {
135 ".long_branch.", /* ppc stub */
136 ".plt_branch.", /* ppc stub */
137 NULL
138 };
139
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900140 const char * const *p;
141
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900142 for (p = ignored_symbols; *p; p++)
143 if (!strcmp(name, *p))
144 return true;
145
146 for (p = ignored_prefixes; *p; p++)
147 if (!strncmp(name, *p, strlen(*p)))
148 return true;
149
150 for (p = ignored_suffixes; *p; p++) {
151 int l = strlen(name) - strlen(*p);
152
153 if (l >= 0 && !strcmp(name + l, *p))
154 return true;
155 }
156
Masahiro Yamada516d9802020-09-23 02:48:56 +0900157 for (p = ignored_matches; *p; p++) {
158 if (strstr(name, *p))
159 return true;
160 }
161
Masahiro Yamada887df762019-11-24 01:04:41 +0900162 if (type == 'U' || type == 'u')
163 return true;
164 /* exclude debugging symbols */
165 if (type == 'N' || type == 'n')
166 return true;
167
168 if (toupper(type) == 'A') {
169 /* Keep these useful absolute symbols */
170 if (strcmp(name, "__kernel_syscall_via_break") &&
171 strcmp(name, "__kernel_syscall_via_epc") &&
172 strcmp(name, "__kernel_sigtramp") &&
173 strcmp(name, "__gp"))
174 return true;
175 }
176
Masahiro Yamadaa41333e2019-11-24 01:04:39 +0900177 return false;
178}
179
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900180static void check_symbol_range(const char *sym, unsigned long long addr,
181 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400182{
183 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030184 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400185
Kees Cook78eb7152014-03-17 13:18:27 +1030186 for (i = 0; i < entries; ++i) {
187 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400188
Kees Cook78eb7152014-03-17 13:18:27 +1030189 if (strcmp(sym, ar->start_sym) == 0) {
190 ar->start = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900191 return;
Kees Cook78eb7152014-03-17 13:18:27 +1030192 } else if (strcmp(sym, ar->end_sym) == 0) {
193 ar->end = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900194 return;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400195 }
196 }
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400197}
198
Masahiro Yamada8d605262020-02-02 14:09:21 +0900199static struct sym_entry *read_symbol(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900201 char name[500], type;
Masahiro Yamada8d605262020-02-02 14:09:21 +0900202 unsigned long long addr;
203 unsigned int len;
204 struct sym_entry *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int rc;
206
Masahiro Yamada8d605262020-02-02 14:09:21 +0900207 rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (rc != 3) {
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900209 if (rc != EOF && fgets(name, 500, in) == NULL)
Jean Sacrenef894872010-09-10 23:13:33 -0600210 fprintf(stderr, "Read error or end of file.\n");
Masahiro Yamada8d605262020-02-02 14:09:21 +0900211 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900213 if (strlen(name) >= KSYM_NAME_LEN) {
Eugene Loh6db29832019-01-17 14:46:00 -0800214 fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).\n"
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900215 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900216 name, strlen(name), KSYM_NAME_LEN);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900217 return NULL;
Andi Kleenf3462aa2013-10-23 15:07:53 +0200218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Masahiro Yamadabe9f6132020-02-02 14:09:20 +0900220 if (strcmp(name, "_text") == 0)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900221 _text = addr;
Masahiro Yamadab6233d02019-11-24 01:04:42 +0900222
Mikhail Petrov7883a142020-03-11 23:37:09 +0300223 /* Ignore most absolute/undefined (?) symbols. */
224 if (is_ignored_symbol(name, type))
225 return NULL;
226
Masahiro Yamada8d605262020-02-02 14:09:21 +0900227 check_symbol_range(name, addr, text_ranges, ARRAY_SIZE(text_ranges));
228 check_symbol_range(name, addr, &percpu_range, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /* include the type field in the symbol name, so that it gets
231 * compressed together */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900232
233 len = strlen(name) + 1;
234
Masahiro Yamada9d1b3892020-02-11 01:18:52 +0900235 sym = malloc(sizeof(*sym) + len + 1);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900236 if (!sym) {
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800237 fprintf(stderr, "kallsyms failure: "
238 "unable to allocate required amount of memory\n");
239 exit(EXIT_FAILURE);
240 }
Masahiro Yamada8d605262020-02-02 14:09:21 +0900241 sym->addr = addr;
242 sym->len = len;
243 sym->sym[0] = type;
Masahiro Yamada9d1b3892020-02-11 01:18:52 +0900244 strcpy(sym_name(sym), name);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900245 sym->percpu_absolute = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Masahiro Yamada8d605262020-02-02 14:09:21 +0900247 return sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900250static int symbol_in_range(const struct sym_entry *s,
251 const struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400252{
253 size_t i;
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900254 const struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400255
Kees Cook78eb7152014-03-17 13:18:27 +1030256 for (i = 0; i < entries; ++i) {
257 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400258
Kees Cook78eb7152014-03-17 13:18:27 +1030259 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400260 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400261 }
262
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400263 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400264}
265
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900266static int symbol_valid(const struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900268 const char *name = sym_name(s);
Ard Biesheuvelbd8b22d2015-03-30 15:20:31 +0200269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 /* if --all-symbols is not specified, then symbols outside the text
271 * and inittext sections are discarded */
272 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030273 if (symbol_in_range(s, text_ranges,
274 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800277 * _etext _einittext; they can move between pass 1 and 2 when
278 * the kallsyms data are added. If these symbols move then
279 * they may get dropped in pass 2, which breaks the kallsyms
280 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400282 if ((s->addr == text_range_text->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900283 strcmp(name, text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400284 (s->addr == text_range_inittext->end &&
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900285 strcmp(name, text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 0;
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 1;
290}
291
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900292/* remove all the invalid symbols from the table */
293static void shrink_table(void)
294{
295 unsigned int i, pos;
296
297 pos = 0;
298 for (i = 0; i < table_cnt; i++) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900299 if (symbol_valid(table[i])) {
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900300 if (pos != i)
301 table[pos] = table[i];
302 pos++;
303 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900304 free(table[i]);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900305 }
306 }
307 table_cnt = pos;
308
309 /* When valid symbol is not registered, exit to error */
310 if (!table_cnt) {
311 fprintf(stderr, "No valid symbol.\n");
312 exit(1);
313 }
314}
315
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700316static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900318 struct sym_entry *sym;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 while (!feof(in)) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900321 sym = read_symbol(in);
322 if (!sym)
323 continue;
324
325 sym->start_pos = table_cnt;
326
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700327 if (table_cnt >= table_size) {
328 table_size += 10000;
329 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!table) {
331 fprintf(stderr, "out of memory\n");
332 exit (1);
333 }
334 }
Masahiro Yamada8d605262020-02-02 14:09:21 +0900335
336 table[table_cnt++] = sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338}
339
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900340static void output_label(const char *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900342 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 printf("\tALGN\n");
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900344 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900347/* Provide proper symbols relocatability by their '_text' relativeness. */
348static void output_address(unsigned long long addr)
349{
350 if (_text <= addr)
351 printf("\tPTR\t_text + %#llx\n", addr - _text);
352 else
353 printf("\tPTR\t_text - %#llx\n", _text - addr);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* uncompress a compressed symbol. When this function is called, the best table
357 * might still be compressed itself, so the function needs to be recursive */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900358static int expand_symbol(const unsigned char *data, int len, char *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 int c, rlen, total=0;
361
362 while (len) {
363 c = *data;
364 /* if the table holds a single char that is the same as the one
365 * we are looking for, then end the search */
366 if (best_table[c][0]==c && best_table_len[c]==1) {
367 *result++ = c;
368 total++;
369 } else {
370 /* if not, recurse and expand */
371 rlen = expand_symbol(best_table[c], best_table_len[c], result);
372 total += rlen;
373 result += rlen;
374 }
375 data++;
376 len--;
377 }
378 *result=0;
379
380 return total;
381}
382
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900383static int symbol_absolute(const struct sym_entry *s)
Kees Cook78eb7152014-03-17 13:18:27 +1030384{
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700385 return s->percpu_absolute;
Kees Cook78eb7152014-03-17 13:18:27 +1030386}
387
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700388static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700390 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned int best_idx[256];
392 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700393 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Masahiro Yamada500193e2019-02-04 10:53:18 +0900395 printf("#include <asm/bitsperlong.h>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 printf("#if BITS_PER_LONG == 64\n");
397 printf("#define PTR .quad\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100398 printf("#define ALGN .balign 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 printf("#else\n");
400 printf("#define PTR .long\n");
Mathias Krause72d3ebb2018-12-30 13:36:00 +0100401 printf("#define ALGN .balign 4\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 printf("#endif\n");
403
Jan Beulichaad09472006-12-08 02:35:57 -0800404 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700406 if (!base_relative)
407 output_label("kallsyms_addresses");
408 else
409 output_label("kallsyms_offsets");
410
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700411 for (i = 0; i < table_cnt; i++) {
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700412 if (base_relative) {
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900413 /*
414 * Use the offset relative to the lowest value
415 * encountered of all relative symbols, and emit
416 * non-relocatable fixed offsets that will be fixed
417 * up at runtime.
418 */
419
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700420 long long offset;
421 int overflow;
422
423 if (!absolute_percpu) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900424 offset = table[i]->addr - relative_base;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700425 overflow = (offset < 0 || offset > UINT_MAX);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900426 } else if (symbol_absolute(table[i])) {
427 offset = table[i]->addr;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700428 overflow = (offset < 0 || offset > INT_MAX);
429 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900430 offset = relative_base - table[i]->addr - 1;
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700431 overflow = (offset < INT_MIN || offset >= 0);
432 }
433 if (overflow) {
434 fprintf(stderr, "kallsyms failure: "
435 "%s symbol value %#llx out of range in relative mode\n",
Masahiro Yamada8d605262020-02-02 14:09:21 +0900436 symbol_absolute(table[i]) ? "absolute" : "relative",
437 table[i]->addr);
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700438 exit(EXIT_FAILURE);
439 }
440 printf("\t.long\t%#x\n", (int)offset);
Masahiro Yamada8d605262020-02-02 14:09:21 +0900441 } else if (!symbol_absolute(table[i])) {
442 output_address(table[i]->addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100443 } else {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900444 printf("\tPTR\t%#llx\n", table[i]->addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 printf("\n");
448
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700449 if (base_relative) {
450 output_label("kallsyms_relative_base");
Masahiro Yamadafd2ab2f2019-12-09 12:51:48 +0900451 output_address(relative_base);
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700452 printf("\n");
453 }
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 output_label("kallsyms_num_syms");
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600456 printf("\t.long\t%u\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 printf("\n");
458
459 /* table of offset markers, that give the offset in the compressed stream
460 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800461 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
462 if (!markers) {
463 fprintf(stderr, "kallsyms failure: "
464 "unable to allocate required memory\n");
465 exit(EXIT_FAILURE);
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700470 for (i = 0; i < table_cnt; i++) {
471 if ((i & 0xFF) == 0)
472 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Masahiro Yamada8d605262020-02-02 14:09:21 +0900474 printf("\t.byte 0x%02x", table[i]->len);
475 for (k = 0; k < table[i]->len; k++)
476 printf(", 0x%02x", table[i]->sym[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 printf("\n");
478
Masahiro Yamada8d605262020-02-02 14:09:21 +0900479 off += table[i]->len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 printf("\n");
482
483 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700484 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Jan Beulich80ffbaa2018-09-03 06:09:34 -0600485 printf("\t.long\t%u\n", markers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 printf("\n");
487
488 free(markers);
489
490 output_label("kallsyms_token_table");
491 off = 0;
492 for (i = 0; i < 256; i++) {
493 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700494 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 printf("\t.asciz\t\"%s\"\n", buf);
496 off += strlen(buf) + 1;
497 }
498 printf("\n");
499
500 output_label("kallsyms_token_index");
501 for (i = 0; i < 256; i++)
502 printf("\t.short\t%d\n", best_idx[i]);
503 printf("\n");
504}
505
506
507/* table lookup compression functions */
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/* count all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900510static void learn_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 int i;
513
514 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700515 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/* decrease the count for all the possible tokens in a symbol */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900519static void forget_symbol(const unsigned char *symbol, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 int i;
522
523 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700524 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900527/* do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static void build_initial_tok_table(void)
529{
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900530 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900532 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900533 learn_symbol(table[i]->sym, table[i]->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Masahiro Yamada2558c132019-11-24 01:04:37 +0900536static unsigned char *find_token(unsigned char *str, int len,
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900537 const unsigned char *token)
Paulo Marques7c5d2492007-06-20 18:09:00 +0100538{
539 int i;
540
541 for (i = 0; i < len - 1; i++) {
542 if (str[i] == token[0] && str[i+1] == token[1])
543 return &str[i];
544 }
545 return NULL;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/* replace a given token in all the valid symbols. Use the sampled symbols
549 * to update the counts */
Masahiro Yamada4bfe2b72019-11-24 01:04:38 +0900550static void compress_symbols(const unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700552 unsigned int i, len, size;
553 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700555 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Masahiro Yamada8d605262020-02-02 14:09:21 +0900557 len = table[i]->len;
558 p1 = table[i]->sym;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700559
560 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100561 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700562 if (!p2) continue;
563
564 /* decrease the counts for this symbol's tokens */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900565 forget_symbol(table[i]->sym, len);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700566
567 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700570 *p2 = idx;
571 p2++;
572 size -= (p2 - p1);
573 memmove(p2, p2 + 1, size);
574 p1 = p2;
575 len--;
576
577 if (size < 2) break;
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100580 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700582 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Masahiro Yamada8d605262020-02-02 14:09:21 +0900584 table[i]->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700586 /* increase the counts for this symbol's new tokens */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900587 learn_symbol(table[i]->sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589}
590
591/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700592static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700594 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700597 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700599 for (i = 0; i < 0x10000; i++) {
600 if (token_profit[i] > bestprofit) {
601 best = i;
602 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return best;
606}
607
608/* this is the core of the algorithm: calculate the "best" table */
609static void optimize_result(void)
610{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700611 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /* using the '\0' symbol last allows compress_symbols to use standard
614 * fast string functions */
615 for (i = 255; i >= 0; i--) {
616
617 /* if this table slot is empty (it is not used by an actual
618 * original char code */
619 if (!best_table_len[i]) {
620
Cao jincbf7a902018-02-27 16:16:19 +0800621 /* find the token with the best profit value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800623 if (token_profit[best] == 0)
624 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700627 best_table_len[i] = 2;
628 best_table[i][0] = best & 0xFF;
629 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700632 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634 }
635}
636
637/* start by placing the symbols that are actually used on the table */
638static void insert_real_symbols_in_table(void)
639{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700640 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700642 for (i = 0; i < table_cnt; i++) {
Masahiro Yamada8d605262020-02-02 14:09:21 +0900643 for (j = 0; j < table[i]->len; j++) {
644 c = table[i]->sym[j];
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700645 best_table[c][0]=c;
646 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648 }
649}
650
651static void optimize_token_table(void)
652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 build_initial_tok_table();
654
655 insert_real_symbols_in_table();
656
657 optimize_result();
658}
659
Lai Jiangshanb478b782009-03-13 15:10:26 +0800660/* guess for "linker script provide" symbol */
661static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
662{
Masahiro Yamada29e55ad2019-11-24 01:04:35 +0900663 const char *symbol = sym_name(se);
Lai Jiangshanb478b782009-03-13 15:10:26 +0800664 int len = se->len - 1;
665
666 if (len < 8)
667 return 0;
668
669 if (symbol[0] != '_' || symbol[1] != '_')
670 return 0;
671
672 /* __start_XXXXX */
673 if (!memcmp(symbol + 2, "start_", 6))
674 return 1;
675
676 /* __stop_XXXXX */
677 if (!memcmp(symbol + 2, "stop_", 5))
678 return 1;
679
680 /* __end_XXXXX */
681 if (!memcmp(symbol + 2, "end_", 4))
682 return 1;
683
684 /* __XXXXX_start */
685 if (!memcmp(symbol + len - 6, "_start", 6))
686 return 1;
687
688 /* __XXXXX_end */
689 if (!memcmp(symbol + len - 4, "_end", 4))
690 return 1;
691
692 return 0;
693}
694
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800695static int compare_symbols(const void *a, const void *b)
696{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900697 const struct sym_entry *sa = *(const struct sym_entry **)a;
698 const struct sym_entry *sb = *(const struct sym_entry **)b;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800699 int wa, wb;
700
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800701 /* sort by address first */
702 if (sa->addr > sb->addr)
703 return 1;
704 if (sa->addr < sb->addr)
705 return -1;
706
707 /* sort by "weakness" type */
708 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
709 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
710 if (wa != wb)
711 return wa - wb;
712
Lai Jiangshanb478b782009-03-13 15:10:26 +0800713 /* sort by "linker script provide" type */
714 wa = may_be_linker_script_provide_symbol(sa);
715 wb = may_be_linker_script_provide_symbol(sb);
716 if (wa != wb)
717 return wa - wb;
718
719 /* sort by the number of prefix underscores */
Masahiro Yamadaaa915242019-11-24 01:04:36 +0900720 wa = strspn(sym_name(sa), "_");
721 wb = strspn(sym_name(sb), "_");
Lai Jiangshanb478b782009-03-13 15:10:26 +0800722 if (wa != wb)
723 return wa - wb;
724
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800725 /* sort by initial order, so that other symbols are left undisturbed */
726 return sa->start_pos - sb->start_pos;
727}
728
729static void sort_symbols(void)
730{
Masahiro Yamada8d605262020-02-02 14:09:21 +0900731 qsort(table, table_cnt, sizeof(table[0]), compare_symbols);
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800732}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030734static void make_percpus_absolute(void)
735{
736 unsigned int i;
737
738 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900739 if (symbol_in_range(table[i], &percpu_range, 1)) {
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700740 /*
741 * Keep the 'A' override for percpu symbols to
742 * ensure consistent behavior compared to older
743 * versions of this tool.
744 */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900745 table[i]->sym[0] = 'A';
746 table[i]->percpu_absolute = 1;
Ard Biesheuvel8c996942016-03-15 14:58:15 -0700747 }
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030748}
749
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700750/* find the minimum non-absolute symbol address */
751static void record_relative_base(void)
752{
753 unsigned int i;
754
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700755 for (i = 0; i < table_cnt; i++)
Masahiro Yamada8d605262020-02-02 14:09:21 +0900756 if (!symbol_absolute(table[i])) {
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900757 /*
758 * The table is sorted by address.
759 * Take the first non-absolute symbol value.
760 */
Masahiro Yamada8d605262020-02-02 14:09:21 +0900761 relative_base = table[i]->addr;
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900762 return;
763 }
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700764}
765
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700766int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700768 if (argc >= 2) {
769 int i;
770 for (i = 1; i < argc; i++) {
771 if(strcmp(argv[i], "--all-symbols") == 0)
772 all_symbols = 1;
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030773 else if (strcmp(argv[i], "--absolute-percpu") == 0)
774 absolute_percpu = 1;
Masahiro Yamada534c9f22018-05-09 16:23:47 +0900775 else if (strcmp(argv[i], "--base-relative") == 0)
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700776 base_relative = 1;
777 else
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700778 usage();
779 }
780 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 usage();
782
783 read_map(stdin);
Masahiro Yamada5e5c4fa2019-11-24 01:04:31 +0900784 shrink_table();
Rusty Russellc6bda7c2014-03-17 14:05:46 +1030785 if (absolute_percpu)
786 make_percpus_absolute();
Masahiro Yamadaf34ea022019-11-24 01:04:32 +0900787 sort_symbols();
Ard Biesheuvel2213e9a2016-03-15 14:58:19 -0700788 if (base_relative)
789 record_relative_base();
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100790 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 write_src();
792
793 return 0;
794}