Intel MPX bound violation handling
[deliverable/binutils-gdb.git] / gdb / i386-linux-tdep.c
1 /* Target-dependent code for GNU/Linux i386.
2
3 Copyright (C) 2000-2016 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "gdbcore.h"
22 #include "frame.h"
23 #include "value.h"
24 #include "regcache.h"
25 #include "regset.h"
26 #include "inferior.h"
27 #include "osabi.h"
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"
33 #include "utils.h"
34 #include "glibc-tdep.h"
35 #include "solib-svr4.h"
36 #include "symtab.h"
37 #include "arch-utils.h"
38 #include "xml-syscall.h"
39
40 #include "i387-tdep.h"
41 #include "x86-xstate.h"
42
43 /* The syscall's XML filename for i386. */
44 #define XML_SYSCALL_FILENAME_I386 "syscalls/i386-linux.xml"
45
46 #include "record-full.h"
47 #include "linux-record.h"
48 #include "features/i386/i386-linux.c"
49 #include "features/i386/i386-mmx-linux.c"
50 #include "features/i386/i386-mpx-linux.c"
51 #include "features/i386/i386-avx-linux.c"
52 #include "features/i386/i386-avx512-linux.c"
53
54 /* Return non-zero, when the register is in the corresponding register
55 group. Put the LINUX_ORIG_EAX register in the system group. */
56 static int
57 i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
58 struct reggroup *group)
59 {
60 if (regnum == I386_LINUX_ORIG_EAX_REGNUM)
61 return (group == system_reggroup
62 || group == save_reggroup
63 || group == restore_reggroup);
64 return i386_register_reggroup_p (gdbarch, regnum, group);
65 }
66
67 \f
68 /* Recognizing signal handler frames. */
69
70 /* GNU/Linux has two flavors of signals. Normal signal handlers, and
71 "realtime" (RT) signals. The RT signals can provide additional
72 information to the signal handler if the SA_SIGINFO flag is set
73 when establishing a signal handler using `sigaction'. It is not
74 unlikely that future versions of GNU/Linux will support SA_SIGINFO
75 for normal signals too. */
76
77 /* When the i386 Linux kernel calls a signal handler and the
78 SA_RESTORER flag isn't set, the return address points to a bit of
79 code on the stack. This function returns whether the PC appears to
80 be within this bit of code.
81
82 The instruction sequence for normal signals is
83 pop %eax
84 mov $0x77, %eax
85 int $0x80
86 or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
87
88 Checking for the code sequence should be somewhat reliable, because
89 the effect is to call the system call sigreturn. This is unlikely
90 to occur anywhere other than in a signal trampoline.
91
92 It kind of sucks that we have to read memory from the process in
93 order to identify a signal trampoline, but there doesn't seem to be
94 any other way. Therefore we only do the memory reads if no
95 function name could be identified, which should be the case since
96 the code is on the stack.
97
98 Detection of signal trampolines for handlers that set the
99 SA_RESTORER flag is in general not possible. Unfortunately this is
100 what the GNU C Library has been doing for quite some time now.
101 However, as of version 2.1.2, the GNU C Library uses signal
102 trampolines (named __restore and __restore_rt) that are identical
103 to the ones used by the kernel. Therefore, these trampolines are
104 supported too. */
105
106 #define LINUX_SIGTRAMP_INSN0 0x58 /* pop %eax */
107 #define LINUX_SIGTRAMP_OFFSET0 0
108 #define LINUX_SIGTRAMP_INSN1 0xb8 /* mov $NNNN, %eax */
109 #define LINUX_SIGTRAMP_OFFSET1 1
110 #define LINUX_SIGTRAMP_INSN2 0xcd /* int */
111 #define LINUX_SIGTRAMP_OFFSET2 6
112
113 static const gdb_byte linux_sigtramp_code[] =
114 {
115 LINUX_SIGTRAMP_INSN0, /* pop %eax */
116 LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00, /* mov $0x77, %eax */
117 LINUX_SIGTRAMP_INSN2, 0x80 /* int $0x80 */
118 };
119
120 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
121
122 /* If THIS_FRAME is a sigtramp routine, return the address of the
123 start of the routine. Otherwise, return 0. */
124
125 static CORE_ADDR
126 i386_linux_sigtramp_start (struct frame_info *this_frame)
127 {
128 CORE_ADDR pc = get_frame_pc (this_frame);
129 gdb_byte buf[LINUX_SIGTRAMP_LEN];
130
131 /* We only recognize a signal trampoline if PC is at the start of
132 one of the three instructions. We optimize for finding the PC at
133 the start, as will be the case when the trampoline is not the
134 first frame on the stack. We assume that in the case where the
135 PC is not at the start of the instruction sequence, there will be
136 a few trailing readable bytes on the stack. */
137
138 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
139 return 0;
140
141 if (buf[0] != LINUX_SIGTRAMP_INSN0)
142 {
143 int adjust;
144
145 switch (buf[0])
146 {
147 case LINUX_SIGTRAMP_INSN1:
148 adjust = LINUX_SIGTRAMP_OFFSET1;
149 break;
150 case LINUX_SIGTRAMP_INSN2:
151 adjust = LINUX_SIGTRAMP_OFFSET2;
152 break;
153 default:
154 return 0;
155 }
156
157 pc -= adjust;
158
159 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
160 return 0;
161 }
162
163 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
164 return 0;
165
166 return pc;
167 }
168
169 /* This function does the same for RT signals. Here the instruction
170 sequence is
171 mov $0xad, %eax
172 int $0x80
173 or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
174
175 The effect is to call the system call rt_sigreturn. */
176
177 #define LINUX_RT_SIGTRAMP_INSN0 0xb8 /* mov $NNNN, %eax */
178 #define LINUX_RT_SIGTRAMP_OFFSET0 0
179 #define LINUX_RT_SIGTRAMP_INSN1 0xcd /* int */
180 #define LINUX_RT_SIGTRAMP_OFFSET1 5
181
182 static const gdb_byte linux_rt_sigtramp_code[] =
183 {
184 LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00, /* mov $0xad, %eax */
185 LINUX_RT_SIGTRAMP_INSN1, 0x80 /* int $0x80 */
186 };
187
188 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
189
190 /* If THIS_FRAME is an RT sigtramp routine, return the address of the
191 start of the routine. Otherwise, return 0. */
192
193 static CORE_ADDR
194 i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
195 {
196 CORE_ADDR pc = get_frame_pc (this_frame);
197 gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
198
199 /* We only recognize a signal trampoline if PC is at the start of
200 one of the two instructions. We optimize for finding the PC at
201 the start, as will be the case when the trampoline is not the
202 first frame on the stack. We assume that in the case where the
203 PC is not at the start of the instruction sequence, there will be
204 a few trailing readable bytes on the stack. */
205
206 if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
207 return 0;
208
209 if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
210 {
211 if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
212 return 0;
213
214 pc -= LINUX_RT_SIGTRAMP_OFFSET1;
215
216 if (!safe_frame_unwind_memory (this_frame, pc, buf,
217 LINUX_RT_SIGTRAMP_LEN))
218 return 0;
219 }
220
221 if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
222 return 0;
223
224 return pc;
225 }
226
227 /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
228 routine. */
229
230 static int
231 i386_linux_sigtramp_p (struct frame_info *this_frame)
232 {
233 CORE_ADDR pc = get_frame_pc (this_frame);
234 const char *name;
235
236 find_pc_partial_function (pc, &name, NULL, NULL);
237
238 /* If we have NAME, we can optimize the search. The trampolines are
239 named __restore and __restore_rt. However, they aren't dynamically
240 exported from the shared C library, so the trampoline may appear to
241 be part of the preceding function. This should always be sigaction,
242 __sigaction, or __libc_sigaction (all aliases to the same function). */
243 if (name == NULL || strstr (name, "sigaction") != NULL)
244 return (i386_linux_sigtramp_start (this_frame) != 0
245 || i386_linux_rt_sigtramp_start (this_frame) != 0);
246
247 return (strcmp ("__restore", name) == 0
248 || strcmp ("__restore_rt", name) == 0);
249 }
250
251 /* Return one if the PC of THIS_FRAME is in a signal trampoline which
252 may have DWARF-2 CFI. */
253
254 static int
255 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
256 struct frame_info *this_frame)
257 {
258 CORE_ADDR pc = get_frame_pc (this_frame);
259 const char *name;
260
261 find_pc_partial_function (pc, &name, NULL, NULL);
262
263 /* If a vsyscall DSO is in use, the signal trampolines may have these
264 names. */
265 if (name && (strcmp (name, "__kernel_sigreturn") == 0
266 || strcmp (name, "__kernel_rt_sigreturn") == 0))
267 return 1;
268
269 return 0;
270 }
271
272 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
273 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
274
275 /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
276 address of the associated sigcontext structure. */
277
278 static CORE_ADDR
279 i386_linux_sigcontext_addr (struct frame_info *this_frame)
280 {
281 struct gdbarch *gdbarch = get_frame_arch (this_frame);
282 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
283 CORE_ADDR pc;
284 CORE_ADDR sp;
285 gdb_byte buf[4];
286
287 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
288 sp = extract_unsigned_integer (buf, 4, byte_order);
289
290 pc = i386_linux_sigtramp_start (this_frame);
291 if (pc)
292 {
293 /* The sigcontext structure lives on the stack, right after
294 the signum argument. We determine the address of the
295 sigcontext structure by looking at the frame's stack
296 pointer. Keep in mind that the first instruction of the
297 sigtramp code is "pop %eax". If the PC is after this
298 instruction, adjust the returned value accordingly. */
299 if (pc == get_frame_pc (this_frame))
300 return sp + 4;
301 return sp;
302 }
303
304 pc = i386_linux_rt_sigtramp_start (this_frame);
305 if (pc)
306 {
307 CORE_ADDR ucontext_addr;
308
309 /* The sigcontext structure is part of the user context. A
310 pointer to the user context is passed as the third argument
311 to the signal handler. */
312 read_memory (sp + 8, buf, 4);
313 ucontext_addr = extract_unsigned_integer (buf, 4, byte_order);
314 return ucontext_addr + I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
315 }
316
317 error (_("Couldn't recognize signal trampoline."));
318 return 0;
319 }
320
321 /* Set the program counter for process PTID to PC. */
322
323 static void
324 i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
325 {
326 regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
327
328 /* We must be careful with modifying the program counter. If we
329 just interrupted a system call, the kernel might try to restart
330 it when we resume the inferior. On restarting the system call,
331 the kernel will try backing up the program counter even though it
332 no longer points at the system call. This typically results in a
333 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
334 "orig_eax" pseudo-register.
335
336 Note that "orig_eax" is saved when setting up a dummy call frame.
337 This means that it is properly restored when that frame is
338 popped, and that the interrupted system call will be restarted
339 when we resume the inferior on return from a function call from
340 within GDB. In all other cases the system call will not be
341 restarted. */
342 regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
343 }
344
345 /* Record all registers but IP register for process-record. */
346
347 static int
348 i386_all_but_ip_registers_record (struct regcache *regcache)
349 {
350 if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
351 return -1;
352 if (record_full_arch_list_add_reg (regcache, I386_ECX_REGNUM))
353 return -1;
354 if (record_full_arch_list_add_reg (regcache, I386_EDX_REGNUM))
355 return -1;
356 if (record_full_arch_list_add_reg (regcache, I386_EBX_REGNUM))
357 return -1;
358 if (record_full_arch_list_add_reg (regcache, I386_ESP_REGNUM))
359 return -1;
360 if (record_full_arch_list_add_reg (regcache, I386_EBP_REGNUM))
361 return -1;
362 if (record_full_arch_list_add_reg (regcache, I386_ESI_REGNUM))
363 return -1;
364 if (record_full_arch_list_add_reg (regcache, I386_EDI_REGNUM))
365 return -1;
366 if (record_full_arch_list_add_reg (regcache, I386_EFLAGS_REGNUM))
367 return -1;
368
369 return 0;
370 }
371
372 /* i386_canonicalize_syscall maps from the native i386 Linux set
373 of syscall ids into a canonical set of syscall ids used by
374 process record (a mostly trivial mapping, since the canonical
375 set was originally taken from the i386 set). */
376
377 static enum gdb_syscall
378 i386_canonicalize_syscall (int syscall)
379 {
380 enum { i386_syscall_max = 499 };
381
382 if (syscall <= i386_syscall_max)
383 return (enum gdb_syscall) syscall;
384 else
385 return gdb_sys_no_syscall;
386 }
387
388 /* Value of the sigcode in case of a boundary fault. */
389
390 #define SIG_CODE_BONDARY_FAULT 3
391
392 /* i386 GNU/Linux implementation of the handle_segmentation_fault
393 gdbarch hook. Displays information related to MPX bound
394 violations. */
395 void
396 i386_linux_handle_segmentation_fault (struct gdbarch *gdbarch,
397 struct ui_out *uiout)
398 {
399 CORE_ADDR lower_bound, upper_bound, access;
400 int is_upper;
401 long sig_code = 0;
402
403 if (!i386_mpx_enabled ())
404 return;
405
406 TRY
407 {
408 /* Sigcode evaluates if the actual segfault is a boundary violation. */
409 sig_code = parse_and_eval_long ("$_siginfo.si_code\n");
410
411 lower_bound
412 = parse_and_eval_long ("$_siginfo._sifields._sigfault._addr_bnd._lower");
413 upper_bound
414 = parse_and_eval_long ("$_siginfo._sifields._sigfault._addr_bnd._upper");
415 access
416 = parse_and_eval_long ("$_siginfo._sifields._sigfault.si_addr");
417 }
418 CATCH (exception, RETURN_MASK_ALL)
419 {
420 return;
421 }
422 END_CATCH
423
424 /* If this is not a boundary violation just return. */
425 if (sig_code != SIG_CODE_BONDARY_FAULT)
426 return;
427
428 is_upper = (access > upper_bound ? 1 : 0);
429
430 ui_out_text (uiout, "\n");
431 if (is_upper)
432 ui_out_field_string (uiout, "sigcode-meaning",
433 _("Upper bound violation"));
434 else
435 ui_out_field_string (uiout, "sigcode-meaning",
436 _("Lower bound violation"));
437
438 ui_out_text (uiout, _(" while accessing address "));
439 ui_out_field_fmt (uiout, "bound-access", "%s",
440 paddress (gdbarch, access));
441
442 ui_out_text (uiout, _("\nBounds: [lower = "));
443 ui_out_field_fmt (uiout, "lower-bound", "%s",
444 paddress (gdbarch, lower_bound));
445
446 ui_out_text (uiout, _(", upper = "));
447 ui_out_field_fmt (uiout, "upper-bound", "%s",
448 paddress (gdbarch, upper_bound));
449
450 ui_out_text (uiout, _("]"));
451 }
452
453 /* Parse the arguments of current system call instruction and record
454 the values of the registers and memory that will be changed into
455 "record_arch_list". This instruction is "int 0x80" (Linux
456 Kernel2.4) or "sysenter" (Linux Kernel 2.6).
457
458 Return -1 if something wrong. */
459
460 static struct linux_record_tdep i386_linux_record_tdep;
461
462 static int
463 i386_linux_intx80_sysenter_syscall_record (struct regcache *regcache)
464 {
465 int ret;
466 LONGEST syscall_native;
467 enum gdb_syscall syscall_gdb;
468
469 regcache_raw_read_signed (regcache, I386_EAX_REGNUM, &syscall_native);
470
471 syscall_gdb = i386_canonicalize_syscall (syscall_native);
472
473 if (syscall_gdb < 0)
474 {
475 printf_unfiltered (_("Process record and replay target doesn't "
476 "support syscall number %s\n"),
477 plongest (syscall_native));
478 return -1;
479 }
480
481 if (syscall_gdb == gdb_sys_sigreturn
482 || syscall_gdb == gdb_sys_rt_sigreturn)
483 {
484 if (i386_all_but_ip_registers_record (regcache))
485 return -1;
486 return 0;
487 }
488
489 ret = record_linux_system_call (syscall_gdb, regcache,
490 &i386_linux_record_tdep);
491 if (ret)
492 return ret;
493
494 /* Record the return value of the system call. */
495 if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
496 return -1;
497
498 return 0;
499 }
500
501 #define I386_LINUX_xstate 270
502 #define I386_LINUX_frame_size 732
503
504 static int
505 i386_linux_record_signal (struct gdbarch *gdbarch,
506 struct regcache *regcache,
507 enum gdb_signal signal)
508 {
509 ULONGEST esp;
510
511 if (i386_all_but_ip_registers_record (regcache))
512 return -1;
513
514 if (record_full_arch_list_add_reg (regcache, I386_EIP_REGNUM))
515 return -1;
516
517 /* Record the change in the stack. */
518 regcache_raw_read_unsigned (regcache, I386_ESP_REGNUM, &esp);
519 /* This is for xstate.
520 sp -= sizeof (struct _fpstate); */
521 esp -= I386_LINUX_xstate;
522 /* This is for frame_size.
523 sp -= sizeof (struct rt_sigframe); */
524 esp -= I386_LINUX_frame_size;
525 if (record_full_arch_list_add_mem (esp,
526 I386_LINUX_xstate + I386_LINUX_frame_size))
527 return -1;
528
529 if (record_full_arch_list_add_end ())
530 return -1;
531
532 return 0;
533 }
534 \f
535
536 /* Core of the implementation for gdbarch get_syscall_number. Get pending
537 syscall number from REGCACHE. If there is no pending syscall -1 will be
538 returned. Pending syscall means ptrace has stepped into the syscall but
539 another ptrace call will step out. PC is right after the int $0x80
540 / syscall / sysenter instruction in both cases, PC does not change during
541 the second ptrace step. */
542
543 static LONGEST
544 i386_linux_get_syscall_number_from_regcache (struct regcache *regcache)
545 {
546 struct gdbarch *gdbarch = get_regcache_arch (regcache);
547 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
548 /* The content of a register. */
549 gdb_byte buf[4];
550 /* The result. */
551 LONGEST ret;
552
553 /* Getting the system call number from the register.
554 When dealing with x86 architecture, this information
555 is stored at %eax register. */
556 regcache_cooked_read (regcache, I386_LINUX_ORIG_EAX_REGNUM, buf);
557
558 ret = extract_signed_integer (buf, 4, byte_order);
559
560 return ret;
561 }
562
563 /* Wrapper for i386_linux_get_syscall_number_from_regcache to make it
564 compatible with gdbarch get_syscall_number method prototype. */
565
566 static LONGEST
567 i386_linux_get_syscall_number (struct gdbarch *gdbarch,
568 ptid_t ptid)
569 {
570 struct regcache *regcache = get_thread_regcache (ptid);
571
572 return i386_linux_get_syscall_number_from_regcache (regcache);
573 }
574
575 /* The register sets used in GNU/Linux ELF core-dumps are identical to
576 the register sets in `struct user' that are used for a.out
577 core-dumps. These are also used by ptrace(2). The corresponding
578 types are `elf_gregset_t' for the general-purpose registers (with
579 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
580 for the floating-point registers.
581
582 Those types used to be available under the names `gregset_t' and
583 `fpregset_t' too, and GDB used those names in the past. But those
584 names are now used for the register sets used in the `mcontext_t'
585 type, which have a different size and layout. */
586
587 /* Mapping between the general-purpose registers in `struct user'
588 format and GDB's register cache layout. */
589
590 /* From <sys/reg.h>. */
591 int i386_linux_gregset_reg_offset[] =
592 {
593 6 * 4, /* %eax */
594 1 * 4, /* %ecx */
595 2 * 4, /* %edx */
596 0 * 4, /* %ebx */
597 15 * 4, /* %esp */
598 5 * 4, /* %ebp */
599 3 * 4, /* %esi */
600 4 * 4, /* %edi */
601 12 * 4, /* %eip */
602 14 * 4, /* %eflags */
603 13 * 4, /* %cs */
604 16 * 4, /* %ss */
605 7 * 4, /* %ds */
606 8 * 4, /* %es */
607 9 * 4, /* %fs */
608 10 * 4, /* %gs */
609 -1, -1, -1, -1, -1, -1, -1, -1,
610 -1, -1, -1, -1, -1, -1, -1, -1,
611 -1, -1, -1, -1, -1, -1, -1, -1,
612 -1,
613 -1, -1, -1, -1, -1, -1, -1, -1,
614 -1, -1, -1, -1, /* MPX registers BND0 ... BND3. */
615 -1, -1, /* MPX registers BNDCFGU, BNDSTATUS. */
616 -1, -1, -1, -1, -1, -1, -1, -1, /* k0 ... k7 (AVX512) */
617 -1, -1, -1, -1, -1, -1, -1, -1, /* zmm0 ... zmm7 (AVX512) */
618 11 * 4, /* "orig_eax" */
619 };
620
621 /* Mapping between the general-purpose registers in `struct
622 sigcontext' format and GDB's register cache layout. */
623
624 /* From <asm/sigcontext.h>. */
625 static int i386_linux_sc_reg_offset[] =
626 {
627 11 * 4, /* %eax */
628 10 * 4, /* %ecx */
629 9 * 4, /* %edx */
630 8 * 4, /* %ebx */
631 7 * 4, /* %esp */
632 6 * 4, /* %ebp */
633 5 * 4, /* %esi */
634 4 * 4, /* %edi */
635 14 * 4, /* %eip */
636 16 * 4, /* %eflags */
637 15 * 4, /* %cs */
638 18 * 4, /* %ss */
639 3 * 4, /* %ds */
640 2 * 4, /* %es */
641 1 * 4, /* %fs */
642 0 * 4 /* %gs */
643 };
644
645 /* Get XSAVE extended state xcr0 from core dump. */
646
647 uint64_t
648 i386_linux_core_read_xcr0 (bfd *abfd)
649 {
650 asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
651 uint64_t xcr0;
652
653 if (xstate)
654 {
655 size_t size = bfd_section_size (abfd, xstate);
656
657 /* Check extended state size. */
658 if (size < X86_XSTATE_AVX_SIZE)
659 xcr0 = X86_XSTATE_SSE_MASK;
660 else
661 {
662 char contents[8];
663
664 if (! bfd_get_section_contents (abfd, xstate, contents,
665 I386_LINUX_XSAVE_XCR0_OFFSET,
666 8))
667 {
668 warning (_("Couldn't read `xcr0' bytes from "
669 "`.reg-xstate' section in core file."));
670 return 0;
671 }
672
673 xcr0 = bfd_get_64 (abfd, contents);
674 }
675 }
676 else
677 xcr0 = 0;
678
679 return xcr0;
680 }
681
682 /* Get Linux/x86 target description from core dump. */
683
684 static const struct target_desc *
685 i386_linux_core_read_description (struct gdbarch *gdbarch,
686 struct target_ops *target,
687 bfd *abfd)
688 {
689 /* Linux/i386. */
690 uint64_t xcr0 = i386_linux_core_read_xcr0 (abfd);
691
692 switch ((xcr0 & X86_XSTATE_ALL_MASK))
693 {
694 case X86_XSTATE_MPX_AVX512_MASK:
695 case X86_XSTATE_AVX512_MASK:
696 return tdesc_i386_avx512_linux;
697 case X86_XSTATE_MPX_MASK:
698 return tdesc_i386_mpx_linux;
699 case X86_XSTATE_AVX_MASK:
700 return tdesc_i386_avx_linux;
701 case X86_XSTATE_SSE_MASK:
702 return tdesc_i386_linux;
703 case X86_XSTATE_X87_MASK:
704 return tdesc_i386_mmx_linux;
705 default:
706 break;
707 }
708
709 if (bfd_get_section_by_name (abfd, ".reg-xfp") != NULL)
710 return tdesc_i386_linux;
711 else
712 return tdesc_i386_mmx_linux;
713 }
714
715 /* Similar to i386_supply_fpregset, but use XSAVE extended state. */
716
717 static void
718 i386_linux_supply_xstateregset (const struct regset *regset,
719 struct regcache *regcache, int regnum,
720 const void *xstateregs, size_t len)
721 {
722 i387_supply_xsave (regcache, regnum, xstateregs);
723 }
724
725 struct type *
726 x86_linux_get_siginfo_type (struct gdbarch *gdbarch)
727 {
728 return linux_get_siginfo_type_with_fields (gdbarch, LINUX_SIGINFO_FIELD_ADDR_BND);
729 }
730
731 /* Similar to i386_collect_fpregset, but use XSAVE extended state. */
732
733 static void
734 i386_linux_collect_xstateregset (const struct regset *regset,
735 const struct regcache *regcache,
736 int regnum, void *xstateregs, size_t len)
737 {
738 i387_collect_xsave (regcache, regnum, xstateregs, 1);
739 }
740
741 /* Register set definitions. */
742
743 static const struct regset i386_linux_xstateregset =
744 {
745 NULL,
746 i386_linux_supply_xstateregset,
747 i386_linux_collect_xstateregset
748 };
749
750 /* Iterate over core file register note sections. */
751
752 static void
753 i386_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
754 iterate_over_regset_sections_cb *cb,
755 void *cb_data,
756 const struct regcache *regcache)
757 {
758 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
759
760 cb (".reg", 68, &i386_gregset, NULL, cb_data);
761
762 if (tdep->xcr0 & X86_XSTATE_AVX)
763 cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0),
764 &i386_linux_xstateregset, "XSAVE extended state", cb_data);
765 else if (tdep->xcr0 & X86_XSTATE_SSE)
766 cb (".reg-xfp", 512, &i386_fpregset, "extended floating-point",
767 cb_data);
768 else
769 cb (".reg2", 108, &i386_fpregset, NULL, cb_data);
770 }
771
772 /* Linux kernel shows PC value after the 'int $0x80' instruction even if
773 inferior is still inside the syscall. On next PTRACE_SINGLESTEP it will
774 finish the syscall but PC will not change.
775
776 Some vDSOs contain 'int $0x80; ret' and during stepping out of the syscall
777 i386_displaced_step_fixup would keep PC at the displaced pad location.
778 As PC is pointing to the 'ret' instruction before the step
779 i386_displaced_step_fixup would expect inferior has just executed that 'ret'
780 and PC should not be adjusted. In reality it finished syscall instead and
781 PC should get relocated back to its vDSO address. Hide the 'ret'
782 instruction by 'nop' so that i386_displaced_step_fixup is not confused.
783
784 It is not fully correct as the bytes in struct displaced_step_closure will
785 not match the inferior code. But we would need some new flag in
786 displaced_step_closure otherwise to keep the state that syscall is finishing
787 for the later i386_displaced_step_fixup execution as the syscall execution
788 is already no longer detectable there. The new flag field would mean
789 i386-linux-tdep.c needs to wrap all the displacement methods of i386-tdep.c
790 which does not seem worth it. The same effect is achieved by patching that
791 'nop' instruction there instead. */
792
793 static struct displaced_step_closure *
794 i386_linux_displaced_step_copy_insn (struct gdbarch *gdbarch,
795 CORE_ADDR from, CORE_ADDR to,
796 struct regcache *regs)
797 {
798 struct displaced_step_closure *closure;
799
800 closure = i386_displaced_step_copy_insn (gdbarch, from, to, regs);
801
802 if (i386_linux_get_syscall_number_from_regcache (regs) != -1)
803 {
804 /* Since we use simple_displaced_step_copy_insn, our closure is a
805 copy of the instruction. */
806 gdb_byte *insn = (gdb_byte *) closure;
807
808 /* Fake nop. */
809 insn[0] = 0x90;
810 }
811
812 return closure;
813 }
814
815 static void
816 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
817 {
818 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
819 const struct target_desc *tdesc = info.target_desc;
820 struct tdesc_arch_data *tdesc_data
821 = (struct tdesc_arch_data *) info.tdep_info;
822 const struct tdesc_feature *feature;
823 int valid_p;
824
825 gdb_assert (tdesc_data);
826
827 linux_init_abi (info, gdbarch);
828
829 /* GNU/Linux uses ELF. */
830 i386_elf_init_abi (info, gdbarch);
831
832 /* Reserve a number for orig_eax. */
833 set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
834
835 if (! tdesc_has_registers (tdesc))
836 tdesc = tdesc_i386_linux;
837 tdep->tdesc = tdesc;
838
839 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.linux");
840 if (feature == NULL)
841 return;
842
843 valid_p = tdesc_numbered_register (feature, tdesc_data,
844 I386_LINUX_ORIG_EAX_REGNUM,
845 "orig_eax");
846 if (!valid_p)
847 return;
848
849 /* Add the %orig_eax register used for syscall restarting. */
850 set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
851
852 tdep->register_reggroup_p = i386_linux_register_reggroup_p;
853
854 tdep->gregset_reg_offset = i386_linux_gregset_reg_offset;
855 tdep->gregset_num_regs = ARRAY_SIZE (i386_linux_gregset_reg_offset);
856 tdep->sizeof_gregset = 17 * 4;
857
858 tdep->jb_pc_offset = 20; /* From <bits/setjmp.h>. */
859
860 tdep->sigtramp_p = i386_linux_sigtramp_p;
861 tdep->sigcontext_addr = i386_linux_sigcontext_addr;
862 tdep->sc_reg_offset = i386_linux_sc_reg_offset;
863 tdep->sc_num_regs = ARRAY_SIZE (i386_linux_sc_reg_offset);
864
865 tdep->xsave_xcr0_offset = I386_LINUX_XSAVE_XCR0_OFFSET;
866
867 set_gdbarch_process_record (gdbarch, i386_process_record);
868 set_gdbarch_process_record_signal (gdbarch, i386_linux_record_signal);
869
870 /* Initialize the i386_linux_record_tdep. */
871 /* These values are the size of the type that will be used in a system
872 call. They are obtained from Linux Kernel source. */
873 i386_linux_record_tdep.size_pointer
874 = gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT;
875 i386_linux_record_tdep.size__old_kernel_stat = 32;
876 i386_linux_record_tdep.size_tms = 16;
877 i386_linux_record_tdep.size_loff_t = 8;
878 i386_linux_record_tdep.size_flock = 16;
879 i386_linux_record_tdep.size_oldold_utsname = 45;
880 i386_linux_record_tdep.size_ustat = 20;
881 i386_linux_record_tdep.size_old_sigaction = 16;
882 i386_linux_record_tdep.size_old_sigset_t = 4;
883 i386_linux_record_tdep.size_rlimit = 8;
884 i386_linux_record_tdep.size_rusage = 72;
885 i386_linux_record_tdep.size_timeval = 8;
886 i386_linux_record_tdep.size_timezone = 8;
887 i386_linux_record_tdep.size_old_gid_t = 2;
888 i386_linux_record_tdep.size_old_uid_t = 2;
889 i386_linux_record_tdep.size_fd_set = 128;
890 i386_linux_record_tdep.size_old_dirent = 268;
891 i386_linux_record_tdep.size_statfs = 64;
892 i386_linux_record_tdep.size_statfs64 = 84;
893 i386_linux_record_tdep.size_sockaddr = 16;
894 i386_linux_record_tdep.size_int
895 = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
896 i386_linux_record_tdep.size_long
897 = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
898 i386_linux_record_tdep.size_ulong
899 = gdbarch_long_bit (gdbarch) / TARGET_CHAR_BIT;
900 i386_linux_record_tdep.size_msghdr = 28;
901 i386_linux_record_tdep.size_itimerval = 16;
902 i386_linux_record_tdep.size_stat = 88;
903 i386_linux_record_tdep.size_old_utsname = 325;
904 i386_linux_record_tdep.size_sysinfo = 64;
905 i386_linux_record_tdep.size_msqid_ds = 88;
906 i386_linux_record_tdep.size_shmid_ds = 84;
907 i386_linux_record_tdep.size_new_utsname = 390;
908 i386_linux_record_tdep.size_timex = 128;
909 i386_linux_record_tdep.size_mem_dqinfo = 24;
910 i386_linux_record_tdep.size_if_dqblk = 68;
911 i386_linux_record_tdep.size_fs_quota_stat = 68;
912 i386_linux_record_tdep.size_timespec = 8;
913 i386_linux_record_tdep.size_pollfd = 8;
914 i386_linux_record_tdep.size_NFS_FHSIZE = 32;
915 i386_linux_record_tdep.size_knfsd_fh = 132;
916 i386_linux_record_tdep.size_TASK_COMM_LEN = 16;
917 i386_linux_record_tdep.size_sigaction = 20;
918 i386_linux_record_tdep.size_sigset_t = 8;
919 i386_linux_record_tdep.size_siginfo_t = 128;
920 i386_linux_record_tdep.size_cap_user_data_t = 12;
921 i386_linux_record_tdep.size_stack_t = 12;
922 i386_linux_record_tdep.size_off_t = i386_linux_record_tdep.size_long;
923 i386_linux_record_tdep.size_stat64 = 96;
924 i386_linux_record_tdep.size_gid_t = 4;
925 i386_linux_record_tdep.size_uid_t = 4;
926 i386_linux_record_tdep.size_PAGE_SIZE = 4096;
927 i386_linux_record_tdep.size_flock64 = 24;
928 i386_linux_record_tdep.size_user_desc = 16;
929 i386_linux_record_tdep.size_io_event = 32;
930 i386_linux_record_tdep.size_iocb = 64;
931 i386_linux_record_tdep.size_epoll_event = 12;
932 i386_linux_record_tdep.size_itimerspec
933 = i386_linux_record_tdep.size_timespec * 2;
934 i386_linux_record_tdep.size_mq_attr = 32;
935 i386_linux_record_tdep.size_termios = 36;
936 i386_linux_record_tdep.size_termios2 = 44;
937 i386_linux_record_tdep.size_pid_t = 4;
938 i386_linux_record_tdep.size_winsize = 8;
939 i386_linux_record_tdep.size_serial_struct = 60;
940 i386_linux_record_tdep.size_serial_icounter_struct = 80;
941 i386_linux_record_tdep.size_hayes_esp_config = 12;
942 i386_linux_record_tdep.size_size_t = 4;
943 i386_linux_record_tdep.size_iovec = 8;
944 i386_linux_record_tdep.size_time_t = 4;
945
946 /* These values are the second argument of system call "sys_ioctl".
947 They are obtained from Linux Kernel source. */
948 i386_linux_record_tdep.ioctl_TCGETS = 0x5401;
949 i386_linux_record_tdep.ioctl_TCSETS = 0x5402;
950 i386_linux_record_tdep.ioctl_TCSETSW = 0x5403;
951 i386_linux_record_tdep.ioctl_TCSETSF = 0x5404;
952 i386_linux_record_tdep.ioctl_TCGETA = 0x5405;
953 i386_linux_record_tdep.ioctl_TCSETA = 0x5406;
954 i386_linux_record_tdep.ioctl_TCSETAW = 0x5407;
955 i386_linux_record_tdep.ioctl_TCSETAF = 0x5408;
956 i386_linux_record_tdep.ioctl_TCSBRK = 0x5409;
957 i386_linux_record_tdep.ioctl_TCXONC = 0x540A;
958 i386_linux_record_tdep.ioctl_TCFLSH = 0x540B;
959 i386_linux_record_tdep.ioctl_TIOCEXCL = 0x540C;
960 i386_linux_record_tdep.ioctl_TIOCNXCL = 0x540D;
961 i386_linux_record_tdep.ioctl_TIOCSCTTY = 0x540E;
962 i386_linux_record_tdep.ioctl_TIOCGPGRP = 0x540F;
963 i386_linux_record_tdep.ioctl_TIOCSPGRP = 0x5410;
964 i386_linux_record_tdep.ioctl_TIOCOUTQ = 0x5411;
965 i386_linux_record_tdep.ioctl_TIOCSTI = 0x5412;
966 i386_linux_record_tdep.ioctl_TIOCGWINSZ = 0x5413;
967 i386_linux_record_tdep.ioctl_TIOCSWINSZ = 0x5414;
968 i386_linux_record_tdep.ioctl_TIOCMGET = 0x5415;
969 i386_linux_record_tdep.ioctl_TIOCMBIS = 0x5416;
970 i386_linux_record_tdep.ioctl_TIOCMBIC = 0x5417;
971 i386_linux_record_tdep.ioctl_TIOCMSET = 0x5418;
972 i386_linux_record_tdep.ioctl_TIOCGSOFTCAR = 0x5419;
973 i386_linux_record_tdep.ioctl_TIOCSSOFTCAR = 0x541A;
974 i386_linux_record_tdep.ioctl_FIONREAD = 0x541B;
975 i386_linux_record_tdep.ioctl_TIOCINQ = i386_linux_record_tdep.ioctl_FIONREAD;
976 i386_linux_record_tdep.ioctl_TIOCLINUX = 0x541C;
977 i386_linux_record_tdep.ioctl_TIOCCONS = 0x541D;
978 i386_linux_record_tdep.ioctl_TIOCGSERIAL = 0x541E;
979 i386_linux_record_tdep.ioctl_TIOCSSERIAL = 0x541F;
980 i386_linux_record_tdep.ioctl_TIOCPKT = 0x5420;
981 i386_linux_record_tdep.ioctl_FIONBIO = 0x5421;
982 i386_linux_record_tdep.ioctl_TIOCNOTTY = 0x5422;
983 i386_linux_record_tdep.ioctl_TIOCSETD = 0x5423;
984 i386_linux_record_tdep.ioctl_TIOCGETD = 0x5424;
985 i386_linux_record_tdep.ioctl_TCSBRKP = 0x5425;
986 i386_linux_record_tdep.ioctl_TIOCTTYGSTRUCT = 0x5426;
987 i386_linux_record_tdep.ioctl_TIOCSBRK = 0x5427;
988 i386_linux_record_tdep.ioctl_TIOCCBRK = 0x5428;
989 i386_linux_record_tdep.ioctl_TIOCGSID = 0x5429;
990 i386_linux_record_tdep.ioctl_TCGETS2 = 0x802c542a;
991 i386_linux_record_tdep.ioctl_TCSETS2 = 0x402c542b;
992 i386_linux_record_tdep.ioctl_TCSETSW2 = 0x402c542c;
993 i386_linux_record_tdep.ioctl_TCSETSF2 = 0x402c542d;
994 i386_linux_record_tdep.ioctl_TIOCGPTN = 0x80045430;
995 i386_linux_record_tdep.ioctl_TIOCSPTLCK = 0x40045431;
996 i386_linux_record_tdep.ioctl_FIONCLEX = 0x5450;
997 i386_linux_record_tdep.ioctl_FIOCLEX = 0x5451;
998 i386_linux_record_tdep.ioctl_FIOASYNC = 0x5452;
999 i386_linux_record_tdep.ioctl_TIOCSERCONFIG = 0x5453;
1000 i386_linux_record_tdep.ioctl_TIOCSERGWILD = 0x5454;
1001 i386_linux_record_tdep.ioctl_TIOCSERSWILD = 0x5455;
1002 i386_linux_record_tdep.ioctl_TIOCGLCKTRMIOS = 0x5456;
1003 i386_linux_record_tdep.ioctl_TIOCSLCKTRMIOS = 0x5457;
1004 i386_linux_record_tdep.ioctl_TIOCSERGSTRUCT = 0x5458;
1005 i386_linux_record_tdep.ioctl_TIOCSERGETLSR = 0x5459;
1006 i386_linux_record_tdep.ioctl_TIOCSERGETMULTI = 0x545A;
1007 i386_linux_record_tdep.ioctl_TIOCSERSETMULTI = 0x545B;
1008 i386_linux_record_tdep.ioctl_TIOCMIWAIT = 0x545C;
1009 i386_linux_record_tdep.ioctl_TIOCGICOUNT = 0x545D;
1010 i386_linux_record_tdep.ioctl_TIOCGHAYESESP = 0x545E;
1011 i386_linux_record_tdep.ioctl_TIOCSHAYESESP = 0x545F;
1012 i386_linux_record_tdep.ioctl_FIOQSIZE = 0x5460;
1013
1014 /* These values are the second argument of system call "sys_fcntl"
1015 and "sys_fcntl64". They are obtained from Linux Kernel source. */
1016 i386_linux_record_tdep.fcntl_F_GETLK = 5;
1017 i386_linux_record_tdep.fcntl_F_GETLK64 = 12;
1018 i386_linux_record_tdep.fcntl_F_SETLK64 = 13;
1019 i386_linux_record_tdep.fcntl_F_SETLKW64 = 14;
1020
1021 i386_linux_record_tdep.arg1 = I386_EBX_REGNUM;
1022 i386_linux_record_tdep.arg2 = I386_ECX_REGNUM;
1023 i386_linux_record_tdep.arg3 = I386_EDX_REGNUM;
1024 i386_linux_record_tdep.arg4 = I386_ESI_REGNUM;
1025 i386_linux_record_tdep.arg5 = I386_EDI_REGNUM;
1026 i386_linux_record_tdep.arg6 = I386_EBP_REGNUM;
1027
1028 tdep->i386_intx80_record = i386_linux_intx80_sysenter_syscall_record;
1029 tdep->i386_sysenter_record = i386_linux_intx80_sysenter_syscall_record;
1030 tdep->i386_syscall_record = i386_linux_intx80_sysenter_syscall_record;
1031
1032 /* N_FUN symbols in shared libaries have 0 for their values and need
1033 to be relocated. */
1034 set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
1035
1036 /* GNU/Linux uses SVR4-style shared libraries. */
1037 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
1038 set_solib_svr4_fetch_link_map_offsets
1039 (gdbarch, svr4_ilp32_fetch_link_map_offsets);
1040
1041 /* GNU/Linux uses the dynamic linker included in the GNU C Library. */
1042 set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
1043
1044 dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
1045
1046 /* Enable TLS support. */
1047 set_gdbarch_fetch_tls_load_module_address (gdbarch,
1048 svr4_fetch_objfile_link_map);
1049
1050 /* Core file support. */
1051 set_gdbarch_iterate_over_regset_sections
1052 (gdbarch, i386_linux_iterate_over_regset_sections);
1053 set_gdbarch_core_read_description (gdbarch,
1054 i386_linux_core_read_description);
1055
1056 /* Displaced stepping. */
1057 set_gdbarch_displaced_step_copy_insn (gdbarch,
1058 i386_linux_displaced_step_copy_insn);
1059 set_gdbarch_displaced_step_fixup (gdbarch, i386_displaced_step_fixup);
1060 set_gdbarch_displaced_step_free_closure (gdbarch,
1061 simple_displaced_step_free_closure);
1062 set_gdbarch_displaced_step_location (gdbarch,
1063 linux_displaced_step_location);
1064
1065 /* Functions for 'catch syscall'. */
1066 set_xml_syscall_file_name (gdbarch, XML_SYSCALL_FILENAME_I386);
1067 set_gdbarch_get_syscall_number (gdbarch,
1068 i386_linux_get_syscall_number);
1069
1070 set_gdbarch_get_siginfo_type (gdbarch, x86_linux_get_siginfo_type);
1071 set_gdbarch_handle_segmentation_fault (gdbarch,
1072 i386_linux_handle_segmentation_fault);
1073 }
1074
1075 /* Provide a prototype to silence -Wmissing-prototypes. */
1076 extern void _initialize_i386_linux_tdep (void);
1077
1078 void
1079 _initialize_i386_linux_tdep (void)
1080 {
1081 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
1082 i386_linux_init_abi);
1083
1084 /* Initialize the Linux target description. */
1085 initialize_tdesc_i386_linux ();
1086 initialize_tdesc_i386_mmx_linux ();
1087 initialize_tdesc_i386_avx_linux ();
1088 initialize_tdesc_i386_mpx_linux ();
1089 initialize_tdesc_i386_avx512_linux ();
1090 }
This page took 0.064813 seconds and 5 git commands to generate.