[gdbarch] New method "execute_dwarf_cfa_vendor_op" and migrate SPARC to it
authorJiong Wang <jiong.wang@arm.com>
Wed, 26 Apr 2017 13:05:03 +0000 (14:05 +0100)
committerJiong Wang <jiong.wang@arm.com>
Wed, 26 Apr 2017 13:05:03 +0000 (14:05 +0100)
Recently a feature called "return address signing" has been added to GCC to
prevent stack smash stack on AArch64.  For details please refer:

  https://gcc.gnu.org/ml/gcc-patches/2017-01/msg00376.html

GDB needs to be aware of this feature so it can restore the original return
address which is critical for unwinding.

On compiler side, whenever return address, i.e. LR register, is mangled or
restored by hardware instruction, compiler is expected to generate a
DW_CFA_AARCH64_negate_ra_state to toggle return address signing status.

DW_CFA_AARCH64_negate_ra_state is using the same CFI number and
therefore need to be multiplexed with DW_CFA_GNU_window_save which was designed
for SPARC.

A new gdbarch method "execute_dwarf_cfa_vendor_op" is introduced by this patch.
It's parameters has been restricted to those only needed by SPARC and AArch64
for multiplexing DW_CFA_GNU_window_save which is a CFI operation takes none
operand.  Should any further DWARF CFI operation want to be multiplexed in the
future,  the parameter list can be extended.  Below is the current function
prototype.

   typedef int (gdbarch_execute_dwarf_cfa_vendor_op_ftype)
     (struct gdbarch *gdbarch, gdb_byte op, struct dwarf2_frame_state *fs);

DW_CFA_GNU_window_save support for SPARC is migrated to this new gdbarch
method by this patch.

gdb/
* gdbarch.sh: New gdbarch method execute_dwarf_cfa_vendor_op.
* gdbarch.c: Regenerated.
* gdbarch.h: Regenerated.
* dwarf2-frame.c (dwarf2_frame_state_alloc_regs): Made the
visibility external.
(execute_cfa_program): Call execute_dwarf_cfa_vendor_op for CFI
between DW_CFA_lo_user and DW_CFA_high_user inclusive.
(enum cfa_how_kind): Move to ...
(struct dwarf2_frame_state_reg_info): Likewise.
(struct dwarf2_frame_state): Likewise.
* dwarf2-frame.h: ... here.
(dwarf2_frame_state_alloc_regs): New declaration.
* sparc-tdep.c (sparc_execute_dwarf_cfa_vendor_op): New function.
(sparc32_gdbarch_init): Register execute_dwarf_cfa_vendor_op hook.

gdb/ChangeLog
gdb/arch-utils.c
gdb/arch-utils.h
gdb/dwarf2-frame.c
gdb/dwarf2-frame.h
gdb/gdbarch.c
gdb/gdbarch.h
gdb/gdbarch.sh
gdb/sparc-tdep.c

index 03bec1f021d04c4cead423b9da96deda3d6c0249..6cde27438eb1ed7f342a97e5b10ce6716246cbc9 100644 (file)
@@ -1,3 +1,20 @@
+2017-04-26  Jiong Wang  <jiong.wang@arm.com>
+
+       * gdbarch.sh: New gdbarch method execute_dwarf_cfa_vendor_op.
+       * gdbarch.c: Regenerated.
+       * gdbarch.h: Regenerated.
+       * dwarf2-frame.c (dwarf2_frame_state_alloc_regs): Made the
+       visibility external.
+       (execute_cfa_program): Call execute_dwarf_cfa_vendor_op for CFI
+       between DW_CFA_lo_user and DW_CFA_high_user inclusive.
+       (enum cfa_how_kind): Move to ...
+       (struct dwarf2_frame_state_reg_info): Likewise.
+       (struct dwarf2_frame_state): Likewise.
+       * dwarf2-frame.h: ... here.
+       (dwarf2_frame_state_alloc_regs): New declaration.
+       * sparc-tdep.c (sparc_execute_dwarf_cfa_vendor_op): New function.
+       (sparc32_gdbarch_init): Register execute_dwarf_cfa_vendor_op hook.
+
 2017-04-26  Alan Hayward  <alan.hayward@arm.com>
 
        * xtensa-tdep.c (xtensa_pseudo_register_read): Use
index 3b22e51fc4dbd4f062bbbd0cfa6b7920ffe4b7e9..b1cec80bcac2b8ffb143b230881b2c80d974dcf9 100644 (file)
@@ -204,6 +204,15 @@ default_adjust_dwarf2_line (CORE_ADDR addr, int rel)
   return addr;
 }
 
+/* See arch-utils.h.  */
+
+bool
+default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
+                                    struct dwarf2_frame_state *fs)
+{
+  return false;
+}
+
 int
 cannot_register_not (struct gdbarch *gdbarch, int regnum)
 {
index 4d7b4991b97c3c40608bd999f2eaeb634d056f7e..967a4b19de17a59843a61a6d810fbe3ca1a2bd8d 100644 (file)
@@ -25,6 +25,7 @@ struct frame_info;
 struct minimal_symbol;
 struct type;
 struct gdbarch_info;
+struct dwarf2_frame_state;
 
 template <size_t bp_size, const gdb_byte *break_insn>
 struct bp_manipulation
@@ -130,6 +131,11 @@ CORE_ADDR default_adjust_dwarf2_addr (CORE_ADDR pc);
 
 CORE_ADDR default_adjust_dwarf2_line (CORE_ADDR addr, int rel);
 
+/* Default DWARF vendor CFI handler.  */
+
+bool default_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
+                                         struct dwarf2_frame_state *fs);
+
 /* Version of cannot_fetch_register() / cannot_store_register() that
    always fails.  */
 
index f8dd1df739d024e73bf2756d033773393bc4ec4b..d7f985b5b045a0cafe937b9a6f4816ea9cd4c18e 100644 (file)
@@ -165,58 +165,6 @@ static CORE_ADDR read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
                                     CORE_ADDR func_base);
 \f
 
-enum cfa_how_kind
-{
-  CFA_UNSET,
-  CFA_REG_OFFSET,
-  CFA_EXP
-};
-
-struct dwarf2_frame_state_reg_info
-{
-  struct dwarf2_frame_state_reg *reg;
-  int num_regs;
-
-  LONGEST cfa_offset;
-  ULONGEST cfa_reg;
-  enum cfa_how_kind cfa_how;
-  const gdb_byte *cfa_exp;
-
-  /* Used to implement DW_CFA_remember_state.  */
-  struct dwarf2_frame_state_reg_info *prev;
-};
-
-/* Structure describing a frame state.  */
-
-struct dwarf2_frame_state
-{
-  /* Each register save state can be described in terms of a CFA slot,
-     another register, or a location expression.  */
-  struct dwarf2_frame_state_reg_info regs;
-
-  /* The PC described by the current frame state.  */
-  CORE_ADDR pc;
-
-  /* Initial register set from the CIE.
-     Used to implement DW_CFA_restore.  */
-  struct dwarf2_frame_state_reg_info initial;
-
-  /* The information we care about from the CIE.  */
-  LONGEST data_align;
-  ULONGEST code_align;
-  ULONGEST retaddr_column;
-
-  /* Flags for known producer quirks.  */
-
-  /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
-     and DW_CFA_def_cfa_offset takes a factored offset.  */
-  int armcc_cfa_offsets_sf;
-
-  /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
-     the CFA is defined as REG - OFFSET rather than REG + OFFSET.  */
-  int armcc_cfa_offsets_reversed;
-};
-
 /* Store the length the expression for the CFA in the `cfa_reg' field,
    which is unused in that case.  */
 #define cfa_exp_len cfa_reg
@@ -224,7 +172,7 @@ struct dwarf2_frame_state
 /* Assert that the register set RS is large enough to store gdbarch_num_regs
    columns.  If necessary, enlarge the register set.  */
 
-static void
+void
 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
                               int num_regs)
 {
@@ -672,31 +620,6 @@ bad CFI data; mismatched DW_CFA_restore_state at %s"),
              /* cfa_how deliberately not set.  */
              break;
 
-           case DW_CFA_GNU_window_save:
-             /* This is SPARC-specific code, and contains hard-coded
-                constants for the register numbering scheme used by
-                GCC.  Rather than having a architecture-specific
-                operation that's only ever used by a single
-                architecture, we provide the implementation here.
-                Incidentally that's what GCC does too in its
-                unwinder.  */
-             {
-               int size = register_size (gdbarch, 0);
-
-               dwarf2_frame_state_alloc_regs (&fs->regs, 32);
-               for (reg = 8; reg < 16; reg++)
-                 {
-                   fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
-                   fs->regs.reg[reg].loc.reg = reg + 16;
-                 }
-               for (reg = 16; reg < 32; reg++)
-                 {
-                   fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
-                   fs->regs.reg[reg].loc.offset = (reg - 16) * size;
-                 }
-             }
-             break;
-
            case DW_CFA_GNU_args_size:
              /* Ignored.  */
              insn_ptr = safe_read_uleb128 (insn_ptr, insn_end, &utmp);
@@ -713,8 +636,17 @@ bad CFI data; mismatched DW_CFA_restore_state at %s"),
              break;
 
            default:
-             internal_error (__FILE__, __LINE__,
-                             _("Unknown CFI encountered."));
+             if (insn >= DW_CFA_lo_user && insn <= DW_CFA_hi_user)
+               {
+                 /* Handle vendor-specific CFI for different architectures.  */
+                 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch, insn, fs))
+                   error (_("Call Frame Instruction op %d in vendor extension "
+                            "space is not handled on this architecture."),
+                          insn);
+               }
+             else
+               internal_error (__FILE__, __LINE__,
+                               _("Unknown CFI encountered."));
            }
        }
     }
index 2ada436f21924068eb91aab312b02450e1738602..9e8668ea7a1a265622335359bad94d8f05bd0937 100644 (file)
@@ -82,6 +82,58 @@ struct dwarf2_frame_state_reg
   enum dwarf2_frame_reg_rule how;
 };
 
+enum cfa_how_kind
+{
+  CFA_UNSET,
+  CFA_REG_OFFSET,
+  CFA_EXP
+};
+
+struct dwarf2_frame_state_reg_info
+{
+  struct dwarf2_frame_state_reg *reg;
+  int num_regs;
+
+  LONGEST cfa_offset;
+  ULONGEST cfa_reg;
+  enum cfa_how_kind cfa_how;
+  const gdb_byte *cfa_exp;
+
+  /* Used to implement DW_CFA_remember_state.  */
+  struct dwarf2_frame_state_reg_info *prev;
+};
+
+/* Structure describing a frame state.  */
+
+struct dwarf2_frame_state
+{
+  /* Each register save state can be described in terms of a CFA slot,
+     another register, or a location expression.  */
+  struct dwarf2_frame_state_reg_info regs;
+
+  /* The PC described by the current frame state.  */
+  CORE_ADDR pc;
+
+  /* Initial register set from the CIE.
+     Used to implement DW_CFA_restore.  */
+  struct dwarf2_frame_state_reg_info initial;
+
+  /* The information we care about from the CIE.  */
+  LONGEST data_align;
+  ULONGEST code_align;
+  ULONGEST retaddr_column;
+
+  /* Flags for known producer quirks.  */
+
+  /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
+     and DW_CFA_def_cfa_offset takes a factored offset.  */
+  int armcc_cfa_offsets_sf;
+
+  /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
+     the CFA is defined as REG - OFFSET rather than REG + OFFSET.  */
+  int armcc_cfa_offsets_reversed;
+};
+
 /* Set the architecture-specific register state initialization
    function for GDBARCH to INIT_REG.  */
 
@@ -120,6 +172,12 @@ extern const struct frame_base *
 
 CORE_ADDR dwarf2_frame_cfa (struct frame_info *this_frame);
 
+/* Assert that the register set RS is large enough to store gdbarch_num_regs
+   columns.  If necessary, enlarge the register set.  */
+
+void dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
+                                   int num_regs);
+
 /* Find the CFA information for PC.
 
    Return 1 if a register is used for the CFA, or 0 if another
index 56643256dab00027f043e6ef58ff59477083e7f5..41c086685d7494c8f9477e882af5d879115ab5f6 100644 (file)
@@ -275,6 +275,7 @@ struct gdbarch
   int have_nonsteppable_watchpoint;
   gdbarch_address_class_type_flags_ftype *address_class_type_flags;
   gdbarch_address_class_type_flags_to_name_ftype *address_class_type_flags_to_name;
+  gdbarch_execute_dwarf_cfa_vendor_op_ftype *execute_dwarf_cfa_vendor_op;
   gdbarch_address_class_name_to_type_flags_ftype *address_class_name_to_type_flags;
   gdbarch_register_reggroup_p_ftype *register_reggroup_p;
   gdbarch_fetch_pointer_argument_ftype *fetch_pointer_argument;
@@ -436,6 +437,7 @@ gdbarch_alloc (const struct gdbarch_info *info,
   gdbarch->make_symbol_special = default_make_symbol_special;
   gdbarch->adjust_dwarf2_addr = default_adjust_dwarf2_addr;
   gdbarch->adjust_dwarf2_line = default_adjust_dwarf2_line;
+  gdbarch->execute_dwarf_cfa_vendor_op = default_execute_dwarf_cfa_vendor_op;
   gdbarch->register_reggroup_p = default_register_reggroup_p;
   gdbarch->skip_permanent_breakpoint = default_skip_permanent_breakpoint;
   gdbarch->displaced_step_hw_singlestep = default_displaced_step_hw_singlestep;
@@ -634,6 +636,7 @@ verify_gdbarch (struct gdbarch *gdbarch)
   /* Skip verify of have_nonsteppable_watchpoint, invalid_p == 0 */
   /* Skip verify of address_class_type_flags, has predicate.  */
   /* Skip verify of address_class_type_flags_to_name, has predicate.  */
+  /* Skip verify of execute_dwarf_cfa_vendor_op, invalid_p == 0 */
   /* Skip verify of address_class_name_to_type_flags, has predicate.  */
   /* Skip verify of register_reggroup_p, invalid_p == 0 */
   /* Skip verify of fetch_pointer_argument, has predicate.  */
@@ -974,6 +977,9 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *file)
   fprintf_unfiltered (file,
                       "gdbarch_dump: elfcore_write_linux_prpsinfo = <%s>\n",
                       host_address_to_string (gdbarch->elfcore_write_linux_prpsinfo));
+  fprintf_unfiltered (file,
+                      "gdbarch_dump: execute_dwarf_cfa_vendor_op = <%s>\n",
+                      host_address_to_string (gdbarch->execute_dwarf_cfa_vendor_op));
   fprintf_unfiltered (file,
                       "gdbarch_dump: fast_tracepoint_valid_at = <%s>\n",
                       host_address_to_string (gdbarch->fast_tracepoint_valid_at));
@@ -3523,6 +3529,23 @@ set_gdbarch_address_class_type_flags_to_name (struct gdbarch *gdbarch,
   gdbarch->address_class_type_flags_to_name = address_class_type_flags_to_name;
 }
 
+bool
+gdbarch_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op, struct dwarf2_frame_state *fs)
+{
+  gdb_assert (gdbarch != NULL);
+  gdb_assert (gdbarch->execute_dwarf_cfa_vendor_op != NULL);
+  if (gdbarch_debug >= 2)
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_execute_dwarf_cfa_vendor_op called\n");
+  return gdbarch->execute_dwarf_cfa_vendor_op (gdbarch, op, fs);
+}
+
+void
+set_gdbarch_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch,
+                                         gdbarch_execute_dwarf_cfa_vendor_op_ftype execute_dwarf_cfa_vendor_op)
+{
+  gdbarch->execute_dwarf_cfa_vendor_op = execute_dwarf_cfa_vendor_op;
+}
+
 int
 gdbarch_address_class_name_to_type_flags_p (struct gdbarch *gdbarch)
 {
index 4845f239d18d3e09eb8c5afd99c0ffb147e1989e..761790828a5c7951786c9bcbb495ef5336b4eb12 100644 (file)
@@ -827,6 +827,13 @@ typedef const char * (gdbarch_address_class_type_flags_to_name_ftype) (struct gd
 extern const char * gdbarch_address_class_type_flags_to_name (struct gdbarch *gdbarch, int type_flags);
 extern void set_gdbarch_address_class_type_flags_to_name (struct gdbarch *gdbarch, gdbarch_address_class_type_flags_to_name_ftype *address_class_type_flags_to_name);
 
+/* Execute vendor-specific DWARF Call Frame Instruction.  OP is the instruction.
+   FS are passed from the generic execute_cfa_program function. */
+
+typedef bool (gdbarch_execute_dwarf_cfa_vendor_op_ftype) (struct gdbarch *gdbarch, gdb_byte op, struct dwarf2_frame_state *fs);
+extern bool gdbarch_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op, struct dwarf2_frame_state *fs);
+extern void set_gdbarch_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdbarch_execute_dwarf_cfa_vendor_op_ftype *execute_dwarf_cfa_vendor_op);
+
 /* Return the appropriate type_flags for the supplied address class.
    This function should return 1 if the address class was recognized and
    type_flags was set, zero otherwise. */
index a42dc437742ee5cdfce8326103608f421832285c..fa85f7cc91b9f73dd7857a644c18cd786ffc4e51 100755 (executable)
@@ -708,6 +708,9 @@ v:int:cannot_step_breakpoint:::0:0::0
 v:int:have_nonsteppable_watchpoint:::0:0::0
 F:int:address_class_type_flags:int byte_size, int dwarf2_addr_class:byte_size, dwarf2_addr_class
 M:const char *:address_class_type_flags_to_name:int type_flags:type_flags
+# Execute vendor-specific DWARF Call Frame Instruction.  OP is the instruction.
+# FS are passed from the generic execute_cfa_program function.
+m:bool:execute_dwarf_cfa_vendor_op:gdb_byte op, struct dwarf2_frame_state *fs:op, fs::default_execute_dwarf_cfa_vendor_op::0
 
 # Return the appropriate type_flags for the supplied address class.
 # This function should return 1 if the address class was recognized and
index 078907a5d4aeaa472f0ce745efd033bab98d8dcc..73152ce87755f619b831b27710a1ea7349101075 100644 (file)
@@ -20,6 +20,7 @@
 #include "defs.h"
 #include "arch-utils.h"
 #include "dis-asm.h"
+#include "dwarf2.h"
 #include "dwarf2-frame.h"
 #include "floatformat.h"
 #include "frame.h"
@@ -1536,6 +1537,34 @@ sparc32_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
     }
 }
 
+/* Implement the execute_dwarf_cfa_vendor_op method.  */
+
+static bool
+sparc_execute_dwarf_cfa_vendor_op (struct gdbarch *gdbarch, gdb_byte op,
+                                  struct dwarf2_frame_state *fs)
+{
+  /* Only DW_CFA_GNU_window_save is expected on SPARC.  */
+  if (op != DW_CFA_GNU_window_save)
+    return false;
+
+  uint64_t reg;
+  int size = register_size (gdbarch, 0);
+
+  dwarf2_frame_state_alloc_regs (&fs->regs, 32);
+  for (reg = 8; reg < 16; reg++)
+    {
+      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
+      fs->regs.reg[reg].loc.reg = reg + 16;
+    }
+  for (reg = 16; reg < 32; reg++)
+    {
+      fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
+      fs->regs.reg[reg].loc.offset = (reg - 16) * size;
+    }
+
+  return true;
+}
+
 \f
 /* The SPARC Architecture doesn't have hardware single-step support,
    and most operating systems don't implement it either, so we provide
@@ -1801,6 +1830,9 @@ sparc32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
 
   /* Hook in the DWARF CFI frame unwinder.  */
   dwarf2_frame_set_init_reg (gdbarch, sparc32_dwarf2_frame_init_reg);
+  /* Register DWARF vendor CFI handler.  */
+  set_gdbarch_execute_dwarf_cfa_vendor_op (gdbarch,
+                                          sparc_execute_dwarf_cfa_vendor_op);
   /* FIXME: kettenis/20050423: Don't enable the unwinder until the
      StackGhost issues have been resolved.  */