1*a9fa9459Szrj /*
2*a9fa9459Szrj * Copyright (c) 1983, 1993, 2001
3*a9fa9459Szrj * The Regents of the University of California. All rights reserved.
4*a9fa9459Szrj *
5*a9fa9459Szrj * Redistribution and use in source and binary forms, with or without
6*a9fa9459Szrj * modification, are permitted provided that the following conditions
7*a9fa9459Szrj * are met:
8*a9fa9459Szrj * 1. Redistributions of source code must retain the above copyright
9*a9fa9459Szrj * notice, this list of conditions and the following disclaimer.
10*a9fa9459Szrj * 2. Redistributions in binary form must reproduce the above copyright
11*a9fa9459Szrj * notice, this list of conditions and the following disclaimer in the
12*a9fa9459Szrj * documentation and/or other materials provided with the distribution.
13*a9fa9459Szrj * 3. Neither the name of the University nor the names of its contributors
14*a9fa9459Szrj * may be used to endorse or promote products derived from this software
15*a9fa9459Szrj * without specific prior written permission.
16*a9fa9459Szrj *
17*a9fa9459Szrj * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*a9fa9459Szrj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*a9fa9459Szrj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*a9fa9459Szrj * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*a9fa9459Szrj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*a9fa9459Szrj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*a9fa9459Szrj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*a9fa9459Szrj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*a9fa9459Szrj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*a9fa9459Szrj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*a9fa9459Szrj * SUCH DAMAGE.
28*a9fa9459Szrj */
29*a9fa9459Szrj #include "gprof.h"
30*a9fa9459Szrj #include "search_list.h"
31*a9fa9459Szrj #include "source.h"
32*a9fa9459Szrj #include "symtab.h"
33*a9fa9459Szrj #include "cg_arcs.h"
34*a9fa9459Szrj #include "corefile.h"
35*a9fa9459Szrj #include "hist.h"
36*a9fa9459Szrj
37*a9fa9459Szrj /*
38*a9fa9459Szrj * opcode of the `callf' instruction
39*a9fa9459Szrj */
40*a9fa9459Szrj #define CALLF 0xfe
41*a9fa9459Szrj
42*a9fa9459Szrj /*
43*a9fa9459Szrj * register for pc relative addressing
44*a9fa9459Szrj */
45*a9fa9459Szrj #define PC 0xf
46*a9fa9459Szrj
47*a9fa9459Szrj enum tahoe_opermodes
48*a9fa9459Szrj {
49*a9fa9459Szrj literal, indexed, reg, regdef, autodec, autoinc, autoincdef,
50*a9fa9459Szrj bytedisp, bytedispdef, worddisp, worddispdef, longdisp, longdispdef,
51*a9fa9459Szrj immediate, absolute, byterel, bytereldef, wordrel, wordreldef,
52*a9fa9459Szrj longrel, longreldef
53*a9fa9459Szrj };
54*a9fa9459Szrj typedef enum tahoe_opermodes tahoe_operandenum;
55*a9fa9459Szrj
56*a9fa9459Szrj /*
57*a9fa9459Szrj * A symbol to be the child of indirect callf:
58*a9fa9459Szrj */
59*a9fa9459Szrj static Sym indirectchild;
60*a9fa9459Szrj
61*a9fa9459Szrj static tahoe_operandenum tahoe_operandmode (unsigned char *);
62*a9fa9459Szrj static char *tahoe_operandname (tahoe_operandenum);
63*a9fa9459Szrj static long tahoe_operandlength (unsigned char *);
64*a9fa9459Szrj static bfd_signed_vma tahoe_offset (unsigned char *);
65*a9fa9459Szrj void tahoe_find_call (Sym *, bfd_vma, bfd_vma);
66*a9fa9459Szrj
67*a9fa9459Szrj static tahoe_operandenum
tahoe_operandmode(unsigned char * modep)68*a9fa9459Szrj tahoe_operandmode (unsigned char *modep)
69*a9fa9459Szrj {
70*a9fa9459Szrj long usesreg = *modep & 0xf;
71*a9fa9459Szrj
72*a9fa9459Szrj switch ((*modep >> 4) & 0xf)
73*a9fa9459Szrj {
74*a9fa9459Szrj case 0:
75*a9fa9459Szrj case 1:
76*a9fa9459Szrj case 2:
77*a9fa9459Szrj case 3:
78*a9fa9459Szrj return literal;
79*a9fa9459Szrj case 4:
80*a9fa9459Szrj return indexed;
81*a9fa9459Szrj case 5:
82*a9fa9459Szrj return reg;
83*a9fa9459Szrj case 6:
84*a9fa9459Szrj return regdef;
85*a9fa9459Szrj case 7:
86*a9fa9459Szrj return autodec;
87*a9fa9459Szrj case 8:
88*a9fa9459Szrj return usesreg != 0xe ? autoinc : immediate;
89*a9fa9459Szrj case 9:
90*a9fa9459Szrj return usesreg != PC ? autoincdef : absolute;
91*a9fa9459Szrj case 10:
92*a9fa9459Szrj return usesreg != PC ? bytedisp : byterel;
93*a9fa9459Szrj case 11:
94*a9fa9459Szrj return usesreg != PC ? bytedispdef : bytereldef;
95*a9fa9459Szrj case 12:
96*a9fa9459Szrj return usesreg != PC ? worddisp : wordrel;
97*a9fa9459Szrj case 13:
98*a9fa9459Szrj return usesreg != PC ? worddispdef : wordreldef;
99*a9fa9459Szrj case 14:
100*a9fa9459Szrj return usesreg != PC ? longdisp : longrel;
101*a9fa9459Szrj case 15:
102*a9fa9459Szrj return usesreg != PC ? longdispdef : longreldef;
103*a9fa9459Szrj }
104*a9fa9459Szrj /* NOTREACHED */
105*a9fa9459Szrj abort ();
106*a9fa9459Szrj }
107*a9fa9459Szrj
108*a9fa9459Szrj static char *
tahoe_operandname(tahoe_operandenum mode)109*a9fa9459Szrj tahoe_operandname (tahoe_operandenum mode)
110*a9fa9459Szrj {
111*a9fa9459Szrj
112*a9fa9459Szrj switch (mode)
113*a9fa9459Szrj {
114*a9fa9459Szrj case literal:
115*a9fa9459Szrj return "literal";
116*a9fa9459Szrj case indexed:
117*a9fa9459Szrj return "indexed";
118*a9fa9459Szrj case reg:
119*a9fa9459Szrj return "register";
120*a9fa9459Szrj case regdef:
121*a9fa9459Szrj return "register deferred";
122*a9fa9459Szrj case autodec:
123*a9fa9459Szrj return "autodecrement";
124*a9fa9459Szrj case autoinc:
125*a9fa9459Szrj return "autoincrement";
126*a9fa9459Szrj case autoincdef:
127*a9fa9459Szrj return "autoincrement deferred";
128*a9fa9459Szrj case bytedisp:
129*a9fa9459Szrj return "byte displacement";
130*a9fa9459Szrj case bytedispdef:
131*a9fa9459Szrj return "byte displacement deferred";
132*a9fa9459Szrj case byterel:
133*a9fa9459Szrj return "byte relative";
134*a9fa9459Szrj case bytereldef:
135*a9fa9459Szrj return "byte relative deferred";
136*a9fa9459Szrj case worddisp:
137*a9fa9459Szrj return "word displacement";
138*a9fa9459Szrj case worddispdef:
139*a9fa9459Szrj return "word displacement deferred";
140*a9fa9459Szrj case wordrel:
141*a9fa9459Szrj return "word relative";
142*a9fa9459Szrj case wordreldef:
143*a9fa9459Szrj return "word relative deferred";
144*a9fa9459Szrj case immediate:
145*a9fa9459Szrj return "immediate";
146*a9fa9459Szrj case absolute:
147*a9fa9459Szrj return "absolute";
148*a9fa9459Szrj case longdisp:
149*a9fa9459Szrj return "long displacement";
150*a9fa9459Szrj case longdispdef:
151*a9fa9459Szrj return "long displacement deferred";
152*a9fa9459Szrj case longrel:
153*a9fa9459Szrj return "long relative";
154*a9fa9459Szrj case longreldef:
155*a9fa9459Szrj return "long relative deferred";
156*a9fa9459Szrj }
157*a9fa9459Szrj /* NOTREACHED */
158*a9fa9459Szrj abort ();
159*a9fa9459Szrj }
160*a9fa9459Szrj
161*a9fa9459Szrj static long
tahoe_operandlength(unsigned char * modep)162*a9fa9459Szrj tahoe_operandlength (unsigned char *modep
163*a9fa9459Szrj )
164*a9fa9459Szrj {
165*a9fa9459Szrj
166*a9fa9459Szrj switch (tahoe_operandmode (modep))
167*a9fa9459Szrj {
168*a9fa9459Szrj case literal:
169*a9fa9459Szrj case reg:
170*a9fa9459Szrj case regdef:
171*a9fa9459Szrj case autodec:
172*a9fa9459Szrj case autoinc:
173*a9fa9459Szrj case autoincdef:
174*a9fa9459Szrj return 1;
175*a9fa9459Szrj case bytedisp:
176*a9fa9459Szrj case bytedispdef:
177*a9fa9459Szrj case byterel:
178*a9fa9459Szrj case bytereldef:
179*a9fa9459Szrj return 2;
180*a9fa9459Szrj case worddisp:
181*a9fa9459Szrj case worddispdef:
182*a9fa9459Szrj case wordrel:
183*a9fa9459Szrj case wordreldef:
184*a9fa9459Szrj return 3;
185*a9fa9459Szrj case immediate:
186*a9fa9459Szrj case absolute:
187*a9fa9459Szrj case longdisp:
188*a9fa9459Szrj case longdispdef:
189*a9fa9459Szrj case longrel:
190*a9fa9459Szrj case longreldef:
191*a9fa9459Szrj return 5;
192*a9fa9459Szrj case indexed:
193*a9fa9459Szrj return 1 + tahoe_operandlength (modep + 1);
194*a9fa9459Szrj }
195*a9fa9459Szrj /* NOTREACHED */
196*a9fa9459Szrj abort ();
197*a9fa9459Szrj }
198*a9fa9459Szrj
199*a9fa9459Szrj static bfd_signed_vma
tahoe_offset(unsigned char * modep)200*a9fa9459Szrj tahoe_offset (unsigned char *modep)
201*a9fa9459Szrj {
202*a9fa9459Szrj tahoe_operandenum mode = tahoe_operandmode (modep);
203*a9fa9459Szrj
204*a9fa9459Szrj ++modep; /* skip over the mode */
205*a9fa9459Szrj switch (mode)
206*a9fa9459Szrj {
207*a9fa9459Szrj default:
208*a9fa9459Szrj fprintf (stderr, "[reladdr] not relative address\n");
209*a9fa9459Szrj return 0;
210*a9fa9459Szrj case byterel:
211*a9fa9459Szrj return 1 + bfd_get_signed_8 (core_bfd, modep);
212*a9fa9459Szrj case wordrel:
213*a9fa9459Szrj return 2 + bfd_get_signed_16 (core_bfd, modep);
214*a9fa9459Szrj case longrel:
215*a9fa9459Szrj return 4 + bfd_get_signed_32 (core_bfd, modep);
216*a9fa9459Szrj }
217*a9fa9459Szrj }
218*a9fa9459Szrj
219*a9fa9459Szrj void
tahoe_find_call(Sym * parent,bfd_vma p_lowpc,bfd_vma p_highpc)220*a9fa9459Szrj tahoe_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
221*a9fa9459Szrj {
222*a9fa9459Szrj unsigned char *instructp;
223*a9fa9459Szrj long length;
224*a9fa9459Szrj Sym *child;
225*a9fa9459Szrj tahoe_operandenum mode;
226*a9fa9459Szrj tahoe_operandenum firstmode;
227*a9fa9459Szrj bfd_vma pc, destpc;
228*a9fa9459Szrj static bfd_boolean inited = FALSE;
229*a9fa9459Szrj
230*a9fa9459Szrj if (!inited)
231*a9fa9459Szrj {
232*a9fa9459Szrj inited = TRUE;
233*a9fa9459Szrj sym_init (&indirectchild);
234*a9fa9459Szrj indirectchild.cg.prop.fract = 1.0;
235*a9fa9459Szrj indirectchild.cg.cyc.head = &indirectchild;
236*a9fa9459Szrj }
237*a9fa9459Szrj
238*a9fa9459Szrj DBG (CALLDEBUG, printf ("[findcall] %s: 0x%lx to 0x%lx\n",
239*a9fa9459Szrj parent->name, (unsigned long) p_lowpc,
240*a9fa9459Szrj (unsigned long) p_highpc));
241*a9fa9459Szrj for (pc = p_lowpc; pc < p_highpc; pc += length)
242*a9fa9459Szrj {
243*a9fa9459Szrj length = 1;
244*a9fa9459Szrj instructp = ((unsigned char *) core_text_space
245*a9fa9459Szrj + pc - core_text_sect->vma);
246*a9fa9459Szrj if ((*instructp & 0xff) == CALLF)
247*a9fa9459Szrj {
248*a9fa9459Szrj /*
249*a9fa9459Szrj * maybe a callf, better check it out.
250*a9fa9459Szrj * skip the count of the number of arguments.
251*a9fa9459Szrj */
252*a9fa9459Szrj DBG (CALLDEBUG, printf ("[findcall]\t0x%lx:callf",
253*a9fa9459Szrj (unsigned long) pc));
254*a9fa9459Szrj firstmode = tahoe_operandmode (instructp + length);
255*a9fa9459Szrj switch (firstmode)
256*a9fa9459Szrj {
257*a9fa9459Szrj case literal:
258*a9fa9459Szrj case immediate:
259*a9fa9459Szrj break;
260*a9fa9459Szrj default:
261*a9fa9459Szrj goto botched;
262*a9fa9459Szrj }
263*a9fa9459Szrj length += tahoe_operandlength (instructp + length);
264*a9fa9459Szrj mode = tahoe_operandmode (instructp + length);
265*a9fa9459Szrj DBG (CALLDEBUG,
266*a9fa9459Szrj printf ("\tfirst operand is %s", tahoe_operandname (firstmode));
267*a9fa9459Szrj printf ("\tsecond operand is %s\n", tahoe_operandname (mode));
268*a9fa9459Szrj );
269*a9fa9459Szrj switch (mode)
270*a9fa9459Szrj {
271*a9fa9459Szrj case regdef:
272*a9fa9459Szrj case bytedispdef:
273*a9fa9459Szrj case worddispdef:
274*a9fa9459Szrj case longdispdef:
275*a9fa9459Szrj case bytereldef:
276*a9fa9459Szrj case wordreldef:
277*a9fa9459Szrj case longreldef:
278*a9fa9459Szrj /*
279*a9fa9459Szrj * indirect call: call through pointer
280*a9fa9459Szrj * either *d(r) as a parameter or local
281*a9fa9459Szrj * (r) as a return value
282*a9fa9459Szrj * *f as a global pointer
283*a9fa9459Szrj * [are there others that we miss?,
284*a9fa9459Szrj * e.g. arrays of pointers to functions???]
285*a9fa9459Szrj */
286*a9fa9459Szrj arc_add (parent, &indirectchild, (unsigned long) 0);
287*a9fa9459Szrj length += tahoe_operandlength (instructp + length);
288*a9fa9459Szrj continue;
289*a9fa9459Szrj case byterel:
290*a9fa9459Szrj case wordrel:
291*a9fa9459Szrj case longrel:
292*a9fa9459Szrj /*
293*a9fa9459Szrj * regular pc relative addressing
294*a9fa9459Szrj * check that this is the address of
295*a9fa9459Szrj * a function.
296*a9fa9459Szrj */
297*a9fa9459Szrj destpc = pc + tahoe_offset (instructp + length);
298*a9fa9459Szrj if (hist_check_address (destpc))
299*a9fa9459Szrj {
300*a9fa9459Szrj child = sym_lookup (&symtab, destpc);
301*a9fa9459Szrj if (child)
302*a9fa9459Szrj {
303*a9fa9459Szrj DBG (CALLDEBUG,
304*a9fa9459Szrj printf ("[findcall]\tdestpc 0x%lx",
305*a9fa9459Szrj (unsigned long) destpc);
306*a9fa9459Szrj printf (" child->name %s", child->name);
307*a9fa9459Szrj printf (" child->addr 0x%lx\n",
308*a9fa9459Szrj (unsigned long) child->addr);
309*a9fa9459Szrj );
310*a9fa9459Szrj if (child->addr == destpc)
311*a9fa9459Szrj {
312*a9fa9459Szrj /*
313*a9fa9459Szrj * a hit
314*a9fa9459Szrj */
315*a9fa9459Szrj arc_add (parent, child, (unsigned long) 0);
316*a9fa9459Szrj length += tahoe_operandlength (instructp + length);
317*a9fa9459Szrj continue;
318*a9fa9459Szrj }
319*a9fa9459Szrj }
320*a9fa9459Szrj goto botched;
321*a9fa9459Szrj }
322*a9fa9459Szrj /*
323*a9fa9459Szrj * else:
324*a9fa9459Szrj * it looked like a callf,
325*a9fa9459Szrj * but it wasn't to anywhere.
326*a9fa9459Szrj */
327*a9fa9459Szrj goto botched;
328*a9fa9459Szrj default:
329*a9fa9459Szrj botched:
330*a9fa9459Szrj /*
331*a9fa9459Szrj * something funny going on.
332*a9fa9459Szrj */
333*a9fa9459Szrj DBG (CALLDEBUG, printf ("[findcall]\tbut it's a botch\n"));
334*a9fa9459Szrj length = 1;
335*a9fa9459Szrj continue;
336*a9fa9459Szrj }
337*a9fa9459Szrj }
338*a9fa9459Szrj }
339*a9fa9459Szrj }
340