xref: /dflybsd-src/contrib/binutils-2.27/gprof/i386.c (revision e656dc90e3d65d744d534af2f5ea88cf8101ebcf)
1*a9fa9459Szrj /*
2*a9fa9459Szrj  * Copyright (c) 1983, 1993, 2001
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 static int i386_iscall (unsigned char *);
38*a9fa9459Szrj void i386_find_call (Sym *, bfd_vma, bfd_vma);
39*a9fa9459Szrj 
40*a9fa9459Szrj static int
i386_iscall(unsigned char * ip)41*a9fa9459Szrj i386_iscall (unsigned char *ip)
42*a9fa9459Szrj {
43*a9fa9459Szrj   if (*ip == 0xe8)
44*a9fa9459Szrj     return 1;
45*a9fa9459Szrj   return 0;
46*a9fa9459Szrj }
47*a9fa9459Szrj 
48*a9fa9459Szrj 
49*a9fa9459Szrj void
i386_find_call(Sym * parent,bfd_vma p_lowpc,bfd_vma p_highpc)50*a9fa9459Szrj i386_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
51*a9fa9459Szrj {
52*a9fa9459Szrj   unsigned char *instructp;
53*a9fa9459Szrj   Sym *child;
54*a9fa9459Szrj   bfd_vma pc, destpc;
55*a9fa9459Szrj 
56*a9fa9459Szrj   DBG (CALLDEBUG, printf ("[findcall] %s: 0x%lx to 0x%lx\n",
57*a9fa9459Szrj 			  parent->name, (unsigned long) p_lowpc,
58*a9fa9459Szrj 			  (unsigned long) p_highpc));
59*a9fa9459Szrj 
60*a9fa9459Szrj   for (pc = p_lowpc; pc < p_highpc; ++pc)
61*a9fa9459Szrj     {
62*a9fa9459Szrj       instructp = (unsigned char *) core_text_space + pc - core_text_sect->vma;
63*a9fa9459Szrj       if (i386_iscall (instructp))
64*a9fa9459Szrj 	{
65*a9fa9459Szrj 	  DBG (CALLDEBUG,
66*a9fa9459Szrj 	       printf ("[findcall]\t0x%lx:call", (unsigned long) pc));
67*a9fa9459Szrj 	  /*
68*a9fa9459Szrj 	   *  regular pc relative addressing
69*a9fa9459Szrj 	   *    check that this is the address of
70*a9fa9459Szrj 	   *    a function.
71*a9fa9459Szrj 	   */
72*a9fa9459Szrj 
73*a9fa9459Szrj 	  destpc = bfd_get_32 (core_bfd, instructp + 1) + pc + 5;
74*a9fa9459Szrj 	  if (hist_check_address (destpc))
75*a9fa9459Szrj 	    {
76*a9fa9459Szrj 	      child = sym_lookup (&symtab, destpc);
77*a9fa9459Szrj 	      if (child && child->addr == destpc)
78*a9fa9459Szrj 		{
79*a9fa9459Szrj 		  /*
80*a9fa9459Szrj 		   *      a hit
81*a9fa9459Szrj 		   */
82*a9fa9459Szrj 		  DBG (CALLDEBUG,
83*a9fa9459Szrj 		       printf ("\tdestpc 0x%lx (%s)\n",
84*a9fa9459Szrj 			       (unsigned long) destpc, child->name));
85*a9fa9459Szrj 		  arc_add (parent, child, (unsigned long) 0);
86*a9fa9459Szrj 		  instructp += 4;	/* call is a 5 byte instruction */
87*a9fa9459Szrj 		  continue;
88*a9fa9459Szrj 		}
89*a9fa9459Szrj 	    }
90*a9fa9459Szrj 	  /*
91*a9fa9459Szrj 	   *  else:
92*a9fa9459Szrj 	   *    it looked like a callf, but it:
93*a9fa9459Szrj 	   *      a) wasn't actually a callf, or
94*a9fa9459Szrj 	   *      b) didn't point to a known function in the symtab, or
95*a9fa9459Szrj 	   *      c) something funny is going on.
96*a9fa9459Szrj 	   */
97*a9fa9459Szrj 	  DBG (CALLDEBUG, printf ("\tbut it's a botch\n"));
98*a9fa9459Szrj 	}
99*a9fa9459Szrj     }
100*a9fa9459Szrj }
101