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