gdbarch: Use an anonymous union for target data in `gdbarch_info'
[external/binutils.git] / gdb / i386-linux-tdep.c
1 /* Target-dependent code for GNU/Linux i386.
2
3    Copyright (C) 2000-2017 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
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"
56
57 /* Return non-zero, when the register is in the corresponding register
58    group.  Put the LINUX_ORIG_EAX register in the system group.  */
59 static int
60 i386_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
61                                 struct reggroup *group)
62 {
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);
68 }
69
70 \f
71 /* Recognizing signal handler frames.  */
72
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.  */
79
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.
84
85    The instruction sequence for normal signals is
86        pop    %eax
87        mov    $0x77, %eax
88        int    $0x80
89    or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
90
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.
94
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.
100
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
107    supported too.  */
108
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
115
116 static const gdb_byte linux_sigtramp_code[] =
117 {
118   LINUX_SIGTRAMP_INSN0,                                 /* pop %eax */
119   LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00,         /* mov $0x77, %eax */
120   LINUX_SIGTRAMP_INSN2, 0x80                            /* int $0x80 */
121 };
122
123 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
124
125 /* If THIS_FRAME is a sigtramp routine, return the address of the
126    start of the routine.  Otherwise, return 0.  */
127
128 static CORE_ADDR
129 i386_linux_sigtramp_start (struct frame_info *this_frame)
130 {
131   CORE_ADDR pc = get_frame_pc (this_frame);
132   gdb_byte buf[LINUX_SIGTRAMP_LEN];
133
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.  */
140
141   if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
142     return 0;
143
144   if (buf[0] != LINUX_SIGTRAMP_INSN0)
145     {
146       int adjust;
147
148       switch (buf[0])
149         {
150         case LINUX_SIGTRAMP_INSN1:
151           adjust = LINUX_SIGTRAMP_OFFSET1;
152           break;
153         case LINUX_SIGTRAMP_INSN2:
154           adjust = LINUX_SIGTRAMP_OFFSET2;
155           break;
156         default:
157           return 0;
158         }
159
160       pc -= adjust;
161
162       if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_SIGTRAMP_LEN))
163         return 0;
164     }
165
166   if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
167     return 0;
168
169   return pc;
170 }
171
172 /* This function does the same for RT signals.  Here the instruction
173    sequence is
174        mov    $0xad, %eax
175        int    $0x80
176    or 0xb8 0xad 0x00 0x00 0x00 0xcd 0x80.
177
178    The effect is to call the system call rt_sigreturn.  */
179
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
184
185 static const gdb_byte linux_rt_sigtramp_code[] =
186 {
187   LINUX_RT_SIGTRAMP_INSN0, 0xad, 0x00, 0x00, 0x00,      /* mov $0xad, %eax */
188   LINUX_RT_SIGTRAMP_INSN1, 0x80                         /* int $0x80 */
189 };
190
191 #define LINUX_RT_SIGTRAMP_LEN (sizeof linux_rt_sigtramp_code)
192
193 /* If THIS_FRAME is an RT sigtramp routine, return the address of the
194    start of the routine.  Otherwise, return 0.  */
195
196 static CORE_ADDR
197 i386_linux_rt_sigtramp_start (struct frame_info *this_frame)
198 {
199   CORE_ADDR pc = get_frame_pc (this_frame);
200   gdb_byte buf[LINUX_RT_SIGTRAMP_LEN];
201
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.  */
208
209   if (!safe_frame_unwind_memory (this_frame, pc, buf, LINUX_RT_SIGTRAMP_LEN))
210     return 0;
211
212   if (buf[0] != LINUX_RT_SIGTRAMP_INSN0)
213     {
214       if (buf[0] != LINUX_RT_SIGTRAMP_INSN1)
215         return 0;
216
217       pc -= LINUX_RT_SIGTRAMP_OFFSET1;
218
219       if (!safe_frame_unwind_memory (this_frame, pc, buf,
220                                      LINUX_RT_SIGTRAMP_LEN))
221         return 0;
222     }
223
224   if (memcmp (buf, linux_rt_sigtramp_code, LINUX_RT_SIGTRAMP_LEN) != 0)
225     return 0;
226
227   return pc;
228 }
229
230 /* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
231    routine.  */
232
233 static int
234 i386_linux_sigtramp_p (struct frame_info *this_frame)
235 {
236   CORE_ADDR pc = get_frame_pc (this_frame);
237   const char *name;
238
239   find_pc_partial_function (pc, &name, NULL, NULL);
240
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);
249
250   return (strcmp ("__restore", name) == 0
251           || strcmp ("__restore_rt", name) == 0);
252 }
253
254 /* Return one if the PC of THIS_FRAME is in a signal trampoline which
255    may have DWARF-2 CFI.  */
256
257 static int
258 i386_linux_dwarf_signal_frame_p (struct gdbarch *gdbarch,
259                                  struct frame_info *this_frame)
260 {
261   CORE_ADDR pc = get_frame_pc (this_frame);
262   const char *name;
263
264   find_pc_partial_function (pc, &name, NULL, NULL);
265
266   /* If a vsyscall DSO is in use, the signal trampolines may have these
267      names.  */
268   if (name && (strcmp (name, "__kernel_sigreturn") == 0
269                || strcmp (name, "__kernel_rt_sigreturn") == 0))
270     return 1;
271
272   return 0;
273 }
274
275 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>.  */
276 #define I386_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 20
277
278 /* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
279    address of the associated sigcontext structure.  */
280
281 static CORE_ADDR
282 i386_linux_sigcontext_addr (struct frame_info *this_frame)
283 {
284   struct gdbarch *gdbarch = get_frame_arch (this_frame);
285   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
286   CORE_ADDR pc;
287   CORE_ADDR sp;
288   gdb_byte buf[4];
289
290   get_frame_register (this_frame, I386_ESP_REGNUM, buf);
291   sp = extract_unsigned_integer (buf, 4, byte_order);
292
293   pc = i386_linux_sigtramp_start (this_frame);
294   if (pc)
295     {
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))
303         return sp + 4;
304       return sp;
305     }
306
307   pc = i386_linux_rt_sigtramp_start (this_frame);
308   if (pc)
309     {
310       CORE_ADDR ucontext_addr;
311
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;
318     }
319
320   error (_("Couldn't recognize signal trampoline."));
321   return 0;
322 }
323
324 /* Set the program counter for process PTID to PC.  */
325
326 static void
327 i386_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
328 {
329   regcache_cooked_write_unsigned (regcache, I386_EIP_REGNUM, pc);
330
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.
338
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
344      restarted.  */
345   regcache_cooked_write_unsigned (regcache, I386_LINUX_ORIG_EAX_REGNUM, -1);
346 }
347
348 /* Record all registers but IP register for process-record.  */
349
350 static int
351 i386_all_but_ip_registers_record (struct regcache *regcache)
352 {
353   if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
354     return -1;
355   if (record_full_arch_list_add_reg (regcache, I386_ECX_REGNUM))
356     return -1;
357   if (record_full_arch_list_add_reg (regcache, I386_EDX_REGNUM))
358     return -1;
359   if (record_full_arch_list_add_reg (regcache, I386_EBX_REGNUM))
360     return -1;
361   if (record_full_arch_list_add_reg (regcache, I386_ESP_REGNUM))
362     return -1;
363   if (record_full_arch_list_add_reg (regcache, I386_EBP_REGNUM))
364     return -1;
365   if (record_full_arch_list_add_reg (regcache, I386_ESI_REGNUM))
366     return -1;
367   if (record_full_arch_list_add_reg (regcache, I386_EDI_REGNUM))
368     return -1;
369   if (record_full_arch_list_add_reg (regcache, I386_EFLAGS_REGNUM))
370     return -1;
371
372   return 0;
373 }
374
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).  */
379
380 static enum gdb_syscall
381 i386_canonicalize_syscall (int syscall)
382 {
383   enum { i386_syscall_max = 499 };
384
385   if (syscall <= i386_syscall_max)
386     return (enum gdb_syscall) syscall;
387   else
388     return gdb_sys_no_syscall;
389 }
390
391 /* Value of the sigcode in case of a boundary fault.  */
392
393 #define SIG_CODE_BONDARY_FAULT 3
394
395 /* i386 GNU/Linux implementation of the handle_segmentation_fault
396    gdbarch hook.  Displays information related to MPX bound
397    violations.  */
398 void
399 i386_linux_handle_segmentation_fault (struct gdbarch *gdbarch,
400                                       struct ui_out *uiout)
401 {
402   /* -Wmaybe-uninitialized  */
403   CORE_ADDR lower_bound = 0, upper_bound = 0, access = 0;
404   int is_upper;
405   long sig_code = 0;
406
407   if (!i386_mpx_enabled ())
408     return;
409
410   TRY
411     {
412       /* Sigcode evaluates if the actual segfault is a boundary violation.  */
413       sig_code = parse_and_eval_long ("$_siginfo.si_code\n");
414
415       lower_bound
416         = parse_and_eval_long ("$_siginfo._sifields._sigfault._addr_bnd._lower");
417       upper_bound
418         = parse_and_eval_long ("$_siginfo._sifields._sigfault._addr_bnd._upper");
419       access
420         = parse_and_eval_long ("$_siginfo._sifields._sigfault.si_addr");
421     }
422   CATCH (exception, RETURN_MASK_ALL)
423     {
424       return;
425     }
426   END_CATCH
427
428   /* If this is not a boundary violation just return.  */
429   if (sig_code != SIG_CODE_BONDARY_FAULT)
430     return;
431
432   is_upper = (access > upper_bound ? 1 : 0);
433
434   uiout->text ("\n");
435   if (is_upper)
436     uiout->field_string ("sigcode-meaning", _("Upper bound violation"));
437   else
438     uiout->field_string ("sigcode-meaning", _("Lower bound violation"));
439
440   uiout->text (_(" while accessing address "));
441   uiout->field_fmt ("bound-access", "%s", paddress (gdbarch, access));
442
443   uiout->text (_("\nBounds: [lower = "));
444   uiout->field_fmt ("lower-bound", "%s", paddress (gdbarch, lower_bound));
445
446   uiout->text (_(", upper = "));
447   uiout->field_fmt ("upper-bound", "%s", paddress (gdbarch, upper_bound));
448
449   uiout->text (_("]"));
450 }
451
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).
456
457    Return -1 if something wrong.  */
458
459 static struct linux_record_tdep i386_linux_record_tdep;
460
461 static int
462 i386_linux_intx80_sysenter_syscall_record (struct regcache *regcache)
463 {
464   int ret;
465   LONGEST syscall_native;
466   enum gdb_syscall syscall_gdb;
467
468   regcache_raw_read_signed (regcache, I386_EAX_REGNUM, &syscall_native);
469
470   syscall_gdb = i386_canonicalize_syscall (syscall_native);
471
472   if (syscall_gdb < 0)
473     {
474       printf_unfiltered (_("Process record and replay target doesn't "
475                            "support syscall number %s\n"), 
476                          plongest (syscall_native));
477       return -1;
478     }
479
480   if (syscall_gdb == gdb_sys_sigreturn
481       || syscall_gdb == gdb_sys_rt_sigreturn)
482    {
483      if (i386_all_but_ip_registers_record (regcache))
484        return -1;
485      return 0;
486    }
487
488   ret = record_linux_system_call (syscall_gdb, regcache,
489                                   &i386_linux_record_tdep);
490   if (ret)
491     return ret;
492
493   /* Record the return value of the system call.  */
494   if (record_full_arch_list_add_reg (regcache, I386_EAX_REGNUM))
495     return -1;
496
497   return 0;
498 }
499
500 #define I386_LINUX_xstate       270
501 #define I386_LINUX_frame_size   732
502
503 static int
504 i386_linux_record_signal (struct gdbarch *gdbarch,
505                           struct regcache *regcache,
506                           enum gdb_signal signal)
507 {
508   ULONGEST esp;
509
510   if (i386_all_but_ip_registers_record (regcache))
511     return -1;
512
513   if (record_full_arch_list_add_reg (regcache, I386_EIP_REGNUM))
514     return -1;
515
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))
526     return -1;
527
528   if (record_full_arch_list_add_end ())
529     return -1;
530
531   return 0;
532 }
533 \f
534
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.  */
541
542 static LONGEST
543 i386_linux_get_syscall_number_from_regcache (struct regcache *regcache)
544 {
545   struct gdbarch *gdbarch = get_regcache_arch (regcache);
546   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
547   /* The content of a register.  */
548   gdb_byte buf[4];
549   /* The result.  */
550   LONGEST ret;
551
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);
556
557   ret = extract_signed_integer (buf, 4, byte_order);
558
559   return ret;
560 }
561
562 /* Wrapper for i386_linux_get_syscall_number_from_regcache to make it
563    compatible with gdbarch get_syscall_number method prototype.  */
564
565 static LONGEST
566 i386_linux_get_syscall_number (struct gdbarch *gdbarch,
567                                ptid_t ptid)
568 {
569   struct regcache *regcache = get_thread_regcache (ptid);
570
571   return i386_linux_get_syscall_number_from_regcache (regcache);
572 }
573
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.
580
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.  */
585
586 /* Mapping between the general-purpose registers in `struct user'
587    format and GDB's register cache layout.  */
588
589 /* From <sys/reg.h>.  */
590 int i386_linux_gregset_reg_offset[] =
591 {
592   6 * 4,                        /* %eax */
593   1 * 4,                        /* %ecx */
594   2 * 4,                        /* %edx */
595   0 * 4,                        /* %ebx */
596   15 * 4,                       /* %esp */
597   5 * 4,                        /* %ebp */
598   3 * 4,                        /* %esi */
599   4 * 4,                        /* %edi */
600   12 * 4,                       /* %eip */
601   14 * 4,                       /* %eflags */
602   13 * 4,                       /* %cs */
603   16 * 4,                       /* %ss */
604   7 * 4,                        /* %ds */
605   8 * 4,                        /* %es */
606   9 * 4,                        /* %fs */
607   10 * 4,                       /* %gs */
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,
611   -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"  */
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 /* See i386-linux-tdep.h.  */
683
684 const struct target_desc *
685 i386_linux_read_description (uint64_t xcr0)
686 {
687   if (xcr0 == 0)
688     return NULL;
689
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;
693
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];
700
701   if (*tdesc == NULL)
702     {
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"));
706
707       long regnum = 0;
708
709       if (xcr0 & X86_XSTATE_X87)
710         regnum = create_feature_i386_32bit_core (*tdesc, regnum);
711
712       if (xcr0 & X86_XSTATE_SSE)
713         regnum = create_feature_i386_32bit_sse (*tdesc, regnum);
714
715       regnum = create_feature_i386_32bit_linux (*tdesc, regnum);
716
717       if (xcr0 & X86_XSTATE_AVX)
718         regnum = create_feature_i386_32bit_avx (*tdesc, regnum);
719
720       if (xcr0 & X86_XSTATE_MPX)
721         regnum = create_feature_i386_32bit_mpx (*tdesc, regnum);
722
723       if (xcr0 & X86_XSTATE_AVX512)
724         regnum = create_feature_i386_32bit_avx512 (*tdesc, regnum);
725
726       if (xcr0 & X86_XSTATE_PKRU)
727         regnum = create_feature_i386_32bit_pkeys (*tdesc, regnum);
728     }
729
730   return *tdesc;
731 }
732
733 /* Get Linux/x86 target description from core dump.  */
734
735 static const struct target_desc *
736 i386_linux_core_read_description (struct gdbarch *gdbarch,
737                                   struct target_ops *target,
738                                   bfd *abfd)
739 {
740   /* Linux/i386.  */
741   uint64_t xcr0 = i386_linux_core_read_xcr0 (abfd);
742   const struct target_desc *tdesc = i386_linux_read_description (xcr0);
743
744   if (tdesc != NULL)
745     return tdesc;
746
747   if (bfd_get_section_by_name (abfd, ".reg-xfp") != NULL)
748     return i386_linux_read_description (X86_XSTATE_SSE_MASK);
749   else
750     return i386_linux_read_description (X86_XSTATE_X87_MASK);
751 }
752
753 /* Similar to i386_supply_fpregset, but use XSAVE extended state.  */
754
755 static void
756 i386_linux_supply_xstateregset (const struct regset *regset,
757                                 struct regcache *regcache, int regnum,
758                                 const void *xstateregs, size_t len)
759 {
760   i387_supply_xsave (regcache, regnum, xstateregs);
761 }
762
763 struct type *
764 x86_linux_get_siginfo_type (struct gdbarch *gdbarch)
765 {
766   return linux_get_siginfo_type_with_fields (gdbarch, LINUX_SIGINFO_FIELD_ADDR_BND);
767 }
768
769 /* Similar to i386_collect_fpregset, but use XSAVE extended state.  */
770
771 static void
772 i386_linux_collect_xstateregset (const struct regset *regset,
773                                  const struct regcache *regcache,
774                                  int regnum, void *xstateregs, size_t len)
775 {
776   i387_collect_xsave (regcache, regnum, xstateregs, 1);
777 }
778
779 /* Register set definitions.  */
780
781 static const struct regset i386_linux_xstateregset =
782   {
783     NULL,
784     i386_linux_supply_xstateregset,
785     i386_linux_collect_xstateregset
786   };
787
788 /* Iterate over core file register note sections.  */
789
790 static void
791 i386_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
792                                          iterate_over_regset_sections_cb *cb,
793                                          void *cb_data,
794                                          const struct regcache *regcache)
795 {
796   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
797
798   cb (".reg", 68, &i386_gregset, NULL, cb_data);
799
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",
805         cb_data);
806   else
807     cb (".reg2", 108, &i386_fpregset, NULL, cb_data);
808 }
809
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.
813    
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.
821    
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.  */
830
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)
835 {
836   struct displaced_step_closure *closure;
837   
838   closure = i386_displaced_step_copy_insn (gdbarch, from, to, regs);
839
840   if (i386_linux_get_syscall_number_from_regcache (regs) != -1)
841     {
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;
845
846       /* Fake nop.  */
847       insn[0] = 0x90;
848     }
849
850   return closure;
851 }
852
853 static void
854 i386_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
855 {
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;
860   int valid_p;
861
862   gdb_assert (tdesc_data);
863
864   linux_init_abi (info, gdbarch);
865
866   /* GNU/Linux uses ELF.  */
867   i386_elf_init_abi (info, gdbarch);
868
869   /* Reserve a number for orig_eax.  */
870   set_gdbarch_num_regs (gdbarch, I386_LINUX_NUM_REGS);
871
872   if (! tdesc_has_registers (tdesc))
873     tdesc = i386_linux_read_description (X86_XSTATE_SSE_MASK);
874   tdep->tdesc = tdesc;
875
876   feature = tdesc_find_feature (tdesc, "org.gnu.gdb.i386.linux");
877   if (feature == NULL)
878     return;
879
880   valid_p = tdesc_numbered_register (feature, tdesc_data,
881                                      I386_LINUX_ORIG_EAX_REGNUM,
882                                      "orig_eax");
883   if (!valid_p)
884     return;
885
886   /* Add the %orig_eax register used for syscall restarting.  */
887   set_gdbarch_write_pc (gdbarch, i386_linux_write_pc);
888
889   tdep->register_reggroup_p = i386_linux_register_reggroup_p;
890
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;
894
895   tdep->jb_pc_offset = 20;      /* From <bits/setjmp.h>.  */
896
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);
901
902   tdep->xsave_xcr0_offset = I386_LINUX_XSAVE_XCR0_OFFSET;
903
904   set_gdbarch_process_record (gdbarch, i386_process_record);
905   set_gdbarch_process_record_signal (gdbarch, i386_linux_record_signal);
906
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;
982
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;
1050
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;
1057
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;
1064
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;
1068
1069   /* N_FUN symbols in shared libaries have 0 for their values and need
1070      to be relocated.  */
1071   set_gdbarch_sofun_address_maybe_missing (gdbarch, 1);
1072
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);
1077
1078   /* GNU/Linux uses the dynamic linker included in the GNU C Library.  */
1079   set_gdbarch_skip_solib_resolver (gdbarch, glibc_skip_solib_resolver);
1080
1081   dwarf2_frame_set_signal_frame_p (gdbarch, i386_linux_dwarf_signal_frame_p);
1082
1083   /* Enable TLS support.  */
1084   set_gdbarch_fetch_tls_load_module_address (gdbarch,
1085                                              svr4_fetch_objfile_link_map);
1086
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);
1092
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);
1099
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);
1104
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);
1108 }
1109
1110 /* Provide a prototype to silence -Wmissing-prototypes.  */
1111 extern void _initialize_i386_linux_tdep (void);
1112
1113 void
1114 _initialize_i386_linux_tdep (void)
1115 {
1116   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_LINUX,
1117                           i386_linux_init_abi);
1118
1119 #if GDB_SELF_TEST
1120   struct
1121   {
1122     const char *xml;
1123     uint64_t mask;
1124   } xml_masks[] = {
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 },
1133   };
1134
1135   for (auto &a : xml_masks)
1136     {
1137       auto tdesc = i386_linux_read_description (a.mask);
1138
1139       selftests::record_xml_tdesc (a.xml, tdesc);
1140     }
1141 #endif /* GDB_SELF_TEST */
1142 }