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