FreeCalypso > hg > freecalypso-reveng
comparison leo-obj/tool/symtab.c @ 130:87b82398a08b
leo-obj project subtree started, tiobjd tool moved into it
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Sun, 06 Apr 2014 22:14:39 +0000 |
parents | ticoff/symtab.c@fb1e47bebe00 |
children | ed533d469838 |
comparison
equal
deleted
inserted
replaced
129:597143ba1c37 | 130:87b82398a08b |
---|---|
1 /* | |
2 * Code for working with the symbol table | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include "filestruct.h" | |
9 #include "intstruct.h" | |
10 #include "coffconst.h" | |
11 #include "globals.h" | |
12 | |
13 static struct classmap { | |
14 int code; | |
15 char *str; | |
16 } classtab[] = { | |
17 {C_NULL, "NULL"}, | |
18 {C_AUTO, "AUTO"}, | |
19 {C_EXT, "EXT"}, | |
20 {C_STAT, "STAT"}, | |
21 {C_REG, "REG"}, | |
22 {C_EXTREF, "EXTREF"}, | |
23 {C_LABEL, "LABEL"}, | |
24 {C_ULABEL, "ULABEL"}, | |
25 {C_MOS, "MOS"}, | |
26 {C_ARG, "ARG"}, | |
27 {C_STRTAG, "STRTAG"}, | |
28 {C_MOU, "MOU"}, | |
29 {C_UNTAG, "UNTAG"}, | |
30 {C_TPDEF, "TPDEF"}, | |
31 {C_USTATIC, "USTATIC"}, | |
32 {C_ENTAG, "ENTAG"}, | |
33 {C_MOE, "MOE"}, | |
34 {C_REGPARM, "REGPARM"}, | |
35 {C_FIELD, "FIELD"}, | |
36 {C_UEXT, "UEXT"}, | |
37 {C_STATLAB, "STATLAB"}, | |
38 {C_EXTLAB, "EXTLAB"}, | |
39 {C_SYSTEM, "SYSTEM"}, | |
40 {C_VARARG, "VARARG"}, | |
41 {C_BLOCK, "BLOCK"}, | |
42 {C_FCN, "FCN"}, | |
43 {C_EOS, "EOS"}, | |
44 {C_FILE, "FILE"}, | |
45 {C_LINE, "LINE"}, | |
46 {0, 0} | |
47 }; | |
48 | |
49 char * | |
50 storage_class_to_string(code, numbuf) | |
51 char *numbuf; | |
52 { | |
53 struct classmap *tp; | |
54 | |
55 for (tp = classtab; tp->str; tp++) | |
56 if (tp->code == code) | |
57 return(tp->str); | |
58 sprintf(numbuf, "%d", code); | |
59 return(numbuf); | |
60 } | |
61 | |
62 dump_symtab() | |
63 { | |
64 unsigned n; | |
65 struct internal_syment *sym; | |
66 char *sec, secstr[8]; | |
67 char *class, classbuf[8]; | |
68 | |
69 printf("%-5s %-30s %-4s %-7s %-12s %-8s\n", | |
70 "Num", "Name", "Type", "Class", "Section", "Value"); | |
71 for (n = 0; n < nsymtab; n++) { | |
72 sym = symtab[n]; | |
73 if (!sym) | |
74 continue; | |
75 if (sym->section) | |
76 sec = sym->section->name; | |
77 else { | |
78 sprintf(secstr, "%d", sym->scnum); | |
79 sec = secstr; | |
80 } | |
81 class = storage_class_to_string(sym->class, classbuf); | |
82 printf("%-5u %-30s %04X %-7s %-12s %08X%s\n", | |
83 n, sym->name, sym->type, class, | |
84 sec, sym->value, sym->aux ? " Aux" : ""); | |
85 } | |
86 return(0); | |
87 } | |
88 | |
89 static void | |
90 initial_fill_for_sort(sec) | |
91 struct internal_scnhdr *sec; | |
92 { | |
93 unsigned n, m; | |
94 struct internal_syment *sym; | |
95 | |
96 m = 0; | |
97 for (n = 0; n < nsymtab; n++) { | |
98 sym = symtab[n]; | |
99 if (!sym) | |
100 continue; | |
101 if (sym->section != sec) | |
102 continue; | |
103 sec->sorted_symbols[m++] = sym; | |
104 } | |
105 } | |
106 | |
107 static int | |
108 compare_for_sort(p1, p2) | |
109 struct internal_syment **p1, **p2; | |
110 { | |
111 /* sort by value first */ | |
112 if ((*p1)->value < (*p2)->value) | |
113 return(-1); | |
114 if ((*p1)->value > (*p2)->value) | |
115 return(1); | |
116 /* if same value, retain the original symtab order */ | |
117 if ((*p1)->number < (*p2)->number) | |
118 return(-1); | |
119 if ((*p1)->number > (*p2)->number) | |
120 return(1); | |
121 else | |
122 return(0); | |
123 } | |
124 | |
125 void | |
126 sort_symbols_of_sec(sec) | |
127 struct internal_scnhdr *sec; | |
128 { | |
129 if (sec->sorted_symbols) | |
130 return; | |
131 if (!sec->nsymbols) { | |
132 fprintf(stderr, | |
133 "BUG: sort_symbols_of_sec() called for section \"%s\" w/o symbols\n", | |
134 sec->name); | |
135 exit(1); | |
136 } | |
137 sec->sorted_symbols = malloc(sizeof(void *) * sec->nsymbols); | |
138 if (!sec->sorted_symbols) { | |
139 perror("malloc"); | |
140 exit(1); | |
141 } | |
142 initial_fill_for_sort(sec); | |
143 qsort(sec->sorted_symbols, sec->nsymbols, sizeof(void *), | |
144 compare_for_sort); | |
145 } | |
146 | |
147 void | |
148 sort_symbols_of_all_sec() | |
149 { | |
150 unsigned n; | |
151 struct internal_scnhdr *sec; | |
152 | |
153 for (n = 0; n < nsections; n++) { | |
154 sec = sections + n; | |
155 if (!sec->nsymbols) | |
156 continue; | |
157 sort_symbols_of_sec(sec); | |
158 } | |
159 } | |
160 | |
161 cmd_nm() | |
162 { | |
163 unsigned n, m; | |
164 struct internal_scnhdr *sec; | |
165 struct internal_syment *sym; | |
166 char classbuf[8]; | |
167 | |
168 get_int_section_table(); | |
169 get_int_symbol_table(); | |
170 for (n = 0; n < nsections; n++) { | |
171 sec = sections + n; | |
172 if (!sec->nsymbols) | |
173 continue; | |
174 printf("%s:\n\n", sec->name); | |
175 sort_symbols_of_sec(sec); | |
176 for (m = 0; m < sec->nsymbols; m++) { | |
177 sym = sec->sorted_symbols[m]; | |
178 printf("%08X %-7s %s\n", sym->value, | |
179 storage_class_to_string(sym->class, classbuf), | |
180 sym->name); | |
181 } | |
182 putchar('\n'); | |
183 } | |
184 exit(0); | |
185 } |