* objfiles.h (pc_in_section): New prototype.
[platform/upstream/binutils.git] / gdb / mips-linux-tdep.c
1 /* Target-dependent code for GNU/Linux on MIPS processors.
2
3    Copyright (C) 2001-2013 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "gdbcore.h"
22 #include "target.h"
23 #include "solib-svr4.h"
24 #include "osabi.h"
25 #include "mips-tdep.h"
26 #include "gdb_string.h"
27 #include "gdb_assert.h"
28 #include "frame.h"
29 #include "regcache.h"
30 #include "trad-frame.h"
31 #include "tramp-frame.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "solib.h"
35 #include "solib-svr4.h"
36 #include "solist.h"
37 #include "symtab.h"
38 #include "target-descriptions.h"
39 #include "regset.h"
40 #include "mips-linux-tdep.h"
41 #include "glibc-tdep.h"
42 #include "linux-tdep.h"
43 #include "xml-syscall.h"
44 #include "gdb_signals.h"
45
46 static struct target_so_ops mips_svr4_so_ops;
47
48 /* Figure out where the longjmp will land.
49    We expect the first arg to be a pointer to the jmp_buf structure
50    from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
51    at.  The pc is copied into PC.  This routine returns 1 on
52    success.  */
53
54 #define MIPS_LINUX_JB_ELEMENT_SIZE 4
55 #define MIPS_LINUX_JB_PC 0
56
57 static int
58 mips_linux_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
59 {
60   CORE_ADDR jb_addr;
61   struct gdbarch *gdbarch = get_frame_arch (frame);
62   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
63   gdb_byte buf[gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT];
64
65   jb_addr = get_frame_register_unsigned (frame, MIPS_A0_REGNUM);
66
67   if (target_read_memory ((jb_addr
68                            + MIPS_LINUX_JB_PC * MIPS_LINUX_JB_ELEMENT_SIZE),
69                           buf, gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
70     return 0;
71
72   *pc = extract_unsigned_integer (buf,
73                                   gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT,
74                                   byte_order);
75
76   return 1;
77 }
78
79 /* Transform the bits comprising a 32-bit register to the right size
80    for regcache_raw_supply().  This is needed when mips_isa_regsize()
81    is 8.  */
82
83 static void
84 supply_32bit_reg (struct regcache *regcache, int regnum, const void *addr)
85 {
86   struct gdbarch *gdbarch = get_regcache_arch (regcache);
87   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
88   gdb_byte buf[MAX_REGISTER_SIZE];
89   store_signed_integer (buf, register_size (gdbarch, regnum), byte_order,
90                         extract_signed_integer (addr, 4, byte_order));
91   regcache_raw_supply (regcache, regnum, buf);
92 }
93
94 /* Unpack an elf_gregset_t into GDB's register cache.  */
95
96 void
97 mips_supply_gregset (struct regcache *regcache,
98                      const mips_elf_gregset_t *gregsetp)
99 {
100   int regi;
101   const mips_elf_greg_t *regp = *gregsetp;
102   char zerobuf[MAX_REGISTER_SIZE];
103   struct gdbarch *gdbarch = get_regcache_arch (regcache);
104
105   memset (zerobuf, 0, MAX_REGISTER_SIZE);
106
107   for (regi = EF_REG0 + 1; regi <= EF_REG31; regi++)
108     supply_32bit_reg (regcache, regi - EF_REG0, regp + regi);
109
110   if (mips_linux_restart_reg_p (gdbarch))
111     supply_32bit_reg (regcache, MIPS_RESTART_REGNUM, regp + EF_REG0);
112
113   supply_32bit_reg (regcache, mips_regnum (gdbarch)->lo, regp + EF_LO);
114   supply_32bit_reg (regcache, mips_regnum (gdbarch)->hi, regp + EF_HI);
115
116   supply_32bit_reg (regcache, mips_regnum (gdbarch)->pc,
117                     regp + EF_CP0_EPC);
118   supply_32bit_reg (regcache, mips_regnum (gdbarch)->badvaddr,
119                     regp + EF_CP0_BADVADDR);
120   supply_32bit_reg (regcache, MIPS_PS_REGNUM, regp + EF_CP0_STATUS);
121   supply_32bit_reg (regcache, mips_regnum (gdbarch)->cause,
122                     regp + EF_CP0_CAUSE);
123
124   /* Fill the inaccessible zero register with zero.  */
125   regcache_raw_supply (regcache, MIPS_ZERO_REGNUM, zerobuf);
126 }
127
128 static void
129 mips_supply_gregset_wrapper (const struct regset *regset,
130                              struct regcache *regcache,
131                              int regnum, const void *gregs, size_t len)
132 {
133   gdb_assert (len == sizeof (mips_elf_gregset_t));
134
135   mips_supply_gregset (regcache, (const mips_elf_gregset_t *)gregs);
136 }
137
138 /* Pack our registers (or one register) into an elf_gregset_t.  */
139
140 void
141 mips_fill_gregset (const struct regcache *regcache,
142                    mips_elf_gregset_t *gregsetp, int regno)
143 {
144   struct gdbarch *gdbarch = get_regcache_arch (regcache);
145   int regaddr, regi;
146   mips_elf_greg_t *regp = *gregsetp;
147   void *dst;
148
149   if (regno == -1)
150     {
151       memset (regp, 0, sizeof (mips_elf_gregset_t));
152       for (regi = 1; regi < 32; regi++)
153         mips_fill_gregset (regcache, gregsetp, regi);
154       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->lo);
155       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->hi);
156       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->pc);
157       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->badvaddr);
158       mips_fill_gregset (regcache, gregsetp, MIPS_PS_REGNUM);
159       mips_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->cause);
160       mips_fill_gregset (regcache, gregsetp, MIPS_RESTART_REGNUM);
161       return;
162    }
163
164   if (regno > 0 && regno < 32)
165     {
166       dst = regp + regno + EF_REG0;
167       regcache_raw_collect (regcache, regno, dst);
168       return;
169     }
170
171   if (regno == mips_regnum (gdbarch)->lo)
172      regaddr = EF_LO;
173   else if (regno == mips_regnum (gdbarch)->hi)
174     regaddr = EF_HI;
175   else if (regno == mips_regnum (gdbarch)->pc)
176     regaddr = EF_CP0_EPC;
177   else if (regno == mips_regnum (gdbarch)->badvaddr)
178     regaddr = EF_CP0_BADVADDR;
179   else if (regno == MIPS_PS_REGNUM)
180     regaddr = EF_CP0_STATUS;
181   else if (regno == mips_regnum (gdbarch)->cause)
182     regaddr = EF_CP0_CAUSE;
183   else if (mips_linux_restart_reg_p (gdbarch)
184            && regno == MIPS_RESTART_REGNUM)
185     regaddr = EF_REG0;
186   else
187     regaddr = -1;
188
189   if (regaddr != -1)
190     {
191       dst = regp + regaddr;
192       regcache_raw_collect (regcache, regno, dst);
193     }
194 }
195
196 static void
197 mips_fill_gregset_wrapper (const struct regset *regset,
198                            const struct regcache *regcache,
199                            int regnum, void *gregs, size_t len)
200 {
201   gdb_assert (len == sizeof (mips_elf_gregset_t));
202
203   mips_fill_gregset (regcache, (mips_elf_gregset_t *)gregs, regnum);
204 }
205
206 /* Likewise, unpack an elf_fpregset_t.  */
207
208 void
209 mips_supply_fpregset (struct regcache *regcache,
210                       const mips_elf_fpregset_t *fpregsetp)
211 {
212   struct gdbarch *gdbarch = get_regcache_arch (regcache);
213   int regi;
214   char zerobuf[MAX_REGISTER_SIZE];
215
216   memset (zerobuf, 0, MAX_REGISTER_SIZE);
217
218   for (regi = 0; regi < 32; regi++)
219     regcache_raw_supply (regcache,
220                          gdbarch_fp0_regnum (gdbarch) + regi,
221                          *fpregsetp + regi);
222
223   regcache_raw_supply (regcache,
224                        mips_regnum (gdbarch)->fp_control_status,
225                        *fpregsetp + 32);
226
227   /* FIXME: how can we supply FCRIR?  The ABI doesn't tell us.  */
228   regcache_raw_supply (regcache,
229                        mips_regnum (gdbarch)->fp_implementation_revision,
230                        zerobuf);
231 }
232
233 static void
234 mips_supply_fpregset_wrapper (const struct regset *regset,
235                               struct regcache *regcache,
236                               int regnum, const void *gregs, size_t len)
237 {
238   gdb_assert (len == sizeof (mips_elf_fpregset_t));
239
240   mips_supply_fpregset (regcache, (const mips_elf_fpregset_t *)gregs);
241 }
242
243 /* Likewise, pack one or all floating point registers into an
244    elf_fpregset_t.  */
245
246 void
247 mips_fill_fpregset (const struct regcache *regcache,
248                     mips_elf_fpregset_t *fpregsetp, int regno)
249 {
250   struct gdbarch *gdbarch = get_regcache_arch (regcache);
251   char *to;
252
253   if ((regno >= gdbarch_fp0_regnum (gdbarch))
254       && (regno < gdbarch_fp0_regnum (gdbarch) + 32))
255     {
256       to = (char *) (*fpregsetp + regno - gdbarch_fp0_regnum (gdbarch));
257       regcache_raw_collect (regcache, regno, to);
258     }
259   else if (regno == mips_regnum (gdbarch)->fp_control_status)
260     {
261       to = (char *) (*fpregsetp + 32);
262       regcache_raw_collect (regcache, regno, to);
263     }
264   else if (regno == -1)
265     {
266       int regi;
267
268       for (regi = 0; regi < 32; regi++)
269         mips_fill_fpregset (regcache, fpregsetp,
270                             gdbarch_fp0_regnum (gdbarch) + regi);
271       mips_fill_fpregset (regcache, fpregsetp,
272                           mips_regnum (gdbarch)->fp_control_status);
273     }
274 }
275
276 static void
277 mips_fill_fpregset_wrapper (const struct regset *regset,
278                             const struct regcache *regcache,
279                             int regnum, void *gregs, size_t len)
280 {
281   gdb_assert (len == sizeof (mips_elf_fpregset_t));
282
283   mips_fill_fpregset (regcache, (mips_elf_fpregset_t *)gregs, regnum);
284 }
285
286 /* Support for 64-bit ABIs.  */
287
288 /* Figure out where the longjmp will land.
289    We expect the first arg to be a pointer to the jmp_buf structure
290    from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
291    at.  The pc is copied into PC.  This routine returns 1 on
292    success.  */
293
294 /* Details about jmp_buf.  */
295
296 #define MIPS64_LINUX_JB_PC 0
297
298 static int
299 mips64_linux_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
300 {
301   CORE_ADDR jb_addr;
302   struct gdbarch *gdbarch = get_frame_arch (frame);
303   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
304   void *buf = alloca (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
305   int element_size = gdbarch_ptr_bit (gdbarch) == 32 ? 4 : 8;
306
307   jb_addr = get_frame_register_unsigned (frame, MIPS_A0_REGNUM);
308
309   if (target_read_memory (jb_addr + MIPS64_LINUX_JB_PC * element_size,
310                           buf,
311                           gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
312     return 0;
313
314   *pc = extract_unsigned_integer (buf,
315                                   gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT,
316                                   byte_order);
317
318   return 1;
319 }
320
321 /* Register set support functions.  These operate on standard 64-bit
322    regsets, but work whether the target is 32-bit or 64-bit.  A 32-bit
323    target will still use the 64-bit format for PTRACE_GETREGS.  */
324
325 /* Supply a 64-bit register.  */
326
327 static void
328 supply_64bit_reg (struct regcache *regcache, int regnum,
329                   const gdb_byte *buf)
330 {
331   struct gdbarch *gdbarch = get_regcache_arch (regcache);
332   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
333       && register_size (gdbarch, regnum) == 4)
334     regcache_raw_supply (regcache, regnum, buf + 4);
335   else
336     regcache_raw_supply (regcache, regnum, buf);
337 }
338
339 /* Unpack a 64-bit elf_gregset_t into GDB's register cache.  */
340
341 void
342 mips64_supply_gregset (struct regcache *regcache,
343                        const mips64_elf_gregset_t *gregsetp)
344 {
345   int regi;
346   const mips64_elf_greg_t *regp = *gregsetp;
347   gdb_byte zerobuf[MAX_REGISTER_SIZE];
348   struct gdbarch *gdbarch = get_regcache_arch (regcache);
349
350   memset (zerobuf, 0, MAX_REGISTER_SIZE);
351
352   for (regi = MIPS64_EF_REG0 + 1; regi <= MIPS64_EF_REG31; regi++)
353     supply_64bit_reg (regcache, regi - MIPS64_EF_REG0,
354                       (const gdb_byte *) (regp + regi));
355
356   if (mips_linux_restart_reg_p (gdbarch))
357     supply_64bit_reg (regcache, MIPS_RESTART_REGNUM,
358                       (const gdb_byte *) (regp + MIPS64_EF_REG0));
359
360   supply_64bit_reg (regcache, mips_regnum (gdbarch)->lo,
361                     (const gdb_byte *) (regp + MIPS64_EF_LO));
362   supply_64bit_reg (regcache, mips_regnum (gdbarch)->hi,
363                     (const gdb_byte *) (regp + MIPS64_EF_HI));
364
365   supply_64bit_reg (regcache, mips_regnum (gdbarch)->pc,
366                     (const gdb_byte *) (regp + MIPS64_EF_CP0_EPC));
367   supply_64bit_reg (regcache, mips_regnum (gdbarch)->badvaddr,
368                     (const gdb_byte *) (regp + MIPS64_EF_CP0_BADVADDR));
369   supply_64bit_reg (regcache, MIPS_PS_REGNUM,
370                     (const gdb_byte *) (regp + MIPS64_EF_CP0_STATUS));
371   supply_64bit_reg (regcache, mips_regnum (gdbarch)->cause,
372                     (const gdb_byte *) (regp + MIPS64_EF_CP0_CAUSE));
373
374   /* Fill the inaccessible zero register with zero.  */
375   regcache_raw_supply (regcache, MIPS_ZERO_REGNUM, zerobuf);
376 }
377
378 static void
379 mips64_supply_gregset_wrapper (const struct regset *regset,
380                                struct regcache *regcache,
381                                int regnum, const void *gregs, size_t len)
382 {
383   gdb_assert (len == sizeof (mips64_elf_gregset_t));
384
385   mips64_supply_gregset (regcache, (const mips64_elf_gregset_t *)gregs);
386 }
387
388 /* Pack our registers (or one register) into a 64-bit elf_gregset_t.  */
389
390 void
391 mips64_fill_gregset (const struct regcache *regcache,
392                      mips64_elf_gregset_t *gregsetp, int regno)
393 {
394   struct gdbarch *gdbarch = get_regcache_arch (regcache);
395   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
396   int regaddr, regi;
397   mips64_elf_greg_t *regp = *gregsetp;
398   void *dst;
399
400   if (regno == -1)
401     {
402       memset (regp, 0, sizeof (mips64_elf_gregset_t));
403       for (regi = 1; regi < 32; regi++)
404         mips64_fill_gregset (regcache, gregsetp, regi);
405       mips64_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->lo);
406       mips64_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->hi);
407       mips64_fill_gregset (regcache, gregsetp, mips_regnum (gdbarch)->pc);
408       mips64_fill_gregset (regcache, gregsetp,
409                            mips_regnum (gdbarch)->badvaddr);
410       mips64_fill_gregset (regcache, gregsetp, MIPS_PS_REGNUM);
411       mips64_fill_gregset (regcache, gregsetp,  mips_regnum (gdbarch)->cause);
412       mips64_fill_gregset (regcache, gregsetp, MIPS_RESTART_REGNUM);
413       return;
414    }
415
416   if (regno > 0 && regno < 32)
417     regaddr = regno + MIPS64_EF_REG0;
418   else if (regno == mips_regnum (gdbarch)->lo)
419     regaddr = MIPS64_EF_LO;
420   else if (regno == mips_regnum (gdbarch)->hi)
421     regaddr = MIPS64_EF_HI;
422   else if (regno == mips_regnum (gdbarch)->pc)
423     regaddr = MIPS64_EF_CP0_EPC;
424   else if (regno == mips_regnum (gdbarch)->badvaddr)
425     regaddr = MIPS64_EF_CP0_BADVADDR;
426   else if (regno == MIPS_PS_REGNUM)
427     regaddr = MIPS64_EF_CP0_STATUS;
428   else if (regno == mips_regnum (gdbarch)->cause)
429     regaddr = MIPS64_EF_CP0_CAUSE;
430   else if (mips_linux_restart_reg_p (gdbarch)
431            && regno == MIPS_RESTART_REGNUM)
432     regaddr = MIPS64_EF_REG0;
433   else
434     regaddr = -1;
435
436   if (regaddr != -1)
437     {
438       gdb_byte buf[MAX_REGISTER_SIZE];
439       LONGEST val;
440
441       regcache_raw_collect (regcache, regno, buf);
442       val = extract_signed_integer (buf, register_size (gdbarch, regno),
443                                     byte_order);
444       dst = regp + regaddr;
445       store_signed_integer (dst, 8, byte_order, val);
446     }
447 }
448
449 static void
450 mips64_fill_gregset_wrapper (const struct regset *regset,
451                              const struct regcache *regcache,
452                              int regnum, void *gregs, size_t len)
453 {
454   gdb_assert (len == sizeof (mips64_elf_gregset_t));
455
456   mips64_fill_gregset (regcache, (mips64_elf_gregset_t *)gregs, regnum);
457 }
458
459 /* Likewise, unpack an elf_fpregset_t.  */
460
461 void
462 mips64_supply_fpregset (struct regcache *regcache,
463                         const mips64_elf_fpregset_t *fpregsetp)
464 {
465   struct gdbarch *gdbarch = get_regcache_arch (regcache);
466   int regi;
467
468   /* See mips_linux_o32_sigframe_init for a description of the
469      peculiar FP register layout.  */
470   if (register_size (gdbarch, gdbarch_fp0_regnum (gdbarch)) == 4)
471     for (regi = 0; regi < 32; regi++)
472       {
473         const gdb_byte *reg_ptr
474           = (const gdb_byte *) (*fpregsetp + (regi & ~1));
475         if ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) != (regi & 1))
476           reg_ptr += 4;
477         regcache_raw_supply (regcache,
478                              gdbarch_fp0_regnum (gdbarch) + regi,
479                              reg_ptr);
480       }
481   else
482     for (regi = 0; regi < 32; regi++)
483       regcache_raw_supply (regcache,
484                            gdbarch_fp0_regnum (gdbarch) + regi,
485                            (const char *) (*fpregsetp + regi));
486
487   supply_32bit_reg (regcache, mips_regnum (gdbarch)->fp_control_status,
488                     (const gdb_byte *) (*fpregsetp + 32));
489
490   /* The ABI doesn't tell us how to supply FCRIR, and core dumps don't
491      include it - but the result of PTRACE_GETFPREGS does.  The best we
492      can do is to assume that its value is present.  */
493   supply_32bit_reg (regcache,
494                     mips_regnum (gdbarch)->fp_implementation_revision,
495                     (const gdb_byte *) (*fpregsetp + 32) + 4);
496 }
497
498 static void
499 mips64_supply_fpregset_wrapper (const struct regset *regset,
500                                 struct regcache *regcache,
501                                 int regnum, const void *gregs, size_t len)
502 {
503   gdb_assert (len == sizeof (mips64_elf_fpregset_t));
504
505   mips64_supply_fpregset (regcache, (const mips64_elf_fpregset_t *)gregs);
506 }
507
508 /* Likewise, pack one or all floating point registers into an
509    elf_fpregset_t.  */
510
511 void
512 mips64_fill_fpregset (const struct regcache *regcache,
513                       mips64_elf_fpregset_t *fpregsetp, int regno)
514 {
515   struct gdbarch *gdbarch = get_regcache_arch (regcache);
516   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
517   gdb_byte *to;
518
519   if ((regno >= gdbarch_fp0_regnum (gdbarch))
520       && (regno < gdbarch_fp0_regnum (gdbarch) + 32))
521     {
522       /* See mips_linux_o32_sigframe_init for a description of the
523          peculiar FP register layout.  */
524       if (register_size (gdbarch, regno) == 4)
525         {
526           int regi = regno - gdbarch_fp0_regnum (gdbarch);
527
528           to = (gdb_byte *) (*fpregsetp + (regi & ~1));
529           if ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) != (regi & 1))
530             to += 4;
531           regcache_raw_collect (regcache, regno, to);
532         }
533       else
534         {
535           to = (gdb_byte *) (*fpregsetp + regno
536                              - gdbarch_fp0_regnum (gdbarch));
537           regcache_raw_collect (regcache, regno, to);
538         }
539     }
540   else if (regno == mips_regnum (gdbarch)->fp_control_status)
541     {
542       gdb_byte buf[MAX_REGISTER_SIZE];
543       LONGEST val;
544
545       regcache_raw_collect (regcache, regno, buf);
546       val = extract_signed_integer (buf, register_size (gdbarch, regno),
547                                     byte_order);
548       to = (gdb_byte *) (*fpregsetp + 32);
549       store_signed_integer (to, 4, byte_order, val);
550     }
551   else if (regno == mips_regnum (gdbarch)->fp_implementation_revision)
552     {
553       gdb_byte buf[MAX_REGISTER_SIZE];
554       LONGEST val;
555
556       regcache_raw_collect (regcache, regno, buf);
557       val = extract_signed_integer (buf, register_size (gdbarch, regno),
558                                     byte_order);
559       to = (gdb_byte *) (*fpregsetp + 32) + 4;
560       store_signed_integer (to, 4, byte_order, val);
561     }
562   else if (regno == -1)
563     {
564       int regi;
565
566       for (regi = 0; regi < 32; regi++)
567         mips64_fill_fpregset (regcache, fpregsetp,
568                               gdbarch_fp0_regnum (gdbarch) + regi);
569       mips64_fill_fpregset (regcache, fpregsetp,
570                             mips_regnum (gdbarch)->fp_control_status);
571       mips64_fill_fpregset (regcache, fpregsetp,
572                             mips_regnum (gdbarch)->fp_implementation_revision);
573     }
574 }
575
576 static void
577 mips64_fill_fpregset_wrapper (const struct regset *regset,
578                               const struct regcache *regcache,
579                               int regnum, void *gregs, size_t len)
580 {
581   gdb_assert (len == sizeof (mips64_elf_fpregset_t));
582
583   mips64_fill_fpregset (regcache, (mips64_elf_fpregset_t *)gregs, regnum);
584 }
585
586 static const struct regset *
587 mips_linux_regset_from_core_section (struct gdbarch *gdbarch,
588                                      const char *sect_name, size_t sect_size)
589 {
590   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
591   mips_elf_gregset_t gregset;
592   mips_elf_fpregset_t fpregset;
593   mips64_elf_gregset_t gregset64;
594   mips64_elf_fpregset_t fpregset64;
595
596   if (strcmp (sect_name, ".reg") == 0)
597     {
598       if (sect_size == sizeof (gregset))
599         {
600           if (tdep->gregset == NULL)
601             tdep->gregset = regset_alloc (gdbarch,
602                                           mips_supply_gregset_wrapper,
603                                           mips_fill_gregset_wrapper);
604           return tdep->gregset;
605         }
606       else if (sect_size == sizeof (gregset64))
607         {
608           if (tdep->gregset64 == NULL)
609             tdep->gregset64 = regset_alloc (gdbarch,
610                                             mips64_supply_gregset_wrapper,
611                                             mips64_fill_gregset_wrapper);
612           return tdep->gregset64;
613         }
614       else
615         {
616           warning (_("wrong size gregset struct in core file"));
617         }
618     }
619   else if (strcmp (sect_name, ".reg2") == 0)
620     {
621       if (sect_size == sizeof (fpregset))
622         {
623           if (tdep->fpregset == NULL)
624             tdep->fpregset = regset_alloc (gdbarch,
625                                            mips_supply_fpregset_wrapper,
626                                            mips_fill_fpregset_wrapper);
627           return tdep->fpregset;
628         }
629       else if (sect_size == sizeof (fpregset64))
630         {
631           if (tdep->fpregset64 == NULL)
632             tdep->fpregset64 = regset_alloc (gdbarch,
633                                              mips64_supply_fpregset_wrapper,
634                                              mips64_fill_fpregset_wrapper);
635           return tdep->fpregset64;
636         }
637       else
638         {
639           warning (_("wrong size fpregset struct in core file"));
640         }
641     }
642
643   return NULL;
644 }
645
646 static const struct target_desc *
647 mips_linux_core_read_description (struct gdbarch *gdbarch,
648                                   struct target_ops *target,
649                                   bfd *abfd)
650 {
651   asection *section = bfd_get_section_by_name (abfd, ".reg");
652   if (! section)
653     return NULL;
654
655   switch (bfd_section_size (abfd, section))
656     {
657     case sizeof (mips_elf_gregset_t):
658       return mips_tdesc_gp32;
659
660     case sizeof (mips64_elf_gregset_t):
661       return mips_tdesc_gp64;
662
663     default:
664       return NULL;
665     }
666 }
667
668
669 /* Check the code at PC for a dynamic linker lazy resolution stub.
670    GNU ld for MIPS has put lazy resolution stubs into a ".MIPS.stubs"
671    section uniformly since version 2.15.  If the pc is in that section,
672    then we are in such a stub.  Before that ".stub" was used in 32-bit
673    ELF binaries, however we do not bother checking for that since we
674    have never had and that case should be extremely rare these days.
675    Instead we pattern-match on the code generated by GNU ld.  They look
676    like this:
677
678    lw t9,0x8010(gp)
679    addu t7,ra
680    jalr t9,ra
681    addiu t8,zero,INDEX
682
683    (with the appropriate doubleword instructions for N64).  As any lazy
684    resolution stubs in microMIPS binaries will always be in a
685    ".MIPS.stubs" section we only ever verify standard MIPS patterns. */
686
687 static int
688 mips_linux_in_dynsym_stub (CORE_ADDR pc)
689 {
690   gdb_byte buf[28], *p;
691   ULONGEST insn, insn1;
692   int n64 = (mips_abi (target_gdbarch ()) == MIPS_ABI_N64);
693   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
694
695   if (in_mips_stubs_section (pc))
696     return 1;
697
698   read_memory (pc - 12, buf, 28);
699
700   if (n64)
701     {
702       /* ld t9,0x8010(gp) */
703       insn1 = 0xdf998010;
704     }
705   else
706     {
707       /* lw t9,0x8010(gp) */
708       insn1 = 0x8f998010;
709     }
710
711   p = buf + 12;
712   while (p >= buf)
713     {
714       insn = extract_unsigned_integer (p, 4, byte_order);
715       if (insn == insn1)
716         break;
717       p -= 4;
718     }
719   if (p < buf)
720     return 0;
721
722   insn = extract_unsigned_integer (p + 4, 4, byte_order);
723   if (n64)
724     {
725       /* daddu t7,ra */
726       if (insn != 0x03e0782d)
727         return 0;
728     }
729   else
730     {
731       /* addu t7,ra */
732       if (insn != 0x03e07821)
733         return 0;
734     }
735
736   insn = extract_unsigned_integer (p + 8, 4, byte_order);
737   /* jalr t9,ra */
738   if (insn != 0x0320f809)
739     return 0;
740
741   insn = extract_unsigned_integer (p + 12, 4, byte_order);
742   if (n64)
743     {
744       /* daddiu t8,zero,0 */
745       if ((insn & 0xffff0000) != 0x64180000)
746         return 0;
747     }
748   else
749     {
750       /* addiu t8,zero,0 */
751       if ((insn & 0xffff0000) != 0x24180000)
752         return 0;
753     }
754
755   return 1;
756 }
757
758 /* Return non-zero iff PC belongs to the dynamic linker resolution
759    code, a PLT entry, or a lazy binding stub.  */
760
761 static int
762 mips_linux_in_dynsym_resolve_code (CORE_ADDR pc)
763 {
764   /* Check whether PC is in the dynamic linker.  This also checks
765      whether it is in the .plt section, used by non-PIC executables.  */
766   if (svr4_in_dynsym_resolve_code (pc))
767     return 1;
768
769   /* Likewise for the stubs.  They live in the .MIPS.stubs section these
770      days, so we check if the PC is within, than fall back to a pattern
771      match.  */
772   if (mips_linux_in_dynsym_stub (pc))
773     return 1;
774
775   return 0;
776 }
777
778 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c,
779    and glibc_skip_solib_resolver in glibc-tdep.c.  The normal glibc
780    implementation of this triggers at "fixup" from the same objfile as
781    "_dl_runtime_resolve"; MIPS GNU/Linux can trigger at
782    "__dl_runtime_resolve" directly.  An unresolved lazy binding
783    stub will point to _dl_runtime_resolve, which will first call
784    __dl_runtime_resolve, and then pass control to the resolved
785    function.  */
786
787 static CORE_ADDR
788 mips_linux_skip_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
789 {
790   struct minimal_symbol *resolver;
791
792   resolver = lookup_minimal_symbol ("__dl_runtime_resolve", NULL, NULL);
793
794   if (resolver && SYMBOL_VALUE_ADDRESS (resolver) == pc)
795     return frame_unwind_caller_pc (get_current_frame ());
796
797   return glibc_skip_solib_resolver (gdbarch, pc);
798 }
799
800 /* Signal trampoline support.  There are four supported layouts for a
801    signal frame: o32 sigframe, o32 rt_sigframe, n32 rt_sigframe, and
802    n64 rt_sigframe.  We handle them all independently; not the most
803    efficient way, but simplest.  First, declare all the unwinders.  */
804
805 static void mips_linux_o32_sigframe_init (const struct tramp_frame *self,
806                                           struct frame_info *this_frame,
807                                           struct trad_frame_cache *this_cache,
808                                           CORE_ADDR func);
809
810 static void mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
811                                              struct frame_info *this_frame,
812                                              struct trad_frame_cache *this_cache,
813                                              CORE_ADDR func);
814
815 #define MIPS_NR_LINUX 4000
816 #define MIPS_NR_N64_LINUX 5000
817 #define MIPS_NR_N32_LINUX 6000
818
819 #define MIPS_NR_sigreturn MIPS_NR_LINUX + 119
820 #define MIPS_NR_rt_sigreturn MIPS_NR_LINUX + 193
821 #define MIPS_NR_N64_rt_sigreturn MIPS_NR_N64_LINUX + 211
822 #define MIPS_NR_N32_rt_sigreturn MIPS_NR_N32_LINUX + 211
823
824 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + MIPS_NR_sigreturn
825 #define MIPS_INST_LI_V0_RT_SIGRETURN 0x24020000 + MIPS_NR_rt_sigreturn
826 #define MIPS_INST_LI_V0_N64_RT_SIGRETURN 0x24020000 + MIPS_NR_N64_rt_sigreturn
827 #define MIPS_INST_LI_V0_N32_RT_SIGRETURN 0x24020000 + MIPS_NR_N32_rt_sigreturn
828 #define MIPS_INST_SYSCALL 0x0000000c
829
830 static const struct tramp_frame mips_linux_o32_sigframe = {
831   SIGTRAMP_FRAME,
832   4,
833   {
834     { MIPS_INST_LI_V0_SIGRETURN, -1 },
835     { MIPS_INST_SYSCALL, -1 },
836     { TRAMP_SENTINEL_INSN, -1 }
837   },
838   mips_linux_o32_sigframe_init
839 };
840
841 static const struct tramp_frame mips_linux_o32_rt_sigframe = {
842   SIGTRAMP_FRAME,
843   4,
844   {
845     { MIPS_INST_LI_V0_RT_SIGRETURN, -1 },
846     { MIPS_INST_SYSCALL, -1 },
847     { TRAMP_SENTINEL_INSN, -1 } },
848   mips_linux_o32_sigframe_init
849 };
850
851 static const struct tramp_frame mips_linux_n32_rt_sigframe = {
852   SIGTRAMP_FRAME,
853   4,
854   {
855     { MIPS_INST_LI_V0_N32_RT_SIGRETURN, -1 },
856     { MIPS_INST_SYSCALL, -1 },
857     { TRAMP_SENTINEL_INSN, -1 }
858   },
859   mips_linux_n32n64_sigframe_init
860 };
861
862 static const struct tramp_frame mips_linux_n64_rt_sigframe = {
863   SIGTRAMP_FRAME,
864   4,
865   {
866     { MIPS_INST_LI_V0_N64_RT_SIGRETURN, -1 },
867     { MIPS_INST_SYSCALL, -1 },
868     { TRAMP_SENTINEL_INSN, -1 }
869   },
870   mips_linux_n32n64_sigframe_init
871 };
872
873 /* *INDENT-OFF* */
874 /* The unwinder for o32 signal frames.  The legacy structures look
875    like this:
876
877    struct sigframe {
878      u32 sf_ass[4];            [argument save space for o32]
879      u32 sf_code[2];           [signal trampoline or fill]
880      struct sigcontext sf_sc;
881      sigset_t sf_mask;
882    };
883
884    Pre-2.6.12 sigcontext:
885
886    struct sigcontext {
887         unsigned int       sc_regmask;          [Unused]
888         unsigned int       sc_status;
889         unsigned long long sc_pc;
890         unsigned long long sc_regs[32];
891         unsigned long long sc_fpregs[32];
892         unsigned int       sc_ownedfp;
893         unsigned int       sc_fpc_csr;
894         unsigned int       sc_fpc_eir;          [Unused]
895         unsigned int       sc_used_math;
896         unsigned int       sc_ssflags;          [Unused]
897         [Alignment hole of four bytes]
898         unsigned long long sc_mdhi;
899         unsigned long long sc_mdlo;
900
901         unsigned int       sc_cause;            [Unused]
902         unsigned int       sc_badvaddr;         [Unused]
903
904         unsigned long      sc_sigset[4];        [kernel's sigset_t]
905    };
906
907    Post-2.6.12 sigcontext (SmartMIPS/DSP support added):
908
909    struct sigcontext {
910         unsigned int       sc_regmask;          [Unused]
911         unsigned int       sc_status;           [Unused]
912         unsigned long long sc_pc;
913         unsigned long long sc_regs[32];
914         unsigned long long sc_fpregs[32];
915         unsigned int       sc_acx;
916         unsigned int       sc_fpc_csr;
917         unsigned int       sc_fpc_eir;          [Unused]
918         unsigned int       sc_used_math;
919         unsigned int       sc_dsp;
920         [Alignment hole of four bytes]
921         unsigned long long sc_mdhi;
922         unsigned long long sc_mdlo;
923         unsigned long      sc_hi1;
924         unsigned long      sc_lo1;
925         unsigned long      sc_hi2;
926         unsigned long      sc_lo2;
927         unsigned long      sc_hi3;
928         unsigned long      sc_lo3;
929    };
930
931    The RT signal frames look like this:
932
933    struct rt_sigframe {
934      u32 rs_ass[4];            [argument save space for o32]
935      u32 rs_code[2]            [signal trampoline or fill]
936      struct siginfo rs_info;
937      struct ucontext rs_uc;
938    };
939
940    struct ucontext {
941      unsigned long     uc_flags;
942      struct ucontext  *uc_link;
943      stack_t           uc_stack;
944      [Alignment hole of four bytes]
945      struct sigcontext uc_mcontext;
946      sigset_t          uc_sigmask;
947    };  */
948 /* *INDENT-ON* */
949
950 #define SIGFRAME_SIGCONTEXT_OFFSET   (6 * 4)
951
952 #define RTSIGFRAME_SIGINFO_SIZE      128
953 #define STACK_T_SIZE                 (3 * 4)
954 #define UCONTEXT_SIGCONTEXT_OFFSET   (2 * 4 + STACK_T_SIZE + 4)
955 #define RTSIGFRAME_SIGCONTEXT_OFFSET (SIGFRAME_SIGCONTEXT_OFFSET \
956                                       + RTSIGFRAME_SIGINFO_SIZE \
957                                       + UCONTEXT_SIGCONTEXT_OFFSET)
958
959 #define SIGCONTEXT_PC       (1 * 8)
960 #define SIGCONTEXT_REGS     (2 * 8)
961 #define SIGCONTEXT_FPREGS   (34 * 8)
962 #define SIGCONTEXT_FPCSR    (66 * 8 + 4)
963 #define SIGCONTEXT_DSPCTL   (68 * 8 + 0)
964 #define SIGCONTEXT_HI       (69 * 8)
965 #define SIGCONTEXT_LO       (70 * 8)
966 #define SIGCONTEXT_CAUSE    (71 * 8 + 0)
967 #define SIGCONTEXT_BADVADDR (71 * 8 + 4)
968 #define SIGCONTEXT_HI1      (71 * 8 + 0)
969 #define SIGCONTEXT_LO1      (71 * 8 + 4)
970 #define SIGCONTEXT_HI2      (72 * 8 + 0)
971 #define SIGCONTEXT_LO2      (72 * 8 + 4)
972 #define SIGCONTEXT_HI3      (73 * 8 + 0)
973 #define SIGCONTEXT_LO3      (73 * 8 + 4)
974
975 #define SIGCONTEXT_REG_SIZE 8
976
977 static void
978 mips_linux_o32_sigframe_init (const struct tramp_frame *self,
979                               struct frame_info *this_frame,
980                               struct trad_frame_cache *this_cache,
981                               CORE_ADDR func)
982 {
983   struct gdbarch *gdbarch = get_frame_arch (this_frame);
984   int ireg;
985   CORE_ADDR frame_sp = get_frame_sp (this_frame);
986   CORE_ADDR sigcontext_base;
987   const struct mips_regnum *regs = mips_regnum (gdbarch);
988   CORE_ADDR regs_base;
989
990   if (self == &mips_linux_o32_sigframe)
991     sigcontext_base = frame_sp + SIGFRAME_SIGCONTEXT_OFFSET;
992   else
993     sigcontext_base = frame_sp + RTSIGFRAME_SIGCONTEXT_OFFSET;
994
995   /* I'm not proud of this hack.  Eventually we will have the
996      infrastructure to indicate the size of saved registers on a
997      per-frame basis, but right now we don't; the kernel saves eight
998      bytes but we only want four.  Use regs_base to access any
999      64-bit fields.  */
1000   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1001     regs_base = sigcontext_base + 4;
1002   else
1003     regs_base = sigcontext_base;
1004
1005   if (mips_linux_restart_reg_p (gdbarch))
1006     trad_frame_set_reg_addr (this_cache,
1007                              (MIPS_RESTART_REGNUM
1008                               + gdbarch_num_regs (gdbarch)),
1009                              regs_base + SIGCONTEXT_REGS);
1010
1011   for (ireg = 1; ireg < 32; ireg++)
1012     trad_frame_set_reg_addr (this_cache,
1013                              (ireg + MIPS_ZERO_REGNUM
1014                               + gdbarch_num_regs (gdbarch)),
1015                              (regs_base + SIGCONTEXT_REGS
1016                               + ireg * SIGCONTEXT_REG_SIZE));
1017
1018   /* The way that floating point registers are saved, unfortunately,
1019      depends on the architecture the kernel is built for.  For the r3000 and
1020      tx39, four bytes of each register are at the beginning of each of the
1021      32 eight byte slots.  For everything else, the registers are saved
1022      using double precision; only the even-numbered slots are initialized,
1023      and the high bits are the odd-numbered register.  Assume the latter
1024      layout, since we can't tell, and it's much more common.  Which bits are
1025      the "high" bits depends on endianness.  */
1026   for (ireg = 0; ireg < 32; ireg++)
1027     if ((gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG) != (ireg & 1))
1028       trad_frame_set_reg_addr (this_cache,
1029                                ireg + regs->fp0 + gdbarch_num_regs (gdbarch),
1030                                (sigcontext_base + SIGCONTEXT_FPREGS + 4
1031                                 + (ireg & ~1) * SIGCONTEXT_REG_SIZE));
1032     else
1033       trad_frame_set_reg_addr (this_cache,
1034                                ireg + regs->fp0 + gdbarch_num_regs (gdbarch),
1035                                (sigcontext_base + SIGCONTEXT_FPREGS
1036                                 + (ireg & ~1) * SIGCONTEXT_REG_SIZE));
1037
1038   trad_frame_set_reg_addr (this_cache,
1039                            regs->pc + gdbarch_num_regs (gdbarch),
1040                            regs_base + SIGCONTEXT_PC);
1041
1042   trad_frame_set_reg_addr (this_cache,
1043                            (regs->fp_control_status
1044                             + gdbarch_num_regs (gdbarch)),
1045                            sigcontext_base + SIGCONTEXT_FPCSR);
1046
1047   if (regs->dspctl != -1)
1048     trad_frame_set_reg_addr (this_cache,
1049                              regs->dspctl + gdbarch_num_regs (gdbarch),
1050                              sigcontext_base + SIGCONTEXT_DSPCTL);
1051
1052   trad_frame_set_reg_addr (this_cache,
1053                            regs->hi + gdbarch_num_regs (gdbarch),
1054                            regs_base + SIGCONTEXT_HI);
1055   trad_frame_set_reg_addr (this_cache,
1056                            regs->lo + gdbarch_num_regs (gdbarch),
1057                            regs_base + SIGCONTEXT_LO);
1058
1059   if (regs->dspacc != -1)
1060     {
1061       trad_frame_set_reg_addr (this_cache,
1062                                regs->dspacc + 0 + gdbarch_num_regs (gdbarch),
1063                                sigcontext_base + SIGCONTEXT_HI1);
1064       trad_frame_set_reg_addr (this_cache,
1065                                regs->dspacc + 1 + gdbarch_num_regs (gdbarch),
1066                                sigcontext_base + SIGCONTEXT_LO1);
1067       trad_frame_set_reg_addr (this_cache,
1068                                regs->dspacc + 2 + gdbarch_num_regs (gdbarch),
1069                                sigcontext_base + SIGCONTEXT_HI2);
1070       trad_frame_set_reg_addr (this_cache,
1071                                regs->dspacc + 3 + gdbarch_num_regs (gdbarch),
1072                                sigcontext_base + SIGCONTEXT_LO2);
1073       trad_frame_set_reg_addr (this_cache,
1074                                regs->dspacc + 4 + gdbarch_num_regs (gdbarch),
1075                                sigcontext_base + SIGCONTEXT_HI3);
1076       trad_frame_set_reg_addr (this_cache,
1077                                regs->dspacc + 5 + gdbarch_num_regs (gdbarch),
1078                                sigcontext_base + SIGCONTEXT_LO3);
1079     }
1080   else
1081     {
1082       trad_frame_set_reg_addr (this_cache,
1083                                regs->cause + gdbarch_num_regs (gdbarch),
1084                                sigcontext_base + SIGCONTEXT_CAUSE);
1085       trad_frame_set_reg_addr (this_cache,
1086                                regs->badvaddr + gdbarch_num_regs (gdbarch),
1087                                sigcontext_base + SIGCONTEXT_BADVADDR);
1088     }
1089
1090   /* Choice of the bottom of the sigframe is somewhat arbitrary.  */
1091   trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
1092 }
1093
1094 /* *INDENT-OFF* */
1095 /* For N32/N64 things look different.  There is no non-rt signal frame.
1096
1097   struct rt_sigframe_n32 {
1098     u32 rs_ass[4];                  [ argument save space for o32 ]
1099     u32 rs_code[2];                 [ signal trampoline or fill ]
1100     struct siginfo rs_info;
1101     struct ucontextn32 rs_uc;
1102   };
1103
1104   struct ucontextn32 {
1105     u32                 uc_flags;
1106     s32                 uc_link;
1107     stack32_t           uc_stack;
1108     struct sigcontext   uc_mcontext;
1109     sigset_t            uc_sigmask;   [ mask last for extensibility ]
1110   };
1111
1112   struct rt_sigframe {
1113     u32 rs_ass[4];                  [ argument save space for o32 ]
1114     u32 rs_code[2];                 [ signal trampoline ]
1115     struct siginfo rs_info;
1116     struct ucontext rs_uc;
1117   };
1118
1119   struct ucontext {
1120     unsigned long     uc_flags;
1121     struct ucontext  *uc_link;
1122     stack_t           uc_stack;
1123     struct sigcontext uc_mcontext;
1124     sigset_t          uc_sigmask;   [ mask last for extensibility ]
1125   };
1126
1127   And the sigcontext is different (this is for both n32 and n64):
1128
1129   struct sigcontext {
1130     unsigned long long sc_regs[32];
1131     unsigned long long sc_fpregs[32];
1132     unsigned long long sc_mdhi;
1133     unsigned long long sc_hi1;
1134     unsigned long long sc_hi2;
1135     unsigned long long sc_hi3;
1136     unsigned long long sc_mdlo;
1137     unsigned long long sc_lo1;
1138     unsigned long long sc_lo2;
1139     unsigned long long sc_lo3;
1140     unsigned long long sc_pc;
1141     unsigned int       sc_fpc_csr;
1142     unsigned int       sc_used_math;
1143     unsigned int       sc_dsp;
1144     unsigned int       sc_reserved;
1145   };
1146
1147   That is the post-2.6.12 definition of the 64-bit sigcontext; before
1148   then, there were no hi1-hi3 or lo1-lo3.  Cause and badvaddr were
1149   included too.  */
1150 /* *INDENT-ON* */
1151
1152 #define N32_STACK_T_SIZE                STACK_T_SIZE
1153 #define N64_STACK_T_SIZE                (2 * 8 + 4)
1154 #define N32_UCONTEXT_SIGCONTEXT_OFFSET  (2 * 4 + N32_STACK_T_SIZE + 4)
1155 #define N64_UCONTEXT_SIGCONTEXT_OFFSET  (2 * 8 + N64_STACK_T_SIZE + 4)
1156 #define N32_SIGFRAME_SIGCONTEXT_OFFSET  (SIGFRAME_SIGCONTEXT_OFFSET \
1157                                          + RTSIGFRAME_SIGINFO_SIZE \
1158                                          + N32_UCONTEXT_SIGCONTEXT_OFFSET)
1159 #define N64_SIGFRAME_SIGCONTEXT_OFFSET  (SIGFRAME_SIGCONTEXT_OFFSET \
1160                                          + RTSIGFRAME_SIGINFO_SIZE \
1161                                          + N64_UCONTEXT_SIGCONTEXT_OFFSET)
1162
1163 #define N64_SIGCONTEXT_REGS     (0 * 8)
1164 #define N64_SIGCONTEXT_FPREGS   (32 * 8)
1165 #define N64_SIGCONTEXT_HI       (64 * 8)
1166 #define N64_SIGCONTEXT_HI1      (65 * 8)
1167 #define N64_SIGCONTEXT_HI2      (66 * 8)
1168 #define N64_SIGCONTEXT_HI3      (67 * 8)
1169 #define N64_SIGCONTEXT_LO       (68 * 8)
1170 #define N64_SIGCONTEXT_LO1      (69 * 8)
1171 #define N64_SIGCONTEXT_LO2      (70 * 8)
1172 #define N64_SIGCONTEXT_LO3      (71 * 8)
1173 #define N64_SIGCONTEXT_PC       (72 * 8)
1174 #define N64_SIGCONTEXT_FPCSR    (73 * 8 + 0)
1175 #define N64_SIGCONTEXT_DSPCTL   (74 * 8 + 0)
1176
1177 #define N64_SIGCONTEXT_REG_SIZE 8
1178
1179 static void
1180 mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
1181                                  struct frame_info *this_frame,
1182                                  struct trad_frame_cache *this_cache,
1183                                  CORE_ADDR func)
1184 {
1185   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1186   int ireg;
1187   CORE_ADDR frame_sp = get_frame_sp (this_frame);
1188   CORE_ADDR sigcontext_base;
1189   const struct mips_regnum *regs = mips_regnum (gdbarch);
1190
1191   if (self == &mips_linux_n32_rt_sigframe)
1192     sigcontext_base = frame_sp + N32_SIGFRAME_SIGCONTEXT_OFFSET;
1193   else
1194     sigcontext_base = frame_sp + N64_SIGFRAME_SIGCONTEXT_OFFSET;
1195
1196   if (mips_linux_restart_reg_p (gdbarch))
1197     trad_frame_set_reg_addr (this_cache,
1198                              (MIPS_RESTART_REGNUM
1199                               + gdbarch_num_regs (gdbarch)),
1200                              sigcontext_base + N64_SIGCONTEXT_REGS);
1201
1202   for (ireg = 1; ireg < 32; ireg++)
1203     trad_frame_set_reg_addr (this_cache,
1204                              (ireg + MIPS_ZERO_REGNUM
1205                               + gdbarch_num_regs (gdbarch)),
1206                              (sigcontext_base + N64_SIGCONTEXT_REGS
1207                               + ireg * N64_SIGCONTEXT_REG_SIZE));
1208
1209   for (ireg = 0; ireg < 32; ireg++)
1210     trad_frame_set_reg_addr (this_cache,
1211                              ireg + regs->fp0 + gdbarch_num_regs (gdbarch),
1212                              (sigcontext_base + N64_SIGCONTEXT_FPREGS
1213                               + ireg * N64_SIGCONTEXT_REG_SIZE));
1214
1215   trad_frame_set_reg_addr (this_cache,
1216                            regs->pc + gdbarch_num_regs (gdbarch),
1217                            sigcontext_base + N64_SIGCONTEXT_PC);
1218
1219   trad_frame_set_reg_addr (this_cache,
1220                            (regs->fp_control_status
1221                             + gdbarch_num_regs (gdbarch)),
1222                            sigcontext_base + N64_SIGCONTEXT_FPCSR);
1223
1224   trad_frame_set_reg_addr (this_cache,
1225                            regs->hi + gdbarch_num_regs (gdbarch),
1226                            sigcontext_base + N64_SIGCONTEXT_HI);
1227   trad_frame_set_reg_addr (this_cache,
1228                            regs->lo + gdbarch_num_regs (gdbarch),
1229                            sigcontext_base + N64_SIGCONTEXT_LO);
1230
1231   if (regs->dspacc != -1)
1232     {
1233       trad_frame_set_reg_addr (this_cache,
1234                                regs->dspacc + 0 + gdbarch_num_regs (gdbarch),
1235                                sigcontext_base + N64_SIGCONTEXT_HI1);
1236       trad_frame_set_reg_addr (this_cache,
1237                                regs->dspacc + 1 + gdbarch_num_regs (gdbarch),
1238                                sigcontext_base + N64_SIGCONTEXT_LO1);
1239       trad_frame_set_reg_addr (this_cache,
1240                                regs->dspacc + 2 + gdbarch_num_regs (gdbarch),
1241                                sigcontext_base + N64_SIGCONTEXT_HI2);
1242       trad_frame_set_reg_addr (this_cache,
1243                                regs->dspacc + 3 + gdbarch_num_regs (gdbarch),
1244                                sigcontext_base + N64_SIGCONTEXT_LO2);
1245       trad_frame_set_reg_addr (this_cache,
1246                                regs->dspacc + 4 + gdbarch_num_regs (gdbarch),
1247                                sigcontext_base + N64_SIGCONTEXT_HI3);
1248       trad_frame_set_reg_addr (this_cache,
1249                                regs->dspacc + 5 + gdbarch_num_regs (gdbarch),
1250                                sigcontext_base + N64_SIGCONTEXT_LO3);
1251     }
1252   if (regs->dspctl != -1)
1253     trad_frame_set_reg_addr (this_cache,
1254                              regs->dspctl + gdbarch_num_regs (gdbarch),
1255                              sigcontext_base + N64_SIGCONTEXT_DSPCTL);
1256
1257   /* Choice of the bottom of the sigframe is somewhat arbitrary.  */
1258   trad_frame_set_id (this_cache, frame_id_build (frame_sp, func));
1259 }
1260
1261 /* Implement the "write_pc" gdbarch method.  */
1262
1263 static void
1264 mips_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
1265 {
1266   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1267
1268   mips_write_pc (regcache, pc);
1269
1270   /* Clear the syscall restart flag.  */
1271   if (mips_linux_restart_reg_p (gdbarch))
1272     regcache_cooked_write_unsigned (regcache, MIPS_RESTART_REGNUM, 0);
1273 }
1274
1275 /* Return 1 if MIPS_RESTART_REGNUM is usable.  */
1276
1277 int
1278 mips_linux_restart_reg_p (struct gdbarch *gdbarch)
1279 {
1280   /* If we do not have a target description with registers, then
1281      MIPS_RESTART_REGNUM will not be included in the register set.  */
1282   if (!tdesc_has_registers (gdbarch_target_desc (gdbarch)))
1283     return 0;
1284
1285   /* If we do, then MIPS_RESTART_REGNUM is safe to check; it will
1286      either be GPR-sized or missing.  */
1287   return register_size (gdbarch, MIPS_RESTART_REGNUM) > 0;
1288 }
1289
1290 /* When FRAME is at a syscall instruction, return the PC of the next
1291    instruction to be executed.  */
1292
1293 static CORE_ADDR
1294 mips_linux_syscall_next_pc (struct frame_info *frame)
1295 {
1296   CORE_ADDR pc = get_frame_pc (frame);
1297   ULONGEST v0 = get_frame_register_unsigned (frame, MIPS_V0_REGNUM);
1298
1299   /* If we are about to make a sigreturn syscall, use the unwinder to
1300      decode the signal frame.  */
1301   if (v0 == MIPS_NR_sigreturn
1302       || v0 == MIPS_NR_rt_sigreturn
1303       || v0 == MIPS_NR_N64_rt_sigreturn
1304       || v0 == MIPS_NR_N32_rt_sigreturn)
1305     return frame_unwind_caller_pc (get_current_frame ());
1306
1307   return pc + 4;
1308 }
1309
1310 /* Return the current system call's number present in the
1311    v0 register.  When the function fails, it returns -1.  */
1312
1313 static LONGEST
1314 mips_linux_get_syscall_number (struct gdbarch *gdbarch,
1315                                ptid_t ptid)
1316 {
1317   struct regcache *regcache = get_thread_regcache (ptid);
1318   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1319   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1320   int regsize = register_size (gdbarch, MIPS_V0_REGNUM);
1321   /* The content of a register */
1322   gdb_byte buf[8];
1323   /* The result */
1324   LONGEST ret;
1325
1326   /* Make sure we're in a known ABI */
1327   gdb_assert (tdep->mips_abi == MIPS_ABI_O32
1328               || tdep->mips_abi == MIPS_ABI_N32
1329               || tdep->mips_abi == MIPS_ABI_N64);
1330
1331   gdb_assert (regsize <= sizeof (buf));
1332
1333   /* Getting the system call number from the register.
1334      syscall number is in v0 or $2.  */
1335   regcache_cooked_read (regcache, MIPS_V0_REGNUM, buf);
1336
1337   ret = extract_signed_integer (buf, regsize, byte_order);
1338
1339   return ret;
1340 }
1341
1342 /* Translate signals based on MIPS signal values.
1343    Adapted from gdb/common/signals.c.  */
1344
1345 static enum gdb_signal
1346 mips_gdb_signal_from_target (struct gdbarch *gdbarch, int signo)
1347 {
1348   switch (signo)
1349     {
1350     case 0:
1351       return GDB_SIGNAL_0;
1352     case MIPS_SIGHUP:
1353       return GDB_SIGNAL_HUP;
1354     case MIPS_SIGINT:
1355       return GDB_SIGNAL_INT;
1356     case MIPS_SIGQUIT:
1357       return GDB_SIGNAL_QUIT;
1358     case MIPS_SIGILL:
1359       return GDB_SIGNAL_ILL;
1360     case MIPS_SIGTRAP:
1361       return GDB_SIGNAL_TRAP;
1362     case MIPS_SIGABRT:
1363       return GDB_SIGNAL_ABRT;
1364     case MIPS_SIGEMT:
1365       return GDB_SIGNAL_EMT;
1366     case MIPS_SIGFPE:
1367       return GDB_SIGNAL_FPE;
1368     case MIPS_SIGKILL:
1369       return GDB_SIGNAL_KILL;
1370     case MIPS_SIGBUS:
1371       return GDB_SIGNAL_BUS;
1372     case MIPS_SIGSEGV:
1373       return GDB_SIGNAL_SEGV;
1374     case MIPS_SIGSYS:
1375       return GDB_SIGNAL_SYS;
1376     case MIPS_SIGPIPE:
1377       return GDB_SIGNAL_PIPE;
1378     case MIPS_SIGALRM:
1379       return GDB_SIGNAL_ALRM;
1380     case MIPS_SIGTERM:
1381       return GDB_SIGNAL_TERM;
1382     case MIPS_SIGUSR1:
1383       return GDB_SIGNAL_USR1;
1384     case MIPS_SIGUSR2:
1385       return GDB_SIGNAL_USR2;
1386     case MIPS_SIGCHLD:
1387       return GDB_SIGNAL_CHLD;
1388     case MIPS_SIGPWR:
1389       return GDB_SIGNAL_PWR;
1390     case MIPS_SIGWINCH:
1391       return GDB_SIGNAL_WINCH;
1392     case MIPS_SIGURG:
1393       return GDB_SIGNAL_URG;
1394     case MIPS_SIGPOLL:
1395       return GDB_SIGNAL_POLL;
1396     case MIPS_SIGSTOP:
1397       return GDB_SIGNAL_STOP;
1398     case MIPS_SIGTSTP:
1399       return GDB_SIGNAL_TSTP;
1400     case MIPS_SIGCONT:
1401       return GDB_SIGNAL_CONT;
1402     case MIPS_SIGTTIN:
1403       return GDB_SIGNAL_TTIN;
1404     case MIPS_SIGTTOU:
1405       return GDB_SIGNAL_TTOU;
1406     case MIPS_SIGVTALRM:
1407       return GDB_SIGNAL_VTALRM;
1408     case MIPS_SIGPROF:
1409       return GDB_SIGNAL_PROF;
1410     case MIPS_SIGXCPU:
1411       return GDB_SIGNAL_XCPU;
1412     case MIPS_SIGXFSZ:
1413       return GDB_SIGNAL_XFSZ;
1414   }
1415
1416   if (signo >= MIPS_SIGRTMIN && signo <= MIPS_SIGRTMAX)
1417     {
1418       /* GDB_SIGNAL_REALTIME values are not contiguous, map parts of
1419          the MIPS block to the respective GDB_SIGNAL_REALTIME blocks.  */
1420       signo -= MIPS_SIGRTMIN;
1421       if (signo == 0)
1422         return GDB_SIGNAL_REALTIME_32;
1423       else if (signo < 32)
1424         return ((enum gdb_signal) (signo - 1 + (int) GDB_SIGNAL_REALTIME_33));
1425       else
1426         return ((enum gdb_signal) (signo - 32 + (int) GDB_SIGNAL_REALTIME_64));
1427     }
1428
1429   return GDB_SIGNAL_UNKNOWN;
1430 }
1431
1432 /* Initialize one of the GNU/Linux OS ABIs.  */
1433
1434 static void
1435 mips_linux_init_abi (struct gdbarch_info info,
1436                      struct gdbarch *gdbarch)
1437 {
1438   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1439   enum mips_abi abi = mips_abi (gdbarch);
1440   struct tdesc_arch_data *tdesc_data = (void *) info.tdep_info;
1441
1442   linux_init_abi (info, gdbarch);
1443
1444   /* Get the syscall number from the arch's register.  */
1445   set_gdbarch_get_syscall_number (gdbarch, mips_linux_get_syscall_number);
1446
1447   switch (abi)
1448     {
1449       case MIPS_ABI_O32:
1450         set_gdbarch_get_longjmp_target (gdbarch,
1451                                         mips_linux_get_longjmp_target);
1452         set_solib_svr4_fetch_link_map_offsets
1453           (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1454         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_sigframe);
1455         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_rt_sigframe);
1456         set_xml_syscall_file_name ("syscalls/mips-o32-linux.xml");
1457         break;
1458       case MIPS_ABI_N32:
1459         set_gdbarch_get_longjmp_target (gdbarch,
1460                                         mips_linux_get_longjmp_target);
1461         set_solib_svr4_fetch_link_map_offsets
1462           (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1463         set_gdbarch_long_double_bit (gdbarch, 128);
1464         /* These floatformats should probably be renamed.  MIPS uses
1465            the same 128-bit IEEE floating point format that IA-64 uses,
1466            except that the quiet/signalling NaN bit is reversed (GDB
1467            does not distinguish between quiet and signalling NaNs).  */
1468         set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
1469         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n32_rt_sigframe);
1470         set_xml_syscall_file_name ("syscalls/mips-n32-linux.xml");
1471         break;
1472       case MIPS_ABI_N64:
1473         set_gdbarch_get_longjmp_target (gdbarch,
1474                                         mips64_linux_get_longjmp_target);
1475         set_solib_svr4_fetch_link_map_offsets
1476           (gdbarch, svr4_lp64_fetch_link_map_offsets);
1477         set_gdbarch_long_double_bit (gdbarch, 128);
1478         /* These floatformats should probably be renamed.  MIPS uses
1479            the same 128-bit IEEE floating point format that IA-64 uses,
1480            except that the quiet/signalling NaN bit is reversed (GDB
1481            does not distinguish between quiet and signalling NaNs).  */
1482         set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
1483         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n64_rt_sigframe);
1484         set_xml_syscall_file_name ("syscalls/mips-n64-linux.xml");
1485         break;
1486       default:
1487         break;
1488     }
1489
1490   set_gdbarch_skip_solib_resolver (gdbarch, mips_linux_skip_resolver);
1491
1492   set_gdbarch_software_single_step (gdbarch, mips_software_single_step);
1493
1494   /* Enable TLS support.  */
1495   set_gdbarch_fetch_tls_load_module_address (gdbarch,
1496                                              svr4_fetch_objfile_link_map);
1497
1498   /* Initialize this lazily, to avoid an initialization order
1499      dependency on solib-svr4.c's _initialize routine.  */
1500   if (mips_svr4_so_ops.in_dynsym_resolve_code == NULL)
1501     {
1502       mips_svr4_so_ops = svr4_so_ops;
1503       mips_svr4_so_ops.in_dynsym_resolve_code
1504         = mips_linux_in_dynsym_resolve_code;
1505     }
1506   set_solib_ops (gdbarch, &mips_svr4_so_ops);
1507
1508   set_gdbarch_write_pc (gdbarch, mips_linux_write_pc);
1509
1510   set_gdbarch_core_read_description (gdbarch,
1511                                      mips_linux_core_read_description);
1512
1513   set_gdbarch_regset_from_core_section (gdbarch,
1514                                         mips_linux_regset_from_core_section);
1515
1516   set_gdbarch_gdb_signal_from_target (gdbarch,
1517                                       mips_gdb_signal_from_target);
1518
1519   tdep->syscall_next_pc = mips_linux_syscall_next_pc;
1520
1521   if (tdesc_data)
1522     {
1523       const struct tdesc_feature *feature;
1524
1525       /* If we have target-described registers, then we can safely
1526          reserve a number for MIPS_RESTART_REGNUM (whether it is
1527          described or not).  */
1528       gdb_assert (gdbarch_num_regs (gdbarch) <= MIPS_RESTART_REGNUM);
1529       set_gdbarch_num_regs (gdbarch, MIPS_RESTART_REGNUM + 1);
1530       set_gdbarch_num_pseudo_regs (gdbarch, MIPS_RESTART_REGNUM + 1);
1531
1532       /* If it's present, then assign it to the reserved number.  */
1533       feature = tdesc_find_feature (info.target_desc,
1534                                     "org.gnu.gdb.mips.linux");
1535       if (feature != NULL)
1536         tdesc_numbered_register (feature, tdesc_data, MIPS_RESTART_REGNUM,
1537                                  "restart");
1538     }
1539 }
1540
1541 /* Provide a prototype to silence -Wmissing-prototypes.  */
1542 extern initialize_file_ftype _initialize_mips_linux_tdep;
1543
1544 void
1545 _initialize_mips_linux_tdep (void)
1546 {
1547   const struct bfd_arch_info *arch_info;
1548
1549   for (arch_info = bfd_lookup_arch (bfd_arch_mips, 0);
1550        arch_info != NULL;
1551        arch_info = arch_info->next)
1552     {
1553       gdbarch_register_osabi (bfd_arch_mips, arch_info->mach,
1554                               GDB_OSABI_LINUX,
1555                               mips_linux_init_abi);
1556     }
1557 }