blob: 3df15f5e7c55384868448a9cdd7493a5ce4aa316 [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
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <ctype.h>
25
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040026#ifndef ARRAY_SIZE
27#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
28#endif
29
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;
37};
38
Kees Cook78eb7152014-03-17 13:18:27 +103039struct addr_range {
40 const char *start_sym, *end_sym;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040041 unsigned long long start, end;
42};
43
44static unsigned long long _text;
Kees Cook78eb7152014-03-17 13:18:27 +103045static struct addr_range text_ranges[] = {
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040046 { "_stext", "_etext" },
47 { "_sinittext", "_einittext" },
48 { "_stext_l1", "_etext_l1" }, /* Blackfin on-chip L1 inst SRAM */
49 { "_stext_l2", "_etext_l2" }, /* Blackfin on-chip L2 SRAM */
50};
51#define text_range_text (&text_ranges[0])
52#define text_range_inittext (&text_ranges[1])
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct sym_entry *table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070055static unsigned int table_size, table_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static int all_symbols = 0;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070057static char symbol_prefix_char = '\0';
Ming Leif6537f22013-11-02 09:11:33 +103058static unsigned long long kernel_start_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070060int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* the table that holds the result of the compression */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070063unsigned char best_table[256][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064unsigned char best_table_len[256];
65
66
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070067static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Ming Leif6537f22013-11-02 09:11:33 +103069 fprintf(stderr, "Usage: kallsyms [--all-symbols] "
70 "[--symbol-prefix=<prefix char>] "
71 "[--page-offset=<CONFIG_PAGE_OFFSET>] "
72 "< in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 exit(1);
74}
75
76/*
77 * This ignores the intensely annoying "mapping symbols" found
78 * in ARM ELF files: $a, $t and $d.
79 */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070080static inline int is_arm_mapping_symbol(const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 return str[0] == '$' && strchr("atd", str[1])
83 && (str[2] == '\0' || str[2] == '.');
84}
85
Kees Cook78eb7152014-03-17 13:18:27 +103086static int check_symbol_range(const char *sym, unsigned long long addr,
87 struct addr_range *ranges, int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040088{
89 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +103090 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040091
Kees Cook78eb7152014-03-17 13:18:27 +103092 for (i = 0; i < entries; ++i) {
93 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040094
Kees Cook78eb7152014-03-17 13:18:27 +103095 if (strcmp(sym, ar->start_sym) == 0) {
96 ar->start = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -040097 return 0;
Kees Cook78eb7152014-03-17 13:18:27 +103098 } else if (strcmp(sym, ar->end_sym) == 0) {
99 ar->end = addr;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400100 return 0;
101 }
102 }
103
104 return 1;
105}
106
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700107static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 char str[500];
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700110 char *sym, stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int rc;
112
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700113 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (rc != 3) {
Jean Sacrenef894872010-09-10 23:13:33 -0600115 if (rc != EOF && fgets(str, 500, in) == NULL)
116 fprintf(stderr, "Read error or end of file.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -1;
118 }
Andi Kleenf3462aa2013-10-23 15:07:53 +0200119 if (strlen(str) > KSYM_NAME_LEN) {
Fabio Estevam6f622592013-11-08 00:45:01 -0200120 fprintf(stderr, "Symbol %s too long for kallsyms (%zu vs %d).\n"
Andi Kleenf3462aa2013-10-23 15:07:53 +0200121 "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n",
122 str, strlen(str), KSYM_NAME_LEN);
123 return -1;
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700126 sym = str;
127 /* skip prefix char */
128 if (symbol_prefix_char && str[0] == symbol_prefix_char)
129 sym++;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100132 if (strcmp(sym, "_text") == 0)
133 _text = s->addr;
Kees Cook78eb7152014-03-17 13:18:27 +1030134 else if (check_symbol_range(sym, s->addr, text_ranges,
135 ARRAY_SIZE(text_ranges)) == 0)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400136 /* nothing to do */;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700137 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 {
139 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700140 if (strcmp(sym, "__kernel_syscall_via_break") &&
141 strcmp(sym, "__kernel_syscall_via_epc") &&
142 strcmp(sym, "__kernel_sigtramp") &&
143 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -1;
145
146 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700147 else if (toupper(stype) == 'U' ||
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700148 is_arm_mapping_symbol(sym))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return -1;
Ralf Baechle6f00df22005-09-06 15:16:41 -0700150 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
151 else if (str[0] == '$')
152 return -1;
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200153 /* exclude debugging symbols */
154 else if (stype == 'N')
155 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 /* include the type field in the symbol name, so that it gets
158 * compressed together */
159 s->len = strlen(str) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700160 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800161 if (!s->sym) {
162 fprintf(stderr, "kallsyms failure: "
163 "unable to allocate required amount of memory\n");
164 exit(EXIT_FAILURE);
165 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700166 strcpy((char *)s->sym + 1, str);
167 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return 0;
170}
171
Kees Cook78eb7152014-03-17 13:18:27 +1030172static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
173 int entries)
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400174{
175 size_t i;
Kees Cook78eb7152014-03-17 13:18:27 +1030176 struct addr_range *ar;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400177
Kees Cook78eb7152014-03-17 13:18:27 +1030178 for (i = 0; i < entries; ++i) {
179 ar = &ranges[i];
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400180
Kees Cook78eb7152014-03-17 13:18:27 +1030181 if (s->addr >= ar->start && s->addr <= ar->end)
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400182 return 1;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400183 }
184
Mike Frysingerac6ca5c2009-06-15 07:52:48 -0400185 return 0;
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400186}
187
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700188static int symbol_valid(struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 /* Symbols which vary between passes. Passes 1 and 2 must have
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100191 * identical symbol lists. The kallsyms_* symbols below are only added
192 * after pass 1, they would be included in pass 2 when --all-symbols is
193 * specified so exclude them to get a stable symbol list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 */
195 static char *special_symbols[] = {
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100196 "kallsyms_addresses",
197 "kallsyms_num_syms",
198 "kallsyms_names",
199 "kallsyms_markers",
200 "kallsyms_token_table",
201 "kallsyms_token_index",
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Exclude linker generated symbols which vary between passes */
204 "_SDA_BASE_", /* ppc */
205 "_SDA2_BASE_", /* ppc */
206 NULL };
207 int i;
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700208 int offset = 1;
209
Ming Leif6537f22013-11-02 09:11:33 +1030210 if (s->addr < kernel_start_addr)
211 return 0;
212
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700213 /* skip prefix char */
214 if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
215 offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* if --all-symbols is not specified, then symbols outside the text
218 * and inittext sections are discarded */
219 if (!all_symbols) {
Kees Cook78eb7152014-03-17 13:18:27 +1030220 if (symbol_in_range(s, text_ranges,
221 ARRAY_SIZE(text_ranges)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
223 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800224 * _etext _einittext; they can move between pass 1 and 2 when
225 * the kallsyms data are added. If these symbols move then
226 * they may get dropped in pass 2, which breaks the kallsyms
227 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400229 if ((s->addr == text_range_text->end &&
Kees Cook78eb7152014-03-17 13:18:27 +1030230 strcmp((char *)s->sym + offset,
231 text_range_text->end_sym)) ||
Mike Frysinger17b1f0d2009-06-08 19:12:13 -0400232 (s->addr == text_range_inittext->end &&
Kees Cook78eb7152014-03-17 13:18:27 +1030233 strcmp((char *)s->sym + offset,
234 text_range_inittext->end_sym)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236 }
237
238 /* Exclude symbols which vary between passes. */
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100239 if (strstr((char *)s->sym + offset, "_compiled."))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return 0;
241
242 for (i = 0; special_symbols[i]; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700243 if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245
246 return 1;
247}
248
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700249static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700252 if (table_cnt >= table_size) {
253 table_size += 10000;
254 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!table) {
256 fprintf(stderr, "out of memory\n");
257 exit (1);
258 }
259 }
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800260 if (read_symbol(in, &table[table_cnt]) == 0) {
261 table[table_cnt].start_pos = table_cnt;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700262 table_cnt++;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265}
266
267static void output_label(char *label)
268{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700269 if (symbol_prefix_char)
270 printf(".globl %c%s\n", symbol_prefix_char, label);
271 else
272 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 printf("\tALGN\n");
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700274 if (symbol_prefix_char)
275 printf("%c%s:\n", symbol_prefix_char, label);
276 else
277 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/* uncompress a compressed symbol. When this function is called, the best table
281 * might still be compressed itself, so the function needs to be recursive */
282static int expand_symbol(unsigned char *data, int len, char *result)
283{
284 int c, rlen, total=0;
285
286 while (len) {
287 c = *data;
288 /* if the table holds a single char that is the same as the one
289 * we are looking for, then end the search */
290 if (best_table[c][0]==c && best_table_len[c]==1) {
291 *result++ = c;
292 total++;
293 } else {
294 /* if not, recurse and expand */
295 rlen = expand_symbol(best_table[c], best_table_len[c], result);
296 total += rlen;
297 result += rlen;
298 }
299 data++;
300 len--;
301 }
302 *result=0;
303
304 return total;
305}
306
Kees Cook78eb7152014-03-17 13:18:27 +1030307static int symbol_absolute(struct sym_entry *s)
308{
309 return toupper(s->sym[0]) == 'A';
310}
311
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700312static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700314 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned int best_idx[256];
316 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700317 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 printf("#include <asm/types.h>\n");
320 printf("#if BITS_PER_LONG == 64\n");
321 printf("#define PTR .quad\n");
322 printf("#define ALGN .align 8\n");
323 printf("#else\n");
324 printf("#define PTR .long\n");
325 printf("#define ALGN .align 4\n");
326 printf("#endif\n");
327
Jan Beulichaad09472006-12-08 02:35:57 -0800328 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100330 /* Provide proper symbols relocatability by their '_text'
331 * relativeness. The symbol names cannot be used to construct
332 * normal symbol references as the list of symbols contains
333 * symbols that are declared static and are private to their
334 * .o files. This prevents .tmp_kallsyms.o or any other
335 * object from referencing them.
336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 output_label("kallsyms_addresses");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700338 for (i = 0; i < table_cnt; i++) {
Kees Cook78eb7152014-03-17 13:18:27 +1030339 if (!symbol_absolute(&table[i])) {
Vivek Goyal2c22d8b2006-12-07 02:14:10 +0100340 if (_text <= table[i].addr)
341 printf("\tPTR\t_text + %#llx\n",
342 table[i].addr - _text);
343 else
Andrew Morton2930ffc2014-03-10 15:49:48 -0700344 printf("\tPTR\t_text - %#llx\n",
345 _text - table[i].addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100346 } else {
347 printf("\tPTR\t%#llx\n", table[i].addr);
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 printf("\n");
351
352 output_label("kallsyms_num_syms");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700353 printf("\tPTR\t%d\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 printf("\n");
355
356 /* table of offset markers, that give the offset in the compressed stream
357 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800358 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
359 if (!markers) {
360 fprintf(stderr, "kallsyms failure: "
361 "unable to allocate required memory\n");
362 exit(EXIT_FAILURE);
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700367 for (i = 0; i < table_cnt; i++) {
368 if ((i & 0xFF) == 0)
369 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 printf("\t.byte 0x%02x", table[i].len);
372 for (k = 0; k < table[i].len; k++)
373 printf(", 0x%02x", table[i].sym[k]);
374 printf("\n");
375
376 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378 printf("\n");
379
380 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700381 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 printf("\tPTR\t%d\n", markers[i]);
383 printf("\n");
384
385 free(markers);
386
387 output_label("kallsyms_token_table");
388 off = 0;
389 for (i = 0; i < 256; i++) {
390 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700391 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 printf("\t.asciz\t\"%s\"\n", buf);
393 off += strlen(buf) + 1;
394 }
395 printf("\n");
396
397 output_label("kallsyms_token_index");
398 for (i = 0; i < 256; i++)
399 printf("\t.short\t%d\n", best_idx[i]);
400 printf("\n");
401}
402
403
404/* table lookup compression functions */
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* count all the possible tokens in a symbol */
407static void learn_symbol(unsigned char *symbol, int len)
408{
409 int i;
410
411 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700412 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
415/* decrease the count for all the possible tokens in a symbol */
416static void forget_symbol(unsigned char *symbol, int len)
417{
418 int i;
419
420 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700421 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700424/* remove all the invalid symbols from the table and do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static void build_initial_tok_table(void)
426{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700427 unsigned int i, pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700429 pos = 0;
430 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if ( symbol_valid(&table[i]) ) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700432 if (pos != i)
433 table[pos] = table[i];
434 learn_symbol(table[pos].sym, table[pos].len);
435 pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700438 table_cnt = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Paulo Marques7c5d2492007-06-20 18:09:00 +0100441static void *find_token(unsigned char *str, int len, unsigned char *token)
442{
443 int i;
444
445 for (i = 0; i < len - 1; i++) {
446 if (str[i] == token[0] && str[i+1] == token[1])
447 return &str[i];
448 }
449 return NULL;
450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* replace a given token in all the valid symbols. Use the sampled symbols
453 * to update the counts */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700454static void compress_symbols(unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700456 unsigned int i, len, size;
457 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700459 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700462 p1 = table[i].sym;
463
464 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100465 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700466 if (!p2) continue;
467
468 /* decrease the counts for this symbol's tokens */
469 forget_symbol(table[i].sym, len);
470
471 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700474 *p2 = idx;
475 p2++;
476 size -= (p2 - p1);
477 memmove(p2, p2 + 1, size);
478 p1 = p2;
479 len--;
480
481 if (size < 2) break;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100484 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700486 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700488 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700490 /* increase the counts for this symbol's new tokens */
491 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493}
494
495/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700496static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700498 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700501 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700503 for (i = 0; i < 0x10000; i++) {
504 if (token_profit[i] > bestprofit) {
505 best = i;
506 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return best;
510}
511
512/* this is the core of the algorithm: calculate the "best" table */
513static void optimize_result(void)
514{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700515 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* using the '\0' symbol last allows compress_symbols to use standard
518 * fast string functions */
519 for (i = 255; i >= 0; i--) {
520
521 /* if this table slot is empty (it is not used by an actual
522 * original char code */
523 if (!best_table_len[i]) {
524
525 /* find the token with the breates profit value */
526 best = find_best_token();
Xiaochen Wange0a04b12011-05-01 11:41:41 +0800527 if (token_profit[best] == 0)
528 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700531 best_table_len[i] = 2;
532 best_table[i][0] = best & 0xFF;
533 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700536 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 }
539}
540
541/* start by placing the symbols that are actually used on the table */
542static void insert_real_symbols_in_table(void)
543{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700544 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 memset(best_table, 0, sizeof(best_table));
547 memset(best_table_len, 0, sizeof(best_table_len));
548
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700549 for (i = 0; i < table_cnt; i++) {
550 for (j = 0; j < table[i].len; j++) {
551 c = table[i].sym[j];
552 best_table[c][0]=c;
553 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 }
556}
557
558static void optimize_token_table(void)
559{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 build_initial_tok_table();
561
562 insert_real_symbols_in_table();
563
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700564 /* When valid symbol is not registered, exit to error */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700565 if (!table_cnt) {
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700566 fprintf(stderr, "No valid symbol.\n");
567 exit(1);
568 }
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 optimize_result();
571}
572
Lai Jiangshanb478b7822009-03-13 15:10:26 +0800573/* guess for "linker script provide" symbol */
574static int may_be_linker_script_provide_symbol(const struct sym_entry *se)
575{
576 const char *symbol = (char *)se->sym + 1;
577 int len = se->len - 1;
578
579 if (len < 8)
580 return 0;
581
582 if (symbol[0] != '_' || symbol[1] != '_')
583 return 0;
584
585 /* __start_XXXXX */
586 if (!memcmp(symbol + 2, "start_", 6))
587 return 1;
588
589 /* __stop_XXXXX */
590 if (!memcmp(symbol + 2, "stop_", 5))
591 return 1;
592
593 /* __end_XXXXX */
594 if (!memcmp(symbol + 2, "end_", 4))
595 return 1;
596
597 /* __XXXXX_start */
598 if (!memcmp(symbol + len - 6, "_start", 6))
599 return 1;
600
601 /* __XXXXX_end */
602 if (!memcmp(symbol + len - 4, "_end", 4))
603 return 1;
604
605 return 0;
606}
607
608static int prefix_underscores_count(const char *str)
609{
610 const char *tail = str;
611
Paul Mundta9ece532009-09-22 16:44:12 -0700612 while (*tail == '_')
Lai Jiangshanb478b7822009-03-13 15:10:26 +0800613 tail++;
614
615 return tail - str;
616}
617
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800618static int compare_symbols(const void *a, const void *b)
619{
620 const struct sym_entry *sa;
621 const struct sym_entry *sb;
622 int wa, wb;
623
624 sa = a;
625 sb = b;
626
627 /* sort by address first */
628 if (sa->addr > sb->addr)
629 return 1;
630 if (sa->addr < sb->addr)
631 return -1;
632
633 /* sort by "weakness" type */
634 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
635 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
636 if (wa != wb)
637 return wa - wb;
638
Lai Jiangshanb478b7822009-03-13 15:10:26 +0800639 /* sort by "linker script provide" type */
640 wa = may_be_linker_script_provide_symbol(sa);
641 wb = may_be_linker_script_provide_symbol(sb);
642 if (wa != wb)
643 return wa - wb;
644
645 /* sort by the number of prefix underscores */
646 wa = prefix_underscores_count((const char *)sa->sym + 1);
647 wb = prefix_underscores_count((const char *)sb->sym + 1);
648 if (wa != wb)
649 return wa - wb;
650
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800651 /* sort by initial order, so that other symbols are left undisturbed */
652 return sa->start_pos - sb->start_pos;
653}
654
655static void sort_symbols(void)
656{
657 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
658}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700660int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700662 if (argc >= 2) {
663 int i;
664 for (i = 1; i < argc; i++) {
665 if(strcmp(argv[i], "--all-symbols") == 0)
666 all_symbols = 1;
667 else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
668 char *p = &argv[i][16];
669 /* skip quote */
670 if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
671 p++;
672 symbol_prefix_char = *p;
Ming Leif6537f22013-11-02 09:11:33 +1030673 } else if (strncmp(argv[i], "--page-offset=", 14) == 0) {
674 const char *p = &argv[i][14];
675 kernel_start_addr = strtoull(p, NULL, 16);
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700676 } else
677 usage();
678 }
679 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 usage();
681
682 read_map(stdin);
Sam Ravnborg2ea03892009-01-14 21:38:20 +0100683 sort_symbols();
684 optimize_token_table();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 write_src();
686
687 return 0;
688}