1*5ba6b03cSchristos /* Gprof -c option support for AArch64.
2*5ba6b03cSchristos    Copyright 2013 Linaro Ltd.
3*5ba6b03cSchristos 
4*5ba6b03cSchristos    Based upon gprof/i386.c.
5*5ba6b03cSchristos 
6*5ba6b03cSchristos    Copyright (c) 1983, 1993, 2001
7*5ba6b03cSchristos    The Regents of the University of California.  All rights reserved.
8*5ba6b03cSchristos 
9*5ba6b03cSchristos    Redistribution and use in source and binary forms, with or without
10*5ba6b03cSchristos    modification, are permitted provided that the following conditions
11*5ba6b03cSchristos    are met:
12*5ba6b03cSchristos 
13*5ba6b03cSchristos    1.  Redistributions of source code must retain the above copyright
14*5ba6b03cSchristos        notice, this list of conditions and the following disclaimer.
15*5ba6b03cSchristos    2.  Redistributions in binary form must reproduce the above copyright
16*5ba6b03cSchristos        notice, this list of conditions and the following disclaimer in the
17*5ba6b03cSchristos        documentation and/or other materials provided with the distribution.
18*5ba6b03cSchristos    3.  Neither the name of the University nor the names of its contributors
19*5ba6b03cSchristos        may be used to endorse or promote products derived from this software
20*5ba6b03cSchristos        without specific prior written permission.
21*5ba6b03cSchristos 
22*5ba6b03cSchristos    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*5ba6b03cSchristos    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*5ba6b03cSchristos    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*5ba6b03cSchristos    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*5ba6b03cSchristos    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*5ba6b03cSchristos    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*5ba6b03cSchristos    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*5ba6b03cSchristos    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*5ba6b03cSchristos    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*5ba6b03cSchristos    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*5ba6b03cSchristos    SUCH DAMAGE.  */
33*5ba6b03cSchristos 
34*5ba6b03cSchristos #include "gprof.h"
35*5ba6b03cSchristos #include "search_list.h"
36*5ba6b03cSchristos #include "source.h"
37*5ba6b03cSchristos #include "symtab.h"
38*5ba6b03cSchristos #include "cg_arcs.h"
39*5ba6b03cSchristos #include "corefile.h"
40*5ba6b03cSchristos #include "hist.h"
41*5ba6b03cSchristos 
42*5ba6b03cSchristos #define BRANCH_MASK    0x7c000000
43*5ba6b03cSchristos #define BRANCH_PATTERN 0x14000000
44*5ba6b03cSchristos 
45*5ba6b03cSchristos void aarch64_find_call (Sym *, bfd_vma, bfd_vma);
46*5ba6b03cSchristos 
47*5ba6b03cSchristos void
aarch64_find_call(Sym * parent,bfd_vma p_lowpc,bfd_vma p_highpc)48*5ba6b03cSchristos aarch64_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
49*5ba6b03cSchristos {
50*5ba6b03cSchristos   bfd_vma pc, dest_pc, offset;
51*5ba6b03cSchristos   unsigned int insn;
52*5ba6b03cSchristos   Sym *child;
53*5ba6b03cSchristos 
54*5ba6b03cSchristos   DBG (CALLDEBUG, printf ("[find_call] %s: 0x%lx to 0x%lx\n",
55*5ba6b03cSchristos 			  parent->name, (unsigned long) p_lowpc,
56*5ba6b03cSchristos 			  (unsigned long) p_highpc));
57*5ba6b03cSchristos 
58*5ba6b03cSchristos   for (pc = p_lowpc; pc < p_highpc; pc += 4)
59*5ba6b03cSchristos     {
60*5ba6b03cSchristos 
61*5ba6b03cSchristos       insn = bfd_get_32 (core_bfd, ((unsigned char *) core_text_space
62*5ba6b03cSchristos 				    + pc - core_text_sect->vma));
63*5ba6b03cSchristos 
64*5ba6b03cSchristos       if ((insn & BRANCH_MASK) == BRANCH_PATTERN)
65*5ba6b03cSchristos 	{
66*5ba6b03cSchristos 	  DBG (CALLDEBUG,
67*5ba6b03cSchristos 	       printf ("[find_call] 0x%lx: bl", (unsigned long) pc));
68*5ba6b03cSchristos 
69*5ba6b03cSchristos 	  /* Regular pc relative addressing check that this is the
70*5ba6b03cSchristos 	     address of a function.  */
71*5ba6b03cSchristos 	  offset = ((((bfd_vma) insn & 0x3ffffff) ^ 0x2000000) - 0x2000000) << 2;
72*5ba6b03cSchristos 
73*5ba6b03cSchristos 	  dest_pc = pc + offset;
74*5ba6b03cSchristos 
75*5ba6b03cSchristos 	  if (hist_check_address (dest_pc))
76*5ba6b03cSchristos 	    {
77*5ba6b03cSchristos 	      child = sym_lookup (&symtab, dest_pc);
78*5ba6b03cSchristos 
79*5ba6b03cSchristos 	      if (child)
80*5ba6b03cSchristos 		{
81*5ba6b03cSchristos 		  DBG (CALLDEBUG,
82*5ba6b03cSchristos 		       printf ("\tdest_pc=0x%lx, (name=%s, addr=0x%lx)\n",
83*5ba6b03cSchristos 			       (unsigned long) dest_pc, child->name,
84*5ba6b03cSchristos 			       (unsigned long) child->addr));
85*5ba6b03cSchristos 
86*5ba6b03cSchristos 		  if (child->addr == dest_pc)
87*5ba6b03cSchristos 		    {
88*5ba6b03cSchristos 		      /* a hit.  */
89*5ba6b03cSchristos 		      arc_add (parent, child, (unsigned long) 0);
90*5ba6b03cSchristos 		      continue;
91*5ba6b03cSchristos 		    }
92*5ba6b03cSchristos 		}
93*5ba6b03cSchristos 	    }
94*5ba6b03cSchristos 
95*5ba6b03cSchristos 	  /* Something funny going on.  */
96*5ba6b03cSchristos 	  DBG (CALLDEBUG, printf ("\tbut it's a botch\n"));
97*5ba6b03cSchristos 	}
98*5ba6b03cSchristos     }
99*5ba6b03cSchristos }
100