1*a9fa9459Szrj /*
2*a9fa9459Szrj * Copyright (c) 1983, 1993, 1998
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 * Opcodes of the call instructions:
39*a9fa9459Szrj */
40*a9fa9459Szrj #define OP_Jxx 0x1aU
41*a9fa9459Szrj #define OP_BSR 0x34U
42*a9fa9459Szrj
43*a9fa9459Szrj #define Jxx_FUNC_JMP 0U
44*a9fa9459Szrj #define Jxx_FUNC_JSR 1U
45*a9fa9459Szrj #define Jxx_FUNC_RET 2U
46*a9fa9459Szrj #define Jxx_FUNC_JSR_COROUTINE 3U
47*a9fa9459Szrj
48*a9fa9459Szrj /* *INDENT-OFF* */
49*a9fa9459Szrj /* Here to document only. We can't use this when cross compiling as
50*a9fa9459Szrj the bitfield layout might not be the same as native.
51*a9fa9459Szrj
52*a9fa9459Szrj typedef union
53*a9fa9459Szrj {
54*a9fa9459Szrj struct
55*a9fa9459Szrj {
56*a9fa9459Szrj unsigned other:26;
57*a9fa9459Szrj unsigned op_code:6;
58*a9fa9459Szrj }
59*a9fa9459Szrj a; -- any format
60*a9fa9459Szrj struct
61*a9fa9459Szrj {
62*a9fa9459Szrj int disp:21;
63*a9fa9459Szrj unsigned ra:5;
64*a9fa9459Szrj unsigned op_code:6;
65*a9fa9459Szrj }
66*a9fa9459Szrj b; -- branch format
67*a9fa9459Szrj struct
68*a9fa9459Szrj {
69*a9fa9459Szrj int hint:14;
70*a9fa9459Szrj unsigned func:2;
71*a9fa9459Szrj unsigned rb:5;
72*a9fa9459Szrj unsigned ra:5;
73*a9fa9459Szrj unsigned op_code:6;
74*a9fa9459Szrj }
75*a9fa9459Szrj j; -- jump format
76*a9fa9459Szrj }
77*a9fa9459Szrj alpha_Instruction;
78*a9fa9459Szrj */
79*a9fa9459Szrj /* *INDENT-ON* */
80*a9fa9459Szrj
81*a9fa9459Szrj static Sym indirect_child;
82*a9fa9459Szrj
83*a9fa9459Szrj void alpha_find_call (Sym *, bfd_vma, bfd_vma);
84*a9fa9459Szrj
85*a9fa9459Szrj /*
86*a9fa9459Szrj * On the Alpha we can only detect PC relative calls, which are
87*a9fa9459Szrj * usually generated for calls to functions within the same
88*a9fa9459Szrj * object file only. This is still better than nothing, however.
89*a9fa9459Szrj * (In particular it should be possible to find functions that
90*a9fa9459Szrj * potentially call integer division routines, for example.)
91*a9fa9459Szrj */
92*a9fa9459Szrj void
alpha_find_call(Sym * parent,bfd_vma p_lowpc,bfd_vma p_highpc)93*a9fa9459Szrj alpha_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
94*a9fa9459Szrj {
95*a9fa9459Szrj bfd_vma pc, dest_pc;
96*a9fa9459Szrj unsigned int insn;
97*a9fa9459Szrj Sym *child;
98*a9fa9459Szrj
99*a9fa9459Szrj if (indirect_child.name == NULL)
100*a9fa9459Szrj {
101*a9fa9459Szrj sym_init (&indirect_child);
102*a9fa9459Szrj indirect_child.name = _("<indirect child>");
103*a9fa9459Szrj indirect_child.cg.prop.fract = 1.0;
104*a9fa9459Szrj indirect_child.cg.cyc.head = &indirect_child;
105*a9fa9459Szrj }
106*a9fa9459Szrj
107*a9fa9459Szrj DBG (CALLDEBUG, printf (_("[find_call] %s: 0x%lx to 0x%lx\n"),
108*a9fa9459Szrj parent->name, (unsigned long) p_lowpc,
109*a9fa9459Szrj (unsigned long) p_highpc));
110*a9fa9459Szrj for (pc = (p_lowpc + 3) & ~(bfd_vma) 3; pc < p_highpc; pc += 4)
111*a9fa9459Szrj {
112*a9fa9459Szrj insn = bfd_get_32 (core_bfd, ((unsigned char *) core_text_space
113*a9fa9459Szrj + pc - core_text_sect->vma));
114*a9fa9459Szrj switch (insn & (0x3fU << 26))
115*a9fa9459Szrj {
116*a9fa9459Szrj case OP_Jxx << 26:
117*a9fa9459Szrj /*
118*a9fa9459Szrj * There is no simple and reliable way to determine the
119*a9fa9459Szrj * target of a jsr (the hint bits help, but there aren't
120*a9fa9459Szrj * enough bits to get a satisfactory hit rate). Instead,
121*a9fa9459Szrj * for any indirect jump we simply add an arc from PARENT
122*a9fa9459Szrj * to INDIRECT_CHILD---that way the user it at least able
123*a9fa9459Szrj * to see that there are other calls as well.
124*a9fa9459Szrj */
125*a9fa9459Szrj if ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14
126*a9fa9459Szrj || (insn & (3 << 14)) == Jxx_FUNC_JSR_COROUTINE << 14)
127*a9fa9459Szrj {
128*a9fa9459Szrj DBG (CALLDEBUG,
129*a9fa9459Szrj printf (_("[find_call] 0x%lx: jsr%s <indirect_child>\n"),
130*a9fa9459Szrj (unsigned long) pc,
131*a9fa9459Szrj ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14
132*a9fa9459Szrj ? "" : "_coroutine")));
133*a9fa9459Szrj arc_add (parent, &indirect_child, (unsigned long) 0);
134*a9fa9459Szrj }
135*a9fa9459Szrj break;
136*a9fa9459Szrj
137*a9fa9459Szrj case OP_BSR << 26:
138*a9fa9459Szrj DBG (CALLDEBUG,
139*a9fa9459Szrj printf (_("[find_call] 0x%lx: bsr"), (unsigned long) pc));
140*a9fa9459Szrj /*
141*a9fa9459Szrj * Regular PC relative addressing. Check that this is the
142*a9fa9459Szrj * address of a function. The linker sometimes redirects
143*a9fa9459Szrj * the entry point by 8 bytes to skip loading the global
144*a9fa9459Szrj * pointer, so we allow for either address:
145*a9fa9459Szrj */
146*a9fa9459Szrj dest_pc = pc + 4 + (((bfd_signed_vma) (insn & 0x1fffff)
147*a9fa9459Szrj ^ 0x100000) - 0x100000);
148*a9fa9459Szrj if (hist_check_address (dest_pc))
149*a9fa9459Szrj {
150*a9fa9459Szrj child = sym_lookup (&symtab, dest_pc);
151*a9fa9459Szrj if (child)
152*a9fa9459Szrj {
153*a9fa9459Szrj DBG (CALLDEBUG,
154*a9fa9459Szrj printf (" 0x%lx\t; name=%s, addr=0x%lx",
155*a9fa9459Szrj (unsigned long) dest_pc, child->name,
156*a9fa9459Szrj (unsigned long) child->addr));
157*a9fa9459Szrj if (child->addr == dest_pc || child->addr == dest_pc - 8)
158*a9fa9459Szrj {
159*a9fa9459Szrj DBG (CALLDEBUG, printf ("\n"));
160*a9fa9459Szrj /* a hit: */
161*a9fa9459Szrj arc_add (parent, child, (unsigned long) 0);
162*a9fa9459Szrj continue;
163*a9fa9459Szrj }
164*a9fa9459Szrj }
165*a9fa9459Szrj }
166*a9fa9459Szrj /*
167*a9fa9459Szrj * Something funny going on.
168*a9fa9459Szrj */
169*a9fa9459Szrj DBG (CALLDEBUG, printf ("\tbut it's a botch\n"));
170*a9fa9459Szrj break;
171*a9fa9459Szrj
172*a9fa9459Szrj default:
173*a9fa9459Szrj break;
174*a9fa9459Szrj }
175*a9fa9459Szrj }
176*a9fa9459Szrj }
177