12a6b7db3Sskrll /*
22a6b7db3Sskrll * Copyright (c) 1983, 1993, 2001
32a6b7db3Sskrll * The Regents of the University of California. All rights reserved.
42a6b7db3Sskrll *
52a6b7db3Sskrll * Redistribution and use in source and binary forms, with or without
62a6b7db3Sskrll * modification, are permitted provided that the following conditions
72a6b7db3Sskrll * are met:
82a6b7db3Sskrll * 1. Redistributions of source code must retain the above copyright
92a6b7db3Sskrll * notice, this list of conditions and the following disclaimer.
102a6b7db3Sskrll * 2. Redistributions in binary form must reproduce the above copyright
112a6b7db3Sskrll * notice, this list of conditions and the following disclaimer in the
122a6b7db3Sskrll * documentation and/or other materials provided with the distribution.
132a6b7db3Sskrll * 3. Neither the name of the University nor the names of its contributors
142a6b7db3Sskrll * may be used to endorse or promote products derived from this software
152a6b7db3Sskrll * without specific prior written permission.
162a6b7db3Sskrll *
172a6b7db3Sskrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
182a6b7db3Sskrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192a6b7db3Sskrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202a6b7db3Sskrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
212a6b7db3Sskrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222a6b7db3Sskrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
232a6b7db3Sskrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
242a6b7db3Sskrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
252a6b7db3Sskrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
262a6b7db3Sskrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
272a6b7db3Sskrll * SUCH DAMAGE.
282a6b7db3Sskrll */
292a6b7db3Sskrll #include "gprof.h"
302a6b7db3Sskrll #include "search_list.h"
312a6b7db3Sskrll #include "source.h"
322a6b7db3Sskrll #include "symtab.h"
332a6b7db3Sskrll #include "cg_arcs.h"
342a6b7db3Sskrll #include "corefile.h"
352a6b7db3Sskrll #include "hist.h"
362a6b7db3Sskrll
372a6b7db3Sskrll static int i386_iscall (unsigned char *);
382a6b7db3Sskrll void i386_find_call (Sym *, bfd_vma, bfd_vma);
392a6b7db3Sskrll
402a6b7db3Sskrll static int
i386_iscall(unsigned char * ip)412a6b7db3Sskrll i386_iscall (unsigned char *ip)
422a6b7db3Sskrll {
432a6b7db3Sskrll if (*ip == 0xe8)
442a6b7db3Sskrll return 1;
452a6b7db3Sskrll return 0;
462a6b7db3Sskrll }
472a6b7db3Sskrll
482a6b7db3Sskrll
492a6b7db3Sskrll void
i386_find_call(Sym * parent,bfd_vma p_lowpc,bfd_vma p_highpc)502a6b7db3Sskrll i386_find_call (Sym *parent, bfd_vma p_lowpc, bfd_vma p_highpc)
512a6b7db3Sskrll {
522a6b7db3Sskrll unsigned char *instructp;
532a6b7db3Sskrll Sym *child;
542a6b7db3Sskrll bfd_vma pc, destpc;
552a6b7db3Sskrll
562a6b7db3Sskrll DBG (CALLDEBUG, printf ("[findcall] %s: 0x%lx to 0x%lx\n",
572a6b7db3Sskrll parent->name, (unsigned long) p_lowpc,
582a6b7db3Sskrll (unsigned long) p_highpc));
592a6b7db3Sskrll
60*cb63e24eSchristos if (p_highpc < 5)
61*cb63e24eSchristos return;
62*cb63e24eSchristos p_highpc -= 5;
632a6b7db3Sskrll for (pc = p_lowpc; pc < p_highpc; ++pc)
642a6b7db3Sskrll {
652a6b7db3Sskrll instructp = (unsigned char *) core_text_space + pc - core_text_sect->vma;
662a6b7db3Sskrll if (i386_iscall (instructp))
672a6b7db3Sskrll {
682a6b7db3Sskrll DBG (CALLDEBUG,
692a6b7db3Sskrll printf ("[findcall]\t0x%lx:call", (unsigned long) pc));
702a6b7db3Sskrll /*
712a6b7db3Sskrll * regular pc relative addressing
722a6b7db3Sskrll * check that this is the address of
732a6b7db3Sskrll * a function.
742a6b7db3Sskrll */
752a6b7db3Sskrll
762a6b7db3Sskrll destpc = bfd_get_32 (core_bfd, instructp + 1) + pc + 5;
772a6b7db3Sskrll if (hist_check_address (destpc))
782a6b7db3Sskrll {
792a6b7db3Sskrll child = sym_lookup (&symtab, destpc);
802a6b7db3Sskrll if (child && child->addr == destpc)
812a6b7db3Sskrll {
822a6b7db3Sskrll /*
832a6b7db3Sskrll * a hit
842a6b7db3Sskrll */
852a6b7db3Sskrll DBG (CALLDEBUG,
862a6b7db3Sskrll printf ("\tdestpc 0x%lx (%s)\n",
872a6b7db3Sskrll (unsigned long) destpc, child->name));
882a6b7db3Sskrll arc_add (parent, child, (unsigned long) 0);
892a6b7db3Sskrll instructp += 4; /* call is a 5 byte instruction */
902a6b7db3Sskrll continue;
912a6b7db3Sskrll }
922a6b7db3Sskrll }
932a6b7db3Sskrll /*
942a6b7db3Sskrll * else:
952a6b7db3Sskrll * it looked like a callf, but it:
962a6b7db3Sskrll * a) wasn't actually a callf, or
972a6b7db3Sskrll * b) didn't point to a known function in the symtab, or
982a6b7db3Sskrll * c) something funny is going on.
992a6b7db3Sskrll */
1002a6b7db3Sskrll DBG (CALLDEBUG, printf ("\tbut it's a botch\n"));
1012a6b7db3Sskrll }
1022a6b7db3Sskrll }
1032a6b7db3Sskrll }
104