119370Spst/* Intel 386 target-dependent stuff.
219370Spst
3130809Smarcel   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4130809Smarcel   1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free Software
5130809Smarcel   Foundation, Inc.
6130809Smarcel
798948Sobrien   This file is part of GDB.
819370Spst
998948Sobrien   This program is free software; you can redistribute it and/or modify
1098948Sobrien   it under the terms of the GNU General Public License as published by
1198948Sobrien   the Free Software Foundation; either version 2 of the License, or
1298948Sobrien   (at your option) any later version.
1319370Spst
1498948Sobrien   This program is distributed in the hope that it will be useful,
1598948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798948Sobrien   GNU General Public License for more details.
1819370Spst
1998948Sobrien   You should have received a copy of the GNU General Public License
2098948Sobrien   along with this program; if not, write to the Free Software
2198948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298948Sobrien   Boston, MA 02111-1307, USA.  */
2319370Spst
2419370Spst#include "defs.h"
25130809Smarcel#include "arch-utils.h"
26130809Smarcel#include "command.h"
27130809Smarcel#include "dummy-frame.h"
28130809Smarcel#include "dwarf2-frame.h"
29130809Smarcel#include "doublest.h"
30130809Smarcel#include "floatformat.h"
3119370Spst#include "frame.h"
32130809Smarcel#include "frame-base.h"
33130809Smarcel#include "frame-unwind.h"
3419370Spst#include "inferior.h"
35130809Smarcel#include "gdbcmd.h"
3619370Spst#include "gdbcore.h"
37130809Smarcel#include "objfiles.h"
38130809Smarcel#include "osabi.h"
39130809Smarcel#include "regcache.h"
40130809Smarcel#include "reggroups.h"
41130809Smarcel#include "regset.h"
42130809Smarcel#include "symfile.h"
43130809Smarcel#include "symtab.h"
4419370Spst#include "target.h"
4598948Sobrien#include "value.h"
46130809Smarcel#include "dis-asm.h"
47130809Smarcel
4898948Sobrien#include "gdb_assert.h"
49130809Smarcel#include "gdb_string.h"
5019370Spst
5198948Sobrien#include "i386-tdep.h"
52130809Smarcel#include "i387-tdep.h"
5319370Spst
5498948Sobrien/* Names of the registers.  The first 10 registers match the register
5598948Sobrien   numbering scheme used by GCC for stabs and DWARF.  */
56130809Smarcel
5798948Sobrienstatic char *i386_register_names[] =
5898948Sobrien{
5998948Sobrien  "eax",   "ecx",    "edx",   "ebx",
6098948Sobrien  "esp",   "ebp",    "esi",   "edi",
6198948Sobrien  "eip",   "eflags", "cs",    "ss",
6298948Sobrien  "ds",    "es",     "fs",    "gs",
6398948Sobrien  "st0",   "st1",    "st2",   "st3",
6498948Sobrien  "st4",   "st5",    "st6",   "st7",
6598948Sobrien  "fctrl", "fstat",  "ftag",  "fiseg",
6698948Sobrien  "fioff", "foseg",  "fooff", "fop",
6798948Sobrien  "xmm0",  "xmm1",   "xmm2",  "xmm3",
6898948Sobrien  "xmm4",  "xmm5",   "xmm6",  "xmm7",
6998948Sobrien  "mxcsr"
7098948Sobrien};
7119370Spst
72130809Smarcelstatic const int i386_num_register_names = ARRAY_SIZE (i386_register_names);
7319370Spst
74130809Smarcel/* MMX registers.  */
75130809Smarcel
76130809Smarcelstatic char *i386_mmx_names[] =
77130809Smarcel{
78130809Smarcel  "mm0", "mm1", "mm2", "mm3",
79130809Smarcel  "mm4", "mm5", "mm6", "mm7"
8098948Sobrien};
8146283Sdfr
82130809Smarcelstatic const int i386_num_mmx_regs = ARRAY_SIZE (i386_mmx_names);
8346283Sdfr
84130809Smarcelstatic int
85130809Smarceli386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
8698948Sobrien{
87130809Smarcel  int mm0_regnum = gdbarch_tdep (gdbarch)->mm0_regnum;
8846283Sdfr
89130809Smarcel  if (mm0_regnum < 0)
90130809Smarcel    return 0;
91130809Smarcel
92130809Smarcel  return (regnum >= mm0_regnum && regnum < mm0_regnum + i386_num_mmx_regs);
9398948Sobrien}
9498948Sobrien
95130809Smarcel/* SSE register?  */
96130809Smarcel
97130809Smarcelstatic int
98130809Smarceli386_sse_regnum_p (struct gdbarch *gdbarch, int regnum)
9998948Sobrien{
100130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
101130809Smarcel
102130809Smarcel#define I387_ST0_REGNUM tdep->st0_regnum
103130809Smarcel#define I387_NUM_XMM_REGS tdep->num_xmm_regs
104130809Smarcel
105130809Smarcel  if (I387_NUM_XMM_REGS == 0)
106130809Smarcel    return 0;
107130809Smarcel
108130809Smarcel  return (I387_XMM0_REGNUM <= regnum && regnum < I387_MXCSR_REGNUM);
109130809Smarcel
110130809Smarcel#undef I387_ST0_REGNUM
111130809Smarcel#undef I387_NUM_XMM_REGS
11298948Sobrien}
11398948Sobrien
114130809Smarcelstatic int
115130809Smarceli386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
116130809Smarcel{
117130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
11898948Sobrien
119130809Smarcel#define I387_ST0_REGNUM tdep->st0_regnum
120130809Smarcel#define I387_NUM_XMM_REGS tdep->num_xmm_regs
121130809Smarcel
122130809Smarcel  if (I387_NUM_XMM_REGS == 0)
123130809Smarcel    return 0;
124130809Smarcel
125130809Smarcel  return (regnum == I387_MXCSR_REGNUM);
126130809Smarcel
127130809Smarcel#undef I387_ST0_REGNUM
128130809Smarcel#undef I387_NUM_XMM_REGS
129130809Smarcel}
130130809Smarcel
131130809Smarcel#define I387_ST0_REGNUM (gdbarch_tdep (current_gdbarch)->st0_regnum)
132130809Smarcel#define I387_MM0_REGNUM (gdbarch_tdep (current_gdbarch)->mm0_regnum)
133130809Smarcel#define I387_NUM_XMM_REGS (gdbarch_tdep (current_gdbarch)->num_xmm_regs)
134130809Smarcel
135130809Smarcel/* FP register?  */
136130809Smarcel
13798948Sobrienint
138130809Smarceli386_fp_regnum_p (int regnum)
13998948Sobrien{
140130809Smarcel  if (I387_ST0_REGNUM < 0)
141130809Smarcel    return 0;
142130809Smarcel
143130809Smarcel  return (I387_ST0_REGNUM <= regnum && regnum < I387_FCTRL_REGNUM);
14498948Sobrien}
14598948Sobrien
14698948Sobrienint
147130809Smarceli386_fpc_regnum_p (int regnum)
14898948Sobrien{
149130809Smarcel  if (I387_ST0_REGNUM < 0)
150130809Smarcel    return 0;
151130809Smarcel
152130809Smarcel  return (I387_FCTRL_REGNUM <= regnum && regnum < I387_XMM0_REGNUM);
15398948Sobrien}
15498948Sobrien
155130809Smarcel/* Return the name of register REG.  */
156130809Smarcel
157130809Smarcelconst char *
158130809Smarceli386_register_name (int reg)
159130809Smarcel{
160130809Smarcel  if (i386_mmx_regnum_p (current_gdbarch, reg))
161130809Smarcel    return i386_mmx_names[reg - I387_MM0_REGNUM];
162130809Smarcel
163130809Smarcel  if (reg >= 0 && reg < i386_num_register_names)
164130809Smarcel    return i386_register_names[reg];
165130809Smarcel
166130809Smarcel  return NULL;
167130809Smarcel}
168130809Smarcel
16998948Sobrien/* Convert stabs register number REG to the appropriate register
17098948Sobrien   number used by GDB.  */
17198948Sobrien
172130809Smarcelstatic int
17398948Sobrieni386_stab_reg_to_regnum (int reg)
17498948Sobrien{
17598948Sobrien  /* This implements what GCC calls the "default" register map.  */
17698948Sobrien  if (reg >= 0 && reg <= 7)
17798948Sobrien    {
178130809Smarcel      /* General-purpose registers.  */
17998948Sobrien      return reg;
18098948Sobrien    }
18198948Sobrien  else if (reg >= 12 && reg <= 19)
18298948Sobrien    {
18398948Sobrien      /* Floating-point registers.  */
184130809Smarcel      return reg - 12 + I387_ST0_REGNUM;
18598948Sobrien    }
18698948Sobrien  else if (reg >= 21 && reg <= 28)
18798948Sobrien    {
18898948Sobrien      /* SSE registers.  */
189130809Smarcel      return reg - 21 + I387_XMM0_REGNUM;
19098948Sobrien    }
19198948Sobrien  else if (reg >= 29 && reg <= 36)
19298948Sobrien    {
19398948Sobrien      /* MMX registers.  */
194130809Smarcel      return reg - 29 + I387_MM0_REGNUM;
19598948Sobrien    }
19698948Sobrien
19798948Sobrien  /* This will hopefully provoke a warning.  */
19898948Sobrien  return NUM_REGS + NUM_PSEUDO_REGS;
19998948Sobrien}
20098948Sobrien
201130809Smarcel/* Convert DWARF register number REG to the appropriate register
20298948Sobrien   number used by GDB.  */
20398948Sobrien
204130809Smarcelstatic int
20598948Sobrieni386_dwarf_reg_to_regnum (int reg)
20698948Sobrien{
20798948Sobrien  /* The DWARF register numbering includes %eip and %eflags, and
20898948Sobrien     numbers the floating point registers differently.  */
20998948Sobrien  if (reg >= 0 && reg <= 9)
21098948Sobrien    {
211130809Smarcel      /* General-purpose registers.  */
21298948Sobrien      return reg;
21398948Sobrien    }
21498948Sobrien  else if (reg >= 11 && reg <= 18)
21598948Sobrien    {
21698948Sobrien      /* Floating-point registers.  */
217130809Smarcel      return reg - 11 + I387_ST0_REGNUM;
21898948Sobrien    }
21998948Sobrien  else if (reg >= 21)
22098948Sobrien    {
22198948Sobrien      /* The SSE and MMX registers have identical numbers as in stabs.  */
22298948Sobrien      return i386_stab_reg_to_regnum (reg);
22398948Sobrien    }
22498948Sobrien
22598948Sobrien  /* This will hopefully provoke a warning.  */
22698948Sobrien  return NUM_REGS + NUM_PSEUDO_REGS;
22798948Sobrien}
228130809Smarcel
229130809Smarcel#undef I387_ST0_REGNUM
230130809Smarcel#undef I387_MM0_REGNUM
231130809Smarcel#undef I387_NUM_XMM_REGS
23298948Sobrien
23398948Sobrien
23498948Sobrien/* This is the variable that is set with "set disassembly-flavor", and
23598948Sobrien   its legitimate values.  */
23698948Sobrienstatic const char att_flavor[] = "att";
23798948Sobrienstatic const char intel_flavor[] = "intel";
23898948Sobrienstatic const char *valid_flavors[] =
23998948Sobrien{
24046283Sdfr  att_flavor,
24146283Sdfr  intel_flavor,
24246283Sdfr  NULL
24346283Sdfr};
24498948Sobrienstatic const char *disassembly_flavor = att_flavor;
245130809Smarcel
24646283Sdfr
247130809Smarcel/* Use the program counter to determine the contents and size of a
248130809Smarcel   breakpoint instruction.  Return a pointer to a string of bytes that
249130809Smarcel   encode a breakpoint instruction, store the length of the string in
250130809Smarcel   *LEN and optionally adjust *PC to point to the correct memory
251130809Smarcel   location for inserting the breakpoint.
25219370Spst
253130809Smarcel   On the i386 we have a single breakpoint that fits in a single byte
254130809Smarcel   and can be inserted anywhere.
25519370Spst
256130809Smarcel   This function is 64-bit safe.  */
257130809Smarcel
258130809Smarcelstatic const unsigned char *
259130809Smarceli386_breakpoint_from_pc (CORE_ADDR *pc, int *len)
26019370Spst{
261130809Smarcel  static unsigned char break_insn[] = { 0xcc };	/* int 3 */
262130809Smarcel
263130809Smarcel  *len = sizeof (break_insn);
264130809Smarcel  return break_insn;
26519370Spst}
266130809Smarcel
267130809Smarcel#ifdef I386_REGNO_TO_SYMMETRY
268130809Smarcel#error "The Sequent Symmetry is no longer supported."
269130809Smarcel#endif
27019370Spst
271130809Smarcel/* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi
272130809Smarcel   and %esp "belong" to the calling function.  Therefore these
273130809Smarcel   registers should be saved if they're going to be modified.  */
274130809Smarcel
275130809Smarcel/* The maximum number of saved registers.  This should include all
276130809Smarcel   registers mentioned above, and %eip.  */
277130809Smarcel#define I386_NUM_SAVED_REGS	I386_NUM_GREGS
278130809Smarcel
279130809Smarcelstruct i386_frame_cache
28019370Spst{
281130809Smarcel  /* Base address.  */
282130809Smarcel  CORE_ADDR base;
283130809Smarcel  CORE_ADDR sp_offset;
284130809Smarcel  CORE_ADDR pc;
28519370Spst
286130809Smarcel  /* Saved registers.  */
287130809Smarcel  CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
288130809Smarcel  CORE_ADDR saved_sp;
289130809Smarcel  int pc_in_eax;
290130809Smarcel
291130809Smarcel  /* Stack space reserved for local variables.  */
292130809Smarcel  long locals;
293130809Smarcel};
294130809Smarcel
295130809Smarcel/* Allocate and initialize a frame cache.  */
296130809Smarcel
297130809Smarcelstatic struct i386_frame_cache *
298130809Smarceli386_alloc_frame_cache (void)
29919370Spst{
300130809Smarcel  struct i386_frame_cache *cache;
30119370Spst  int i;
302130809Smarcel
303130809Smarcel  cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
304130809Smarcel
305130809Smarcel  /* Base address.  */
306130809Smarcel  cache->base = 0;
307130809Smarcel  cache->sp_offset = -4;
308130809Smarcel  cache->pc = 0;
309130809Smarcel
310130809Smarcel  /* Saved registers.  We initialize these to -1 since zero is a valid
311130809Smarcel     offset (that's where %ebp is supposed to be stored).  */
312130809Smarcel  for (i = 0; i < I386_NUM_SAVED_REGS; i++)
313130809Smarcel    cache->saved_regs[i] = -1;
314130809Smarcel  cache->saved_sp = 0;
315130809Smarcel  cache->pc_in_eax = 0;
316130809Smarcel
317130809Smarcel  /* Frameless until proven otherwise.  */
318130809Smarcel  cache->locals = -1;
319130809Smarcel
320130809Smarcel  return cache;
32119370Spst}
32219370Spst
323130809Smarcel/* If the instruction at PC is a jump, return the address of its
324130809Smarcel   target.  Otherwise, return PC.  */
32519370Spst
326130809Smarcelstatic CORE_ADDR
327130809Smarceli386_follow_jump (CORE_ADDR pc)
32819370Spst{
329130809Smarcel  unsigned char op;
330130809Smarcel  long delta = 0;
331130809Smarcel  int data16 = 0;
33219370Spst
333130809Smarcel  op = read_memory_unsigned_integer (pc, 1);
334130809Smarcel  if (op == 0x66)
33519370Spst    {
33619370Spst      data16 = 1;
337130809Smarcel      op = read_memory_unsigned_integer (pc + 1, 1);
33819370Spst    }
33919370Spst
340130809Smarcel  switch (op)
34119370Spst    {
34219370Spst    case 0xe9:
34398948Sobrien      /* Relative jump: if data16 == 0, disp32, else disp16.  */
34419370Spst      if (data16)
34519370Spst	{
346130809Smarcel	  delta = read_memory_integer (pc + 2, 2);
34719370Spst
34898948Sobrien	  /* Include the size of the jmp instruction (including the
34998948Sobrien             0x66 prefix).  */
350130809Smarcel	  delta += 4;
35119370Spst	}
35219370Spst      else
35319370Spst	{
354130809Smarcel	  delta = read_memory_integer (pc + 1, 4);
35519370Spst
356130809Smarcel	  /* Include the size of the jmp instruction.  */
357130809Smarcel	  delta += 5;
35819370Spst	}
35919370Spst      break;
36019370Spst    case 0xeb:
36198948Sobrien      /* Relative jump, disp8 (ignore data16).  */
362130809Smarcel      delta = read_memory_integer (pc + data16 + 1, 1);
36319370Spst
364130809Smarcel      delta += data16 + 2;
36519370Spst      break;
36619370Spst    }
367130809Smarcel
368130809Smarcel  return pc + delta;
36919370Spst}
37019370Spst
371130809Smarcel/* Check whether PC points at a prologue for a function returning a
372130809Smarcel   structure or union.  If so, it updates CACHE and returns the
373130809Smarcel   address of the first instruction after the code sequence that
374130809Smarcel   removes the "hidden" argument from the stack or CURRENT_PC,
375130809Smarcel   whichever is smaller.  Otherwise, return PC.  */
37619370Spst
377130809Smarcelstatic CORE_ADDR
378130809Smarceli386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
379130809Smarcel			    struct i386_frame_cache *cache)
38019370Spst{
381130809Smarcel  /* Functions that return a structure or union start with:
38219370Spst
383130809Smarcel        popl %eax             0x58
384130809Smarcel        xchgl %eax, (%esp)    0x87 0x04 0x24
385130809Smarcel     or xchgl %eax, 0(%esp)   0x87 0x44 0x24 0x00
38619370Spst
387130809Smarcel     (the System V compiler puts out the second `xchg' instruction,
388130809Smarcel     and the assembler doesn't try to optimize it, so the 'sib' form
389130809Smarcel     gets generated).  This sequence is used to get the address of the
390130809Smarcel     return buffer for a function that returns a structure.  */
391130809Smarcel  static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
392130809Smarcel  static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
393130809Smarcel  unsigned char buf[4];
394130809Smarcel  unsigned char op;
39519370Spst
396130809Smarcel  if (current_pc <= pc)
397130809Smarcel    return pc;
39819370Spst
399130809Smarcel  op = read_memory_unsigned_integer (pc, 1);
40098948Sobrien
401130809Smarcel  if (op != 0x58)		/* popl %eax */
402130809Smarcel    return pc;
40398948Sobrien
404130809Smarcel  read_memory (pc + 1, buf, 4);
405130809Smarcel  if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
406130809Smarcel    return pc;
40798948Sobrien
408130809Smarcel  if (current_pc == pc)
409130809Smarcel    {
410130809Smarcel      cache->sp_offset += 4;
411130809Smarcel      return current_pc;
41219370Spst    }
41319370Spst
414130809Smarcel  if (current_pc == pc + 1)
41546283Sdfr    {
416130809Smarcel      cache->pc_in_eax = 1;
417130809Smarcel      return current_pc;
418130809Smarcel    }
419130809Smarcel
420130809Smarcel  if (buf[1] == proto1[1])
421130809Smarcel    return pc + 4;
422130809Smarcel  else
423130809Smarcel    return pc + 5;
424130809Smarcel}
42598948Sobrien
426130809Smarcelstatic CORE_ADDR
427130809Smarceli386_skip_probe (CORE_ADDR pc)
428130809Smarcel{
429130809Smarcel  /* A function may start with
430130809Smarcel
431130809Smarcel        pushl constant
432130809Smarcel        call _probe
433130809Smarcel	addl $4, %esp
43498948Sobrien
435130809Smarcel     followed by
43698948Sobrien
437130809Smarcel        pushl %ebp
43898948Sobrien
439130809Smarcel     etc.  */
440130809Smarcel  unsigned char buf[8];
441130809Smarcel  unsigned char op;
44246283Sdfr
443130809Smarcel  op = read_memory_unsigned_integer (pc, 1);
444130809Smarcel
445130809Smarcel  if (op == 0x68 || op == 0x6a)
446130809Smarcel    {
447130809Smarcel      int delta;
448130809Smarcel
449130809Smarcel      /* Skip past the `pushl' instruction; it has either a one-byte or a
450130809Smarcel	 four-byte operand, depending on the opcode.  */
45146283Sdfr      if (op == 0x68)
452130809Smarcel	delta = 5;
45346283Sdfr      else
454130809Smarcel	delta = 2;
45546283Sdfr
456130809Smarcel      /* Read the following 8 bytes, which should be `call _probe' (6
457130809Smarcel	 bytes) followed by `addl $4,%esp' (2 bytes).  */
458130809Smarcel      read_memory (pc + delta, buf, sizeof (buf));
45946283Sdfr      if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
460130809Smarcel	pc += delta + sizeof (buf);
46146283Sdfr    }
46246283Sdfr
463130809Smarcel  return pc;
464130809Smarcel}
465130809Smarcel
466130809Smarcel/* Check whether PC points at a code that sets up a new stack frame.
467130809Smarcel   If so, it updates CACHE and returns the address of the first
468130809Smarcel   instruction after the sequence that sets removes the "hidden"
469130809Smarcel   argument from the stack or CURRENT_PC, whichever is smaller.
470130809Smarcel   Otherwise, return PC.  */
471130809Smarcel
472130809Smarcelstatic CORE_ADDR
473130809Smarceli386_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR current_pc,
474130809Smarcel			  struct i386_frame_cache *cache)
475130809Smarcel{
476130809Smarcel  unsigned char op;
477130809Smarcel  int skip = 0;
478130809Smarcel
479130809Smarcel  if (current_pc <= pc)
480130809Smarcel    return current_pc;
481130809Smarcel
482130809Smarcel  op = read_memory_unsigned_integer (pc, 1);
483130809Smarcel
48419370Spst  if (op == 0x55)		/* pushl %ebp */
48598948Sobrien    {
486130809Smarcel      /* Take into account that we've executed the `pushl %ebp' that
487130809Smarcel	 starts this instruction sequence.  */
488130809Smarcel      cache->saved_regs[I386_EBP_REGNUM] = 0;
489130809Smarcel      cache->sp_offset += 4;
490130809Smarcel
491130809Smarcel      /* If that's all, return now.  */
492130809Smarcel      if (current_pc <= pc + 1)
493130809Smarcel	return current_pc;
494130809Smarcel
495130809Smarcel      op = read_memory_unsigned_integer (pc + 1, 1);
496130809Smarcel
497130809Smarcel      /* Check for some special instructions that might be migrated
498130809Smarcel	 by GCC into the prologue.  We check for
499130809Smarcel
500130809Smarcel	    xorl %ebx, %ebx
501130809Smarcel	    xorl %ecx, %ecx
502130809Smarcel	    xorl %edx, %edx
503130809Smarcel	    xorl %eax, %eax
504130809Smarcel
505130809Smarcel	 and the equivalent
506130809Smarcel
507130809Smarcel	    subl %ebx, %ebx
508130809Smarcel	    subl %ecx, %ecx
509130809Smarcel	    subl %edx, %edx
510130809Smarcel	    subl %eax, %eax
511130809Smarcel
512130809Smarcel	 Because of the symmetry, there are actually two ways to
513130809Smarcel	 encode these instructions; with opcode bytes 0x29 and 0x2b
514130809Smarcel	 for `subl' and opcode bytes 0x31 and 0x33 for `xorl'.
515130809Smarcel
516130809Smarcel	 Make sure we only skip these instructions if we later see the
517130809Smarcel	 `movl %esp, %ebp' that actually sets up the frame.  */
518130809Smarcel      while (op == 0x29 || op == 0x2b || op == 0x31 || op == 0x33)
51919370Spst	{
520130809Smarcel	  op = read_memory_unsigned_integer (pc + skip + 2, 1);
521130809Smarcel	  switch (op)
522130809Smarcel	    {
523130809Smarcel	    case 0xdb:	/* %ebx */
524130809Smarcel	    case 0xc9:	/* %ecx */
525130809Smarcel	    case 0xd2:	/* %edx */
526130809Smarcel	    case 0xc0:	/* %eax */
527130809Smarcel	      skip += 2;
528130809Smarcel	      break;
529130809Smarcel	    default:
530130809Smarcel	      return pc + 1;
531130809Smarcel	    }
532130809Smarcel
533130809Smarcel	  op = read_memory_unsigned_integer (pc + skip + 1, 1);
534130809Smarcel	}
535130809Smarcel
536130809Smarcel      /* Check for `movl %esp, %ebp' -- can be written in two ways.  */
537130809Smarcel      switch (op)
538130809Smarcel	{
53919370Spst	case 0x8b:
540130809Smarcel	  if (read_memory_unsigned_integer (pc + skip + 2, 1) != 0xec)
541130809Smarcel	    return pc + 1;
54219370Spst	  break;
54319370Spst	case 0x89:
544130809Smarcel	  if (read_memory_unsigned_integer (pc + skip + 2, 1) != 0xe5)
545130809Smarcel	    return pc + 1;
54619370Spst	  break;
54719370Spst	default:
548130809Smarcel	  return pc + 1;
54919370Spst	}
550130809Smarcel
551130809Smarcel      /* OK, we actually have a frame.  We just don't know how large
552130809Smarcel	 it is yet.  Set its size to zero.  We'll adjust it if
553130809Smarcel	 necessary.  We also now commit to skipping the special
554130809Smarcel	 instructions mentioned before.  */
555130809Smarcel      cache->locals = 0;
556130809Smarcel      pc += skip;
557130809Smarcel
558130809Smarcel      /* If that's all, return now.  */
559130809Smarcel      if (current_pc <= pc + 3)
560130809Smarcel	return current_pc;
561130809Smarcel
56298948Sobrien      /* Check for stack adjustment
56398948Sobrien
564130809Smarcel	    subl $XXX, %esp
56598948Sobrien
56698948Sobrien	 NOTE: You can't subtract a 16 bit immediate from a 32 bit
56798948Sobrien	 reg, so we don't have to worry about a data16 prefix.  */
568130809Smarcel      op = read_memory_unsigned_integer (pc + 3, 1);
56919370Spst      if (op == 0x83)
57019370Spst	{
57198948Sobrien	  /* `subl' with 8 bit immediate.  */
572130809Smarcel	  if (read_memory_unsigned_integer (pc + 4, 1) != 0xec)
57398948Sobrien	    /* Some instruction starting with 0x83 other than `subl'.  */
574130809Smarcel	    return pc + 3;
575130809Smarcel
576130809Smarcel	  /* `subl' with signed byte immediate (though it wouldn't make
577130809Smarcel	     sense to be negative).  */
578130809Smarcel	  cache->locals = read_memory_integer (pc + 5, 1);
579130809Smarcel	  return pc + 6;
58019370Spst	}
58119370Spst      else if (op == 0x81)
58219370Spst	{
58398948Sobrien	  /* Maybe it is `subl' with a 32 bit immedediate.  */
584130809Smarcel	  if (read_memory_unsigned_integer (pc + 4, 1) != 0xec)
58598948Sobrien	    /* Some instruction starting with 0x81 other than `subl'.  */
586130809Smarcel	    return pc + 3;
587130809Smarcel
58898948Sobrien	  /* It is `subl' with a 32 bit immediate.  */
589130809Smarcel	  cache->locals = read_memory_integer (pc + 5, 4);
590130809Smarcel	  return pc + 9;
59119370Spst	}
59219370Spst      else
59319370Spst	{
594130809Smarcel	  /* Some instruction other than `subl'.  */
595130809Smarcel	  return pc + 3;
59619370Spst	}
59719370Spst    }
598130809Smarcel  else if (op == 0xc8)		/* enter $XXX */
59919370Spst    {
600130809Smarcel      cache->locals = read_memory_unsigned_integer (pc + 1, 2);
601130809Smarcel      return pc + 4;
60219370Spst    }
60319370Spst
604130809Smarcel  return pc;
60598948Sobrien}
60698948Sobrien
607130809Smarcel/* Check whether PC points at code that saves registers on the stack.
608130809Smarcel   If so, it updates CACHE and returns the address of the first
609130809Smarcel   instruction after the register saves or CURRENT_PC, whichever is
610130809Smarcel   smaller.  Otherwise, return PC.  */
61198948Sobrien
612130809Smarcelstatic CORE_ADDR
613130809Smarceli386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
614130809Smarcel			     struct i386_frame_cache *cache)
61598948Sobrien{
616130809Smarcel  CORE_ADDR offset = 0;
617130809Smarcel  unsigned char op;
618130809Smarcel  int i;
61998948Sobrien
620130809Smarcel  if (cache->locals > 0)
621130809Smarcel    offset -= cache->locals;
622130809Smarcel  for (i = 0; i < 8 && pc < current_pc; i++)
623130809Smarcel    {
624130809Smarcel      op = read_memory_unsigned_integer (pc, 1);
625130809Smarcel      if (op < 0x50 || op > 0x57)
626130809Smarcel	break;
62798948Sobrien
628130809Smarcel      offset -= 4;
629130809Smarcel      cache->saved_regs[op - 0x50] = offset;
630130809Smarcel      cache->sp_offset += 4;
631130809Smarcel      pc++;
632130809Smarcel    }
63398948Sobrien
634130809Smarcel  return pc;
63598948Sobrien}
63698948Sobrien
637130809Smarcel/* Do a full analysis of the prologue at PC and update CACHE
638130809Smarcel   accordingly.  Bail out early if CURRENT_PC is reached.  Return the
639130809Smarcel   address where the analysis stopped.
64098948Sobrien
64198948Sobrien   We handle these cases:
64219370Spst
64398948Sobrien   The startup sequence can be at the start of the function, or the
64498948Sobrien   function can start with a branch to startup code at the end.
64598948Sobrien
64698948Sobrien   %ebp can be set up with either the 'enter' instruction, or "pushl
64798948Sobrien   %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
64898948Sobrien   once used in the System V compiler).
64998948Sobrien
65098948Sobrien   Local space is allocated just below the saved %ebp by either the
65198948Sobrien   'enter' instruction, or by "subl $<size>, %esp".  'enter' has a 16
65298948Sobrien   bit unsigned argument for space to allocate, and the 'addl'
65398948Sobrien   instruction could have either a signed byte, or 32 bit immediate.
65498948Sobrien
65598948Sobrien   Next, the registers used by this function are pushed.  With the
65698948Sobrien   System V compiler they will always be in the order: %edi, %esi,
65798948Sobrien   %ebx (and sometimes a harmless bug causes it to also save but not
65898948Sobrien   restore %eax); however, the code below is willing to see the pushes
65998948Sobrien   in any order, and will handle up to 8 of them.
66098948Sobrien
66198948Sobrien   If the setup sequence is at the end of the function, then the next
66298948Sobrien   instruction will be a branch back to the start.  */
66398948Sobrien
664130809Smarcelstatic CORE_ADDR
665130809Smarceli386_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
666130809Smarcel		       struct i386_frame_cache *cache)
66719370Spst{
668130809Smarcel  pc = i386_follow_jump (pc);
669130809Smarcel  pc = i386_analyze_struct_return (pc, current_pc, cache);
670130809Smarcel  pc = i386_skip_probe (pc);
671130809Smarcel  pc = i386_analyze_frame_setup (pc, current_pc, cache);
672130809Smarcel  return i386_analyze_register_saves (pc, current_pc, cache);
67319370Spst}
67419370Spst
67598948Sobrien/* Return PC of first real instruction.  */
67619370Spst
677130809Smarcelstatic CORE_ADDR
678130809Smarceli386_skip_prologue (CORE_ADDR start_pc)
67919370Spst{
680130809Smarcel  static unsigned char pic_pat[6] =
681130809Smarcel  {
682130809Smarcel    0xe8, 0, 0, 0, 0,		/* call 0x0 */
683130809Smarcel    0x5b,			/* popl %ebx */
684130809Smarcel  };
685130809Smarcel  struct i386_frame_cache cache;
686130809Smarcel  CORE_ADDR pc;
68719370Spst  unsigned char op;
68819370Spst  int i;
68998948Sobrien
690130809Smarcel  cache.locals = -1;
691130809Smarcel  pc = i386_analyze_prologue (start_pc, 0xffffffff, &cache);
692130809Smarcel  if (cache.locals < 0)
693130809Smarcel    return start_pc;
69498948Sobrien
695130809Smarcel  /* Found valid frame setup.  */
69698948Sobrien
69798948Sobrien  /* The native cc on SVR4 in -K PIC mode inserts the following code
69898948Sobrien     to get the address of the global offset table (GOT) into register
699130809Smarcel     %ebx:
700130809Smarcel
70198948Sobrien        call	0x0
70298948Sobrien	popl    %ebx
70398948Sobrien        movl    %ebx,x(%ebp)    (optional)
70498948Sobrien        addl    y,%ebx
70598948Sobrien
70619370Spst     This code is with the rest of the prologue (at the end of the
70719370Spst     function), so we have to skip it to get to the first real
70819370Spst     instruction at the start of the function.  */
70998948Sobrien
71019370Spst  for (i = 0; i < 6; i++)
71119370Spst    {
712130809Smarcel      op = read_memory_unsigned_integer (pc + i, 1);
71398948Sobrien      if (pic_pat[i] != op)
71419370Spst	break;
71519370Spst    }
71619370Spst  if (i == 6)
71719370Spst    {
718130809Smarcel      int delta = 6;
71919370Spst
720130809Smarcel      op = read_memory_unsigned_integer (pc + delta, 1);
721130809Smarcel
72298948Sobrien      if (op == 0x89)		/* movl %ebx, x(%ebp) */
72319370Spst	{
724130809Smarcel	  op = read_memory_unsigned_integer (pc + delta + 1, 1);
725130809Smarcel
72698948Sobrien	  if (op == 0x5d)	/* One byte offset from %ebp.  */
727130809Smarcel	    delta += 3;
72898948Sobrien	  else if (op == 0x9d)	/* Four byte offset from %ebp.  */
729130809Smarcel	    delta += 6;
73098948Sobrien	  else			/* Unexpected instruction.  */
731130809Smarcel	    delta = 0;
732130809Smarcel
733130809Smarcel	  op = read_memory_unsigned_integer (pc + delta, 1);
73419370Spst	}
735130809Smarcel
73698948Sobrien      /* addl y,%ebx */
737130809Smarcel      if (delta > 0 && op == 0x81
738219418Semaste	  && read_memory_unsigned_integer (pc + delta + 1, 1) == 0xc3)
73919370Spst	{
740130809Smarcel	  pc += delta + 6;
74119370Spst	}
74219370Spst    }
74398948Sobrien
744130809Smarcel  return i386_follow_jump (pc);
745130809Smarcel}
74698948Sobrien
747130809Smarcel/* This function is 64-bit safe.  */
748130809Smarcel
749130809Smarcelstatic CORE_ADDR
750130809Smarceli386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
751130809Smarcel{
752130809Smarcel  char buf[8];
753130809Smarcel
754130809Smarcel  frame_unwind_register (next_frame, PC_REGNUM, buf);
755130809Smarcel  return extract_typed_address (buf, builtin_type_void_func_ptr);
75619370Spst}
757130809Smarcel
75819370Spst
759130809Smarcel/* Normal frames.  */
760130809Smarcel
761130809Smarcelstatic struct i386_frame_cache *
762130809Smarceli386_frame_cache (struct frame_info *next_frame, void **this_cache)
76319370Spst{
764130809Smarcel  struct i386_frame_cache *cache;
765130809Smarcel  char buf[4];
766130809Smarcel  int i;
76798948Sobrien
768130809Smarcel  if (*this_cache)
769130809Smarcel    return *this_cache;
770130809Smarcel
771130809Smarcel  cache = i386_alloc_frame_cache ();
772130809Smarcel  *this_cache = cache;
773130809Smarcel
774130809Smarcel  /* In principle, for normal frames, %ebp holds the frame pointer,
775130809Smarcel     which holds the base address for the current stack frame.
776130809Smarcel     However, for functions that don't need it, the frame pointer is
777130809Smarcel     optional.  For these "frameless" functions the frame pointer is
778130809Smarcel     actually the frame pointer of the calling frame.  Signal
779130809Smarcel     trampolines are just a special case of a "frameless" function.
780130809Smarcel     They (usually) share their frame pointer with the frame that was
781130809Smarcel     in progress when the signal occurred.  */
782130809Smarcel
783130809Smarcel  frame_unwind_register (next_frame, I386_EBP_REGNUM, buf);
784130809Smarcel  cache->base = extract_unsigned_integer (buf, 4);
785130809Smarcel  if (cache->base == 0)
786130809Smarcel    return cache;
787130809Smarcel
788130809Smarcel  /* For normal frames, %eip is stored at 4(%ebp).  */
789130809Smarcel  cache->saved_regs[I386_EIP_REGNUM] = 4;
790130809Smarcel
791130809Smarcel  cache->pc = frame_func_unwind (next_frame);
792130809Smarcel  if (cache->pc != 0)
793130809Smarcel    i386_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
794130809Smarcel
795130809Smarcel  if (cache->locals < 0)
79619370Spst    {
797130809Smarcel      /* We didn't find a valid frame, which means that CACHE->base
798130809Smarcel	 currently holds the frame pointer for our calling frame.  If
799130809Smarcel	 we're at the start of a function, or somewhere half-way its
800130809Smarcel	 prologue, the function's frame probably hasn't been fully
801130809Smarcel	 setup yet.  Try to reconstruct the base address for the stack
802130809Smarcel	 frame by looking at the stack pointer.  For truly "frameless"
803130809Smarcel	 functions this might work too.  */
804130809Smarcel
805130809Smarcel      frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
806130809Smarcel      cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
80719370Spst    }
808130809Smarcel
809130809Smarcel  /* Now that we have the base address for the stack frame we can
810130809Smarcel     calculate the value of %esp in the calling frame.  */
811130809Smarcel  cache->saved_sp = cache->base + 8;
812130809Smarcel
813130809Smarcel  /* Adjust all the saved registers such that they contain addresses
814130809Smarcel     instead of offsets.  */
815130809Smarcel  for (i = 0; i < I386_NUM_SAVED_REGS; i++)
816130809Smarcel    if (cache->saved_regs[i] != -1)
817130809Smarcel      cache->saved_regs[i] += cache->base;
818130809Smarcel
819130809Smarcel  return cache;
82019370Spst}
82119370Spst
822130809Smarcelstatic void
823130809Smarceli386_frame_this_id (struct frame_info *next_frame, void **this_cache,
824130809Smarcel		    struct frame_id *this_id)
82519370Spst{
826130809Smarcel  struct i386_frame_cache *cache = i386_frame_cache (next_frame, this_cache);
82798948Sobrien
828130809Smarcel  /* This marks the outermost frame.  */
829130809Smarcel  if (cache->base == 0)
830130809Smarcel    return;
83198948Sobrien
832130809Smarcel  /* See the end of i386_push_dummy_call.  */
833130809Smarcel  (*this_id) = frame_id_build (cache->base + 8, cache->pc);
83498948Sobrien}
83598948Sobrien
836130809Smarcelstatic void
837130809Smarceli386_frame_prev_register (struct frame_info *next_frame, void **this_cache,
838130809Smarcel			  int regnum, int *optimizedp,
839130809Smarcel			  enum lval_type *lvalp, CORE_ADDR *addrp,
840130809Smarcel			  int *realnump, void *valuep)
84198948Sobrien{
842130809Smarcel  struct i386_frame_cache *cache = i386_frame_cache (next_frame, this_cache);
84398948Sobrien
844130809Smarcel  gdb_assert (regnum >= 0);
84598948Sobrien
846130809Smarcel  /* The System V ABI says that:
847130809Smarcel
848130809Smarcel     "The flags register contains the system flags, such as the
849130809Smarcel     direction flag and the carry flag.  The direction flag must be
850130809Smarcel     set to the forward (that is, zero) direction before entry and
851130809Smarcel     upon exit from a function.  Other user flags have no specified
852130809Smarcel     role in the standard calling sequence and are not preserved."
853130809Smarcel
854130809Smarcel     To guarantee the "upon exit" part of that statement we fake a
855130809Smarcel     saved flags register that has its direction flag cleared.
856130809Smarcel
857130809Smarcel     Note that GCC doesn't seem to rely on the fact that the direction
858130809Smarcel     flag is cleared after a function return; it always explicitly
859130809Smarcel     clears the flag before operations where it matters.
860130809Smarcel
861130809Smarcel     FIXME: kettenis/20030316: I'm not quite sure whether this is the
862130809Smarcel     right thing to do.  The way we fake the flags register here makes
863130809Smarcel     it impossible to change it.  */
864130809Smarcel
865130809Smarcel  if (regnum == I386_EFLAGS_REGNUM)
86619370Spst    {
867130809Smarcel      *optimizedp = 0;
868130809Smarcel      *lvalp = not_lval;
869130809Smarcel      *addrp = 0;
870130809Smarcel      *realnump = -1;
871130809Smarcel      if (valuep)
87219370Spst	{
873130809Smarcel	  ULONGEST val;
874130809Smarcel
875130809Smarcel	  /* Clear the direction flag.  */
876130809Smarcel	  val = frame_unwind_register_unsigned (next_frame,
877130809Smarcel						I386_EFLAGS_REGNUM);
878130809Smarcel	  val &= ~(1 << 10);
879130809Smarcel	  store_unsigned_integer (valuep, 4, val);
88019370Spst	}
881130809Smarcel
882130809Smarcel      return;
88319370Spst    }
884130809Smarcel
885130809Smarcel  if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
886130809Smarcel    {
887130809Smarcel      frame_register_unwind (next_frame, I386_EAX_REGNUM,
888130809Smarcel			     optimizedp, lvalp, addrp, realnump, valuep);
889130809Smarcel      return;
890130809Smarcel    }
891130809Smarcel
892130809Smarcel  if (regnum == I386_ESP_REGNUM && cache->saved_sp)
893130809Smarcel    {
894130809Smarcel      *optimizedp = 0;
895130809Smarcel      *lvalp = not_lval;
896130809Smarcel      *addrp = 0;
897130809Smarcel      *realnump = -1;
898130809Smarcel      if (valuep)
899130809Smarcel	{
900130809Smarcel	  /* Store the value.  */
901130809Smarcel	  store_unsigned_integer (valuep, 4, cache->saved_sp);
902130809Smarcel	}
903130809Smarcel      return;
904130809Smarcel    }
905130809Smarcel
906130809Smarcel  if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
907130809Smarcel    {
908130809Smarcel      *optimizedp = 0;
909130809Smarcel      *lvalp = lval_memory;
910130809Smarcel      *addrp = cache->saved_regs[regnum];
911130809Smarcel      *realnump = -1;
912130809Smarcel      if (valuep)
913130809Smarcel	{
914130809Smarcel	  /* Read the value in from memory.  */
915130809Smarcel	  read_memory (*addrp, valuep,
916130809Smarcel		       register_size (current_gdbarch, regnum));
917130809Smarcel	}
918130809Smarcel      return;
919130809Smarcel    }
920130809Smarcel
921130809Smarcel  frame_register_unwind (next_frame, regnum,
922130809Smarcel			 optimizedp, lvalp, addrp, realnump, valuep);
92319370Spst}
924130809Smarcel
925130809Smarcelstatic const struct frame_unwind i386_frame_unwind =
926130809Smarcel{
927130809Smarcel  NORMAL_FRAME,
928130809Smarcel  i386_frame_this_id,
929130809Smarcel  i386_frame_prev_register
930130809Smarcel};
931130809Smarcel
932130809Smarcelstatic const struct frame_unwind *
933130809Smarceli386_frame_sniffer (struct frame_info *next_frame)
934130809Smarcel{
935130809Smarcel  return &i386_frame_unwind;
936130809Smarcel}
93798948Sobrien
93819370Spst
939130809Smarcel/* Signal trampolines.  */
94019370Spst
941130809Smarcelstatic struct i386_frame_cache *
942130809Smarceli386_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
943130809Smarcel{
944130809Smarcel  struct i386_frame_cache *cache;
945130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
946130809Smarcel  CORE_ADDR addr;
947130809Smarcel  char buf[4];
94819370Spst
949130809Smarcel  if (*this_cache)
950130809Smarcel    return *this_cache;
95198948Sobrien
952130809Smarcel  cache = i386_alloc_frame_cache ();
953130809Smarcel
954130809Smarcel  frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
955130809Smarcel  cache->base = extract_unsigned_integer (buf, 4) - 4;
956130809Smarcel
957130809Smarcel  addr = tdep->sigcontext_addr (next_frame);
958130809Smarcel  if (tdep->sc_reg_offset)
959130809Smarcel    {
960130809Smarcel      int i;
961130809Smarcel
962130809Smarcel      gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS);
963130809Smarcel
964130809Smarcel      for (i = 0; i < tdep->sc_num_regs; i++)
965130809Smarcel	if (tdep->sc_reg_offset[i] != -1)
966130809Smarcel	  cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
967130809Smarcel    }
968130809Smarcel  else
969130809Smarcel    {
970130809Smarcel      cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset;
971130809Smarcel      cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset;
972130809Smarcel    }
973130809Smarcel
974130809Smarcel  *this_cache = cache;
975130809Smarcel  return cache;
976130809Smarcel}
977130809Smarcel
978130809Smarcelstatic void
979130809Smarceli386_sigtramp_frame_this_id (struct frame_info *next_frame, void **this_cache,
980130809Smarcel			     struct frame_id *this_id)
981130809Smarcel{
982130809Smarcel  struct i386_frame_cache *cache =
983130809Smarcel    i386_sigtramp_frame_cache (next_frame, this_cache);
984130809Smarcel
985130809Smarcel  /* See the end of i386_push_dummy_call.  */
986130809Smarcel  (*this_id) = frame_id_build (cache->base + 8, frame_pc_unwind (next_frame));
987130809Smarcel}
988130809Smarcel
989130809Smarcelstatic void
990130809Smarceli386_sigtramp_frame_prev_register (struct frame_info *next_frame,
991130809Smarcel				   void **this_cache,
992130809Smarcel				   int regnum, int *optimizedp,
993130809Smarcel				   enum lval_type *lvalp, CORE_ADDR *addrp,
994130809Smarcel				   int *realnump, void *valuep)
995130809Smarcel{
996130809Smarcel  /* Make sure we've initialized the cache.  */
997130809Smarcel  i386_sigtramp_frame_cache (next_frame, this_cache);
998130809Smarcel
999130809Smarcel  i386_frame_prev_register (next_frame, this_cache, regnum,
1000130809Smarcel			    optimizedp, lvalp, addrp, realnump, valuep);
1001130809Smarcel}
1002130809Smarcel
1003130809Smarcelstatic const struct frame_unwind i386_sigtramp_frame_unwind =
1004130809Smarcel{
1005130809Smarcel  SIGTRAMP_FRAME,
1006130809Smarcel  i386_sigtramp_frame_this_id,
1007130809Smarcel  i386_sigtramp_frame_prev_register
1008130809Smarcel};
1009130809Smarcel
1010130809Smarcelstatic const struct frame_unwind *
1011130809Smarceli386_sigtramp_frame_sniffer (struct frame_info *next_frame)
1012130809Smarcel{
1013130809Smarcel  CORE_ADDR pc = frame_pc_unwind (next_frame);
1014130809Smarcel  char *name;
1015130809Smarcel
1016130809Smarcel  /* We shouldn't even bother to try if the OSABI didn't register
1017130809Smarcel     a sigcontext_addr handler.  */
1018130809Smarcel  if (!gdbarch_tdep (current_gdbarch)->sigcontext_addr)
1019130809Smarcel    return NULL;
1020130809Smarcel
1021130809Smarcel  find_pc_partial_function (pc, &name, NULL, NULL);
1022130809Smarcel  if (PC_IN_SIGTRAMP (pc, name))
1023130809Smarcel    return &i386_sigtramp_frame_unwind;
1024130809Smarcel
1025130809Smarcel  return NULL;
1026130809Smarcel}
1027130809Smarcel
1028130809Smarcel
1029130809Smarcelstatic CORE_ADDR
1030130809Smarceli386_frame_base_address (struct frame_info *next_frame, void **this_cache)
1031130809Smarcel{
1032130809Smarcel  struct i386_frame_cache *cache = i386_frame_cache (next_frame, this_cache);
1033130809Smarcel
1034130809Smarcel  return cache->base;
1035130809Smarcel}
1036130809Smarcel
1037130809Smarcelstatic const struct frame_base i386_frame_base =
1038130809Smarcel{
1039130809Smarcel  &i386_frame_unwind,
1040130809Smarcel  i386_frame_base_address,
1041130809Smarcel  i386_frame_base_address,
1042130809Smarcel  i386_frame_base_address
1043130809Smarcel};
1044130809Smarcel
1045130809Smarcelstatic struct frame_id
1046130809Smarceli386_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1047130809Smarcel{
1048130809Smarcel  char buf[4];
1049130809Smarcel  CORE_ADDR fp;
1050130809Smarcel
1051130809Smarcel  frame_unwind_register (next_frame, I386_EBP_REGNUM, buf);
1052130809Smarcel  fp = extract_unsigned_integer (buf, 4);
1053130809Smarcel
1054130809Smarcel  /* See the end of i386_push_dummy_call.  */
1055130809Smarcel  return frame_id_build (fp + 8, frame_pc_unwind (next_frame));
1056130809Smarcel}
1057130809Smarcel
1058130809Smarcel
105998948Sobrien/* Figure out where the longjmp will land.  Slurp the args out of the
106098948Sobrien   stack.  We expect the first arg to be a pointer to the jmp_buf
1061130809Smarcel   structure from which we extract the address that we will land at.
1062130809Smarcel   This address is copied into PC.  This routine returns non-zero on
1063130809Smarcel   success.
106498948Sobrien
1065130809Smarcel   This function is 64-bit safe.  */
1066130809Smarcel
1067130809Smarcelstatic int
1068130809Smarceli386_get_longjmp_target (CORE_ADDR *pc)
106919370Spst{
1070130809Smarcel  char buf[8];
107119370Spst  CORE_ADDR sp, jb_addr;
1072130809Smarcel  int jb_pc_offset = gdbarch_tdep (current_gdbarch)->jb_pc_offset;
1073130809Smarcel  int len = TYPE_LENGTH (builtin_type_void_func_ptr);
107419370Spst
1075130809Smarcel  /* If JB_PC_OFFSET is -1, we have no way to find out where the
1076130809Smarcel     longjmp will land.  */
1077130809Smarcel  if (jb_pc_offset == -1)
1078130809Smarcel    return 0;
107919370Spst
1080130809Smarcel  /* Don't use I386_ESP_REGNUM here, since this function is also used
1081130809Smarcel     for AMD64.  */
1082130809Smarcel  regcache_cooked_read (current_regcache, SP_REGNUM, buf);
1083130809Smarcel  sp = extract_typed_address (buf, builtin_type_void_data_ptr);
1084130809Smarcel  if (target_read_memory (sp + len, buf, len))
108519370Spst    return 0;
108619370Spst
1087130809Smarcel  jb_addr = extract_typed_address (buf, builtin_type_void_data_ptr);
1088130809Smarcel  if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
108919370Spst    return 0;
109019370Spst
1091130809Smarcel  *pc = extract_typed_address (buf, builtin_type_void_func_ptr);
109219370Spst  return 1;
109319370Spst}
109498948Sobrien
109519370Spst
1096130809Smarcelstatic CORE_ADDR
1097130809Smarceli386_push_dummy_call (struct gdbarch *gdbarch, CORE_ADDR func_addr,
1098130809Smarcel		      struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
1099130809Smarcel		      struct value **args, CORE_ADDR sp, int struct_return,
1100130809Smarcel		      CORE_ADDR struct_addr)
110198948Sobrien{
1102130809Smarcel  char buf[4];
1103130809Smarcel  int i;
1104130809Smarcel
1105130809Smarcel  /* Push arguments in reverse order.  */
1106130809Smarcel  for (i = nargs - 1; i >= 0; i--)
1107130809Smarcel    {
1108130809Smarcel      int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
1109130809Smarcel
1110130809Smarcel      /* The System V ABI says that:
1111130809Smarcel
1112130809Smarcel	 "An argument's size is increased, if necessary, to make it a
1113130809Smarcel	 multiple of [32-bit] words.  This may require tail padding,
1114130809Smarcel	 depending on the size of the argument."
1115130809Smarcel
1116130809Smarcel	 This makes sure the stack says word-aligned.  */
1117130809Smarcel      sp -= (len + 3) & ~3;
1118130809Smarcel      write_memory (sp, VALUE_CONTENTS_ALL (args[i]), len);
1119130809Smarcel    }
1120130809Smarcel
1121130809Smarcel  /* Push value address.  */
112298948Sobrien  if (struct_return)
112398948Sobrien    {
112498948Sobrien      sp -= 4;
1125130809Smarcel      store_unsigned_integer (buf, 4, struct_addr);
112698948Sobrien      write_memory (sp, buf, 4);
112798948Sobrien    }
112898948Sobrien
1129130809Smarcel  /* Store return address.  */
1130130809Smarcel  sp -= 4;
1131130809Smarcel  store_unsigned_integer (buf, 4, bp_addr);
1132130809Smarcel  write_memory (sp, buf, 4);
113398948Sobrien
1134130809Smarcel  /* Finally, update the stack pointer...  */
1135130809Smarcel  store_unsigned_integer (buf, 4, sp);
1136130809Smarcel  regcache_cooked_write (regcache, I386_ESP_REGNUM, buf);
1137130809Smarcel
1138130809Smarcel  /* ...and fake a frame pointer.  */
1139130809Smarcel  regcache_cooked_write (regcache, I386_EBP_REGNUM, buf);
1140130809Smarcel
1141130809Smarcel  /* MarkK wrote: This "+ 8" is all over the place:
1142130809Smarcel     (i386_frame_this_id, i386_sigtramp_frame_this_id,
1143130809Smarcel     i386_unwind_dummy_id).  It's there, since all frame unwinders for
1144130809Smarcel     a given target have to agree (within a certain margin) on the
1145130809Smarcel     defenition of the stack address of a frame.  Otherwise
1146130809Smarcel     frame_id_inner() won't work correctly.  Since DWARF2/GCC uses the
1147130809Smarcel     stack address *before* the function call as a frame's CFA.  On
1148130809Smarcel     the i386, when %ebp is used as a frame pointer, the offset
1149130809Smarcel     between the contents %ebp and the CFA as defined by GCC.  */
1150130809Smarcel  return sp + 8;
115198948Sobrien}
115298948Sobrien
115398948Sobrien/* These registers are used for returning integers (and on some
115498948Sobrien   targets also for returning `struct' and `union' values when their
115598948Sobrien   size and alignment match an integer type).  */
1156130809Smarcel#define LOW_RETURN_REGNUM	I386_EAX_REGNUM /* %eax */
1157130809Smarcel#define HIGH_RETURN_REGNUM	I386_EDX_REGNUM /* %edx */
115898948Sobrien
1159130809Smarcel/* Read, for architecture GDBARCH, a function return value of TYPE
1160130809Smarcel   from REGCACHE, and copy that into VALBUF.  */
116198948Sobrien
1162130809Smarcelstatic void
1163130809Smarceli386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
1164130809Smarcel			   struct regcache *regcache, void *valbuf)
116598948Sobrien{
1166130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
116798948Sobrien  int len = TYPE_LENGTH (type);
1168130809Smarcel  char buf[I386_MAX_REGISTER_SIZE];
116998948Sobrien
117098948Sobrien  if (TYPE_CODE (type) == TYPE_CODE_FLT)
117198948Sobrien    {
1172130809Smarcel      if (tdep->st0_regnum < 0)
117398948Sobrien	{
117498948Sobrien	  warning ("Cannot find floating-point return value.");
117598948Sobrien	  memset (valbuf, 0, len);
117698948Sobrien	  return;
117798948Sobrien	}
117898948Sobrien
117998948Sobrien      /* Floating-point return values can be found in %st(0).  Convert
118098948Sobrien	 its contents to the desired type.  This is probably not
118198948Sobrien	 exactly how it would happen on the target itself, but it is
118298948Sobrien	 the best we can do.  */
1183130809Smarcel      regcache_raw_read (regcache, I386_ST0_REGNUM, buf);
1184130809Smarcel      convert_typed_floating (buf, builtin_type_i387_ext, valbuf, type);
118598948Sobrien    }
118619370Spst  else
118798948Sobrien    {
1188130809Smarcel      int low_size = register_size (current_gdbarch, LOW_RETURN_REGNUM);
1189130809Smarcel      int high_size = register_size (current_gdbarch, HIGH_RETURN_REGNUM);
119098948Sobrien
119198948Sobrien      if (len <= low_size)
1192130809Smarcel	{
1193130809Smarcel	  regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
1194130809Smarcel	  memcpy (valbuf, buf, len);
1195130809Smarcel	}
119698948Sobrien      else if (len <= (low_size + high_size))
119798948Sobrien	{
1198130809Smarcel	  regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
1199130809Smarcel	  memcpy (valbuf, buf, low_size);
1200130809Smarcel	  regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
1201130809Smarcel	  memcpy ((char *) valbuf + low_size, buf, len - low_size);
120298948Sobrien	}
120398948Sobrien      else
120498948Sobrien	internal_error (__FILE__, __LINE__,
120598948Sobrien			"Cannot extract return value of %d bytes long.", len);
120619370Spst    }
120719370Spst}
120819370Spst
1209130809Smarcel/* Write, for architecture GDBARCH, a function return value of TYPE
1210130809Smarcel   from VALBUF into REGCACHE.  */
121198948Sobrien
1212130809Smarcelstatic void
1213130809Smarceli386_store_return_value (struct gdbarch *gdbarch, struct type *type,
1214130809Smarcel			 struct regcache *regcache, const void *valbuf)
121598948Sobrien{
1216130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
121798948Sobrien  int len = TYPE_LENGTH (type);
121898948Sobrien
1219130809Smarcel  /* Define I387_ST0_REGNUM such that we use the proper definitions
1220130809Smarcel     for the architecture.  */
1221130809Smarcel#define I387_ST0_REGNUM I386_ST0_REGNUM
122298948Sobrien
122398948Sobrien  if (TYPE_CODE (type) == TYPE_CODE_FLT)
122498948Sobrien    {
1225130809Smarcel      ULONGEST fstat;
1226130809Smarcel      char buf[I386_MAX_REGISTER_SIZE];
122798948Sobrien
1228130809Smarcel      if (tdep->st0_regnum < 0)
122998948Sobrien	{
123098948Sobrien	  warning ("Cannot set floating-point return value.");
123198948Sobrien	  return;
123298948Sobrien	}
123398948Sobrien
123498948Sobrien      /* Returning floating-point values is a bit tricky.  Apart from
123598948Sobrien         storing the return value in %st(0), we have to simulate the
123698948Sobrien         state of the FPU at function return point.  */
123798948Sobrien
123898948Sobrien      /* Convert the value found in VALBUF to the extended
123998948Sobrien	 floating-point format used by the FPU.  This is probably
124098948Sobrien	 not exactly how it would happen on the target itself, but
124198948Sobrien	 it is the best we can do.  */
124298948Sobrien      convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
1243130809Smarcel      regcache_raw_write (regcache, I386_ST0_REGNUM, buf);
124498948Sobrien
124598948Sobrien      /* Set the top of the floating-point register stack to 7.  The
124698948Sobrien         actual value doesn't really matter, but 7 is what a normal
124798948Sobrien         function return would end up with if the program started out
124898948Sobrien         with a freshly initialized FPU.  */
1249130809Smarcel      regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM, &fstat);
125098948Sobrien      fstat |= (7 << 11);
1251130809Smarcel      regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM, fstat);
125298948Sobrien
125398948Sobrien      /* Mark %st(1) through %st(7) as empty.  Since we set the top of
125498948Sobrien         the floating-point register stack to 7, the appropriate value
125598948Sobrien         for the tag word is 0x3fff.  */
1256130809Smarcel      regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM, 0x3fff);
125798948Sobrien    }
125898948Sobrien  else
125998948Sobrien    {
1260130809Smarcel      int low_size = register_size (current_gdbarch, LOW_RETURN_REGNUM);
1261130809Smarcel      int high_size = register_size (current_gdbarch, HIGH_RETURN_REGNUM);
126298948Sobrien
126398948Sobrien      if (len <= low_size)
1264130809Smarcel	regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
126598948Sobrien      else if (len <= (low_size + high_size))
126698948Sobrien	{
1267130809Smarcel	  regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
1268130809Smarcel	  regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
1269130809Smarcel				   len - low_size, (char *) valbuf + low_size);
127098948Sobrien	}
127198948Sobrien      else
127298948Sobrien	internal_error (__FILE__, __LINE__,
127398948Sobrien			"Cannot store return value of %d bytes long.", len);
127498948Sobrien    }
1275130809Smarcel
1276130809Smarcel#undef I387_ST0_REGNUM
127798948Sobrien}
1278130809Smarcel
127998948Sobrien
1280130809Smarcel/* This is the variable that is set with "set struct-convention", and
1281130809Smarcel   its legitimate values.  */
1282130809Smarcelstatic const char default_struct_convention[] = "default";
1283130809Smarcelstatic const char pcc_struct_convention[] = "pcc";
1284130809Smarcelstatic const char reg_struct_convention[] = "reg";
1285130809Smarcelstatic const char *valid_conventions[] =
1286130809Smarcel{
1287130809Smarcel  default_struct_convention,
1288130809Smarcel  pcc_struct_convention,
1289130809Smarcel  reg_struct_convention,
1290130809Smarcel  NULL
1291130809Smarcel};
1292130809Smarcelstatic const char *struct_convention = default_struct_convention;
129398948Sobrien
1294130809Smarcel/* Return non-zero if TYPE, which is assumed to be a structure or
1295130809Smarcel   union type, should be returned in registers for architecture
1296130809Smarcel   GDBARCH.  */
1297130809Smarcel
1298130809Smarcelstatic int
1299130809Smarceli386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
130098948Sobrien{
1301130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1302130809Smarcel  enum type_code code = TYPE_CODE (type);
1303130809Smarcel  int len = TYPE_LENGTH (type);
1304130809Smarcel
1305130809Smarcel  gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
1306130809Smarcel
1307130809Smarcel  if (struct_convention == pcc_struct_convention
1308130809Smarcel      || (struct_convention == default_struct_convention
1309130809Smarcel	  && tdep->struct_return == pcc_struct_return))
1310130809Smarcel    return 0;
1311130809Smarcel
1312130809Smarcel  return (len == 1 || len == 2 || len == 4 || len == 8);
131398948Sobrien}
1314130809Smarcel
1315130809Smarcel/* Determine, for architecture GDBARCH, how a return value of TYPE
1316130809Smarcel   should be returned.  If it is supposed to be returned in registers,
1317130809Smarcel   and READBUF is non-zero, read the appropriate value from REGCACHE,
1318130809Smarcel   and copy it into READBUF.  If WRITEBUF is non-zero, write the value
1319130809Smarcel   from WRITEBUF into REGCACHE.  */
1320130809Smarcel
1321130809Smarcelstatic enum return_value_convention
1322130809Smarceli386_return_value (struct gdbarch *gdbarch, struct type *type,
1323130809Smarcel		   struct regcache *regcache, void *readbuf,
1324130809Smarcel		   const void *writebuf)
1325130809Smarcel{
1326130809Smarcel  enum type_code code = TYPE_CODE (type);
1327130809Smarcel
1328130809Smarcel  if ((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
1329130809Smarcel      && !i386_reg_struct_return_p (gdbarch, type))
1330130809Smarcel    return RETURN_VALUE_STRUCT_CONVENTION;
1331130809Smarcel
1332130809Smarcel  /* This special case is for structures consisting of a single
1333130809Smarcel     `float' or `double' member.  These structures are returned in
1334130809Smarcel     %st(0).  For these structures, we call ourselves recursively,
1335130809Smarcel     changing TYPE into the type of the first member of the structure.
1336130809Smarcel     Since that should work for all structures that have only one
1337130809Smarcel     member, we don't bother to check the member's type here.  */
1338130809Smarcel  if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
1339130809Smarcel    {
1340130809Smarcel      type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1341130809Smarcel      return i386_return_value (gdbarch, type, regcache, readbuf, writebuf);
1342130809Smarcel    }
1343130809Smarcel
1344130809Smarcel  if (readbuf)
1345130809Smarcel    i386_extract_return_value (gdbarch, type, regcache, readbuf);
1346130809Smarcel  if (writebuf)
1347130809Smarcel    i386_store_return_value (gdbarch, type, regcache, writebuf);
1348130809Smarcel
1349130809Smarcel  return RETURN_VALUE_REGISTER_CONVENTION;
1350130809Smarcel}
135198948Sobrien
135298948Sobrien
135398948Sobrien/* Return the GDB type object for the "standard" data type of data in
135498948Sobrien   register REGNUM.  Perhaps %esi and %edi should go here, but
135598948Sobrien   potentially they could be used for things other than address.  */
135698948Sobrien
1357130809Smarcelstatic struct type *
1358130809Smarceli386_register_type (struct gdbarch *gdbarch, int regnum)
135998948Sobrien{
1360130809Smarcel  if (regnum == I386_EIP_REGNUM
1361130809Smarcel      || regnum == I386_EBP_REGNUM || regnum == I386_ESP_REGNUM)
136298948Sobrien    return lookup_pointer_type (builtin_type_void);
136398948Sobrien
1364130809Smarcel  if (i386_fp_regnum_p (regnum))
136598948Sobrien    return builtin_type_i387_ext;
136698948Sobrien
1367130809Smarcel  if (i386_sse_regnum_p (gdbarch, regnum))
1368130809Smarcel    return builtin_type_vec128i;
136998948Sobrien
1370130809Smarcel  if (i386_mmx_regnum_p (gdbarch, regnum))
1371130809Smarcel    return builtin_type_vec64i;
1372130809Smarcel
137398948Sobrien  return builtin_type_int;
137498948Sobrien}
137598948Sobrien
1376130809Smarcel/* Map a cooked register onto a raw register or memory.  For the i386,
1377130809Smarcel   the MMX registers need to be mapped onto floating point registers.  */
137898948Sobrien
1379130809Smarcelstatic int
1380130809Smarceli386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
138198948Sobrien{
1382130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
1383130809Smarcel  int mmxreg, fpreg;
1384130809Smarcel  ULONGEST fstat;
1385130809Smarcel  int tos;
1386130809Smarcel
1387130809Smarcel  /* Define I387_ST0_REGNUM such that we use the proper definitions
1388130809Smarcel     for REGCACHE's architecture.  */
1389130809Smarcel#define I387_ST0_REGNUM tdep->st0_regnum
1390130809Smarcel
1391130809Smarcel  mmxreg = regnum - tdep->mm0_regnum;
1392130809Smarcel  regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM, &fstat);
1393130809Smarcel  tos = (fstat >> 11) & 0x7;
1394130809Smarcel  fpreg = (mmxreg + tos) % 8;
1395130809Smarcel
1396130809Smarcel  return (I387_ST0_REGNUM + fpreg);
1397130809Smarcel
1398130809Smarcel#undef I387_ST0_REGNUM
139998948Sobrien}
140098948Sobrien
1401130809Smarcelstatic void
1402130809Smarceli386_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
1403130809Smarcel			   int regnum, void *buf)
1404130809Smarcel{
1405130809Smarcel  if (i386_mmx_regnum_p (gdbarch, regnum))
1406130809Smarcel    {
1407130809Smarcel      char mmx_buf[MAX_REGISTER_SIZE];
1408130809Smarcel      int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
140998948Sobrien
1410130809Smarcel      /* Extract (always little endian).  */
1411130809Smarcel      regcache_raw_read (regcache, fpnum, mmx_buf);
1412130809Smarcel      memcpy (buf, mmx_buf, register_size (gdbarch, regnum));
1413130809Smarcel    }
1414130809Smarcel  else
1415130809Smarcel    regcache_raw_read (regcache, regnum, buf);
1416130809Smarcel}
1417130809Smarcel
1418130809Smarcelstatic void
1419130809Smarceli386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
1420130809Smarcel			    int regnum, const void *buf)
142198948Sobrien{
1422130809Smarcel  if (i386_mmx_regnum_p (gdbarch, regnum))
1423130809Smarcel    {
1424130809Smarcel      char mmx_buf[MAX_REGISTER_SIZE];
1425130809Smarcel      int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
142698948Sobrien
1427130809Smarcel      /* Read ...  */
1428130809Smarcel      regcache_raw_read (regcache, fpnum, mmx_buf);
1429130809Smarcel      /* ... Modify ... (always little endian).  */
1430130809Smarcel      memcpy (mmx_buf, buf, register_size (gdbarch, regnum));
1431130809Smarcel      /* ... Write.  */
1432130809Smarcel      regcache_raw_write (regcache, fpnum, mmx_buf);
1433130809Smarcel    }
1434130809Smarcel  else
1435130809Smarcel    regcache_raw_write (regcache, regnum, buf);
1436130809Smarcel}
1437130809Smarcel
1438130809Smarcel
1439130809Smarcel/* Return the register number of the register allocated by GCC after
1440130809Smarcel   REGNUM, or -1 if there is no such register.  */
1441130809Smarcel
1442130809Smarcelstatic int
1443130809Smarceli386_next_regnum (int regnum)
1444130809Smarcel{
1445130809Smarcel  /* GCC allocates the registers in the order:
1446130809Smarcel
1447130809Smarcel     %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ...
1448130809Smarcel
1449130809Smarcel     Since storing a variable in %esp doesn't make any sense we return
1450130809Smarcel     -1 for %ebp and for %esp itself.  */
1451130809Smarcel  static int next_regnum[] =
1452130809Smarcel  {
1453130809Smarcel    I386_EDX_REGNUM,		/* Slot for %eax.  */
1454130809Smarcel    I386_EBX_REGNUM,		/* Slot for %ecx.  */
1455130809Smarcel    I386_ECX_REGNUM,		/* Slot for %edx.  */
1456130809Smarcel    I386_ESI_REGNUM,		/* Slot for %ebx.  */
1457130809Smarcel    -1, -1,			/* Slots for %esp and %ebp.  */
1458130809Smarcel    I386_EDI_REGNUM,		/* Slot for %esi.  */
1459130809Smarcel    I386_EBP_REGNUM		/* Slot for %edi.  */
1460130809Smarcel  };
1461130809Smarcel
1462130809Smarcel  if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
1463130809Smarcel    return next_regnum[regnum];
1464130809Smarcel
1465130809Smarcel  return -1;
1466130809Smarcel}
1467130809Smarcel
1468130809Smarcel/* Return nonzero if a value of type TYPE stored in register REGNUM
1469130809Smarcel   needs any special handling.  */
1470130809Smarcel
1471130809Smarcelstatic int
1472130809Smarceli386_convert_register_p (int regnum, struct type *type)
1473130809Smarcel{
1474130809Smarcel  int len = TYPE_LENGTH (type);
1475130809Smarcel
1476130809Smarcel  /* Values may be spread across multiple registers.  Most debugging
1477130809Smarcel     formats aren't expressive enough to specify the locations, so
1478130809Smarcel     some heuristics is involved.  Right now we only handle types that
1479130809Smarcel     have a length that is a multiple of the word size, since GCC
1480130809Smarcel     doesn't seem to put any other types into registers.  */
1481130809Smarcel  if (len > 4 && len % 4 == 0)
148298948Sobrien    {
1483130809Smarcel      int last_regnum = regnum;
1484130809Smarcel
1485130809Smarcel      while (len > 4)
1486130809Smarcel	{
1487130809Smarcel	  last_regnum = i386_next_regnum (last_regnum);
1488130809Smarcel	  len -= 4;
1489130809Smarcel	}
1490130809Smarcel
1491130809Smarcel      if (last_regnum != -1)
1492130809Smarcel	return 1;
1493130809Smarcel    }
1494130809Smarcel
1495130809Smarcel  return i386_fp_regnum_p (regnum);
1496130809Smarcel}
1497130809Smarcel
1498130809Smarcel/* Read a value of type TYPE from register REGNUM in frame FRAME, and
1499130809Smarcel   return its contents in TO.  */
1500130809Smarcel
1501130809Smarcelstatic void
1502130809Smarceli386_register_to_value (struct frame_info *frame, int regnum,
1503130809Smarcel			struct type *type, void *to)
1504130809Smarcel{
1505130809Smarcel  int len = TYPE_LENGTH (type);
1506130809Smarcel  char *buf = to;
1507130809Smarcel
1508130809Smarcel  /* FIXME: kettenis/20030609: What should we do if REGNUM isn't
1509130809Smarcel     available in FRAME (i.e. if it wasn't saved)?  */
1510130809Smarcel
1511130809Smarcel  if (i386_fp_regnum_p (regnum))
1512130809Smarcel    {
1513130809Smarcel      i387_register_to_value (frame, regnum, type, to);
151498948Sobrien      return;
151598948Sobrien    }
151698948Sobrien
1517130809Smarcel  /* Read a value spread accross multiple registers.  */
1518130809Smarcel
1519130809Smarcel  gdb_assert (len > 4 && len % 4 == 0);
1520130809Smarcel
1521130809Smarcel  while (len > 0)
1522130809Smarcel    {
1523130809Smarcel      gdb_assert (regnum != -1);
1524130809Smarcel      gdb_assert (register_size (current_gdbarch, regnum) == 4);
1525130809Smarcel
1526130809Smarcel      get_frame_register (frame, regnum, buf);
1527130809Smarcel      regnum = i386_next_regnum (regnum);
1528130809Smarcel      len -= 4;
1529130809Smarcel      buf += 4;
1530130809Smarcel    }
153198948Sobrien}
153298948Sobrien
1533130809Smarcel/* Write the contents FROM of a value of type TYPE into register
1534130809Smarcel   REGNUM in frame FRAME.  */
153598948Sobrien
1536130809Smarcelstatic void
1537130809Smarceli386_value_to_register (struct frame_info *frame, int regnum,
1538130809Smarcel			struct type *type, const void *from)
1539130809Smarcel{
1540130809Smarcel  int len = TYPE_LENGTH (type);
1541130809Smarcel  const char *buf = from;
1542130809Smarcel
1543130809Smarcel  if (i386_fp_regnum_p (regnum))
1544130809Smarcel    {
1545130809Smarcel      i387_value_to_register (frame, regnum, type, from);
1546130809Smarcel      return;
1547130809Smarcel    }
1548130809Smarcel
1549130809Smarcel  /* Write a value spread accross multiple registers.  */
1550130809Smarcel
1551130809Smarcel  gdb_assert (len > 4 && len % 4 == 0);
1552130809Smarcel
1553130809Smarcel  while (len > 0)
1554130809Smarcel    {
1555130809Smarcel      gdb_assert (regnum != -1);
1556130809Smarcel      gdb_assert (register_size (current_gdbarch, regnum) == 4);
1557130809Smarcel
1558130809Smarcel      put_frame_register (frame, regnum, buf);
1559130809Smarcel      regnum = i386_next_regnum (regnum);
1560130809Smarcel      len -= 4;
1561130809Smarcel      buf += 4;
1562130809Smarcel    }
1563130809Smarcel}
1564130809Smarcel
1565130809Smarcel/* Supply register REGNUM from the general-purpose register set REGSET
1566130809Smarcel   to register cache REGCACHE.  If REGNUM is -1, do this for all
1567130809Smarcel   registers in REGSET.  */
1568130809Smarcel
156998948Sobrienvoid
1570130809Smarceli386_supply_gregset (const struct regset *regset, struct regcache *regcache,
1571130809Smarcel		     int regnum, const void *gregs, size_t len)
157298948Sobrien{
1573130809Smarcel  const struct gdbarch_tdep *tdep = regset->descr;
1574130809Smarcel  const char *regs = gregs;
1575130809Smarcel  int i;
157698948Sobrien
1577130809Smarcel  gdb_assert (len == tdep->sizeof_gregset);
1578130809Smarcel
1579130809Smarcel  for (i = 0; i < tdep->gregset_num_regs; i++)
158098948Sobrien    {
1581130809Smarcel      if ((regnum == i || regnum == -1)
1582130809Smarcel	  && tdep->gregset_reg_offset[i] != -1)
1583130809Smarcel	regcache_raw_supply (regcache, i, regs + tdep->gregset_reg_offset[i]);
1584130809Smarcel    }
1585130809Smarcel}
1586130809Smarcel
1587130809Smarcel/* Supply register REGNUM from the floating-point register set REGSET
1588130809Smarcel   to register cache REGCACHE.  If REGNUM is -1, do this for all
1589130809Smarcel   registers in REGSET.  */
1590130809Smarcel
1591130809Smarcelstatic void
1592130809Smarceli386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
1593130809Smarcel		      int regnum, const void *fpregs, size_t len)
1594130809Smarcel{
1595130809Smarcel  const struct gdbarch_tdep *tdep = regset->descr;
1596130809Smarcel
1597130809Smarcel  if (len == I387_SIZEOF_FXSAVE)
1598130809Smarcel    {
1599130809Smarcel      i387_supply_fxsave (regcache, regnum, fpregs);
160098948Sobrien      return;
160198948Sobrien    }
160298948Sobrien
1603130809Smarcel  gdb_assert (len == tdep->sizeof_fpregset);
1604130809Smarcel  i387_supply_fsave (regcache, regnum, fpregs);
160598948Sobrien}
160698948Sobrien
1607130809Smarcel/* Return the appropriate register set for the core section identified
1608130809Smarcel   by SECT_NAME and SECT_SIZE.  */
160919370Spst
1610130809Smarcelconst struct regset *
1611130809Smarceli386_regset_from_core_section (struct gdbarch *gdbarch,
1612130809Smarcel			       const char *sect_name, size_t sect_size)
161319370Spst{
1614130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
161519370Spst
1616130809Smarcel  if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
161719370Spst    {
1618130809Smarcel      if (tdep->gregset == NULL)
1619130809Smarcel	{
1620130809Smarcel	  tdep->gregset = XMALLOC (struct regset);
1621130809Smarcel	  tdep->gregset->descr = tdep;
1622130809Smarcel	  tdep->gregset->supply_regset = i386_supply_gregset;
1623130809Smarcel	}
1624130809Smarcel      return tdep->gregset;
162519370Spst    }
162619370Spst
1627130809Smarcel  if ((strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
1628130809Smarcel      || (strcmp (sect_name, ".reg-xfp") == 0
1629130809Smarcel	  && sect_size == I387_SIZEOF_FXSAVE))
1630130809Smarcel    {
1631130809Smarcel      if (tdep->fpregset == NULL)
1632130809Smarcel	{
1633130809Smarcel	  tdep->fpregset = XMALLOC (struct regset);
1634130809Smarcel	  tdep->fpregset->descr = tdep;
1635130809Smarcel	  tdep->fpregset->supply_regset = i386_supply_fpregset;
1636130809Smarcel	}
1637130809Smarcel      return tdep->fpregset;
1638130809Smarcel    }
1639130809Smarcel
1640130809Smarcel  return NULL;
164119370Spst}
164298948Sobrien
164319370Spst
164446283Sdfr#ifdef STATIC_TRANSFORM_NAME
164598948Sobrien/* SunPRO encodes the static variables.  This is not related to C++
164698948Sobrien   mangling, it is done for C too.  */
164719370Spst
164846283Sdfrchar *
164998948Sobriensunpro_static_transform_name (char *name)
165046283Sdfr{
165146283Sdfr  char *p;
165246283Sdfr  if (IS_STATIC_TRANSFORM_NAME (name))
165346283Sdfr    {
165498948Sobrien      /* For file-local statics there will be a period, a bunch of
165598948Sobrien         junk (the contents of which match a string given in the
165698948Sobrien         N_OPT), a period and the name.  For function-local statics
165798948Sobrien         there will be a bunch of junk (which seems to change the
165898948Sobrien         second character from 'A' to 'B'), a period, the name of the
165998948Sobrien         function, and the name.  So just skip everything before the
166098948Sobrien         last period.  */
166146283Sdfr      p = strrchr (name, '.');
166246283Sdfr      if (p != NULL)
166346283Sdfr	name = p + 1;
166446283Sdfr    }
166546283Sdfr  return name;
166646283Sdfr}
166746283Sdfr#endif /* STATIC_TRANSFORM_NAME */
166898948Sobrien
166919370Spst
167098948Sobrien/* Stuff for WIN32 PE style DLL's but is pretty generic really.  */
167146283Sdfr
167219370SpstCORE_ADDR
1673130809Smarceli386_pe_skip_trampoline_code (CORE_ADDR pc, char *name)
167419370Spst{
167519370Spst  if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
167619370Spst    {
167798948Sobrien      unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
167819370Spst      struct minimal_symbol *indsym =
167919370Spst	indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1680130809Smarcel      char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0;
168119370Spst
168298948Sobrien      if (symname)
168319370Spst	{
168498948Sobrien	  if (strncmp (symname, "__imp_", 6) == 0
168598948Sobrien	      || strncmp (symname, "_imp_", 5) == 0)
168619370Spst	    return name ? 1 : read_memory_unsigned_integer (indirect, 4);
168719370Spst	}
168819370Spst    }
168998948Sobrien  return 0;			/* Not a trampoline.  */
169019370Spst}
169198948Sobrien
169219370Spst
1693130809Smarcel/* Return non-zero if PC and NAME show that we are in a signal
1694130809Smarcel   trampoline.  */
1695130809Smarcel
1696130809Smarcelstatic int
1697130809Smarceli386_pc_in_sigtramp (CORE_ADDR pc, char *name)
1698130809Smarcel{
1699130809Smarcel  return (name && strcmp ("_sigtramp", name) == 0);
1700130809Smarcel}
1701130809Smarcel
1702130809Smarcel
170398948Sobrien/* We have two flavours of disassembly.  The machinery on this page
170498948Sobrien   deals with switching between those.  */
170598948Sobrien
170646283Sdfrstatic int
1707130809Smarceli386_print_insn (bfd_vma pc, struct disassemble_info *info)
170846283Sdfr{
1709130809Smarcel  gdb_assert (disassembly_flavor == att_flavor
1710130809Smarcel	      || disassembly_flavor == intel_flavor);
1711130809Smarcel
1712130809Smarcel  /* FIXME: kettenis/20020915: Until disassembler_options is properly
1713130809Smarcel     constified, cast to prevent a compiler warning.  */
1714130809Smarcel  info->disassembler_options = (char *) disassembly_flavor;
1715130809Smarcel  info->mach = gdbarch_bfd_arch_info (current_gdbarch)->mach;
1716130809Smarcel
1717130809Smarcel  return print_insn_i386 (pc, info);
171846283Sdfr}
1719130809Smarcel
172046283Sdfr
1721130809Smarcel/* There are a few i386 architecture variants that differ only
1722130809Smarcel   slightly from the generic i386 target.  For now, we don't give them
1723130809Smarcel   their own source file, but include them here.  As a consequence,
1724130809Smarcel   they'll always be included.  */
1725130809Smarcel
1726130809Smarcel/* System V Release 4 (SVR4).  */
1727130809Smarcel
1728130809Smarcelstatic int
1729130809Smarceli386_svr4_pc_in_sigtramp (CORE_ADDR pc, char *name)
1730130809Smarcel{
1731130809Smarcel  /* UnixWare uses _sigacthandler.  The origin of the other symbols is
1732130809Smarcel     currently unknown.  */
1733130809Smarcel  return (name && (strcmp ("_sigreturn", name) == 0
1734130809Smarcel		   || strcmp ("_sigacthandler", name) == 0
1735130809Smarcel		   || strcmp ("sigvechandler", name) == 0));
1736130809Smarcel}
1737130809Smarcel
1738130809Smarcel/* Assuming NEXT_FRAME is for a frame following a SVR4 sigtramp
1739130809Smarcel   routine, return the address of the associated sigcontext (ucontext)
1740130809Smarcel   structure.  */
1741130809Smarcel
1742130809Smarcelstatic CORE_ADDR
1743130809Smarceli386_svr4_sigcontext_addr (struct frame_info *next_frame)
1744130809Smarcel{
1745130809Smarcel  char buf[4];
1746130809Smarcel  CORE_ADDR sp;
1747130809Smarcel
1748130809Smarcel  frame_unwind_register (next_frame, I386_ESP_REGNUM, buf);
1749130809Smarcel  sp = extract_unsigned_integer (buf, 4);
1750130809Smarcel
1751130809Smarcel  return read_memory_unsigned_integer (sp + 8, 4);
1752130809Smarcel}
175398948Sobrien
1754130809Smarcel
1755130809Smarcel/* DJGPP.  */
1756130809Smarcel
1757130809Smarcelstatic int
1758130809Smarceli386_go32_pc_in_sigtramp (CORE_ADDR pc, char *name)
1759130809Smarcel{
1760130809Smarcel  /* DJGPP doesn't have any special frames for signal handlers.  */
1761130809Smarcel  return 0;
1762130809Smarcel}
1763130809Smarcel
1764130809Smarcel
1765130809Smarcel/* Generic ELF.  */
1766130809Smarcel
1767130809Smarcelvoid
1768130809Smarceli386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1769130809Smarcel{
1770130809Smarcel  /* We typically use stabs-in-ELF with the DWARF register numbering.  */
1771130809Smarcel  set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1772130809Smarcel}
1773130809Smarcel
1774130809Smarcel/* System V Release 4 (SVR4).  */
1775130809Smarcel
1776130809Smarcelvoid
1777130809Smarceli386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1778130809Smarcel{
1779130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1780130809Smarcel
1781130809Smarcel  /* System V Release 4 uses ELF.  */
1782130809Smarcel  i386_elf_init_abi (info, gdbarch);
1783130809Smarcel
1784130809Smarcel  /* System V Release 4 has shared libraries.  */
1785130809Smarcel  set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
1786130809Smarcel  set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
1787130809Smarcel
1788130809Smarcel  set_gdbarch_pc_in_sigtramp (gdbarch, i386_svr4_pc_in_sigtramp);
1789130809Smarcel  tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
1790130809Smarcel  tdep->sc_pc_offset = 36 + 14 * 4;
1791130809Smarcel  tdep->sc_sp_offset = 36 + 17 * 4;
1792130809Smarcel
1793130809Smarcel  tdep->jb_pc_offset = 20;
1794130809Smarcel}
1795130809Smarcel
1796130809Smarcel/* DJGPP.  */
1797130809Smarcel
179898948Sobrienstatic void
1799130809Smarceli386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
180098948Sobrien{
1801130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
180298948Sobrien
1803130809Smarcel  set_gdbarch_pc_in_sigtramp (gdbarch, i386_go32_pc_in_sigtramp);
180498948Sobrien
1805130809Smarcel  tdep->jb_pc_offset = 36;
1806130809Smarcel}
180798948Sobrien
1808130809Smarcel/* NetWare.  */
180998948Sobrien
1810130809Smarcelstatic void
1811130809Smarceli386_nw_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1812130809Smarcel{
1813130809Smarcel  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
181498948Sobrien
1815130809Smarcel  tdep->jb_pc_offset = 24;
181698948Sobrien}
1817130809Smarcel
181898948Sobrien
1819130809Smarcel/* i386 register groups.  In addition to the normal groups, add "mmx"
1820130809Smarcel   and "sse".  */
1821130809Smarcel
1822130809Smarcelstatic struct reggroup *i386_sse_reggroup;
1823130809Smarcelstatic struct reggroup *i386_mmx_reggroup;
1824130809Smarcel
1825130809Smarcelstatic void
1826130809Smarceli386_init_reggroups (void)
1827130809Smarcel{
1828130809Smarcel  i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
1829130809Smarcel  i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
1830130809Smarcel}
1831130809Smarcel
1832130809Smarcelstatic void
1833130809Smarceli386_add_reggroups (struct gdbarch *gdbarch)
1834130809Smarcel{
1835130809Smarcel  reggroup_add (gdbarch, i386_sse_reggroup);
1836130809Smarcel  reggroup_add (gdbarch, i386_mmx_reggroup);
1837130809Smarcel  reggroup_add (gdbarch, general_reggroup);
1838130809Smarcel  reggroup_add (gdbarch, float_reggroup);
1839130809Smarcel  reggroup_add (gdbarch, all_reggroup);
1840130809Smarcel  reggroup_add (gdbarch, save_reggroup);
1841130809Smarcel  reggroup_add (gdbarch, restore_reggroup);
1842130809Smarcel  reggroup_add (gdbarch, vector_reggroup);
1843130809Smarcel  reggroup_add (gdbarch, system_reggroup);
1844130809Smarcel}
1845130809Smarcel
1846130809Smarcelint
1847130809Smarceli386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1848130809Smarcel			  struct reggroup *group)
1849130809Smarcel{
1850130809Smarcel  int sse_regnum_p = (i386_sse_regnum_p (gdbarch, regnum)
1851130809Smarcel		      || i386_mxcsr_regnum_p (gdbarch, regnum));
1852130809Smarcel  int fp_regnum_p = (i386_fp_regnum_p (regnum)
1853130809Smarcel		     || i386_fpc_regnum_p (regnum));
1854130809Smarcel  int mmx_regnum_p = (i386_mmx_regnum_p (gdbarch, regnum));
1855130809Smarcel
1856130809Smarcel  if (group == i386_mmx_reggroup)
1857130809Smarcel    return mmx_regnum_p;
1858130809Smarcel  if (group == i386_sse_reggroup)
1859130809Smarcel    return sse_regnum_p;
1860130809Smarcel  if (group == vector_reggroup)
1861130809Smarcel    return (mmx_regnum_p || sse_regnum_p);
1862130809Smarcel  if (group == float_reggroup)
1863130809Smarcel    return fp_regnum_p;
1864130809Smarcel  if (group == general_reggroup)
1865130809Smarcel    return (!fp_regnum_p && !mmx_regnum_p && !sse_regnum_p);
1866130809Smarcel
1867130809Smarcel  return default_register_reggroup_p (gdbarch, regnum, group);
1868130809Smarcel}
1869130809Smarcel
1870130809Smarcel
1871130809Smarcel/* Get the ARGIth function argument for the current function.  */
1872130809Smarcel
1873130809Smarcelstatic CORE_ADDR
1874130809Smarceli386_fetch_pointer_argument (struct frame_info *frame, int argi,
1875130809Smarcel			     struct type *type)
1876130809Smarcel{
1877130809Smarcel  CORE_ADDR sp = get_frame_register_unsigned  (frame, I386_ESP_REGNUM);
1878130809Smarcel  return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4);
1879130809Smarcel}
1880130809Smarcel
1881130809Smarcel
1882130809Smarcelstatic struct gdbarch *
188398948Sobrieni386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
188498948Sobrien{
188598948Sobrien  struct gdbarch_tdep *tdep;
188698948Sobrien  struct gdbarch *gdbarch;
188798948Sobrien
1888130809Smarcel  /* If there is already a candidate, use it.  */
1889130809Smarcel  arches = gdbarch_list_lookup_by_info (arches, &info);
1890130809Smarcel  if (arches != NULL)
1891130809Smarcel    return arches->gdbarch;
189298948Sobrien
189398948Sobrien  /* Allocate space for the new architecture.  */
189498948Sobrien  tdep = XMALLOC (struct gdbarch_tdep);
189598948Sobrien  gdbarch = gdbarch_alloc (&info, tdep);
189698948Sobrien
1897130809Smarcel  /* General-purpose registers.  */
1898130809Smarcel  tdep->gregset = NULL;
1899130809Smarcel  tdep->gregset_reg_offset = NULL;
1900130809Smarcel  tdep->gregset_num_regs = I386_NUM_GREGS;
1901130809Smarcel  tdep->sizeof_gregset = 0;
190298948Sobrien
1903130809Smarcel  /* Floating-point registers.  */
1904130809Smarcel  tdep->fpregset = NULL;
1905130809Smarcel  tdep->sizeof_fpregset = I387_SIZEOF_FSAVE;
190698948Sobrien
1907130809Smarcel  /* The default settings include the FPU registers, the MMX registers
1908130809Smarcel     and the SSE registers.  This can be overidden for a specific ABI
1909130809Smarcel     by adjusting the members `st0_regnum', `mm0_regnum' and
1910130809Smarcel     `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers
1911130809Smarcel     will show up in the output of "info all-registers".  Ideally we
1912130809Smarcel     should try to autodetect whether they are available, such that we
1913130809Smarcel     can prevent "info all-registers" from displaying registers that
1914130809Smarcel     aren't available.
191598948Sobrien
1916130809Smarcel     NOTE: kevinb/2003-07-13: ... if it's a choice between printing
1917130809Smarcel     [the SSE registers] always (even when they don't exist) or never
1918130809Smarcel     showing them to the user (even when they do exist), I prefer the
1919130809Smarcel     former over the latter.  */
1920130809Smarcel
1921130809Smarcel  tdep->st0_regnum = I386_ST0_REGNUM;
1922130809Smarcel
1923130809Smarcel  /* The MMX registers are implemented as pseudo-registers.  Put off
1924130809Smarcel     caclulating the register number for %mm0 until we know the number
1925130809Smarcel     of raw registers.  */
1926130809Smarcel  tdep->mm0_regnum = 0;
1927130809Smarcel
1928130809Smarcel  /* I386_NUM_XREGS includes %mxcsr, so substract one.  */
1929130809Smarcel  tdep->num_xmm_regs = I386_NUM_XREGS - 1;
1930130809Smarcel
1931130809Smarcel  tdep->jb_pc_offset = -1;
1932130809Smarcel  tdep->struct_return = pcc_struct_return;
1933130809Smarcel  tdep->sigtramp_start = 0;
1934130809Smarcel  tdep->sigtramp_end = 0;
1935130809Smarcel  tdep->sigcontext_addr = NULL;
1936130809Smarcel  tdep->sc_reg_offset = NULL;
1937130809Smarcel  tdep->sc_pc_offset = -1;
1938130809Smarcel  tdep->sc_sp_offset = -1;
1939130809Smarcel
1940130809Smarcel  /* The format used for `long double' on almost all i386 targets is
1941130809Smarcel     the i387 extended floating-point format.  In fact, of all targets
1942130809Smarcel     in the GCC 2.95 tree, only OSF/1 does it different, and insists
1943130809Smarcel     on having a `long double' that's not `long' at all.  */
1944130809Smarcel  set_gdbarch_long_double_format (gdbarch, &floatformat_i387_ext);
1945130809Smarcel
1946130809Smarcel  /* Although the i387 extended floating-point has only 80 significant
1947130809Smarcel     bits, a `long double' actually takes up 96, probably to enforce
1948130809Smarcel     alignment.  */
1949130809Smarcel  set_gdbarch_long_double_bit (gdbarch, 96);
1950130809Smarcel
1951130809Smarcel  /* The default ABI includes general-purpose registers,
1952130809Smarcel     floating-point registers, and the SSE registers.  */
1953130809Smarcel  set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS);
1954130809Smarcel  set_gdbarch_register_name (gdbarch, i386_register_name);
1955130809Smarcel  set_gdbarch_register_type (gdbarch, i386_register_type);
1956130809Smarcel
1957130809Smarcel  /* Register numbers of various important registers.  */
1958130809Smarcel  set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */
1959130809Smarcel  set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */
1960130809Smarcel  set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */
1961130809Smarcel  set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */
1962130809Smarcel
1963130809Smarcel  /* Use the "default" register numbering scheme for stabs and COFF.  */
1964130809Smarcel  set_gdbarch_stab_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1965130809Smarcel  set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1966130809Smarcel
1967130809Smarcel  /* Use the DWARF register numbering scheme for DWARF and DWARF 2.  */
1968130809Smarcel  set_gdbarch_dwarf_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1969130809Smarcel  set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1970130809Smarcel
1971130809Smarcel  /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1972130809Smarcel     be in use on any of the supported i386 targets.  */
1973130809Smarcel
1974130809Smarcel  set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
1975130809Smarcel
1976130809Smarcel  set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
1977130809Smarcel
197898948Sobrien  /* Call dummy code.  */
1979130809Smarcel  set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
198098948Sobrien
1981130809Smarcel  set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p);
1982130809Smarcel  set_gdbarch_register_to_value (gdbarch,  i386_register_to_value);
1983130809Smarcel  set_gdbarch_value_to_register (gdbarch, i386_value_to_register);
198498948Sobrien
1985130809Smarcel  set_gdbarch_return_value (gdbarch, i386_return_value);
198698948Sobrien
1987130809Smarcel  set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
198898948Sobrien
1989130809Smarcel  /* Stack grows downward.  */
1990130809Smarcel  set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
199198948Sobrien
1992130809Smarcel  set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
1993130809Smarcel  set_gdbarch_decr_pc_after_break (gdbarch, 1);
1994130809Smarcel
1995130809Smarcel  set_gdbarch_frame_args_skip (gdbarch, 8);
1996130809Smarcel  set_gdbarch_pc_in_sigtramp (gdbarch, i386_pc_in_sigtramp);
1997130809Smarcel
1998130809Smarcel  /* Wire in the MMX registers.  */
1999130809Smarcel  set_gdbarch_num_pseudo_regs (gdbarch, i386_num_mmx_regs);
2000130809Smarcel  set_gdbarch_pseudo_register_read (gdbarch, i386_pseudo_register_read);
2001130809Smarcel  set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
2002130809Smarcel
2003130809Smarcel  set_gdbarch_print_insn (gdbarch, i386_print_insn);
2004130809Smarcel
2005130809Smarcel  set_gdbarch_unwind_dummy_id (gdbarch, i386_unwind_dummy_id);
2006130809Smarcel
2007130809Smarcel  set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc);
2008130809Smarcel
2009130809Smarcel  /* Add the i386 register groups.  */
2010130809Smarcel  i386_add_reggroups (gdbarch);
2011130809Smarcel  set_gdbarch_register_reggroup_p (gdbarch, i386_register_reggroup_p);
2012130809Smarcel
2013130809Smarcel  /* Helper for function argument information.  */
2014130809Smarcel  set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
2015130809Smarcel
2016130809Smarcel  /* Hook in the DWARF CFI frame unwinder.  */
2017130809Smarcel  frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
2018130809Smarcel
2019130809Smarcel  frame_base_set_default (gdbarch, &i386_frame_base);
2020130809Smarcel
2021130809Smarcel  /* Hook in ABI-specific overrides, if they have been registered.  */
2022130809Smarcel  gdbarch_init_osabi (info, gdbarch);
2023130809Smarcel
2024130809Smarcel  frame_unwind_append_sniffer (gdbarch, i386_sigtramp_frame_sniffer);
2025130809Smarcel  frame_unwind_append_sniffer (gdbarch, i386_frame_sniffer);
2026130809Smarcel
2027130809Smarcel  /* If we have a register mapping, enable the generic core file
2028130809Smarcel     support, unless it has already been enabled.  */
2029130809Smarcel  if (tdep->gregset_reg_offset
2030130809Smarcel      && !gdbarch_regset_from_core_section_p (gdbarch))
2031130809Smarcel    set_gdbarch_regset_from_core_section (gdbarch,
2032130809Smarcel					  i386_regset_from_core_section);
2033130809Smarcel
2034130809Smarcel  /* Unless support for MMX has been disabled, make %mm0 the first
2035130809Smarcel     pseudo-register.  */
2036130809Smarcel  if (tdep->mm0_regnum == 0)
2037130809Smarcel    tdep->mm0_regnum = gdbarch_num_regs (gdbarch);
2038130809Smarcel
203998948Sobrien  return gdbarch;
204098948Sobrien}
204198948Sobrien
2042130809Smarcelstatic enum gdb_osabi
2043130809Smarceli386_coff_osabi_sniffer (bfd *abfd)
2044130809Smarcel{
2045130809Smarcel  if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
2046130809Smarcel      || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
2047130809Smarcel    return GDB_OSABI_GO32;
2048130809Smarcel
2049130809Smarcel  return GDB_OSABI_UNKNOWN;
2050130809Smarcel}
2051130809Smarcel
2052130809Smarcelstatic enum gdb_osabi
2053130809Smarceli386_nlm_osabi_sniffer (bfd *abfd)
2054130809Smarcel{
2055130809Smarcel  return GDB_OSABI_NETWARE;
2056130809Smarcel}
2057130809Smarcel
2058130809Smarcel
205998948Sobrien/* Provide a prototype to silence -Wmissing-prototypes.  */
206098948Sobrienvoid _initialize_i386_tdep (void);
206198948Sobrien
206219370Spstvoid
206398948Sobrien_initialize_i386_tdep (void)
206419370Spst{
206598948Sobrien  register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
206698948Sobrien
206798948Sobrien  /* Add the variable that controls the disassembly flavor.  */
206898948Sobrien  {
206998948Sobrien    struct cmd_list_element *new_cmd;
207098948Sobrien
207198948Sobrien    new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
207298948Sobrien				valid_flavors,
207398948Sobrien				&disassembly_flavor,
207498948Sobrien				"\
207598948SobrienSet the disassembly flavor, the valid values are \"att\" and \"intel\", \
207646283Sdfrand the default value is \"att\".",
207798948Sobrien				&setlist);
207898948Sobrien    add_show_from_set (new_cmd, &showlist);
207998948Sobrien  }
2080130809Smarcel
2081130809Smarcel  /* Add the variable that controls the convention for returning
2082130809Smarcel     structs.  */
2083130809Smarcel  {
2084130809Smarcel    struct cmd_list_element *new_cmd;
2085130809Smarcel
2086130809Smarcel    new_cmd = add_set_enum_cmd ("struct-convention", no_class,
2087130809Smarcel				valid_conventions,
2088130809Smarcel				&struct_convention, "\
2089130809SmarcelSet the convention for returning small structs, valid values \
2090130809Smarcelare \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
2091130809Smarcel                                &setlist);
2092130809Smarcel    add_show_from_set (new_cmd, &showlist);
2093130809Smarcel  }
2094130809Smarcel
2095130809Smarcel  gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
2096130809Smarcel				  i386_coff_osabi_sniffer);
2097130809Smarcel  gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_nlm_flavour,
2098130809Smarcel				  i386_nlm_osabi_sniffer);
2099130809Smarcel
2100130809Smarcel  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
2101130809Smarcel			  i386_svr4_init_abi);
2102130809Smarcel  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
2103130809Smarcel			  i386_go32_init_abi);
2104130809Smarcel  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETWARE,
2105130809Smarcel			  i386_nw_init_abi);
2106130809Smarcel
2107130809Smarcel  /* Initialize the i386 specific register groups.  */
2108130809Smarcel  i386_init_reggroups ();
210919370Spst}
2110