Blame view

scripts/kallsyms.c 18.1 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
  /* Generate assembler source containing symbol information
   *
   * Copyright 2002       by Kai Germaschewski
   *
   * This software may be used and distributed according to the terms
   * of the GNU General Public License, incorporated herein by reference.
   *
   * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   *      Table compression uses all the unused char codes on the symbols and
   *  maps these to the most used substrings (tokens). For instance, it might
   *  map char code 0xF7 to represent "write_" and then in every symbol where
   *  "write_" appears it can be replaced by 0xF7, saving 5 bytes.
   *      The used codes themselves are also placed in the table so that the
   *  decompresion can work without "special cases".
   *      Applied to kernel symbols, this usually produces a compression ratio
   *  of about 50%.
   *
   */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <ctype.h>
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
25
  #include <limits.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26

17b1f0de7   Mike Frysinger   kallsyms: general...
27
28
29
  #ifndef ARRAY_SIZE
  #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
  #endif
9281acea6   Tejun Heo   kallsyms: make KS...
30
  #define KSYM_NAME_LEN		128
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
  struct sym_entry {
  	unsigned long long addr;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
34
  	unsigned int len;
f2df3f65d   Paulo Marques   kallsyms should p...
35
  	unsigned int start_pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
  	unsigned char *sym;
8c996940b   Ard Biesheuvel   kallsyms: don't o...
37
  	unsigned int percpu_absolute;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  };
78eb71594   Kees Cook   kallsyms: general...
39
40
  struct addr_range {
  	const char *start_sym, *end_sym;
17b1f0de7   Mike Frysinger   kallsyms: general...
41
42
43
44
  	unsigned long long start, end;
  };
  
  static unsigned long long _text;
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
45
  static unsigned long long relative_base;
78eb71594   Kees Cook   kallsyms: general...
46
  static struct addr_range text_ranges[] = {
17b1f0de7   Mike Frysinger   kallsyms: general...
47
48
49
50
51
52
53
  	{ "_stext",     "_etext"     },
  	{ "_sinittext", "_einittext" },
  	{ "_stext_l1",  "_etext_l1"  },	/* Blackfin on-chip L1 inst SRAM */
  	{ "_stext_l2",  "_etext_l2"  },	/* Blackfin on-chip L2 SRAM */
  };
  #define text_range_text     (&text_ranges[0])
  #define text_range_inittext (&text_ranges[1])
c6bda7c98   Rusty Russell   kallsyms: fix per...
54
55
56
  static struct addr_range percpu_range = {
  	"__per_cpu_start", "__per_cpu_end", -1ULL, 0
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
  static struct sym_entry *table;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
58
  static unsigned int table_size, table_cnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  static int all_symbols = 0;
c6bda7c98   Rusty Russell   kallsyms: fix per...
60
  static int absolute_percpu = 0;
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
61
  static int base_relative = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62

b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
63
  int token_profit[0x10000];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
  
  /* the table that holds the result of the compression */
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
66
  unsigned char best_table[256][2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  unsigned char best_table_len[256];
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
68
  static void usage(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
  {
f6537f2f0   Ming Lei   scripts/kallsyms:...
70
  	fprintf(stderr, "Usage: kallsyms [--all-symbols] "
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
71
72
  			"[--base-relative] < in.map > out.S
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
78
79
  	exit(1);
  }
  
  /*
   * This ignores the intensely annoying "mapping symbols" found
   * in ARM ELF files: $a, $t and $d.
   */
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
80
  static inline int is_arm_mapping_symbol(const char *str)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
  {
6c34f1f54   Kyle McMartin   aarch64: filter $...
82
  	return str[0] == '$' && strchr("axtd", str[1])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
  	       && (str[2] == '\0' || str[2] == '.');
  }
78eb71594   Kees Cook   kallsyms: general...
85
86
  static int check_symbol_range(const char *sym, unsigned long long addr,
  			      struct addr_range *ranges, int entries)
17b1f0de7   Mike Frysinger   kallsyms: general...
87
88
  {
  	size_t i;
78eb71594   Kees Cook   kallsyms: general...
89
  	struct addr_range *ar;
17b1f0de7   Mike Frysinger   kallsyms: general...
90

78eb71594   Kees Cook   kallsyms: general...
91
92
  	for (i = 0; i < entries; ++i) {
  		ar = &ranges[i];
17b1f0de7   Mike Frysinger   kallsyms: general...
93

78eb71594   Kees Cook   kallsyms: general...
94
95
  		if (strcmp(sym, ar->start_sym) == 0) {
  			ar->start = addr;
17b1f0de7   Mike Frysinger   kallsyms: general...
96
  			return 0;
78eb71594   Kees Cook   kallsyms: general...
97
98
  		} else if (strcmp(sym, ar->end_sym) == 0) {
  			ar->end = addr;
17b1f0de7   Mike Frysinger   kallsyms: general...
99
100
101
102
103
104
  			return 0;
  		}
  	}
  
  	return 1;
  }
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
105
  static int read_symbol(FILE *in, struct sym_entry *s)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  {
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
107
  	char sym[500], stype;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
  	int rc;
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
109
110
  	rc = fscanf(in, "%llx %c %499s
  ", &s->addr, &stype, sym);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  	if (rc != 3) {
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
112
  		if (rc != EOF && fgets(sym, 500, in) == NULL)
ef894870c   Jean Sacren   scripts/kallsyms:...
113
114
  			fprintf(stderr, "Read error or end of file.
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
  		return -1;
  	}
cacf3c0d8   Eugene Loh   kallsyms: Handle ...
117
118
119
  	if (strlen(sym) >= KSYM_NAME_LEN) {
  		fprintf(stderr, "Symbol %s too long for kallsyms (%zu >= %d).
  "
bb66fc671   Masahiro Yamada   kbuild: trivial -...
120
121
  				"Please increase KSYM_NAME_LEN both in kernel and kallsyms.c
  ",
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
122
  			sym, strlen(sym), KSYM_NAME_LEN);
f3462aa95   Andi Kleen   Kbuild: Handle lo...
123
124
  		return -1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
  
  	/* Ignore most absolute/undefined (?) symbols. */
fd593d127   Eric W. Biederman   [PATCH] relocatab...
127
128
  	if (strcmp(sym, "_text") == 0)
  		_text = s->addr;
78eb71594   Kees Cook   kallsyms: general...
129
130
  	else if (check_symbol_range(sym, s->addr, text_ranges,
  				    ARRAY_SIZE(text_ranges)) == 0)
17b1f0de7   Mike Frysinger   kallsyms: general...
131
  		/* nothing to do */;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
132
  	else if (toupper(stype) == 'A')
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
  	{
  		/* Keep these useful absolute symbols */
41f11a4fa   Yoshinori Sato   [PATCH] kallsyms ...
135
136
137
138
  		if (strcmp(sym, "__kernel_syscall_via_break") &&
  		    strcmp(sym, "__kernel_syscall_via_epc") &&
  		    strcmp(sym, "__kernel_sigtramp") &&
  		    strcmp(sym, "__gp"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
  			return -1;
  
  	}
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
142
  	else if (toupper(stype) == 'U' ||
41f11a4fa   Yoshinori Sato   [PATCH] kallsyms ...
143
  		 is_arm_mapping_symbol(sym))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
144
  		return -1;
6f00df24e   Ralf Baechle   [PATCH] Strip loc...
145
  	/* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
146
  	else if (sym[0] == '$')
6f00df24e   Ralf Baechle   [PATCH] Strip loc...
147
  		return -1;
aab34ac85   Sam Ravnborg   kbuild: filter aw...
148
  	/* exclude debugging symbols */
51962a9d4   Guenter Roeck   scripts/kallsyms....
149
  	else if (stype == 'N' || stype == 'n')
aab34ac85   Sam Ravnborg   kbuild: filter aw...
150
  		return -1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
153
  
  	/* include the type field in the symbol name, so that it gets
  	 * compressed together */
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
154
  	s->len = strlen(sym) + 1;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
155
  	s->sym = malloc(s->len + 1);
f1a136e0d   Jesper Juhl   [PATCH] kallsyms:...
156
157
158
159
160
161
  	if (!s->sym) {
  		fprintf(stderr, "kallsyms failure: "
  			"unable to allocate required amount of memory
  ");
  		exit(EXIT_FAILURE);
  	}
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
162
  	strcpy((char *)s->sym + 1, sym);
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
163
  	s->sym[0] = stype;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164

8c996940b   Ard Biesheuvel   kallsyms: don't o...
165
  	s->percpu_absolute = 0;
c6bda7c98   Rusty Russell   kallsyms: fix per...
166
167
  	/* Record if we've found __per_cpu_start/end. */
  	check_symbol_range(sym, s->addr, &percpu_range, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
  	return 0;
  }
78eb71594   Kees Cook   kallsyms: general...
170
171
  static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
  			   int entries)
17b1f0de7   Mike Frysinger   kallsyms: general...
172
173
  {
  	size_t i;
78eb71594   Kees Cook   kallsyms: general...
174
  	struct addr_range *ar;
17b1f0de7   Mike Frysinger   kallsyms: general...
175

78eb71594   Kees Cook   kallsyms: general...
176
177
  	for (i = 0; i < entries; ++i) {
  		ar = &ranges[i];
17b1f0de7   Mike Frysinger   kallsyms: general...
178

78eb71594   Kees Cook   kallsyms: general...
179
  		if (s->addr >= ar->start && s->addr <= ar->end)
ac6ca5c86   Mike Frysinger   kallsyms: fix inv...
180
  			return 1;
17b1f0de7   Mike Frysinger   kallsyms: general...
181
  	}
ac6ca5c86   Mike Frysinger   kallsyms: fix inv...
182
  	return 0;
17b1f0de7   Mike Frysinger   kallsyms: general...
183
  }
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
184
  static int symbol_valid(struct sym_entry *s)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
  {
  	/* Symbols which vary between passes.  Passes 1 and 2 must have
2ea038917   Sam Ravnborg   Revert "kbuild: s...
187
188
189
  	 * identical symbol lists.  The kallsyms_* symbols below are only added
  	 * after pass 1, they would be included in pass 2 when --all-symbols is
  	 * specified so exclude them to get a stable symbol list.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
  	 */
  	static char *special_symbols[] = {
2ea038917   Sam Ravnborg   Revert "kbuild: s...
192
  		"kallsyms_addresses",
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
193
194
  		"kallsyms_offsets",
  		"kallsyms_relative_base",
2ea038917   Sam Ravnborg   Revert "kbuild: s...
195
196
197
198
199
  		"kallsyms_num_syms",
  		"kallsyms_names",
  		"kallsyms_markers",
  		"kallsyms_token_table",
  		"kallsyms_token_index",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
202
203
  	/* Exclude linker generated symbols which vary between passes */
  		"_SDA_BASE_",		/* ppc */
  		"_SDA2_BASE_",		/* ppc */
  		NULL };
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
204

56067812d   Ard Biesheuvel   kbuild: modversio...
205
206
  	static char *special_prefixes[] = {
  		"__crc_",		/* modversions */
1212f7a16   Ard Biesheuvel   scripts/kallsyms:...
207
  		"__efistub_",		/* arm64 EFI stub namespace */
56067812d   Ard Biesheuvel   kbuild: modversio...
208
  		NULL };
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
209
  	static char *special_suffixes[] = {
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
210
  		"_veneer",		/* arm */
b9b74be16   Ard Biesheuvel   ARM: 8555/1: kall...
211
212
  		"_from_arm",		/* arm */
  		"_from_thumb",		/* arm */
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
213
  		NULL };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  	int i;
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
215
  	char *sym_name = (char *)s->sym + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
217
218
  	/* if --all-symbols is not specified, then symbols outside the text
  	 * and inittext sections are discarded */
  	if (!all_symbols) {
78eb71594   Kees Cook   kallsyms: general...
219
220
  		if (symbol_in_range(s, text_ranges,
  				    ARRAY_SIZE(text_ranges)) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
222
  			return 0;
  		/* Corner case.  Discard any symbols with the same value as
a3b81113f   Robin Getz   remove support fo...
223
224
225
226
  		 * _etext _einittext; they can move between pass 1 and 2 when
  		 * the kallsyms data are added.  If these symbols move then
  		 * they may get dropped in pass 2, which breaks the kallsyms
  		 * rules.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  		 */
17b1f0de7   Mike Frysinger   kallsyms: general...
228
  		if ((s->addr == text_range_text->end &&
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
229
  				strcmp(sym_name,
78eb71594   Kees Cook   kallsyms: general...
230
  				       text_range_text->end_sym)) ||
17b1f0de7   Mike Frysinger   kallsyms: general...
231
  		    (s->addr == text_range_inittext->end &&
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
232
  				strcmp(sym_name,
78eb71594   Kees Cook   kallsyms: general...
233
  				       text_range_inittext->end_sym)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
236
237
  			return 0;
  	}
  
  	/* Exclude symbols which vary between passes. */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
238
  	for (i = 0; special_symbols[i]; i++)
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
239
  		if (strcmp(sym_name, special_symbols[i]) == 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  			return 0;
56067812d   Ard Biesheuvel   kbuild: modversio...
241
242
243
244
245
246
247
  	for (i = 0; special_prefixes[i]; i++) {
  		int l = strlen(special_prefixes[i]);
  
  		if (l <= strlen(sym_name) &&
  		    strncmp(sym_name, special_prefixes[i], l) == 0)
  			return 0;
  	}
bd8b22d28   Ard Biesheuvel   Kbuild: kallsyms:...
248
249
250
251
252
253
  	for (i = 0; special_suffixes[i]; i++) {
  		int l = strlen(sym_name) - strlen(special_suffixes[i]);
  
  		if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0)
  			return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
  	return 1;
  }
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
256
  static void read_map(FILE *in)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
258
  {
  	while (!feof(in)) {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
259
260
261
  		if (table_cnt >= table_size) {
  			table_size += 10000;
  			table = realloc(table, sizeof(*table) * table_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
264
265
266
267
  			if (!table) {
  				fprintf(stderr, "out of memory
  ");
  				exit (1);
  			}
  		}
f2df3f65d   Paulo Marques   kallsyms should p...
268
269
  		if (read_symbol(in, &table[table_cnt]) == 0) {
  			table[table_cnt].start_pos = table_cnt;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
270
  			table_cnt++;
f2df3f65d   Paulo Marques   kallsyms should p...
271
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
272
273
274
275
276
  	}
  }
  
  static void output_label(char *label)
  {
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
277
278
  	printf(".globl %s
  ", label);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
  	printf("\tALGN
  ");
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
281
282
  	printf("%s:
  ", label);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  }
  
  /* uncompress a compressed symbol. When this function is called, the best table
   * might still be compressed itself, so the function needs to be recursive */
  static int expand_symbol(unsigned char *data, int len, char *result)
  {
  	int c, rlen, total=0;
  
  	while (len) {
  		c = *data;
  		/* if the table holds a single char that is the same as the one
  		 * we are looking for, then end the search */
  		if (best_table[c][0]==c && best_table_len[c]==1) {
  			*result++ = c;
  			total++;
  		} else {
  			/* if not, recurse and expand */
  			rlen = expand_symbol(best_table[c], best_table_len[c], result);
  			total += rlen;
  			result += rlen;
  		}
  		data++;
  		len--;
  	}
  	*result=0;
  
  	return total;
  }
78eb71594   Kees Cook   kallsyms: general...
311
312
  static int symbol_absolute(struct sym_entry *s)
  {
8c996940b   Ard Biesheuvel   kallsyms: don't o...
313
  	return s->percpu_absolute;
78eb71594   Kees Cook   kallsyms: general...
314
  }
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
315
  static void write_src(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
316
  {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
317
  	unsigned int i, k, off;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
318
319
  	unsigned int best_idx[256];
  	unsigned int *markers;
9281acea6   Tejun Heo   kallsyms: make KS...
320
  	char buf[KSYM_NAME_LEN];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  
  	printf("#include <asm/types.h>
  ");
  	printf("#if BITS_PER_LONG == 64
  ");
  	printf("#define PTR .quad
  ");
  	printf("#define ALGN .align 8
  ");
  	printf("#else
  ");
  	printf("#define PTR .long
  ");
  	printf("#define ALGN .align 4
  ");
  	printf("#endif
  ");
aad094701   Jan Beulich   [PATCH] move kall...
338
339
  	printf("\t.section .rodata, \"a\"
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340

2213e9a66   Ard Biesheuvel   kallsyms: add sup...
341
342
343
344
345
346
347
348
349
350
351
352
  	/* Provide proper symbols relocatability by their relativeness
  	 * to a fixed anchor point in the runtime image, either '_text'
  	 * for absolute address tables, in which case the linker will
  	 * emit the final addresses at build time. Otherwise, use the
  	 * offset relative to the lowest value encountered of all relative
  	 * symbols, and emit non-relocatable fixed offsets that will be fixed
  	 * up at runtime.
  	 *
  	 * The symbol names cannot be used to construct normal symbol
  	 * references as the list of symbols contains symbols that are
  	 * declared static and are private to their .o files.  This prevents
  	 * .tmp_kallsyms.o or any other object from referencing them.
fd593d127   Eric W. Biederman   [PATCH] relocatab...
353
  	 */
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
354
355
356
357
  	if (!base_relative)
  		output_label("kallsyms_addresses");
  	else
  		output_label("kallsyms_offsets");
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
358
  	for (i = 0; i < table_cnt; i++) {
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  		if (base_relative) {
  			long long offset;
  			int overflow;
  
  			if (!absolute_percpu) {
  				offset = table[i].addr - relative_base;
  				overflow = (offset < 0 || offset > UINT_MAX);
  			} else if (symbol_absolute(&table[i])) {
  				offset = table[i].addr;
  				overflow = (offset < 0 || offset > INT_MAX);
  			} else {
  				offset = relative_base - table[i].addr - 1;
  				overflow = (offset < INT_MIN || offset >= 0);
  			}
  			if (overflow) {
  				fprintf(stderr, "kallsyms failure: "
  					"%s symbol value %#llx out of range in relative mode
  ",
  					symbol_absolute(&table[i]) ? "absolute" : "relative",
  					table[i].addr);
  				exit(EXIT_FAILURE);
  			}
  			printf("\t.long\t%#x
  ", (int)offset);
  		} else if (!symbol_absolute(&table[i])) {
2c22d8baa   Vivek Goyal   [PATCH] relocatab...
384
385
386
387
388
  			if (_text <= table[i].addr)
  				printf("\tPTR\t_text + %#llx
  ",
  					table[i].addr - _text);
  			else
2930ffc75   Andrew Morton   revert "kallsyms:...
389
390
391
  				printf("\tPTR\t_text - %#llx
  ",
  					_text - table[i].addr);
fd593d127   Eric W. Biederman   [PATCH] relocatab...
392
393
394
395
  		} else {
  			printf("\tPTR\t%#llx
  ", table[i].addr);
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
398
  	}
  	printf("
  ");
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
399
400
401
402
403
404
405
  	if (base_relative) {
  		output_label("kallsyms_relative_base");
  		printf("\tPTR\t_text - %#llx
  ", _text - relative_base);
  		printf("
  ");
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
406
  	output_label("kallsyms_num_syms");
ac5db1fc8   nixiaoming   scripts: Fixed pr...
407
408
  	printf("\tPTR\t%u
  ", table_cnt);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
409
410
411
412
413
  	printf("
  ");
  
  	/* table of offset markers, that give the offset in the compressed stream
  	 * every 256 symbols */
f1a136e0d   Jesper Juhl   [PATCH] kallsyms:...
414
415
416
417
418
419
420
  	markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
  	if (!markers) {
  		fprintf(stderr, "kallsyms failure: "
  			"unable to allocate required memory
  ");
  		exit(EXIT_FAILURE);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
  
  	output_label("kallsyms_names");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  	off = 0;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
424
425
426
  	for (i = 0; i < table_cnt; i++) {
  		if ((i & 0xFF) == 0)
  			markers[i >> 8] = off;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
427
428
429
430
431
432
433
434
  
  		printf("\t.byte 0x%02x", table[i].len);
  		for (k = 0; k < table[i].len; k++)
  			printf(", 0x%02x", table[i].sym[k]);
  		printf("
  ");
  
  		off += table[i].len + 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
435
436
437
438
439
  	}
  	printf("
  ");
  
  	output_label("kallsyms_markers");
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
440
  	for (i = 0; i < ((table_cnt + 255) >> 8); i++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441
442
443
444
445
446
447
448
449
450
451
  		printf("\tPTR\t%d
  ", markers[i]);
  	printf("
  ");
  
  	free(markers);
  
  	output_label("kallsyms_token_table");
  	off = 0;
  	for (i = 0; i < 256; i++) {
  		best_idx[i] = off;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
452
  		expand_symbol(best_table[i], best_table_len[i], buf);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
  		printf("\t.asciz\t\"%s\"
  ", buf);
  		off += strlen(buf) + 1;
  	}
  	printf("
  ");
  
  	output_label("kallsyms_token_index");
  	for (i = 0; i < 256; i++)
  		printf("\t.short\t%d
  ", best_idx[i]);
  	printf("
  ");
  }
  
  
  /* table lookup compression functions */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
474
475
  /* count all the possible tokens in a symbol */
  static void learn_symbol(unsigned char *symbol, int len)
  {
  	int i;
  
  	for (i = 0; i < len - 1; i++)
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
476
  		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
478
479
480
481
482
483
484
  }
  
  /* decrease the count for all the possible tokens in a symbol */
  static void forget_symbol(unsigned char *symbol, int len)
  {
  	int i;
  
  	for (i = 0; i < len - 1; i++)
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
485
  		token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
486
  }
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
487
  /* remove all the invalid symbols from the table and do the initial token count */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
488
489
  static void build_initial_tok_table(void)
  {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
490
  	unsigned int i, pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491

b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
492
493
  	pos = 0;
  	for (i = 0; i < table_cnt; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
494
  		if ( symbol_valid(&table[i]) ) {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
495
496
497
498
  			if (pos != i)
  				table[pos] = table[i];
  			learn_symbol(table[pos].sym, table[pos].len);
  			pos++;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
  	}
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
501
  	table_cnt = pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502
  }
7c5d249ad   Paulo Marques   kallsyms: remove ...
503
504
505
506
507
508
509
510
511
512
  static void *find_token(unsigned char *str, int len, unsigned char *token)
  {
  	int i;
  
  	for (i = 0; i < len - 1; i++) {
  		if (str[i] == token[0] && str[i+1] == token[1])
  			return &str[i];
  	}
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
514
  /* replace a given token in all the valid symbols. Use the sampled symbols
   * to update the counts */
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
515
  static void compress_symbols(unsigned char *str, int idx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
516
  {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
517
518
  	unsigned int i, len, size;
  	unsigned char *p1, *p2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519

b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
520
  	for (i = 0; i < table_cnt; i++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
521
522
  
  		len = table[i].len;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
523
524
525
  		p1 = table[i].sym;
  
  		/* find the token on the symbol */
7c5d249ad   Paulo Marques   kallsyms: remove ...
526
  		p2 = find_token(p1, len, str);
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
527
528
529
530
531
532
  		if (!p2) continue;
  
  		/* decrease the counts for this symbol's tokens */
  		forget_symbol(table[i].sym, len);
  
  		size = len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
533
534
  
  		do {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
535
536
537
538
539
540
541
542
  			*p2 = idx;
  			p2++;
  			size -= (p2 - p1);
  			memmove(p2, p2 + 1, size);
  			p1 = p2;
  			len--;
  
  			if (size < 2) break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
  			/* find the token on the symbol */
7c5d249ad   Paulo Marques   kallsyms: remove ...
544
  			p2 = find_token(p1, size, str);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545

b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
546
  		} while (p2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547

b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
548
  		table[i].len = len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549

b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
550
551
  		/* increase the counts for this symbol's new tokens */
  		learn_symbol(table[i].sym, len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
553
554
555
  	}
  }
  
  /* search the token with the maximum profit */
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
556
  static int find_best_token(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
557
  {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
558
  	int i, best, bestprofit;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
560
  
  	bestprofit=-10000;
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
561
  	best = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
562

b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
563
564
565
566
  	for (i = 0; i < 0x10000; i++) {
  		if (token_profit[i] > bestprofit) {
  			best = i;
  			bestprofit = token_profit[i];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
567
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
568
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
571
572
573
574
  	return best;
  }
  
  /* this is the core of the algorithm: calculate the "best" table */
  static void optimize_result(void)
  {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
575
  	int i, best;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
576
577
578
579
580
581
582
583
  
  	/* using the '\0' symbol last allows compress_symbols to use standard
  	 * fast string functions */
  	for (i = 255; i >= 0; i--) {
  
  		/* if this table slot is empty (it is not used by an actual
  		 * original char code */
  		if (!best_table_len[i]) {
cbf7a90e3   Cao jin   kbuild/kallsyms: ...
584
  			/* find the token with the best profit value */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
585
  			best = find_best_token();
e0a04b11e   Xiaochen Wang   scripts/kallsyms....
586
587
  			if (token_profit[best] == 0)
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
589
  
  			/* place it in the "best" table */
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
590
591
592
  			best_table_len[i] = 2;
  			best_table[i][0] = best & 0xFF;
  			best_table[i][1] = (best >> 8) & 0xFF;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
594
  
  			/* replace this token in all the valid symbols */
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
595
  			compress_symbols(best_table[i], i);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
596
597
598
599
600
601
602
  		}
  	}
  }
  
  /* start by placing the symbols that are actually used on the table */
  static void insert_real_symbols_in_table(void)
  {
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
603
  	unsigned int i, j, c;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
604
605
606
  
  	memset(best_table, 0, sizeof(best_table));
  	memset(best_table_len, 0, sizeof(best_table_len));
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
607
608
609
610
611
  	for (i = 0; i < table_cnt; i++) {
  		for (j = 0; j < table[i].len; j++) {
  			c = table[i].sym[j];
  			best_table[c][0]=c;
  			best_table_len[c]=1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
616
617
  		}
  	}
  }
  
  static void optimize_token_table(void)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
618
619
620
  	build_initial_tok_table();
  
  	insert_real_symbols_in_table();
41f11a4fa   Yoshinori Sato   [PATCH] kallsyms ...
621
  	/* When valid symbol is not registered, exit to error */
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
622
  	if (!table_cnt) {
41f11a4fa   Yoshinori Sato   [PATCH] kallsyms ...
623
624
625
626
  		fprintf(stderr, "No valid symbol.
  ");
  		exit(1);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
627
628
  	optimize_result();
  }
b478b782e   Lai Jiangshan   kallsyms, tracing...
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
  /* guess for "linker script provide" symbol */
  static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
  {
  	const char *symbol = (char *)se->sym + 1;
  	int len = se->len - 1;
  
  	if (len < 8)
  		return 0;
  
  	if (symbol[0] != '_' || symbol[1] != '_')
  		return 0;
  
  	/* __start_XXXXX */
  	if (!memcmp(symbol + 2, "start_", 6))
  		return 1;
  
  	/* __stop_XXXXX */
  	if (!memcmp(symbol + 2, "stop_", 5))
  		return 1;
  
  	/* __end_XXXXX */
  	if (!memcmp(symbol + 2, "end_", 4))
  		return 1;
  
  	/* __XXXXX_start */
  	if (!memcmp(symbol + len - 6, "_start", 6))
  		return 1;
  
  	/* __XXXXX_end */
  	if (!memcmp(symbol + len - 4, "_end", 4))
  		return 1;
  
  	return 0;
  }
  
  static int prefix_underscores_count(const char *str)
  {
  	const char *tail = str;
a9ece53c4   Paul Mundt   kallsyms: fix seg...
667
  	while (*tail == '_')
b478b782e   Lai Jiangshan   kallsyms, tracing...
668
669
670
671
  		tail++;
  
  	return tail - str;
  }
f2df3f65d   Paulo Marques   kallsyms should p...
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
  static int compare_symbols(const void *a, const void *b)
  {
  	const struct sym_entry *sa;
  	const struct sym_entry *sb;
  	int wa, wb;
  
  	sa = a;
  	sb = b;
  
  	/* sort by address first */
  	if (sa->addr > sb->addr)
  		return 1;
  	if (sa->addr < sb->addr)
  		return -1;
  
  	/* sort by "weakness" type */
  	wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
  	wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
  	if (wa != wb)
  		return wa - wb;
b478b782e   Lai Jiangshan   kallsyms, tracing...
692
693
694
695
696
697
698
699
700
701
702
  	/* sort by "linker script provide" type */
  	wa = may_be_linker_script_provide_symbol(sa);
  	wb = may_be_linker_script_provide_symbol(sb);
  	if (wa != wb)
  		return wa - wb;
  
  	/* sort by the number of prefix underscores */
  	wa = prefix_underscores_count((const char *)sa->sym + 1);
  	wb = prefix_underscores_count((const char *)sb->sym + 1);
  	if (wa != wb)
  		return wa - wb;
f2df3f65d   Paulo Marques   kallsyms should p...
703
704
705
706
707
708
709
710
  	/* sort by initial order, so that other symbols are left undisturbed */
  	return sa->start_pos - sb->start_pos;
  }
  
  static void sort_symbols(void)
  {
  	qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711

c6bda7c98   Rusty Russell   kallsyms: fix per...
712
713
714
715
716
  static void make_percpus_absolute(void)
  {
  	unsigned int i;
  
  	for (i = 0; i < table_cnt; i++)
8c996940b   Ard Biesheuvel   kallsyms: don't o...
717
718
719
720
721
722
  		if (symbol_in_range(&table[i], &percpu_range, 1)) {
  			/*
  			 * Keep the 'A' override for percpu symbols to
  			 * ensure consistent behavior compared to older
  			 * versions of this tool.
  			 */
c6bda7c98   Rusty Russell   kallsyms: fix per...
723
  			table[i].sym[0] = 'A';
8c996940b   Ard Biesheuvel   kallsyms: don't o...
724
725
  			table[i].percpu_absolute = 1;
  		}
c6bda7c98   Rusty Russell   kallsyms: fix per...
726
  }
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
727
728
729
730
731
732
733
734
735
736
737
  /* find the minimum non-absolute symbol address */
  static void record_relative_base(void)
  {
  	unsigned int i;
  
  	relative_base = -1ULL;
  	for (i = 0; i < table_cnt; i++)
  		if (!symbol_absolute(&table[i]) &&
  		    table[i].addr < relative_base)
  			relative_base = table[i].addr;
  }
b3dbb4ecd   Paulo Marques   [PATCH] kallsyms:...
738
  int main(int argc, char **argv)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739
  {
41f11a4fa   Yoshinori Sato   [PATCH] kallsyms ...
740
741
742
743
744
  	if (argc >= 2) {
  		int i;
  		for (i = 1; i < argc; i++) {
  			if(strcmp(argv[i], "--all-symbols") == 0)
  				all_symbols = 1;
c6bda7c98   Rusty Russell   kallsyms: fix per...
745
746
  			else if (strcmp(argv[i], "--absolute-percpu") == 0)
  				absolute_percpu = 1;
534c9f2ec   Masahiro Yamada   kallsyms: remove ...
747
  			else if (strcmp(argv[i], "--base-relative") == 0)
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
748
749
  				base_relative = 1;
  			else
41f11a4fa   Yoshinori Sato   [PATCH] kallsyms ...
750
751
752
  				usage();
  		}
  	} else if (argc != 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
754
755
  		usage();
  
  	read_map(stdin);
c6bda7c98   Rusty Russell   kallsyms: fix per...
756
757
  	if (absolute_percpu)
  		make_percpus_absolute();
2213e9a66   Ard Biesheuvel   kallsyms: add sup...
758
759
  	if (base_relative)
  		record_relative_base();
2ea038917   Sam Ravnborg   Revert "kbuild: s...
760
761
  	sort_symbols();
  	optimize_token_table();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
763
764
765
  	write_src();
  
  	return 0;
  }