* i386-linux-nat.c [! HAVE_PTRACE_GETFPXREGS] (fetch_fpxregs,
[external/binutils.git] / gdb / i386-linux-nat.c
1 /* Native-dependent code for Linux/x86.
2    Copyright 1999, 2000 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24
25 /* For i386_linux_skip_solib_resolver.  */
26 #include "symtab.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29
30 #include <sys/ptrace.h>
31 #include <sys/user.h>
32 #include <sys/procfs.h>
33
34 #ifdef HAVE_SYS_REG_H
35 #include <sys/reg.h>
36 #endif
37
38 /* Prototypes for supply_gregset etc.  */
39 #include "gregset.h"
40
41 /* Prototypes for i387_supply_fsave etc.  */
42 #include "i387-nat.h"
43
44 /* On Linux, threads are implemented as pseudo-processes, in which
45    case we may be tracing more than one process at a time.  In that
46    case, inferior_pid will contain the main process ID and the
47    individual thread (process) ID mashed together.  These macros are
48    used to separate them out.  These definitions should be overridden
49    if thread support is included.  */
50
51 #if !defined (PIDGET)   /* Default definition for PIDGET/TIDGET.  */
52 #define PIDGET(PID)     PID
53 #define TIDGET(PID)     0
54 #endif
55 \f
56
57 /* The register sets used in Linux ELF core-dumps are identical to the
58    register sets in `struct user' that is used for a.out core-dumps,
59    and is also used by `ptrace'.  The corresponding types are
60    `elf_gregset_t' for the general-purpose registers (with
61    `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
62    for the floating-point registers.
63
64    Those types used to be available under the names `gregset_t' and
65    `fpregset_t' too, and this file used those names in the past.  But
66    those names are now used for the register sets used in the
67    `mcontext_t' type, and have a different size and layout.  */
68
69 /* Mapping between the general-purpose registers in `struct user'
70    format and GDB's register array layout.  */
71 static int regmap[] = 
72 {
73   EAX, ECX, EDX, EBX,
74   UESP, EBP, ESI, EDI,
75   EIP, EFL, CS, SS,
76   DS, ES, FS, GS
77 };
78
79 /* Which ptrace request retrieves which registers?
80    These apply to the corresponding SET requests as well.  */
81 #define GETREGS_SUPPLIES(regno) \
82   (0 <= (regno) && (regno) <= 15)
83 #define GETFPREGS_SUPPLIES(regno) \
84   (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
85 #define GETFPXREGS_SUPPLIES(regno) \
86   (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
87
88 /* Does the current host support the GETREGS request?  */
89 int have_ptrace_getregs =
90 #ifdef HAVE_PTRACE_GETREGS
91   1
92 #else
93   0
94 #endif
95 ;
96
97 /* Does the current host support the GETFPXREGS request?  The header
98    file may or may not define it, and even if it is defined, the
99    kernel will return EIO if it's running on a pre-SSE processor.
100
101    My instinct is to attach this to some architecture- or
102    target-specific data structure, but really, a particular GDB
103    process can only run on top of one kernel at a time.  So it's okay
104    for this to be a simple variable.  */
105 int have_ptrace_getfpxregs =
106 #ifdef HAVE_PTRACE_GETFPXREGS
107   1
108 #else
109   0
110 #endif
111 ;
112 \f
113
114 /* Fetching registers directly from the U area, one at a time.  */
115
116 /* FIXME: kettenis/2000-03-05: This duplicates code from `inptrace.c'.
117    The problem is that we define FETCH_INFERIOR_REGISTERS since we
118    want to use our own versions of {fetch,store}_inferior_registers
119    that use the GETREGS request.  This means that the code in
120    `infptrace.c' is #ifdef'd out.  But we need to fall back on that
121    code when GDB is running on top of a kernel that doesn't support
122    the GETREGS request.  I want to avoid changing `infptrace.c' right
123    now.  */
124
125 #ifndef PT_READ_U
126 #define PT_READ_U PTRACE_PEEKUSR
127 #endif
128 #ifndef PT_WRITE_U
129 #define PT_WRITE_U PTRACE_POKEUSR
130 #endif
131
132 /* Default the type of the ptrace transfer to int.  */
133 #ifndef PTRACE_XFER_TYPE
134 #define PTRACE_XFER_TYPE int
135 #endif
136
137 /* Registers we shouldn't try to fetch.  */
138 #if !defined (CANNOT_FETCH_REGISTER)
139 #define CANNOT_FETCH_REGISTER(regno) 0
140 #endif
141
142 /* Fetch one register.  */
143
144 static void
145 fetch_register (int regno)
146 {
147   /* This isn't really an address.  But ptrace thinks of it as one.  */
148   CORE_ADDR regaddr;
149   char mess[128];               /* For messages */
150   register int i;
151   unsigned int offset;          /* Offset of registers within the u area.  */
152   char buf[MAX_REGISTER_RAW_SIZE];
153   int tid;
154
155   if (CANNOT_FETCH_REGISTER (regno))
156     {
157       memset (buf, '\0', REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
158       supply_register (regno, buf);
159       return;
160     }
161
162   /* Overload thread id onto process id */
163   if ((tid = TIDGET (inferior_pid)) == 0)
164     tid = inferior_pid;         /* no thread id, just use process id */
165
166   offset = U_REGS_OFFSET;
167
168   regaddr = register_addr (regno, offset);
169   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
170     {
171       errno = 0;
172       *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
173                                                (PTRACE_ARG3_TYPE) regaddr, 0);
174       regaddr += sizeof (PTRACE_XFER_TYPE);
175       if (errno != 0)
176         {
177           sprintf (mess, "reading register %s (#%d)", 
178                    REGISTER_NAME (regno), regno);
179           perror_with_name (mess);
180         }
181     }
182   supply_register (regno, buf);
183 }
184
185 /* Fetch register values from the inferior.
186    If REGNO is negative, do this for all registers.
187    Otherwise, REGNO specifies which register (so we can save time). */
188
189 void
190 old_fetch_inferior_registers (int regno)
191 {
192   if (regno >= 0)
193     {
194       fetch_register (regno);
195     }
196   else
197     {
198       for (regno = 0; regno < ARCH_NUM_REGS; regno++)
199         {
200           fetch_register (regno);
201         }
202     }
203 }
204
205 /* Registers we shouldn't try to store.  */
206 #if !defined (CANNOT_STORE_REGISTER)
207 #define CANNOT_STORE_REGISTER(regno) 0
208 #endif
209
210 /* Store one register. */
211
212 static void
213 store_register (int regno)
214 {
215   /* This isn't really an address.  But ptrace thinks of it as one.  */
216   CORE_ADDR regaddr;
217   char mess[128];               /* For messages */
218   register int i;
219   unsigned int offset;          /* Offset of registers within the u area.  */
220   int tid;
221
222   if (CANNOT_STORE_REGISTER (regno))
223     {
224       return;
225     }
226
227   /* Overload thread id onto process id */
228   if ((tid = TIDGET (inferior_pid)) == 0)
229     tid = inferior_pid;         /* no thread id, just use process id */
230
231   offset = U_REGS_OFFSET;
232
233   regaddr = register_addr (regno, offset);
234   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
235     {
236       errno = 0;
237       ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
238               *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
239       regaddr += sizeof (PTRACE_XFER_TYPE);
240       if (errno != 0)
241         {
242           sprintf (mess, "writing register %s (#%d)", 
243                    REGISTER_NAME (regno), regno);
244           perror_with_name (mess);
245         }
246     }
247 }
248
249 /* Store our register values back into the inferior.
250    If REGNO is negative, do this for all registers.
251    Otherwise, REGNO specifies which register (so we can save time).  */
252
253 void
254 old_store_inferior_registers (int regno)
255 {
256   if (regno >= 0)
257     {
258       store_register (regno);
259     }
260   else
261     {
262       for (regno = 0; regno < ARCH_NUM_REGS; regno++)
263         {
264           store_register (regno);
265         }
266     }
267 }
268 \f
269
270 /* Transfering the general-purpose registers between GDB, inferiors
271    and core files.  */
272
273 /* Fill GDB's register array with the genereal-purpose register values
274    in *GREGSETP.  */
275
276 void
277 supply_gregset (elf_gregset_t *gregsetp)
278 {
279   elf_greg_t *regp = (elf_greg_t *) gregsetp;
280   int i;
281
282   for (i = 0; i < NUM_GREGS; i++)
283     supply_register (i, (char *) (regp + regmap[i]));
284 }
285
286 /* Fill register REGNO (if it is a general-purpose register) in
287    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
288    do this for all registers.  */
289
290 void
291 fill_gregset (elf_gregset_t *gregsetp, int regno)
292 {
293   elf_greg_t *regp = (elf_greg_t *) gregsetp;
294   int i;
295
296   for (i = 0; i < NUM_GREGS; i++)
297     if ((regno == -1 || regno == i))
298       *(regp + regmap[i]) = *(elf_greg_t *) &registers[REGISTER_BYTE (i)];
299 }
300
301 #ifdef HAVE_PTRACE_GETREGS
302
303 /* Fetch all general-purpose registers from process/thread TID and
304    store their values in GDB's register array.  */
305
306 static void
307 fetch_regs (int tid)
308 {
309   elf_gregset_t regs;
310
311   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
312     {
313       if (errno == EIO)
314         {
315           /* The kernel we're running on doesn't support the GETREGS
316              request.  Reset `have_ptrace_getregs'.  */
317           have_ptrace_getregs = 0;
318           return;
319         }
320
321       perror_with_name ("Couldn't get registers");
322     }
323
324   supply_gregset (&regs);
325 }
326
327 /* Store all valid general-purpose registers in GDB's register array
328    into the process/thread specified by TID.  */
329
330 static void
331 store_regs (int tid, int regno)
332 {
333   elf_gregset_t regs;
334
335   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
336     perror_with_name ("Couldn't get registers");
337
338   fill_gregset (&regs, regno);
339   
340   if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
341     perror_with_name ("Couldn't write registers");
342 }
343
344 #else
345
346 static void fetch_regs (int tid) {}
347 static void store_regs (int tid, int regno) {}
348
349 #endif
350 \f
351
352 /* Transfering floating-point registers between GDB, inferiors and cores.  */
353
354 /* Fill GDB's register array with the floating-point register values in
355    *FPREGSETP.  */
356
357 void 
358 supply_fpregset (elf_fpregset_t *fpregsetp)
359 {
360   i387_supply_fsave ((char *) fpregsetp);
361 }
362
363 /* Fill register REGNO (if it is a floating-point register) in
364    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
365    do this for all registers.  */
366
367 void
368 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
369 {
370   i387_fill_fsave ((char *) fpregsetp, regno);
371 }
372
373 #ifdef HAVE_PTRACE_GETREGS
374
375 /* Fetch all floating-point registers from process/thread TID and store
376    thier values in GDB's register array.  */
377
378 static void
379 fetch_fpregs (int tid)
380 {
381   elf_fpregset_t fpregs;
382
383   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
384     perror_with_name ("Couldn't get floating point status");
385
386   supply_fpregset (&fpregs);
387 }
388
389 /* Store all valid floating-point registers in GDB's register array
390    into the process/thread specified by TID.  */
391
392 static void
393 store_fpregs (int tid, int regno)
394 {
395   elf_fpregset_t fpregs;
396
397   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
398     perror_with_name ("Couldn't get floating point status");
399
400   fill_fpregset (&fpregs, regno);
401
402   if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
403     perror_with_name ("Couldn't write floating point status");
404 }
405
406 #else
407
408 static void fetch_fpregs (int tid) {}
409 static void store_fpregs (int tid, int regno) {}
410
411 #endif
412 \f
413
414 /* Transfering floating-point and SSE registers to and from GDB.  */
415
416 #ifdef HAVE_PTRACE_GETFPXREGS
417
418 /* Fill GDB's register array with the floating-point and SSE register
419    values in *FPXREGSETP.  */
420
421 static void
422 supply_fpxregset (elf_fpxregset_t *fpxregsetp)
423 {
424   i387_supply_fxsave ((char *) fpxregsetp);
425 }
426
427 /* Fill register REGNO (if it is a floating-point or SSE register) in
428    *FPXREGSETP with the value in GDB's register array.  If REGNO is
429    -1, do this for all registers.  */
430
431 static void
432 fill_fpxregset (elf_fpxregset_t *fpxregsetp, int regno)
433 {
434   i387_fill_fxsave ((char *) fpxregsetp, regno);
435 }
436
437 /* Fetch all registers covered by the PTRACE_GETFPXREGS request from
438    process/thread TID and store their values in GDB's register array.
439    Return non-zero if successful, zero otherwise.  */
440
441 static int
442 fetch_fpxregs (int tid)
443 {
444   elf_fpxregset_t fpxregs;
445
446   if (! have_ptrace_getfpxregs)
447     return 0;
448
449   if (ptrace (PTRACE_GETFPXREGS, tid, 0, (int) &fpxregs) < 0)
450     {
451       if (errno == EIO)
452         {
453           have_ptrace_getfpxregs = 0;
454           return 0;
455         }
456
457       perror_with_name ("Couldn't read floating-point and SSE registers");
458     }
459
460   supply_fpxregset (&fpxregs);
461   return 1;
462 }
463
464 /* Store all valid registers in GDB's register array covered by the
465    PTRACE_SETFPXREGS request into the process/thread specified by TID.
466    Return non-zero if successful, zero otherwise.  */
467
468 static int
469 store_fpxregs (int tid, int regno)
470 {
471   elf_fpxregset_t fpxregs;
472
473   if (! have_ptrace_getfpxregs)
474     return 0;
475   
476   if (ptrace (PTRACE_GETFPXREGS, tid, 0, &fpxregs) == -1)
477     perror_with_name ("Couldn't read floating-point and SSE registers");
478
479   fill_fpxregset (&fpxregs, regno);
480
481   if (ptrace (PTRACE_SETFPXREGS, tid, 0, &fpxregs) == -1)
482     perror_with_name ("Couldn't write floating-point and SSE registers");
483
484   return 1;
485 }
486
487 /* Fill the XMM registers in the register array with dummy values.  For
488    cases where we don't have access to the XMM registers.  I think
489    this is cleaner than printing a warning.  For a cleaner solution,
490    we should gdbarchify the i386 family.  */
491
492 static void
493 dummy_sse_values (void)
494 {
495   /* C doesn't have a syntax for NaN's, so write it out as an array of
496      longs.  */
497   static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
498   static long mxcsr = 0x1f80;
499   int reg;
500
501   for (reg = 0; reg < 8; reg++)
502     supply_register (XMM0_REGNUM + reg, (char *) dummy);
503   supply_register (MXCSR_REGNUM, (char *) &mxcsr);
504 }
505
506 #else
507
508 static int fetch_fpxregs (int tid) { return 0; }
509 static int store_fpxregs (int tid, int regno) { return 0; }
510 static void dummy_sse_values (void) {}
511
512 #endif /* HAVE_PTRACE_GETFPXREGS */
513 \f
514
515 /* Transferring arbitrary registers between GDB and inferior.  */
516
517 /* Fetch register REGNO from the child process.  If REGNO is -1, do
518    this for all registers (including the floating point and SSE
519    registers).  */
520
521 void
522 fetch_inferior_registers (int regno)
523 {
524   int tid;
525
526   /* Use the old method of peeking around in `struct user' if the
527      GETREGS request isn't available.  */
528   if (! have_ptrace_getregs)
529     {
530       old_fetch_inferior_registers (regno);
531       return;
532     }
533
534   /* Linux LWP ID's are process ID's.  */
535   if ((tid = TIDGET (inferior_pid)) == 0)
536     tid = inferior_pid;         /* Not a threaded program.  */
537
538   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
539      transfers more registers in one system call, and we'll cache the
540      results.  But remember that fetch_fpxregs can fail, and return
541      zero.  */
542   if (regno == -1)
543     {
544       fetch_regs (tid);
545
546       /* The call above might reset `have_ptrace_getregs'.  */
547       if (! have_ptrace_getregs)
548         {
549           old_fetch_inferior_registers (-1);
550           return;
551         }
552
553       if (fetch_fpxregs (tid))
554         return;
555       fetch_fpregs (tid);
556       return;
557     }
558
559   if (GETREGS_SUPPLIES (regno))
560     {
561       fetch_regs (tid);
562       return;
563     }
564
565   if (GETFPXREGS_SUPPLIES (regno))
566     {
567       if (fetch_fpxregs (tid))
568         return;
569
570       /* Either our processor or our kernel doesn't support the SSE
571          registers, so read the FP registers in the traditional way,
572          and fill the SSE registers with dummy values.  It would be
573          more graceful to handle differences in the register set using
574          gdbarch.  Until then, this will at least make things work
575          plausibly.  */
576       fetch_fpregs (tid);
577       dummy_sse_values ();
578       return;
579     }
580
581   internal_error ("Got request for bad register number %d.", regno);
582 }
583
584 /* Store register REGNO back into the child process.  If REGNO is -1,
585    do this for all registers (including the floating point and SSE
586    registers).  */
587 void
588 store_inferior_registers (int regno)
589 {
590   int tid;
591
592   /* Use the old method of poking around in `struct user' if the
593      SETREGS request isn't available.  */
594   if (! have_ptrace_getregs)
595     {
596       old_store_inferior_registers (regno);
597       return;
598     }
599
600   /* Linux LWP ID's are process ID's.  */
601   if ((tid = TIDGET (inferior_pid)) == 0)
602     tid = inferior_pid;         /* Not a threaded program.  */
603
604   /* Use the PTRACE_SETFPXREGS requests whenever possible, since it
605      transfers more registers in one system call.  But remember that
606      store_fpxregs can fail, and return zero.  */
607   if (regno == -1)
608     {
609       store_regs (tid, regno);
610       if (store_fpxregs (tid, regno))
611         return;
612       store_fpregs (tid, regno);
613       return;
614     }
615
616   if (GETREGS_SUPPLIES (regno))
617     {
618       store_regs (tid, regno);
619       return;
620     }
621
622   if (GETFPXREGS_SUPPLIES (regno))
623     {
624       if (store_fpxregs (tid, regno))
625         return;
626
627       /* Either our processor or our kernel doesn't support the SSE
628          registers, so just write the FP registers in the traditional
629          way.  */
630       store_fpregs (tid, regno);
631       return;
632     }
633
634   internal_error ("Got request to store bad register number %d.", regno);
635 }
636 \f
637
638 /* Interpreting register set info found in core files.  */
639
640 /* Provide registers to GDB from a core file.
641
642    (We can't use the generic version of this function in
643    core-regset.c, because Linux has *three* different kinds of
644    register set notes.  core-regset.c would have to call
645    supply_fpxregset, which most platforms don't have.)
646
647    CORE_REG_SECT points to an array of bytes, which are the contents
648    of a `note' from a core file which BFD thinks might contain
649    register contents.  CORE_REG_SIZE is its size.
650
651    WHICH says which register set corelow suspects this is:
652      0 --- the general-purpose register set, in elf_gregset_t format
653      2 --- the floating-point register set, in elf_fpregset_t format
654      3 --- the extended floating-point register set, in elf_fpxregset_t format
655
656    REG_ADDR isn't used on Linux.  */
657
658 static void
659 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
660                       int which, CORE_ADDR reg_addr)
661 {
662   elf_gregset_t gregset;
663   elf_fpregset_t fpregset;
664
665   switch (which)
666     {
667     case 0:
668       if (core_reg_size != sizeof (gregset))
669         warning ("Wrong size gregset in core file.");
670       else
671         {
672           memcpy (&gregset, core_reg_sect, sizeof (gregset));
673           supply_gregset (&gregset);
674         }
675       break;
676
677     case 2:
678       if (core_reg_size != sizeof (fpregset))
679         warning ("Wrong size fpregset in core file.");
680       else
681         {
682           memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
683           supply_fpregset (&fpregset);
684         }
685       break;
686
687 #ifdef HAVE_PTRACE_GETFPXREGS
688       {
689         elf_fpxregset_t fpxregset;
690
691       case 3:
692         if (core_reg_size != sizeof (fpxregset))
693           warning ("Wrong size fpxregset in core file.");
694         else
695           {
696             memcpy (&fpxregset, core_reg_sect, sizeof (fpxregset));
697             supply_fpxregset (&fpxregset);
698           }
699         break;
700       }
701 #endif
702
703     default:
704       /* We've covered all the kinds of registers we know about here,
705          so this must be something we wouldn't know what to do with
706          anyway.  Just ignore it.  */
707       break;
708     }
709 }
710 \f
711
712 /* The instruction for a Linux system call is:
713        int $0x80
714    or 0xcd 0x80.  */
715
716 static const unsigned char linux_syscall[] = { 0xcd, 0x80 };
717
718 #define LINUX_SYSCALL_LEN (sizeof linux_syscall)
719
720 /* The system call number is stored in the %eax register.  */
721 #define LINUX_SYSCALL_REGNUM 0  /* %eax */
722
723 /* We are specifically interested in the sigreturn and rt_sigreturn
724    system calls.  */
725
726 #ifndef SYS_sigreturn
727 #define SYS_sigreturn           0x77
728 #endif
729 #ifndef SYS_rt_sigreturn
730 #define SYS_rt_sigreturn        0xad
731 #endif
732
733 /* Offset to saved processor flags, from <asm/sigcontext.h>.  */
734 #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (64)
735
736 /* Resume execution of the inferior process.
737    If STEP is nonzero, single-step it.
738    If SIGNAL is nonzero, give it that signal.  */
739
740 void
741 child_resume (int pid, int step, enum target_signal signal)
742 {
743   int request = PTRACE_CONT;
744
745   if (pid == -1)
746     /* Resume all threads.  */
747     /* I think this only gets used in the non-threaded case, where "resume
748        all threads" and "resume inferior_pid" are the same.  */
749     pid = inferior_pid;
750
751   if (step)
752     {
753       CORE_ADDR pc = read_pc_pid (pid);
754       unsigned char buf[LINUX_SYSCALL_LEN];
755
756       request = PTRACE_SINGLESTEP;
757
758       /* Returning from a signal trampoline is done by calling a
759          special system call (sigreturn or rt_sigreturn, see
760          i386-linux-tdep.c for more information).  This system call
761          restores the registers that were saved when the signal was
762          raised, including %eflags.  That means that single-stepping
763          won't work.  Instead, we'll have to modify the signal context
764          that's about to be restored, and set the trace flag there.  */
765
766       /* First check if PC is at a system call.  */
767       if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0
768           && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0)
769         {
770           int syscall = read_register_pid (LINUX_SYSCALL_REGNUM, pid);
771
772           /* Then check the system call number.  */
773           if (syscall == SYS_sigreturn || syscall == SYS_rt_sigreturn)
774             {
775               CORE_ADDR sp = read_register (SP_REGNUM);
776               CORE_ADDR addr = sp;
777               unsigned long int eflags;
778               
779               if (syscall == SYS_rt_sigreturn)
780                 addr = read_memory_integer (sp + 8, 4) + 20;
781
782               /* Set the trace flag in the context that's about to be
783                  restored.  */
784               addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET;
785               read_memory (addr, (char *) &eflags, 4);
786               eflags |= 0x0100;
787               write_memory (addr, (char *) &eflags, 4);
788             }
789         }
790     }
791
792   if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1)
793     perror_with_name ("ptrace");
794 }
795 \f
796
797 /* Calling functions in shared libraries.  */
798 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
799    target-dependent file?  The function
800    `i386_linux_skip_solib_resolver' is mentioned in
801    `config/i386/tm-linux.h'.  */
802
803 /* Find the minimal symbol named NAME, and return both the minsym
804    struct and its objfile.  This probably ought to be in minsym.c, but
805    everything there is trying to deal with things like C++ and
806    SOFUN_ADDRESS_MAYBE_TURQUOISE, ...  Since this is so simple, it may
807    be considered too special-purpose for general consumption.  */
808
809 static struct minimal_symbol *
810 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
811 {
812   struct objfile *objfile;
813
814   ALL_OBJFILES (objfile)
815     {
816       struct minimal_symbol *msym;
817
818       ALL_OBJFILE_MSYMBOLS (objfile, msym)
819         {
820           if (SYMBOL_NAME (msym)
821               && STREQ (SYMBOL_NAME (msym), name))
822             {
823               *objfile_p = objfile;
824               return msym;
825             }
826         }
827     }
828
829   return 0;
830 }
831
832 static CORE_ADDR
833 skip_hurd_resolver (CORE_ADDR pc)
834 {
835   /* The HURD dynamic linker is part of the GNU C library, so many
836      GNU/Linux distributions use it.  (All ELF versions, as far as I
837      know.)  An unresolved PLT entry points to "_dl_runtime_resolve",
838      which calls "fixup" to patch the PLT, and then passes control to
839      the function.
840
841      We look for the symbol `_dl_runtime_resolve', and find `fixup' in
842      the same objfile.  If we are at the entry point of `fixup', then
843      we set a breakpoint at the return address (at the top of the
844      stack), and continue.
845   
846      It's kind of gross to do all these checks every time we're
847      called, since they don't change once the executable has gotten
848      started.  But this is only a temporary hack --- upcoming versions
849      of Linux will provide a portable, efficient interface for
850      debugging programs that use shared libraries.  */
851
852   struct objfile *objfile;
853   struct minimal_symbol *resolver 
854     = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
855
856   if (resolver)
857     {
858       struct minimal_symbol *fixup
859         = lookup_minimal_symbol ("fixup", 0, objfile);
860
861       if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
862         return (SAVED_PC_AFTER_CALL (get_current_frame ()));
863     }
864
865   return 0;
866 }      
867
868 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
869    This function:
870    1) decides whether a PLT has sent us into the linker to resolve
871       a function reference, and 
872    2) if so, tells us where to set a temporary breakpoint that will
873       trigger when the dynamic linker is done.  */
874
875 CORE_ADDR
876 i386_linux_skip_solib_resolver (CORE_ADDR pc)
877 {
878   CORE_ADDR result;
879
880   /* Plug in functions for other kinds of resolvers here.  */
881   result = skip_hurd_resolver (pc);
882   if (result)
883     return result;
884
885   return 0;
886 }
887 \f
888
889 /* Register that we are able to handle Linux ELF core file formats.  */
890
891 static struct core_fns linux_elf_core_fns =
892 {
893   bfd_target_elf_flavour,               /* core_flavour */
894   default_check_format,                 /* check_format */
895   default_core_sniffer,                 /* core_sniffer */
896   fetch_core_registers,                 /* core_read_registers */
897   NULL                                  /* next */
898 };
899
900 void
901 _initialize_i386_linux_nat (void)
902 {
903   add_core_fns (&linux_elf_core_fns);
904 }