1 /* Target-dependent code for FreeBSD/mips.
3 Copyright (C) 2017 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "trad-frame.h"
24 #include "tramp-frame.h"
26 #include "fbsd-tdep.h"
27 #include "mips-tdep.h"
28 #include "mips-fbsd-tdep.h"
30 #include "solib-svr4.h"
32 /* Core file support. */
34 /* Number of registers in `struct reg' from <machine/reg.h>. The
35 first 38 follow the standard MIPS layout. The 39th holds
36 IC_INT_REG on RM7K and RM9K processors. The 40th is a dummy for
38 #define MIPS_FBSD_NUM_GREGS 40
40 /* Number of registers in `struct fpreg' from <machine/reg.h>. The
41 first 32 hold floating point registers. 33 holds the FSR. The
42 34th is a dummy for padding. */
43 #define MIPS_FBSD_NUM_FPREGS 34
45 /* Supply a single register. If the source register size matches the
46 size the regcache expects, this can use regcache_raw_supply(). If
47 they are different, this copies the source register into a buffer
48 that can be passed to regcache_raw_supply(). */
51 mips_fbsd_supply_reg (struct regcache
*regcache
, int regnum
, const void *addr
,
54 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
56 if (register_size (gdbarch
, regnum
) == len
)
57 regcache_raw_supply (regcache
, regnum
, addr
);
60 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
61 gdb_byte buf
[MAX_REGISTER_SIZE
];
64 val
= extract_signed_integer ((const gdb_byte
*) addr
, len
, byte_order
);
65 store_signed_integer (buf
, register_size (gdbarch
, regnum
), byte_order
,
67 regcache_raw_supply (regcache
, regnum
, buf
);
71 /* Collect a single register. If the destination register size
72 matches the size the regcache expects, this can use
73 regcache_raw_supply(). If they are different, this fetches the
74 register via regcache_raw_supply() into a buffer and then copies it
75 into the final destination. */
78 mips_fbsd_collect_reg (const struct regcache
*regcache
, int regnum
, void *addr
,
81 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
83 if (register_size (gdbarch
, regnum
) == len
)
84 regcache_raw_collect (regcache
, regnum
, addr
);
87 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
88 gdb_byte buf
[MAX_REGISTER_SIZE
];
91 regcache_raw_collect (regcache
, regnum
, buf
);
92 val
= extract_signed_integer (buf
, register_size (gdbarch
, regnum
),
94 store_signed_integer ((gdb_byte
*) addr
, len
, byte_order
, val
);
98 /* Supply the floating-point registers stored in FPREGS to REGCACHE.
99 Each floating-point register in FPREGS is REGSIZE bytes in
103 mips_fbsd_supply_fpregs (struct regcache
*regcache
, int regnum
,
104 const void *fpregs
, size_t regsize
)
106 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
107 const gdb_byte
*regs
= (const gdb_byte
*) fpregs
;
108 int i
, fp0num
, fsrnum
;
110 fp0num
= mips_regnum (gdbarch
)->fp0
;
111 fsrnum
= mips_regnum (gdbarch
)->fp_control_status
;
112 for (i
= fp0num
; i
<= fsrnum
; i
++)
113 if (regnum
== i
|| regnum
== -1)
114 mips_fbsd_supply_reg (regcache
, i
,
115 regs
+ (i
- fp0num
) * regsize
, regsize
);
118 /* Supply the general-purpose registers stored in GREGS to REGCACHE.
119 Each general-purpose register in GREGS is REGSIZE bytes in
123 mips_fbsd_supply_gregs (struct regcache
*regcache
, int regnum
,
124 const void *gregs
, size_t regsize
)
126 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
127 const gdb_byte
*regs
= (const gdb_byte
*) gregs
;
130 for (i
= 0; i
<= mips_regnum (gdbarch
)->pc
; i
++)
131 if (regnum
== i
|| regnum
== -1)
132 mips_fbsd_supply_reg (regcache
, i
, regs
+ i
* regsize
, regsize
);
135 /* Collect the floating-point registers from REGCACHE and store them
136 in FPREGS. Each floating-point register in FPREGS is REGSIZE bytes
140 mips_fbsd_collect_fpregs (const struct regcache
*regcache
, int regnum
,
141 void *fpregs
, size_t regsize
)
143 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
144 gdb_byte
*regs
= (gdb_byte
*) fpregs
;
145 int i
, fp0num
, fsrnum
;
147 fp0num
= mips_regnum (gdbarch
)->fp0
;
148 fsrnum
= mips_regnum (gdbarch
)->fp_control_status
;
149 for (i
= fp0num
; i
<= fsrnum
; i
++)
150 if (regnum
== i
|| regnum
== -1)
151 mips_fbsd_collect_reg (regcache
, i
,
152 regs
+ (i
- fp0num
) * regsize
, regsize
);
155 /* Collect the general-purpose registers from REGCACHE and store them
156 in GREGS. Each general-purpose register in GREGS is REGSIZE bytes
160 mips_fbsd_collect_gregs (const struct regcache
*regcache
, int regnum
,
161 void *gregs
, size_t regsize
)
163 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
164 gdb_byte
*regs
= (gdb_byte
*) gregs
;
167 for (i
= 0; i
<= mips_regnum (gdbarch
)->pc
; i
++)
168 if (regnum
== i
|| regnum
== -1)
169 mips_fbsd_collect_reg (regcache
, i
, regs
+ i
* regsize
, regsize
);
172 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
173 in the floating-point register set REGSET to register cache
174 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
177 mips_fbsd_supply_fpregset (const struct regset
*regset
,
178 struct regcache
*regcache
,
179 int regnum
, const void *fpregs
, size_t len
)
181 size_t regsize
= mips_abi_regsize (get_regcache_arch (regcache
));
183 gdb_assert (len
>= MIPS_FBSD_NUM_FPREGS
* regsize
);
185 mips_fbsd_supply_fpregs (regcache
, regnum
, fpregs
, regsize
);
188 /* Collect register REGNUM from the register cache REGCACHE and store
189 it in the buffer specified by FPREGS and LEN in the floating-point
190 register set REGSET. If REGNUM is -1, do this for all registers in
194 mips_fbsd_collect_fpregset (const struct regset
*regset
,
195 const struct regcache
*regcache
,
196 int regnum
, void *fpregs
, size_t len
)
198 size_t regsize
= mips_abi_regsize (get_regcache_arch (regcache
));
200 gdb_assert (len
>= MIPS_FBSD_NUM_FPREGS
* regsize
);
202 mips_fbsd_collect_fpregs (regcache
, regnum
, fpregs
, regsize
);
205 /* Supply register REGNUM from the buffer specified by GREGS and LEN
206 in the general-purpose register set REGSET to register cache
207 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
210 mips_fbsd_supply_gregset (const struct regset
*regset
,
211 struct regcache
*regcache
, int regnum
,
212 const void *gregs
, size_t len
)
214 size_t regsize
= mips_abi_regsize (get_regcache_arch (regcache
));
216 gdb_assert (len
>= MIPS_FBSD_NUM_GREGS
* regsize
);
218 mips_fbsd_supply_gregs (regcache
, regnum
, gregs
, regsize
);
221 /* Collect register REGNUM from the register cache REGCACHE and store
222 it in the buffer specified by GREGS and LEN in the general-purpose
223 register set REGSET. If REGNUM is -1, do this for all registers in
227 mips_fbsd_collect_gregset (const struct regset
*regset
,
228 const struct regcache
*regcache
,
229 int regnum
, void *gregs
, size_t len
)
231 size_t regsize
= mips_abi_regsize (get_regcache_arch (regcache
));
233 gdb_assert (len
>= MIPS_FBSD_NUM_GREGS
* regsize
);
235 mips_fbsd_collect_gregs (regcache
, regnum
, gregs
, regsize
);
238 /* FreeBSD/mips register sets. */
240 static const struct regset mips_fbsd_gregset
=
243 mips_fbsd_supply_gregset
,
244 mips_fbsd_collect_gregset
,
247 static const struct regset mips_fbsd_fpregset
=
250 mips_fbsd_supply_fpregset
,
251 mips_fbsd_collect_fpregset
,
254 /* Iterate over core file register note sections. */
257 mips_fbsd_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
258 iterate_over_regset_sections_cb
*cb
,
260 const struct regcache
*regcache
)
262 size_t regsize
= mips_abi_regsize (gdbarch
);
264 cb (".reg", MIPS_FBSD_NUM_GREGS
* regsize
, &mips_fbsd_gregset
,
266 cb (".reg2", MIPS_FBSD_NUM_FPREGS
* regsize
, &mips_fbsd_fpregset
,
270 /* Signal trampoline support. */
272 #define FBSD_SYS_sigreturn 417
274 #define MIPS_INST_LI_V0_SIGRETURN 0x24020000 + FBSD_SYS_sigreturn
275 #define MIPS_INST_SYSCALL 0x0000000c
276 #define MIPS_INST_BREAK 0x0000000d
278 #define O32_SIGFRAME_UCONTEXT_OFFSET (16)
279 #define O32_SIGSET_T_SIZE (16)
281 #define O32_UCONTEXT_ONSTACK (O32_SIGSET_T_SIZE)
282 #define O32_UCONTEXT_PC (O32_UCONTEXT_ONSTACK + 4)
283 #define O32_UCONTEXT_REGS (O32_UCONTEXT_PC + 4)
284 #define O32_UCONTEXT_SR (O32_UCONTEXT_REGS + 4 * 32)
285 #define O32_UCONTEXT_LO (O32_UCONTEXT_SR + 4)
286 #define O32_UCONTEXT_HI (O32_UCONTEXT_LO + 4)
287 #define O32_UCONTEXT_FPUSED (O32_UCONTEXT_HI + 4)
288 #define O32_UCONTEXT_FPREGS (O32_UCONTEXT_FPUSED + 4)
290 #define O32_UCONTEXT_REG_SIZE 4
293 mips_fbsd_sigframe_init (const struct tramp_frame
*self
,
294 struct frame_info
*this_frame
,
295 struct trad_frame_cache
*cache
,
298 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
299 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
300 CORE_ADDR sp
, ucontext_addr
, addr
;
304 /* We find the appropriate instance of `ucontext_t' at a
305 fixed offset in the signal frame. */
306 sp
= get_frame_register_signed (this_frame
,
307 MIPS_SP_REGNUM
+ gdbarch_num_regs (gdbarch
));
308 ucontext_addr
= sp
+ O32_SIGFRAME_UCONTEXT_OFFSET
;
311 regnum
= mips_regnum (gdbarch
)->pc
;
312 trad_frame_set_reg_addr (cache
,
313 regnum
+ gdbarch_num_regs (gdbarch
),
314 ucontext_addr
+ O32_UCONTEXT_PC
);
317 for (regnum
= MIPS_ZERO_REGNUM
, addr
= ucontext_addr
+ O32_UCONTEXT_REGS
;
318 regnum
<= MIPS_RA_REGNUM
; regnum
++, addr
+= O32_UCONTEXT_REG_SIZE
)
319 trad_frame_set_reg_addr (cache
,
320 regnum
+ gdbarch_num_regs (gdbarch
),
323 regnum
= MIPS_PS_REGNUM
;
324 trad_frame_set_reg_addr (cache
,
325 regnum
+ gdbarch_num_regs (gdbarch
),
326 ucontext_addr
+ O32_UCONTEXT_SR
);
329 regnum
= mips_regnum (gdbarch
)->lo
;
330 trad_frame_set_reg_addr (cache
,
331 regnum
+ gdbarch_num_regs (gdbarch
),
332 ucontext_addr
+ O32_UCONTEXT_LO
);
333 regnum
= mips_regnum (gdbarch
)->hi
;
334 trad_frame_set_reg_addr (cache
,
335 regnum
+ gdbarch_num_regs (gdbarch
),
336 ucontext_addr
+ O32_UCONTEXT_HI
);
338 if (target_read_memory (ucontext_addr
+ O32_UCONTEXT_FPUSED
, buf
, 4) == 0
339 && extract_unsigned_integer (buf
, 4, byte_order
) != 0)
341 for (regnum
= 0, addr
= ucontext_addr
+ O32_UCONTEXT_FPREGS
;
342 regnum
< 32; regnum
++, addr
+= O32_UCONTEXT_REG_SIZE
)
343 trad_frame_set_reg_addr (cache
,
344 regnum
+ gdbarch_fp0_regnum (gdbarch
),
346 trad_frame_set_reg_addr (cache
, mips_regnum (gdbarch
)->fp_control_status
,
350 trad_frame_set_id (cache
, frame_id_build (sp
, func
));
353 #define MIPS_INST_ADDIU_A0_SP_O32 (0x27a40000 \
354 + O32_SIGFRAME_UCONTEXT_OFFSET)
356 static const struct tramp_frame mips_fbsd_sigframe
=
361 { MIPS_INST_ADDIU_A0_SP_O32
, -1 }, /* addiu a0, sp, SIGF_UC */
362 { MIPS_INST_LI_V0_SIGRETURN
, -1 }, /* li v0, SYS_sigreturn */
363 { MIPS_INST_SYSCALL
, -1 }, /* syscall */
364 { MIPS_INST_BREAK
, -1 }, /* break */
365 { TRAMP_SENTINEL_INSN
, -1 }
367 mips_fbsd_sigframe_init
370 #define N64_SIGFRAME_UCONTEXT_OFFSET (32)
371 #define N64_SIGSET_T_SIZE (16)
373 #define N64_UCONTEXT_ONSTACK (N64_SIGSET_T_SIZE)
374 #define N64_UCONTEXT_PC (N64_UCONTEXT_ONSTACK + 8)
375 #define N64_UCONTEXT_REGS (N64_UCONTEXT_PC + 8)
376 #define N64_UCONTEXT_SR (N64_UCONTEXT_REGS + 8 * 32)
377 #define N64_UCONTEXT_LO (N64_UCONTEXT_SR + 8)
378 #define N64_UCONTEXT_HI (N64_UCONTEXT_LO + 8)
379 #define N64_UCONTEXT_FPUSED (N64_UCONTEXT_HI + 8)
380 #define N64_UCONTEXT_FPREGS (N64_UCONTEXT_FPUSED + 8)
382 #define N64_UCONTEXT_REG_SIZE 8
385 mips64_fbsd_sigframe_init (const struct tramp_frame
*self
,
386 struct frame_info
*this_frame
,
387 struct trad_frame_cache
*cache
,
390 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
391 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
392 CORE_ADDR sp
, ucontext_addr
, addr
;
396 /* We find the appropriate instance of `ucontext_t' at a
397 fixed offset in the signal frame. */
398 sp
= get_frame_register_signed (this_frame
,
399 MIPS_SP_REGNUM
+ gdbarch_num_regs (gdbarch
));
400 ucontext_addr
= sp
+ N64_SIGFRAME_UCONTEXT_OFFSET
;
403 regnum
= mips_regnum (gdbarch
)->pc
;
404 trad_frame_set_reg_addr (cache
,
405 regnum
+ gdbarch_num_regs (gdbarch
),
406 ucontext_addr
+ N64_UCONTEXT_PC
);
409 for (regnum
= MIPS_ZERO_REGNUM
, addr
= ucontext_addr
+ N64_UCONTEXT_REGS
;
410 regnum
<= MIPS_RA_REGNUM
; regnum
++, addr
+= N64_UCONTEXT_REG_SIZE
)
411 trad_frame_set_reg_addr (cache
,
412 regnum
+ gdbarch_num_regs (gdbarch
),
415 regnum
= MIPS_PS_REGNUM
;
416 trad_frame_set_reg_addr (cache
,
417 regnum
+ gdbarch_num_regs (gdbarch
),
418 ucontext_addr
+ N64_UCONTEXT_SR
);
421 regnum
= mips_regnum (gdbarch
)->lo
;
422 trad_frame_set_reg_addr (cache
,
423 regnum
+ gdbarch_num_regs (gdbarch
),
424 ucontext_addr
+ N64_UCONTEXT_LO
);
425 regnum
= mips_regnum (gdbarch
)->hi
;
426 trad_frame_set_reg_addr (cache
,
427 regnum
+ gdbarch_num_regs (gdbarch
),
428 ucontext_addr
+ N64_UCONTEXT_HI
);
430 if (target_read_memory (ucontext_addr
+ N64_UCONTEXT_FPUSED
, buf
, 4) == 0
431 && extract_unsigned_integer (buf
, 4, byte_order
) != 0)
433 for (regnum
= 0, addr
= ucontext_addr
+ N64_UCONTEXT_FPREGS
;
434 regnum
< 32; regnum
++, addr
+= N64_UCONTEXT_REG_SIZE
)
435 trad_frame_set_reg_addr (cache
,
436 regnum
+ gdbarch_fp0_regnum (gdbarch
),
438 trad_frame_set_reg_addr (cache
, mips_regnum (gdbarch
)->fp_control_status
,
442 trad_frame_set_id (cache
, frame_id_build (sp
, func
));
445 #define MIPS_INST_DADDIU_A0_SP_N64 (0x67a40000 \
446 + N64_SIGFRAME_UCONTEXT_OFFSET)
448 static const struct tramp_frame mips64_fbsd_sigframe
=
453 { MIPS_INST_DADDIU_A0_SP_N64
, -1 }, /* daddiu a0, sp, SIGF_UC */
454 { MIPS_INST_LI_V0_SIGRETURN
, -1 }, /* li v0, SYS_sigreturn */
455 { MIPS_INST_SYSCALL
, -1 }, /* syscall */
456 { MIPS_INST_BREAK
, -1 }, /* break */
457 { TRAMP_SENTINEL_INSN
, -1 }
459 mips64_fbsd_sigframe_init
462 /* Shared library support. */
464 /* FreeBSD/mips uses a slightly different `struct link_map' than the
465 other FreeBSD platforms as it includes an additional `l_off'
468 static struct link_map_offsets
*
469 mips_fbsd_ilp32_fetch_link_map_offsets (void)
471 static struct link_map_offsets lmo
;
472 static struct link_map_offsets
*lmp
= NULL
;
478 lmo
.r_version_offset
= 0;
479 lmo
.r_version_size
= 4;
480 lmo
.r_map_offset
= 4;
481 lmo
.r_brk_offset
= 8;
482 lmo
.r_ldsomap_offset
= -1;
484 lmo
.link_map_size
= 24;
485 lmo
.l_addr_offset
= 0;
486 lmo
.l_name_offset
= 8;
487 lmo
.l_ld_offset
= 12;
488 lmo
.l_next_offset
= 16;
489 lmo
.l_prev_offset
= 20;
495 static struct link_map_offsets
*
496 mips_fbsd_lp64_fetch_link_map_offsets (void)
498 static struct link_map_offsets lmo
;
499 static struct link_map_offsets
*lmp
= NULL
;
505 lmo
.r_version_offset
= 0;
506 lmo
.r_version_size
= 4;
507 lmo
.r_map_offset
= 8;
508 lmo
.r_brk_offset
= 16;
509 lmo
.r_ldsomap_offset
= -1;
511 lmo
.link_map_size
= 48;
512 lmo
.l_addr_offset
= 0;
513 lmo
.l_name_offset
= 16;
514 lmo
.l_ld_offset
= 24;
515 lmo
.l_next_offset
= 32;
516 lmo
.l_prev_offset
= 40;
523 mips_fbsd_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
525 enum mips_abi abi
= mips_abi (gdbarch
);
527 /* Generic FreeBSD support. */
528 fbsd_init_abi (info
, gdbarch
);
530 set_gdbarch_software_single_step (gdbarch
, mips_software_single_step
);
535 tramp_frame_prepend_unwinder (gdbarch
, &mips_fbsd_sigframe
);
540 tramp_frame_prepend_unwinder (gdbarch
, &mips64_fbsd_sigframe
);
544 set_gdbarch_iterate_over_regset_sections
545 (gdbarch
, mips_fbsd_iterate_over_regset_sections
);
547 /* FreeBSD/mips has SVR4-style shared libraries. */
548 set_solib_svr4_fetch_link_map_offsets
549 (gdbarch
, (gdbarch_ptr_bit (gdbarch
) == 32 ?
550 mips_fbsd_ilp32_fetch_link_map_offsets
:
551 mips_fbsd_lp64_fetch_link_map_offsets
));
555 /* Provide a prototype to silence -Wmissing-prototypes. */
556 void _initialize_mips_fbsd_tdep (void);
559 _initialize_mips_fbsd_tdep (void)
561 gdbarch_register_osabi (bfd_arch_mips
, 0, GDB_OSABI_FREEBSD
,