2e46ead746ca66c2a06db9d5f1cdd73f7c797e49
[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,
18    Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "gdbcore.h"
23
24 /* For i386_linux_skip_solib_resolver.  */
25 #include "symtab.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 /* On Linux, threads are implemented as pseudo-processes, in which
38    case we may be tracing more than one process at a time.  In that
39    case, inferior_pid will contain the main process ID and the
40    individual thread (process) ID mashed together.  These macros are
41    used to separate them out.  These definitions should be overridden
42    if thread support is included.  */
43
44 #if !defined (PIDGET)   /* Default definition for PIDGET/TIDGET.  */
45 #define PIDGET(PID)     PID
46 #define TIDGET(PID)     0
47 #endif
48
49
50 /* The register sets used in Linux ELF core-dumps are identical to the
51    register sets in `struct user' that is used for a.out core-dumps,
52    and is also used by `ptrace'.  The corresponding types are
53    `elf_gregset_t' for the general-purpose registers (with
54    `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
55    for the floating-point registers.
56
57    Those types used to be available under the names `gregset_t' and
58    `fpregset_t' too, and this file used those names in the past.  But
59    those names are now used for the register sets used in the
60    `mcontext_t' type, and have a different size and layout.  */
61
62 /* Mapping between the general-purpose registers in `struct user'
63    format and GDB's register array layout.  */
64 static int regmap[] = 
65 {
66   EAX, ECX, EDX, EBX,
67   UESP, EBP, ESI, EDI,
68   EIP, EFL, CS, SS,
69   DS, ES, FS, GS
70 };
71
72 /* Which ptrace request retrieves which registers?
73    These apply to the corresponding SET requests as well.  */
74 #define GETREGS_SUPPLIES(regno) \
75   (0 <= (regno) && (regno) <= 15)
76 #define GETFPREGS_SUPPLIES(regno) \
77   (FP0_REGNUM <= (regno) && (regno) <= LAST_FPU_CTRL_REGNUM)
78 #define GETXFPREGS_SUPPLIES(regno) \
79   (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM)
80
81 /* Does the current host support the GETREGS request?  */
82 int have_ptrace_getregs =
83 #ifdef HAVE_PTRACE_GETREGS
84   1
85 #else
86   0
87 #endif
88 ;
89
90 /* Does the current host support the GETXFPREGS request?  The header
91    file may or may not define it, and even if it is defined, the
92    kernel will return EIO if it's running on a pre-SSE processor.
93
94    PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
95    Linux kernel patch for SSE support.  That patch may or may not
96    actually make it into the official distribution.  If you find that
97    years have gone by since this stuff was added, and Linux isn't
98    using PTRACE_GETXFPREGS, that means that our patch didn't make it,
99    and you can delete this, and the related code.
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_getxfpregs =
106 #ifdef HAVE_PTRACE_GETXFPREGS
107   1
108 #else
109   0
110 #endif
111 ;
112
113 \f
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 /* Default the type of the ptrace transfer to int.  */
126 #ifndef PTRACE_XFER_TYPE
127 #define PTRACE_XFER_TYPE int
128 #endif
129
130 /* Registers we shouldn't try to fetch.  */
131 #if !defined (CANNOT_FETCH_REGISTER)
132 #define CANNOT_FETCH_REGISTER(regno) 0
133 #endif
134
135 /* Fetch one register.  */
136
137 static void
138 fetch_register (regno)
139      int regno;
140 {
141   /* This isn't really an address.  But ptrace thinks of it as one.  */
142   CORE_ADDR regaddr;
143   char mess[128];               /* For messages */
144   register int i;
145   unsigned int offset;          /* Offset of registers within the u area.  */
146   char buf[MAX_REGISTER_RAW_SIZE];
147   int tid;
148
149   if (CANNOT_FETCH_REGISTER (regno))
150     {
151       memset (buf, '\0', REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
152       supply_register (regno, buf);
153       return;
154     }
155
156   /* Overload thread id onto process id */
157   if ((tid = TIDGET (inferior_pid)) == 0)
158     tid = inferior_pid;         /* no thread id, just use process id */
159
160   offset = U_REGS_OFFSET;
161
162   regaddr = register_addr (regno, offset);
163   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
164     {
165       errno = 0;
166       *(PTRACE_XFER_TYPE *) & buf[i] = ptrace (PT_READ_U, tid,
167                                                (PTRACE_ARG3_TYPE) regaddr, 0);
168       regaddr += sizeof (PTRACE_XFER_TYPE);
169       if (errno != 0)
170         {
171           sprintf (mess, "reading register %s (#%d)", 
172                    REGISTER_NAME (regno), regno);
173           perror_with_name (mess);
174         }
175     }
176   supply_register (regno, buf);
177 }
178
179 /* Fetch register values from the inferior.
180    If REGNO is negative, do this for all registers.
181    Otherwise, REGNO specifies which register (so we can save time). */
182
183 void
184 old_fetch_inferior_registers (regno)
185      int regno;
186 {
187   if (regno >= 0)
188     {
189       fetch_register (regno);
190     }
191   else
192     {
193       for (regno = 0; regno < ARCH_NUM_REGS; regno++)
194         {
195           fetch_register (regno);
196         }
197     }
198 }
199
200 /* Registers we shouldn't try to store.  */
201 #if !defined (CANNOT_STORE_REGISTER)
202 #define CANNOT_STORE_REGISTER(regno) 0
203 #endif
204
205 /* Store one register. */
206
207 static void
208 store_register (regno)
209      int regno;
210 {
211   /* This isn't really an address.  But ptrace thinks of it as one.  */
212   CORE_ADDR regaddr;
213   char mess[128];               /* For messages */
214   register int i;
215   unsigned int offset;          /* Offset of registers within the u area.  */
216   int tid;
217
218   if (CANNOT_STORE_REGISTER (regno))
219     {
220       return;
221     }
222
223   /* Overload thread id onto process id */
224   if ((tid = TIDGET (inferior_pid)) == 0)
225     tid = inferior_pid;         /* no thread id, just use process id */
226
227   offset = U_REGS_OFFSET;
228
229   regaddr = register_addr (regno, offset);
230   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE))
231     {
232       errno = 0;
233       ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
234               *(PTRACE_XFER_TYPE *) & registers[REGISTER_BYTE (regno) + i]);
235       regaddr += sizeof (PTRACE_XFER_TYPE);
236       if (errno != 0)
237         {
238           sprintf (mess, "writing register %s (#%d)", 
239                    REGISTER_NAME (regno), regno);
240           perror_with_name (mess);
241         }
242     }
243 }
244
245 /* Store our register values back into the inferior.
246    If REGNO is negative, do this for all registers.
247    Otherwise, REGNO specifies which register (so we can save time).  */
248
249 void
250 old_store_inferior_registers (regno)
251      int regno;
252 {
253   if (regno >= 0)
254     {
255       store_register (regno);
256     }
257   else
258     {
259       for (regno = 0; regno < ARCH_NUM_REGS; regno++)
260         {
261           store_register (regno);
262         }
263     }
264 }
265
266 \f
267 /* Transfering the general-purpose registers between GDB, inferiors
268    and core files.  */
269
270 /* Fill GDB's register array with the genereal-purpose register values
271    in *GREGSETP.  */
272
273 void
274 supply_gregset (elf_gregset_t *gregsetp)
275 {
276   elf_greg_t *regp = (elf_greg_t *) gregsetp;
277   int regi;
278
279   for (regi = 0; regi < NUM_GREGS; regi++)
280     supply_register (regi, (char *) (regp + regmap[regi]));
281 }
282
283 /* Convert the valid general-purpose register values in GDB's register
284    array to `struct user' format and store them in *GREGSETP.  The
285    array VALID indicates which register values are valid.  If VALID is
286    NULL, all registers are assumed to be valid.  */
287
288 static void
289 convert_to_gregset (elf_gregset_t *gregsetp, signed char *valid)
290 {
291   elf_greg_t *regp = (elf_greg_t *) gregsetp;
292   int regi;
293
294   for (regi = 0; regi < NUM_GREGS; regi++)
295     if (! valid || valid[regi])
296       *(regp + regmap[regi]) = * (int *) &registers[REGISTER_BYTE (regi)];
297 }
298
299 /* Fill register REGNO (if it is a general-purpose register) in
300    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
301    do this for all registers.  */
302 void
303 fill_gregset (elf_gregset_t *gregsetp, int regno)
304 {
305   if (regno == -1)
306     {
307       convert_to_gregset (gregsetp, NULL);
308       return;
309     }
310
311   if (GETREGS_SUPPLIES (regno))
312     {
313       signed char valid[NUM_GREGS];
314
315       memset (valid, 0, sizeof (valid));
316       valid[regno] = 1;
317
318       convert_to_gregset (gregsetp, valid);
319     }
320 }
321
322 #ifdef HAVE_PTRACE_GETREGS
323
324 /* Fetch all general-purpose registers from process/thread TID and
325    store their values in GDB's register array.  */
326
327 static void
328 fetch_regs (int tid)
329 {
330   elf_gregset_t regs;
331   int ret;
332
333   ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &regs);
334   if (ret < 0)
335     {
336       if (errno == EIO)
337         {
338           /* The kernel we're running on doesn't support the GETREGS
339              request.  Reset `have_ptrace_getregs'.  */
340           have_ptrace_getregs = 0;
341           return;
342         }
343
344       warning ("Couldn't get registers.");
345       return;
346     }
347
348   supply_gregset (&regs);
349 }
350
351 /* Store all valid general-purpose registers in GDB's register array
352    into the process/thread specified by TID.  */
353
354 static void
355 store_regs (int tid)
356 {
357   elf_gregset_t regs;
358   int ret;
359
360   ret = ptrace (PTRACE_GETREGS, tid, 0, (int) &regs);
361   if (ret < 0)
362     {
363       warning ("Couldn't get registers.");
364       return;
365     }
366
367   convert_to_gregset (&regs, register_valid);
368
369   ret = ptrace (PTRACE_SETREGS, tid, 0, (int) &regs);
370   if (ret < 0)
371     {
372       warning ("Couldn't write registers.");
373       return;
374     }
375 }
376
377 #else
378
379 static void fetch_regs (int tid) {}
380 static void store_regs (int tid) {}
381
382 #endif
383
384 \f
385 /* Transfering floating-point registers between GDB, inferiors and cores.  */
386
387 /* What is the address of st(N) within the floating-point register set F?  */
388 #define FPREG_ADDR(f, n) ((char *) &(f)->st_space + (n) * 10)
389
390 /* Fill GDB's register array with the floating-point register values in
391    *FPREGSETP.  */
392
393 void 
394 supply_fpregset (elf_fpregset_t *fpregsetp)
395 {
396   int reg;
397   long l;
398
399   /* Supply the floating-point registers.  */
400   for (reg = 0; reg < 8; reg++)
401     supply_register (FP0_REGNUM + reg, FPREG_ADDR (fpregsetp, reg));
402
403   /* We have to mask off the reserved bits in *FPREGSETP before
404      storing the values in GDB's register file.  */
405 #define supply(REGNO, MEMBER)                                           \
406   l = fpregsetp->MEMBER & 0xffff;                                       \
407   supply_register (REGNO, (char *) &l)
408
409   supply (FCTRL_REGNUM, cwd);
410   supply (FSTAT_REGNUM, swd);
411   supply (FTAG_REGNUM, twd);
412   supply_register (FCOFF_REGNUM, (char *) &fpregsetp->fip);
413   supply (FDS_REGNUM, fos);
414   supply_register (FDOFF_REGNUM, (char *) &fpregsetp->foo);
415
416 #undef supply
417
418   /* Extract the code segment and opcode from the  "fcs" member.  */
419   l = fpregsetp->fcs & 0xffff;
420   supply_register (FCS_REGNUM, (char *) &l);
421
422   l = (fpregsetp->fcs >> 16) & ((1 << 11) - 1);
423   supply_register (FOP_REGNUM, (char *) &l);
424 }
425
426 /* Convert the valid floating-point register values in GDB's register
427    array to `struct user' format and store them in *FPREGSETP.  The
428    array VALID indicates which register values are valid.  If VALID is
429    NULL, all registers are assumed to be valid.  */
430
431 static void
432 convert_to_fpregset (elf_fpregset_t *fpregsetp, signed char *valid)
433 {
434   int reg;
435
436   /* Fill in the floating-point registers.  */
437   for (reg = 0; reg < 8; reg++)
438     if (!valid || valid[reg])
439       memcpy (FPREG_ADDR (fpregsetp, reg),
440               &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
441               REGISTER_RAW_SIZE(FP0_REGNUM + reg));
442
443   /* We're not supposed to touch the reserved bits in *FPREGSETP.  */
444
445 #define fill(MEMBER, REGNO)                                             \
446   if (! valid || valid[(REGNO)])                                        \
447     fpregsetp->MEMBER                                                   \
448       = ((fpregsetp->MEMBER & ~0xffff)                                  \
449          | (* (int *) &registers[REGISTER_BYTE (REGNO)] & 0xffff))
450
451 #define fill_register(MEMBER, REGNO)                                    \
452   if (! valid || valid[(REGNO)])                                        \
453     memcpy (&fpregsetp->MEMBER, &registers[REGISTER_BYTE (REGNO)],      \
454             sizeof (fpregsetp->MEMBER))
455
456   fill (cwd, FCTRL_REGNUM);
457   fill (swd, FSTAT_REGNUM);
458   fill (twd, FTAG_REGNUM);
459   fill_register (fip, FCOFF_REGNUM);
460   fill (foo, FDOFF_REGNUM);
461   fill_register (fos, FDS_REGNUM);
462
463 #undef fill
464 #undef fill_register
465
466   if (! valid || valid[FCS_REGNUM])
467     fpregsetp->fcs
468       = ((fpregsetp->fcs & ~0xffff)
469          | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
470
471   if (! valid || valid[FOP_REGNUM])
472     fpregsetp->fcs
473       = ((fpregsetp->fcs & 0xffff)
474          | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
475             << 16));
476 }
477
478 /* Fill register REGNO (if it is a floating-point register) in
479    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
480    do this for all registers.  */
481
482 void
483 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
484 {
485   if (regno == -1)
486     {
487       convert_to_fpregset (fpregsetp, NULL);
488       return;
489     }
490
491   if (GETFPREGS_SUPPLIES(regno))
492     {
493       signed char valid[MAX_NUM_REGS];
494       
495       memset (valid, 0, sizeof (valid));
496       valid[regno] = 1;
497               
498       convert_to_fpregset (fpregsetp, valid);
499     }
500 }
501
502 #ifdef HAVE_PTRACE_GETREGS
503
504 /* Fetch all floating-point registers from process/thread TID and store
505    thier values in GDB's register array.  */
506
507 static void
508 fetch_fpregs (int tid)
509 {
510   elf_fpregset_t fpregs;
511   int ret;
512
513   ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
514   if (ret < 0)
515     {
516       warning ("Couldn't get floating point status.");
517       return;
518     }
519
520   supply_fpregset (&fpregs);
521 }
522
523 /* Store all valid floating-point registers in GDB's register array
524    into the process/thread specified by TID.  */
525
526 static void
527 store_fpregs (int tid)
528 {
529   elf_fpregset_t fpregs;
530   int ret;
531
532   ret = ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs);
533   if (ret < 0)
534     {
535       warning ("Couldn't get floating point status.");
536       return;
537     }
538
539   convert_to_fpregset (&fpregs, register_valid);
540
541   ret = ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs);
542   if (ret < 0)
543     {
544       warning ("Couldn't write floating point status.");
545       return;
546     }
547 }
548
549 #else
550
551 static void fetch_fpregs (int tid) {}
552 static void store_fpregs (int tid) {}
553
554 #endif
555
556 \f
557 /* Transfering floating-point and SSE registers to and from GDB.  */
558
559 /* PTRACE_GETXFPREGS is a Cygnus invention, since we wrote our own
560    Linux kernel patch for SSE support.  That patch may or may not
561    actually make it into the official distribution.  If you find that
562    years have gone by since this code was added, and Linux isn't using
563    PTRACE_GETXFPREGS, that means that our patch didn't make it, and
564    you can delete this code.  */
565
566 #ifdef HAVE_PTRACE_GETXFPREGS
567
568 /* Fill GDB's register array with the floating-point and SSE register
569    values in *XFPREGS.  */
570
571 static void
572 supply_xfpregset (struct user_xfpregs_struct *xfpregs)
573 {
574   int reg;
575
576   /* Supply the floating-point registers.  */
577   for (reg = 0; reg < 8; reg++)
578     supply_register (FP0_REGNUM + reg, (char *) &xfpregs->st_space[reg]);
579
580   {
581     supply_register (FCTRL_REGNUM, (char *) &xfpregs->cwd);
582     supply_register (FSTAT_REGNUM, (char *) &xfpregs->swd);
583     supply_register (FTAG_REGNUM,  (char *) &xfpregs->twd);
584     supply_register (FCOFF_REGNUM, (char *) &xfpregs->fip);
585     supply_register (FDS_REGNUM,   (char *) &xfpregs->fos);
586     supply_register (FDOFF_REGNUM, (char *) &xfpregs->foo);
587   
588     /* Extract the code segment and opcode from the  "fcs" member.  */
589     {
590       long l;
591       
592       l = xfpregs->fcs & 0xffff;
593       supply_register (FCS_REGNUM, (char *) &l);
594
595       l = (xfpregs->fcs >> 16) & ((1 << 11) - 1);
596       supply_register (FOP_REGNUM, (char *) &l);
597     }
598   }
599
600   /* Supply the SSE registers.  */
601   for (reg = 0; reg < 8; reg++)
602     supply_register (XMM0_REGNUM + reg, (char *) &xfpregs->xmm_space[reg]);
603   supply_register (MXCSR_REGNUM, (char *) &xfpregs->mxcsr);
604 }
605
606 /* Convert the valid floating-point and SSE registers in GDB's
607    register array to `struct user' format and store them in *XFPREGS.
608    The array VALID indicates which registers are valid.  If VALID is
609    NULL, all registers are assumed to be valid.  */
610
611 static void
612 convert_to_xfpregset (struct user_xfpregs_struct *xfpregs,
613                       signed char *valid)
614 {
615   int reg;
616
617   /* Fill in the floating-point registers.  */
618   for (reg = 0; reg < 8; reg++)
619     if (!valid || valid[reg])
620       memcpy (&xfpregs->st_space[reg],
621               &registers[REGISTER_BYTE (FP0_REGNUM + reg)],
622               REGISTER_RAW_SIZE(FP0_REGNUM + reg));
623
624 #define fill(MEMBER, REGNO)                                             \
625   if (! valid || valid[(REGNO)])                                        \
626     memcpy (&xfpregs->MEMBER, &registers[REGISTER_BYTE (REGNO)],        \
627             sizeof (xfpregs->MEMBER))
628
629   fill (cwd, FCTRL_REGNUM);
630   fill (swd, FSTAT_REGNUM);
631   fill (twd, FTAG_REGNUM);
632   fill (fip, FCOFF_REGNUM);
633   fill (foo, FDOFF_REGNUM);
634   fill (fos, FDS_REGNUM);
635
636 #undef fill
637
638   if (! valid || valid[FCS_REGNUM])
639     xfpregs->fcs
640       = ((xfpregs->fcs & ~0xffff)
641          | (* (int *) &registers[REGISTER_BYTE (FCS_REGNUM)] & 0xffff));
642
643   if (! valid || valid[FOP_REGNUM])
644     xfpregs->fcs
645       = ((xfpregs->fcs & 0xffff)
646          | ((*(int *) &registers[REGISTER_BYTE (FOP_REGNUM)] & ((1 << 11) - 1))
647             << 16));
648
649   /* Fill in the XMM registers.  */
650   for (reg = 0; reg < 8; reg++)
651     if (! valid || valid[reg])
652       memcpy (&xfpregs->xmm_space[reg],
653               &registers[REGISTER_BYTE (XMM0_REGNUM + reg)],
654               REGISTER_RAW_SIZE (XMM0_REGNUM + reg));
655 }
656
657 /* Fetch all registers covered by the PTRACE_SETXFPREGS request from
658    process/thread TID and store their values in GDB's register array.
659    Return non-zero if successful, zero otherwise.  */
660
661 static int
662 fetch_xfpregs (int tid)
663 {
664   struct user_xfpregs_struct xfpregs;
665   int ret;
666
667   if (! have_ptrace_getxfpregs) 
668     return 0;
669
670   ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
671   if (ret == -1)
672     {
673       if (errno == EIO)
674         {
675           have_ptrace_getxfpregs = 0;
676           return 0;
677         }
678
679       warning ("Couldn't read floating-point and SSE registers.");
680       return 0;
681     }
682
683   supply_xfpregset (&xfpregs);
684   return 1;
685 }
686
687 /* Store all valid registers in GDB's register array covered by the
688    PTRACE_SETXFPREGS request into the process/thread specified by TID.
689    Return non-zero if successful, zero otherwise.  */
690
691 static int
692 store_xfpregs (int tid)
693 {
694   struct user_xfpregs_struct xfpregs;
695   int ret;
696
697   if (! have_ptrace_getxfpregs)
698     return 0;
699
700   ret = ptrace (PTRACE_GETXFPREGS, tid, 0, &xfpregs);
701   if (ret == -1)
702     {
703       if (errno == EIO)
704         {
705           have_ptrace_getxfpregs = 0;
706           return 0;
707         }
708
709       warning ("Couldn't read floating-point and SSE registers.");
710       return 0;
711     }
712
713   convert_to_xfpregset (&xfpregs, register_valid);
714
715   if (ptrace (PTRACE_SETXFPREGS, tid, 0, &xfpregs) < 0)
716     {
717       warning ("Couldn't write floating-point and SSE registers.");
718       return 0;
719     }
720
721   return 1;
722 }
723
724 /* Fill the XMM registers in the register array with dummy values.  For
725    cases where we don't have access to the XMM registers.  I think
726    this is cleaner than printing a warning.  For a cleaner solution,
727    we should gdbarchify the i386 family.  */
728
729 static void
730 dummy_sse_values (void)
731 {
732   /* C doesn't have a syntax for NaN's, so write it out as an array of
733      longs.  */
734   static long dummy[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff };
735   static long mxcsr = 0x1f80;
736   int reg;
737
738   for (reg = 0; reg < 8; reg++)
739     supply_register (XMM0_REGNUM + reg, (char *) dummy);
740   supply_register (MXCSR_REGNUM, (char *) &mxcsr);
741 }
742
743 #else
744
745 /* Stub versions of the above routines, for systems that don't have
746    PTRACE_GETXFPREGS.  */
747 static int store_xfpregs (int tid) { return 0; }
748 static int fetch_xfpregs (int tid) { return 0; }
749 static void dummy_sse_values (void) {}
750
751 #endif
752
753 \f
754 /* Transferring arbitrary registers between GDB and inferior.  */
755
756 /* Fetch register REGNO from the child process.  If REGNO is -1, do
757    this for all registers (including the floating point and SSE
758    registers).  */
759
760 void
761 fetch_inferior_registers (int regno)
762 {
763   int tid;
764
765   /* Use the old method of peeking around in `struct user' if the
766      GETREGS request isn't available.  */
767   if (! have_ptrace_getregs)
768     {
769       old_fetch_inferior_registers (regno);
770       return;
771     }
772
773   /* Linux LWP ID's are process ID's.  */
774   if ((tid = TIDGET (inferior_pid)) == 0)
775     tid = inferior_pid;         /* Not a threaded program.  */
776
777   /* Use the PTRACE_GETXFPREGS request whenever possible, since it
778      transfers more registers in one system call, and we'll cache the
779      results.  But remember that fetch_xfpregs can fail, and return
780      zero.  */
781   if (regno == -1)
782     {
783       fetch_regs (tid);
784
785       /* The call above might reset `have_ptrace_getregs'.  */
786       if (! have_ptrace_getregs)
787         {
788           old_fetch_inferior_registers (-1);
789           return;
790         }
791
792       if (fetch_xfpregs (tid))
793         return;
794       fetch_fpregs (tid);
795       return;
796     }
797
798   if (GETREGS_SUPPLIES (regno))
799     {
800       fetch_regs (tid);
801       return;
802     }
803
804   if (GETXFPREGS_SUPPLIES (regno))
805     {
806       if (fetch_xfpregs (tid))
807         return;
808
809       /* Either our processor or our kernel doesn't support the SSE
810          registers, so read the FP registers in the traditional way,
811          and fill the SSE registers with dummy values.  It would be
812          more graceful to handle differences in the register set using
813          gdbarch.  Until then, this will at least make things work
814          plausibly.  */
815       fetch_fpregs (tid);
816       dummy_sse_values ();
817       return;
818     }
819
820   internal_error ("i386-linux-nat.c (fetch_inferior_registers): "
821                   "got request for bad register number %d", regno);
822 }
823
824 /* Store register REGNO back into the child process.  If REGNO is -1,
825    do this for all registers (including the floating point and SSE
826    registers).  */
827 void
828 store_inferior_registers (int regno)
829 {
830   int tid;
831
832   /* Use the old method of poking around in `struct user' if the
833      SETREGS request isn't available.  */
834   if (! have_ptrace_getregs)
835     {
836       old_store_inferior_registers (regno);
837       return;
838     }
839
840   /* Linux LWP ID's are process ID's.  */
841   if ((tid = TIDGET (inferior_pid)) == 0)
842     tid = inferior_pid;         /* Not a threaded program.  */
843
844   /* Use the PTRACE_SETXFPREGS requests whenever possibl, since it
845      transfers more registers in one system call.  But remember that
846      store_xfpregs can fail, and return zero.  */
847   if (regno == -1)
848     {
849       store_regs (tid);
850       if (store_xfpregs (tid))
851         return;
852       store_fpregs (tid);
853       return;
854     }
855
856   if (GETREGS_SUPPLIES (regno))
857     {
858       store_regs (tid);
859       return;
860     }
861
862   if (GETXFPREGS_SUPPLIES (regno))
863     {
864       if (store_xfpregs (tid))
865         return;
866
867       /* Either our processor or our kernel doesn't support the SSE
868          registers, so just write the FP registers in the traditional
869          way.  */
870       store_fpregs (tid);
871       return;
872     }
873
874   internal_error ("Got request to store bad register number %d.", regno);
875 }
876
877 \f
878 /* Interpreting register set info found in core files.  */
879
880 /* Provide registers to GDB from a core file.
881
882    (We can't use the generic version of this function in
883    core-regset.c, because Linux has *three* different kinds of
884    register set notes.  core-regset.c would have to call
885    supply_xfpregset, which most platforms don't have.)
886
887    CORE_REG_SECT points to an array of bytes, which are the contents
888    of a `note' from a core file which BFD thinks might contain
889    register contents.  CORE_REG_SIZE is its size.
890
891    WHICH says which register set corelow suspects this is:
892      0 --- the general-purpose register set, in elf_gregset_t format
893      2 --- the floating-point register set, in elf_fpregset_t format
894      3 --- the extended floating-point register set, in struct
895            user_xfpregs_struct format
896
897    REG_ADDR isn't used on Linux.  */
898
899 static void
900 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
901                       int which, CORE_ADDR reg_addr)
902 {
903   elf_gregset_t gregset;
904   elf_fpregset_t fpregset;
905
906   switch (which)
907     {
908     case 0:
909       if (core_reg_size != sizeof (gregset))
910         warning ("Wrong size gregset in core file.");
911       else
912         {
913           memcpy (&gregset, core_reg_sect, sizeof (gregset));
914           supply_gregset (&gregset);
915         }
916       break;
917
918     case 2:
919       if (core_reg_size != sizeof (fpregset))
920         warning ("Wrong size fpregset in core file.");
921       else
922         {
923           memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
924           supply_fpregset (&fpregset);
925         }
926       break;
927
928 #ifdef HAVE_PTRACE_GETXFPREGS
929       {
930         struct user_xfpregs_struct xfpregset;
931
932       case 3:
933         if (core_reg_size != sizeof (xfpregset))
934           warning ("Wrong size user_xfpregs_struct in core file.");
935         else
936           {
937             memcpy (&xfpregset, core_reg_sect, sizeof (xfpregset));
938             supply_xfpregset (&xfpregset);
939           }
940         break;
941       }
942 #endif
943
944     default:
945       /* We've covered all the kinds of registers we know about here,
946          so this must be something we wouldn't know what to do with
947          anyway.  Just ignore it.  */
948       break;
949     }
950 }
951
952 \f
953 /* Calling functions in shared libraries.  */
954 /* FIXME: kettenis/2000-03-05: Doesn't this belong in a
955    target-dependent file?  The function
956    `i386_linux_skip_solib_resolver' is mentioned in
957    `config/i386/tm-linux.h'.  */
958
959 /* Find the minimal symbol named NAME, and return both the minsym
960    struct and its objfile.  This probably ought to be in minsym.c, but
961    everything there is trying to deal with things like C++ and
962    SOFUN_ADDRESS_MAYBE_TURQUOISE, ...  Since this is so simple, it may
963    be considered too special-purpose for general consumption.  */
964
965 static struct minimal_symbol *
966 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
967 {
968   struct objfile *objfile;
969
970   ALL_OBJFILES (objfile)
971     {
972       struct minimal_symbol *msym;
973
974       ALL_OBJFILE_MSYMBOLS (objfile, msym)
975         {
976           if (SYMBOL_NAME (msym)
977               && STREQ (SYMBOL_NAME (msym), name))
978             {
979               *objfile_p = objfile;
980               return msym;
981             }
982         }
983     }
984
985   return 0;
986 }
987
988
989 static CORE_ADDR
990 skip_hurd_resolver (CORE_ADDR pc)
991 {
992   /* The HURD dynamic linker is part of the GNU C library, so many
993      GNU/Linux distributions use it.  (All ELF versions, as far as I
994      know.)  An unresolved PLT entry points to "_dl_runtime_resolve",
995      which calls "fixup" to patch the PLT, and then passes control to
996      the function.
997
998      We look for the symbol `_dl_runtime_resolve', and find `fixup' in
999      the same objfile.  If we are at the entry point of `fixup', then
1000      we set a breakpoint at the return address (at the top of the
1001      stack), and continue.
1002   
1003      It's kind of gross to do all these checks every time we're
1004      called, since they don't change once the executable has gotten
1005      started.  But this is only a temporary hack --- upcoming versions
1006      of Linux will provide a portable, efficient interface for
1007      debugging programs that use shared libraries.  */
1008
1009   struct objfile *objfile;
1010   struct minimal_symbol *resolver 
1011     = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
1012
1013   if (resolver)
1014     {
1015       struct minimal_symbol *fixup
1016         = lookup_minimal_symbol ("fixup", 0, objfile);
1017
1018       if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
1019         return (SAVED_PC_AFTER_CALL (get_current_frame ()));
1020     }
1021
1022   return 0;
1023 }      
1024
1025 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
1026    This function:
1027    1) decides whether a PLT has sent us into the linker to resolve
1028       a function reference, and 
1029    2) if so, tells us where to set a temporary breakpoint that will
1030       trigger when the dynamic linker is done.  */
1031
1032 CORE_ADDR
1033 i386_linux_skip_solib_resolver (CORE_ADDR pc)
1034 {
1035   CORE_ADDR result;
1036
1037   /* Plug in functions for other kinds of resolvers here.  */
1038   result = skip_hurd_resolver (pc);
1039   if (result)
1040     return result;
1041
1042   return 0;
1043 }
1044
1045 \f
1046 /* Register that we are able to handle Linux ELF core file formats.  */
1047
1048 static struct core_fns linux_elf_core_fns =
1049 {
1050   bfd_target_elf_flavour,               /* core_flavour */
1051   default_check_format,                 /* check_format */
1052   default_core_sniffer,                 /* core_sniffer */
1053   fetch_core_registers,                 /* core_read_registers */
1054   NULL                                  /* next */
1055 };
1056
1057 void
1058 _initialize_i386_linux_nat ()
1059 {
1060   add_core_fns (&linux_elf_core_fns);
1061 }