1 /* Target-dependent code for GNU/Linux i386.
3 Copyright (C) 2000-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/>. */
28 #include "reggroups.h"
29 #include "dwarf2-frame.h"
30 #include "i386-tdep.h"
31 #include "i386-linux-tdep.h"
32 #include "linux-tdep.h"
34 #include "glibc-tdep.h"
35 #include "solib-svr4.h"
37 #include "arch-utils.h"
38 #include "xml-syscall.h"
40 #include "i387-tdep.h"
41 #include "x86-xstate.h"
43 /* The syscall's XML filename for i386. */
44 #define XML_SYSCALL_FILENAME_I386 "syscalls/i386-linux.xml"
46 #include "record-full.h"
47 #include "linux-record.h"
49 #include "features/i386/32bit-core.c"
50 #include "features/i386/32bit-sse.c"
51 #include "features/i386/32bit-linux.c"
52 #include "features/i386/32bit-avx.c"
53 #include "features/i386/32bit-mpx.c"
54 #include "features/i386/32bit-avx512.c"
55 #include "features/i386/32bit-pkeys.c"
57 /* Return non-zero, when the register is in the corresponding register
58 group. Put the LINUX_ORIG_EAX register in the system group. */
60 i386_linux_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
61 struct reggroup
*group
)
63 if (regnum
== I386_LINUX_ORIG_EAX_REGNUM
)
64 return (group
== system_reggroup
65 || group
== save_reggroup
66 || group
== restore_reggroup
);
67 return i386_register_reggroup_p (gdbarch
, regnum
, group
);
71 /* Recognizing signal handler frames. */
73 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
74 "realtime" (RT) signals. The RT signals can provide additional
75 information to the signal handler if the SA_SIGINFO flag is set
76 when establishing a signal handler using `sigaction'. It is not
77 unlikely that future versions of GNU/Linux will support SA_SIGINFO
78 for normal signals too. */
80 /* When the i386 Linux kernel calls a signal handler and the
81 SA_RESTORER flag isn't set, the return address points to a bit of
82 code on the stack. This function returns whether the PC appears to
83 be within this bit of code.
85 The instruction sequence for normal signals is
89 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
91 Checking for the code sequence should be somewhat reliable, because
92 the effect is to call the system call sigreturn. This is unlikely
93 to occur anywhere other than in a signal trampoline.
95 It kind of sucks that we have to read memory from the process in
96 order to identify a signal trampoline, but there doesn't seem to be
97 any other way. Therefore we only do the memory reads if no
98 function name could be identified, which should be the case since
99 the code is on the stack.
101 Detection of signal trampolines for handlers that set the
102 SA_RESTORER flag is in general not possible. Unfortunately this is
103 what the GNU C Library has been doing for quite some time now.
104 However, as of version 2.1.2, the GNU C Library uses signal
105 trampolines (named __restore and __restore_rt) that are identical
106 to the ones used by the kernel. Therefore, these trampolines are
109 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
110 #define LINUX_SIGTRAMP_OFFSET0 0
111 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
112 #define LINUX_SIGTRAMP_OFFSET1 1
113 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
114 #define LINUX_SIGTRAMP_OFFSET2 6
116 static const gdb_byte linux_sigtramp_code
[] =
118 LINUX_SIGTRAMP_INSN0
, /* pop %eax */
119 LINUX_SIGTRAMP_INSN1
, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
120 LINUX_SIGTRAMP_INSN2
, 0x80 /* int $0x80 */
123 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
125 /* If THIS_FRAME is a sigtramp routine, return the address of the
126 start of the routine. Otherwise, return 0. */
129 i386_linux_sigtramp_start (struct frame_info
*this_frame
)
131 CORE_ADDR pc
= get_frame_pc (this_frame
);
132 gdb_byte buf
[LINUX_SIGTRAMP_LEN
];
134 /* We only recognize a signal trampoline if PC is at the start of
135 one of the three instructions. We optimize for finding the PC at
136 the start, as will be the case when the trampoline is not the
137 first frame on the stack. We assume that in the case where the
138 PC is not at the start of the instruction sequence, there will be
139 a few trailing readable bytes on the stack. */
141 if (!safe_frame_unwind_memory (this_frame
, pc
, buf
, LINUX_SIGTRAMP_LEN
))
144 if (buf
[0] != LINUX_SIGTRAMP_INSN0
)
150 case LINUX_SIGTRAMP_INSN1
:
151 adjust
= LINUX_SIGTRAMP_OFFSET1
;
153 case LINUX_SIGTRAMP_INSN2
:
154 adjust
= LINUX_SIGTRAMP_OFFSET2
;
162 if (!safe_frame_unwind_memory (this_frame
, pc
, buf
, LINUX_SIGTRAMP_LEN
))
166 if (memcmp (buf
, linux_sigtramp_code
, LINUX_SIGTRAMP_LEN
) != 0)
172 /* This function does the same for RT signals. Here the instruction
176 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
178 The effect is to call the system call rt_sigreturn. */
180 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
181 #define LINUX_RT_SIGTRAMP_OFFSET0 0
182 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
183 #define LINUX_RT_SIGTRAMP_OFFSET1 5
185 static const gdb_byte linux_rt_sigtramp_code
[] =
187 LINUX_RT_SIGTRAMP_INSN0
, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
188 LINUX_RT_SIGTRAMP_INSN1
, 0x80 /* int $0x80 */
191 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
193 /* If THIS_FRAME is an RT sigtramp routine, return the address of the
194 start of the routine. Otherwise, return 0. */
197 i386_linux_rt_sigtramp_start (struct frame_info
*this_frame
)
199 CORE_ADDR pc
= get_frame_pc (this_frame
);
200 gdb_byte buf
[LINUX_RT_SIGTRAMP_LEN
];
202 /* We only recognize a signal trampoline if PC is at the start of
203 one of the two instructions. We optimize for finding the PC at
204 the start, as will be the case when the trampoline is not the
205 first frame on the stack. We assume that in the case where the
206 PC is not at the start of the instruction sequence, there will be
207 a few trailing readable bytes on the stack. */
209 if (!safe_frame_unwind_memory (this_frame
, pc
, buf
, LINUX_RT_SIGTRAMP_LEN
))
212 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN0
)
214 if (buf
[0] != LINUX_RT_SIGTRAMP_INSN1
)
217 pc
-= LINUX_RT_SIGTRAMP_OFFSET1
;
219 if (!safe_frame_unwind_memory (this_frame
, pc
, buf
,
220 LINUX_RT_SIGTRAMP_LEN
))
224 if (memcmp (buf
, linux_rt_sigtramp_code
, LINUX_RT_SIGTRAMP_LEN
) != 0)
230 /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
234 i386_linux_sigtramp_p (struct frame_info
*this_frame
)
236 CORE_ADDR pc
= get_frame_pc (this_frame
);
239 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
241 /* If we have NAME, we can optimize the search. The trampolines are
242 named __restore and __restore_rt. However, they aren't dynamically
243 exported from the shared C library, so the trampoline may appear to
244 be part of the preceding function. This should always be sigaction,
245 __sigaction, or __libc_sigaction (all aliases to the same function). */
246 if (name
== NULL
|| strstr (name
, "sigaction") != NULL
)
247 return (i386_linux_sigtramp_start (this_frame
) != 0
248 || i386_linux_rt_sigtramp_start (this_frame
) != 0);
250 return (strcmp ("__restore", name
) == 0
251 || strcmp ("__restore_rt", name
) == 0);
254 /* Return one if the PC of THIS_FRAME is in a signal trampoline which
255 may have DWARF-2 CFI. */
258 i386_linux_dwarf_signal_frame_p (struct gdbarch
*gdbarch
,
259 struct frame_info
*this_frame
)
261 CORE_ADDR pc
= get_frame_pc (this_frame
);
264 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
266 /* If a vsyscall DSO is in use, the signal trampolines may have these
268 if (name
&& (strcmp (name
, "__kernel_sigreturn") == 0
269 || strcmp (name
, "__kernel_rt_sigreturn") == 0))
275 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
276 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
278 /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
279 address of the associated sigcontext structure. */
282 i386_linux_sigcontext_addr (struct frame_info
*this_frame
)
284 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
285 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
290 get_frame_register (this_frame
, I386_ESP_REGNUM
, buf
);
291 sp
= extract_unsigned_integer (buf
, 4, byte_order
);
293 pc
= i386_linux_sigtramp_start (this_frame
);
296 /* The sigcontext structure lives on the stack, right after
297 the signum argument. We determine the address of the
298 sigcontext structure by looking at the frame's stack
299 pointer. Keep in mind that the first instruction of the
300 sigtramp code is "pop %eax". If the PC is after this
301 instruction, adjust the returned value accordingly. */
302 if (pc
== get_frame_pc (this_frame
))
307 pc
= i386_linux_rt_sigtramp_start (this_frame
);
310 CORE_ADDR ucontext_addr
;
312 /* The sigcontext structure is part of the user context. A
313 pointer to the user context is passed as the third argument
314 to the signal handler. */
315 read_memory (sp
+ 8, buf
, 4);
316 ucontext_addr
= extract_unsigned_integer (buf
, 4, byte_order
);
317 return ucontext_addr
+ I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET
;
320 error (_("Couldn't recognize signal trampoline."));
324 /* Set the program counter for process PTID to PC. */
327 i386_linux_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
329 regcache_cooked_write_unsigned (regcache
, I386_EIP_REGNUM
, pc
);
331 /* We must be careful with modifying the program counter. If we
332 just interrupted a system call, the kernel might try to restart
333 it when we resume the inferior. On restarting the system call,
334 the kernel will try backing up the program counter even though it
335 no longer points at the system call. This typically results in a
336 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
337 "orig_eax" pseudo-register.
339 Note that "orig_eax" is saved when setting up a dummy call frame.
340 This means that it is properly restored when that frame is
341 popped, and that the interrupted system call will be restarted
342 when we resume the inferior on return from a function call from
343 within GDB. In all other cases the system call will not be
345 regcache_cooked_write_unsigned (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, -1);
348 /* Record all registers but IP register for process-record. */
351 i386_all_but_ip_registers_record (struct regcache
*regcache
)
353 if (record_full_arch_list_add_reg (regcache
, I386_EAX_REGNUM
))
355 if (record_full_arch_list_add_reg (regcache
, I386_ECX_REGNUM
))
357 if (record_full_arch_list_add_reg (regcache
, I386_EDX_REGNUM
))
359 if (record_full_arch_list_add_reg (regcache
, I386_EBX_REGNUM
))
361 if (record_full_arch_list_add_reg (regcache
, I386_ESP_REGNUM
))
363 if (record_full_arch_list_add_reg (regcache
, I386_EBP_REGNUM
))
365 if (record_full_arch_list_add_reg (regcache
, I386_ESI_REGNUM
))
367 if (record_full_arch_list_add_reg (regcache
, I386_EDI_REGNUM
))
369 if (record_full_arch_list_add_reg (regcache
, I386_EFLAGS_REGNUM
))
375 /* i386_canonicalize_syscall maps from the native i386 Linux set
376 of syscall ids into a canonical set of syscall ids used by
377 process record (a mostly trivial mapping, since the canonical
378 set was originally taken from the i386 set). */
380 static enum gdb_syscall
381 i386_canonicalize_syscall (int syscall
)
383 enum { i386_syscall_max
= 499 };
385 if (syscall
<= i386_syscall_max
)
386 return (enum gdb_syscall
) syscall
;
388 return gdb_sys_no_syscall
;
391 /* Value of the sigcode in case of a boundary fault. */
393 #define SIG_CODE_BONDARY_FAULT 3
395 /* i386 GNU/Linux implementation of the handle_segmentation_fault
396 gdbarch hook. Displays information related to MPX bound
399 i386_linux_handle_segmentation_fault (struct gdbarch
*gdbarch
,
400 struct ui_out
*uiout
)
402 /* -Wmaybe-uninitialized */
403 CORE_ADDR lower_bound
= 0, upper_bound
= 0, access
= 0;
407 if (!i386_mpx_enabled ())
412 /* Sigcode evaluates if the actual segfault is a boundary violation. */
413 sig_code
= parse_and_eval_long ("$_siginfo.si_code\n");
416 = parse_and_eval_long ("$_siginfo._sifields._sigfault._addr_bnd._lower");
418 = parse_and_eval_long ("$_siginfo._sifields._sigfault._addr_bnd._upper");
420 = parse_and_eval_long ("$_siginfo._sifields._sigfault.si_addr");
422 CATCH (exception
, RETURN_MASK_ALL
)
428 /* If this is not a boundary violation just return. */
429 if (sig_code
!= SIG_CODE_BONDARY_FAULT
)
432 is_upper
= (access
> upper_bound
? 1 : 0);
436 uiout
->field_string ("sigcode-meaning", _("Upper bound violation"));
438 uiout
->field_string ("sigcode-meaning", _("Lower bound violation"));
440 uiout
->text (_(" while accessing address "));
441 uiout
->field_fmt ("bound-access", "%s", paddress (gdbarch
, access
));
443 uiout
->text (_("\nBounds: [lower = "));
444 uiout
->field_fmt ("lower-bound", "%s", paddress (gdbarch
, lower_bound
));
446 uiout
->text (_(", upper = "));
447 uiout
->field_fmt ("upper-bound", "%s", paddress (gdbarch
, upper_bound
));
449 uiout
->text (_("]"));
452 /* Parse the arguments of current system call instruction and record
453 the values of the registers and memory that will be changed into
454 "record_arch_list". This instruction is "int 0x80" (Linux
455 Kernel2.4) or "sysenter" (Linux Kernel 2.6).
457 Return -1 if something wrong. */
459 static struct linux_record_tdep i386_linux_record_tdep
;
462 i386_linux_intx80_sysenter_syscall_record (struct regcache
*regcache
)
465 LONGEST syscall_native
;
466 enum gdb_syscall syscall_gdb
;
468 regcache_raw_read_signed (regcache
, I386_EAX_REGNUM
, &syscall_native
);
470 syscall_gdb
= i386_canonicalize_syscall (syscall_native
);
474 printf_unfiltered (_("Process record and replay target doesn't "
475 "support syscall number %s\n"),
476 plongest (syscall_native
));
480 if (syscall_gdb
== gdb_sys_sigreturn
481 || syscall_gdb
== gdb_sys_rt_sigreturn
)
483 if (i386_all_but_ip_registers_record (regcache
))
488 ret
= record_linux_system_call (syscall_gdb
, regcache
,
489 &i386_linux_record_tdep
);
493 /* Record the return value of the system call. */
494 if (record_full_arch_list_add_reg (regcache
, I386_EAX_REGNUM
))
500 #define I386_LINUX_xstate 270
501 #define I386_LINUX_frame_size 732
504 i386_linux_record_signal (struct gdbarch
*gdbarch
,
505 struct regcache
*regcache
,
506 enum gdb_signal signal
)
510 if (i386_all_but_ip_registers_record (regcache
))
513 if (record_full_arch_list_add_reg (regcache
, I386_EIP_REGNUM
))
516 /* Record the change in the stack. */
517 regcache_raw_read_unsigned (regcache
, I386_ESP_REGNUM
, &esp
);
518 /* This is for xstate.
519 sp -= sizeof (struct _fpstate); */
520 esp
-= I386_LINUX_xstate
;
521 /* This is for frame_size.
522 sp -= sizeof (struct rt_sigframe); */
523 esp
-= I386_LINUX_frame_size
;
524 if (record_full_arch_list_add_mem (esp
,
525 I386_LINUX_xstate
+ I386_LINUX_frame_size
))
528 if (record_full_arch_list_add_end ())
535 /* Core of the implementation for gdbarch get_syscall_number. Get pending
536 syscall number from REGCACHE. If there is no pending syscall -1 will be
537 returned. Pending syscall means ptrace has stepped into the syscall but
538 another ptrace call will step out. PC is right after the int $0x80
539 / syscall / sysenter instruction in both cases, PC does not change during
540 the second ptrace step. */
543 i386_linux_get_syscall_number_from_regcache (struct regcache
*regcache
)
545 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
546 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
547 /* The content of a register. */
552 /* Getting the system call number from the register.
553 When dealing with x86 architecture, this information
554 is stored at %eax register. */
555 regcache_cooked_read (regcache
, I386_LINUX_ORIG_EAX_REGNUM
, buf
);
557 ret
= extract_signed_integer (buf
, 4, byte_order
);
562 /* Wrapper for i386_linux_get_syscall_number_from_regcache to make it
563 compatible with gdbarch get_syscall_number method prototype. */
566 i386_linux_get_syscall_number (struct gdbarch
*gdbarch
,
569 struct regcache
*regcache
= get_thread_regcache (ptid
);
571 return i386_linux_get_syscall_number_from_regcache (regcache
);
574 /* The register sets used in GNU/Linux ELF core-dumps are identical to
575 the register sets in `struct user' that are used for a.out
576 core-dumps. These are also used by ptrace(2). The corresponding
577 types are `elf_gregset_t' for the general-purpose registers (with
578 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
579 for the floating-point registers.
581 Those types used to be available under the names `gregset_t' and
582 `fpregset_t' too, and GDB used those names in the past. But those
583 names are now used for the register sets used in the `mcontext_t'
584 type, which have a different size and layout. */
586 /* Mapping between the general-purpose registers in `struct user'
587 format and GDB's register cache layout. */
589 /* From <sys/reg.h>. */
590 int i386_linux_gregset_reg_offset
[] =
601 14 * 4, /* %eflags */
608 -1, -1, -1, -1, -1, -1, -1, -1,
609 -1, -1, -1, -1, -1, -1, -1, -1,
610 -1, -1, -1, -1, -1, -1, -1, -1,
612 -1, -1, -1, -1, -1, -1, -1, -1,
613 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
614 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
615 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
616 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */
617 -1, /* PKRU register */
618 11 * 4, /* "orig_eax" */
621 /* Mapping between the general-purpose registers in `struct
622 sigcontext' format and GDB's register cache layout. */
624 /* From <asm/sigcontext.h>. */
625 static int i386_linux_sc_reg_offset
[] =
636 16 * 4, /* %eflags */
645 /* Get XSAVE extended state xcr0 from core dump. */
648 i386_linux_core_read_xcr0 (bfd
*abfd
)
650 asection
*xstate
= bfd_get_section_by_name (abfd
, ".reg-xstate");
655 size_t size
= bfd_section_size (abfd
, xstate
);
657 /* Check extended state size. */
658 if (size
< X86_XSTATE_AVX_SIZE
)
659 xcr0
= X86_XSTATE_SSE_MASK
;
664 if (! bfd_get_section_contents (abfd
, xstate
, contents
,
665 I386_LINUX_XSAVE_XCR0_OFFSET
,
668 warning (_("Couldn't read `xcr0' bytes from "
669 "`.reg-xstate' section in core file."));
673 xcr0
= bfd_get_64 (abfd
, contents
);
682 /* See i386-linux-tdep.h. */
684 const struct target_desc
*
685 i386_linux_read_description (uint64_t xcr0
)
690 static struct target_desc
*i386_linux_tdescs \
691 [2/*X87*/][2/*SSE*/][2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/] = {};
692 struct target_desc
**tdesc
;
694 tdesc
= &i386_linux_tdescs
[(xcr0
& X86_XSTATE_X87
) ? 1 : 0]
695 [(xcr0
& X86_XSTATE_SSE
) ? 1 : 0]
696 [(xcr0
& X86_XSTATE_AVX
) ? 1 : 0]
697 [(xcr0
& X86_XSTATE_MPX
) ? 1 : 0]
698 [(xcr0
& X86_XSTATE_AVX512
) ? 1 : 0]
699 [(xcr0
& X86_XSTATE_PKRU
) ? 1 : 0];
703 *tdesc
= allocate_target_description ();
704 set_tdesc_architecture (*tdesc
, bfd_scan_arch ("i386"));
705 set_tdesc_osabi (*tdesc
, osabi_from_tdesc_string ("GNU/Linux"));
709 if (xcr0
& X86_XSTATE_X87
)
710 regnum
= create_feature_i386_32bit_core (*tdesc
, regnum
);
712 if (xcr0
& X86_XSTATE_SSE
)
713 regnum
= create_feature_i386_32bit_sse (*tdesc
, regnum
);
715 regnum
= create_feature_i386_32bit_linux (*tdesc
, regnum
);
717 if (xcr0
& X86_XSTATE_AVX
)
718 regnum
= create_feature_i386_32bit_avx (*tdesc
, regnum
);
720 if (xcr0
& X86_XSTATE_MPX
)
721 regnum
= create_feature_i386_32bit_mpx (*tdesc
, regnum
);
723 if (xcr0
& X86_XSTATE_AVX512
)
724 regnum
= create_feature_i386_32bit_avx512 (*tdesc
, regnum
);
726 if (xcr0
& X86_XSTATE_PKRU
)
727 regnum
= create_feature_i386_32bit_pkeys (*tdesc
, regnum
);
733 /* Get Linux/x86 target description from core dump. */
735 static const struct target_desc
*
736 i386_linux_core_read_description (struct gdbarch
*gdbarch
,
737 struct target_ops
*target
,
741 uint64_t xcr0
= i386_linux_core_read_xcr0 (abfd
);
742 const struct target_desc
*tdesc
= i386_linux_read_description (xcr0
);
747 if (bfd_get_section_by_name (abfd
, ".reg-xfp") != NULL
)
748 return i386_linux_read_description (X86_XSTATE_SSE_MASK
);
750 return i386_linux_read_description (X86_XSTATE_X87_MASK
);
753 /* Similar to i386_supply_fpregset, but use XSAVE extended state. */
756 i386_linux_supply_xstateregset (const struct regset
*regset
,
757 struct regcache
*regcache
, int regnum
,
758 const void *xstateregs
, size_t len
)
760 i387_supply_xsave (regcache
, regnum
, xstateregs
);
764 x86_linux_get_siginfo_type (struct gdbarch
*gdbarch
)
766 return linux_get_siginfo_type_with_fields (gdbarch
, LINUX_SIGINFO_FIELD_ADDR_BND
);
769 /* Similar to i386_collect_fpregset, but use XSAVE extended state. */
772 i386_linux_collect_xstateregset (const struct regset
*regset
,
773 const struct regcache
*regcache
,
774 int regnum
, void *xstateregs
, size_t len
)
776 i387_collect_xsave (regcache
, regnum
, xstateregs
, 1);
779 /* Register set definitions. */
781 static const struct regset i386_linux_xstateregset
=
784 i386_linux_supply_xstateregset
,
785 i386_linux_collect_xstateregset
788 /* Iterate over core file register note sections. */
791 i386_linux_iterate_over_regset_sections (struct gdbarch
*gdbarch
,
792 iterate_over_regset_sections_cb
*cb
,
794 const struct regcache
*regcache
)
796 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
798 cb (".reg", 68, &i386_gregset
, NULL
, cb_data
);
800 if (tdep
->xcr0
& X86_XSTATE_AVX
)
801 cb (".reg-xstate", X86_XSTATE_SIZE (tdep
->xcr0
),
802 &i386_linux_xstateregset
, "XSAVE extended state", cb_data
);
803 else if (tdep
->xcr0
& X86_XSTATE_SSE
)
804 cb (".reg-xfp", 512, &i386_fpregset
, "extended floating-point",
807 cb (".reg2", 108, &i386_fpregset
, NULL
, cb_data
);
810 /* Linux kernel shows PC value after the 'int $0x80' instruction even if
811 inferior is still inside the syscall. On next PTRACE_SINGLESTEP it will
812 finish the syscall but PC will not change.
814 Some vDSOs contain 'int $0x80; ret' and during stepping out of the syscall
815 i386_displaced_step_fixup would keep PC at the displaced pad location.
816 As PC is pointing to the 'ret' instruction before the step
817 i386_displaced_step_fixup would expect inferior has just executed that 'ret'
818 and PC should not be adjusted. In reality it finished syscall instead and
819 PC should get relocated back to its vDSO address. Hide the 'ret'
820 instruction by 'nop' so that i386_displaced_step_fixup is not confused.
822 It is not fully correct as the bytes in struct displaced_step_closure will
823 not match the inferior code. But we would need some new flag in
824 displaced_step_closure otherwise to keep the state that syscall is finishing
825 for the later i386_displaced_step_fixup execution as the syscall execution
826 is already no longer detectable there. The new flag field would mean
827 i386-linux-tdep.c needs to wrap all the displacement methods of i386-tdep.c
828 which does not seem worth it. The same effect is achieved by patching that
829 'nop' instruction there instead. */
831 static struct displaced_step_closure
*
832 i386_linux_displaced_step_copy_insn (struct gdbarch
*gdbarch
,
833 CORE_ADDR from
, CORE_ADDR to
,
834 struct regcache
*regs
)
836 struct displaced_step_closure
*closure
;
838 closure
= i386_displaced_step_copy_insn (gdbarch
, from
, to
, regs
);
840 if (i386_linux_get_syscall_number_from_regcache (regs
) != -1)
842 /* Since we use simple_displaced_step_copy_insn, our closure is a
843 copy of the instruction. */
844 gdb_byte
*insn
= (gdb_byte
*) closure
;
854 i386_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
856 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
857 const struct target_desc
*tdesc
= info
.target_desc
;
858 struct tdesc_arch_data
*tdesc_data
= info
.tdesc_data
;
859 const struct tdesc_feature
*feature
;
862 gdb_assert (tdesc_data
);
864 linux_init_abi (info
, gdbarch
);
866 /* GNU/Linux uses ELF. */
867 i386_elf_init_abi (info
, gdbarch
);
869 /* Reserve a number for orig_eax. */
870 set_gdbarch_num_regs (gdbarch
, I386_LINUX_NUM_REGS
);
872 if (! tdesc_has_registers (tdesc
))
873 tdesc
= i386_linux_read_description (X86_XSTATE_SSE_MASK
);
876 feature
= tdesc_find_feature (tdesc
, "org.gnu.gdb.i386.linux");
880 valid_p
= tdesc_numbered_register (feature
, tdesc_data
,
881 I386_LINUX_ORIG_EAX_REGNUM
,
886 /* Add the %orig_eax register used for syscall restarting. */
887 set_gdbarch_write_pc (gdbarch
, i386_linux_write_pc
);
889 tdep
->register_reggroup_p
= i386_linux_register_reggroup_p
;
891 tdep
->gregset_reg_offset
= i386_linux_gregset_reg_offset
;
892 tdep
->gregset_num_regs
= ARRAY_SIZE (i386_linux_gregset_reg_offset
);
893 tdep
->sizeof_gregset
= 17 * 4;
895 tdep
->jb_pc_offset
= 20; /* From <bits/setjmp.h>. */
897 tdep
->sigtramp_p
= i386_linux_sigtramp_p
;
898 tdep
->sigcontext_addr
= i386_linux_sigcontext_addr
;
899 tdep
->sc_reg_offset
= i386_linux_sc_reg_offset
;
900 tdep
->sc_num_regs
= ARRAY_SIZE (i386_linux_sc_reg_offset
);
902 tdep
->xsave_xcr0_offset
= I386_LINUX_XSAVE_XCR0_OFFSET
;
904 set_gdbarch_process_record (gdbarch
, i386_process_record
);
905 set_gdbarch_process_record_signal (gdbarch
, i386_linux_record_signal
);
907 /* Initialize the i386_linux_record_tdep. */
908 /* These values are the size of the type that will be used in a system
909 call. They are obtained from Linux Kernel source. */
910 i386_linux_record_tdep
.size_pointer
911 = gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
912 i386_linux_record_tdep
.size__old_kernel_stat
= 32;
913 i386_linux_record_tdep
.size_tms
= 16;
914 i386_linux_record_tdep
.size_loff_t
= 8;
915 i386_linux_record_tdep
.size_flock
= 16;
916 i386_linux_record_tdep
.size_oldold_utsname
= 45;
917 i386_linux_record_tdep
.size_ustat
= 20;
918 i386_linux_record_tdep
.size_old_sigaction
= 16;
919 i386_linux_record_tdep
.size_old_sigset_t
= 4;
920 i386_linux_record_tdep
.size_rlimit
= 8;
921 i386_linux_record_tdep
.size_rusage
= 72;
922 i386_linux_record_tdep
.size_timeval
= 8;
923 i386_linux_record_tdep
.size_timezone
= 8;
924 i386_linux_record_tdep
.size_old_gid_t
= 2;
925 i386_linux_record_tdep
.size_old_uid_t
= 2;
926 i386_linux_record_tdep
.size_fd_set
= 128;
927 i386_linux_record_tdep
.size_old_dirent
= 268;
928 i386_linux_record_tdep
.size_statfs
= 64;
929 i386_linux_record_tdep
.size_statfs64
= 84;
930 i386_linux_record_tdep
.size_sockaddr
= 16;
931 i386_linux_record_tdep
.size_int
932 = gdbarch_int_bit (gdbarch
) / TARGET_CHAR_BIT
;
933 i386_linux_record_tdep
.size_long
934 = gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
935 i386_linux_record_tdep
.size_ulong
936 = gdbarch_long_bit (gdbarch
) / TARGET_CHAR_BIT
;
937 i386_linux_record_tdep
.size_msghdr
= 28;
938 i386_linux_record_tdep
.size_itimerval
= 16;
939 i386_linux_record_tdep
.size_stat
= 88;
940 i386_linux_record_tdep
.size_old_utsname
= 325;
941 i386_linux_record_tdep
.size_sysinfo
= 64;
942 i386_linux_record_tdep
.size_msqid_ds
= 88;
943 i386_linux_record_tdep
.size_shmid_ds
= 84;
944 i386_linux_record_tdep
.size_new_utsname
= 390;
945 i386_linux_record_tdep
.size_timex
= 128;
946 i386_linux_record_tdep
.size_mem_dqinfo
= 24;
947 i386_linux_record_tdep
.size_if_dqblk
= 68;
948 i386_linux_record_tdep
.size_fs_quota_stat
= 68;
949 i386_linux_record_tdep
.size_timespec
= 8;
950 i386_linux_record_tdep
.size_pollfd
= 8;
951 i386_linux_record_tdep
.size_NFS_FHSIZE
= 32;
952 i386_linux_record_tdep
.size_knfsd_fh
= 132;
953 i386_linux_record_tdep
.size_TASK_COMM_LEN
= 16;
954 i386_linux_record_tdep
.size_sigaction
= 20;
955 i386_linux_record_tdep
.size_sigset_t
= 8;
956 i386_linux_record_tdep
.size_siginfo_t
= 128;
957 i386_linux_record_tdep
.size_cap_user_data_t
= 12;
958 i386_linux_record_tdep
.size_stack_t
= 12;
959 i386_linux_record_tdep
.size_off_t
= i386_linux_record_tdep
.size_long
;
960 i386_linux_record_tdep
.size_stat64
= 96;
961 i386_linux_record_tdep
.size_gid_t
= 4;
962 i386_linux_record_tdep
.size_uid_t
= 4;
963 i386_linux_record_tdep
.size_PAGE_SIZE
= 4096;
964 i386_linux_record_tdep
.size_flock64
= 24;
965 i386_linux_record_tdep
.size_user_desc
= 16;
966 i386_linux_record_tdep
.size_io_event
= 32;
967 i386_linux_record_tdep
.size_iocb
= 64;
968 i386_linux_record_tdep
.size_epoll_event
= 12;
969 i386_linux_record_tdep
.size_itimerspec
970 = i386_linux_record_tdep
.size_timespec
* 2;
971 i386_linux_record_tdep
.size_mq_attr
= 32;
972 i386_linux_record_tdep
.size_termios
= 36;
973 i386_linux_record_tdep
.size_termios2
= 44;
974 i386_linux_record_tdep
.size_pid_t
= 4;
975 i386_linux_record_tdep
.size_winsize
= 8;
976 i386_linux_record_tdep
.size_serial_struct
= 60;
977 i386_linux_record_tdep
.size_serial_icounter_struct
= 80;
978 i386_linux_record_tdep
.size_hayes_esp_config
= 12;
979 i386_linux_record_tdep
.size_size_t
= 4;
980 i386_linux_record_tdep
.size_iovec
= 8;
981 i386_linux_record_tdep
.size_time_t
= 4;
983 /* These values are the second argument of system call "sys_ioctl".
984 They are obtained from Linux Kernel source. */
985 i386_linux_record_tdep
.ioctl_TCGETS
= 0x5401;
986 i386_linux_record_tdep
.ioctl_TCSETS
= 0x5402;
987 i386_linux_record_tdep
.ioctl_TCSETSW
= 0x5403;
988 i386_linux_record_tdep
.ioctl_TCSETSF
= 0x5404;
989 i386_linux_record_tdep
.ioctl_TCGETA
= 0x5405;
990 i386_linux_record_tdep
.ioctl_TCSETA
= 0x5406;
991 i386_linux_record_tdep
.ioctl_TCSETAW
= 0x5407;
992 i386_linux_record_tdep
.ioctl_TCSETAF
= 0x5408;
993 i386_linux_record_tdep
.ioctl_TCSBRK
= 0x5409;
994 i386_linux_record_tdep
.ioctl_TCXONC
= 0x540A;
995 i386_linux_record_tdep
.ioctl_TCFLSH
= 0x540B;
996 i386_linux_record_tdep
.ioctl_TIOCEXCL
= 0x540C;
997 i386_linux_record_tdep
.ioctl_TIOCNXCL
= 0x540D;
998 i386_linux_record_tdep
.ioctl_TIOCSCTTY
= 0x540E;
999 i386_linux_record_tdep
.ioctl_TIOCGPGRP
= 0x540F;
1000 i386_linux_record_tdep
.ioctl_TIOCSPGRP
= 0x5410;
1001 i386_linux_record_tdep
.ioctl_TIOCOUTQ
= 0x5411;
1002 i386_linux_record_tdep
.ioctl_TIOCSTI
= 0x5412;
1003 i386_linux_record_tdep
.ioctl_TIOCGWINSZ
= 0x5413;
1004 i386_linux_record_tdep
.ioctl_TIOCSWINSZ
= 0x5414;
1005 i386_linux_record_tdep
.ioctl_TIOCMGET
= 0x5415;
1006 i386_linux_record_tdep
.ioctl_TIOCMBIS
= 0x5416;
1007 i386_linux_record_tdep
.ioctl_TIOCMBIC
= 0x5417;
1008 i386_linux_record_tdep
.ioctl_TIOCMSET
= 0x5418;
1009 i386_linux_record_tdep
.ioctl_TIOCGSOFTCAR
= 0x5419;
1010 i386_linux_record_tdep
.ioctl_TIOCSSOFTCAR
= 0x541A;
1011 i386_linux_record_tdep
.ioctl_FIONREAD
= 0x541B;
1012 i386_linux_record_tdep
.ioctl_TIOCINQ
= i386_linux_record_tdep
.ioctl_FIONREAD
;
1013 i386_linux_record_tdep
.ioctl_TIOCLINUX
= 0x541C;
1014 i386_linux_record_tdep
.ioctl_TIOCCONS
= 0x541D;
1015 i386_linux_record_tdep
.ioctl_TIOCGSERIAL
= 0x541E;
1016 i386_linux_record_tdep
.ioctl_TIOCSSERIAL
= 0x541F;
1017 i386_linux_record_tdep
.ioctl_TIOCPKT
= 0x5420;
1018 i386_linux_record_tdep
.ioctl_FIONBIO
= 0x5421;
1019 i386_linux_record_tdep
.ioctl_TIOCNOTTY
= 0x5422;
1020 i386_linux_record_tdep
.ioctl_TIOCSETD
= 0x5423;
1021 i386_linux_record_tdep
.ioctl_TIOCGETD
= 0x5424;
1022 i386_linux_record_tdep
.ioctl_TCSBRKP
= 0x5425;
1023 i386_linux_record_tdep
.ioctl_TIOCTTYGSTRUCT
= 0x5426;
1024 i386_linux_record_tdep
.ioctl_TIOCSBRK
= 0x5427;
1025 i386_linux_record_tdep
.ioctl_TIOCCBRK
= 0x5428;
1026 i386_linux_record_tdep
.ioctl_TIOCGSID
= 0x5429;
1027 i386_linux_record_tdep
.ioctl_TCGETS2
= 0x802c542a;
1028 i386_linux_record_tdep
.ioctl_TCSETS2
= 0x402c542b;
1029 i386_linux_record_tdep
.ioctl_TCSETSW2
= 0x402c542c;
1030 i386_linux_record_tdep
.ioctl_TCSETSF2
= 0x402c542d;
1031 i386_linux_record_tdep
.ioctl_TIOCGPTN
= 0x80045430;
1032 i386_linux_record_tdep
.ioctl_TIOCSPTLCK
= 0x40045431;
1033 i386_linux_record_tdep
.ioctl_FIONCLEX
= 0x5450;
1034 i386_linux_record_tdep
.ioctl_FIOCLEX
= 0x5451;
1035 i386_linux_record_tdep
.ioctl_FIOASYNC
= 0x5452;
1036 i386_linux_record_tdep
.ioctl_TIOCSERCONFIG
= 0x5453;
1037 i386_linux_record_tdep
.ioctl_TIOCSERGWILD
= 0x5454;
1038 i386_linux_record_tdep
.ioctl_TIOCSERSWILD
= 0x5455;
1039 i386_linux_record_tdep
.ioctl_TIOCGLCKTRMIOS
= 0x5456;
1040 i386_linux_record_tdep
.ioctl_TIOCSLCKTRMIOS
= 0x5457;
1041 i386_linux_record_tdep
.ioctl_TIOCSERGSTRUCT
= 0x5458;
1042 i386_linux_record_tdep
.ioctl_TIOCSERGETLSR
= 0x5459;
1043 i386_linux_record_tdep
.ioctl_TIOCSERGETMULTI
= 0x545A;
1044 i386_linux_record_tdep
.ioctl_TIOCSERSETMULTI
= 0x545B;
1045 i386_linux_record_tdep
.ioctl_TIOCMIWAIT
= 0x545C;
1046 i386_linux_record_tdep
.ioctl_TIOCGICOUNT
= 0x545D;
1047 i386_linux_record_tdep
.ioctl_TIOCGHAYESESP
= 0x545E;
1048 i386_linux_record_tdep
.ioctl_TIOCSHAYESESP
= 0x545F;
1049 i386_linux_record_tdep
.ioctl_FIOQSIZE
= 0x5460;
1051 /* These values are the second argument of system call "sys_fcntl"
1052 and "sys_fcntl64". They are obtained from Linux Kernel source. */
1053 i386_linux_record_tdep
.fcntl_F_GETLK
= 5;
1054 i386_linux_record_tdep
.fcntl_F_GETLK64
= 12;
1055 i386_linux_record_tdep
.fcntl_F_SETLK64
= 13;
1056 i386_linux_record_tdep
.fcntl_F_SETLKW64
= 14;
1058 i386_linux_record_tdep
.arg1
= I386_EBX_REGNUM
;
1059 i386_linux_record_tdep
.arg2
= I386_ECX_REGNUM
;
1060 i386_linux_record_tdep
.arg3
= I386_EDX_REGNUM
;
1061 i386_linux_record_tdep
.arg4
= I386_ESI_REGNUM
;
1062 i386_linux_record_tdep
.arg5
= I386_EDI_REGNUM
;
1063 i386_linux_record_tdep
.arg6
= I386_EBP_REGNUM
;
1065 tdep
->i386_intx80_record
= i386_linux_intx80_sysenter_syscall_record
;
1066 tdep
->i386_sysenter_record
= i386_linux_intx80_sysenter_syscall_record
;
1067 tdep
->i386_syscall_record
= i386_linux_intx80_sysenter_syscall_record
;
1069 /* N_FUN symbols in shared libaries have 0 for their values and need
1071 set_gdbarch_sofun_address_maybe_missing (gdbarch
, 1);
1073 /* GNU/Linux uses SVR4-style shared libraries. */
1074 set_gdbarch_skip_trampoline_code (gdbarch
, find_solib_trampoline_target
);
1075 set_solib_svr4_fetch_link_map_offsets
1076 (gdbarch
, svr4_ilp32_fetch_link_map_offsets
);
1078 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
1079 set_gdbarch_skip_solib_resolver (gdbarch
, glibc_skip_solib_resolver
);
1081 dwarf2_frame_set_signal_frame_p (gdbarch
, i386_linux_dwarf_signal_frame_p
);
1083 /* Enable TLS support. */
1084 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
1085 svr4_fetch_objfile_link_map
);
1087 /* Core file support. */
1088 set_gdbarch_iterate_over_regset_sections
1089 (gdbarch
, i386_linux_iterate_over_regset_sections
);
1090 set_gdbarch_core_read_description (gdbarch
,
1091 i386_linux_core_read_description
);
1093 /* Displaced stepping. */
1094 set_gdbarch_displaced_step_copy_insn (gdbarch
,
1095 i386_linux_displaced_step_copy_insn
);
1096 set_gdbarch_displaced_step_fixup (gdbarch
, i386_displaced_step_fixup
);
1097 set_gdbarch_displaced_step_location (gdbarch
,
1098 linux_displaced_step_location
);
1100 /* Functions for 'catch syscall'. */
1101 set_xml_syscall_file_name (gdbarch
, XML_SYSCALL_FILENAME_I386
);
1102 set_gdbarch_get_syscall_number (gdbarch
,
1103 i386_linux_get_syscall_number
);
1105 set_gdbarch_get_siginfo_type (gdbarch
, x86_linux_get_siginfo_type
);
1106 set_gdbarch_handle_segmentation_fault (gdbarch
,
1107 i386_linux_handle_segmentation_fault
);
1110 /* Provide a prototype to silence -Wmissing-prototypes. */
1111 extern void _initialize_i386_linux_tdep (void);
1114 _initialize_i386_linux_tdep (void)
1116 gdbarch_register_osabi (bfd_arch_i386
, 0, GDB_OSABI_LINUX
,
1117 i386_linux_init_abi
);
1125 { "i386/i386-linux.xml", X86_XSTATE_SSE_MASK
},
1126 { "i386/i386-mmx-linux.xml", X86_XSTATE_X87_MASK
},
1127 { "i386/i386-avx-linux.xml", X86_XSTATE_AVX_MASK
},
1128 { "i386/i386-mpx-linux.xml", X86_XSTATE_MPX_MASK
},
1129 { "i386/i386-avx-mpx-linux.xml", X86_XSTATE_AVX_MPX_MASK
},
1130 { "i386/i386-avx-avx512-linux.xml", X86_XSTATE_AVX_AVX512_MASK
},
1131 { "i386/i386-avx-mpx-avx512-pku-linux.xml",
1132 X86_XSTATE_AVX_MPX_AVX512_PKU_MASK
},
1135 for (auto &a
: xml_masks
)
1137 auto tdesc
= i386_linux_read_description (a
.mask
);
1139 selftests::record_xml_tdesc (a
.xml
, tdesc
);
1141 #endif /* GDB_SELF_TEST */