1130803Smarcel/* Target-dependent code for the GNU C Library (glibc).
2130803Smarcel
3130803Smarcel   Copyright 2002, 2003 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel#include "frame.h"
24130803Smarcel#include "symtab.h"
25130803Smarcel#include "symfile.h"
26130803Smarcel#include "objfiles.h"
27130803Smarcel
28130803Smarcel#include "glibc-tdep.h"
29130803Smarcel
30130803Smarcel/* Calling functions in shared libraries.  */
31130803Smarcel
32130803Smarcel/* Find the minimal symbol named NAME, and return both the minsym
33130803Smarcel   struct and its objfile.  This probably ought to be in minsym.c, but
34130803Smarcel   everything there is trying to deal with things like C++ and
35130803Smarcel   SOFUN_ADDRESS_MAYBE_TURQUOISE, ...  Since this is so simple, it may
36130803Smarcel   be considered too special-purpose for general consumption.  */
37130803Smarcel
38130803Smarcelstatic struct minimal_symbol *
39130803Smarcelfind_minsym_and_objfile (char *name, struct objfile **objfile_p)
40130803Smarcel{
41130803Smarcel  struct objfile *objfile;
42130803Smarcel
43130803Smarcel  ALL_OBJFILES (objfile)
44130803Smarcel    {
45130803Smarcel      struct minimal_symbol *msym;
46130803Smarcel
47130803Smarcel      ALL_OBJFILE_MSYMBOLS (objfile, msym)
48130803Smarcel	{
49130803Smarcel	  if (SYMBOL_LINKAGE_NAME (msym)
50130803Smarcel	      && strcmp (SYMBOL_LINKAGE_NAME (msym), name) == 0)
51130803Smarcel	    {
52130803Smarcel	      *objfile_p = objfile;
53130803Smarcel	      return msym;
54130803Smarcel	    }
55130803Smarcel	}
56130803Smarcel    }
57130803Smarcel
58130803Smarcel  return 0;
59130803Smarcel}
60130803Smarcel
61130803Smarcel/* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
62130803Smarcel   This function:
63130803Smarcel   1) decides whether a PLT has sent us into the linker to resolve
64130803Smarcel      a function reference, and
65130803Smarcel   2) if so, tells us where to set a temporary breakpoint that will
66130803Smarcel      trigger when the dynamic linker is done.  */
67130803Smarcel
68130803SmarcelCORE_ADDR
69130803Smarcelglibc_skip_solib_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
70130803Smarcel{
71130803Smarcel  /* The GNU dynamic linker is part of the GNU C library, and is used
72130803Smarcel     by all GNU systems (GNU/Hurd, GNU/Linux).  An unresolved PLT
73130803Smarcel     entry points to "_dl_runtime_resolve", which calls "fixup" to
74130803Smarcel     patch the PLT, and then passes control to the function.
75130803Smarcel
76130803Smarcel     We look for the symbol `_dl_runtime_resolve', and find `fixup' in
77130803Smarcel     the same objfile.  If we are at the entry point of `fixup', then
78130803Smarcel     we set a breakpoint at the return address (at the top of the
79130803Smarcel     stack), and continue.
80130803Smarcel
81130803Smarcel     It's kind of gross to do all these checks every time we're
82130803Smarcel     called, since they don't change once the executable has gotten
83130803Smarcel     started.  But this is only a temporary hack --- upcoming versions
84130803Smarcel     of GNU/Linux will provide a portable, efficient interface for
85130803Smarcel     debugging programs that use shared libraries.  */
86130803Smarcel
87130803Smarcel  struct objfile *objfile;
88130803Smarcel  struct minimal_symbol *resolver
89130803Smarcel    = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
90130803Smarcel
91130803Smarcel  if (resolver)
92130803Smarcel    {
93130803Smarcel      struct minimal_symbol *fixup
94130803Smarcel	= lookup_minimal_symbol ("fixup", NULL, objfile);
95130803Smarcel
96130803Smarcel      if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
97130803Smarcel	return frame_pc_unwind (get_current_frame ());
98130803Smarcel    }
99130803Smarcel
100130803Smarcel  return 0;
101130803Smarcel}
102