Lots of changes from David Mosberger-Tang; see ChangeLog and NOTES for details:
[deliverable/binutils-gdb.git] / gprof / symtab.c
1 #include "gprof.h"
2 #include "cg_arcs.h"
3 #include "core.h"
4 #include "symtab.h"
5
6 Sym_Table symtab;
7
8
9 /*
10 * Initialize a symbol (so it's empty).
11 */
12 void
13 DEFUN(sym_init, (sym), Sym *sym)
14 {
15 memset(sym, 0, sizeof(*sym));
16 /*
17 * It is not safe to assume that a binary zero corresponds to
18 * a floating-point 0.0, so initialize floats explicitly:
19 */
20 sym->hist.time = 0.0;
21 sym->cg.child_time = 0.0;
22 sym->cg.prop.fract = 0.0;
23 sym->cg.prop.self = 0.0;
24 sym->cg.prop.child = 0.0;
25 } /* sym_init */
26
27
28 /*
29 * Compare the function entry-point of two symbols and return <0, =0,
30 * or >0 depending on whether the left value is smaller than, equal
31 * to, or greater than the right value. If two symbols are equal
32 * but one has is_func set and the other doesn't, we make the
33 * non-function symbol one "bigger" so that the function symbol will
34 * survive duplicate removal. Finally, if both symbols have the
35 * same is_func value, we discriminate against is_static such that
36 * the global symbol survives.
37 */
38 static int
39 DEFUN(cmp_addr, (lp, rp), const PTR lp AND const PTR rp)
40 {
41 Sym *left = (Sym*) lp;
42 Sym *right = (Sym*) rp;
43
44 if (left->addr > right->addr) {
45 return 1;
46 } else if (left->addr < right->addr) {
47 return -1;
48 } /* if */
49
50 if (left->is_func != right->is_func) {
51 return right->is_func - left->is_func;
52 } /* if */
53
54 return left->is_static - right->is_static;
55 } /* cmp_addr */
56
57
58 void
59 DEFUN(symtab_finalize, (tab), Sym_Table *tab)
60 {
61 Sym *src, *dst;
62 bfd_vma prev_addr;
63
64 if (!tab->len) {
65 return;
66 } /* if */
67
68 /*
69 * Sort symbol table in order of increasing function addresses:
70 */
71 qsort(tab->base, tab->len, sizeof(Sym), cmp_addr);
72
73 /*
74 * Remove duplicate entries to speed-up later processing and
75 * set end_addr if its not set yet:
76 */
77 prev_addr = tab->base[0].addr + 1;
78 for (src = dst = tab->base; src < tab->limit; ++src) {
79 if (src->addr == prev_addr) {
80 /*
81 * If same address, favor global symbol over static one.
82 * If both symbols are either static or global, check
83 * whether one has name beginning with underscore while
84 * the other doesn't. In such cases, keep sym without
85 * underscore. This takes cares of compiler generated
86 * symbols (such as __gnu_compiled, __c89_used, etc.).
87 */
88 if ((!src->is_static && dst[-1].is_static)
89 || ((src->is_static == dst[-1].is_static) &&
90 (src->name[0] != '_' && dst[-1].name[0] == '_')
91 || (src->name[0]
92 && src->name[1] != '_' && dst[-1].name[1] == '_')))
93 {
94 DBG(AOUTDEBUG|IDDEBUG,
95 printf("[symtab_finalize] favor %s@%c%c over %s@%c%c",
96 src->name, src->is_static ? 't' : 'T',
97 src->is_func ? 'F' : 'f',
98 dst[-1].name, dst[-1].is_static ? 't' : 'T',
99 dst[-1].is_func ? 'F' : 'f');
100 printf(" (addr=%lx)\n", src->addr));
101 dst[-1] = *src;
102 } else {
103 DBG(AOUTDEBUG|IDDEBUG,
104 printf("[symtab_finalize] favor %s@%c%c over %s@%c%c",
105 dst[-1].name, dst[-1].is_static ? 't' : 'T',
106 dst[-1].is_func ? 'F' : 'f',
107 src->name, src->is_static ? 't' : 'T',
108 src->is_func ? 'F' : 'f');
109 printf(" (addr=%lx)\n", src->addr));
110 } /* if */
111 } else {
112 if (dst > tab->base && dst[-1].end_addr == 0) {
113 dst[-1].end_addr = src->addr - 1;
114 } /* if */
115
116 /* retain sym only if it has a non-empty address range: */
117 if (!src->end_addr || src->addr <= src->end_addr) {
118 *dst++ = *src;
119 prev_addr = src->addr;
120 } /* if */
121 } /* if */
122 } /* if */
123 if (tab->len > 0 && dst[-1].end_addr == 0) {
124 dst[-1].end_addr = core_text_sect->vma + core_text_sect->_raw_size - 1;
125 } /* if */
126
127 DBG(AOUTDEBUG|IDDEBUG,
128 printf("[symtab_finalize]: removed %d duplicate entries\n",
129 tab->len - (int) (dst - tab->base)));
130
131 tab->limit = dst;
132 tab->len = tab->limit - tab->base;
133
134 DBG(AOUTDEBUG|IDDEBUG,
135 int j;
136
137 for (j = 0; j < tab->len; ++j){
138 printf("[symtab_finalize] 0x%lx-0x%lx\t%s\n",
139 (long) tab->base[j].addr, (long) tab->base[j].end_addr,
140 tab->base[j].name);
141 } /* for */);
142 } /* symtab_finalize */
143
144
145 #ifdef DEBUG
146
147 Sym*
148 DEFUN(dbg_sym_lookup, (symtab, address), Sym_Table *symtab AND bfd_vma address)
149 {
150 long low, mid, high;
151 Sym *sym;
152
153 fprintf(stderr,"[sym_lookup] address 0x%lx\n", address);
154
155 sym = symtab->base;
156 for (low = 0, high = symtab->len - 1 ; low != high ;) {
157 mid = (high + low) >> 1;
158 fprintf(stderr, "[dbg_sym_lookup] low=0x%lx, mid=0x%lx, high=0x%lx\n",
159 low, mid, high);
160 fprintf(stderr, "[dbg_sym_lookup] sym[m]=0x%lx sym[m + 1]=0x%lx\n",
161 sym[mid].addr, sym[mid + 1].addr);
162 if (sym[mid].addr <= address && sym[mid + 1].addr > address) {
163 return &sym[mid];
164 } /* if */
165 if (sym[mid].addr > address) {
166 high = mid;
167 } else {
168 low = mid + 1;
169 } /* if */
170 } /* for */
171 fprintf(stderr, "[sym_lookup] binary search fails???\n");
172 return 0;
173 } /* dbg_sym_lookup */
174
175 #endif DEBUG
176
177
178 /*
179 * Look up an address in the symbol-table that is sorted by address.
180 * If address does not hit any symbol, 0 is returned.
181 */
182 Sym*
183 DEFUN(sym_lookup, (symtab, address), Sym_Table *symtab AND bfd_vma address)
184 {
185 long low, high;
186 long mid = -1;
187 Sym *sym;
188 #ifdef DEBUG
189 int probes = 0;
190 #endif /* DEBUG */
191
192 if (!symtab->len) {
193 return 0;
194 } /* if */
195
196 sym = symtab->base;
197 for (low = 0, high = symtab->len - 1 ; low != high ;) {
198 DBG(LOOKUPDEBUG, ++probes);
199 mid = (high + low) / 2;
200 if (sym[mid].addr <= address && sym[mid + 1].addr > address) {
201 if (address > sym[mid].end_addr) {
202 /*
203 * Address falls into gap between sym[mid] and
204 * sym[mid + 1]:
205 */
206 return 0;
207 } else {
208 DBG(LOOKUPDEBUG,
209 printf("[sym_lookup] %d probes (symtab->len=%d)\n",
210 probes, symtab->len - 1));
211 return &sym[mid];
212 } /* if */
213 } /* if */
214 if (sym[mid].addr > address) {
215 high = mid;
216 } else {
217 low = mid + 1;
218 } /* if */
219 } /* for */
220 if (sym[mid + 1].addr <= address) {
221 if (address > sym[mid + 1].end_addr) {
222 /* address is beyond end of sym[mid + 1]: */
223 return 0;
224 } else {
225 DBG(LOOKUPDEBUG, printf("[sym_lookup] %d (%d) probes, fall off\n",
226 probes, symtab->len - 1));
227 return &sym[mid + 1];
228 } /* if */
229 } /* if */
230 return 0;
231 } /* sym_lookup */
232
233 /*** end of symtab.c ***/
This page took 0.050622 seconds and 5 git commands to generate.