* solib-svr4.c (svr4_truncate_ptr): New function.
[deliverable/binutils-gdb.git] / gdb / i386-tdep.c
index d1a730cdc821cf1862946477d89e085f91be566a..31ecb76379b0a73350a8bc204d1b1bec64308181 100644 (file)
 #include "regcache.h"
 #include "doublest.h"
 #include "value.h"
-
 #include "gdb_assert.h"
 
-/* i386_register_byte[i] is the offset into the register file of the
+#include "elf-bfd.h"
+
+#include "i386-tdep.h"
+
+#undef XMALLOC
+#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
+
+/* Names of the registers.  The first 10 registers match the register
+   numbering scheme used by GCC for stabs and DWARF.  */
+static char *i386_register_names[] =
+{
+  "eax",   "ecx",    "edx",   "ebx",
+  "esp",   "ebp",    "esi",   "edi",
+  "eip",   "eflags", "cs",    "ss",
+  "ds",    "es",     "fs",    "gs",
+  "st0",   "st1",    "st2",   "st3",
+  "st4",   "st5",    "st6",   "st7",
+  "fctrl", "fstat",  "ftag",  "fiseg",
+  "fioff", "foseg",  "fooff", "fop",
+  "xmm0",  "xmm1",   "xmm2",  "xmm3",
+  "xmm4",  "xmm5",   "xmm6",  "xmm7",
+  "mxcsr"
+};
+
+/* i386_register_offset[i] is the offset into the register file of the
    start of register number i.  We initialize this from
-   i386_register_raw_size.  */
-int i386_register_byte[MAX_NUM_REGS];
+   i386_register_size.  */
+static int i386_register_offset[MAX_NUM_REGS];
 
-/* i386_register_raw_size[i] is the number of bytes of storage in
-   GDB's register array occupied by register i.  */
-int i386_register_raw_size[MAX_NUM_REGS] = {
+/* i386_register_size[i] is the number of bytes of storage in GDB's
+   register array occupied by register i.  */
+static int i386_register_size[MAX_NUM_REGS] = {
    4,  4,  4,  4,
    4,  4,  4,  4,
    4,  4,  4,  4,
@@ -58,9 +81,43 @@ int i386_register_raw_size[MAX_NUM_REGS] = {
    4
 };
 
-/* i386_register_virtual_size[i] is the size in bytes of the virtual
-   type of register i.  */
-int i386_register_virtual_size[MAX_NUM_REGS];
+/* Return the name of register REG.  */
+
+char *
+i386_register_name (int reg)
+{
+  if (reg < 0)
+    return NULL;
+  if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names))
+    return NULL;
+
+  return i386_register_names[reg];
+}
+
+/* Return the offset into the register array of the start of register
+   number REG.  */
+int
+i386_register_byte (int reg)
+{
+  return i386_register_offset[reg];
+}
+
+/* Return the number of bytes of storage in GDB's register array
+   occupied by register REG.  */
+
+int
+i386_register_raw_size (int reg)
+{
+  return i386_register_size[reg];
+}
+
+/* Return the size in bytes of the virtual type of register REG.  */
+
+int
+i386_register_virtual_size (int reg)
+{
+  return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (reg));
+}
 
 /* Convert stabs register number REG to the appropriate register
    number used by GDB.  */
@@ -137,13 +194,6 @@ static const char *valid_flavors[] =
 };
 static const char *disassembly_flavor = att_flavor;
 
-/* This is used to keep the bfd arch_info in sync with the disassembly
-   flavor.  */
-static void set_disassembly_flavor_sfunc (char *, int,
-                                         struct cmd_list_element *);
-static void set_disassembly_flavor (void);
-\f
-
 /* Stdio style buffering was used to minimize calls to ptrace, but
    this buffering did not take into account that the code section
    being accessed may not be an even number of buffers long (even if
@@ -461,6 +511,12 @@ i386_frame_saved_pc (struct frame_info *frame)
   return read_memory_unsigned_integer (frame->frame + 4, 4);
 }
 
+CORE_ADDR
+i386go32_frame_saved_pc (struct frame_info *frame)
+{
+  return read_memory_integer (frame->frame + 4, 4);
+}
+
 /* Immediately after a function call, return the saved pc.  */
 
 CORE_ADDR
@@ -716,18 +772,20 @@ void
 i386_push_dummy_frame (void)
 {
   CORE_ADDR sp = read_register (SP_REGNUM);
+  CORE_ADDR fp;
   int regnum;
   char regbuf[MAX_REGISTER_RAW_SIZE];
 
   sp = push_word (sp, read_register (PC_REGNUM));
   sp = push_word (sp, read_register (FP_REGNUM));
-  write_register (FP_REGNUM, sp);
+  fp = sp;
   for (regnum = 0; regnum < NUM_REGS; regnum++)
     {
       read_register_gen (regnum, regbuf);
       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
     }
   write_register (SP_REGNUM, sp);
+  write_register (FP_REGNUM, fp);
 }
 
 /* Insert the (relative) function address into the call sequence
@@ -869,26 +927,12 @@ i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
          return;
        }
 
-      /* Floating-point return values can be found in %st(0).  */
-      if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
-         && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
-       {
-         /* Copy straight over, but take care of the padding.  */
-         memcpy (valbuf, &regbuf[REGISTER_BYTE (FP0_REGNUM)],
-                 FPU_REG_RAW_SIZE);
-         memset (valbuf + FPU_REG_RAW_SIZE, 0, len - FPU_REG_RAW_SIZE);
-       }
-      else
-       {
-         /* Convert the extended floating-point number found in
-             %st(0) to the desired type.  This is probably not exactly
-             how it would happen on the target itself, but it is the
-             best we can do.  */
-         DOUBLEST val;
-         floatformat_to_doublest (&floatformat_i387_ext,
-                                  &regbuf[REGISTER_BYTE (FP0_REGNUM)], &val);
-         store_floating (valbuf, TYPE_LENGTH (type), val);
-       }
+      /* Floating-point return values can be found in %st(0).  Convert
+        its contents to the desired type.  This is probably not
+        exactly how it would happen on the target itself, but it is
+        the best we can do.  */
+      convert_typed_floating (&regbuf[REGISTER_BYTE (FP0_REGNUM)],
+                             builtin_type_i387_ext, valbuf, type);
     }
   else
     {
@@ -928,6 +972,7 @@ i386_store_return_value (struct type *type, char *valbuf)
   if (TYPE_CODE (type) == TYPE_CODE_FLT)
     {
       unsigned int fstat;
+      char buf[FPU_REG_RAW_SIZE];
 
       if (NUM_FREGS == 0)
        {
@@ -939,27 +984,13 @@ i386_store_return_value (struct type *type, char *valbuf)
          storing the return value in %st(0), we have to simulate the
          state of the FPU at function return point.  */
 
-      if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
-         && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
-       {
-         /* Copy straight over.  */
-         write_register_bytes (REGISTER_BYTE (FP0_REGNUM), valbuf,
-                               FPU_REG_RAW_SIZE);
-       }
-      else
-       {
-         char buf[FPU_REG_RAW_SIZE];
-         DOUBLEST val;
-
-         /* Convert the value found in VALBUF to the extended
-             floating-point format used by the FPU.  This is probably
-             not exactly how it would happen on the target itself, but
-             it is the best we can do.  */
-         val = extract_floating (valbuf, TYPE_LENGTH (type));
-         floatformat_from_doublest (&floatformat_i387_ext, &val, buf);
-         write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
-                               FPU_REG_RAW_SIZE);
-       }
+      /* Convert the value found in VALBUF to the extended
+        floating-point format used by the FPU.  This is probably
+        not exactly how it would happen on the target itself, but
+        it is the best we can do.  */
+      convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
+      write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
+                           FPU_REG_RAW_SIZE);
 
       /* Set the top of the floating-point register stack to 7.  The
          actual value doesn't really matter, but 7 is what a normal
@@ -1017,7 +1048,7 @@ i386_register_virtual_type (int regnum)
     return lookup_pointer_type (builtin_type_void);
 
   if (IS_FP_REGNUM (regnum))
-    return builtin_type_long_double;
+    return builtin_type_i387_ext;
 
   if (IS_SSE_REGNUM (regnum))
     return builtin_type_v4sf;
@@ -1044,8 +1075,7 @@ void
 i386_register_convert_to_virtual (int regnum, struct type *type,
                                  char *from, char *to)
 {
-  char buf[12];
-  DOUBLEST d;
+  gdb_assert (IS_FP_REGNUM (regnum));
 
   /* We only support floating-point values.  */
   if (TYPE_CODE (type) != TYPE_CODE_FLT)
@@ -1056,14 +1086,9 @@ i386_register_convert_to_virtual (int regnum, struct type *type,
       return;
     }
 
-  /* First add the necessary padding.  */
-  memcpy (buf, from, FPU_REG_RAW_SIZE);
-  memset (buf + FPU_REG_RAW_SIZE, 0, sizeof buf - FPU_REG_RAW_SIZE);
-
-  /* Convert to TYPE.  This should be a no-op, if TYPE is equivalent
-     to the extended floating-point format used by the FPU.  */
-  d = extract_floating (buf, sizeof buf);
-  store_floating (to, TYPE_LENGTH (type), d);
+  /* Convert to TYPE.  This should be a no-op if TYPE is equivalent to
+     the extended floating-point format used by the FPU.  */
+  convert_typed_floating (from, builtin_type_i387_ext, to, type);
 }
 
 /* Convert data from virtual format with type TYPE in buffer FROM to
@@ -1073,11 +1098,20 @@ void
 i386_register_convert_to_raw (struct type *type, int regnum,
                              char *from, char *to)
 {
-  gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT
-             && TYPE_LENGTH (type) == 12);
+  gdb_assert (IS_FP_REGNUM (regnum));
 
-  /* Simply omit the two unused bytes.  */
-  memcpy (to, from, FPU_REG_RAW_SIZE);
+  /* We only support floating-point values.  */
+  if (TYPE_CODE (type) != TYPE_CODE_FLT)
+    {
+      warning ("Cannot convert non-floating-point type "
+              "to floating-point register value.");
+      memset (to, 0, TYPE_LENGTH (type));
+      return;
+    }
+
+  /* Convert from TYPE.  This should be a no-op if TYPE is equivalent
+     to the extended floating-point format used by the FPU.  */
+  convert_typed_floating (from, type, to, builtin_type_i387_ext);
 }
 \f     
 
@@ -1173,27 +1207,132 @@ gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
   internal_error (__FILE__, __LINE__, "failed internal consistency check");
 }
 
-/* If the disassembly mode is intel, we have to also switch the bfd
-   mach_type.  This function is run in the set disassembly_flavor
-   command, and does that.  */
-
+\f
 static void
-set_disassembly_flavor_sfunc (char *args, int from_tty,
-                             struct cmd_list_element *c)
+process_note_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
 {
-  set_disassembly_flavor ();
+  int *os_ident_ptr = obj;
+  const char *name;
+  unsigned int sect_size;
+
+  name = bfd_get_section_name (abfd, sect);
+  sect_size = bfd_section_size (abfd, sect);
+  if (strcmp (name, ".note.ABI-tag") == 0 && sect_size > 0)
+    {
+      unsigned int name_length, data_length, note_type;
+      char *note = alloca (sect_size);
+
+      bfd_get_section_contents (abfd, sect, note,
+                                (file_ptr) 0, (bfd_size_type) sect_size);
+
+      name_length = bfd_h_get_32 (abfd, note);
+      data_length = bfd_h_get_32 (abfd, note + 4);
+      note_type = bfd_h_get_32 (abfd, note + 8);
+
+      if (name_length == 4 && data_length == 16 && note_type == 1
+          && strcmp (note + 12, "GNU") == 0)
+        {
+          int os_number = bfd_h_get_32 (abfd, note + 16);
+
+          /* The case numbers are from abi-tags in glibc.  */
+          switch (os_number)
+            {
+            case 0:
+              *os_ident_ptr = ELFOSABI_LINUX;
+              break;
+            case 1:
+              *os_ident_ptr = ELFOSABI_HURD;
+              break;
+            case 2:
+              *os_ident_ptr = ELFOSABI_SOLARIS;
+              break;
+            default:
+              internal_error (__FILE__, __LINE__,
+                              "process_note_abi_sections: "
+                              "unknown OS number %d", os_number);
+              break;
+            }
+        }
+    }
 }
 
-static void
-set_disassembly_flavor (void)
+struct gdbarch *
+i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 {
-  if (disassembly_flavor == att_flavor)
-    set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
-  else if (disassembly_flavor == intel_flavor)
-    set_architecture_from_arch_mach (bfd_arch_i386,
-                                    bfd_mach_i386_i386_intel_syntax);
+  struct gdbarch_tdep *tdep;
+  struct gdbarch *gdbarch;
+  int os_ident;
+
+  if (info.abfd != NULL
+      && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
+    {
+      os_ident = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
+
+      /* If os_ident is 0, it is not necessarily the case that we're
+         on a SYSV system.  (ELFOSABI_NONE is defined to be 0.)
+         GNU/Linux uses a note section to record OS/ABI info, but
+         leaves e_ident[EI_OSABI] zero.  So we have to check for note
+         sections too.  */
+      if (os_ident == ELFOSABI_NONE)
+       bfd_map_over_sections (info.abfd,
+                              process_note_abi_tag_sections,
+                              &os_ident);
+         
+      /* If that didn't help us, revert to some non-standard checks.  */
+      if (os_ident == ELFOSABI_NONE)
+       {
+         /* FreeBSD folks are naughty; they stored the string
+            "FreeBSD" in the padding of the e_ident field of the ELF
+            header.  */
+         if (strcmp (&elf_elfheader (info.abfd)->e_ident[8], "FreeBSD") == 0)
+           os_ident = ELFOSABI_FREEBSD;
+        }
+    }
+  else
+    os_ident = -1;
+
+  for (arches = gdbarch_list_lookup_by_info (arches, &info);
+       arches != NULL;
+       arches = gdbarch_list_lookup_by_info (arches->next, &info))
+    {
+      if (gdbarch_tdep (current_gdbarch)->os_ident != os_ident)
+        continue;
+      return arches->gdbarch;
+    }
+
+  /* Allocate space for the new architecture.  */
+  tdep = XMALLOC (struct gdbarch_tdep);
+  gdbarch = gdbarch_alloc (&info, tdep);
+
+  tdep->os_ident = os_ident;
+
+  /* FIXME: kettenis/2001-11-24: Although not all IA-32 processors
+     have the SSE registers, it's easier to set the default to 8.  */
+  tdep->num_xmm_regs = 8;
+
+  set_gdbarch_use_generic_dummy_frames (gdbarch, 0);
+
+  /* Call dummy code.  */
+  set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
+  set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 5);
+  set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
+  set_gdbarch_call_dummy_p (gdbarch, 1);
+  set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
+
+  set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
+  set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
+
+  set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_on_stack);
+
+  /* NOTE: tm-i386nw.h and tm-i386v4.h override this.  */
+  set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
+
+  /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-linux.h,
+     tm-ptx.h, tm-symmetry.h currently override this.  Sigh.  */
+  set_gdbarch_num_regs (gdbarch, NUM_GREGS + NUM_FREGS + NUM_SSE_REGS);
+
+  return gdbarch;
 }
-\f
 
 /* Provide a prototype to silence -Wmissing-prototypes.  */
 void _initialize_i386_tdep (void);
@@ -1201,6 +1340,8 @@ void _initialize_i386_tdep (void);
 void
 _initialize_i386_tdep (void)
 {
+  register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
+
   /* Initialize the table saying where each register starts in the
      register file.  */
   {
@@ -1209,19 +1350,11 @@ _initialize_i386_tdep (void)
     offset = 0;
     for (i = 0; i < MAX_NUM_REGS; i++)
       {
-       i386_register_byte[i] = offset;
-       offset += i386_register_raw_size[i];
+       i386_register_offset[i] = offset;
+       offset += i386_register_size[i];
       }
   }
 
-  /* Initialize the table of virtual register sizes.  */
-  {
-    int i;
-
-    for (i = 0; i < MAX_NUM_REGS; i++)
-      i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
-  }
-
   tm_print_insn = gdb_print_insn_i386;
   tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
 
@@ -1236,11 +1369,6 @@ _initialize_i386_tdep (void)
 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
 and the default value is \"att\".",
                                &setlist);
-    new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
     add_show_from_set (new_cmd, &showlist);
   }
-
-  /* Finally, initialize the disassembly flavor to the default given
-     in the disassembly_flavor variable.  */
-  set_disassembly_flavor ();
 }
This page took 0.029597 seconds and 4 git commands to generate.