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