import gdb-1999-12-21 snapshot
[external/binutils.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for Linux running on i386's, for GDB.
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 #include "defs.h"
20 #include "inferior.h"
21 #include "gdbcore.h"
22
23 /* For i386_linux_skip_solib_resolver */
24 #include "symtab.h"
25 #include "frame.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28
29 #include <sys/ptrace.h>
30 #include <sys/user.h>
31 #include <sys/procfs.h>
32
33 #ifdef HAVE_SYS_REG_H
34 #include <sys/reg.h>
35 #endif
36
37 /*
38  * Some systems (Linux) may have threads implemented as pseudo-processes, 
39  * in which case we may be tracing more than one process at a time.
40  * In that case, inferior_pid will contain the main process ID and the 
41  * individual thread (process) id mashed together.  These macros are 
42  * used to separate them out.  The definitions may be overridden in tm.h
43  */
44
45 #if !defined (PIDGET)   /* Default definition for PIDGET/TIDGET.  */
46 #define PIDGET(PID)     PID
47 #define TIDGET(PID)     0
48 #endif
49
50 /* This is a duplicate of the table in i386-xdep.c. */
51
52 static int regmap[] = 
53 {
54   EAX, ECX, EDX, EBX,
55   UESP, EBP, ESI, EDI,
56   EIP, EFL, CS, SS,
57   DS, ES, FS, GS,
58 };
59
60
61 /* Which ptrace request retrieves which registers?
62    These apply to the corresponding SET requests as well.  */
63 #define GETREGS_SUPPLIES(regno) \
64   (0 <= (regno) && (regno) <= 15)
65 #define GETFPREGS_SUPPLIES(regno) \
66   (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
67 #define GETXFPREGS_SUPPLIES(regno) \
68   (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
69
70 /* Does the current host support the GETXFPREGS request?  The header
71    file may or may not define it, and even if it is defined, the
72    kernel will return EIO if it's running on a pre-SSE processor.
73
74    PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
75    Linux kernel patch for SSE support.  That patch may or may not
76    actually make it into the official distribution.  If you find that
77    years have gone by since this stuff was added, and Linux isn't
78    using PTRACE_GETXFPREGS, that means that our patch didn't make it,
79    and you can delete this, and the related code.
80
81    My instinct is to attach this to some architecture- or
82    target-specific data structure, but really, a particular GDB
83    process can only run on top of one kernel at a time.  So it's okay
84    for this to be a simple variable.  */
85 int have_ptrace_getxfpregs =
86 #ifdef HAVE_PTRACE_GETXFPREGS
87   1
88 #else
89   0
90 #endif
91 ;
92
93
94 \f
95 /* Transfering the general registers between GDB, inferiors and core files.  */
96
97 /* Given a pointer to a general register set in struct user format
98    (gregset_t *), unpack the register contents and supply them as
99    gdb's idea of the current register values. */
100 void
101 supply_gregset (gregsetp)
102      gregset_t *gregsetp;
103 {
104   register int regi;
105   register greg_t *regp = (greg_t *) gregsetp;
106
107   for (regi = 0; regi < NUM_GREGS; regi++)
108     {
109       supply_register (regi, (char *) (regp + regmap[regi]));
110     }
111 }
112
113
114 /* Fill in a gregset_t object with selected data from a gdb-format
115    register file.
116    - GREGSETP points to the gregset_t object to be filled.
117    - GDB_REGS points to the GDB-style register file providing the data.
118    - VALID is an array indicating which registers in GDB_REGS are
119      valid; the parts of *GREGSETP that would hold registers marked
120      invalid in GDB_REGS are left unchanged.  If VALID is zero, all
121      registers are assumed to be valid.  */
122 void
123 convert_to_gregset (gregset_t *gregsetp,
124                     char *gdb_regs,
125                     signed char *valid)
126 {
127   int regi;
128   register greg_t *regp = (greg_t *) gregsetp;
129
130   for (regi = 0; regi < NUM_GREGS; regi++)
131     if (! valid || valid[regi])
132       *(regp + regmap[regi]) = * (int *) &registers[REGISTER_BYTE (regi)];
133 }
134
135
136 /* Store GDB's value for REGNO in *GREGSETP.  If REGNO is -1, do all
137    of them.  */
138 void
139 fill_gregset (gregset_t *gregsetp,
140               int regno)
141 {
142   if (regno == -1)
143     convert_to_gregset (gregsetp, registers, 0);
144   else if (regno >= 0 && regno < NUM_GREGS)
145     {
146       signed char valid[NUM_GREGS];
147       memset (valid, 0, sizeof (valid));
148       valid[regno] = 1;
149       convert_to_gregset (gregsetp, valid, valid);
150     }
151 }
152
153
154 /* Read the general registers from the process, and store them
155    in registers[].  */
156 static void
157 fetch_regs (int tid)
158 {
159   int ret, regno;
160   gregset_t buf;
161
162   ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &buf);
163   if (ret < 0)
164     {
165       warning ("Couldn't get registers");
166       return;
167     }
168
169   supply_gregset (&buf);
170 }
171
172
173 /* Set the inferior's general registers to the values in registers[]
174    --- but only those registers marked as valid.  */
175 static void
176 store_regs (int tid)
177 {
178   int ret, regno;
179   gregset_t buf;
180
181   ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &buf);
182   if (ret < 0)
183     {
184       warning ("Couldn't get registers");
185       return;
186     }
187
188   convert_to_gregset (&buf, registers, register_valid);
189
190   ret = ptrace (PTRACE_SETREGS, tid, 0, (int)buf);
191   if (ret < 0)
192     {
193       warning ("Couldn't write registers");
194       return;
195     }
196 }
197
198
199 \f
200 /* Transfering floating-point registers between GDB, inferiors and cores.  */
201
202 /* What is the address of st(N) within the fpregset_t structure F?  */
203 #define FPREGSET_T_FPREG_ADDR(f, n) \
204   ((char *) &(f)->st_space + (n) * 10)
205
206 /* Fill GDB's register file with the floating-point register values in
207    *FPREGSETP.  */
208 void 
209 supply_fpregset (fpregset_t *fpregsetp)
210 {
211   int i;
212
213   /* Supply the floating-point registers.  */
214   for (i = 0; i < 8; i++)
215     supply_register (FP0_REGNUM + i, FPREGSET_T_FPREG_ADDR (fpregsetp, i));
216
217   supply_register (FCTRL_REGNUM, (char *) &fpregsetp->cwd);
218   supply_register (FSTAT_REGNUM, (char *) &fpregsetp->swd);
219   supply_register (FTAG_REGNUM,  (char *) &fpregsetp->twd);
220   supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip);
221   supply_register (FDS_REGNUM,   (char *) &fpregsetp->fos);
222   supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo);
223   
224   /* Extract the code segment and opcode from the  "fcs" member.  */
225   {
226     long l;
227
228     l = fpregsetp->fcs & 0xffff;
229     supply_register (FCS_REGNUM, (char *) &l);
230
231     l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
232     supply_register (FOP_REGNUM, (char *) &l);
233   }
234 }
235
236
237 /* Fill in an fpregset_t structure with selected data from a
238    gdb-format register file.
239    - FPREGSETP points to the structure to be filled. 
240    - GDB_REGS points to the GDB-style register file providing the data.
241    - VALID is an array indicating which registers in GDB_REGS are
242      valid; the parts of *FPREGSETP that would hold registers marked
243      invalid in GDB_REGS are left unchanged.  If VALID is zero, all
244      registers are assumed to be valid.  */
245 void
246 convert_to_fpregset (fpregset_t *fpregsetp,
247                      char *gdb_regs,
248                      signed char *valid)
249 {
250   int i;
251
252   /* Fill in the floating-point registers.  */
253   for (i = 0; i < 8; i++)
254     if (!valid || valid[i])
255       memcpy (FPREGSET_T_FPREG_ADDR (fpregsetp, i),
256               &registers[REGISTER_BYTE (FP0_REGNUM + i)],
257               REGISTER_RAW_SIZE(FP0_REGNUM + i));
258
259 #define fill(MEMBER, REGNO)                                             \
260   if (! valid || valid[(REGNO)])                                        \
261     memcpy (&fpregsetp->MEMBER, &registers[REGISTER_BYTE (REGNO)],      \
262             sizeof (fpregsetp->MEMBER))
263
264   fill (cwd, FCTRL_REGNUM);
265   fill (swd, FSTAT_REGNUM);
266   fill (twd, FTAG_REGNUM);
267   fill (fip, FCOFF_REGNUM);
268   fill (foo, FDOFF_REGNUM);
269   fill (fos, FDS_REGNUM);
270
271 #undef fill
272
273   if (! valid || valid[FCS_REGNUM])
274     fpregsetp->fcs
275       = ((fpregsetp->fcs & ~0xffff)
276          | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
277
278   if (! valid || valid[FOP_REGNUM])
279     fpregsetp->fcs
280       = ((fpregsetp->fcs & 0xffff)
281          | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
282             << 16));
283 }
284
285
286 /* Given a pointer to a floating point register set in (fpregset_t *)
287    format, update all of the registers from gdb's idea of the current
288    floating point register set.  */
289
290 void
291 fill_fpregset (fpregset_t *fpregsetp,
292                int regno)
293 {
294   convert_to_fpregset (fpregsetp, registers, 0);
295 }
296
297
298 /* Get the whole floating point state of the process and store the
299    floating point stack into registers[].  */
300 static void
301 fetch_fpregs (int tid)
302 {
303   int ret, regno;
304   fpregset_t buf;
305
306   ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &buf);
307   if (ret < 0)
308     {
309       warning ("Couldn't get floating point status");
310       return;
311     }
312
313   /* ptrace fills an fpregset_t, so we can use the same function we do
314      for core files.  */
315   supply_fpregset (&buf);
316 }
317
318
319 /* Set the inferior's floating-point registers to the values in
320    registers[] --- but only those registers marked valid.  */
321 static void
322 store_fpregs (int tid)
323 {
324   int ret;
325   fpregset_t buf;
326
327   ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &buf);
328   if (ret < 0)
329     {
330       warning ("Couldn't get floating point status");
331       return;
332     }
333
334   convert_to_fpregset (&buf, registers, register_valid);
335
336   ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &buf);
337   if (ret < 0)
338     {
339       warning ("Couldn't write floating point status");
340       return;
341     }
342 }
343
344 \f
345 /* Transfering floating-point and SSE registers to and from GDB.  */
346
347
348 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
349    Linux kernel patch for SSE support.  That patch may or may not
350    actually make it into the official distribution.  If you find that
351    years have gone by since this code was added, and Linux isn't using
352    PTRACE_GETXFPREGS, that means that our patch didn't make it, and
353    you can delete this code.  */
354
355 #ifdef HAVE_PTRACE_GETXFPREGS
356 static void
357 supply_xfpregset (struct user_xfpregs_struct *xfpregs)
358 {
359   int reg;
360
361   /* Supply the floating-point registers.  */
362   for (reg = 0; reg < 8; reg++)
363     supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
364
365   {
366     supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd);
367     supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd);
368     supply_register (FTAG_REGNUM,  (char *) &xfpregs->twd);
369     supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip);
370     supply_register (FDS_REGNUM,   (char *) &xfpregs->fos);
371     supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo);
372   
373     /* Extract the code segment and opcode from the  "fcs" member.  */
374     {
375       long l;
376       
377       l = xfpregs->fcs & 0xffff;
378       supply_register (FCS_REGNUM, (char *) &l);
379
380       l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
381       supply_register (FOP_REGNUM, (char *) &l);
382     }
383   }
384
385   /* Supply the SSE registers.  */
386   for (reg = 0; reg < 8; reg++)
387     supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]);
388   supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr);
389 }
390
391
392 static void
393 convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
394                       char *gdb_regs,
395                       signed char *valid)
396 {
397   int reg;
398
399   /* Fill in the floating-point registers.  */
400   for (reg = 0; reg < 8; reg++)
401     if (!valid || valid[reg])
402       memcpy (&xfpregs->st_space[reg],
403               &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
404               REGISTER_RAW_SIZE(FP0_REGNUM + reg));
405
406 #define fill(MEMBER, REGNO)                                             \
407   if (! valid || valid[(REGNO)])                                        \
408     memcpy (&xfpregs->MEMBER, &registers[REGISTER_BYTE (REGNO)],        \
409             sizeof (xfpregs->MEMBER))
410
411   fill (cwd, FCTRL_REGNUM);
412   fill (swd, FSTAT_REGNUM);
413   fill (twd, FTAG_REGNUM);
414   fill (fip, FCOFF_REGNUM);
415   fill (foo, FDOFF_REGNUM);
416   fill (fos, FDS_REGNUM);
417
418 #undef fill
419
420   if (! valid || valid[FCS_REGNUM])
421     xfpregs->fcs
422       = ((xfpregs->fcs & ~0xffff)
423          | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
424
425   if (! valid || valid[FOP_REGNUM])
426     xfpregs->fcs
427       = ((xfpregs->fcs & 0xffff)
428          | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
429             << 16));
430
431   /* Fill in the XMM registers.  */
432   for (reg = 0; reg < 8; reg++)
433     if (! valid || valid[reg])
434       memcpy (&xfpregs->xmm_space[reg],
435               &registers[REGISTER_BYTE (XMM0_REGNUM + reg)],
436               REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
437 }
438
439
440 /* Make a PTRACE_GETXFPREGS request, and supply all the register
441    values that yields to GDB.  */
442 static int
443 fetch_xfpregs (int tid)
444 {
445   int ret;
446   struct user_xfpregs_struct xfpregs;
447
448   if (! have_ptrace_getxfpregs) 
449     return 0;
450
451   ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
452   if (ret == -1)
453     {
454       if (errno == EIO)
455         {
456           have_ptrace_getxfpregs = 0;
457           return 0;
458         }
459
460       warning ("couldn't read floating-point and SSE registers.");
461       return 0;
462     }
463
464   supply_xfpregset (&xfpregs);
465   return 1;
466 }
467
468
469 /* Send all the valid register values in GDB's register file covered
470    by the PTRACE_SETXFPREGS request to the inferior.  */
471 static int
472 store_xfpregs (int tid)
473 {
474   int ret;
475   struct user_xfpregs_struct xfpregs;
476
477   if (! have_ptrace_getxfpregs)
478     return 0;
479
480   ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
481   if (ret == -1)
482     {
483       if (errno == EIO)
484         {
485           have_ptrace_getxfpregs = 0;
486           return 0;
487         }
488
489       warning ("couldn't read floating-point and SSE registers.");
490       return 0;
491     }
492
493   convert_to_xfpregset (&xfpregs, registers, register_valid);
494
495   if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0)
496     {
497       warning ("Couldn't write floating-point and SSE registers.");
498       return 0;
499     }
500
501   return 1;
502 }
503
504
505 /* Fill the XMM registers in the register file with dummy values.  For
506    cases where we don't have access to the XMM registers.  I think
507    this is cleaner than printing a warning.  For a cleaner solution,
508    we should gdbarchify the i386 family.  */
509 static void
510 dummy_sse_values ()
511 {
512   /* C doesn't have a syntax for NaN's, so write it out as an array of
513      longs.  */
514   static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
515   static long mxcsr = 0x1f80;
516   int reg;
517
518   for (reg = 0; reg < 8; reg++)
519     supply_register (XMM0_REGNUM + reg, (char *) dummy);
520   supply_register (MXCSR_REGNUM, (char *) &mxcsr);
521 }
522
523 #else
524
525 /* Stub versions of the above routines, for systems that don't have
526    PTRACE_GETXFPREGS.  */
527 static int store_xfpregs (int tid) { return 0; }
528 static int fetch_xfpregs (int tid) { return 0; }
529 static void dummy_sse_values () {}
530
531 #endif
532
533 \f
534 /* Transferring arbitrary registers between GDB and inferior.  */
535
536 /* Fetch registers from the child process.
537    Fetch all if regno == -1, otherwise fetch all ordinary
538    registers or all floating point registers depending
539    upon the value of regno. */
540
541 void
542 fetch_inferior_registers (int regno)
543 {
544   /* linux lwp id's are process id's */
545   int tid;
546
547   if ((tid = TIDGET (inferior_pid)) == 0)
548     tid = inferior_pid;         /* not a threaded program */
549
550   /* Use the xfpregs requests whenever possible, since they transfer
551      more registers in one system call, and we'll cache the results.
552      But remember that fetch_xfpregs can fail, and return zero.  */
553   if (regno == -1)
554     {
555       fetch_regs (tid);
556       if (fetch_xfpregs (tid))
557         return;
558       fetch_fpregs (tid);
559       return;
560     }
561
562   if (GETREGS_SUPPLIES (regno))
563     {
564       fetch_regs (tid);
565       return;
566     }
567
568   if (GETXFPREGS_SUPPLIES (regno))
569     {
570       if (fetch_xfpregs (tid))
571         return;
572
573       /* Either our processor or our kernel doesn't support the SSE
574          registers, so read the FP registers in the traditional way,
575          and fill the SSE registers with dummy values.  It would be
576          more graceful to handle differences in the register set using
577          gdbarch.  Until then, this will at least make things work
578          plausibly.  */
579       fetch_fpregs (tid);
580       dummy_sse_values ();
581       return;
582     }
583
584   internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
585                   "got request for bad register number %d", regno);
586 }
587
588
589 /* Store our register values back into the inferior.
590    If REGNO is -1, do this for all registers.
591    Otherwise, REGNO specifies which register, which
592    then determines whether we store all ordinary
593    registers or all of the floating point registers. */
594
595 void
596 store_inferior_registers (regno)
597      int regno;
598 {
599   /* linux lwp id's are process id's */
600   int tid;
601
602   if ((tid = TIDGET (inferior_pid)) == 0)
603     tid = inferior_pid;         /* not a threaded program */
604
605   /* Use the xfpregs requests whenever possible, since they transfer
606      more registers in one system call.  But remember that
607      store_xfpregs can fail, and return zero.  */
608   if (regno == -1)
609     {
610       store_regs (tid);
611       if (store_xfpregs (tid))
612         return;
613       store_fpregs (tid);
614       return;
615     }
616
617   if (GETREGS_SUPPLIES (regno))
618     {
619       store_regs (tid);
620       return;
621     }
622
623   if (GETXFPREGS_SUPPLIES (regno))
624     {
625       if (store_xfpregs (tid))
626         return;
627
628       /* Either our processor or our kernel doesn't support the SSE
629          registers, so just write the FP registers in the traditional way.  */
630       store_fpregs (tid);
631       return;
632     }
633
634   internal_error ("i386-linux-nat.c (store_inferior_registers): "
635                   "got request to store bad register number %d", regno);
636 }
637
638
639 \f
640 /* Interpreting register set info found in core files.  */
641
642 /* Provide registers to GDB from a core file.
643
644    (We can't use the generic version of this function in
645    core-regset.c, because Linux has *three* different kinds of
646    register set notes.  core-regset.c would have to call
647    supply_xfpregset, which most platforms don't have.)
648
649    CORE_REG_SECT points to an array of bytes, which are the contents
650    of a `note' from a core file which BFD thinks might contain
651    register contents.  CORE_REG_SIZE is its size.
652
653    WHICH says which register set corelow suspects this is:
654    0 --- the general register set, in gregset format
655    2 --- the floating-point register set, in fpregset format
656    3 --- the extended floating-point register set, in struct
657          user_xfpregs_struct format
658
659    DUMMY isn't used on Linux.  */
660 static void
661 i386_linux_fetch_core_registers (char *core_reg_sect,
662                                  unsigned core_reg_size,
663                                  int which,
664                                  CORE_ADDR dummy)
665 {
666   gregset_t gregset;
667   fpregset_t fpregset;
668
669   switch (which)
670     {
671     case 0:
672       if (core_reg_size != sizeof (gregset))
673         warning ("wrong size gregset struct in core file");
674       else
675         {
676           memcpy (&gregset, core_reg_sect, sizeof (gregset));
677           supply_gregset (&gregset);
678         }
679       break;
680
681     case 2:
682       if (core_reg_size != sizeof (fpregset))
683         warning ("wrong size fpregset struct in core file");
684       else
685         {
686           memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
687           supply_fpregset (&fpregset);
688         }
689       break;
690
691 #ifdef HAVE_PTRACE_GETXFPREGS
692       {
693         struct user_xfpregs_struct xfpregset;
694       case 3:
695         if (core_reg_size != sizeof (struct user_xfpregs_struct))
696           warning ("wrong size user_xfpregs_struct in core file");
697         else
698           {
699             memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset));
700             supply_xfpregset (&xfpregset);
701           }
702         break;
703       }
704 #endif
705
706     default:
707       /* We've covered all the kinds of registers we know about here,
708          so this must be something we wouldn't know what to do with
709          anyway.  Just ignore it.  */
710       break;
711     }
712 }
713
714
715 static struct core_fns i386_linux_nat_core_fns =
716 {
717   bfd_target_elf_flavour,               /* core_flavour */
718   default_check_format,                 /* check_format */
719   default_core_sniffer,                 /* core_sniffer */
720   i386_linux_fetch_core_registers,      /* core_read_registers */
721   NULL                                  /* next */
722 };
723
724 \f
725 /* Calling functions in shared libraries.  */
726
727 /* Find the minimal symbol named NAME, and return both the minsym
728    struct and its objfile.  This probably ought to be in minsym.c, but
729    everything there is trying to deal with things like C++ and
730    SOFUN_ADDRESS_MAYBE_TURQUOISE, ...  Since this is so simple, it may
731    be considered too special-purpose for general consumption.  */
732
733 static struct minimal_symbol *
734 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
735 {
736   struct objfile *objfile;
737
738   ALL_OBJFILES (objfile)
739     {
740       struct minimal_symbol *msym;
741
742       ALL_OBJFILE_MSYMBOLS (objfile, msym)
743         {
744           if (SYMBOL_NAME (msym)
745               && STREQ (SYMBOL_NAME (msym), name))
746             {
747               *objfile_p = objfile;
748               return msym;
749             }
750         }
751     }
752
753   return 0;
754 }
755
756
757 static CORE_ADDR
758 skip_hurd_resolver (CORE_ADDR pc)
759 {
760   /* The HURD dynamic linker is part of the GNU C library, so many
761      GNU/Linux distributions use it.  (All ELF versions, as far as I
762      know.)  An unresolved PLT entry points to "_dl_runtime_resolve",
763      which calls "fixup" to patch the PLT, and then passes control to
764      the function.
765
766      We look for the symbol `_dl_runtime_resolve', and find `fixup' in
767      the same objfile.  If we are at the entry point of `fixup', then
768      we set a breakpoint at the return address (at the top of the
769      stack), and continue.
770   
771      It's kind of gross to do all these checks every time we're
772      called, since they don't change once the executable has gotten
773      started.  But this is only a temporary hack --- upcoming versions
774      of Linux will provide a portable, efficient interface for
775      debugging programs that use shared libraries.  */
776
777   struct objfile *objfile;
778   struct minimal_symbol *resolver 
779     = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
780
781   if (resolver)
782     {
783       struct minimal_symbol *fixup
784         = lookup_minimal_symbol ("fixup", 0, objfile);
785
786       if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
787         return (SAVED_PC_AFTER_CALL (get_current_frame ()));
788     }
789
790   return 0;
791 }      
792
793
794 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
795    This function:
796    1) decides whether a PLT has sent us into the linker to resolve
797       a function reference, and 
798    2) if so, tells us where to set a temporary breakpoint that will
799       trigger when the dynamic linker is done.  */
800
801 CORE_ADDR
802 i386_linux_skip_solib_resolver (CORE_ADDR pc)
803 {
804   CORE_ADDR result;
805
806   /* Plug in functions for other kinds of resolvers here.  */
807   result = skip_hurd_resolver (pc);
808   if (result)
809     return result;
810
811   return 0;
812 }
813
814
815 \f
816 /* Module initialization.  */
817
818 void
819 _initialize_i386_linux_nat ()
820 {
821   add_core_fns (&i386_linux_nat_core_fns);
822 }