blob: 4c1ad0acbb4b096ac056755b407ab743235b7f1a [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 *
10 * ChangeLog:
11 *
12 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
13 * Changed the compression method from stem compression to "table lookup"
14 * compression
15 *
16 * Table compression uses all the unused char codes on the symbols and
17 * maps these to the most used substrings (tokens). For instance, it might
18 * map char code 0xF7 to represent "write_" and then in every symbol where
19 * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
20 * The used codes themselves are also placed in the table so that the
21 * decompresion can work without "special cases".
22 * Applied to kernel symbols, this usually produces a compression ratio
23 * of about 50%.
24 *
25 */
26
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070027#define _GNU_SOURCE
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <ctype.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#define KSYM_NAME_LEN 127
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37struct sym_entry {
38 unsigned long long addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070039 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 unsigned char *sym;
41};
42
43
44static struct sym_entry *table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070045static unsigned int table_size, table_cnt;
Eric W. Biedermanfd593d12006-12-07 02:14:04 +010046static unsigned long long _text, _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int all_symbols = 0;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070048static char symbol_prefix_char = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070050int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* the table that holds the result of the compression */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070053unsigned char best_table[256][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070054unsigned char best_table_len[256];
55
56
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070057static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Yoshinori Sato41f11a42005-05-01 08:59:06 -070059 fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 exit(1);
61}
62
63/*
64 * This ignores the intensely annoying "mapping symbols" found
65 * in ARM ELF files: $a, $t and $d.
66 */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070067static inline int is_arm_mapping_symbol(const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 return str[0] == '$' && strchr("atd", str[1])
70 && (str[2] == '\0' || str[2] == '.');
71}
72
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070073static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 char str[500];
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070076 char *sym, stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int rc;
78
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070079 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (rc != 3) {
81 if (rc != EOF) {
82 /* skip line */
83 fgets(str, 500, in);
84 }
85 return -1;
86 }
87
Yoshinori Sato41f11a42005-05-01 08:59:06 -070088 sym = str;
89 /* skip prefix char */
90 if (symbol_prefix_char && str[0] == symbol_prefix_char)
91 sym++;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +010094 if (strcmp(sym, "_text") == 0)
95 _text = s->addr;
96 else if (strcmp(sym, "_stext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 _stext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070098 else if (strcmp(sym, "_etext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 _etext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700100 else if (strcmp(sym, "_sinittext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 _sinittext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700102 else if (strcmp(sym, "_einittext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 _einittext = s->addr;
David Woodhouse075d6eb2005-05-05 16:15:09 -0700104 else if (strcmp(sym, "_sextratext") == 0)
105 _sextratext = s->addr;
106 else if (strcmp(sym, "_eextratext") == 0)
107 _eextratext = s->addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700108 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 {
110 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700111 if (strcmp(sym, "__kernel_syscall_via_break") &&
112 strcmp(sym, "__kernel_syscall_via_epc") &&
113 strcmp(sym, "__kernel_sigtramp") &&
114 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -1;
116
117 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700118 else if (toupper(stype) == 'U' ||
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700119 is_arm_mapping_symbol(sym))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -1;
Ralf Baechle6f00df22005-09-06 15:16:41 -0700121 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
122 else if (str[0] == '$')
123 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /* include the type field in the symbol name, so that it gets
126 * compressed together */
127 s->len = strlen(str) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700128 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800129 if (!s->sym) {
130 fprintf(stderr, "kallsyms failure: "
131 "unable to allocate required amount of memory\n");
132 exit(EXIT_FAILURE);
133 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700134 strcpy((char *)s->sym + 1, str);
135 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 return 0;
138}
139
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700140static int symbol_valid(struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 /* Symbols which vary between passes. Passes 1 and 2 must have
143 * identical symbol lists. The kallsyms_* symbols below are only added
144 * after pass 1, they would be included in pass 2 when --all-symbols is
145 * specified so exclude them to get a stable symbol list.
146 */
147 static char *special_symbols[] = {
148 "kallsyms_addresses",
149 "kallsyms_num_syms",
150 "kallsyms_names",
151 "kallsyms_markers",
152 "kallsyms_token_table",
153 "kallsyms_token_index",
154
155 /* Exclude linker generated symbols which vary between passes */
156 "_SDA_BASE_", /* ppc */
157 "_SDA2_BASE_", /* ppc */
158 NULL };
159 int i;
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700160 int offset = 1;
161
162 /* skip prefix char */
163 if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
164 offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 /* if --all-symbols is not specified, then symbols outside the text
167 * and inittext sections are discarded */
168 if (!all_symbols) {
169 if ((s->addr < _stext || s->addr > _etext)
David Woodhouse075d6eb2005-05-05 16:15:09 -0700170 && (s->addr < _sinittext || s->addr > _einittext)
171 && (s->addr < _sextratext || s->addr > _eextratext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173 /* Corner case. Discard any symbols with the same value as
David Woodhouse075d6eb2005-05-05 16:15:09 -0700174 * _etext _einittext or _eextratext; they can move between pass
175 * 1 and 2 when the kallsyms data are added. If these symbols
176 * move then they may get dropped in pass 2, which breaks the
177 * kallsyms rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000179 if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) ||
180 (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) ||
181 (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return 0;
183 }
184
185 /* Exclude symbols which vary between passes. */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700186 if (strstr((char *)s->sym + offset, "_compiled."))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return 0;
188
189 for (i = 0; special_symbols[i]; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700190 if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return 0;
192
193 return 1;
194}
195
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700196static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700199 if (table_cnt >= table_size) {
200 table_size += 10000;
201 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!table) {
203 fprintf(stderr, "out of memory\n");
204 exit (1);
205 }
206 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700207 if (read_symbol(in, &table[table_cnt]) == 0)
208 table_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210}
211
212static void output_label(char *label)
213{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700214 if (symbol_prefix_char)
215 printf(".globl %c%s\n", symbol_prefix_char, label);
216 else
217 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 printf("\tALGN\n");
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700219 if (symbol_prefix_char)
220 printf("%c%s:\n", symbol_prefix_char, label);
221 else
222 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/* uncompress a compressed symbol. When this function is called, the best table
226 * might still be compressed itself, so the function needs to be recursive */
227static int expand_symbol(unsigned char *data, int len, char *result)
228{
229 int c, rlen, total=0;
230
231 while (len) {
232 c = *data;
233 /* if the table holds a single char that is the same as the one
234 * we are looking for, then end the search */
235 if (best_table[c][0]==c && best_table_len[c]==1) {
236 *result++ = c;
237 total++;
238 } else {
239 /* if not, recurse and expand */
240 rlen = expand_symbol(best_table[c], best_table_len[c], result);
241 total += rlen;
242 result += rlen;
243 }
244 data++;
245 len--;
246 }
247 *result=0;
248
249 return total;
250}
251
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700252static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700254 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned int best_idx[256];
256 unsigned int *markers;
257 char buf[KSYM_NAME_LEN+1];
258
259 printf("#include <asm/types.h>\n");
260 printf("#if BITS_PER_LONG == 64\n");
261 printf("#define PTR .quad\n");
262 printf("#define ALGN .align 8\n");
263 printf("#else\n");
264 printf("#define PTR .long\n");
265 printf("#define ALGN .align 4\n");
266 printf("#endif\n");
267
268 printf(".data\n");
269
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100270 /* Provide proper symbols relocatability by their '_text'
271 * relativeness. The symbol names cannot be used to construct
272 * normal symbol references as the list of symbols contains
273 * symbols that are declared static and are private to their
274 * .o files. This prevents .tmp_kallsyms.o or any other
275 * object from referencing them.
276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 output_label("kallsyms_addresses");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700278 for (i = 0; i < table_cnt; i++) {
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100279 if (toupper(table[i].sym[0]) != 'A') {
280 printf("\tPTR\t_text + %#llx\n",
281 table[i].addr - _text);
282 } else {
283 printf("\tPTR\t%#llx\n", table[i].addr);
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286 printf("\n");
287
288 output_label("kallsyms_num_syms");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700289 printf("\tPTR\t%d\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 printf("\n");
291
292 /* table of offset markers, that give the offset in the compressed stream
293 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800294 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
295 if (!markers) {
296 fprintf(stderr, "kallsyms failure: "
297 "unable to allocate required memory\n");
298 exit(EXIT_FAILURE);
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700303 for (i = 0; i < table_cnt; i++) {
304 if ((i & 0xFF) == 0)
305 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 printf("\t.byte 0x%02x", table[i].len);
308 for (k = 0; k < table[i].len; k++)
309 printf(", 0x%02x", table[i].sym[k]);
310 printf("\n");
311
312 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 printf("\n");
315
316 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700317 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 printf("\tPTR\t%d\n", markers[i]);
319 printf("\n");
320
321 free(markers);
322
323 output_label("kallsyms_token_table");
324 off = 0;
325 for (i = 0; i < 256; i++) {
326 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700327 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 printf("\t.asciz\t\"%s\"\n", buf);
329 off += strlen(buf) + 1;
330 }
331 printf("\n");
332
333 output_label("kallsyms_token_index");
334 for (i = 0; i < 256; i++)
335 printf("\t.short\t%d\n", best_idx[i]);
336 printf("\n");
337}
338
339
340/* table lookup compression functions */
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/* count all the possible tokens in a symbol */
343static void learn_symbol(unsigned char *symbol, int len)
344{
345 int i;
346
347 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700348 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351/* decrease the count for all the possible tokens in a symbol */
352static void forget_symbol(unsigned char *symbol, int len)
353{
354 int i;
355
356 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700357 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700360/* remove all the invalid symbols from the table and do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static void build_initial_tok_table(void)
362{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700363 unsigned int i, pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700365 pos = 0;
366 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if ( symbol_valid(&table[i]) ) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700368 if (pos != i)
369 table[pos] = table[i];
370 learn_symbol(table[pos].sym, table[pos].len);
371 pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700374 table_cnt = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377/* replace a given token in all the valid symbols. Use the sampled symbols
378 * to update the counts */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700379static void compress_symbols(unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700381 unsigned int i, len, size;
382 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700384 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700387 p1 = table[i].sym;
388
389 /* find the token on the symbol */
390 p2 = memmem(p1, len, str, 2);
391 if (!p2) continue;
392
393 /* decrease the counts for this symbol's tokens */
394 forget_symbol(table[i].sym, len);
395
396 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700399 *p2 = idx;
400 p2++;
401 size -= (p2 - p1);
402 memmove(p2, p2 + 1, size);
403 p1 = p2;
404 len--;
405
406 if (size < 2) break;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* find the token on the symbol */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700409 p2 = memmem(p1, size, str, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700411 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700413 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700415 /* increase the counts for this symbol's new tokens */
416 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418}
419
420/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700421static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700423 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700426 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700428 for (i = 0; i < 0x10000; i++) {
429 if (token_profit[i] > bestprofit) {
430 best = i;
431 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return best;
435}
436
437/* this is the core of the algorithm: calculate the "best" table */
438static void optimize_result(void)
439{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700440 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* using the '\0' symbol last allows compress_symbols to use standard
443 * fast string functions */
444 for (i = 255; i >= 0; i--) {
445
446 /* if this table slot is empty (it is not used by an actual
447 * original char code */
448 if (!best_table_len[i]) {
449
450 /* find the token with the breates profit value */
451 best = find_best_token();
452
453 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700454 best_table_len[i] = 2;
455 best_table[i][0] = best & 0xFF;
456 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700459 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461 }
462}
463
464/* start by placing the symbols that are actually used on the table */
465static void insert_real_symbols_in_table(void)
466{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700467 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 memset(best_table, 0, sizeof(best_table));
470 memset(best_table_len, 0, sizeof(best_table_len));
471
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700472 for (i = 0; i < table_cnt; i++) {
473 for (j = 0; j < table[i].len; j++) {
474 c = table[i].sym[j];
475 best_table[c][0]=c;
476 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 }
479}
480
481static void optimize_token_table(void)
482{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 build_initial_tok_table();
484
485 insert_real_symbols_in_table();
486
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700487 /* When valid symbol is not registered, exit to error */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700488 if (!table_cnt) {
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700489 fprintf(stderr, "No valid symbol.\n");
490 exit(1);
491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 optimize_result();
494}
495
496
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700497int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700499 if (argc >= 2) {
500 int i;
501 for (i = 1; i < argc; i++) {
502 if(strcmp(argv[i], "--all-symbols") == 0)
503 all_symbols = 1;
504 else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
505 char *p = &argv[i][16];
506 /* skip quote */
507 if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
508 p++;
509 symbol_prefix_char = *p;
510 } else
511 usage();
512 }
513 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 usage();
515
516 read_map(stdin);
517 optimize_token_table();
518 write_src();
519
520 return 0;
521}