Resolve more problems with readelf uncovered by fuzzing binary files.
[deliverable/binutils-gdb.git] / gdb / m32c-tdep.c
index 8e7c7e94fe7a0e78b501461d6874ca6ee69b1a62..ab123c8563065ad7e4bdc00dd4b210379e0b7e6d 100644 (file)
@@ -1,7 +1,6 @@
 /* Renesas M32C target-dependent code for GDB, the GNU debugger.
 
-   Copyright 2004, 2005, 2007, 2008, 2009, 2010, 2011
-   Free Software Foundation, Inc.
+   Copyright (C) 2004-2014 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
-
-#include <stdarg.h>
-
-#if defined (HAVE_STRING_H)
-#include <string.h>
-#endif
-
-#include "gdb_assert.h"
 #include "elf-bfd.h"
 #include "elf/m32c.h"
 #include "gdb/sim-m32c.h"
@@ -44,6 +35,7 @@
 #include "reggroups.h"
 #include "prologue-value.h"
 #include "target.h"
+#include "objfiles.h"
 
 \f
 /* The m32c tdep structure.  */
@@ -54,9 +46,9 @@ struct m32c_reg;
 
 /* The type of a function that moves the value of REG between CACHE or
    BUF --- in either direction.  */
-typedef void (m32c_move_reg_t) (struct m32c_reg *reg,
-                               struct regcache *cache,
-                               void *buf);
+typedef enum register_status (m32c_move_reg_t) (struct m32c_reg *reg,
+                                               struct regcache *cache,
+                                               void *buf);
 
 struct m32c_reg
 {
@@ -201,16 +193,16 @@ make_types (struct gdbarch *arch)
   TYPE_UNSIGNED (tdep->ptr_voyd) = 1;
   tdep->func_voyd = lookup_function_type (tdep->voyd);
 
-  sprintf (type_name, "%s_data_addr_t",
-          gdbarch_bfd_arch_info (arch)->printable_name);
+  xsnprintf (type_name, sizeof (type_name), "%s_data_addr_t",
+            gdbarch_bfd_arch_info (arch)->printable_name);
   tdep->data_addr_reg_type
     = arch_type (arch, TYPE_CODE_PTR, data_addr_reg_bits / TARGET_CHAR_BIT,
                  xstrdup (type_name));
   TYPE_TARGET_TYPE (tdep->data_addr_reg_type) = tdep->voyd;
   TYPE_UNSIGNED (tdep->data_addr_reg_type) = 1;
 
-  sprintf (type_name, "%s_code_addr_t",
-          gdbarch_bfd_arch_info (arch)->printable_name);
+  xsnprintf (type_name, sizeof (type_name), "%s_code_addr_t",
+            gdbarch_bfd_arch_info (arch)->printable_name);
   tdep->code_addr_reg_type
     = arch_type (arch, TYPE_CODE_PTR, code_addr_reg_bits / TARGET_CHAR_BIT,
                  xstrdup (type_name));
@@ -315,18 +307,20 @@ static m32c_move_reg_t m32c_r3r2r1r0_read, m32c_r3r2r1r0_write;
 
 
 /* Copy the value of the raw register REG from CACHE to BUF.  */
-static void
+static enum register_status
 m32c_raw_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
-  regcache_raw_read (cache, reg->num, buf);
+  return regcache_raw_read (cache, reg->num, buf);
 }
 
 
 /* Copy the value of the raw register REG from BUF to CACHE.  */
-static void
+static enum register_status
 m32c_raw_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   regcache_raw_write (cache, reg->num, (const void *) buf);
+
+  return REG_VALID;
 }
 
 
@@ -353,11 +347,11 @@ m32c_banked_register (struct m32c_reg *reg, struct regcache *cache)
    If the value of the 'flg' register in CACHE has any of the bits
    masked in REG->n set, then read REG->ry.  Otherwise, read
    REG->rx.  */
-static void
+static enum register_status
 m32c_banked_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
-  regcache_raw_read (cache, bank_reg->num, buf);
+  return regcache_raw_read (cache, bank_reg->num, buf);
 }
 
 
@@ -365,35 +359,39 @@ m32c_banked_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
    If the value of the 'flg' register in CACHE has any of the bits
    masked in REG->n set, then write REG->ry.  Otherwise, write
    REG->rx.  */
-static void
+static enum register_status
 m32c_banked_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
   regcache_raw_write (cache, bank_reg->num, (const void *) buf);
+
+  return REG_VALID;
 }
 
 
 /* Move the value of SB from CACHE to BUF.  On bfd_mach_m32c, SB is a
    banked register; on bfd_mach_m16c, it's not.  */
-static void
+static enum register_status
 m32c_sb_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
-    m32c_raw_read (reg->rx, cache, buf);
+    return m32c_raw_read (reg->rx, cache, buf);
   else
-    m32c_banked_read (reg, cache, buf);
+    return m32c_banked_read (reg, cache, buf);
 }
 
 
 /* Move the value of SB from BUF to CACHE.  On bfd_mach_m32c, SB is a
    banked register; on bfd_mach_m16c, it's not.  */
-static void
+static enum register_status
 m32c_sb_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
     m32c_raw_write (reg->rx, cache, buf);
   else
     m32c_banked_write (reg, cache, buf);
+
+  return REG_VALID;
 }
 
 
@@ -438,13 +436,14 @@ m32c_find_part (struct m32c_reg *reg, int *offset_p, int *len_p)
    to BUF.  Treating the value of the register REG->rx as an array of
    REG->type values, where higher indices refer to more significant
    bits, read the value of the REG->n'th element.  */
-static void
+static enum register_status
 m32c_part_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   int offset, len;
+
   memset (buf, 0, TYPE_LENGTH (reg->type));
   m32c_find_part (reg, &offset, &len);
-  regcache_cooked_read_part (cache, reg->rx->num, offset, len, buf);
+  return regcache_cooked_read_part (cache, reg->rx->num, offset, len, buf);
 }
 
 
@@ -452,45 +451,53 @@ m32c_part_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
    Treating the value of the register REG->rx as an array of REG->type
    values, where higher indices refer to more significant bits, write
    the value of the REG->n'th element.  */
-static void
+static enum register_status
 m32c_part_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   int offset, len;
+
   m32c_find_part (reg, &offset, &len);
   regcache_cooked_write_part (cache, reg->rx->num, offset, len, buf);
+
+  return REG_VALID;
 }
 
 
 /* Move the value of REG from CACHE to BUF.  REG's value is the
    concatenation of the values of the registers REG->rx and REG->ry,
    with REG->rx contributing the more significant bits.  */
-static void
+static enum register_status
 m32c_cat_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   int high_bytes = TYPE_LENGTH (reg->rx->type);
   int low_bytes  = TYPE_LENGTH (reg->ry->type);
   /* For address arithmetic.  */
   unsigned char *cbuf = buf;
+  enum register_status status;
 
   gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
 
   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
     {
-      regcache_cooked_read (cache, reg->rx->num, cbuf);
-      regcache_cooked_read (cache, reg->ry->num, cbuf + high_bytes);
+      status = regcache_cooked_read (cache, reg->rx->num, cbuf);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, reg->ry->num, cbuf + high_bytes);
     }
   else
     {
-      regcache_cooked_read (cache, reg->rx->num, cbuf + low_bytes);
-      regcache_cooked_read (cache, reg->ry->num, cbuf);
+      status = regcache_cooked_read (cache, reg->rx->num, cbuf + low_bytes);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, reg->ry->num, cbuf);
     }
+
+  return status;
 }
 
 
 /* Move the value of REG from CACHE to BUF.  REG's value is the
    concatenation of the values of the registers REG->rx and REG->ry,
    with REG->rx contributing the more significant bits.  */
-static void
+static enum register_status
 m32c_cat_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   int high_bytes = TYPE_LENGTH (reg->rx->type);
@@ -510,42 +517,53 @@ m32c_cat_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
       regcache_cooked_write (cache, reg->rx->num, cbuf + low_bytes);
       regcache_cooked_write (cache, reg->ry->num, cbuf);
     }
+
+  return REG_VALID;
 }
 
 
 /* Copy the value of the raw register REG from CACHE to BUF.  REG is
    the concatenation (from most significant to least) of r3, r2, r1,
    and r0.  */
-static void
+static enum register_status
 m32c_r3r2r1r0_read (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
   int len = TYPE_LENGTH (tdep->r0->type);
+  enum register_status status;
 
   /* For address arithmetic.  */
   unsigned char *cbuf = buf;
 
   if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
     {
-      regcache_cooked_read (cache, tdep->r0->num, cbuf + len * 3);
-      regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 2);
-      regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 1);
-      regcache_cooked_read (cache, tdep->r3->num, cbuf);
+      status = regcache_cooked_read (cache, tdep->r0->num, cbuf + len * 3);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 2);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 1);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, tdep->r3->num, cbuf);
     }
   else
     {
-      regcache_cooked_read (cache, tdep->r0->num, cbuf);
-      regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 1);
-      regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 2);
-      regcache_cooked_read (cache, tdep->r3->num, cbuf + len * 3);
+      status = regcache_cooked_read (cache, tdep->r0->num, cbuf);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, tdep->r1->num, cbuf + len * 1);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, tdep->r2->num, cbuf + len * 2);
+      if (status == REG_VALID)
+       status = regcache_cooked_read (cache, tdep->r3->num, cbuf + len * 3);
     }
+
+  return status;
 }
 
 
 /* Copy the value of the raw register REG from BUF to CACHE.  REG is
    the concatenation (from most significant to least) of r3, r2, r1,
    and r0.  */
-static void
+static enum register_status
 m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
 {
   struct gdbarch_tdep *tdep = gdbarch_tdep (reg->arch);
@@ -568,10 +586,12 @@ m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache, void *buf)
       regcache_cooked_write (cache, tdep->r2->num, cbuf + len * 2);
       regcache_cooked_write (cache, tdep->r3->num, cbuf + len * 3);
     }
+
+  return REG_VALID;
 }
 
 
-static void
+static enum register_status
 m32c_pseudo_register_read (struct gdbarch *arch,
                           struct regcache *cache,
                           int cookednum,
@@ -585,7 +605,7 @@ m32c_pseudo_register_read (struct gdbarch *arch,
   gdb_assert (arch == tdep->regs[cookednum].arch);
   reg = &tdep->regs[cookednum];
 
-  reg->read (reg, cache, buf);
+  return reg->read (reg, cache, buf);
 }
 
 
@@ -1807,7 +1827,7 @@ m32c_analyze_prologue (struct gdbarch *arch,
 static CORE_ADDR
 m32c_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR ip)
 {
-  char *name;
+  const char *name;
   CORE_ADDR func_addr, func_end, sal_end;
   struct m32c_prologue p;
 
@@ -1932,6 +1952,7 @@ m32c_prev_register (struct frame_info *this_frame,
 
 static const struct frame_unwind m32c_unwind = {
   NORMAL_FRAME,
+  default_frame_unwind_stop_reason,
   m32c_this_id,
   m32c_prev_register,
   NULL,
@@ -2178,7 +2199,7 @@ m32c_return_by_passed_buf (struct type *type)
 
 static enum return_value_convention
 m32c_return_value (struct gdbarch *gdbarch,
-                  struct type *func_type,
+                  struct value *function,
                   struct type *valtype,
                   struct regcache *regcache,
                   gdb_byte *readbuf,
@@ -2215,14 +2236,14 @@ m32c_return_value (struct gdbarch *gdbarch,
          /* Everything else is passed in mem0, using as many bytes as
             needed.  This is not what the Renesas tools do, but it's
             what GCC does at the moment.  */
-         struct minimal_symbol *mem0
+         struct bound_minimal_symbol mem0
            = lookup_minimal_symbol ("mem0", NULL, NULL);
 
-         if (! mem0)
-           error ("The return value is stored in memory at 'mem0', "
-                  "but GDB cannot find\n"
-                  "its address.");
-         read_memory (SYMBOL_VALUE_ADDRESS (mem0), readbuf, valtype_len);
+         if (! mem0.minsym)
+           error (_("The return value is stored in memory at 'mem0', "
+                    "but GDB cannot find\n"
+                    "its address."));
+         read_memory (BMSYMBOL_VALUE_ADDRESS (mem0), readbuf, valtype_len);
        }
     }
 
@@ -2247,15 +2268,14 @@ m32c_return_value (struct gdbarch *gdbarch,
          /* Everything else is passed in mem0, using as many bytes as
             needed.  This is not what the Renesas tools do, but it's
             what GCC does at the moment.  */
-         struct minimal_symbol *mem0
+         struct bound_minimal_symbol mem0
            = lookup_minimal_symbol ("mem0", NULL, NULL);
 
-         if (! mem0)
-           error ("The return value is stored in memory at 'mem0', "
-                  "but GDB cannot find\n"
-                  " its address.");
-         write_memory (SYMBOL_VALUE_ADDRESS (mem0),
-                        (char *) writebuf, valtype_len);
+         if (! mem0.minsym)
+           error (_("The return value is stored in memory at 'mem0', "
+                    "but GDB cannot find\n"
+                    " its address."));
+         write_memory (BMSYMBOL_VALUE_ADDRESS (mem0), writebuf, valtype_len);
        }
     }
 
@@ -2325,7 +2345,7 @@ m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
      someone loaded a new executable, and I'm not quite sure of the
      best way to do that.  find_pc_partial_function does do some
      caching, so we'll see how this goes.  */
-  char *name;
+  const char *name;
   CORE_ADDR start, end;
 
   if (find_pc_partial_function (stop_pc, &name, &start, &end))
@@ -2345,8 +2365,8 @@ m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
 
          /* What we have now is the address of a jump instruction.
             What we need is the destination of that jump.
-            The opcode is 1 byte, and the destination is the next 3 bytes.
-         */
+            The opcode is 1 byte, and the destination is the next 3 bytes.  */
+
          target = read_memory_unsigned_integer (target + 1, 3, byte_order);
          return target;
        }
@@ -2424,19 +2444,20 @@ m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
 
   if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
     {
-      char *func_name;
+      const char *func_name;
       char *tramp_name;
-      struct minimal_symbol *tramp_msym;
+      struct bound_minimal_symbol tramp_msym;
 
       /* Try to find a linker symbol at this address.  */
-      struct minimal_symbol *func_msym = lookup_minimal_symbol_by_pc (addr);
+      struct bound_minimal_symbol func_msym
+       = lookup_minimal_symbol_by_pc (addr);
 
-      if (! func_msym)
+      if (! func_msym.minsym)
         error (_("Cannot convert code address %s to function pointer:\n"
                "couldn't find a symbol at that address, to find trampoline."),
                paddress (gdbarch, addr));
 
-      func_name = SYMBOL_LINKAGE_NAME (func_msym);
+      func_name = MSYMBOL_LINKAGE_NAME (func_msym.minsym);
       tramp_name = xmalloc (strlen (func_name) + 5);
       strcpy (tramp_name, func_name);
       strcat (tramp_name, ".plt");
@@ -2448,7 +2469,7 @@ m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
          the name any more.  */
       xfree (tramp_name);
 
-      if (! tramp_msym)
+      if (! tramp_msym.minsym)
        {
          CORE_ADDR ptrval;
 
@@ -2480,7 +2501,7 @@ m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
       else
        {
          /* The trampoline's address is our pointer.  */
-         addr = SYMBOL_VALUE_ADDRESS (tramp_msym);
+         addr = BMSYMBOL_VALUE_ADDRESS (tramp_msym);
        }
     }
 
@@ -2507,17 +2528,17 @@ m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
     {
       /* See if there is a minimal symbol at that address whose name is
          "NAME.plt".  */
-      struct minimal_symbol *ptr_msym = lookup_minimal_symbol_by_pc (ptr);
+      struct bound_minimal_symbol ptr_msym = lookup_minimal_symbol_by_pc (ptr);
 
-      if (ptr_msym)
+      if (ptr_msym.minsym)
         {
-          char *ptr_msym_name = SYMBOL_LINKAGE_NAME (ptr_msym);
+          const char *ptr_msym_name = MSYMBOL_LINKAGE_NAME (ptr_msym.minsym);
           int len = strlen (ptr_msym_name);
 
           if (len > 4
               && strcmp (ptr_msym_name + len - 4, ".plt") == 0)
             {
-             struct minimal_symbol *func_msym;
+             struct bound_minimal_symbol func_msym;
               /* We have a .plt symbol; try to find the symbol for the
                  corresponding function.
 
@@ -2532,8 +2553,8 @@ m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
 
               /* If we do have such a symbol, return its value as the
                  function's true address.  */
-              if (func_msym)
-                ptr = SYMBOL_VALUE_ADDRESS (func_msym);
+              if (func_msym.minsym)
+                ptr = BMSYMBOL_VALUE_ADDRESS (func_msym);
             }
         }
       else
@@ -2544,7 +2565,7 @@ m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
            {
              ptr_msym = lookup_minimal_symbol_by_pc ((aspace << 16) | ptr);
              
-             if (ptr_msym)
+             if (ptr_msym.minsym)
                ptr |= aspace << 16;
            }
        }
@@ -2558,15 +2579,16 @@ m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
                            int *frame_regnum,
                            LONGEST *frame_offset)
 {
-  char *name;
-  CORE_ADDR func_addr, func_end, sal_end;
+  const char *name;
+  CORE_ADDR func_addr, func_end;
   struct m32c_prologue p;
 
   struct regcache *regcache = get_current_regcache ();
   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
   
   if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
-    internal_error (__FILE__, __LINE__, _("No virtual frame pointer available"));
+    internal_error (__FILE__, __LINE__,
+                   _("No virtual frame pointer available"));
 
   m32c_analyze_prologue (gdbarch, func_addr, pc, &p);
   switch (p.kind)
@@ -2586,7 +2608,8 @@ m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
     }
   /* Sanity check */
   if (*frame_regnum > gdbarch_num_regs (gdbarch))
-    internal_error (__FILE__, __LINE__, _("No virtual frame pointer available"));
+    internal_error (__FILE__, __LINE__,
+                   _("No virtual frame pointer available"));
 }
 
 \f
@@ -2638,8 +2661,7 @@ m32c_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
      They may be in the dwarf2 cfi code in GDB, or they may be in
      the debug info emitted by the upstream toolchain.  I don't 
      know which, but I do know that the prologue analyzer works better.
-     MVS 04/13/06
-  */
+     MVS 04/13/06  */
   dwarf2_append_sniffers (arch);
 #endif
   frame_unwind_append_unwinder (arch, &m32c_unwind);
@@ -2657,7 +2679,7 @@ m32c_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   /* m32c function boundary addresses are not necessarily even.
      Therefore, the `vbit', which indicates a pointer to a virtual
      member function, is stored in the delta field, rather than as
-     the low bit of a function pointer address.  
+     the low bit of a function pointer address.
 
      In order to verify this, see the definition of
      TARGET_PTRMEMFUNC_VBIT_LOCATION in gcc/defaults.h along with the
This page took 0.033581 seconds and 4 git commands to generate.