Avoid "Invalid parameter passed to C runtime function" warning
[deliverable/binutils-gdb.git] / gdb / riscv-tdep.c
index 4b5f38a8779838250b8b353b0d5ac9a36efa9cf8..408ab0af24b73a3ca9c1470b722be371b08b6bcf 100644 (file)
@@ -59,6 +59,9 @@
 /* The stack must be 16-byte aligned.  */
 #define SP_ALIGNMENT 16
 
+/* The biggest alignment that the target supports.  */
+#define BIGGEST_ALIGNMENT 16
+
 /* Forward declarations.  */
 static bool riscv_has_feature (struct gdbarch *gdbarch, char feature);
 
@@ -415,18 +418,34 @@ riscv_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
 {
   if (use_compressed_breakpoints == AUTO_BOOLEAN_AUTO)
     {
+      bool unaligned_p = false;
       gdb_byte buf[1];
 
-      /* Read the opcode byte to determine the instruction length.  */
-      read_code (*pcptr, buf, 1);
+      /* Some targets don't support unaligned reads.  The address can only
+        be unaligned if the C extension is supported.  So it is safe to
+        use a compressed breakpoint in this case.  */
+      if (*pcptr & 0x2)
+       unaligned_p = true;
+      else
+       {
+         /* Read the opcode byte to determine the instruction length.  */
+         read_code (*pcptr, buf, 1);
+       }
 
       if (riscv_debug_breakpoints)
-       fprintf_unfiltered
-         (gdb_stdlog,
-          "Using %s for breakpoint at %s (instruction length %d)\n",
-          riscv_insn_length (buf[0]) == 2 ? "C.EBREAK" : "EBREAK",
-          paddress (gdbarch, *pcptr), riscv_insn_length (buf[0]));
-      if (riscv_insn_length (buf[0]) == 2)
+       {
+         const char *bp = (unaligned_p || riscv_insn_length (buf[0]) == 2
+                           ? "C.EBREAK" : "EBREAK");
+
+         fprintf_unfiltered (gdb_stdlog, "Using %s for breakpoint at %s ",
+                             bp, paddress (gdbarch, *pcptr));
+         if (unaligned_p)
+           fprintf_unfiltered (gdb_stdlog, "(unaligned address)\n");
+         else
+           fprintf_unfiltered (gdb_stdlog, "(instruction length %d)\n",
+                               riscv_insn_length (buf[0]));
+       }
+      if (unaligned_p || riscv_insn_length (buf[0]) == 2)
        return 2;
       else
        return 4;
@@ -1240,7 +1259,9 @@ riscv_insn::fetch_instruction (struct gdbarch *gdbarch,
   return extract_unsigned_integer (buf, instlen, byte_order);
 }
 
-/* Fetch from target memory an instruction at PC and decode it.  */
+/* Fetch from target memory an instruction at PC and decode it.  This can
+   throw an error if the memory access fails, callers are responsible for
+   handling this error if that is appropriate.  */
 
 void
 riscv_insn::decode (struct gdbarch *gdbarch, CORE_ADDR pc)
@@ -1624,6 +1645,10 @@ riscv_type_alignment (struct type *t)
       return TYPE_LENGTH (t);
 
     case TYPE_CODE_ARRAY:
+      if (TYPE_VECTOR (t))
+       return std::min (TYPE_LENGTH (t), (unsigned) BIGGEST_ALIGNMENT);
+      /* FALLTHROUGH */
+
     case TYPE_CODE_COMPLEX:
       return riscv_type_alignment (TYPE_TARGET_TYPE (t));
 
@@ -1714,6 +1739,9 @@ struct riscv_arg_info
        then this offset will be set to 0.  */
     int c_offset;
   } argloc[2];
+
+  /* TRUE if this is an unnamed argument.  */
+  bool is_unnamed;
 };
 
 /* Information about a set of registers being used for passing arguments as
@@ -1907,12 +1935,19 @@ riscv_call_arg_scalar_int (struct riscv_arg_info *ainfo,
     }
   else
     {
-      int len = (ainfo->length > cinfo->xlen) ? cinfo->xlen : ainfo->length;
+      int len = std::min (ainfo->length, cinfo->xlen);
+      int align = std::max (ainfo->align, cinfo->xlen);
+
+      /* Unnamed arguments in registers that require 2*XLEN alignment are
+        passed in an aligned register pair.  */
+      if (ainfo->is_unnamed && (align == cinfo->xlen * 2)
+         && cinfo->int_regs.next_regnum & 1)
+       cinfo->int_regs.next_regnum++;
 
       if (!riscv_assign_reg_location (&ainfo->argloc[0],
                                      &cinfo->int_regs, len, 0))
        riscv_assign_stack_location (&ainfo->argloc[0],
-                                    &cinfo->memory, len, ainfo->align);
+                                    &cinfo->memory, len, align);
 
       if (len < ainfo->length)
        {
@@ -2189,7 +2224,9 @@ riscv_call_arg_struct (struct riscv_arg_info *ainfo,
    selected from CINFO which holds information about what call argument
    locations are available for use next.  The TYPE is the type of the
    argument being passed, this information is recorded into AINFO (along
-   with some additional information derived from the type).
+   with some additional information derived from the type).  IS_UNNAMED
+   is true if this is an unnamed (stdarg) argument, this info is also
+   recorded into AINFO.
 
    After assigning a location to AINFO, CINFO will have been updated.  */
 
@@ -2197,11 +2234,12 @@ static void
 riscv_arg_location (struct gdbarch *gdbarch,
                    struct riscv_arg_info *ainfo,
                    struct riscv_call_info *cinfo,
-                   struct type *type)
+                   struct type *type, bool is_unnamed)
 {
   ainfo->type = type;
   ainfo->length = TYPE_LENGTH (ainfo->type);
   ainfo->align = riscv_type_alignment (ainfo->type);
+  ainfo->is_unnamed = is_unnamed;
   ainfo->contents = nullptr;
 
   switch (TYPE_CODE (ainfo->type))
@@ -2336,7 +2374,7 @@ riscv_push_dummy_call (struct gdbarch *gdbarch,
                       int nargs,
                       struct value **args,
                       CORE_ADDR sp,
-                      int struct_return,
+                      function_call_return_method return_method,
                       CORE_ADDR struct_addr)
 {
   int i;
@@ -2350,8 +2388,13 @@ riscv_push_dummy_call (struct gdbarch *gdbarch,
 
   CORE_ADDR osp = sp;
 
+  struct type *ftype = check_typedef (value_type (function));
+
+  if (TYPE_CODE (ftype) == TYPE_CODE_PTR)
+    ftype = check_typedef (TYPE_TARGET_TYPE (ftype));
+
   /* We'll use register $a0 if we're returning a struct.  */
-  if (struct_return)
+  if (return_method == return_method_struct)
     ++call_info.int_regs.next_regnum;
 
   for (i = 0; i < nargs; ++i)
@@ -2363,7 +2406,8 @@ riscv_push_dummy_call (struct gdbarch *gdbarch,
       arg_value = args[i];
       arg_type = check_typedef (value_type (arg_value));
 
-      riscv_arg_location (gdbarch, info, &call_info, arg_type);
+      riscv_arg_location (gdbarch, info, &call_info, arg_type,
+                         TYPE_VARARGS (ftype) && i >= TYPE_NFIELDS (ftype));
 
       if (info->type != arg_type)
        arg_value = value_cast (info->type, arg_value);
@@ -2381,7 +2425,7 @@ riscv_push_dummy_call (struct gdbarch *gdbarch,
               (riscv_has_fp_abi (gdbarch) ? "is" : "is not"));
       fprintf_unfiltered (gdb_stdlog, ": xlen: %d\n: flen: %d\n",
               call_info.xlen, call_info.flen);
-      if (struct_return)
+      if (return_method == return_method_struct)
        fprintf_unfiltered (gdb_stdlog,
                            "[*] struct return pointer in register $A0\n");
       for (i = 0; i < nargs; ++i)
@@ -2408,7 +2452,7 @@ riscv_push_dummy_call (struct gdbarch *gdbarch,
 
   /* Now load the argument into registers, or onto the stack.  */
 
-  if (struct_return)
+  if (return_method == return_method_struct)
     {
       gdb_byte buf[sizeof (LONGEST)];
 
@@ -2540,7 +2584,7 @@ riscv_return_value (struct gdbarch  *gdbarch,
   struct type *arg_type;
 
   arg_type = check_typedef (type);
-  riscv_arg_location (gdbarch, &info, &call_info, arg_type);
+  riscv_arg_location (gdbarch, &info, &call_info, arg_type, false);
 
   if (riscv_debug_infcall > 0)
     {
@@ -2736,8 +2780,17 @@ riscv_frame_this_id (struct frame_info *this_frame,
 {
   struct riscv_unwind_cache *cache;
 
-  cache = riscv_frame_cache (this_frame, prologue_cache);
-  *this_id = cache->this_id;
+  TRY
+    {
+      cache = riscv_frame_cache (this_frame, prologue_cache);
+      *this_id = cache->this_id;
+    }
+  CATCH (ex, RETURN_MASK_ERROR)
+    {
+      /* Ignore errors, this leaves the frame id as the predefined outer
+         frame id which terminates the backtrace at this point.  */
+    }
+  END_CATCH
 }
 
 /* Implement the prev_register callback for RiscV frame unwinder.  */
This page took 0.033316 seconds and 4 git commands to generate.