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