blob: 22d281c6ec244c16bb0dc1c870e7f5c5ce913fb5 [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;
David Woodhouse075d6eb2005-05-05 16:15:09 -070046static unsigned long long _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. */
Yoshinori Sato41f11a42005-05-01 08:59:06 -070094 if (strcmp(sym, "_stext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 _stext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070096 else if (strcmp(sym, "_etext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 _etext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070098 else if (strcmp(sym, "_sinittext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 _sinittext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700100 else if (strcmp(sym, "_einittext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 _einittext = s->addr;
David Woodhouse075d6eb2005-05-05 16:15:09 -0700102 else if (strcmp(sym, "_sextratext") == 0)
103 _sextratext = s->addr;
104 else if (strcmp(sym, "_eextratext") == 0)
105 _eextratext = s->addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700106 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 {
108 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700109 if (strcmp(sym, "__kernel_syscall_via_break") &&
110 strcmp(sym, "__kernel_syscall_via_epc") &&
111 strcmp(sym, "__kernel_sigtramp") &&
112 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return -1;
114
115 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700116 else if (toupper(stype) == 'U' ||
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700117 is_arm_mapping_symbol(sym))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return -1;
Ralf Baechle6f00df22005-09-06 15:16:41 -0700119 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
120 else if (str[0] == '$')
121 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* include the type field in the symbol name, so that it gets
124 * compressed together */
125 s->len = strlen(str) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700126 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800127 if (!s->sym) {
128 fprintf(stderr, "kallsyms failure: "
129 "unable to allocate required amount of memory\n");
130 exit(EXIT_FAILURE);
131 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700132 strcpy((char *)s->sym + 1, str);
133 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return 0;
136}
137
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700138static int symbol_valid(struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 /* Symbols which vary between passes. Passes 1 and 2 must have
141 * identical symbol lists. The kallsyms_* symbols below are only added
142 * after pass 1, they would be included in pass 2 when --all-symbols is
143 * specified so exclude them to get a stable symbol list.
144 */
145 static char *special_symbols[] = {
146 "kallsyms_addresses",
147 "kallsyms_num_syms",
148 "kallsyms_names",
149 "kallsyms_markers",
150 "kallsyms_token_table",
151 "kallsyms_token_index",
152
153 /* Exclude linker generated symbols which vary between passes */
154 "_SDA_BASE_", /* ppc */
155 "_SDA2_BASE_", /* ppc */
156 NULL };
157 int i;
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700158 int offset = 1;
159
160 /* skip prefix char */
161 if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
162 offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 /* if --all-symbols is not specified, then symbols outside the text
165 * and inittext sections are discarded */
166 if (!all_symbols) {
167 if ((s->addr < _stext || s->addr > _etext)
David Woodhouse075d6eb2005-05-05 16:15:09 -0700168 && (s->addr < _sinittext || s->addr > _einittext)
169 && (s->addr < _sextratext || s->addr > _eextratext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171 /* Corner case. Discard any symbols with the same value as
David Woodhouse075d6eb2005-05-05 16:15:09 -0700172 * _etext _einittext or _eextratext; they can move between pass
173 * 1 and 2 when the kallsyms data are added. If these symbols
174 * move then they may get dropped in pass 2, which breaks the
175 * kallsyms rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
J.A. Magallon61d9cdf2005-07-15 22:14:43 +0000177 if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) ||
178 (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) ||
179 (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181 }
182
183 /* Exclude symbols which vary between passes. */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700184 if (strstr((char *)s->sym + offset, "_compiled."))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return 0;
186
187 for (i = 0; special_symbols[i]; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700188 if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
190
191 return 1;
192}
193
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700194static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700197 if (table_cnt >= table_size) {
198 table_size += 10000;
199 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!table) {
201 fprintf(stderr, "out of memory\n");
202 exit (1);
203 }
204 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700205 if (read_symbol(in, &table[table_cnt]) == 0)
206 table_cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208}
209
210static void output_label(char *label)
211{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700212 if (symbol_prefix_char)
213 printf(".globl %c%s\n", symbol_prefix_char, label);
214 else
215 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 printf("\tALGN\n");
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700217 if (symbol_prefix_char)
218 printf("%c%s:\n", symbol_prefix_char, label);
219 else
220 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223/* uncompress a compressed symbol. When this function is called, the best table
224 * might still be compressed itself, so the function needs to be recursive */
225static int expand_symbol(unsigned char *data, int len, char *result)
226{
227 int c, rlen, total=0;
228
229 while (len) {
230 c = *data;
231 /* if the table holds a single char that is the same as the one
232 * we are looking for, then end the search */
233 if (best_table[c][0]==c && best_table_len[c]==1) {
234 *result++ = c;
235 total++;
236 } else {
237 /* if not, recurse and expand */
238 rlen = expand_symbol(best_table[c], best_table_len[c], result);
239 total += rlen;
240 result += rlen;
241 }
242 data++;
243 len--;
244 }
245 *result=0;
246
247 return total;
248}
249
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700250static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700252 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned int best_idx[256];
254 unsigned int *markers;
255 char buf[KSYM_NAME_LEN+1];
256
257 printf("#include <asm/types.h>\n");
258 printf("#if BITS_PER_LONG == 64\n");
259 printf("#define PTR .quad\n");
260 printf("#define ALGN .align 8\n");
261 printf("#else\n");
262 printf("#define PTR .long\n");
263 printf("#define ALGN .align 4\n");
264 printf("#endif\n");
265
266 printf(".data\n");
267
268 output_label("kallsyms_addresses");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700269 for (i = 0; i < table_cnt; i++) {
270 printf("\tPTR\t%#llx\n", table[i].addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 printf("\n");
273
274 output_label("kallsyms_num_syms");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700275 printf("\tPTR\t%d\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 printf("\n");
277
278 /* table of offset markers, that give the offset in the compressed stream
279 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800280 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
281 if (!markers) {
282 fprintf(stderr, "kallsyms failure: "
283 "unable to allocate required memory\n");
284 exit(EXIT_FAILURE);
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700289 for (i = 0; i < table_cnt; i++) {
290 if ((i & 0xFF) == 0)
291 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 printf("\t.byte 0x%02x", table[i].len);
294 for (k = 0; k < table[i].len; k++)
295 printf(", 0x%02x", table[i].sym[k]);
296 printf("\n");
297
298 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 printf("\n");
301
302 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700303 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 printf("\tPTR\t%d\n", markers[i]);
305 printf("\n");
306
307 free(markers);
308
309 output_label("kallsyms_token_table");
310 off = 0;
311 for (i = 0; i < 256; i++) {
312 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700313 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 printf("\t.asciz\t\"%s\"\n", buf);
315 off += strlen(buf) + 1;
316 }
317 printf("\n");
318
319 output_label("kallsyms_token_index");
320 for (i = 0; i < 256; i++)
321 printf("\t.short\t%d\n", best_idx[i]);
322 printf("\n");
323}
324
325
326/* table lookup compression functions */
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/* count all the possible tokens in a symbol */
329static void learn_symbol(unsigned char *symbol, int len)
330{
331 int i;
332
333 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700334 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337/* decrease the count for all the possible tokens in a symbol */
338static void forget_symbol(unsigned char *symbol, int len)
339{
340 int i;
341
342 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700343 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700346/* remove all the invalid symbols from the table and do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static void build_initial_tok_table(void)
348{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700349 unsigned int i, pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700351 pos = 0;
352 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if ( symbol_valid(&table[i]) ) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700354 if (pos != i)
355 table[pos] = table[i];
356 learn_symbol(table[pos].sym, table[pos].len);
357 pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700360 table_cnt = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/* replace a given token in all the valid symbols. Use the sampled symbols
364 * to update the counts */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700365static void compress_symbols(unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700367 unsigned int i, len, size;
368 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700370 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700373 p1 = table[i].sym;
374
375 /* find the token on the symbol */
376 p2 = memmem(p1, len, str, 2);
377 if (!p2) continue;
378
379 /* decrease the counts for this symbol's tokens */
380 forget_symbol(table[i].sym, len);
381
382 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700385 *p2 = idx;
386 p2++;
387 size -= (p2 - p1);
388 memmove(p2, p2 + 1, size);
389 p1 = p2;
390 len--;
391
392 if (size < 2) break;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* find the token on the symbol */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700395 p2 = memmem(p1, size, str, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700397 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700399 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700401 /* increase the counts for this symbol's new tokens */
402 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404}
405
406/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700407static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700409 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700412 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700414 for (i = 0; i < 0x10000; i++) {
415 if (token_profit[i] > bestprofit) {
416 best = i;
417 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return best;
421}
422
423/* this is the core of the algorithm: calculate the "best" table */
424static void optimize_result(void)
425{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700426 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* using the '\0' symbol last allows compress_symbols to use standard
429 * fast string functions */
430 for (i = 255; i >= 0; i--) {
431
432 /* if this table slot is empty (it is not used by an actual
433 * original char code */
434 if (!best_table_len[i]) {
435
436 /* find the token with the breates profit value */
437 best = find_best_token();
438
439 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700440 best_table_len[i] = 2;
441 best_table[i][0] = best & 0xFF;
442 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700445 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 }
447 }
448}
449
450/* start by placing the symbols that are actually used on the table */
451static void insert_real_symbols_in_table(void)
452{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700453 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 memset(best_table, 0, sizeof(best_table));
456 memset(best_table_len, 0, sizeof(best_table_len));
457
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700458 for (i = 0; i < table_cnt; i++) {
459 for (j = 0; j < table[i].len; j++) {
460 c = table[i].sym[j];
461 best_table[c][0]=c;
462 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464 }
465}
466
467static void optimize_token_table(void)
468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 build_initial_tok_table();
470
471 insert_real_symbols_in_table();
472
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700473 /* When valid symbol is not registered, exit to error */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700474 if (!table_cnt) {
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700475 fprintf(stderr, "No valid symbol.\n");
476 exit(1);
477 }
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 optimize_result();
480}
481
482
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700483int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700485 if (argc >= 2) {
486 int i;
487 for (i = 1; i < argc; i++) {
488 if(strcmp(argv[i], "--all-symbols") == 0)
489 all_symbols = 1;
490 else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
491 char *p = &argv[i][16];
492 /* skip quote */
493 if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
494 p++;
495 symbol_prefix_char = *p;
496 } else
497 usage();
498 }
499 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 usage();
501
502 read_map(stdin);
503 optimize_token_table();
504 write_src();
505
506 return 0;
507}