1 /* Target-dependent code for GNU/Linux on MIPS processors.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "solib-svr4.h"
27 #include "mips-tdep.h"
28 #include "gdb_string.h"
29 #include "gdb_assert.h"
31 /* Copied from <asm/elf.h>. */
35 typedef unsigned char elf_greg_t[4];
36 typedef elf_greg_t elf_gregset_t[ELF_NGREG];
38 typedef unsigned char elf_fpreg_t[8];
39 typedef elf_fpreg_t elf_fpregset_t[ELF_NFPREG];
41 /* 0 - 31 are integer registers, 32 - 63 are fp registers. */
56 #define EF_CP0_BADVADDR 41
57 #define EF_CP0_STATUS 42
58 #define EF_CP0_CAUSE 43
62 /* Figure out where the longjmp will land.
63 We expect the first arg to be a pointer to the jmp_buf structure from
64 which we extract the pc (MIPS_LINUX_JB_PC) that we will land at. The pc
65 is copied into PC. This routine returns 1 on success. */
67 #define MIPS_LINUX_JB_ELEMENT_SIZE 4
68 #define MIPS_LINUX_JB_PC 0
71 mips_linux_get_longjmp_target (CORE_ADDR *pc)
74 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
76 jb_addr = read_register (A0_REGNUM);
78 if (target_read_memory (jb_addr
79 + MIPS_LINUX_JB_PC * MIPS_LINUX_JB_ELEMENT_SIZE,
80 buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
83 *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
88 /* Transform the bits comprising a 32-bit register to the right
89 size for supply_register(). This is needed when MIPS_REGSIZE is 8. */
92 supply_32bit_reg (int regnum, const void *addr)
94 char buf[MAX_REGISTER_SIZE];
95 store_signed_integer (buf, REGISTER_RAW_SIZE (regnum),
96 extract_signed_integer (addr, 4));
97 supply_register (regnum, buf);
100 /* Unpack an elf_gregset_t into GDB's register cache. */
103 supply_gregset (elf_gregset_t *gregsetp)
106 elf_greg_t *regp = *gregsetp;
107 char zerobuf[MAX_REGISTER_SIZE];
109 memset (zerobuf, 0, MAX_REGISTER_SIZE);
111 for (regi = EF_REG0; regi <= EF_REG31; regi++)
112 supply_32bit_reg ((regi - EF_REG0), (char *)(regp + regi));
114 supply_32bit_reg (LO_REGNUM, (char *)(regp + EF_LO));
115 supply_32bit_reg (HI_REGNUM, (char *)(regp + EF_HI));
117 supply_32bit_reg (PC_REGNUM, (char *)(regp + EF_CP0_EPC));
118 supply_32bit_reg (BADVADDR_REGNUM, (char *)(regp + EF_CP0_BADVADDR));
119 supply_32bit_reg (PS_REGNUM, (char *)(regp + EF_CP0_STATUS));
120 supply_32bit_reg (CAUSE_REGNUM, (char *)(regp + EF_CP0_CAUSE));
122 /* Fill inaccessible registers with zero. */
123 supply_register (UNUSED_REGNUM, zerobuf);
124 for (regi = FIRST_EMBED_REGNUM; regi < LAST_EMBED_REGNUM; regi++)
125 supply_register (regi, zerobuf);
128 /* Pack our registers (or one register) into an elf_gregset_t. */
131 fill_gregset (elf_gregset_t *gregsetp, int regno)
134 elf_greg_t *regp = *gregsetp;
139 memset (regp, 0, sizeof (elf_gregset_t));
140 for (regi = 0; regi < 32; regi++)
141 fill_gregset (gregsetp, regi);
142 fill_gregset (gregsetp, LO_REGNUM);
143 fill_gregset (gregsetp, HI_REGNUM);
144 fill_gregset (gregsetp, PC_REGNUM);
145 fill_gregset (gregsetp, BADVADDR_REGNUM);
146 fill_gregset (gregsetp, PS_REGNUM);
147 fill_gregset (gregsetp, CAUSE_REGNUM);
154 dst = regp + regno + EF_REG0;
155 regcache_collect (regno, dst);
169 regaddr = EF_CP0_EPC;
171 case BADVADDR_REGNUM:
172 regaddr = EF_CP0_BADVADDR;
175 regaddr = EF_CP0_STATUS;
178 regaddr = EF_CP0_CAUSE;
184 dst = regp + regaddr;
185 regcache_collect (regno, dst);
189 /* Likewise, unpack an elf_fpregset_t. */
192 supply_fpregset (elf_fpregset_t *fpregsetp)
195 char zerobuf[MAX_REGISTER_SIZE];
197 memset (zerobuf, 0, MAX_REGISTER_SIZE);
199 for (regi = 0; regi < 32; regi++)
200 supply_register (FP0_REGNUM + regi,
201 (char *)(*fpregsetp + regi));
203 supply_register (FCRCS_REGNUM, (char *)(*fpregsetp + 32));
205 /* FIXME: how can we supply FCRIR_REGNUM? The ABI doesn't tell us. */
206 supply_register (FCRIR_REGNUM, zerobuf);
209 /* Likewise, pack one or all floating point registers into an
213 fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
217 if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
219 from = (char *) &deprecated_registers[REGISTER_BYTE (regno)];
220 to = (char *) (*fpregsetp + regno - FP0_REGNUM);
221 memcpy (to, from, REGISTER_RAW_SIZE (regno - FP0_REGNUM));
223 else if (regno == FCRCS_REGNUM)
225 from = (char *) &deprecated_registers[REGISTER_BYTE (regno)];
226 to = (char *) (*fpregsetp + 32);
227 memcpy (to, from, REGISTER_RAW_SIZE (regno));
229 else if (regno == -1)
233 for (regi = 0; regi < 32; regi++)
234 fill_fpregset (fpregsetp, FP0_REGNUM + regi);
235 fill_fpregset(fpregsetp, FCRCS_REGNUM);
239 /* Map gdb internal register number to ptrace ``address''.
240 These ``addresses'' are normally defined in <asm/ptrace.h>. */
243 mips_linux_register_addr (int regno, CORE_ADDR blockend)
247 if (regno < 0 || regno >= NUM_REGS)
248 error ("Bogon register number %d.", regno);
252 else if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
253 regaddr = FPR_BASE + (regno - FP0_REGNUM);
254 else if (regno == PC_REGNUM)
256 else if (regno == CAUSE_REGNUM)
258 else if (regno == BADVADDR_REGNUM)
260 else if (regno == LO_REGNUM)
262 else if (regno == HI_REGNUM)
264 else if (regno == FCRCS_REGNUM)
266 else if (regno == FCRIR_REGNUM)
269 error ("Unknowable register number %d.", regno);
275 /* Fetch (and possibly build) an appropriate link_map_offsets
276 structure for native GNU/Linux MIPS targets using the struct offsets
277 defined in link.h (but without actual reference to that file).
279 This makes it possible to access GNU/Linux MIPS shared libraries from a
280 GDB that was built on a different host platform (for cross debugging). */
282 static struct link_map_offsets *
283 mips_linux_svr4_fetch_link_map_offsets (void)
285 static struct link_map_offsets lmo;
286 static struct link_map_offsets *lmp = NULL;
292 lmo.r_debug_size = 8; /* The actual size is 20 bytes, but
293 this is all we need. */
294 lmo.r_map_offset = 4;
297 lmo.link_map_size = 20;
299 lmo.l_addr_offset = 0;
302 lmo.l_name_offset = 4;
305 lmo.l_next_offset = 12;
308 lmo.l_prev_offset = 16;
315 /* Support for 64-bit ABIs. */
317 /* Copied from <asm/elf.h>. */
318 #define MIPS64_ELF_NGREG 45
319 #define MIPS64_ELF_NFPREG 33
321 typedef unsigned char mips64_elf_greg_t[8];
322 typedef mips64_elf_greg_t mips64_elf_gregset_t[MIPS64_ELF_NGREG];
324 typedef unsigned char mips64_elf_fpreg_t[8];
325 typedef mips64_elf_fpreg_t mips64_elf_fpregset_t[MIPS64_ELF_NFPREG];
327 /* 0 - 31 are integer registers, 32 - 63 are fp registers. */
328 #define MIPS64_FPR_BASE 32
330 #define MIPS64_CAUSE 65
331 #define MIPS64_BADVADDR 66
332 #define MIPS64_MMHI 67
333 #define MIPS64_MMLO 68
334 #define MIPS64_FPC_CSR 69
335 #define MIPS64_FPC_EIR 70
337 #define MIPS64_EF_REG0 0
338 #define MIPS64_EF_REG31 31
339 #define MIPS64_EF_LO 32
340 #define MIPS64_EF_HI 33
341 #define MIPS64_EF_CP0_EPC 34
342 #define MIPS64_EF_CP0_BADVADDR 35
343 #define MIPS64_EF_CP0_STATUS 36
344 #define MIPS64_EF_CP0_CAUSE 37
346 #define MIPS64_EF_SIZE 304
348 /* Figure out where the longjmp will land.
349 We expect the first arg to be a pointer to the jmp_buf structure from
350 which we extract the pc (MIPS_LINUX_JB_PC) that we will land at. The pc
351 is copied into PC. This routine returns 1 on success. */
353 /* Details about jmp_buf. */
355 #define MIPS64_LINUX_JB_PC 0
358 mips64_linux_get_longjmp_target (CORE_ADDR *pc)
361 void *buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
362 int element_size = TARGET_PTR_BIT == 32 ? 4 : 8;
364 jb_addr = read_register (A0_REGNUM);
366 if (target_read_memory (jb_addr + MIPS64_LINUX_JB_PC * element_size,
367 buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
370 *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
375 /* Unpack an elf_gregset_t into GDB's register cache. */
378 mips64_supply_gregset (mips64_elf_gregset_t *gregsetp)
381 mips64_elf_greg_t *regp = *gregsetp;
382 char zerobuf[MAX_REGISTER_SIZE];
384 memset (zerobuf, 0, MAX_REGISTER_SIZE);
386 for (regi = MIPS64_EF_REG0; regi <= MIPS64_EF_REG31; regi++)
387 supply_register ((regi - MIPS64_EF_REG0), (char *)(regp + regi));
389 supply_register (LO_REGNUM, (char *)(regp + MIPS64_EF_LO));
390 supply_register (HI_REGNUM, (char *)(regp + MIPS64_EF_HI));
392 supply_register (PC_REGNUM, (char *)(regp + MIPS64_EF_CP0_EPC));
393 supply_register (BADVADDR_REGNUM, (char *)(regp + MIPS64_EF_CP0_BADVADDR));
394 supply_register (PS_REGNUM, (char *)(regp + MIPS64_EF_CP0_STATUS));
395 supply_register (CAUSE_REGNUM, (char *)(regp + MIPS64_EF_CP0_CAUSE));
397 /* Fill inaccessible registers with zero. */
398 supply_register (UNUSED_REGNUM, zerobuf);
399 for (regi = FIRST_EMBED_REGNUM; regi < LAST_EMBED_REGNUM; regi++)
400 supply_register (regi, zerobuf);
403 /* Pack our registers (or one register) into an elf_gregset_t. */
406 mips64_fill_gregset (mips64_elf_gregset_t *gregsetp, int regno)
409 mips64_elf_greg_t *regp = *gregsetp;
414 memset (regp, 0, sizeof (mips64_elf_gregset_t));
415 for (regi = 0; regi < 32; regi++)
416 mips64_fill_gregset (gregsetp, regi);
417 mips64_fill_gregset (gregsetp, LO_REGNUM);
418 mips64_fill_gregset (gregsetp, HI_REGNUM);
419 mips64_fill_gregset (gregsetp, PC_REGNUM);
420 mips64_fill_gregset (gregsetp, BADVADDR_REGNUM);
421 mips64_fill_gregset (gregsetp, PS_REGNUM);
422 mips64_fill_gregset (gregsetp, CAUSE_REGNUM);
429 dst = regp + regno + MIPS64_EF_REG0;
430 regcache_collect (regno, dst);
438 regaddr = MIPS64_EF_LO;
441 regaddr = MIPS64_EF_HI;
444 regaddr = MIPS64_EF_CP0_EPC;
446 case BADVADDR_REGNUM:
447 regaddr = MIPS64_EF_CP0_BADVADDR;
450 regaddr = MIPS64_EF_CP0_STATUS;
453 regaddr = MIPS64_EF_CP0_CAUSE;
459 dst = regp + regaddr;
460 regcache_collect (regno, dst);
464 /* Likewise, unpack an elf_fpregset_t. */
467 mips64_supply_fpregset (mips64_elf_fpregset_t *fpregsetp)
470 char zerobuf[MAX_REGISTER_SIZE];
472 memset (zerobuf, 0, MAX_REGISTER_SIZE);
474 for (regi = 0; regi < 32; regi++)
475 supply_register (FP0_REGNUM + regi,
476 (char *)(*fpregsetp + regi));
478 supply_register (FCRCS_REGNUM, (char *)(*fpregsetp + 32));
480 /* FIXME: how can we supply FCRIR_REGNUM? The ABI doesn't tell us. */
481 supply_register (FCRIR_REGNUM, zerobuf);
484 /* Likewise, pack one or all floating point registers into an
488 mips64_fill_fpregset (mips64_elf_fpregset_t *fpregsetp, int regno)
492 if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
494 from = (char *) &deprecated_registers[REGISTER_BYTE (regno)];
495 to = (char *) (*fpregsetp + regno - FP0_REGNUM);
496 memcpy (to, from, REGISTER_RAW_SIZE (regno - FP0_REGNUM));
498 else if (regno == FCRCS_REGNUM)
500 from = (char *) &deprecated_registers[REGISTER_BYTE (regno)];
501 to = (char *) (*fpregsetp + 32);
502 memcpy (to, from, REGISTER_RAW_SIZE (regno));
504 else if (regno == -1)
508 for (regi = 0; regi < 32; regi++)
509 mips64_fill_fpregset (fpregsetp, FP0_REGNUM + regi);
510 mips64_fill_fpregset(fpregsetp, FCRCS_REGNUM);
515 /* Map gdb internal register number to ptrace ``address''.
516 These ``addresses'' are normally defined in <asm/ptrace.h>. */
519 mips64_linux_register_addr (int regno, CORE_ADDR blockend)
523 if (regno < 0 || regno >= NUM_REGS)
524 error ("Bogon register number %d.", regno);
528 else if ((regno >= FP0_REGNUM) && (regno < FP0_REGNUM + 32))
529 regaddr = MIPS64_FPR_BASE + (regno - FP0_REGNUM);
530 else if (regno == PC_REGNUM)
532 else if (regno == CAUSE_REGNUM)
533 regaddr = MIPS64_CAUSE;
534 else if (regno == BADVADDR_REGNUM)
535 regaddr = MIPS64_BADVADDR;
536 else if (regno == LO_REGNUM)
537 regaddr = MIPS64_MMLO;
538 else if (regno == HI_REGNUM)
539 regaddr = MIPS64_MMHI;
540 else if (regno == FCRCS_REGNUM)
541 regaddr = MIPS64_FPC_CSR;
542 else if (regno == FCRIR_REGNUM)
543 regaddr = MIPS64_FPC_EIR;
545 error ("Unknowable register number %d.", regno);
550 /* Use a local version of this function to get the correct types for
551 regsets, until multi-arch core support is ready. */
554 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
555 int which, CORE_ADDR reg_addr)
557 elf_gregset_t gregset;
558 elf_fpregset_t fpregset;
559 mips64_elf_gregset_t gregset64;
560 mips64_elf_fpregset_t fpregset64;
564 if (core_reg_size == sizeof (gregset))
566 memcpy ((char *) &gregset, core_reg_sect, sizeof (gregset));
567 supply_gregset (&gregset);
569 else if (core_reg_size == sizeof (gregset64))
571 memcpy ((char *) &gregset64, core_reg_sect, sizeof (gregset64));
572 mips64_supply_gregset (&gregset64);
576 warning ("wrong size gregset struct in core file");
581 if (core_reg_size == sizeof (fpregset))
583 memcpy ((char *) &fpregset, core_reg_sect, sizeof (fpregset));
584 supply_fpregset (&fpregset);
586 else if (core_reg_size == sizeof (fpregset64))
588 memcpy ((char *) &fpregset64, core_reg_sect, sizeof (fpregset64));
589 mips64_supply_fpregset (&fpregset64);
593 warning ("wrong size fpregset struct in core file");
598 /* Register that we are able to handle ELF file formats using standard
599 procfs "regset" structures. */
601 static struct core_fns regset_core_fns =
603 bfd_target_elf_flavour, /* core_flavour */
604 default_check_format, /* check_format */
605 default_core_sniffer, /* core_sniffer */
606 fetch_core_registers, /* core_read_registers */
610 /* Fetch (and possibly build) an appropriate link_map_offsets
611 structure for native GNU/Linux MIPS targets using the struct offsets
612 defined in link.h (but without actual reference to that file).
614 This makes it possible to access GNU/Linux MIPS shared libraries from a
615 GDB that was built on a different host platform (for cross debugging). */
617 static struct link_map_offsets *
618 mips64_linux_svr4_fetch_link_map_offsets (void)
620 static struct link_map_offsets lmo;
621 static struct link_map_offsets *lmp = NULL;
627 lmo.r_debug_size = 16; /* The actual size is 40 bytes, but
628 this is all we need. */
629 lmo.r_map_offset = 8;
632 lmo.link_map_size = 40;
634 lmo.l_addr_offset = 0;
637 lmo.l_name_offset = 8;
640 lmo.l_next_offset = 24;
643 lmo.l_prev_offset = 32;
650 /* Handle for obtaining pointer to the current register_addr() function
651 for a given architecture. */
652 static struct gdbarch_data *register_addr_data;
655 register_addr (int regno, CORE_ADDR blockend)
657 CORE_ADDR (*register_addr_ptr) (int, CORE_ADDR) =
658 gdbarch_data (current_gdbarch, register_addr_data);
660 gdb_assert (register_addr_ptr != 0);
662 return register_addr_ptr (regno, blockend);
666 set_mips_linux_register_addr (struct gdbarch *gdbarch,
667 CORE_ADDR (*register_addr_ptr) (int, CORE_ADDR))
669 set_gdbarch_data (gdbarch, register_addr_data, register_addr_ptr);
673 init_register_addr_data (struct gdbarch *gdbarch)
679 mips_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
681 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
682 enum mips_abi abi = mips_abi (gdbarch);
687 set_gdbarch_get_longjmp_target (gdbarch,
688 mips_linux_get_longjmp_target);
689 set_solib_svr4_fetch_link_map_offsets
690 (gdbarch, mips_linux_svr4_fetch_link_map_offsets);
691 set_mips_linux_register_addr (gdbarch, mips_linux_register_addr);
694 set_gdbarch_get_longjmp_target (gdbarch,
695 mips_linux_get_longjmp_target);
696 set_solib_svr4_fetch_link_map_offsets
697 (gdbarch, mips_linux_svr4_fetch_link_map_offsets);
698 set_mips_linux_register_addr (gdbarch, mips64_linux_register_addr);
701 set_gdbarch_get_longjmp_target (gdbarch,
702 mips64_linux_get_longjmp_target);
703 set_solib_svr4_fetch_link_map_offsets
704 (gdbarch, mips64_linux_svr4_fetch_link_map_offsets);
705 set_mips_linux_register_addr (gdbarch, mips64_linux_register_addr);
708 internal_error (__FILE__, __LINE__, "can't handle ABI");
714 _initialize_mips_linux_tdep (void)
716 const struct bfd_arch_info *arch_info;
719 register_gdbarch_data (init_register_addr_data, 0);
721 for (arch_info = bfd_lookup_arch (bfd_arch_mips, 0);
723 arch_info = arch_info->next)
725 gdbarch_register_osabi (bfd_arch_mips, arch_info->mach, GDB_OSABI_LINUX,
726 mips_linux_init_abi);
729 add_core_fns (®set_core_fns);