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