xref: /netbsd-src/external/gpl3/binutils.old/dist/gprof/alpha.c (revision 75fd0b742a7e4a64301bc6c44e9bc5240c58bb92)
1*75fd0b74Schristos /*
2*75fd0b74Schristos  * Copyright (c) 1983, 1993, 1998
3*75fd0b74Schristos  *      The Regents of the University of California.  All rights reserved.
4*75fd0b74Schristos  *
5*75fd0b74Schristos  * Redistribution and use in source and binary forms, with or without
6*75fd0b74Schristos  * modification, are permitted provided that the following conditions
7*75fd0b74Schristos  * are met:
8*75fd0b74Schristos  * 1. Redistributions of source code must retain the above copyright
9*75fd0b74Schristos  *    notice, this list of conditions and the following disclaimer.
10*75fd0b74Schristos  * 2. Redistributions in binary form must reproduce the above copyright
11*75fd0b74Schristos  *    notice, this list of conditions and the following disclaimer in the
12*75fd0b74Schristos  *    documentation and/or other materials provided with the distribution.
13*75fd0b74Schristos  * 3. Neither the name of the University nor the names of its contributors
14*75fd0b74Schristos  *    may be used to endorse or promote products derived from this software
15*75fd0b74Schristos  *    without specific prior written permission.
16*75fd0b74Schristos  *
17*75fd0b74Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*75fd0b74Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*75fd0b74Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*75fd0b74Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*75fd0b74Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*75fd0b74Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*75fd0b74Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*75fd0b74Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*75fd0b74Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*75fd0b74Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*75fd0b74Schristos  * SUCH DAMAGE.
28*75fd0b74Schristos  */
29*75fd0b74Schristos #include "gprof.h"
30*75fd0b74Schristos #include "search_list.h"
31*75fd0b74Schristos #include "source.h"
32*75fd0b74Schristos #include "symtab.h"
33*75fd0b74Schristos #include "cg_arcs.h"
34*75fd0b74Schristos #include "corefile.h"
35*75fd0b74Schristos #include "hist.h"
36*75fd0b74Schristos 
37*75fd0b74Schristos /*
38*75fd0b74Schristos  * Opcodes of the call instructions:
39*75fd0b74Schristos  */
40*75fd0b74Schristos #define OP_Jxx	0x1aU
41*75fd0b74Schristos #define	OP_BSR	0x34U
42*75fd0b74Schristos 
43*75fd0b74Schristos #define Jxx_FUNC_JMP		0U
44*75fd0b74Schristos #define Jxx_FUNC_JSR		1U
45*75fd0b74Schristos #define Jxx_FUNC_RET		2U
46*75fd0b74Schristos #define Jxx_FUNC_JSR_COROUTINE	3U
47*75fd0b74Schristos 
48*75fd0b74Schristos /* *INDENT-OFF* */
49*75fd0b74Schristos /* Here to document only.  We can't use this when cross compiling as
50*75fd0b74Schristos    the bitfield layout might not be the same as native.
51*75fd0b74Schristos 
52*75fd0b74Schristos    typedef union
53*75fd0b74Schristos      {
54*75fd0b74Schristos        struct
55*75fd0b74Schristos          {
56*75fd0b74Schristos 	   unsigned other:26;
57*75fd0b74Schristos 	   unsigned op_code:6;
58*75fd0b74Schristos          }
59*75fd0b74Schristos        a;				-- any format
60*75fd0b74Schristos        struct
61*75fd0b74Schristos          {
62*75fd0b74Schristos 	   int disp:21;
63*75fd0b74Schristos 	   unsigned ra:5;
64*75fd0b74Schristos 	   unsigned op_code:6;
65*75fd0b74Schristos          }
66*75fd0b74Schristos        b;				-- branch format
67*75fd0b74Schristos        struct
68*75fd0b74Schristos          {
69*75fd0b74Schristos 	   int hint:14;
70*75fd0b74Schristos 	   unsigned func:2;
71*75fd0b74Schristos 	   unsigned rb:5;
72*75fd0b74Schristos 	   unsigned ra:5;
73*75fd0b74Schristos 	   unsigned op_code:6;
74*75fd0b74Schristos          }
75*75fd0b74Schristos        j;				-- jump format
76*75fd0b74Schristos      }
77*75fd0b74Schristos     alpha_Instruction;
78*75fd0b74Schristos */
79*75fd0b74Schristos /* *INDENT-ON* */
80*75fd0b74Schristos 
81*75fd0b74Schristos static Sym indirect_child;
82*75fd0b74Schristos 
83*75fd0b74Schristos void alpha_find_call (Sym *, bfd_vma, bfd_vma);
84*75fd0b74Schristos 
85*75fd0b74Schristos /*
86*75fd0b74Schristos  * On the Alpha we can only detect PC relative calls, which are
87*75fd0b74Schristos  * usually generated for calls to functions within the same
88*75fd0b74Schristos  * object file only.  This is still better than nothing, however.
89*75fd0b74Schristos  * (In particular it should be possible to find functions that
90*75fd0b74Schristos  *  potentially call integer division routines, for example.)
91*75fd0b74Schristos  */
92*75fd0b74Schristos void
alpha_find_call(Sym * parent,bfd_vma p_lowpc,bfd_vma p_highpc)93*75fd0b74Schristos alpha_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
94*75fd0b74Schristos {
95*75fd0b74Schristos   bfd_vma pc, dest_pc;
96*75fd0b74Schristos   unsigned int insn;
97*75fd0b74Schristos   Sym *child;
98*75fd0b74Schristos 
99*75fd0b74Schristos   if (indirect_child.name == NULL)
100*75fd0b74Schristos     {
101*75fd0b74Schristos       sym_init (&indirect_child);
102*75fd0b74Schristos       indirect_child.name = _("<indirect child>");
103*75fd0b74Schristos       indirect_child.cg.prop.fract = 1.0;
104*75fd0b74Schristos       indirect_child.cg.cyc.head = &indirect_child;
105*75fd0b74Schristos     }
106*75fd0b74Schristos 
107*75fd0b74Schristos   DBG (CALLDEBUG, printf (_("[find_call] %s: 0x%lx to 0x%lx\n"),
108*75fd0b74Schristos 			  parent->name, (unsigned long) p_lowpc,
109*75fd0b74Schristos 			  (unsigned long) p_highpc));
110*75fd0b74Schristos   for (pc = (p_lowpc + 3) & ~(bfd_vma) 3; pc < p_highpc; pc += 4)
111*75fd0b74Schristos     {
112*75fd0b74Schristos       insn = bfd_get_32 (core_bfd, ((unsigned char *) core_text_space
113*75fd0b74Schristos 				    + pc - core_text_sect->vma));
114*75fd0b74Schristos       switch (insn & (0x3fU << 26))
115*75fd0b74Schristos 	{
116*75fd0b74Schristos 	case OP_Jxx << 26:
117*75fd0b74Schristos 	  /*
118*75fd0b74Schristos 	   * There is no simple and reliable way to determine the
119*75fd0b74Schristos 	   * target of a jsr (the hint bits help, but there aren't
120*75fd0b74Schristos 	   * enough bits to get a satisfactory hit rate).  Instead,
121*75fd0b74Schristos 	   * for any indirect jump we simply add an arc from PARENT
122*75fd0b74Schristos 	   * to INDIRECT_CHILD---that way the user it at least able
123*75fd0b74Schristos 	   * to see that there are other calls as well.
124*75fd0b74Schristos 	   */
125*75fd0b74Schristos 	  if ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14
126*75fd0b74Schristos 	      || (insn & (3 << 14)) == Jxx_FUNC_JSR_COROUTINE << 14)
127*75fd0b74Schristos 	    {
128*75fd0b74Schristos 	      DBG (CALLDEBUG,
129*75fd0b74Schristos 		   printf (_("[find_call] 0x%lx: jsr%s <indirect_child>\n"),
130*75fd0b74Schristos 			   (unsigned long) pc,
131*75fd0b74Schristos 			   ((insn & (3 << 14)) == Jxx_FUNC_JSR << 14
132*75fd0b74Schristos 			    ? "" : "_coroutine")));
133*75fd0b74Schristos 	      arc_add (parent, &indirect_child, (unsigned long) 0);
134*75fd0b74Schristos 	    }
135*75fd0b74Schristos 	  break;
136*75fd0b74Schristos 
137*75fd0b74Schristos 	case OP_BSR << 26:
138*75fd0b74Schristos 	  DBG (CALLDEBUG,
139*75fd0b74Schristos 	       printf (_("[find_call] 0x%lx: bsr"), (unsigned long) pc));
140*75fd0b74Schristos 	  /*
141*75fd0b74Schristos 	   * Regular PC relative addressing.  Check that this is the
142*75fd0b74Schristos 	   * address of a function.  The linker sometimes redirects
143*75fd0b74Schristos 	   * the entry point by 8 bytes to skip loading the global
144*75fd0b74Schristos 	   * pointer, so we allow for either address:
145*75fd0b74Schristos 	   */
146*75fd0b74Schristos 	  dest_pc = pc + 4 + (((bfd_signed_vma) (insn & 0x1fffff)
147*75fd0b74Schristos 			       ^ 0x100000) - 0x100000);
148*75fd0b74Schristos 	  if (hist_check_address (dest_pc))
149*75fd0b74Schristos 	    {
150*75fd0b74Schristos 	      child = sym_lookup (&symtab, dest_pc);
151*75fd0b74Schristos               if (child)
152*75fd0b74Schristos                 {
153*75fd0b74Schristos 	          DBG (CALLDEBUG,
154*75fd0b74Schristos 		       printf (" 0x%lx\t; name=%s, addr=0x%lx",
155*75fd0b74Schristos 			       (unsigned long) dest_pc, child->name,
156*75fd0b74Schristos 			       (unsigned long) child->addr));
157*75fd0b74Schristos 	          if (child->addr == dest_pc || child->addr == dest_pc - 8)
158*75fd0b74Schristos 		    {
159*75fd0b74Schristos 		      DBG (CALLDEBUG, printf ("\n"));
160*75fd0b74Schristos 		      /* a hit:  */
161*75fd0b74Schristos 		      arc_add (parent, child, (unsigned long) 0);
162*75fd0b74Schristos 		      continue;
163*75fd0b74Schristos 		    }
164*75fd0b74Schristos                 }
165*75fd0b74Schristos 	    }
166*75fd0b74Schristos 	  /*
167*75fd0b74Schristos 	   * Something funny going on.
168*75fd0b74Schristos 	   */
169*75fd0b74Schristos 	  DBG (CALLDEBUG, printf ("\tbut it's a botch\n"));
170*75fd0b74Schristos 	  break;
171*75fd0b74Schristos 
172*75fd0b74Schristos 	default:
173*75fd0b74Schristos 	  break;
174*75fd0b74Schristos 	}
175*75fd0b74Schristos     }
176*75fd0b74Schristos }
177