Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / mips-linux-tdep.c
blob5d789d245771a49b1204176030e8ee6ff72b8f48
1 /* Target-dependent code for GNU/Linux on MIPS processors.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
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 "floatformat.h"
36 #include "mips-linux-tdep.h"
38 /* Figure out where the longjmp will land.
39 We expect the first arg to be a pointer to the jmp_buf structure
40 from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
41 at. The pc is copied into PC. This routine returns 1 on
42 success. */
44 #define MIPS_LINUX_JB_ELEMENT_SIZE 4
45 #define MIPS_LINUX_JB_PC 0
47 static int
48 mips_linux_get_longjmp_target (CORE_ADDR *pc)
50 CORE_ADDR jb_addr;
51 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
53 jb_addr = read_register (MIPS_A0_REGNUM);
55 if (target_read_memory (jb_addr
56 + MIPS_LINUX_JB_PC * MIPS_LINUX_JB_ELEMENT_SIZE,
57 buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
58 return 0;
60 *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
62 return 1;
65 /* Transform the bits comprising a 32-bit register to the right size
66 for regcache_raw_supply(). This is needed when mips_isa_regsize()
67 is 8. */
69 static void
70 supply_32bit_reg (int regnum, const void *addr)
72 gdb_byte buf[MAX_REGISTER_SIZE];
73 store_signed_integer (buf, register_size (current_gdbarch, regnum),
74 extract_signed_integer (addr, 4));
75 regcache_raw_supply (current_regcache, regnum, buf);
78 /* Unpack an elf_gregset_t into GDB's register cache. */
80 void
81 mips_supply_gregset (mips_elf_gregset_t *gregsetp)
83 int regi;
84 mips_elf_greg_t *regp = *gregsetp;
85 char zerobuf[MAX_REGISTER_SIZE];
87 memset (zerobuf, 0, MAX_REGISTER_SIZE);
89 for (regi = EF_REG0; regi <= EF_REG31; regi++)
90 supply_32bit_reg ((regi - EF_REG0), (char *)(regp + regi));
92 supply_32bit_reg (mips_regnum (current_gdbarch)->lo,
93 (char *)(regp + EF_LO));
94 supply_32bit_reg (mips_regnum (current_gdbarch)->hi,
95 (char *)(regp + EF_HI));
97 supply_32bit_reg (mips_regnum (current_gdbarch)->pc,
98 (char *)(regp + EF_CP0_EPC));
99 supply_32bit_reg (mips_regnum (current_gdbarch)->badvaddr,
100 (char *)(regp + EF_CP0_BADVADDR));
101 supply_32bit_reg (MIPS_PS_REGNUM, (char *)(regp + EF_CP0_STATUS));
102 supply_32bit_reg (mips_regnum (current_gdbarch)->cause,
103 (char *)(regp + EF_CP0_CAUSE));
105 /* Fill inaccessible registers with zero. */
106 regcache_raw_supply (current_regcache, MIPS_UNUSED_REGNUM, zerobuf);
107 for (regi = MIPS_FIRST_EMBED_REGNUM;
108 regi < MIPS_LAST_EMBED_REGNUM;
109 regi++)
110 regcache_raw_supply (current_regcache, regi, zerobuf);
113 /* Pack our registers (or one register) into an elf_gregset_t. */
115 void
116 mips_fill_gregset (mips_elf_gregset_t *gregsetp, int regno)
118 int regaddr, regi;
119 mips_elf_greg_t *regp = *gregsetp;
120 void *dst;
122 if (regno == -1)
124 memset (regp, 0, sizeof (mips_elf_gregset_t));
125 for (regi = 0; regi < 32; regi++)
126 mips_fill_gregset (gregsetp, regi);
127 mips_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->lo);
128 mips_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->hi);
129 mips_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->pc);
130 mips_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->badvaddr);
131 mips_fill_gregset (gregsetp, MIPS_PS_REGNUM);
132 mips_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->cause);
134 return;
137 if (regno < 32)
139 dst = regp + regno + EF_REG0;
140 regcache_raw_collect (current_regcache, regno, dst);
141 return;
144 if (regno == mips_regnum (current_gdbarch)->lo)
145 regaddr = EF_LO;
146 else if (regno == mips_regnum (current_gdbarch)->hi)
147 regaddr = EF_HI;
148 else if (regno == mips_regnum (current_gdbarch)->pc)
149 regaddr = EF_CP0_EPC;
150 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
151 regaddr = EF_CP0_BADVADDR;
152 else if (regno == MIPS_PS_REGNUM)
153 regaddr = EF_CP0_STATUS;
154 else if (regno == mips_regnum (current_gdbarch)->cause)
155 regaddr = EF_CP0_CAUSE;
156 else
157 regaddr = -1;
159 if (regaddr != -1)
161 dst = regp + regaddr;
162 regcache_raw_collect (current_regcache, regno, dst);
166 /* Likewise, unpack an elf_fpregset_t. */
168 void
169 mips_supply_fpregset (mips_elf_fpregset_t *fpregsetp)
171 int regi;
172 char zerobuf[MAX_REGISTER_SIZE];
174 memset (zerobuf, 0, MAX_REGISTER_SIZE);
176 for (regi = 0; regi < 32; regi++)
177 regcache_raw_supply (current_regcache, FP0_REGNUM + regi,
178 (char *)(*fpregsetp + regi));
180 regcache_raw_supply (current_regcache,
181 mips_regnum (current_gdbarch)->fp_control_status,
182 (char *)(*fpregsetp + 32));
184 /* FIXME: how can we supply FCRIR? The ABI doesn't tell us. */
185 regcache_raw_supply (current_regcache,
186 mips_regnum (current_gdbarch)->fp_implementation_revision,
187 zerobuf);
190 /* Likewise, pack one or all floating point registers into an
191 elf_fpregset_t. */
193 void
194 mips_fill_fpregset (mips_elf_fpregset_t *fpregsetp, int regno)
196 char *from, *to;
198 if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
200 to = (char *) (*fpregsetp + regno - FP0_REGNUM);
201 regcache_raw_collect (current_regcache, regno, to);
203 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
205 to = (char *) (*fpregsetp + 32);
206 regcache_raw_collect (current_regcache, regno, to);
208 else if (regno == -1)
210 int regi;
212 for (regi = 0; regi < 32; regi++)
213 mips_fill_fpregset (fpregsetp, FP0_REGNUM + regi);
214 mips_fill_fpregset (fpregsetp,
215 mips_regnum (current_gdbarch)->fp_control_status);
219 /* Map gdb internal register number to ptrace ``address''.
220 These ``addresses'' are normally defined in <asm/ptrace.h>. */
222 static CORE_ADDR
223 mips_linux_register_addr (int regno, CORE_ADDR blockend)
225 int regaddr;
227 if (regno < 0 || regno >= NUM_REGS)
228 error (_("Bogon register number %d."), regno);
230 if (regno < 32)
231 regaddr = regno;
232 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
233 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
234 regaddr = FPR_BASE + (regno - mips_regnum (current_gdbarch)->fp0);
235 else if (regno == mips_regnum (current_gdbarch)->pc)
236 regaddr = PC;
237 else if (regno == mips_regnum (current_gdbarch)->cause)
238 regaddr = CAUSE;
239 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
240 regaddr = BADVADDR;
241 else if (regno == mips_regnum (current_gdbarch)->lo)
242 regaddr = MMLO;
243 else if (regno == mips_regnum (current_gdbarch)->hi)
244 regaddr = MMHI;
245 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
246 regaddr = FPC_CSR;
247 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
248 regaddr = FPC_EIR;
249 else
250 error (_("Unknowable register number %d."), regno);
252 return regaddr;
255 /* Support for 64-bit ABIs. */
257 /* Figure out where the longjmp will land.
258 We expect the first arg to be a pointer to the jmp_buf structure
259 from which we extract the pc (MIPS_LINUX_JB_PC) that we will land
260 at. The pc is copied into PC. This routine returns 1 on
261 success. */
263 /* Details about jmp_buf. */
265 #define MIPS64_LINUX_JB_PC 0
267 static int
268 mips64_linux_get_longjmp_target (CORE_ADDR *pc)
270 CORE_ADDR jb_addr;
271 void *buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
272 int element_size = TARGET_PTR_BIT == 32 ? 4 : 8;
274 jb_addr = read_register (MIPS_A0_REGNUM);
276 if (target_read_memory (jb_addr + MIPS64_LINUX_JB_PC * element_size,
277 buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
278 return 0;
280 *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
282 return 1;
285 /* Register set support functions. These operate on standard 64-bit
286 regsets, but work whether the target is 32-bit or 64-bit. A 32-bit
287 target will still use the 64-bit format for PTRACE_GETREGS. */
289 /* Supply a 64-bit register. */
291 void
292 supply_64bit_reg (int regnum, const gdb_byte *buf)
294 if (gdbarch_byte_order (current_gdbarch) == BFD_ENDIAN_BIG
295 && register_size (current_gdbarch, regnum) == 4)
296 regcache_raw_supply (current_regcache, regnum, buf + 4);
297 else
298 regcache_raw_supply (current_regcache, regnum, buf);
301 /* Unpack a 64-bit elf_gregset_t into GDB's register cache. */
303 void
304 mips64_supply_gregset (mips64_elf_gregset_t *gregsetp)
306 int regi;
307 mips64_elf_greg_t *regp = *gregsetp;
308 gdb_byte zerobuf[MAX_REGISTER_SIZE];
310 memset (zerobuf, 0, MAX_REGISTER_SIZE);
312 for (regi = MIPS64_EF_REG0; regi <= MIPS64_EF_REG31; regi++)
313 supply_64bit_reg (regi - MIPS64_EF_REG0, (gdb_byte *)(regp + regi));
315 supply_64bit_reg (mips_regnum (current_gdbarch)->lo,
316 (gdb_byte *) (regp + MIPS64_EF_LO));
317 supply_64bit_reg (mips_regnum (current_gdbarch)->hi,
318 (gdb_byte *) (regp + MIPS64_EF_HI));
320 supply_64bit_reg (mips_regnum (current_gdbarch)->pc,
321 (gdb_byte *) (regp + MIPS64_EF_CP0_EPC));
322 supply_64bit_reg (mips_regnum (current_gdbarch)->badvaddr,
323 (gdb_byte *) (regp + MIPS64_EF_CP0_BADVADDR));
324 supply_64bit_reg (MIPS_PS_REGNUM,
325 (gdb_byte *) (regp + MIPS64_EF_CP0_STATUS));
326 supply_64bit_reg (mips_regnum (current_gdbarch)->cause,
327 (gdb_byte *) (regp + MIPS64_EF_CP0_CAUSE));
329 /* Fill inaccessible registers with zero. */
330 regcache_raw_supply (current_regcache, MIPS_UNUSED_REGNUM, zerobuf);
331 for (regi = MIPS_FIRST_EMBED_REGNUM;
332 regi < MIPS_LAST_EMBED_REGNUM;
333 regi++)
334 regcache_raw_supply (current_regcache, regi, zerobuf);
337 /* Pack our registers (or one register) into a 64-bit elf_gregset_t. */
339 void
340 mips64_fill_gregset (mips64_elf_gregset_t *gregsetp, int regno)
342 int regaddr, regi;
343 mips64_elf_greg_t *regp = *gregsetp;
344 void *src, *dst;
346 if (regno == -1)
348 memset (regp, 0, sizeof (mips64_elf_gregset_t));
349 for (regi = 0; regi < 32; regi++)
350 mips64_fill_gregset (gregsetp, regi);
351 mips64_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->lo);
352 mips64_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->hi);
353 mips64_fill_gregset (gregsetp, mips_regnum (current_gdbarch)->pc);
354 mips64_fill_gregset (gregsetp,
355 mips_regnum (current_gdbarch)->badvaddr);
356 mips64_fill_gregset (gregsetp, MIPS_PS_REGNUM);
357 mips64_fill_gregset (gregsetp,
358 mips_regnum (current_gdbarch)->cause);
360 return;
363 if (regno < 32)
364 regaddr = regno + MIPS64_EF_REG0;
365 else if (regno == mips_regnum (current_gdbarch)->lo)
366 regaddr = MIPS64_EF_LO;
367 else if (regno == mips_regnum (current_gdbarch)->hi)
368 regaddr = MIPS64_EF_HI;
369 else if (regno == mips_regnum (current_gdbarch)->pc)
370 regaddr = MIPS64_EF_CP0_EPC;
371 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
372 regaddr = MIPS64_EF_CP0_BADVADDR;
373 else if (regno == MIPS_PS_REGNUM)
374 regaddr = MIPS64_EF_CP0_STATUS;
375 else if (regno == mips_regnum (current_gdbarch)->cause)
376 regaddr = MIPS64_EF_CP0_CAUSE;
377 else
378 regaddr = -1;
380 if (regaddr != -1)
382 gdb_byte buf[MAX_REGISTER_SIZE];
383 LONGEST val;
385 regcache_raw_collect (current_regcache, regno, buf);
386 val = extract_signed_integer (buf,
387 register_size (current_gdbarch, regno));
388 dst = regp + regaddr;
389 store_signed_integer (dst, 8, val);
393 /* Likewise, unpack an elf_fpregset_t. */
395 void
396 mips64_supply_fpregset (mips64_elf_fpregset_t *fpregsetp)
398 int regi;
400 /* See mips_linux_o32_sigframe_init for a description of the
401 peculiar FP register layout. */
402 if (register_size (current_gdbarch, FP0_REGNUM) == 4)
403 for (regi = 0; regi < 32; regi++)
405 gdb_byte *reg_ptr = (gdb_byte *) (*fpregsetp + (regi & ~1));
406 if ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) != (regi & 1))
407 reg_ptr += 4;
408 regcache_raw_supply (current_regcache, FP0_REGNUM + regi, reg_ptr);
410 else
411 for (regi = 0; regi < 32; regi++)
412 regcache_raw_supply (current_regcache, FP0_REGNUM + regi,
413 (char *)(*fpregsetp + regi));
415 supply_32bit_reg (mips_regnum (current_gdbarch)->fp_control_status,
416 (gdb_byte *)(*fpregsetp + 32));
418 /* The ABI doesn't tell us how to supply FCRIR, and core dumps don't
419 include it - but the result of PTRACE_GETFPREGS does. The best we
420 can do is to assume that its value is present. */
421 supply_32bit_reg (mips_regnum (current_gdbarch)->fp_implementation_revision,
422 (gdb_byte *)(*fpregsetp + 32) + 4);
425 /* Likewise, pack one or all floating point registers into an
426 elf_fpregset_t. */
428 void
429 mips64_fill_fpregset (mips64_elf_fpregset_t *fpregsetp, int regno)
431 gdb_byte *to;
433 if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
435 /* See mips_linux_o32_sigframe_init for a description of the
436 peculiar FP register layout. */
437 if (register_size (current_gdbarch, regno) == 4)
439 int regi = regno - FP0_REGNUM;
441 to = (gdb_byte *) (*fpregsetp + (regi & ~1));
442 if ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) != (regi & 1))
443 to += 4;
444 regcache_raw_collect (current_regcache, regno, to);
446 else
448 to = (gdb_byte *) (*fpregsetp + regno - FP0_REGNUM);
449 regcache_raw_collect (current_regcache, regno, to);
452 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
454 gdb_byte buf[MAX_REGISTER_SIZE];
455 LONGEST val;
457 regcache_raw_collect (current_regcache, regno, buf);
458 val = extract_signed_integer (buf,
459 register_size (current_gdbarch, regno));
460 to = (gdb_byte *) (*fpregsetp + 32);
461 store_signed_integer (to, 4, val);
463 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
465 gdb_byte buf[MAX_REGISTER_SIZE];
466 LONGEST val;
468 regcache_raw_collect (current_regcache, regno, buf);
469 val = extract_signed_integer (buf,
470 register_size (current_gdbarch, regno));
471 to = (gdb_byte *) (*fpregsetp + 32) + 4;
472 store_signed_integer (to, 4, val);
474 else if (regno == -1)
476 int regi;
478 for (regi = 0; regi < 32; regi++)
479 mips64_fill_fpregset (fpregsetp, FP0_REGNUM + regi);
480 mips64_fill_fpregset (fpregsetp,
481 mips_regnum (current_gdbarch)->fp_control_status);
482 mips64_fill_fpregset (fpregsetp, (mips_regnum (current_gdbarch)
483 ->fp_implementation_revision));
488 /* Map gdb internal register number to ptrace ``address''.
489 These ``addresses'' are normally defined in <asm/ptrace.h>. */
491 static CORE_ADDR
492 mips64_linux_register_addr (int regno, CORE_ADDR blockend)
494 int regaddr;
496 if (regno < 0 || regno >= NUM_REGS)
497 error (_("Bogon register number %d."), regno);
499 if (regno < 32)
500 regaddr = regno;
501 else if ((regno >= mips_regnum (current_gdbarch)->fp0)
502 && (regno < mips_regnum (current_gdbarch)->fp0 + 32))
503 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
504 else if (regno == mips_regnum (current_gdbarch)->pc)
505 regaddr = MIPS64_PC;
506 else if (regno == mips_regnum (current_gdbarch)->cause)
507 regaddr = MIPS64_CAUSE;
508 else if (regno == mips_regnum (current_gdbarch)->badvaddr)
509 regaddr = MIPS64_BADVADDR;
510 else if (regno == mips_regnum (current_gdbarch)->lo)
511 regaddr = MIPS64_MMLO;
512 else if (regno == mips_regnum (current_gdbarch)->hi)
513 regaddr = MIPS64_MMHI;
514 else if (regno == mips_regnum (current_gdbarch)->fp_control_status)
515 regaddr = MIPS64_FPC_CSR;
516 else if (regno == mips_regnum (current_gdbarch)->fp_implementation_revision)
517 regaddr = MIPS64_FPC_EIR;
518 else
519 error (_("Unknowable register number %d."), regno);
521 return regaddr;
524 /* Use a local version of this function to get the correct types for
525 regsets, until multi-arch core support is ready. */
527 static void
528 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
529 int which, CORE_ADDR reg_addr)
531 mips_elf_gregset_t gregset;
532 mips_elf_fpregset_t fpregset;
533 mips64_elf_gregset_t gregset64;
534 mips64_elf_fpregset_t fpregset64;
536 if (which == 0)
538 if (core_reg_size == sizeof (gregset))
540 memcpy ((char *) &gregset, core_reg_sect, sizeof (gregset));
541 mips_supply_gregset (&gregset);
543 else if (core_reg_size == sizeof (gregset64))
545 memcpy ((char *) &gregset64, core_reg_sect, sizeof (gregset64));
546 mips64_supply_gregset (&gregset64);
548 else
550 warning (_("wrong size gregset struct in core file"));
553 else if (which == 2)
555 if (core_reg_size == sizeof (fpregset))
557 memcpy ((char *) &fpregset, core_reg_sect, sizeof (fpregset));
558 mips_supply_fpregset (&fpregset);
560 else if (core_reg_size == sizeof (fpregset64))
562 memcpy ((char *) &fpregset64, core_reg_sect,
563 sizeof (fpregset64));
564 mips64_supply_fpregset (&fpregset64);
566 else
568 warning (_("wrong size fpregset struct in core file"));
573 /* Register that we are able to handle ELF file formats using standard
574 procfs "regset" structures. */
576 static struct core_fns regset_core_fns =
578 bfd_target_elf_flavour, /* core_flavour */
579 default_check_format, /* check_format */
580 default_core_sniffer, /* core_sniffer */
581 fetch_core_registers, /* core_read_registers */
582 NULL /* next */
585 /* Handle for obtaining pointer to the current register_addr()
586 function for a given architecture. */
587 static struct gdbarch_data *register_addr_data;
589 CORE_ADDR
590 register_addr (int regno, CORE_ADDR blockend)
592 CORE_ADDR (*register_addr_ptr) (int, CORE_ADDR) =
593 gdbarch_data (current_gdbarch, register_addr_data);
595 gdb_assert (register_addr_ptr != 0);
597 return register_addr_ptr (regno, blockend);
600 static void
601 set_mips_linux_register_addr (struct gdbarch *gdbarch,
602 CORE_ADDR (*register_addr_ptr) (int,
603 CORE_ADDR))
605 deprecated_set_gdbarch_data (gdbarch, register_addr_data,
606 register_addr_ptr);
609 static void *
610 init_register_addr_data (struct gdbarch *gdbarch)
612 return 0;
615 /* Check the code at PC for a dynamic linker lazy resolution stub.
616 Because they aren't in the .plt section, we pattern-match on the
617 code generated by GNU ld. They look like this:
619 lw t9,0x8010(gp)
620 addu t7,ra
621 jalr t9,ra
622 addiu t8,zero,INDEX
624 (with the appropriate doubleword instructions for N64). Also
625 return the dynamic symbol index used in the last instruction. */
627 static int
628 mips_linux_in_dynsym_stub (CORE_ADDR pc, char *name)
630 unsigned char buf[28], *p;
631 ULONGEST insn, insn1;
632 int n64 = (mips_abi (current_gdbarch) == MIPS_ABI_N64);
634 read_memory (pc - 12, buf, 28);
636 if (n64)
638 /* ld t9,0x8010(gp) */
639 insn1 = 0xdf998010;
641 else
643 /* lw t9,0x8010(gp) */
644 insn1 = 0x8f998010;
647 p = buf + 12;
648 while (p >= buf)
650 insn = extract_unsigned_integer (p, 4);
651 if (insn == insn1)
652 break;
653 p -= 4;
655 if (p < buf)
656 return 0;
658 insn = extract_unsigned_integer (p + 4, 4);
659 if (n64)
661 /* daddu t7,ra */
662 if (insn != 0x03e0782d)
663 return 0;
665 else
667 /* addu t7,ra */
668 if (insn != 0x03e07821)
669 return 0;
672 insn = extract_unsigned_integer (p + 8, 4);
673 /* jalr t9,ra */
674 if (insn != 0x0320f809)
675 return 0;
677 insn = extract_unsigned_integer (p + 12, 4);
678 if (n64)
680 /* daddiu t8,zero,0 */
681 if ((insn & 0xffff0000) != 0x64180000)
682 return 0;
684 else
686 /* addiu t8,zero,0 */
687 if ((insn & 0xffff0000) != 0x24180000)
688 return 0;
691 return (insn & 0xffff);
694 /* Return non-zero iff PC belongs to the dynamic linker resolution
695 code or to a stub. */
698 mips_linux_in_dynsym_resolve_code (CORE_ADDR pc)
700 /* Check whether PC is in the dynamic linker. This also checks
701 whether it is in the .plt section, which MIPS does not use. */
702 if (in_solib_dynsym_resolve_code (pc))
703 return 1;
705 /* Pattern match for the stub. It would be nice if there were a
706 more efficient way to avoid this check. */
707 if (mips_linux_in_dynsym_stub (pc, NULL))
708 return 1;
710 return 0;
713 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c,
714 and glibc_skip_solib_resolver in glibc-tdep.c. The normal glibc
715 implementation of this triggers at "fixup" from the same objfile as
716 "_dl_runtime_resolve"; MIPS GNU/Linux can trigger at
717 "__dl_runtime_resolve" directly. An unresolved PLT entry will
718 point to _dl_runtime_resolve, which will first call
719 __dl_runtime_resolve, and then pass control to the resolved
720 function. */
722 static CORE_ADDR
723 mips_linux_skip_resolver (struct gdbarch *gdbarch, CORE_ADDR pc)
725 struct minimal_symbol *resolver;
727 resolver = lookup_minimal_symbol ("__dl_runtime_resolve", NULL, NULL);
729 if (resolver && SYMBOL_VALUE_ADDRESS (resolver) == pc)
730 return frame_pc_unwind (get_current_frame ());
732 return 0;
735 /* Signal trampoline support. There are four supported layouts for a
736 signal frame: o32 sigframe, o32 rt_sigframe, n32 rt_sigframe, and
737 n64 rt_sigframe. We handle them all independently; not the most
738 efficient way, but simplest. First, declare all the unwinders. */
740 static void mips_linux_o32_sigframe_init (const struct tramp_frame *self,
741 struct frame_info *next_frame,
742 struct trad_frame_cache *this_cache,
743 CORE_ADDR func);
745 static void mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
746 struct frame_info *next_frame,
747 struct trad_frame_cache *this_cache,
748 CORE_ADDR func);
750 #define MIPS_NR_LINUX 4000
751 #define MIPS_NR_N64_LINUX 5000
752 #define MIPS_NR_N32_LINUX 6000
754 #define MIPS_NR_sigreturn MIPS_NR_LINUX + 119
755 #define MIPS_NR_rt_sigreturn MIPS_NR_LINUX + 193
756 #define MIPS_NR_N64_rt_sigreturn MIPS_NR_N64_LINUX + 211
757 #define MIPS_NR_N32_rt_sigreturn MIPS_NR_N32_LINUX + 211
759 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + MIPS_NR_sigreturn
760 #define MIPS_INST_LI_V0_RT_SIGRETURN 0x24020000 + MIPS_NR_rt_sigreturn
761 #define MIPS_INST_LI_V0_N64_RT_SIGRETURN 0x24020000 + MIPS_NR_N64_rt_sigreturn
762 #define MIPS_INST_LI_V0_N32_RT_SIGRETURN 0x24020000 + MIPS_NR_N32_rt_sigreturn
763 #define MIPS_INST_SYSCALL 0x0000000c
765 static const struct tramp_frame mips_linux_o32_sigframe = {
766 SIGTRAMP_FRAME,
769 { MIPS_INST_LI_V0_SIGRETURN, -1 },
770 { MIPS_INST_SYSCALL, -1 },
771 { TRAMP_SENTINEL_INSN, -1 }
773 mips_linux_o32_sigframe_init
776 static const struct tramp_frame mips_linux_o32_rt_sigframe = {
777 SIGTRAMP_FRAME,
780 { MIPS_INST_LI_V0_RT_SIGRETURN, -1 },
781 { MIPS_INST_SYSCALL, -1 },
782 { TRAMP_SENTINEL_INSN, -1 } },
783 mips_linux_o32_sigframe_init
786 static const struct tramp_frame mips_linux_n32_rt_sigframe = {
787 SIGTRAMP_FRAME,
790 { MIPS_INST_LI_V0_N32_RT_SIGRETURN, -1 },
791 { MIPS_INST_SYSCALL, -1 },
792 { TRAMP_SENTINEL_INSN, -1 }
794 mips_linux_n32n64_sigframe_init
797 static const struct tramp_frame mips_linux_n64_rt_sigframe = {
798 SIGTRAMP_FRAME,
800 { MIPS_INST_LI_V0_N64_RT_SIGRETURN,
801 MIPS_INST_SYSCALL,
802 TRAMP_SENTINEL_INSN },
803 mips_linux_n32n64_sigframe_init
806 /* *INDENT-OFF* */
807 /* The unwinder for o32 signal frames. The legacy structures look
808 like this:
810 struct sigframe {
811 u32 sf_ass[4]; [argument save space for o32]
812 u32 sf_code[2]; [signal trampoline]
813 struct sigcontext sf_sc;
814 sigset_t sf_mask;
817 struct sigcontext {
818 unsigned int sc_regmask; [Unused]
819 unsigned int sc_status;
820 unsigned long long sc_pc;
821 unsigned long long sc_regs[32];
822 unsigned long long sc_fpregs[32];
823 unsigned int sc_ownedfp;
824 unsigned int sc_fpc_csr;
825 unsigned int sc_fpc_eir; [Unused]
826 unsigned int sc_used_math;
827 unsigned int sc_ssflags; [Unused]
828 [Alignment hole of four bytes]
829 unsigned long long sc_mdhi;
830 unsigned long long sc_mdlo;
832 unsigned int sc_cause; [Unused]
833 unsigned int sc_badvaddr; [Unused]
835 unsigned long sc_sigset[4]; [kernel's sigset_t]
838 The RT signal frames look like this:
840 struct rt_sigframe {
841 u32 rs_ass[4]; [argument save space for o32]
842 u32 rs_code[2] [signal trampoline]
843 struct siginfo rs_info;
844 struct ucontext rs_uc;
847 struct ucontext {
848 unsigned long uc_flags;
849 struct ucontext *uc_link;
850 stack_t uc_stack;
851 [Alignment hole of four bytes]
852 struct sigcontext uc_mcontext;
853 sigset_t uc_sigmask;
854 }; */
855 /* *INDENT-ON* */
857 #define SIGFRAME_CODE_OFFSET (4 * 4)
858 #define SIGFRAME_SIGCONTEXT_OFFSET (6 * 4)
860 #define RTSIGFRAME_SIGINFO_SIZE 128
861 #define STACK_T_SIZE (3 * 4)
862 #define UCONTEXT_SIGCONTEXT_OFFSET (2 * 4 + STACK_T_SIZE + 4)
863 #define RTSIGFRAME_SIGCONTEXT_OFFSET (SIGFRAME_SIGCONTEXT_OFFSET \
864 + RTSIGFRAME_SIGINFO_SIZE \
865 + UCONTEXT_SIGCONTEXT_OFFSET)
867 #define SIGCONTEXT_PC (1 * 8)
868 #define SIGCONTEXT_REGS (2 * 8)
869 #define SIGCONTEXT_FPREGS (34 * 8)
870 #define SIGCONTEXT_FPCSR (66 * 8 + 4)
871 #define SIGCONTEXT_HI (69 * 8)
872 #define SIGCONTEXT_LO (70 * 8)
873 #define SIGCONTEXT_CAUSE (71 * 8 + 0)
874 #define SIGCONTEXT_BADVADDR (71 * 8 + 4)
876 #define SIGCONTEXT_REG_SIZE 8
878 static void
879 mips_linux_o32_sigframe_init (const struct tramp_frame *self,
880 struct frame_info *next_frame,
881 struct trad_frame_cache *this_cache,
882 CORE_ADDR func)
884 int ireg, reg_position;
885 CORE_ADDR sigcontext_base = func - SIGFRAME_CODE_OFFSET;
886 const struct mips_regnum *regs = mips_regnum (current_gdbarch);
887 CORE_ADDR regs_base;
889 if (self == &mips_linux_o32_sigframe)
890 sigcontext_base += SIGFRAME_SIGCONTEXT_OFFSET;
891 else
892 sigcontext_base += RTSIGFRAME_SIGCONTEXT_OFFSET;
894 /* I'm not proud of this hack. Eventually we will have the
895 infrastructure to indicate the size of saved registers on a
896 per-frame basis, but right now we don't; the kernel saves eight
897 bytes but we only want four. Use regs_base to access any
898 64-bit fields. */
899 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
900 regs_base = sigcontext_base + 4;
901 else
902 regs_base = sigcontext_base;
904 #if 0
905 trad_frame_set_reg_addr (this_cache, ORIG_ZERO_REGNUM + NUM_REGS,
906 regs_base + SIGCONTEXT_REGS);
907 #endif
909 for (ireg = 1; ireg < 32; ireg++)
910 trad_frame_set_reg_addr (this_cache,
911 ireg + MIPS_ZERO_REGNUM + NUM_REGS,
912 regs_base + SIGCONTEXT_REGS
913 + ireg * SIGCONTEXT_REG_SIZE);
915 /* The way that floating point registers are saved, unfortunately,
916 depends on the architecture the kernel is built for. For the r3000 and
917 tx39, four bytes of each register are at the beginning of each of the
918 32 eight byte slots. For everything else, the registers are saved
919 using double precision; only the even-numbered slots are initialized,
920 and the high bits are the odd-numbered register. Assume the latter
921 layout, since we can't tell, and it's much more common. Which bits are
922 the "high" bits depends on endianness. */
923 for (ireg = 0; ireg < 32; ireg++)
924 if ((TARGET_BYTE_ORDER == BFD_ENDIAN_BIG) != (ireg & 1))
925 trad_frame_set_reg_addr (this_cache, ireg + regs->fp0 + NUM_REGS,
926 sigcontext_base + SIGCONTEXT_FPREGS + 4
927 + (ireg & ~1) * SIGCONTEXT_REG_SIZE);
928 else
929 trad_frame_set_reg_addr (this_cache, ireg + regs->fp0 + NUM_REGS,
930 sigcontext_base + SIGCONTEXT_FPREGS
931 + (ireg & ~1) * SIGCONTEXT_REG_SIZE);
933 trad_frame_set_reg_addr (this_cache, regs->pc + NUM_REGS,
934 regs_base + SIGCONTEXT_PC);
936 trad_frame_set_reg_addr (this_cache,
937 regs->fp_control_status + NUM_REGS,
938 sigcontext_base + SIGCONTEXT_FPCSR);
939 trad_frame_set_reg_addr (this_cache, regs->hi + NUM_REGS,
940 regs_base + SIGCONTEXT_HI);
941 trad_frame_set_reg_addr (this_cache, regs->lo + NUM_REGS,
942 regs_base + SIGCONTEXT_LO);
943 trad_frame_set_reg_addr (this_cache, regs->cause + NUM_REGS,
944 sigcontext_base + SIGCONTEXT_CAUSE);
945 trad_frame_set_reg_addr (this_cache, regs->badvaddr + NUM_REGS,
946 sigcontext_base + SIGCONTEXT_BADVADDR);
948 /* Choice of the bottom of the sigframe is somewhat arbitrary. */
949 trad_frame_set_id (this_cache,
950 frame_id_build (func - SIGFRAME_CODE_OFFSET,
951 func));
954 /* *INDENT-OFF* */
955 /* For N32/N64 things look different. There is no non-rt signal frame.
957 struct rt_sigframe_n32 {
958 u32 rs_ass[4]; [ argument save space for o32 ]
959 u32 rs_code[2]; [ signal trampoline ]
960 struct siginfo rs_info;
961 struct ucontextn32 rs_uc;
964 struct ucontextn32 {
965 u32 uc_flags;
966 s32 uc_link;
967 stack32_t uc_stack;
968 struct sigcontext uc_mcontext;
969 sigset_t uc_sigmask; [ mask last for extensibility ]
972 struct rt_sigframe_n32 {
973 u32 rs_ass[4]; [ argument save space for o32 ]
974 u32 rs_code[2]; [ signal trampoline ]
975 struct siginfo rs_info;
976 struct ucontext rs_uc;
979 struct ucontext {
980 unsigned long uc_flags;
981 struct ucontext *uc_link;
982 stack_t uc_stack;
983 struct sigcontext uc_mcontext;
984 sigset_t uc_sigmask; [ mask last for extensibility ]
987 And the sigcontext is different (this is for both n32 and n64):
989 struct sigcontext {
990 unsigned long long sc_regs[32];
991 unsigned long long sc_fpregs[32];
992 unsigned long long sc_mdhi;
993 unsigned long long sc_mdlo;
994 unsigned long long sc_pc;
995 unsigned int sc_status;
996 unsigned int sc_fpc_csr;
997 unsigned int sc_fpc_eir;
998 unsigned int sc_used_math;
999 unsigned int sc_cause;
1000 unsigned int sc_badvaddr;
1001 }; */
1002 /* *INDENT-ON* */
1004 #define N32_STACK_T_SIZE STACK_T_SIZE
1005 #define N64_STACK_T_SIZE (2 * 8 + 4)
1006 #define N32_UCONTEXT_SIGCONTEXT_OFFSET (2 * 4 + N32_STACK_T_SIZE + 4)
1007 #define N64_UCONTEXT_SIGCONTEXT_OFFSET (2 * 8 + N64_STACK_T_SIZE + 4)
1008 #define N32_SIGFRAME_SIGCONTEXT_OFFSET (SIGFRAME_SIGCONTEXT_OFFSET \
1009 + RTSIGFRAME_SIGINFO_SIZE \
1010 + N32_UCONTEXT_SIGCONTEXT_OFFSET)
1011 #define N64_SIGFRAME_SIGCONTEXT_OFFSET (SIGFRAME_SIGCONTEXT_OFFSET \
1012 + RTSIGFRAME_SIGINFO_SIZE \
1013 + N64_UCONTEXT_SIGCONTEXT_OFFSET)
1015 #define N64_SIGCONTEXT_REGS (0 * 8)
1016 #define N64_SIGCONTEXT_FPREGS (32 * 8)
1017 #define N64_SIGCONTEXT_HI (64 * 8)
1018 #define N64_SIGCONTEXT_LO (65 * 8)
1019 #define N64_SIGCONTEXT_PC (66 * 8)
1020 #define N64_SIGCONTEXT_FPCSR (67 * 8 + 1 * 4)
1021 #define N64_SIGCONTEXT_FIR (67 * 8 + 2 * 4)
1022 #define N64_SIGCONTEXT_CAUSE (67 * 8 + 4 * 4)
1023 #define N64_SIGCONTEXT_BADVADDR (67 * 8 + 5 * 4)
1025 #define N64_SIGCONTEXT_REG_SIZE 8
1027 static void
1028 mips_linux_n32n64_sigframe_init (const struct tramp_frame *self,
1029 struct frame_info *next_frame,
1030 struct trad_frame_cache *this_cache,
1031 CORE_ADDR func)
1033 int ireg, reg_position;
1034 CORE_ADDR sigcontext_base = func - SIGFRAME_CODE_OFFSET;
1035 const struct mips_regnum *regs = mips_regnum (current_gdbarch);
1037 if (self == &mips_linux_n32_rt_sigframe)
1038 sigcontext_base += N32_SIGFRAME_SIGCONTEXT_OFFSET;
1039 else
1040 sigcontext_base += N64_SIGFRAME_SIGCONTEXT_OFFSET;
1042 #if 0
1043 trad_frame_set_reg_addr (this_cache, ORIG_ZERO_REGNUM + NUM_REGS,
1044 sigcontext_base + N64_SIGCONTEXT_REGS);
1045 #endif
1047 for (ireg = 1; ireg < 32; ireg++)
1048 trad_frame_set_reg_addr (this_cache,
1049 ireg + MIPS_ZERO_REGNUM + NUM_REGS,
1050 sigcontext_base + N64_SIGCONTEXT_REGS
1051 + ireg * N64_SIGCONTEXT_REG_SIZE);
1053 for (ireg = 0; ireg < 32; ireg++)
1054 trad_frame_set_reg_addr (this_cache, ireg + regs->fp0 + NUM_REGS,
1055 sigcontext_base + N64_SIGCONTEXT_FPREGS
1056 + ireg * N64_SIGCONTEXT_REG_SIZE);
1058 trad_frame_set_reg_addr (this_cache, regs->pc + NUM_REGS,
1059 sigcontext_base + N64_SIGCONTEXT_PC);
1061 trad_frame_set_reg_addr (this_cache,
1062 regs->fp_control_status + NUM_REGS,
1063 sigcontext_base + N64_SIGCONTEXT_FPCSR);
1064 trad_frame_set_reg_addr (this_cache, regs->hi + NUM_REGS,
1065 sigcontext_base + N64_SIGCONTEXT_HI);
1066 trad_frame_set_reg_addr (this_cache, regs->lo + NUM_REGS,
1067 sigcontext_base + N64_SIGCONTEXT_LO);
1068 trad_frame_set_reg_addr (this_cache, regs->cause + NUM_REGS,
1069 sigcontext_base + N64_SIGCONTEXT_CAUSE);
1070 trad_frame_set_reg_addr (this_cache, regs->badvaddr + NUM_REGS,
1071 sigcontext_base + N64_SIGCONTEXT_BADVADDR);
1073 /* Choice of the bottom of the sigframe is somewhat arbitrary. */
1074 trad_frame_set_id (this_cache,
1075 frame_id_build (func - SIGFRAME_CODE_OFFSET,
1076 func));
1079 /* Wrapper functions. These are only used by libthread_db. */
1081 void
1082 supply_gregset (mips_elf_gregset_t *gregsetp)
1084 if (mips_isa_regsize (current_gdbarch) == 4)
1085 mips_supply_gregset (gregsetp);
1086 else
1087 mips64_supply_gregset ((void *) gregsetp);
1090 void
1091 fill_gregset (mips_elf_gregset_t *gregsetp, int regno)
1093 if (mips_isa_regsize (current_gdbarch) == 4)
1094 mips_fill_gregset (gregsetp, regno);
1095 else
1096 mips64_fill_gregset ((void *) gregsetp, regno);
1099 /* Likewise, unpack an elf_fpregset_t. */
1101 void
1102 supply_fpregset (mips_elf_fpregset_t *fpregsetp)
1104 if (mips_isa_regsize (current_gdbarch) == 4)
1105 mips_supply_fpregset (fpregsetp);
1106 else
1107 mips64_supply_fpregset ((void *) fpregsetp);
1110 /* Likewise, pack one or all floating point registers into an
1111 elf_fpregset_t. */
1113 void
1114 fill_fpregset (mips_elf_fpregset_t *fpregsetp, int regno)
1116 if (mips_isa_regsize (current_gdbarch) == 4)
1117 mips_fill_fpregset (fpregsetp, regno);
1118 else
1119 mips64_fill_fpregset ((void *) fpregsetp, regno);
1122 /* Initialize one of the GNU/Linux OS ABIs. */
1124 static void
1125 mips_linux_init_abi (struct gdbarch_info info,
1126 struct gdbarch *gdbarch)
1128 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1129 enum mips_abi abi = mips_abi (gdbarch);
1131 switch (abi)
1133 case MIPS_ABI_O32:
1134 set_gdbarch_get_longjmp_target (gdbarch,
1135 mips_linux_get_longjmp_target);
1136 set_solib_svr4_fetch_link_map_offsets
1137 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1138 set_mips_linux_register_addr (gdbarch, mips_linux_register_addr);
1139 tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_sigframe);
1140 tramp_frame_prepend_unwinder (gdbarch, &mips_linux_o32_rt_sigframe);
1141 break;
1142 case MIPS_ABI_N32:
1143 set_gdbarch_get_longjmp_target (gdbarch,
1144 mips_linux_get_longjmp_target);
1145 set_solib_svr4_fetch_link_map_offsets
1146 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1147 set_mips_linux_register_addr (gdbarch, mips64_linux_register_addr);
1148 set_gdbarch_long_double_bit (gdbarch, 128);
1149 /* These floatformats should probably be renamed. MIPS uses
1150 the same 128-bit IEEE floating point format that IA-64 uses,
1151 except that the quiet/signalling NaN bit is reversed (GDB
1152 does not distinguish between quiet and signalling NaNs). */
1153 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1154 set_gdbarch_long_double_format (gdbarch, &floatformat_ia64_quad_big);
1155 else
1156 set_gdbarch_long_double_format (gdbarch, &floatformat_ia64_quad_little);
1157 tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n32_rt_sigframe);
1158 break;
1159 case MIPS_ABI_N64:
1160 set_gdbarch_get_longjmp_target (gdbarch,
1161 mips64_linux_get_longjmp_target);
1162 set_solib_svr4_fetch_link_map_offsets
1163 (gdbarch, svr4_lp64_fetch_link_map_offsets);
1164 set_mips_linux_register_addr (gdbarch, mips64_linux_register_addr);
1165 set_gdbarch_long_double_bit (gdbarch, 128);
1166 /* These floatformats should probably be renamed. MIPS uses
1167 the same 128-bit IEEE floating point format that IA-64 uses,
1168 except that the quiet/signalling NaN bit is reversed (GDB
1169 does not distinguish between quiet and signalling NaNs). */
1170 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1171 set_gdbarch_long_double_format (gdbarch, &floatformat_ia64_quad_big);
1172 else
1173 set_gdbarch_long_double_format (gdbarch, &floatformat_ia64_quad_little);
1174 tramp_frame_prepend_unwinder (gdbarch, &mips_linux_n64_rt_sigframe);
1175 break;
1176 default:
1177 internal_error (__FILE__, __LINE__, _("can't handle ABI"));
1178 break;
1181 set_gdbarch_skip_solib_resolver (gdbarch, mips_linux_skip_resolver);
1183 set_gdbarch_software_single_step (gdbarch, mips_software_single_step);
1185 /* Enable TLS support. */
1186 set_gdbarch_fetch_tls_load_module_address (gdbarch,
1187 svr4_fetch_objfile_link_map);
1190 void
1191 _initialize_mips_linux_tdep (void)
1193 const struct bfd_arch_info *arch_info;
1195 register_addr_data =
1196 gdbarch_data_register_post_init (init_register_addr_data);
1198 for (arch_info = bfd_lookup_arch (bfd_arch_mips, 0);
1199 arch_info != NULL;
1200 arch_info = arch_info->next)
1202 gdbarch_register_osabi (bfd_arch_mips, arch_info->mach,
1203 GDB_OSABI_LINUX,
1204 mips_linux_init_abi);
1207 deprecated_add_core_fns (&regset_core_fns);