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