]>
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 | 36 | unsigned char *sym; |
8c996940 | 37 | unsigned int percpu_absolute; |
1da177e4 LT |
38 | }; |
39 | ||
78eb7159 KC |
40 | struct addr_range { |
41 | const char *start_sym, *end_sym; | |
17b1f0de MF |
42 | unsigned long long start, end; |
43 | }; | |
44 | ||
45 | static unsigned long long _text; | |
78eb7159 | 46 | static struct addr_range text_ranges[] = { |
17b1f0de MF |
47 | { "_stext", "_etext" }, |
48 | { "_sinittext", "_einittext" }, | |
49 | { "_stext_l1", "_etext_l1" }, /* Blackfin on-chip L1 inst SRAM */ | |
50 | { "_stext_l2", "_etext_l2" }, /* Blackfin on-chip L2 SRAM */ | |
51 | }; | |
52 | #define text_range_text (&text_ranges[0]) | |
53 | #define text_range_inittext (&text_ranges[1]) | |
54 | ||
c6bda7c9 RR |
55 | static struct addr_range percpu_range = { |
56 | "__per_cpu_start", "__per_cpu_end", -1ULL, 0 | |
57 | }; | |
58 | ||
1da177e4 | 59 | static struct sym_entry *table; |
b3dbb4ec | 60 | static unsigned int table_size, table_cnt; |
1da177e4 | 61 | static int all_symbols = 0; |
c6bda7c9 | 62 | static int absolute_percpu = 0; |
41f11a4f | 63 | static char symbol_prefix_char = '\0'; |
f6537f2f | 64 | static unsigned long long kernel_start_addr = 0; |
1da177e4 | 65 | |
b3dbb4ec | 66 | int token_profit[0x10000]; |
1da177e4 LT |
67 | |
68 | /* the table that holds the result of the compression */ | |
b3dbb4ec | 69 | unsigned char best_table[256][2]; |
1da177e4 LT |
70 | unsigned char best_table_len[256]; |
71 | ||
72 | ||
b3dbb4ec | 73 | static void usage(void) |
1da177e4 | 74 | { |
f6537f2f ML |
75 | fprintf(stderr, "Usage: kallsyms [--all-symbols] " |
76 | "[--symbol-prefix=<prefix char>] " | |
77 | "[--page-offset=<CONFIG_PAGE_OFFSET>] " | |
78 | "< in.map > out.S\n"); | |
1da177e4 LT |
79 | exit(1); |
80 | } | |
81 | ||
82 | /* | |
83 | * This ignores the intensely annoying "mapping symbols" found | |
84 | * in ARM ELF files: $a, $t and $d. | |
85 | */ | |
b3dbb4ec | 86 | static inline int is_arm_mapping_symbol(const char *str) |
1da177e4 | 87 | { |
6c34f1f5 | 88 | return str[0] == '$' && strchr("axtd", str[1]) |
1da177e4 LT |
89 | && (str[2] == '\0' || str[2] == '.'); |
90 | } | |
91 | ||
78eb7159 KC |
92 | static int check_symbol_range(const char *sym, unsigned long long addr, |
93 | struct addr_range *ranges, int entries) | |
17b1f0de MF |
94 | { |
95 | size_t i; | |
78eb7159 | 96 | struct addr_range *ar; |
17b1f0de | 97 | |
78eb7159 KC |
98 | for (i = 0; i < entries; ++i) { |
99 | ar = &ranges[i]; | |
17b1f0de | 100 | |
78eb7159 KC |
101 | if (strcmp(sym, ar->start_sym) == 0) { |
102 | ar->start = addr; | |
17b1f0de | 103 | return 0; |
78eb7159 KC |
104 | } else if (strcmp(sym, ar->end_sym) == 0) { |
105 | ar->end = addr; | |
17b1f0de MF |
106 | return 0; |
107 | } | |
108 | } | |
109 | ||
110 | return 1; | |
111 | } | |
112 | ||
b3dbb4ec | 113 | static int read_symbol(FILE *in, struct sym_entry *s) |
1da177e4 LT |
114 | { |
115 | char str[500]; | |
b3dbb4ec | 116 | char *sym, stype; |
1da177e4 LT |
117 | int rc; |
118 | ||
b3dbb4ec | 119 | rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str); |
1da177e4 | 120 | if (rc != 3) { |
ef894870 JS |
121 | if (rc != EOF && fgets(str, 500, in) == NULL) |
122 | fprintf(stderr, "Read error or end of file.\n"); | |
1da177e4 LT |
123 | return -1; |
124 | } | |
f3462aa9 | 125 | if (strlen(str) > KSYM_NAME_LEN) { |
6f62259b | 126 | fprintf(stderr, "Symbol %s too long for kallsyms (%zu vs %d).\n" |
bb66fc67 | 127 | "Please increase KSYM_NAME_LEN both in kernel and kallsyms.c\n", |
f3462aa9 AK |
128 | str, strlen(str), KSYM_NAME_LEN); |
129 | return -1; | |
130 | } | |
1da177e4 | 131 | |
41f11a4f YS |
132 | sym = str; |
133 | /* skip prefix char */ | |
134 | if (symbol_prefix_char && str[0] == symbol_prefix_char) | |
135 | sym++; | |
136 | ||
1da177e4 | 137 | /* Ignore most absolute/undefined (?) symbols. */ |
fd593d12 EB |
138 | if (strcmp(sym, "_text") == 0) |
139 | _text = s->addr; | |
78eb7159 KC |
140 | else if (check_symbol_range(sym, s->addr, text_ranges, |
141 | ARRAY_SIZE(text_ranges)) == 0) | |
17b1f0de | 142 | /* nothing to do */; |
b3dbb4ec | 143 | else if (toupper(stype) == 'A') |
1da177e4 LT |
144 | { |
145 | /* Keep these useful absolute symbols */ | |
41f11a4f YS |
146 | if (strcmp(sym, "__kernel_syscall_via_break") && |
147 | strcmp(sym, "__kernel_syscall_via_epc") && | |
148 | strcmp(sym, "__kernel_sigtramp") && | |
149 | strcmp(sym, "__gp")) | |
1da177e4 LT |
150 | return -1; |
151 | ||
152 | } | |
b3dbb4ec | 153 | else if (toupper(stype) == 'U' || |
41f11a4f | 154 | is_arm_mapping_symbol(sym)) |
1da177e4 | 155 | return -1; |
6f00df24 RB |
156 | /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */ |
157 | else if (str[0] == '$') | |
158 | return -1; | |
aab34ac8 SR |
159 | /* exclude debugging symbols */ |
160 | else if (stype == 'N') | |
161 | return -1; | |
1da177e4 LT |
162 | |
163 | /* include the type field in the symbol name, so that it gets | |
164 | * compressed together */ | |
165 | s->len = strlen(str) + 1; | |
b3dbb4ec | 166 | s->sym = malloc(s->len + 1); |
f1a136e0 JJ |
167 | if (!s->sym) { |
168 | fprintf(stderr, "kallsyms failure: " | |
169 | "unable to allocate required amount of memory\n"); | |
170 | exit(EXIT_FAILURE); | |
171 | } | |
b3dbb4ec PM |
172 | strcpy((char *)s->sym + 1, str); |
173 | s->sym[0] = stype; | |
1da177e4 | 174 | |
8c996940 AB |
175 | s->percpu_absolute = 0; |
176 | ||
c6bda7c9 RR |
177 | /* Record if we've found __per_cpu_start/end. */ |
178 | check_symbol_range(sym, s->addr, &percpu_range, 1); | |
179 | ||
1da177e4 LT |
180 | return 0; |
181 | } | |
182 | ||
78eb7159 KC |
183 | static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges, |
184 | int entries) | |
17b1f0de MF |
185 | { |
186 | size_t i; | |
78eb7159 | 187 | struct addr_range *ar; |
17b1f0de | 188 | |
78eb7159 KC |
189 | for (i = 0; i < entries; ++i) { |
190 | ar = &ranges[i]; | |
17b1f0de | 191 | |
78eb7159 | 192 | if (s->addr >= ar->start && s->addr <= ar->end) |
ac6ca5c8 | 193 | return 1; |
17b1f0de MF |
194 | } |
195 | ||
ac6ca5c8 | 196 | return 0; |
17b1f0de MF |
197 | } |
198 | ||
b3dbb4ec | 199 | static int symbol_valid(struct sym_entry *s) |
1da177e4 LT |
200 | { |
201 | /* Symbols which vary between passes. Passes 1 and 2 must have | |
2ea03891 SR |
202 | * identical symbol lists. The kallsyms_* symbols below are only added |
203 | * after pass 1, they would be included in pass 2 when --all-symbols is | |
204 | * specified so exclude them to get a stable symbol list. | |
1da177e4 LT |
205 | */ |
206 | static char *special_symbols[] = { | |
2ea03891 SR |
207 | "kallsyms_addresses", |
208 | "kallsyms_num_syms", | |
209 | "kallsyms_names", | |
210 | "kallsyms_markers", | |
211 | "kallsyms_token_table", | |
212 | "kallsyms_token_index", | |
213 | ||
1da177e4 LT |
214 | /* Exclude linker generated symbols which vary between passes */ |
215 | "_SDA_BASE_", /* ppc */ | |
216 | "_SDA2_BASE_", /* ppc */ | |
217 | NULL }; | |
bd8b22d2 AB |
218 | |
219 | static char *special_suffixes[] = { | |
bd8b22d2 AB |
220 | "_veneer", /* arm */ |
221 | NULL }; | |
222 | ||
1da177e4 | 223 | int i; |
bd8b22d2 AB |
224 | char *sym_name = (char *)s->sym + 1; |
225 | ||
41f11a4f | 226 | |
f6537f2f ML |
227 | if (s->addr < kernel_start_addr) |
228 | return 0; | |
229 | ||
41f11a4f | 230 | /* skip prefix char */ |
bd8b22d2 AB |
231 | if (symbol_prefix_char && *sym_name == symbol_prefix_char) |
232 | sym_name++; | |
233 | ||
1da177e4 LT |
234 | |
235 | /* if --all-symbols is not specified, then symbols outside the text | |
236 | * and inittext sections are discarded */ | |
237 | if (!all_symbols) { | |
78eb7159 KC |
238 | if (symbol_in_range(s, text_ranges, |
239 | ARRAY_SIZE(text_ranges)) == 0) | |
1da177e4 LT |
240 | return 0; |
241 | /* Corner case. Discard any symbols with the same value as | |
a3b81113 RG |
242 | * _etext _einittext; they can move between pass 1 and 2 when |
243 | * the kallsyms data are added. If these symbols move then | |
244 | * they may get dropped in pass 2, which breaks the kallsyms | |
245 | * rules. | |
1da177e4 | 246 | */ |
17b1f0de | 247 | if ((s->addr == text_range_text->end && |
bd8b22d2 | 248 | strcmp(sym_name, |
78eb7159 | 249 | text_range_text->end_sym)) || |
17b1f0de | 250 | (s->addr == text_range_inittext->end && |
bd8b22d2 | 251 | strcmp(sym_name, |
78eb7159 | 252 | text_range_inittext->end_sym))) |
1da177e4 LT |
253 | return 0; |
254 | } | |
255 | ||
256 | /* Exclude symbols which vary between passes. */ | |
1da177e4 | 257 | for (i = 0; special_symbols[i]; i++) |
bd8b22d2 | 258 | if (strcmp(sym_name, special_symbols[i]) == 0) |
1da177e4 LT |
259 | return 0; |
260 | ||
bd8b22d2 AB |
261 | for (i = 0; special_suffixes[i]; i++) { |
262 | int l = strlen(sym_name) - strlen(special_suffixes[i]); | |
263 | ||
264 | if (l >= 0 && strcmp(sym_name + l, special_suffixes[i]) == 0) | |
265 | return 0; | |
266 | } | |
267 | ||
1da177e4 LT |
268 | return 1; |
269 | } | |
270 | ||
b3dbb4ec | 271 | static void read_map(FILE *in) |
1da177e4 LT |
272 | { |
273 | while (!feof(in)) { | |
b3dbb4ec PM |
274 | if (table_cnt >= table_size) { |
275 | table_size += 10000; | |
276 | table = realloc(table, sizeof(*table) * table_size); | |
1da177e4 LT |
277 | if (!table) { |
278 | fprintf(stderr, "out of memory\n"); | |
279 | exit (1); | |
280 | } | |
281 | } | |
f2df3f65 PM |
282 | if (read_symbol(in, &table[table_cnt]) == 0) { |
283 | table[table_cnt].start_pos = table_cnt; | |
b3dbb4ec | 284 | table_cnt++; |
f2df3f65 | 285 | } |
1da177e4 LT |
286 | } |
287 | } | |
288 | ||
289 | static void output_label(char *label) | |
290 | { | |
41f11a4f YS |
291 | if (symbol_prefix_char) |
292 | printf(".globl %c%s\n", symbol_prefix_char, label); | |
293 | else | |
294 | printf(".globl %s\n", label); | |
1da177e4 | 295 | printf("\tALGN\n"); |
41f11a4f YS |
296 | if (symbol_prefix_char) |
297 | printf("%c%s:\n", symbol_prefix_char, label); | |
298 | else | |
299 | printf("%s:\n", label); | |
1da177e4 LT |
300 | } |
301 | ||
302 | /* uncompress a compressed symbol. When this function is called, the best table | |
303 | * might still be compressed itself, so the function needs to be recursive */ | |
304 | static int expand_symbol(unsigned char *data, int len, char *result) | |
305 | { | |
306 | int c, rlen, total=0; | |
307 | ||
308 | while (len) { | |
309 | c = *data; | |
310 | /* if the table holds a single char that is the same as the one | |
311 | * we are looking for, then end the search */ | |
312 | if (best_table[c][0]==c && best_table_len[c]==1) { | |
313 | *result++ = c; | |
314 | total++; | |
315 | } else { | |
316 | /* if not, recurse and expand */ | |
317 | rlen = expand_symbol(best_table[c], best_table_len[c], result); | |
318 | total += rlen; | |
319 | result += rlen; | |
320 | } | |
321 | data++; | |
322 | len--; | |
323 | } | |
324 | *result=0; | |
325 | ||
326 | return total; | |
327 | } | |
328 | ||
78eb7159 KC |
329 | static int symbol_absolute(struct sym_entry *s) |
330 | { | |
8c996940 | 331 | return s->percpu_absolute; |
78eb7159 KC |
332 | } |
333 | ||
b3dbb4ec | 334 | static void write_src(void) |
1da177e4 | 335 | { |
b3dbb4ec | 336 | unsigned int i, k, off; |
1da177e4 LT |
337 | unsigned int best_idx[256]; |
338 | unsigned int *markers; | |
9281acea | 339 | char buf[KSYM_NAME_LEN]; |
1da177e4 LT |
340 | |
341 | printf("#include <asm/types.h>\n"); | |
342 | printf("#if BITS_PER_LONG == 64\n"); | |
343 | printf("#define PTR .quad\n"); | |
344 | printf("#define ALGN .align 8\n"); | |
345 | printf("#else\n"); | |
346 | printf("#define PTR .long\n"); | |
347 | printf("#define ALGN .align 4\n"); | |
348 | printf("#endif\n"); | |
349 | ||
aad09470 | 350 | printf("\t.section .rodata, \"a\"\n"); |
1da177e4 | 351 | |
fd593d12 EB |
352 | /* Provide proper symbols relocatability by their '_text' |
353 | * relativeness. The symbol names cannot be used to construct | |
354 | * normal symbol references as the list of symbols contains | |
355 | * symbols that are declared static and are private to their | |
356 | * .o files. This prevents .tmp_kallsyms.o or any other | |
357 | * object from referencing them. | |
358 | */ | |
1da177e4 | 359 | output_label("kallsyms_addresses"); |
b3dbb4ec | 360 | for (i = 0; i < table_cnt; i++) { |
78eb7159 | 361 | if (!symbol_absolute(&table[i])) { |
2c22d8ba VG |
362 | if (_text <= table[i].addr) |
363 | printf("\tPTR\t_text + %#llx\n", | |
364 | table[i].addr - _text); | |
365 | else | |
2930ffc7 AM |
366 | printf("\tPTR\t_text - %#llx\n", |
367 | _text - table[i].addr); | |
fd593d12 EB |
368 | } else { |
369 | printf("\tPTR\t%#llx\n", table[i].addr); | |
370 | } | |
1da177e4 LT |
371 | } |
372 | printf("\n"); | |
373 | ||
374 | output_label("kallsyms_num_syms"); | |
b3dbb4ec | 375 | printf("\tPTR\t%d\n", table_cnt); |
1da177e4 LT |
376 | printf("\n"); |
377 | ||
378 | /* table of offset markers, that give the offset in the compressed stream | |
379 | * every 256 symbols */ | |
f1a136e0 JJ |
380 | markers = malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256)); |
381 | if (!markers) { | |
382 | fprintf(stderr, "kallsyms failure: " | |
383 | "unable to allocate required memory\n"); | |
384 | exit(EXIT_FAILURE); | |
385 | } | |
1da177e4 LT |
386 | |
387 | output_label("kallsyms_names"); | |
1da177e4 | 388 | off = 0; |
b3dbb4ec PM |
389 | for (i = 0; i < table_cnt; i++) { |
390 | if ((i & 0xFF) == 0) | |
391 | markers[i >> 8] = off; | |
1da177e4 LT |
392 | |
393 | printf("\t.byte 0x%02x", table[i].len); | |
394 | for (k = 0; k < table[i].len; k++) | |
395 | printf(", 0x%02x", table[i].sym[k]); | |
396 | printf("\n"); | |
397 | ||
398 | off += table[i].len + 1; | |
1da177e4 LT |
399 | } |
400 | printf("\n"); | |
401 | ||
402 | output_label("kallsyms_markers"); | |
b3dbb4ec | 403 | for (i = 0; i < ((table_cnt + 255) >> 8); i++) |
1da177e4 LT |
404 | printf("\tPTR\t%d\n", markers[i]); |
405 | printf("\n"); | |
406 | ||
407 | free(markers); | |
408 | ||
409 | output_label("kallsyms_token_table"); | |
410 | off = 0; | |
411 | for (i = 0; i < 256; i++) { | |
412 | best_idx[i] = off; | |
b3dbb4ec | 413 | expand_symbol(best_table[i], best_table_len[i], buf); |
1da177e4 LT |
414 | printf("\t.asciz\t\"%s\"\n", buf); |
415 | off += strlen(buf) + 1; | |
416 | } | |
417 | printf("\n"); | |
418 | ||
419 | output_label("kallsyms_token_index"); | |
420 | for (i = 0; i < 256; i++) | |
421 | printf("\t.short\t%d\n", best_idx[i]); | |
422 | printf("\n"); | |
423 | } | |
424 | ||
425 | ||
426 | /* table lookup compression functions */ | |
427 | ||
1da177e4 LT |
428 | /* count all the possible tokens in a symbol */ |
429 | static void learn_symbol(unsigned char *symbol, int len) | |
430 | { | |
431 | int i; | |
432 | ||
433 | for (i = 0; i < len - 1; i++) | |
b3dbb4ec | 434 | token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++; |
1da177e4 LT |
435 | } |
436 | ||
437 | /* decrease the count for all the possible tokens in a symbol */ | |
438 | static void forget_symbol(unsigned char *symbol, int len) | |
439 | { | |
440 | int i; | |
441 | ||
442 | for (i = 0; i < len - 1; i++) | |
b3dbb4ec | 443 | token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--; |
1da177e4 LT |
444 | } |
445 | ||
b3dbb4ec | 446 | /* remove all the invalid symbols from the table and do the initial token count */ |
1da177e4 LT |
447 | static void build_initial_tok_table(void) |
448 | { | |
b3dbb4ec | 449 | unsigned int i, pos; |
1da177e4 | 450 | |
b3dbb4ec PM |
451 | pos = 0; |
452 | for (i = 0; i < table_cnt; i++) { | |
1da177e4 | 453 | if ( symbol_valid(&table[i]) ) { |
b3dbb4ec PM |
454 | if (pos != i) |
455 | table[pos] = table[i]; | |
456 | learn_symbol(table[pos].sym, table[pos].len); | |
457 | pos++; | |
1da177e4 | 458 | } |
1da177e4 | 459 | } |
b3dbb4ec | 460 | table_cnt = pos; |
1da177e4 LT |
461 | } |
462 | ||
7c5d249a PM |
463 | static void *find_token(unsigned char *str, int len, unsigned char *token) |
464 | { | |
465 | int i; | |
466 | ||
467 | for (i = 0; i < len - 1; i++) { | |
468 | if (str[i] == token[0] && str[i+1] == token[1]) | |
469 | return &str[i]; | |
470 | } | |
471 | return NULL; | |
472 | } | |
473 | ||
1da177e4 LT |
474 | /* replace a given token in all the valid symbols. Use the sampled symbols |
475 | * to update the counts */ | |
b3dbb4ec | 476 | static void compress_symbols(unsigned char *str, int idx) |
1da177e4 | 477 | { |
b3dbb4ec PM |
478 | unsigned int i, len, size; |
479 | unsigned char *p1, *p2; | |
1da177e4 | 480 | |
b3dbb4ec | 481 | for (i = 0; i < table_cnt; i++) { |
1da177e4 LT |
482 | |
483 | len = table[i].len; | |
b3dbb4ec PM |
484 | p1 = table[i].sym; |
485 | ||
486 | /* find the token on the symbol */ | |
7c5d249a | 487 | p2 = find_token(p1, len, str); |
b3dbb4ec PM |
488 | if (!p2) continue; |
489 | ||
490 | /* decrease the counts for this symbol's tokens */ | |
491 | forget_symbol(table[i].sym, len); | |
492 | ||
493 | size = len; | |
1da177e4 LT |
494 | |
495 | do { | |
b3dbb4ec PM |
496 | *p2 = idx; |
497 | p2++; | |
498 | size -= (p2 - p1); | |
499 | memmove(p2, p2 + 1, size); | |
500 | p1 = p2; | |
501 | len--; | |
502 | ||
503 | if (size < 2) break; | |
504 | ||
1da177e4 | 505 | /* find the token on the symbol */ |
7c5d249a | 506 | p2 = find_token(p1, size, str); |
1da177e4 | 507 | |
b3dbb4ec | 508 | } while (p2); |
1da177e4 | 509 | |
b3dbb4ec | 510 | table[i].len = len; |
1da177e4 | 511 | |
b3dbb4ec PM |
512 | /* increase the counts for this symbol's new tokens */ |
513 | learn_symbol(table[i].sym, len); | |
1da177e4 LT |
514 | } |
515 | } | |
516 | ||
517 | /* search the token with the maximum profit */ | |
b3dbb4ec | 518 | static int find_best_token(void) |
1da177e4 | 519 | { |
b3dbb4ec | 520 | int i, best, bestprofit; |
1da177e4 LT |
521 | |
522 | bestprofit=-10000; | |
b3dbb4ec | 523 | best = 0; |
1da177e4 | 524 | |
b3dbb4ec PM |
525 | for (i = 0; i < 0x10000; i++) { |
526 | if (token_profit[i] > bestprofit) { | |
527 | best = i; | |
528 | bestprofit = token_profit[i]; | |
1da177e4 | 529 | } |
1da177e4 | 530 | } |
1da177e4 LT |
531 | return best; |
532 | } | |
533 | ||
534 | /* this is the core of the algorithm: calculate the "best" table */ | |
535 | static void optimize_result(void) | |
536 | { | |
b3dbb4ec | 537 | int i, best; |
1da177e4 LT |
538 | |
539 | /* using the '\0' symbol last allows compress_symbols to use standard | |
540 | * fast string functions */ | |
541 | for (i = 255; i >= 0; i--) { | |
542 | ||
543 | /* if this table slot is empty (it is not used by an actual | |
544 | * original char code */ | |
545 | if (!best_table_len[i]) { | |
546 | ||
547 | /* find the token with the breates profit value */ | |
548 | best = find_best_token(); | |
e0a04b11 XW |
549 | if (token_profit[best] == 0) |
550 | break; | |
1da177e4 LT |
551 | |
552 | /* place it in the "best" table */ | |
b3dbb4ec PM |
553 | best_table_len[i] = 2; |
554 | best_table[i][0] = best & 0xFF; | |
555 | best_table[i][1] = (best >> 8) & 0xFF; | |
1da177e4 LT |
556 | |
557 | /* replace this token in all the valid symbols */ | |
b3dbb4ec | 558 | compress_symbols(best_table[i], i); |
1da177e4 LT |
559 | } |
560 | } | |
561 | } | |
562 | ||
563 | /* start by placing the symbols that are actually used on the table */ | |
564 | static void insert_real_symbols_in_table(void) | |
565 | { | |
b3dbb4ec | 566 | unsigned int i, j, c; |
1da177e4 LT |
567 | |
568 | memset(best_table, 0, sizeof(best_table)); | |
569 | memset(best_table_len, 0, sizeof(best_table_len)); | |
570 | ||
b3dbb4ec PM |
571 | for (i = 0; i < table_cnt; i++) { |
572 | for (j = 0; j < table[i].len; j++) { | |
573 | c = table[i].sym[j]; | |
574 | best_table[c][0]=c; | |
575 | best_table_len[c]=1; | |
1da177e4 LT |
576 | } |
577 | } | |
578 | } | |
579 | ||
580 | static void optimize_token_table(void) | |
581 | { | |
1da177e4 LT |
582 | build_initial_tok_table(); |
583 | ||
584 | insert_real_symbols_in_table(); | |
585 | ||
41f11a4f | 586 | /* When valid symbol is not registered, exit to error */ |
b3dbb4ec | 587 | if (!table_cnt) { |
41f11a4f YS |
588 | fprintf(stderr, "No valid symbol.\n"); |
589 | exit(1); | |
590 | } | |
591 | ||
1da177e4 LT |
592 | optimize_result(); |
593 | } | |
594 | ||
b478b782 LJ |
595 | /* guess for "linker script provide" symbol */ |
596 | static int may_be_linker_script_provide_symbol(const struct sym_entry *se) | |
597 | { | |
598 | const char *symbol = (char *)se->sym + 1; | |
599 | int len = se->len - 1; | |
600 | ||
601 | if (len < 8) | |
602 | return 0; | |
603 | ||
604 | if (symbol[0] != '_' || symbol[1] != '_') | |
605 | return 0; | |
606 | ||
607 | /* __start_XXXXX */ | |
608 | if (!memcmp(symbol + 2, "start_", 6)) | |
609 | return 1; | |
610 | ||
611 | /* __stop_XXXXX */ | |
612 | if (!memcmp(symbol + 2, "stop_", 5)) | |
613 | return 1; | |
614 | ||
615 | /* __end_XXXXX */ | |
616 | if (!memcmp(symbol + 2, "end_", 4)) | |
617 | return 1; | |
618 | ||
619 | /* __XXXXX_start */ | |
620 | if (!memcmp(symbol + len - 6, "_start", 6)) | |
621 | return 1; | |
622 | ||
623 | /* __XXXXX_end */ | |
624 | if (!memcmp(symbol + len - 4, "_end", 4)) | |
625 | return 1; | |
626 | ||
627 | return 0; | |
628 | } | |
629 | ||
630 | static int prefix_underscores_count(const char *str) | |
631 | { | |
632 | const char *tail = str; | |
633 | ||
a9ece53c | 634 | while (*tail == '_') |
b478b782 LJ |
635 | tail++; |
636 | ||
637 | return tail - str; | |
638 | } | |
639 | ||
f2df3f65 PM |
640 | static int compare_symbols(const void *a, const void *b) |
641 | { | |
642 | const struct sym_entry *sa; | |
643 | const struct sym_entry *sb; | |
644 | int wa, wb; | |
645 | ||
646 | sa = a; | |
647 | sb = b; | |
648 | ||
649 | /* sort by address first */ | |
650 | if (sa->addr > sb->addr) | |
651 | return 1; | |
652 | if (sa->addr < sb->addr) | |
653 | return -1; | |
654 | ||
655 | /* sort by "weakness" type */ | |
656 | wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W'); | |
657 | wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W'); | |
658 | if (wa != wb) | |
659 | return wa - wb; | |
660 | ||
b478b782 LJ |
661 | /* sort by "linker script provide" type */ |
662 | wa = may_be_linker_script_provide_symbol(sa); | |
663 | wb = may_be_linker_script_provide_symbol(sb); | |
664 | if (wa != wb) | |
665 | return wa - wb; | |
666 | ||
667 | /* sort by the number of prefix underscores */ | |
668 | wa = prefix_underscores_count((const char *)sa->sym + 1); | |
669 | wb = prefix_underscores_count((const char *)sb->sym + 1); | |
670 | if (wa != wb) | |
671 | return wa - wb; | |
672 | ||
f2df3f65 PM |
673 | /* sort by initial order, so that other symbols are left undisturbed */ |
674 | return sa->start_pos - sb->start_pos; | |
675 | } | |
676 | ||
677 | static void sort_symbols(void) | |
678 | { | |
679 | qsort(table, table_cnt, sizeof(struct sym_entry), compare_symbols); | |
680 | } | |
1da177e4 | 681 | |
c6bda7c9 RR |
682 | static void make_percpus_absolute(void) |
683 | { | |
684 | unsigned int i; | |
685 | ||
686 | for (i = 0; i < table_cnt; i++) | |
8c996940 AB |
687 | if (symbol_in_range(&table[i], &percpu_range, 1)) { |
688 | /* | |
689 | * Keep the 'A' override for percpu symbols to | |
690 | * ensure consistent behavior compared to older | |
691 | * versions of this tool. | |
692 | */ | |
c6bda7c9 | 693 | table[i].sym[0] = 'A'; |
8c996940 AB |
694 | table[i].percpu_absolute = 1; |
695 | } | |
c6bda7c9 RR |
696 | } |
697 | ||
b3dbb4ec | 698 | int main(int argc, char **argv) |
1da177e4 | 699 | { |
41f11a4f YS |
700 | if (argc >= 2) { |
701 | int i; | |
702 | for (i = 1; i < argc; i++) { | |
703 | if(strcmp(argv[i], "--all-symbols") == 0) | |
704 | all_symbols = 1; | |
c6bda7c9 RR |
705 | else if (strcmp(argv[i], "--absolute-percpu") == 0) |
706 | absolute_percpu = 1; | |
41f11a4f YS |
707 | else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) { |
708 | char *p = &argv[i][16]; | |
709 | /* skip quote */ | |
710 | if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\'')) | |
711 | p++; | |
712 | symbol_prefix_char = *p; | |
f6537f2f ML |
713 | } else if (strncmp(argv[i], "--page-offset=", 14) == 0) { |
714 | const char *p = &argv[i][14]; | |
715 | kernel_start_addr = strtoull(p, NULL, 16); | |
41f11a4f YS |
716 | } else |
717 | usage(); | |
718 | } | |
719 | } else if (argc != 1) | |
1da177e4 LT |
720 | usage(); |
721 | ||
722 | read_map(stdin); | |
c6bda7c9 RR |
723 | if (absolute_percpu) |
724 | make_percpus_absolute(); | |
2ea03891 SR |
725 | sort_symbols(); |
726 | optimize_token_table(); | |
1da177e4 LT |
727 | write_src(); |
728 | ||
729 | return 0; | |
730 | } |