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