]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* 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 | * | |
1da177e4 LT |
10 | * 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 | ||
17b1f0de MF |
26 | #ifndef ARRAY_SIZE |
27 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) | |
28 | #endif | |
29 | ||
9281acea | 30 | #define KSYM_NAME_LEN 128 |
1da177e4 | 31 | |
1da177e4 LT |
32 | struct sym_entry { |
33 | unsigned long long addr; | |
b3dbb4ec | 34 | unsigned int len; |
f2df3f65 | 35 | unsigned int start_pos; |
1da177e4 LT |
36 | unsigned char *sym; |
37 | }; | |
38 | ||
78eb7159 KC |
39 | struct addr_range { |
40 | const char *start_sym, *end_sym; | |
17b1f0de MF |
41 | unsigned long long start, end; |
42 | }; | |
43 | ||
44 | static unsigned long long _text; | |
78eb7159 | 45 | static struct addr_range text_ranges[] = { |
17b1f0de MF |
46 | { "_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 | ||
1da177e4 | 54 | static struct sym_entry *table; |
b3dbb4ec | 55 | static unsigned int table_size, table_cnt; |
1da177e4 | 56 | static int all_symbols = 0; |
41f11a4f | 57 | static char symbol_prefix_char = '\0'; |
f6537f2f | 58 | static unsigned long long kernel_start_addr = 0; |
1da177e4 | 59 | |
b3dbb4ec | 60 | int token_profit[0x10000]; |
1da177e4 LT |
61 | |
62 | /* the table that holds the result of the compression */ | |
b3dbb4ec | 63 | unsigned char best_table[256][2]; |
1da177e4 LT |
64 | unsigned char best_table_len[256]; |
65 | ||
66 | ||
b3dbb4ec | 67 | static void usage(void) |
1da177e4 | 68 | { |
f6537f2f ML |
69 | fprintf(stderr, "Usage: kallsyms [--all-symbols] " |
70 | "[--symbol-prefix=<prefix char>] " | |
71 | "[--page-offset=<CONFIG_PAGE_OFFSET>] " | |
72 | "< in.map > out.S\n"); | |
1da177e4 LT |
73 | exit(1); |
74 | } | |
75 | ||
76 | /* | |
77 | * This ignores the intensely annoying "mapping symbols" found | |
78 | * in ARM ELF files: $a, $t and $d. | |
79 | */ | |
b3dbb4ec | 80 | static inline int is_arm_mapping_symbol(const char *str) |
1da177e4 LT |
81 | { |
82 | return str[0] == '$' && strchr("atd", str[1]) | |
83 | && (str[2] == '\0' || str[2] == '.'); | |
84 | } | |
85 | ||
78eb7159 KC |
86 | static int check_symbol_range(const char *sym, unsigned long long addr, |
87 | struct addr_range *ranges, int entries) | |
17b1f0de MF |
88 | { |
89 | size_t i; | |
78eb7159 | 90 | struct addr_range *ar; |
17b1f0de | 91 | |
78eb7159 KC |
92 | for (i = 0; i < entries; ++i) { |
93 | ar = &ranges[i]; | |
17b1f0de | 94 | |
78eb7159 KC |
95 | if (strcmp(sym, ar->start_sym) == 0) { |
96 | ar->start = addr; | |
17b1f0de | 97 | return 0; |
78eb7159 KC |
98 | } else if (strcmp(sym, ar->end_sym) == 0) { |
99 | ar->end = addr; | |
17b1f0de MF |
100 | return 0; |
101 | } | |
102 | } | |
103 | ||
104 | return 1; | |
105 | } | |
106 | ||
b3dbb4ec | 107 | static int read_symbol(FILE *in, struct sym_entry *s) |
1da177e4 LT |
108 | { |
109 | char str[500]; | |
b3dbb4ec | 110 | char *sym, stype; |
1da177e4 LT |
111 | int rc; |
112 | ||
b3dbb4ec | 113 | rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str); |
1da177e4 | 114 | if (rc != 3) { |
ef894870 JS |
115 | if (rc != EOF && fgets(str, 500, in) == NULL) |
116 | fprintf(stderr, "Read error or end of file.\n"); | |
1da177e4 LT |
117 | return -1; |
118 | } | |
f3462aa9 | 119 | if (strlen(str) > KSYM_NAME_LEN) { |
6f62259b | 120 | fprintf(stderr, "Symbol %s too long for kallsyms (%zu vs %d).\n" |
f3462aa9 AK |
121 | "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n", |
122 | str, strlen(str), KSYM_NAME_LEN); | |
123 | return -1; | |
124 | } | |
1da177e4 | 125 | |
41f11a4f YS |
126 | sym = str; |
127 | /* skip prefix char */ | |
128 | if (symbol_prefix_char && str[0] == symbol_prefix_char) | |
129 | sym++; | |
130 | ||
1da177e4 | 131 | /* Ignore most absolute/undefined (?) symbols. */ |
fd593d12 EB |
132 | if (strcmp(sym, "_text") == 0) |
133 | _text = s->addr; | |
78eb7159 KC |
134 | else if (check_symbol_range(sym, s->addr, text_ranges, |
135 | ARRAY_SIZE(text_ranges)) == 0) | |
17b1f0de | 136 | /* nothing to do */; |
b3dbb4ec | 137 | else if (toupper(stype) == 'A') |
1da177e4 LT |
138 | { |
139 | /* Keep these useful absolute symbols */ | |
41f11a4f YS |
140 | if (strcmp(sym, "__kernel_syscall_via_break") && |
141 | strcmp(sym, "__kernel_syscall_via_epc") && | |
142 | strcmp(sym, "__kernel_sigtramp") && | |
143 | strcmp(sym, "__gp")) | |
1da177e4 LT |
144 | return -1; |
145 | ||
146 | } | |
b3dbb4ec | 147 | else if (toupper(stype) == 'U' || |
41f11a4f | 148 | is_arm_mapping_symbol(sym)) |
1da177e4 | 149 | return -1; |
6f00df24 RB |
150 | /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */ |
151 | else if (str[0] == '$') | |
152 | return -1; | |
aab34ac8 SR |
153 | /* exclude debugging symbols */ |
154 | else if (stype == 'N') | |
155 | return -1; | |
1da177e4 LT |
156 | |
157 | /* include the type field in the symbol name, so that it gets | |
158 | * compressed together */ | |
159 | s->len = strlen(str) + 1; | |
b3dbb4ec | 160 | s->sym = malloc(s->len + 1); |
f1a136e0 JJ |
161 | if (!s->sym) { |
162 | fprintf(stderr, "kallsyms failure: " | |
163 | "unable to allocate required amount of memory\n"); | |
164 | exit(EXIT_FAILURE); | |
165 | } | |
b3dbb4ec PM |
166 | strcpy((char *)s->sym + 1, str); |
167 | s->sym[0] = stype; | |
1da177e4 LT |
168 | |
169 | return 0; | |
170 | } | |
171 | ||
78eb7159 KC |
172 | static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges, |
173 | int entries) | |
17b1f0de MF |
174 | { |
175 | size_t i; | |
78eb7159 | 176 | struct addr_range *ar; |
17b1f0de | 177 | |
78eb7159 KC |
178 | for (i = 0; i < entries; ++i) { |
179 | ar = &ranges[i]; | |
17b1f0de | 180 | |
78eb7159 | 181 | if (s->addr >= ar->start && s->addr <= ar->end) |
ac6ca5c8 | 182 | return 1; |
17b1f0de MF |
183 | } |
184 | ||
ac6ca5c8 | 185 | return 0; |
17b1f0de MF |
186 | } |
187 | ||
b3dbb4ec | 188 | static int symbol_valid(struct sym_entry *s) |
1da177e4 LT |
189 | { |
190 | /* Symbols which vary between passes. Passes 1 and 2 must have | |
2ea03891 SR |
191 | * 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. | |
1da177e4 LT |
194 | */ |
195 | static char *special_symbols[] = { | |
2ea03891 SR |
196 | "kallsyms_addresses", |
197 | "kallsyms_num_syms", | |
198 | "kallsyms_names", | |
199 | "kallsyms_markers", | |
200 | "kallsyms_token_table", | |
201 | "kallsyms_token_index", | |
202 | ||
1da177e4 LT |
203 | /* Exclude linker generated symbols which vary between passes */ |
204 | "_SDA_BASE_", /* ppc */ | |
205 | "_SDA2_BASE_", /* ppc */ | |
206 | NULL }; | |
207 | int i; | |
41f11a4f YS |
208 | int offset = 1; |
209 | ||
f6537f2f ML |
210 | if (s->addr < kernel_start_addr) |
211 | return 0; | |
212 | ||
41f11a4f YS |
213 | /* skip prefix char */ |
214 | if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char) | |
215 | offset++; | |
1da177e4 LT |
216 | |
217 | /* if --all-symbols is not specified, then symbols outside the text | |
218 | * and inittext sections are discarded */ | |
219 | if (!all_symbols) { | |
78eb7159 KC |
220 | if (symbol_in_range(s, text_ranges, |
221 | ARRAY_SIZE(text_ranges)) == 0) | |
1da177e4 LT |
222 | return 0; |
223 | /* Corner case. Discard any symbols with the same value as | |
a3b81113 RG |
224 | * _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. | |
1da177e4 | 228 | */ |
17b1f0de | 229 | if ((s->addr == text_range_text->end && |
78eb7159 KC |
230 | strcmp((char *)s->sym + offset, |
231 | text_range_text->end_sym)) || | |
17b1f0de | 232 | (s->addr == text_range_inittext->end && |
78eb7159 KC |
233 | strcmp((char *)s->sym + offset, |
234 | text_range_inittext->end_sym))) | |
1da177e4 LT |
235 | return 0; |
236 | } | |
237 | ||
238 | /* Exclude symbols which vary between passes. */ | |
2ea03891 | 239 | if (strstr((char *)s->sym + offset, "_compiled.")) |
1da177e4 LT |
240 | return 0; |
241 | ||
242 | for (i = 0; special_symbols[i]; i++) | |
b3dbb4ec | 243 | if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 ) |
1da177e4 LT |
244 | return 0; |
245 | ||
246 | return 1; | |
247 | } | |
248 | ||
b3dbb4ec | 249 | static void read_map(FILE *in) |
1da177e4 LT |
250 | { |
251 | while (!feof(in)) { | |
b3dbb4ec PM |
252 | if (table_cnt >= table_size) { |
253 | table_size += 10000; | |
254 | table = realloc(table, sizeof(*table) * table_size); | |
1da177e4 LT |
255 | if (!table) { |
256 | fprintf(stderr, "out of memory\n"); | |
257 | exit (1); | |
258 | } | |
259 | } | |
f2df3f65 PM |
260 | if (read_symbol(in, &table[table_cnt]) == 0) { |
261 | table[table_cnt].start_pos = table_cnt; | |
b3dbb4ec | 262 | table_cnt++; |
f2df3f65 | 263 | } |
1da177e4 LT |
264 | } |
265 | } | |
266 | ||
267 | static void output_label(char *label) | |
268 | { | |
41f11a4f YS |
269 | if (symbol_prefix_char) |
270 | printf(".globl %c%s\n", symbol_prefix_char, label); | |
271 | else | |
272 | printf(".globl %s\n", label); | |
1da177e4 | 273 | printf("\tALGN\n"); |
41f11a4f YS |
274 | if (symbol_prefix_char) |
275 | printf("%c%s:\n", symbol_prefix_char, label); | |
276 | else | |
277 | printf("%s:\n", label); | |
1da177e4 LT |
278 | } |
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 */ | |
282 | static 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 | ||
78eb7159 KC |
307 | static int symbol_absolute(struct sym_entry *s) |
308 | { | |
309 | return toupper(s->sym[0]) == 'A'; | |
310 | } | |
311 | ||
b3dbb4ec | 312 | static void write_src(void) |
1da177e4 | 313 | { |
b3dbb4ec | 314 | unsigned int i, k, off; |
1da177e4 LT |
315 | unsigned int best_idx[256]; |
316 | unsigned int *markers; | |
9281acea | 317 | char buf[KSYM_NAME_LEN]; |
1da177e4 LT |
318 | |
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 | ||
aad09470 | 328 | printf("\t.section .rodata, \"a\"\n"); |
1da177e4 | 329 | |
fd593d12 EB |
330 | /* 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 | */ | |
1da177e4 | 337 | output_label("kallsyms_addresses"); |
b3dbb4ec | 338 | for (i = 0; i < table_cnt; i++) { |
78eb7159 | 339 | if (!symbol_absolute(&table[i])) { |
2c22d8ba VG |
340 | if (_text <= table[i].addr) |
341 | printf("\tPTR\t_text + %#llx\n", | |
342 | table[i].addr - _text); | |
343 | else | |
2930ffc7 AM |
344 | printf("\tPTR\t_text - %#llx\n", |
345 | _text - table[i].addr); | |
fd593d12 EB |
346 | } else { |
347 | printf("\tPTR\t%#llx\n", table[i].addr); | |
348 | } | |
1da177e4 LT |
349 | } |
350 | printf("\n"); | |
351 | ||
352 | output_label("kallsyms_num_syms"); | |
b3dbb4ec | 353 | printf("\tPTR\t%d\n", table_cnt); |
1da177e4 LT |
354 | printf("\n"); |
355 | ||
356 | /* table of offset markers, that give the offset in the compressed stream | |
357 | * every 256 symbols */ | |
f1a136e0 JJ |
358 | 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 | } | |
1da177e4 LT |
364 | |
365 | output_label("kallsyms_names"); | |
1da177e4 | 366 | off = 0; |
b3dbb4ec PM |
367 | for (i = 0; i < table_cnt; i++) { |
368 | if ((i & 0xFF) == 0) | |
369 | markers[i >> 8] = off; | |
1da177e4 LT |
370 | |
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; | |
1da177e4 LT |
377 | } |
378 | printf("\n"); | |
379 | ||
380 | output_label("kallsyms_markers"); | |
b3dbb4ec | 381 | for (i = 0; i < ((table_cnt + 255) >> 8); i++) |
1da177e4 LT |
382 | 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; | |
b3dbb4ec | 391 | expand_symbol(best_table[i], best_table_len[i], buf); |
1da177e4 LT |
392 | 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 | ||
1da177e4 LT |
406 | /* count all the possible tokens in a symbol */ |
407 | static void learn_symbol(unsigned char *symbol, int len) | |
408 | { | |
409 | int i; | |
410 | ||
411 | for (i = 0; i < len - 1; i++) | |
b3dbb4ec | 412 | token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; |
1da177e4 LT |
413 | } |
414 | ||
415 | /* decrease the count for all the possible tokens in a symbol */ | |
416 | static void forget_symbol(unsigned char *symbol, int len) | |
417 | { | |
418 | int i; | |
419 | ||
420 | for (i = 0; i < len - 1; i++) | |
b3dbb4ec | 421 | token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--; |
1da177e4 LT |
422 | } |
423 | ||
b3dbb4ec | 424 | /* remove all the invalid symbols from the table and do the initial token count */ |
1da177e4 LT |
425 | static void build_initial_tok_table(void) |
426 | { | |
b3dbb4ec | 427 | unsigned int i, pos; |
1da177e4 | 428 | |
b3dbb4ec PM |
429 | pos = 0; |
430 | for (i = 0; i < table_cnt; i++) { | |
1da177e4 | 431 | if ( symbol_valid(&table[i]) ) { |
b3dbb4ec PM |
432 | if (pos != i) |
433 | table[pos] = table[i]; | |
434 | learn_symbol(table[pos].sym, table[pos].len); | |
435 | pos++; | |
1da177e4 | 436 | } |
1da177e4 | 437 | } |
b3dbb4ec | 438 | table_cnt = pos; |
1da177e4 LT |
439 | } |
440 | ||
7c5d249a PM |
441 | static 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 | ||
1da177e4 LT |
452 | /* replace a given token in all the valid symbols. Use the sampled symbols |
453 | * to update the counts */ | |
b3dbb4ec | 454 | static void compress_symbols(unsigned char *str, int idx) |
1da177e4 | 455 | { |
b3dbb4ec PM |
456 | unsigned int i, len, size; |
457 | unsigned char *p1, *p2; | |
1da177e4 | 458 | |
b3dbb4ec | 459 | for (i = 0; i < table_cnt; i++) { |
1da177e4 LT |
460 | |
461 | len = table[i].len; | |
b3dbb4ec PM |
462 | p1 = table[i].sym; |
463 | ||
464 | /* find the token on the symbol */ | |
7c5d249a | 465 | p2 = find_token(p1, len, str); |
b3dbb4ec PM |
466 | if (!p2) continue; |
467 | ||
468 | /* decrease the counts for this symbol's tokens */ | |
469 | forget_symbol(table[i].sym, len); | |
470 | ||
471 | size = len; | |
1da177e4 LT |
472 | |
473 | do { | |
b3dbb4ec PM |
474 | *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 | ||
1da177e4 | 483 | /* find the token on the symbol */ |
7c5d249a | 484 | p2 = find_token(p1, size, str); |
1da177e4 | 485 | |
b3dbb4ec | 486 | } while (p2); |
1da177e4 | 487 | |
b3dbb4ec | 488 | table[i].len = len; |
1da177e4 | 489 | |
b3dbb4ec PM |
490 | /* increase the counts for this symbol's new tokens */ |
491 | learn_symbol(table[i].sym, len); | |
1da177e4 LT |
492 | } |
493 | } | |
494 | ||
495 | /* search the token with the maximum profit */ | |
b3dbb4ec | 496 | static int find_best_token(void) |
1da177e4 | 497 | { |
b3dbb4ec | 498 | int i, best, bestprofit; |
1da177e4 LT |
499 | |
500 | bestprofit=-10000; | |
b3dbb4ec | 501 | best = 0; |
1da177e4 | 502 | |
b3dbb4ec PM |
503 | for (i = 0; i < 0x10000; i++) { |
504 | if (token_profit[i] > bestprofit) { | |
505 | best = i; | |
506 | bestprofit = token_profit[i]; | |
1da177e4 | 507 | } |
1da177e4 | 508 | } |
1da177e4 LT |
509 | return best; |
510 | } | |
511 | ||
512 | /* this is the core of the algorithm: calculate the "best" table */ | |
513 | static void optimize_result(void) | |
514 | { | |
b3dbb4ec | 515 | int i, best; |
1da177e4 LT |
516 | |
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(); | |
e0a04b11 XW |
527 | if (token_profit[best] == 0) |
528 | break; | |
1da177e4 LT |
529 | |
530 | /* place it in the "best" table */ | |
b3dbb4ec PM |
531 | best_table_len[i] = 2; |
532 | best_table[i][0] = best & 0xFF; | |
533 | best_table[i][1] = (best >> 8) & 0xFF; | |
1da177e4 LT |
534 | |
535 | /* replace this token in all the valid symbols */ | |
b3dbb4ec | 536 | compress_symbols(best_table[i], i); |
1da177e4 LT |
537 | } |
538 | } | |
539 | } | |
540 | ||
541 | /* start by placing the symbols that are actually used on the table */ | |
542 | static void insert_real_symbols_in_table(void) | |
543 | { | |
b3dbb4ec | 544 | unsigned int i, j, c; |
1da177e4 LT |
545 | |
546 | memset(best_table, 0, sizeof(best_table)); | |
547 | memset(best_table_len, 0, sizeof(best_table_len)); | |
548 | ||
b3dbb4ec PM |
549 | 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; | |
1da177e4 LT |
554 | } |
555 | } | |
556 | } | |
557 | ||
558 | static void optimize_token_table(void) | |
559 | { | |
1da177e4 LT |
560 | build_initial_tok_table(); |
561 | ||
562 | insert_real_symbols_in_table(); | |
563 | ||
41f11a4f | 564 | /* When valid symbol is not registered, exit to error */ |
b3dbb4ec | 565 | if (!table_cnt) { |
41f11a4f YS |
566 | fprintf(stderr, "No valid symbol.\n"); |
567 | exit(1); | |
568 | } | |
569 | ||
1da177e4 LT |
570 | optimize_result(); |
571 | } | |
572 | ||
b478b782 LJ |
573 | /* guess for "linker script provide" symbol */ |
574 | static 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 | ||
608 | static int prefix_underscores_count(const char *str) | |
609 | { | |
610 | const char *tail = str; | |
611 | ||
a9ece53c | 612 | while (*tail == '_') |
b478b782 LJ |
613 | tail++; |
614 | ||
615 | return tail - str; | |
616 | } | |
617 | ||
f2df3f65 PM |
618 | static 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 | ||
b478b782 LJ |
639 | /* 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 | ||
f2df3f65 PM |
651 | /* sort by initial order, so that other symbols are left undisturbed */ |
652 | return sa->start_pos - sb->start_pos; | |
653 | } | |
654 | ||
655 | static void sort_symbols(void) | |
656 | { | |
657 | qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols); | |
658 | } | |
1da177e4 | 659 | |
b3dbb4ec | 660 | int main(int argc, char **argv) |
1da177e4 | 661 | { |
41f11a4f YS |
662 | 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; | |
f6537f2f ML |
673 | } else if (strncmp(argv[i], "--page-offset=", 14) == 0) { |
674 | const char *p = &argv[i][14]; | |
675 | kernel_start_addr = strtoull(p, NULL, 16); | |
41f11a4f YS |
676 | } else |
677 | usage(); | |
678 | } | |
679 | } else if (argc != 1) | |
1da177e4 LT |
680 | usage(); |
681 | ||
682 | read_map(stdin); | |
2ea03891 SR |
683 | sort_symbols(); |
684 | optimize_token_table(); | |
1da177e4 LT |
685 | write_src(); |
686 | ||
687 | return 0; | |
688 | } |