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