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