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