* mips-linux-tdep.c (supply_32bit_reg): Add REGCACHE parameter. Use it
[platform/upstream/binutils.git] / gdb / mips-linux-tdep.c
1 /* Target-dependent code for GNU/Linux on MIPS processors.
2
3    Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 #include "defs.h"
24 #include "gdbcore.h"
25 #include "target.h"
26 #include "solib-svr4.h"
27 #include "osabi.h"
28 #include "mips-tdep.h"
29 #include "gdb_string.h"
30 #include "gdb_assert.h"
31 #include "frame.h"
32 #include "regcache.h"
33 #include "trad-frame.h"
34 #include "tramp-frame.h"
35 #include "gdbtypes.h"
36 #include "solib.h"
37 #include "symtab.h"
38 #include "mips-linux-tdep.h"
39
40 /* Figure out where the longjmp will land.
41    We expect the first arg to be a pointer to the jmp_buf structure
42    from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
43    at.  The pc is copied into PC.  This routine returns 1 on
44    success.  */
45
46 #define MIPS_LINUX_JB_ELEMENT_SIZE 4
47 #define MIPS_LINUX_JB_PC 0
48
49 static int
50 mips_linux_get_longjmp_target (CORE_ADDR *pc)
51 {
52   CORE_ADDR jb_addr;
53   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
54
55   jb_addr = read_register (MIPS_A0_REGNUM);
56
57   if (target_read_memory (jb_addr
58                           + MIPS_LINUX_JB_PC * MIPS_LINUX_JB_ELEMENT_SIZE,
59                           buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
60     return 0;
61
62   *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
63
64   return 1;
65 }
66
67 /* Transform the bits comprising a 32-bit register to the right size
68    for regcache_raw_supply().  This is needed when mips_isa_regsize()
69    is 8.  */
70
71 static void
72 supply_32bit_reg (struct regcache *regcache, int regnum, const void *addr)
73 {
74   gdb_byte buf[MAX_REGISTER_SIZE];
75   store_signed_integer (buf, register_size (current_gdbarch, regnum),
76                         extract_signed_integer (addr, 4));
77   regcache_raw_supply (regcache, regnum, buf);
78 }
79
80 /* Unpack an elf_gregset_t into GDB's register cache.  */
81
82 void
83 mips_supply_gregset (struct regcache *regcache,
84                      const mips_elf_gregset_t *gregsetp)
85 {
86   int regi;
87   const mips_elf_greg_t *regp = *gregsetp;
88   char zerobuf[MAX_REGISTER_SIZE];
89
90   memset (zerobuf, 0, MAX_REGISTER_SIZE);
91
92   for (regi = EF_REG0; regi <= EF_REG31; regi++)
93     supply_32bit_reg (regcache, regi - EF_REG0, regp + regi);
94
95   supply_32bit_reg (regcache, mips_regnum (current_gdbarch)->lo,
96                     regp + EF_LO);
97   supply_32bit_reg (regcache, mips_regnum (current_gdbarch)->hi,
98                     regp + EF_HI);
99
100   supply_32bit_reg (regcache, mips_regnum (current_gdbarch)->pc,
101                     regp + EF_CP0_EPC);
102   supply_32bit_reg (regcache, mips_regnum (current_gdbarch)->badvaddr,
103                     regp + EF_CP0_BADVADDR);
104   supply_32bit_reg (regcache, MIPS_PS_REGNUM, regp + EF_CP0_STATUS);
105   supply_32bit_reg (regcache, mips_regnum (current_gdbarch)->cause,
106                     regp + EF_CP0_CAUSE);
107
108   /* Fill inaccessible registers with zero.  */
109   regcache_raw_supply (regcache, MIPS_UNUSED_REGNUM, zerobuf);
110   for (regi = MIPS_FIRST_EMBED_REGNUM;
111        regi < MIPS_LAST_EMBED_REGNUM;
112        regi++)
113     regcache_raw_supply (regcache, regi, zerobuf);
114 }
115
116 /* Pack our registers (or one register) into an elf_gregset_t.  */
117
118 void
119 mips_fill_gregset (const struct regcache *regcache,
120                    mips_elf_gregset_t *gregsetp, int regno)
121 {
122   int regaddr, regi;
123   mips_elf_greg_t *regp = *gregsetp;
124   void *dst;
125
126   if (regno == -1)
127     {
128       memset (regp, 0, sizeof (mips_elf_gregset_t));
129       for (regi = 0; regi < 32; regi++)
130         mips_fill_gregset (regcache, gregsetp, regi);
131       mips_fill_gregset (regcache, gregsetp,
132                          mips_regnum (current_gdbarch)->lo);
133       mips_fill_gregset (regcache, gregsetp,
134                          mips_regnum (current_gdbarch)->hi);
135       mips_fill_gregset (regcache, gregsetp,
136                          mips_regnum (current_gdbarch)->pc);
137       mips_fill_gregset (regcache, gregsetp,
138                          mips_regnum (current_gdbarch)->badvaddr);
139       mips_fill_gregset (regcache, gregsetp, MIPS_PS_REGNUM);
140       mips_fill_gregset (regcache, gregsetp,
141                          mips_regnum (current_gdbarch)->cause);
142       return;
143    }
144
145   if (regno < 32)
146     {
147       dst = regp + regno + EF_REG0;
148       regcache_raw_collect (regcache, regno, dst);
149       return;
150     }
151
152   if (regno == mips_regnum (current_gdbarch)->lo)
153     regaddr = EF_LO;
154   else if (regno == mips_regnum (current_gdbarch)->hi)
155     regaddr = EF_HI;
156   else if (regno == mips_regnum (current_gdbarch)->pc)
157     regaddr = EF_CP0_EPC;
158   else if (regno == mips_regnum (current_gdbarch)->badvaddr)
159     regaddr = EF_CP0_BADVADDR;
160   else if (regno == MIPS_PS_REGNUM)
161     regaddr = EF_CP0_STATUS;
162   else if (regno == mips_regnum (current_gdbarch)->cause)
163     regaddr = EF_CP0_CAUSE;
164   else
165     regaddr = -1;
166
167   if (regaddr != -1)
168     {
169       dst = regp + regaddr;
170       regcache_raw_collect (regcache, regno, dst);
171     }
172 }
173
174 /* Likewise, unpack an elf_fpregset_t.  */
175
176 void
177 mips_supply_fpregset (struct regcache *regcache,
178                       const mips_elf_fpregset_t *fpregsetp)
179 {
180   int regi;
181   char zerobuf[MAX_REGISTER_SIZE];
182
183   memset (zerobuf, 0, MAX_REGISTER_SIZE);
184
185   for (regi = 0; regi < 32; regi++)
186     regcache_raw_supply (regcache, FP0_REGNUM + regi, *fpregsetp + regi);
187
188   regcache_raw_supply (regcache,
189                        mips_regnum (current_gdbarch)->fp_control_status,
190                        *fpregsetp + 32);
191
192   /* FIXME: how can we supply FCRIR?  The ABI doesn't tell us.  */
193   regcache_raw_supply (regcache,
194                        mips_regnum (current_gdbarch)->fp_implementation_revision,
195                        zerobuf);
196 }
197
198 /* Likewise, pack one or all floating point registers into an
199    elf_fpregset_t.  */
200
201 void
202 mips_fill_fpregset (const struct regcache *regcache,
203                     mips_elf_fpregset_t *fpregsetp, int regno)
204 {
205   char *from, *to;
206
207   if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
208     {
209       to = (char *) (*fpregsetp + regno - FP0_REGNUM);
210       regcache_raw_collect (regcache, regno, to);
211     }
212   else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
213     {
214       to = (char *) (*fpregsetp + 32);
215       regcache_raw_collect (regcache, regno, to);
216     }
217   else if (regno == -1)
218     {
219       int regi;
220
221       for (regi = 0; regi < 32; regi++)
222         mips_fill_fpregset (regcache, fpregsetp, FP0_REGNUM + regi);
223       mips_fill_fpregset (regcache, fpregsetp,
224                           mips_regnum (current_gdbarch)->fp_control_status);
225     }
226 }
227
228 /* Support for 64-bit ABIs.  */
229
230 /* Figure out where the longjmp will land.
231    We expect the first arg to be a pointer to the jmp_buf structure
232    from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
233    at.  The pc is copied into PC.  This routine returns 1 on
234    success.  */
235
236 /* Details about jmp_buf.  */
237
238 #define MIPS64_LINUX_JB_PC 0
239
240 static int
241 mips64_linux_get_longjmp_target (CORE_ADDR *pc)
242 {
243   CORE_ADDR jb_addr;
244   void *buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
245   int element_size = TARGET_PTR_BIT == 32 ? 4 : 8;
246
247   jb_addr = read_register (MIPS_A0_REGNUM);
248
249   if (target_read_memory (jb_addr + MIPS64_LINUX_JB_PC * element_size,
250                           buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
251     return 0;
252
253   *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
254
255   return 1;
256 }
257
258 /* Register set support functions.  These operate on standard 64-bit
259    regsets, but work whether the target is 32-bit or 64-bit.  A 32-bit
260    target will still use the 64-bit format for PTRACE_GETREGS.  */
261
262 /* Supply a 64-bit register.  */
263
264 void
265 supply_64bit_reg (struct regcache *regcache, int regnum,
266                   const gdb_byte *buf)
267 {
268   if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG
269       && register_size (current_gdbarch, regnum) == 4)
270     regcache_raw_supply (regcache, regnum, buf + 4);
271   else
272     regcache_raw_supply (regcache, regnum, buf);
273 }
274
275 /* Unpack a 64-bit elf_gregset_t into GDB's register cache.  */
276
277 void
278 mips64_supply_gregset (struct regcache *regcache,
279                        const mips64_elf_gregset_t *gregsetp)
280 {
281   int regi;
282   const mips64_elf_greg_t *regp = *gregsetp;
283   gdb_byte zerobuf[MAX_REGISTER_SIZE];
284
285   memset (zerobuf, 0, MAX_REGISTER_SIZE);
286
287   for (regi = MIPS64_EF_REG0; regi <= MIPS64_EF_REG31; regi++)
288     supply_64bit_reg (regcache, regi - MIPS64_EF_REG0,
289                       (const gdb_byte *)(regp + regi));
290
291   supply_64bit_reg (regcache, mips_regnum (current_gdbarch)->lo,
292                     (const gdb_byte *) (regp + MIPS64_EF_LO));
293   supply_64bit_reg (regcache, mips_regnum (current_gdbarch)->hi,
294                     (const gdb_byte *) (regp + MIPS64_EF_HI));
295
296   supply_64bit_reg (regcache, mips_regnum (current_gdbarch)->pc,
297                     (const gdb_byte *) (regp + MIPS64_EF_CP0_EPC));
298   supply_64bit_reg (regcache, mips_regnum (current_gdbarch)->badvaddr,
299                     (const gdb_byte *) (regp + MIPS64_EF_CP0_BADVADDR));
300   supply_64bit_reg (regcache, MIPS_PS_REGNUM,
301                     (const gdb_byte *) (regp + MIPS64_EF_CP0_STATUS));
302   supply_64bit_reg (regcache, mips_regnum (current_gdbarch)->cause,
303                     (const gdb_byte *) (regp + MIPS64_EF_CP0_CAUSE));
304
305   /* Fill inaccessible registers with zero.  */
306   regcache_raw_supply (regcache, MIPS_UNUSED_REGNUM, zerobuf);
307   for (regi = MIPS_FIRST_EMBED_REGNUM;
308        regi < MIPS_LAST_EMBED_REGNUM;
309        regi++)
310     regcache_raw_supply (regcache, regi, zerobuf);
311 }
312
313 /* Pack our registers (or one register) into a 64-bit elf_gregset_t.  */
314
315 void
316 mips64_fill_gregset (const struct regcache *regcache,
317                      mips64_elf_gregset_t *gregsetp, int regno)
318 {
319   int regaddr, regi;
320   mips64_elf_greg_t *regp = *gregsetp;
321   void *src, *dst;
322
323   if (regno == -1)
324     {
325       memset (regp, 0, sizeof (mips64_elf_gregset_t));
326       for (regi = 0; regi < 32; regi++)
327         mips64_fill_gregset (regcache, gregsetp, regi);
328       mips64_fill_gregset (regcache, gregsetp,
329                            mips_regnum (current_gdbarch)->lo);
330       mips64_fill_gregset (regcache, gregsetp,
331                            mips_regnum (current_gdbarch)->hi);
332       mips64_fill_gregset (regcache, gregsetp,
333                            mips_regnum (current_gdbarch)->pc);
334       mips64_fill_gregset (regcache, gregsetp,
335                            mips_regnum (current_gdbarch)->badvaddr);
336       mips64_fill_gregset (regcache, gregsetp, MIPS_PS_REGNUM);
337       mips64_fill_gregset (regcache, gregsetp,
338                            mips_regnum (current_gdbarch)->cause);
339       return;
340    }
341
342   if (regno < 32)
343     regaddr = regno + MIPS64_EF_REG0;
344   else if (regno == mips_regnum (current_gdbarch)->lo)
345     regaddr = MIPS64_EF_LO;
346   else if (regno == mips_regnum (current_gdbarch)->hi)
347     regaddr = MIPS64_EF_HI;
348   else if (regno == mips_regnum (current_gdbarch)->pc)
349     regaddr = MIPS64_EF_CP0_EPC;
350   else if (regno == mips_regnum (current_gdbarch)->badvaddr)
351     regaddr = MIPS64_EF_CP0_BADVADDR;
352   else if (regno == MIPS_PS_REGNUM)
353     regaddr = MIPS64_EF_CP0_STATUS;
354   else if (regno == mips_regnum (current_gdbarch)->cause)
355     regaddr = MIPS64_EF_CP0_CAUSE;
356   else
357     regaddr = -1;
358
359   if (regaddr != -1)
360     {
361       gdb_byte buf[MAX_REGISTER_SIZE];
362       LONGEST val;
363
364       regcache_raw_collect (regcache, regno, buf);
365       val = extract_signed_integer (buf,
366                                     register_size (current_gdbarch, regno));
367       dst = regp + regaddr;
368       store_signed_integer (dst, 8, val);
369     }
370 }
371
372 /* Likewise, unpack an elf_fpregset_t.  */
373
374 void
375 mips64_supply_fpregset (struct regcache *regcache,
376                         const mips64_elf_fpregset_t *fpregsetp)
377 {
378   int regi;
379
380   /* See mips_linux_o32_sigframe_init for a description of the
381      peculiar FP register layout.  */
382   if (register_size (current_gdbarch, FP0_REGNUM) == 4)
383     for (regi = 0; regi < 32; regi++)
384       {
385         const gdb_byte *reg_ptr = (const gdb_byte *)(*fpregsetp + (regi & ~1));
386         if ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) != (regi & 1))
387           reg_ptr += 4;
388         regcache_raw_supply (regcache, FP0_REGNUM + regi, reg_ptr);
389       }
390   else
391     for (regi = 0; regi < 32; regi++)
392       regcache_raw_supply (regcache, FP0_REGNUM + regi,
393                            (const char *)(*fpregsetp + regi));
394
395   supply_32bit_reg (regcache, mips_regnum (current_gdbarch)->fp_control_status,
396                     (const gdb_byte *)(*fpregsetp + 32));
397
398   /* The ABI doesn't tell us how to supply FCRIR, and core dumps don't
399      include it - but the result of PTRACE_GETFPREGS does.  The best we
400      can do is to assume that its value is present.  */
401   supply_32bit_reg (regcache,
402                     mips_regnum (current_gdbarch)->fp_implementation_revision,
403                     (const gdb_byte *)(*fpregsetp + 32) + 4);
404 }
405
406 /* Likewise, pack one or all floating point registers into an
407    elf_fpregset_t.  */
408
409 void
410 mips64_fill_fpregset (const struct regcache *regcache,
411                       mips64_elf_fpregset_t *fpregsetp, int regno)
412 {
413   gdb_byte *to;
414
415   if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
416     {
417       /* See mips_linux_o32_sigframe_init for a description of the
418          peculiar FP register layout.  */
419       if (register_size (current_gdbarch, regno) == 4)
420         {
421           int regi = regno - FP0_REGNUM;
422
423           to = (gdb_byte *) (*fpregsetp + (regi & ~1));
424           if ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) != (regi & 1))
425             to += 4;
426           regcache_raw_collect (regcache, regno, to);
427         }
428       else
429         {
430           to = (gdb_byte *) (*fpregsetp + regno - FP0_REGNUM);
431           regcache_raw_collect (regcache, regno, to);
432         }
433     }
434   else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
435     {
436       gdb_byte buf[MAX_REGISTER_SIZE];
437       LONGEST val;
438
439       regcache_raw_collect (regcache, regno, buf);
440       val = extract_signed_integer (buf,
441                                     register_size (current_gdbarch, regno));
442       to = (gdb_byte *) (*fpregsetp + 32);
443       store_signed_integer (to, 4, val);
444     }
445   else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
446     {
447       gdb_byte buf[MAX_REGISTER_SIZE];
448       LONGEST val;
449
450       regcache_raw_collect (regcache, regno, buf);
451       val = extract_signed_integer (buf,
452                                     register_size (current_gdbarch, regno));
453       to = (gdb_byte *) (*fpregsetp + 32) + 4;
454       store_signed_integer (to, 4, val);
455     }
456   else if (regno == -1)
457     {
458       int regi;
459
460       for (regi = 0; regi < 32; regi++)
461         mips64_fill_fpregset (regcache, fpregsetp, FP0_REGNUM + regi);
462       mips64_fill_fpregset (regcache, fpregsetp,
463                             mips_regnum (current_gdbarch)->fp_control_status);
464       mips64_fill_fpregset (regcache, fpregsetp,
465                             (mips_regnum (current_gdbarch)
466                              ->fp_implementation_revision));
467     }
468 }
469
470
471 /*  Use a local version of this function to get the correct types for
472     regsets, until multi-arch core support is ready.  */
473
474 static void
475 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
476                       int which, CORE_ADDR reg_addr)
477 {
478   mips_elf_gregset_t gregset;
479   mips_elf_fpregset_t fpregset;
480   mips64_elf_gregset_t gregset64;
481   mips64_elf_fpregset_t fpregset64;
482
483   if (which == 0)
484     {
485       if (core_reg_size == sizeof (gregset))
486         {
487           memcpy ((char *) &gregset, core_reg_sect, sizeof (gregset));
488           mips_supply_gregset (current_regcache,
489                                (const mips_elf_gregset_t *) &gregset);
490         }
491       else if (core_reg_size == sizeof (gregset64))
492         {
493           memcpy ((char *) &gregset64, core_reg_sect, sizeof (gregset64));
494           mips64_supply_gregset (current_regcache,
495                                  (const mips64_elf_gregset_t *) &gregset64);
496         }
497       else
498         {
499           warning (_("wrong size gregset struct in core file"));
500         }
501     }
502   else if (which == 2)
503     {
504       if (core_reg_size == sizeof (fpregset))
505         {
506           memcpy ((char *) &fpregset, core_reg_sect, sizeof (fpregset));
507           mips_supply_fpregset (current_regcache,
508                                 (const mips_elf_fpregset_t *) &fpregset);
509         }
510       else if (core_reg_size == sizeof (fpregset64))
511         {
512           memcpy ((char *) &fpregset64, core_reg_sect,
513                   sizeof (fpregset64));
514           mips64_supply_fpregset (current_regcache,
515                                   (const mips64_elf_fpregset_t *) &fpregset64);
516         }
517       else
518         {
519           warning (_("wrong size fpregset struct in core file"));
520         }
521     }
522 }
523
524 /* Register that we are able to handle ELF file formats using standard
525    procfs "regset" structures.  */
526
527 static struct core_fns regset_core_fns =
528 {
529   bfd_target_elf_flavour,               /* core_flavour */
530   default_check_format,                 /* check_format */
531   default_core_sniffer,                 /* core_sniffer */
532   fetch_core_registers,                 /* core_read_registers */
533   NULL                                  /* next */
534 };
535
536
537 /* Check the code at PC for a dynamic linker lazy resolution stub.
538    Because they aren't in the .plt section, we pattern-match on the
539    code generated by GNU ld.  They look like this:
540
541    lw t9,0x8010(gp)
542    addu t7,ra
543    jalr t9,ra
544    addiu t8,zero,INDEX
545
546    (with the appropriate doubleword instructions for N64).  Also
547    return the dynamic symbol index used in the last instruction.  */
548
549 static int
550 mips_linux_in_dynsym_stub (CORE_ADDR pc, char *name)
551 {
552   unsigned char buf[28], *p;
553   ULONGEST insn, insn1;
554   int n64 = (mips_abi (current_gdbarch) == MIPS_ABI_N64);
555
556   read_memory (pc - 12, buf, 28);
557
558   if (n64)
559     {
560       /* ld t9,0x8010(gp) */
561       insn1 = 0xdf998010;
562     }
563   else
564     {
565       /* lw t9,0x8010(gp) */
566       insn1 = 0x8f998010;
567     }
568
569   p = buf + 12;
570   while (p >= buf)
571     {
572       insn = extract_unsigned_integer (p, 4);
573       if (insn == insn1)
574         break;
575       p -= 4;
576     }
577   if (p < buf)
578     return 0;
579
580   insn = extract_unsigned_integer (p + 4, 4);
581   if (n64)
582     {
583       /* daddu t7,ra */
584       if (insn != 0x03e0782d)
585         return 0;
586     }
587   else
588     {
589       /* addu t7,ra */
590       if (insn != 0x03e07821)
591         return 0;
592     }
593
594   insn = extract_unsigned_integer (p + 8, 4);
595   /* jalr t9,ra */
596   if (insn != 0x0320f809)
597     return 0;
598
599   insn = extract_unsigned_integer (p + 12, 4);
600   if (n64)
601     {
602       /* daddiu t8,zero,0 */
603       if ((insn & 0xffff0000) != 0x64180000)
604         return 0;
605     }
606   else
607     {
608       /* addiu t8,zero,0 */
609       if ((insn & 0xffff0000) != 0x24180000)
610         return 0;
611     }
612
613   return (insn & 0xffff);
614 }
615
616 /* Return non-zero iff PC belongs to the dynamic linker resolution
617    code or to a stub.  */
618
619 int
620 mips_linux_in_dynsym_resolve_code (CORE_ADDR pc)
621 {
622   /* Check whether PC is in the dynamic linker.  This also checks
623      whether it is in the .plt section, which MIPS does not use.  */
624   if (in_solib_dynsym_resolve_code (pc))
625     return 1;
626
627   /* Pattern match for the stub.  It would be nice if there were a
628      more efficient way to avoid this check.  */
629   if (mips_linux_in_dynsym_stub (pc, NULL))
630     return 1;
631
632   return 0;
633 }
634
635 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c,
636    and glibc_skip_solib_resolver in glibc-tdep.c.  The normal glibc
637    implementation of this triggers at "fixup" from the same objfile as
638    "_dl_runtime_resolve"; MIPS GNU/Linux can trigger at
639    "__dl_runtime_resolve" directly.  An unresolved PLT entry will
640    point to _dl_runtime_resolve, which will first call
641    __dl_runtime_resolve, and then pass control to the resolved
642    function.  */
643
644 static CORE_ADDR
645 mips_linux_skip_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
646 {
647   struct minimal_symbol *resolver;
648
649   resolver = lookup_minimal_symbol ("__dl_runtime_resolve", NULL, NULL);
650
651   if (resolver && SYMBOL_VALUE_ADDRESS (resolver) == pc)
652     return frame_pc_unwind (get_current_frame ());
653
654   return 0;
655 }
656
657 /* Signal trampoline support.  There are four supported layouts for a
658    signal frame: o32 sigframe, o32 rt_sigframe, n32 rt_sigframe, and
659    n64 rt_sigframe.  We handle them all independently; not the most
660    efficient way, but simplest.  First, declare all the unwinders.  */
661
662 static void mips_linux_o32_sigframe_init (const struct tramp_frame *self,
663                                           struct frame_info *next_frame,
664                                           struct trad_frame_cache *this_cache,
665                                           CORE_ADDR func);
666
667 static void mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
668                                              struct frame_info *next_frame,
669                                              struct trad_frame_cache *this_cache,
670                                              CORE_ADDR func);
671
672 #define MIPS_NR_LINUX 4000
673 #define MIPS_NR_N64_LINUX 5000
674 #define MIPS_NR_N32_LINUX 6000
675
676 #define MIPS_NR_sigreturn MIPS_NR_LINUX + 119
677 #define MIPS_NR_rt_sigreturn MIPS_NR_LINUX + 193
678 #define MIPS_NR_N64_rt_sigreturn MIPS_NR_N64_LINUX + 211
679 #define MIPS_NR_N32_rt_sigreturn MIPS_NR_N32_LINUX + 211
680
681 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + MIPS_NR_sigreturn
682 #define MIPS_INST_LI_V0_RT_SIGRETURN 0x24020000 + MIPS_NR_rt_sigreturn
683 #define MIPS_INST_LI_V0_N64_RT_SIGRETURN 0x24020000 + MIPS_NR_N64_rt_sigreturn
684 #define MIPS_INST_LI_V0_N32_RT_SIGRETURN 0x24020000 + MIPS_NR_N32_rt_sigreturn
685 #define MIPS_INST_SYSCALL 0x0000000c
686
687 static const struct tramp_frame mips_linux_o32_sigframe = {
688   SIGTRAMP_FRAME,
689   4,
690   {
691     { MIPS_INST_LI_V0_SIGRETURN, -1 },
692     { MIPS_INST_SYSCALL, -1 },
693     { TRAMP_SENTINEL_INSN, -1 }
694   },
695   mips_linux_o32_sigframe_init
696 };
697
698 static const struct tramp_frame mips_linux_o32_rt_sigframe = {
699   SIGTRAMP_FRAME,
700   4,
701   {
702     { MIPS_INST_LI_V0_RT_SIGRETURN, -1 },
703     { MIPS_INST_SYSCALL, -1 },
704     { TRAMP_SENTINEL_INSN, -1 } },
705   mips_linux_o32_sigframe_init
706 };
707
708 static const struct tramp_frame mips_linux_n32_rt_sigframe = {
709   SIGTRAMP_FRAME,
710   4,
711   {
712     { MIPS_INST_LI_V0_N32_RT_SIGRETURN, -1 },
713     { MIPS_INST_SYSCALL, -1 },
714     { TRAMP_SENTINEL_INSN, -1 }
715   },
716   mips_linux_n32n64_sigframe_init
717 };
718
719 static const struct tramp_frame mips_linux_n64_rt_sigframe = {
720   SIGTRAMP_FRAME,
721   4,
722   {
723     { MIPS_INST_LI_V0_N64_RT_SIGRETURN, -1 },
724     { MIPS_INST_SYSCALL, -1 },
725     { TRAMP_SENTINEL_INSN, -1 }
726   },
727   mips_linux_n32n64_sigframe_init
728 };
729
730 /* *INDENT-OFF* */
731 /* The unwinder for o32 signal frames.  The legacy structures look
732    like this:
733
734    struct sigframe {
735      u32 sf_ass[4];            [argument save space for o32]
736      u32 sf_code[2];           [signal trampoline]
737      struct sigcontext sf_sc;
738      sigset_t sf_mask;
739    };
740
741    struct sigcontext {
742         unsigned int       sc_regmask;          [Unused]
743         unsigned int       sc_status;
744         unsigned long long sc_pc;
745         unsigned long long sc_regs[32];
746         unsigned long long sc_fpregs[32];
747         unsigned int       sc_ownedfp;
748         unsigned int       sc_fpc_csr;
749         unsigned int       sc_fpc_eir;          [Unused]
750         unsigned int       sc_used_math;
751         unsigned int       sc_ssflags;          [Unused]
752         [Alignment hole of four bytes]
753         unsigned long long sc_mdhi;
754         unsigned long long sc_mdlo;
755
756         unsigned int       sc_cause;            [Unused]
757         unsigned int       sc_badvaddr;         [Unused]
758
759         unsigned long      sc_sigset[4];        [kernel's sigset_t]
760    };
761
762    The RT signal frames look like this:
763
764    struct rt_sigframe {
765      u32 rs_ass[4];            [argument save space for o32]
766      u32 rs_code[2]            [signal trampoline]
767      struct siginfo rs_info;
768      struct ucontext rs_uc;
769    };
770
771    struct ucontext {
772      unsigned long     uc_flags;
773      struct ucontext  *uc_link;
774      stack_t           uc_stack;
775      [Alignment hole of four bytes]
776      struct sigcontext uc_mcontext;
777      sigset_t          uc_sigmask;
778    };  */
779 /* *INDENT-ON* */
780
781 #define SIGFRAME_CODE_OFFSET         (4 * 4)
782 #define SIGFRAME_SIGCONTEXT_OFFSET   (6 * 4)
783
784 #define RTSIGFRAME_SIGINFO_SIZE      128
785 #define STACK_T_SIZE                 (3 * 4)
786 #define UCONTEXT_SIGCONTEXT_OFFSET   (2 * 4 + STACK_T_SIZE + 4)
787 #define RTSIGFRAME_SIGCONTEXT_OFFSET (SIGFRAME_SIGCONTEXT_OFFSET \
788                                       + RTSIGFRAME_SIGINFO_SIZE \
789                                       + UCONTEXT_SIGCONTEXT_OFFSET)
790
791 #define SIGCONTEXT_PC       (1 * 8)
792 #define SIGCONTEXT_REGS     (2 * 8)
793 #define SIGCONTEXT_FPREGS   (34 * 8)
794 #define SIGCONTEXT_FPCSR    (66 * 8 + 4)
795 #define SIGCONTEXT_HI       (69 * 8)
796 #define SIGCONTEXT_LO       (70 * 8)
797 #define SIGCONTEXT_CAUSE    (71 * 8 + 0)
798 #define SIGCONTEXT_BADVADDR (71 * 8 + 4)
799
800 #define SIGCONTEXT_REG_SIZE 8
801
802 static void
803 mips_linux_o32_sigframe_init (const struct tramp_frame *self,
804                               struct frame_info *next_frame,
805                               struct trad_frame_cache *this_cache,
806                               CORE_ADDR func)
807 {
808   int ireg, reg_position;
809   CORE_ADDR sigcontext_base = func - SIGFRAME_CODE_OFFSET;
810   const struct mips_regnum *regs = mips_regnum (current_gdbarch);
811   CORE_ADDR regs_base;
812
813   if (self == &mips_linux_o32_sigframe)
814     sigcontext_base += SIGFRAME_SIGCONTEXT_OFFSET;
815   else
816     sigcontext_base += RTSIGFRAME_SIGCONTEXT_OFFSET;
817
818   /* I'm not proud of this hack.  Eventually we will have the
819      infrastructure to indicate the size of saved registers on a
820      per-frame basis, but right now we don't; the kernel saves eight
821      bytes but we only want four.  Use regs_base to access any
822      64-bit fields.  */
823   if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
824     regs_base = sigcontext_base + 4;
825   else
826     regs_base = sigcontext_base;
827
828 #if 0
829   trad_frame_set_reg_addr (this_cache, ORIG_ZERO_REGNUM + NUM_REGS,
830                            regs_base + SIGCONTEXT_REGS);
831 #endif
832
833   for (ireg = 1; ireg < 32; ireg++)
834     trad_frame_set_reg_addr (this_cache,
835                              ireg + MIPS_ZERO_REGNUM + NUM_REGS,
836                              regs_base + SIGCONTEXT_REGS
837                              + ireg * SIGCONTEXT_REG_SIZE);
838
839   /* The way that floating point registers are saved, unfortunately,
840      depends on the architecture the kernel is built for.  For the r3000 and
841      tx39, four bytes of each register are at the beginning of each of the
842      32 eight byte slots.  For everything else, the registers are saved
843      using double precision; only the even-numbered slots are initialized,
844      and the high bits are the odd-numbered register.  Assume the latter
845      layout, since we can't tell, and it's much more common.  Which bits are
846      the "high" bits depends on endianness.  */
847   for (ireg = 0; ireg < 32; ireg++)
848     if ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) != (ireg & 1))
849       trad_frame_set_reg_addr (this_cache, ireg + regs->fp0 + NUM_REGS,
850                                sigcontext_base + SIGCONTEXT_FPREGS + 4
851                                + (ireg & ~1) * SIGCONTEXT_REG_SIZE);
852     else
853       trad_frame_set_reg_addr (this_cache, ireg + regs->fp0 + NUM_REGS,
854                                sigcontext_base + SIGCONTEXT_FPREGS
855                                + (ireg & ~1) * SIGCONTEXT_REG_SIZE);
856
857   trad_frame_set_reg_addr (this_cache, regs->pc + NUM_REGS,
858                            regs_base + SIGCONTEXT_PC);
859
860   trad_frame_set_reg_addr (this_cache,
861                            regs->fp_control_status + NUM_REGS,
862                            sigcontext_base + SIGCONTEXT_FPCSR);
863   trad_frame_set_reg_addr (this_cache, regs->hi + NUM_REGS,
864                            regs_base + SIGCONTEXT_HI);
865   trad_frame_set_reg_addr (this_cache, regs->lo + NUM_REGS,
866                            regs_base + SIGCONTEXT_LO);
867   trad_frame_set_reg_addr (this_cache, regs->cause + NUM_REGS,
868                            sigcontext_base + SIGCONTEXT_CAUSE);
869   trad_frame_set_reg_addr (this_cache, regs->badvaddr + NUM_REGS,
870                            sigcontext_base + SIGCONTEXT_BADVADDR);
871
872   /* Choice of the bottom of the sigframe is somewhat arbitrary.  */
873   trad_frame_set_id (this_cache,
874                      frame_id_build (func - SIGFRAME_CODE_OFFSET,
875                                      func));
876 }
877
878 /* *INDENT-OFF* */
879 /* For N32/N64 things look different.  There is no non-rt signal frame.
880
881   struct rt_sigframe_n32 {
882     u32 rs_ass[4];                  [ argument save space for o32 ]
883     u32 rs_code[2];                 [ signal trampoline ]
884     struct siginfo rs_info;
885     struct ucontextn32 rs_uc;
886   };
887
888   struct ucontextn32 {
889     u32                 uc_flags;
890     s32                 uc_link;
891     stack32_t           uc_stack;
892     struct sigcontext   uc_mcontext;
893     sigset_t            uc_sigmask;   [ mask last for extensibility ]
894   };
895
896   struct rt_sigframe_n32 {
897     u32 rs_ass[4];                  [ argument save space for o32 ]
898     u32 rs_code[2];                 [ signal trampoline ]
899     struct siginfo rs_info;
900     struct ucontext rs_uc;
901   };
902
903   struct ucontext {
904     unsigned long     uc_flags;
905     struct ucontext  *uc_link;
906     stack_t           uc_stack;
907     struct sigcontext uc_mcontext;
908     sigset_t          uc_sigmask;   [ mask last for extensibility ]
909   };
910
911   And the sigcontext is different (this is for both n32 and n64):
912
913   struct sigcontext {
914     unsigned long long sc_regs[32];
915     unsigned long long sc_fpregs[32];
916     unsigned long long sc_mdhi;
917     unsigned long long sc_mdlo;
918     unsigned long long sc_pc;
919     unsigned int       sc_status;
920     unsigned int       sc_fpc_csr;
921     unsigned int       sc_fpc_eir;
922     unsigned int       sc_used_math;
923     unsigned int       sc_cause;
924     unsigned int       sc_badvaddr;
925   };  */
926 /* *INDENT-ON* */
927
928 #define N32_STACK_T_SIZE                STACK_T_SIZE
929 #define N64_STACK_T_SIZE                (2 * 8 + 4)
930 #define N32_UCONTEXT_SIGCONTEXT_OFFSET  (2 * 4 + N32_STACK_T_SIZE + 4)
931 #define N64_UCONTEXT_SIGCONTEXT_OFFSET  (2 * 8 + N64_STACK_T_SIZE + 4)
932 #define N32_SIGFRAME_SIGCONTEXT_OFFSET  (SIGFRAME_SIGCONTEXT_OFFSET \
933                                          + RTSIGFRAME_SIGINFO_SIZE \
934                                          + N32_UCONTEXT_SIGCONTEXT_OFFSET)
935 #define N64_SIGFRAME_SIGCONTEXT_OFFSET  (SIGFRAME_SIGCONTEXT_OFFSET \
936                                          + RTSIGFRAME_SIGINFO_SIZE \
937                                          + N64_UCONTEXT_SIGCONTEXT_OFFSET)
938
939 #define N64_SIGCONTEXT_REGS     (0 * 8)
940 #define N64_SIGCONTEXT_FPREGS   (32 * 8)
941 #define N64_SIGCONTEXT_HI       (64 * 8)
942 #define N64_SIGCONTEXT_LO       (65 * 8)
943 #define N64_SIGCONTEXT_PC       (66 * 8)
944 #define N64_SIGCONTEXT_FPCSR    (67 * 8 + 1 * 4)
945 #define N64_SIGCONTEXT_FIR      (67 * 8 + 2 * 4)
946 #define N64_SIGCONTEXT_CAUSE    (67 * 8 + 4 * 4)
947 #define N64_SIGCONTEXT_BADVADDR (67 * 8 + 5 * 4)
948
949 #define N64_SIGCONTEXT_REG_SIZE 8
950
951 static void
952 mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
953                                  struct frame_info *next_frame,
954                                  struct trad_frame_cache *this_cache,
955                                  CORE_ADDR func)
956 {
957   int ireg, reg_position;
958   CORE_ADDR sigcontext_base = func - SIGFRAME_CODE_OFFSET;
959   const struct mips_regnum *regs = mips_regnum (current_gdbarch);
960
961   if (self == &mips_linux_n32_rt_sigframe)
962     sigcontext_base += N32_SIGFRAME_SIGCONTEXT_OFFSET;
963   else
964     sigcontext_base += N64_SIGFRAME_SIGCONTEXT_OFFSET;
965
966 #if 0
967   trad_frame_set_reg_addr (this_cache, ORIG_ZERO_REGNUM + NUM_REGS,
968                            sigcontext_base + N64_SIGCONTEXT_REGS);
969 #endif
970
971   for (ireg = 1; ireg < 32; ireg++)
972     trad_frame_set_reg_addr (this_cache,
973                              ireg + MIPS_ZERO_REGNUM + NUM_REGS,
974                              sigcontext_base + N64_SIGCONTEXT_REGS
975                              + ireg * N64_SIGCONTEXT_REG_SIZE);
976
977   for (ireg = 0; ireg < 32; ireg++)
978     trad_frame_set_reg_addr (this_cache, ireg + regs->fp0 + NUM_REGS,
979                              sigcontext_base + N64_SIGCONTEXT_FPREGS
980                              + ireg * N64_SIGCONTEXT_REG_SIZE);
981
982   trad_frame_set_reg_addr (this_cache, regs->pc + NUM_REGS,
983                            sigcontext_base + N64_SIGCONTEXT_PC);
984
985   trad_frame_set_reg_addr (this_cache,
986                            regs->fp_control_status + NUM_REGS,
987                            sigcontext_base + N64_SIGCONTEXT_FPCSR);
988   trad_frame_set_reg_addr (this_cache, regs->hi + NUM_REGS,
989                            sigcontext_base + N64_SIGCONTEXT_HI);
990   trad_frame_set_reg_addr (this_cache, regs->lo + NUM_REGS,
991                            sigcontext_base + N64_SIGCONTEXT_LO);
992   trad_frame_set_reg_addr (this_cache, regs->cause + NUM_REGS,
993                            sigcontext_base + N64_SIGCONTEXT_CAUSE);
994   trad_frame_set_reg_addr (this_cache, regs->badvaddr + NUM_REGS,
995                            sigcontext_base + N64_SIGCONTEXT_BADVADDR);
996
997   /* Choice of the bottom of the sigframe is somewhat arbitrary.  */
998   trad_frame_set_id (this_cache,
999                      frame_id_build (func - SIGFRAME_CODE_OFFSET,
1000                                      func));
1001 }
1002
1003
1004 /* Initialize one of the GNU/Linux OS ABIs.  */
1005
1006 static void
1007 mips_linux_init_abi (struct gdbarch_info info,
1008                      struct gdbarch *gdbarch)
1009 {
1010   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1011   enum mips_abi abi = mips_abi (gdbarch);
1012
1013   switch (abi)
1014     {
1015       case MIPS_ABI_O32:
1016         set_gdbarch_get_longjmp_target (gdbarch,
1017                                         mips_linux_get_longjmp_target);
1018         set_solib_svr4_fetch_link_map_offsets
1019           (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1020         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_sigframe);
1021         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_rt_sigframe);
1022         break;
1023       case MIPS_ABI_N32:
1024         set_gdbarch_get_longjmp_target (gdbarch,
1025                                         mips_linux_get_longjmp_target);
1026         set_solib_svr4_fetch_link_map_offsets
1027           (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1028         set_gdbarch_long_double_bit (gdbarch, 128);
1029         /* These floatformats should probably be renamed.  MIPS uses
1030            the same 128-bit IEEE floating point format that IA-64 uses,
1031            except that the quiet/signalling NaN bit is reversed (GDB
1032            does not distinguish between quiet and signalling NaNs).  */
1033         set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
1034         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n32_rt_sigframe);
1035         break;
1036       case MIPS_ABI_N64:
1037         set_gdbarch_get_longjmp_target (gdbarch,
1038                                         mips64_linux_get_longjmp_target);
1039         set_solib_svr4_fetch_link_map_offsets
1040           (gdbarch, svr4_lp64_fetch_link_map_offsets);
1041         set_gdbarch_long_double_bit (gdbarch, 128);
1042         /* These floatformats should probably be renamed.  MIPS uses
1043            the same 128-bit IEEE floating point format that IA-64 uses,
1044            except that the quiet/signalling NaN bit is reversed (GDB
1045            does not distinguish between quiet and signalling NaNs).  */
1046         set_gdbarch_long_double_format (gdbarch, floatformats_ia64_quad);
1047         tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n64_rt_sigframe);
1048         break;
1049       default:
1050         internal_error (__FILE__, __LINE__, _("can't handle ABI"));
1051         break;
1052     }
1053
1054   set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
1055   set_gdbarch_skip_solib_resolver (gdbarch, mips_linux_skip_resolver);
1056
1057   set_gdbarch_software_single_step (gdbarch, mips_software_single_step);
1058
1059   /* Enable TLS support.  */
1060   set_gdbarch_fetch_tls_load_module_address (gdbarch,
1061                                              svr4_fetch_objfile_link_map);
1062 }
1063
1064 void
1065 _initialize_mips_linux_tdep (void)
1066 {
1067   const struct bfd_arch_info *arch_info;
1068
1069   for (arch_info = bfd_lookup_arch (bfd_arch_mips, 0);
1070        arch_info != NULL;
1071        arch_info = arch_info->next)
1072     {
1073       gdbarch_register_osabi (bfd_arch_mips, arch_info->mach,
1074                               GDB_OSABI_LINUX,
1075                               mips_linux_init_abi);
1076     }
1077
1078   deprecated_add_core_fns (&regset_core_fns);
1079 }