ia64/xen-unstable

view xen/tools/symbols.c @ 18842:9be2fe3de567

shadow: fix the fix for promotion/resync race.

Signed-off-by: Gianluca Guida <gianluca.guida@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Nov 28 12:02:43 2008 +0000 (2008-11-28)
parents 8d5474a5c66b
children
line source
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/symbols [--all-symbols] > symbols.S
9 *
10 * ChangeLog:
11 *
12 * (25/Aug/2004) Paulo Marques <pmarques@grupopie.com>
13 * Changed the compression method from stem compression to "table lookup"
14 * compression
15 *
16 * Table compression uses all the unused char codes on the symbols and
17 * maps these to the most used substrings (tokens). For instance, it might
18 * map char code 0xF7 to represent "write_" and then in every symbol where
19 * "write_" appears it can be replaced by 0xF7, saving 5 bytes.
20 * The used codes themselves are also placed in the table so that the
21 * decompresion can work without "special cases".
22 * Applied to kernel symbols, this usually produces a compression ratio
23 * of about 50%.
24 *
25 */
27 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <ctype.h>
35 #define KSYM_NAME_LEN 127
38 struct sym_entry {
39 unsigned long long addr;
40 unsigned int len;
41 unsigned char *sym;
42 };
45 static struct sym_entry *table;
46 static unsigned int table_size, table_cnt;
47 static unsigned long long _stext, _etext, _sinittext, _einittext, _sextratext, _eextratext;
48 static int all_symbols = 0;
49 static char symbol_prefix_char = '\0';
51 int token_profit[0x10000];
53 /* the table that holds the result of the compression */
54 unsigned char best_table[256][2];
55 unsigned char best_table_len[256];
58 static void usage(void)
59 {
60 fprintf(stderr, "Usage: symbols [--all-symbols] [--symbol-prefix=<prefix char>] < in.map > out.S\n");
61 exit(1);
62 }
64 /*
65 * This ignores the intensely annoying "mapping symbols" found
66 * in ARM ELF files: $a, $t and $d.
67 */
68 static inline int is_arm_mapping_symbol(const char *str)
69 {
70 return str[0] == '$' && strchr("atd", str[1])
71 && (str[2] == '\0' || str[2] == '.');
72 }
74 static int read_symbol(FILE *in, struct sym_entry *s)
75 {
76 char str[500];
77 char *sym, stype;
78 int rc;
80 rc = fscanf(in, "%llx %c %499s\n", &s->addr, &stype, str);
81 if (rc != 3) {
82 if (rc != EOF) {
83 /* skip line */
84 if (fgets(str, 500, in) == NULL)
85 return -1; /* must check fgets result */
86 }
87 return -1;
88 }
90 sym = str;
91 /* skip prefix char */
92 if (symbol_prefix_char && str[0] == symbol_prefix_char)
93 sym++;
95 /* Ignore most absolute/undefined (?) symbols. */
96 if (strcmp(sym, "_stext") == 0)
97 _stext = s->addr;
98 else if (strcmp(sym, "_etext") == 0)
99 _etext = s->addr;
100 else if (strcmp(sym, "_sinittext") == 0)
101 _sinittext = s->addr;
102 else if (strcmp(sym, "_einittext") == 0)
103 _einittext = s->addr;
104 else if (strcmp(sym, "_sextratext") == 0)
105 _sextratext = s->addr;
106 else if (strcmp(sym, "_eextratext") == 0)
107 _eextratext = s->addr;
108 else if (toupper((uint8_t)stype) == 'A')
109 {
110 /* Keep these useful absolute symbols */
111 if (strcmp(sym, "__kernel_syscall_via_break") &&
112 strcmp(sym, "__kernel_syscall_via_epc") &&
113 strcmp(sym, "__kernel_sigtramp") &&
114 strcmp(sym, "__gp"))
115 return -1;
117 }
118 else if (toupper((uint8_t)stype) == 'U' ||
119 is_arm_mapping_symbol(sym))
120 return -1;
121 /* exclude also MIPS ELF local symbols ($L123 instead of .L123) */
122 else if (str[0] == '$')
123 return -1;
125 /* include the type field in the symbol name, so that it gets
126 * compressed together */
127 s->len = strlen(str) + 1;
128 s->sym = malloc(s->len + 1);
129 strcpy((char *)s->sym + 1, str);
130 s->sym[0] = stype;
132 return 0;
133 }
135 static int symbol_valid(struct sym_entry *s)
136 {
137 /* Symbols which vary between passes. Passes 1 and 2 must have
138 * identical symbol lists. The symbols_* symbols below are only added
139 * after pass 1, they would be included in pass 2 when --all-symbols is
140 * specified so exclude them to get a stable symbol list.
141 */
142 static char *special_symbols[] = {
143 "symbols_addresses",
144 "symbols_num_syms",
145 "symbols_names",
146 "symbols_markers",
147 "symbols_token_table",
148 "symbols_token_index",
150 /* Exclude linker generated symbols which vary between passes */
151 "_SDA_BASE_", /* ppc */
152 "_SDA2_BASE_", /* ppc */
153 NULL };
154 int i;
155 int offset = 1;
157 /* skip prefix char */
158 if (symbol_prefix_char && *(s->sym + 1) == symbol_prefix_char)
159 offset++;
161 /* if --all-symbols is not specified, then symbols outside the text
162 * and inittext sections are discarded */
163 if (!all_symbols) {
164 if ((s->addr < _stext || s->addr > _etext)
165 && (s->addr < _sinittext || s->addr > _einittext)
166 && (s->addr < _sextratext || s->addr > _eextratext))
167 return 0;
168 /* Corner case. Discard any symbols with the same value as
169 * _etext _einittext or _eextratext; they can move between pass
170 * 1 and 2 when the symbols data are added. If these symbols
171 * move then they may get dropped in pass 2, which breaks the
172 * symbols rules.
173 */
174 if ((s->addr == _etext && strcmp((char*)s->sym + offset, "_etext")) ||
175 (s->addr == _einittext && strcmp((char*)s->sym + offset, "_einittext")) ||
176 (s->addr == _eextratext && strcmp((char*)s->sym + offset, "_eextratext")))
177 return 0;
178 }
180 /* Exclude symbols which vary between passes. */
181 if (strstr((char *)s->sym + offset, "_compiled."))
182 return 0;
184 for (i = 0; special_symbols[i]; i++)
185 if( strcmp((char *)s->sym + offset, special_symbols[i]) == 0 )
186 return 0;
188 return 1;
189 }
191 static void read_map(FILE *in)
192 {
193 while (!feof(in)) {
194 if (table_cnt >= table_size) {
195 table_size += 10000;
196 table = realloc(table, sizeof(*table) * table_size);
197 if (!table) {
198 fprintf(stderr, "out of memory\n");
199 exit (1);
200 }
201 }
202 if (read_symbol(in, &table[table_cnt]) == 0)
203 table_cnt++;
204 }
205 }
207 static void output_label(char *label)
208 {
209 if (symbol_prefix_char)
210 printf(".globl %c%s\n", symbol_prefix_char, label);
211 else
212 printf(".globl %s\n", label);
213 printf("\tALGN\n");
214 if (symbol_prefix_char)
215 printf("%c%s:\n", symbol_prefix_char, label);
216 else
217 printf("%s:\n", label);
218 }
220 /* uncompress a compressed symbol. When this function is called, the best table
221 * might still be compressed itself, so the function needs to be recursive */
222 static int expand_symbol(unsigned char *data, int len, char *result)
223 {
224 int c, rlen, total=0;
226 while (len) {
227 c = *data;
228 /* if the table holds a single char that is the same as the one
229 * we are looking for, then end the search */
230 if (best_table[c][0]==c && best_table_len[c]==1) {
231 *result++ = c;
232 total++;
233 } else {
234 /* if not, recurse and expand */
235 rlen = expand_symbol(best_table[c], best_table_len[c], result);
236 total += rlen;
237 result += rlen;
238 }
239 data++;
240 len--;
241 }
242 *result=0;
244 return total;
245 }
247 static void write_src(void)
248 {
249 unsigned int i, k, off;
250 unsigned int best_idx[256];
251 unsigned int *markers;
252 char buf[KSYM_NAME_LEN+1];
254 printf("#include <asm/types.h>\n");
255 printf("#if BITS_PER_LONG == 64\n");
256 printf("#define PTR .quad\n");
257 printf("#define ALGN .align 8\n");
258 printf("#else\n");
259 printf("#define PTR .long\n");
260 printf("#define ALGN .align 4\n");
261 printf("#endif\n");
263 printf(".data\n");
265 output_label("symbols_addresses");
266 for (i = 0; i < table_cnt; i++) {
267 printf("\tPTR\t%#llx\n", table[i].addr);
268 }
269 printf("\n");
271 output_label("symbols_num_syms");
272 printf("\tPTR\t%d\n", table_cnt);
273 printf("\n");
275 /* table of offset markers, that give the offset in the compressed stream
276 * every 256 symbols */
277 markers = (unsigned int *) malloc(sizeof(unsigned int) * ((table_cnt + 255) / 256));
279 output_label("symbols_names");
280 off = 0;
281 for (i = 0; i < table_cnt; i++) {
282 if ((i & 0xFF) == 0)
283 markers[i >> 8] = off;
285 printf("\t.byte 0x%02x", table[i].len);
286 for (k = 0; k < table[i].len; k++)
287 printf(", 0x%02x", table[i].sym[k]);
288 printf("\n");
290 off += table[i].len + 1;
291 }
292 printf("\n");
294 output_label("symbols_markers");
295 for (i = 0; i < ((table_cnt + 255) >> 8); i++)
296 printf("\tPTR\t%d\n", markers[i]);
297 printf("\n");
299 free(markers);
301 output_label("symbols_token_table");
302 off = 0;
303 for (i = 0; i < 256; i++) {
304 best_idx[i] = off;
305 expand_symbol(best_table[i], best_table_len[i], buf);
306 printf("\t.asciz\t\"%s\"\n", buf);
307 off += strlen(buf) + 1;
308 }
309 printf("\n");
311 output_label("symbols_token_index");
312 for (i = 0; i < 256; i++)
313 printf("\t.short\t%d\n", best_idx[i]);
314 printf("\n");
315 }
318 /* table lookup compression functions */
320 /* count all the possible tokens in a symbol */
321 static void learn_symbol(unsigned char *symbol, int len)
322 {
323 int i;
325 for (i = 0; i < len - 1; i++)
326 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]++;
327 }
329 /* decrease the count for all the possible tokens in a symbol */
330 static void forget_symbol(unsigned char *symbol, int len)
331 {
332 int i;
334 for (i = 0; i < len - 1; i++)
335 token_profit[ symbol[i] + (symbol[i + 1] << 8) ]--;
336 }
338 /* remove all the invalid symbols from the table and do the initial token count */
339 static void build_initial_tok_table(void)
340 {
341 unsigned int i, pos;
343 pos = 0;
344 for (i = 0; i < table_cnt; i++) {
345 if ( symbol_valid(&table[i]) ) {
346 if (pos != i)
347 table[pos] = table[i];
348 learn_symbol(table[pos].sym, table[pos].len);
349 pos++;
350 }
351 }
352 table_cnt = pos;
353 }
355 static void *memmem_pvt(void *h, size_t hlen, void *n, size_t nlen)
356 {
357 char *p;
358 for (p = h; (p - (char *)h) <= (long)(hlen - nlen); p++)
359 if (!memcmp(p, n, nlen)) return p;
360 return NULL;
361 }
363 /* replace a given token in all the valid symbols. Use the sampled symbols
364 * to update the counts */
365 static void compress_symbols(unsigned char *str, int idx)
366 {
367 unsigned int i, len, size;
368 unsigned char *p1, *p2;
370 for (i = 0; i < table_cnt; i++) {
372 len = table[i].len;
373 p1 = table[i].sym;
375 /* find the token on the symbol */
376 p2 = memmem_pvt(p1, len, str, 2);
377 if (!p2) continue;
379 /* decrease the counts for this symbol's tokens */
380 forget_symbol(table[i].sym, len);
382 size = len;
384 do {
385 *p2 = idx;
386 p2++;
387 size -= (p2 - p1);
388 memmove(p2, p2 + 1, size);
389 p1 = p2;
390 len--;
392 if (size < 2) break;
394 /* find the token on the symbol */
395 p2 = memmem_pvt(p1, size, str, 2);
397 } while (p2);
399 table[i].len = len;
401 /* increase the counts for this symbol's new tokens */
402 learn_symbol(table[i].sym, len);
403 }
404 }
406 /* search the token with the maximum profit */
407 static int find_best_token(void)
408 {
409 int i, best, bestprofit;
411 bestprofit=-10000;
412 best = 0;
414 for (i = 0; i < 0x10000; i++) {
415 if (token_profit[i] > bestprofit) {
416 best = i;
417 bestprofit = token_profit[i];
418 }
419 }
420 return best;
421 }
423 /* this is the core of the algorithm: calculate the "best" table */
424 static void optimize_result(void)
425 {
426 int i, best;
428 /* using the '\0' symbol last allows compress_symbols to use standard
429 * fast string functions */
430 for (i = 255; i >= 0; i--) {
432 /* if this table slot is empty (it is not used by an actual
433 * original char code */
434 if (!best_table_len[i]) {
436 /* find the token with the breates profit value */
437 best = find_best_token();
439 /* place it in the "best" table */
440 best_table_len[i] = 2;
441 best_table[i][0] = best & 0xFF;
442 best_table[i][1] = (best >> 8) & 0xFF;
444 /* replace this token in all the valid symbols */
445 compress_symbols(best_table[i], i);
446 }
447 }
448 }
450 /* start by placing the symbols that are actually used on the table */
451 static void insert_real_symbols_in_table(void)
452 {
453 unsigned int i, j, c;
455 memset(best_table, 0, sizeof(best_table));
456 memset(best_table_len, 0, sizeof(best_table_len));
458 for (i = 0; i < table_cnt; i++) {
459 for (j = 0; j < table[i].len; j++) {
460 c = table[i].sym[j];
461 best_table[c][0]=c;
462 best_table_len[c]=1;
463 }
464 }
465 }
467 static void optimize_token_table(void)
468 {
469 build_initial_tok_table();
471 insert_real_symbols_in_table();
473 /* When valid symbol is not registered, exit to error */
474 if (!table_cnt) {
475 fprintf(stderr, "No valid symbol.\n");
476 exit(1);
477 }
479 optimize_result();
480 }
483 int main(int argc, char **argv)
484 {
485 if (argc >= 2) {
486 int i;
487 for (i = 1; i < argc; i++) {
488 if(strcmp(argv[i], "--all-symbols") == 0)
489 all_symbols = 1;
490 else if (strncmp(argv[i], "--symbol-prefix=", 16) == 0) {
491 char *p = &argv[i][16];
492 /* skip quote */
493 if ((*p == '"' && *(p+2) == '"') || (*p == '\'' && *(p+2) == '\''))
494 p++;
495 symbol_prefix_char = *p;
496 } else
497 usage();
498 }
499 } else if (argc != 1)
500 usage();
502 read_map(stdin);
503 optimize_token_table();
504 write_src();
506 return 0;
507 }