blob: ad2434b26970c17b4b72efb82761afd0ac04042d [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
Tejun Heo9281ace2007-07-17 04:03:51 -070026#define KSYM_NAME_LEN 128
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct sym_entry {
29 unsigned long long addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070030 unsigned int len;
Paulo Marquesf2df3f62008-02-06 01:37:33 -080031 unsigned int start_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 unsigned char *sym;
33};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static struct sym_entry *table;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070036static unsigned int table_size, table_cnt;
Robin Getza3b81112008-02-06 01:36:26 -080037static unsigned long long _text, _stext, _etext, _sinittext, _einittext;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static int all_symbols = 0;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070039static char symbol_prefix_char = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070041int token_profit[0x10000];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* the table that holds the result of the compression */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070044unsigned char best_table[256][2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070045unsigned char best_table_len[256];
46
47
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070048static void usage(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Yoshinori Sato41f11a42005-05-01 08:59:06 -070050 fprintf(stderr, "Usage: kallsyms [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 exit(1);
52}
53
54/*
55 * This ignores the intensely annoying "mapping symbols" found
56 * in ARM ELF files: $a, $t and $d.
57 */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070058static inline int is_arm_mapping_symbol(const char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 return str[0] == '$' && strchr("atd", str[1])
61 && (str[2] == '\0' || str[2] == '.');
62}
63
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070064static int read_symbol(FILE *in, struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 char str[500];
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070067 char *sym, stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 int rc;
69
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070070 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (rc != 3) {
72 if (rc != EOF) {
73 /* skip line */
74 fgets(str, 500, in);
75 }
76 return -1;
77 }
78
Yoshinori Sato41f11a42005-05-01 08:59:06 -070079 sym = str;
80 /* skip prefix char */
81 if (symbol_prefix_char && str[0] == symbol_prefix_char)
82 sym++;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* Ignore most absolute/undefined (?) symbols. */
Eric W. Biedermanfd593d12006-12-07 02:14:04 +010085 if (strcmp(sym, "_text") == 0)
86 _text = s->addr;
87 else if (strcmp(sym, "_stext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 _stext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070089 else if (strcmp(sym, "_etext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 _etext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070091 else if (strcmp(sym, "_sinittext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 _sinittext = s->addr;
Yoshinori Sato41f11a42005-05-01 08:59:06 -070093 else if (strcmp(sym, "_einittext") == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 _einittext = s->addr;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -070095 else if (toupper(stype) == 'A')
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 {
97 /* Keep these useful absolute symbols */
Yoshinori Sato41f11a42005-05-01 08:59:06 -070098 if (strcmp(sym, "__kernel_syscall_via_break") &&
99 strcmp(sym, "__kernel_syscall_via_epc") &&
100 strcmp(sym, "__kernel_sigtramp") &&
101 strcmp(sym, "__gp"))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return -1;
103
104 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700105 else if (toupper(stype) == 'U' ||
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700106 is_arm_mapping_symbol(sym))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -1;
Ralf Baechle6f00df22005-09-06 15:16:41 -0700108 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
109 else if (str[0] == '$')
110 return -1;
Sam Ravnborgaab34ac2008-05-19 20:07:58 +0200111 /* exclude debugging symbols */
112 else if (stype == 'N')
113 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* include the type field in the symbol name, so that it gets
116 * compressed together */
117 s->len = strlen(str) + 1;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700118 s->sym = malloc(s->len + 1);
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800119 if (!s->sym) {
120 fprintf(stderr, "kallsyms failure: "
121 "unable to allocate required amount of memory\n");
122 exit(EXIT_FAILURE);
123 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700124 strcpy((char *)s->sym + 1, str);
125 s->sym[0] = stype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return 0;
128}
129
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700130static int symbol_valid(struct sym_entry *s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 /* Symbols which vary between passes. Passes 1 and 2 must have
133 * identical symbol lists. The kallsyms_* symbols below are only added
134 * after pass 1, they would be included in pass 2 when --all-symbols is
135 * specified so exclude them to get a stable symbol list.
136 */
137 static char *special_symbols[] = {
138 "kallsyms_addresses",
139 "kallsyms_num_syms",
140 "kallsyms_names",
141 "kallsyms_markers",
142 "kallsyms_token_table",
143 "kallsyms_token_index",
144
145 /* Exclude linker generated symbols which vary between passes */
146 "_SDA_BASE_", /* ppc */
147 "_SDA2_BASE_", /* ppc */
148 NULL };
149 int i;
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700150 int offset = 1;
151
152 /* skip prefix char */
153 if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
154 offset++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 /* if --all-symbols is not specified, then symbols outside the text
157 * and inittext sections are discarded */
158 if (!all_symbols) {
159 if ((s->addr < _stext || s->addr > _etext)
Robin Getza3b81112008-02-06 01:36:26 -0800160 && (s->addr < _sinittext || s->addr > _einittext))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return 0;
162 /* Corner case. Discard any symbols with the same value as
Robin Getza3b81112008-02-06 01:36:26 -0800163 * _etext _einittext; they can move between pass 1 and 2 when
164 * the kallsyms data are added. If these symbols move then
165 * they may get dropped in pass 2, which breaks the kallsyms
166 * rules.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Robin Getza3b81112008-02-06 01:36:26 -0800168 if ((s->addr == _etext &&
169 strcmp((char *)s->sym + offset, "_etext")) ||
170 (s->addr == _einittext &&
171 strcmp((char *)s->sym + offset, "_einittext")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return 0;
173 }
174
175 /* Exclude symbols which vary between passes. */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700176 if (strstr((char *)s->sym + offset, "_compiled."))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return 0;
178
179 for (i = 0; special_symbols[i]; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700180 if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182
183 return 1;
184}
185
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700186static void read_map(FILE *in)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 while (!feof(in)) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700189 if (table_cnt >= table_size) {
190 table_size += 10000;
191 table = realloc(table, sizeof(*table) * table_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!table) {
193 fprintf(stderr, "out of memory\n");
194 exit (1);
195 }
196 }
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800197 if (read_symbol(in, &table[table_cnt]) == 0) {
198 table[table_cnt].start_pos = table_cnt;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700199 table_cnt++;
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202}
203
204static void output_label(char *label)
205{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700206 if (symbol_prefix_char)
207 printf(".globl %c%s\n", symbol_prefix_char, label);
208 else
209 printf(".globl %s\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 printf("\tALGN\n");
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700211 if (symbol_prefix_char)
212 printf("%c%s:\n", symbol_prefix_char, label);
213 else
214 printf("%s:\n", label);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/* uncompress a compressed symbol. When this function is called, the best table
218 * might still be compressed itself, so the function needs to be recursive */
219static int expand_symbol(unsigned char *data, int len, char *result)
220{
221 int c, rlen, total=0;
222
223 while (len) {
224 c = *data;
225 /* if the table holds a single char that is the same as the one
226 * we are looking for, then end the search */
227 if (best_table[c][0]==c && best_table_len[c]==1) {
228 *result++ = c;
229 total++;
230 } else {
231 /* if not, recurse and expand */
232 rlen = expand_symbol(best_table[c], best_table_len[c], result);
233 total += rlen;
234 result += rlen;
235 }
236 data++;
237 len--;
238 }
239 *result=0;
240
241 return total;
242}
243
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700244static void write_src(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700246 unsigned int i, k, off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 unsigned int best_idx[256];
248 unsigned int *markers;
Tejun Heo9281ace2007-07-17 04:03:51 -0700249 char buf[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 printf("#include <asm/types.h>\n");
252 printf("#if BITS_PER_LONG == 64\n");
253 printf("#define PTR .quad\n");
254 printf("#define ALGN .align 8\n");
255 printf("#else\n");
256 printf("#define PTR .long\n");
257 printf("#define ALGN .align 4\n");
258 printf("#endif\n");
259
Jan Beulichaad09472006-12-08 02:35:57 -0800260 printf("\t.section .rodata, \"a\"\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100262 /* Provide proper symbols relocatability by their '_text'
263 * relativeness. The symbol names cannot be used to construct
264 * normal symbol references as the list of symbols contains
265 * symbols that are declared static and are private to their
266 * .o files. This prevents .tmp_kallsyms.o or any other
267 * object from referencing them.
268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 output_label("kallsyms_addresses");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700270 for (i = 0; i < table_cnt; i++) {
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100271 if (toupper(table[i].sym[0]) != 'A') {
Vivek Goyal2c22d8b2006-12-07 02:14:10 +0100272 if (_text <= table[i].addr)
273 printf("\tPTR\t_text + %#llx\n",
274 table[i].addr - _text);
275 else
276 printf("\tPTR\t_text - %#llx\n",
277 _text - table[i].addr);
Eric W. Biedermanfd593d12006-12-07 02:14:04 +0100278 } else {
279 printf("\tPTR\t%#llx\n", table[i].addr);
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 printf("\n");
283
284 output_label("kallsyms_num_syms");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700285 printf("\tPTR\t%d\n", table_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 printf("\n");
287
288 /* table of offset markers, that give the offset in the compressed stream
289 * every 256 symbols */
Jesper Juhlf1a136e2006-03-25 03:07:46 -0800290 markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
291 if (!markers) {
292 fprintf(stderr, "kallsyms failure: "
293 "unable to allocate required memory\n");
294 exit(EXIT_FAILURE);
295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 output_label("kallsyms_names");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 off = 0;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700299 for (i = 0; i < table_cnt; i++) {
300 if ((i & 0xFF) == 0)
301 markers[i >> 8] = off;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 printf("\t.byte 0x%02x", table[i].len);
304 for (k = 0; k < table[i].len; k++)
305 printf(", 0x%02x", table[i].sym[k]);
306 printf("\n");
307
308 off += table[i].len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 printf("\n");
311
312 output_label("kallsyms_markers");
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700313 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 printf("\tPTR\t%d\n", markers[i]);
315 printf("\n");
316
317 free(markers);
318
319 output_label("kallsyms_token_table");
320 off = 0;
321 for (i = 0; i < 256; i++) {
322 best_idx[i] = off;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700323 expand_symbol(best_table[i], best_table_len[i], buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 printf("\t.asciz\t\"%s\"\n", buf);
325 off += strlen(buf) + 1;
326 }
327 printf("\n");
328
329 output_label("kallsyms_token_index");
330 for (i = 0; i < 256; i++)
331 printf("\t.short\t%d\n", best_idx[i]);
332 printf("\n");
333}
334
335
336/* table lookup compression functions */
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/* count all the possible tokens in a symbol */
339static void learn_symbol(unsigned char *symbol, int len)
340{
341 int i;
342
343 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700344 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/* decrease the count for all the possible tokens in a symbol */
348static void forget_symbol(unsigned char *symbol, int len)
349{
350 int i;
351
352 for (i = 0; i < len - 1; i++)
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700353 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700356/* remove all the invalid symbols from the table and do the initial token count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357static void build_initial_tok_table(void)
358{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700359 unsigned int i, pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700361 pos = 0;
362 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if ( symbol_valid(&table[i]) ) {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700364 if (pos != i)
365 table[pos] = table[i];
366 learn_symbol(table[pos].sym, table[pos].len);
367 pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 }
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700370 table_cnt = pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Paulo Marques7c5d2492007-06-20 18:09:00 +0100373static void *find_token(unsigned char *str, int len, unsigned char *token)
374{
375 int i;
376
377 for (i = 0; i < len - 1; i++) {
378 if (str[i] == token[0] && str[i+1] == token[1])
379 return &str[i];
380 }
381 return NULL;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/* replace a given token in all the valid symbols. Use the sampled symbols
385 * to update the counts */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700386static void compress_symbols(unsigned char *str, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700388 unsigned int i, len, size;
389 unsigned char *p1, *p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700391 for (i = 0; i < table_cnt; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 len = table[i].len;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700394 p1 = table[i].sym;
395
396 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100397 p2 = find_token(p1, len, str);
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700398 if (!p2) continue;
399
400 /* decrease the counts for this symbol's tokens */
401 forget_symbol(table[i].sym, len);
402
403 size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 do {
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700406 *p2 = idx;
407 p2++;
408 size -= (p2 - p1);
409 memmove(p2, p2 + 1, size);
410 p1 = p2;
411 len--;
412
413 if (size < 2) break;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* find the token on the symbol */
Paulo Marques7c5d2492007-06-20 18:09:00 +0100416 p2 = find_token(p1, size, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700418 } while (p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700420 table[i].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700422 /* increase the counts for this symbol's new tokens */
423 learn_symbol(table[i].sym, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425}
426
427/* search the token with the maximum profit */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700428static int find_best_token(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700430 int i, best, bestprofit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 bestprofit=-10000;
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700433 best = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700435 for (i = 0; i < 0x10000; i++) {
436 if (token_profit[i] > bestprofit) {
437 best = i;
438 bestprofit = token_profit[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return best;
442}
443
444/* this is the core of the algorithm: calculate the "best" table */
445static void optimize_result(void)
446{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700447 int i, best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* using the '\0' symbol last allows compress_symbols to use standard
450 * fast string functions */
451 for (i = 255; i >= 0; i--) {
452
453 /* if this table slot is empty (it is not used by an actual
454 * original char code */
455 if (!best_table_len[i]) {
456
457 /* find the token with the breates profit value */
458 best = find_best_token();
459
460 /* place it in the "best" table */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700461 best_table_len[i] = 2;
462 best_table[i][0] = best & 0xFF;
463 best_table[i][1] = (best >> 8) & 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 /* replace this token in all the valid symbols */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700466 compress_symbols(best_table[i], i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 }
469}
470
471/* start by placing the symbols that are actually used on the table */
472static void insert_real_symbols_in_table(void)
473{
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700474 unsigned int i, j, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 memset(best_table, 0, sizeof(best_table));
477 memset(best_table_len, 0, sizeof(best_table_len));
478
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700479 for (i = 0; i < table_cnt; i++) {
480 for (j = 0; j < table[i].len; j++) {
481 c = table[i].sym[j];
482 best_table[c][0]=c;
483 best_table_len[c]=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 }
486}
487
488static void optimize_token_table(void)
489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 build_initial_tok_table();
491
492 insert_real_symbols_in_table();
493
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700494 /* When valid symbol is not registered, exit to error */
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700495 if (!table_cnt) {
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700496 fprintf(stderr, "No valid symbol.\n");
497 exit(1);
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 optimize_result();
501}
502
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800503static int compare_symbols(const void *a, const void *b)
504{
505 const struct sym_entry *sa;
506 const struct sym_entry *sb;
507 int wa, wb;
508
509 sa = a;
510 sb = b;
511
512 /* sort by address first */
513 if (sa->addr > sb->addr)
514 return 1;
515 if (sa->addr < sb->addr)
516 return -1;
517
518 /* sort by "weakness" type */
519 wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W');
520 wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W');
521 if (wa != wb)
522 return wa - wb;
523
524 /* sort by initial order, so that other symbols are left undisturbed */
525 return sa->start_pos - sb->start_pos;
526}
527
528static void sort_symbols(void)
529{
530 qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols);
531}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Paulo Marquesb3dbb4e2005-09-06 15:16:31 -0700533int main(int argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Yoshinori Sato41f11a42005-05-01 08:59:06 -0700535 if (argc >= 2) {
536 int i;
537 for (i = 1; i < argc; i++) {
538 if(strcmp(argv[i], "--all-symbols") == 0)
539 all_symbols = 1;
540 else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
541 char *p = &argv[i][16];
542 /* skip quote */
543 if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
544 p++;
545 symbol_prefix_char = *p;
546 } else
547 usage();
548 }
549 } else if (argc != 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 usage();
551
552 read_map(stdin);
Paulo Marquesf2df3f62008-02-06 01:37:33 -0800553 sort_symbols();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 optimize_token_table();
555 write_src();
556
557 return 0;
558}