Blame SOURCES/gdb-rhbz1522798-ppc64-plt-reverse.patch

2c2fa1
commit db9077b7275e86637218a7a7d165cb85a4de116f
2c2fa1
Author: Alan Modra <amodra@gmail.com>
2c2fa1
Date:   Mon Dec 11 17:31:11 2017 +1030
2c2fa1
2c2fa1
    PR22576, ppc64_skip_trampoline_code uses wrong r2 for EXEC_REVERSE
2c2fa1
    
2c2fa1
    The TOC pointer register, r2, on powerpc64 is generally not mentioned
2c2fa1
    in debug info.  It is saved and restored by call linkage code, and
2c2fa1
    set to the callee value either by call stub code (ELFv1) or in the
2c2fa1
    callee global entry point code (ELFv2).  A call stub uses the caller
2c2fa1
    TOC pointer to access the PLT.  So for gdb to read the correct PLT
2c2fa1
    entry in order to determine the destination of the trampoline, gdb
2c2fa1
    needs to know the caller r2.  When skipping over trampolines in the
2c2fa1
    normal forward direction, the caller r2 is simply the current value of
2c2fa1
    r2 (at the start of the trampoline).  However, when reversing over
2c2fa1
    trampolines the current value of r2 is that for the callee.  Using
2c2fa1
    that value results in wild reads of memory rather than the correct PLT
2c2fa1
    entry.
2c2fa1
    
2c2fa1
    This patch corrects the value of r2 by using the value saved on the
2c2fa1
    stack for reverse execution.  Note that in reverse execution mode it
2c2fa1
    isn't really necessary for skip_trampoline_code to return the actual
2c2fa1
    destination, so we're doing a little more work than needed here.  Any
2c2fa1
    non-zero return value would do (and it would be nicer if the interface
2c2fa1
    was changed to return the start of the stub).
2c2fa1
    
2c2fa1
            PR tdep/22576
2c2fa1
            * ppc64-tdep.c (ppc64_plt_entry_point): Rewrite to take TOC-relative
2c2fa1
            PLT offset, and retrieve r2 from stack when executing in reverse.
2c2fa1
            (ppc64_standard_linkage1_target): Drop pc param.  Calculate offset
2c2fa1
            rather than PLT address.
2c2fa1
            (ppc64_standard_linkage2_target): Likewise.
2c2fa1
            (ppc64_standard_linkage3_target): Likewise.
2c2fa1
            (ppc64_standard_linkage4_target): Likewise.
2c2fa1
            (ppc64_skip_trampoline_code_1): Adjust to suit.
2c2fa1
2c2fa1
### a/gdb/ChangeLog
2c2fa1
### b/gdb/ChangeLog
2c2fa1
## -1,3 +1,15 @@
2c2fa1
+2017-12-12  Alan Modra  <amodra@gmail.com>
2c2fa1
+
2c2fa1
+	PR tdep/22576
2c2fa1
+	* ppc64-tdep.c (ppc64_plt_entry_point): Rewrite to take TOC-relative
2c2fa1
+	PLT offset, and retrieve r2 from stack when executing in reverse.
2c2fa1
+	(ppc64_standard_linkage1_target): Drop pc param.  Calculate offset
2c2fa1
+	rather than PLT address.
2c2fa1
+	(ppc64_standard_linkage2_target): Likewise.
2c2fa1
+	(ppc64_standard_linkage3_target): Likewise.
2c2fa1
+	(ppc64_standard_linkage4_target): Likewise.
2c2fa1
+	(ppc64_skip_trampoline_code_1): Adjust to suit.
2c2fa1
+
2c2fa1
 2017-12-11  Simon Marchi  <simon.marchi@ericsson.com>
2c2fa1
 
2c2fa1
 	PR gdb/22556
2c2fa1
--- a/gdb/ppc64-tdep.c
2c2fa1
+++ b/gdb/ppc64-tdep.c
2c2fa1
@@ -49,15 +49,30 @@
2c2fa1
    | (((spr) & 0x3e0) << 6)                     \
2c2fa1
    | (((xo) & 0x3ff) << 1))
2c2fa1
 
2c2fa1
-/* If PLT is the address of a 64-bit PowerPC PLT entry,
2c2fa1
-   return the function's entry point.  */
2c2fa1
+/* PLT_OFF is the TOC-relative offset of a 64-bit PowerPC PLT entry.
2c2fa1
+   Return the function's entry point.  */
2c2fa1
 
2c2fa1
 static CORE_ADDR
2c2fa1
-ppc64_plt_entry_point (struct gdbarch *gdbarch, CORE_ADDR plt)
2c2fa1
+ppc64_plt_entry_point (struct frame_info *frame, CORE_ADDR plt_off)
2c2fa1
 {
2c2fa1
+  struct gdbarch *gdbarch = get_frame_arch (frame);
2c2fa1
   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2c2fa1
+  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2c2fa1
+  CORE_ADDR tocp;
2c2fa1
+
2c2fa1
+  if (execution_direction == EXEC_REVERSE)
2c2fa1
+    {
2c2fa1
+      /* If executing in reverse, r2 will have been stored to the stack.  */
2c2fa1
+      CORE_ADDR sp = get_frame_register_unsigned (frame,
2c2fa1
+						  tdep->ppc_gp0_regnum + 1);
2c2fa1
+      unsigned int sp_off = tdep->elf_abi == POWERPC_ELF_V1 ? 40 : 24;
2c2fa1
+      tocp = read_memory_unsigned_integer (sp + sp_off, 8, byte_order);
2c2fa1
+    }
2c2fa1
+  else
2c2fa1
+    tocp = get_frame_register_unsigned (frame, tdep->ppc_gp0_regnum + 2);
2c2fa1
+
2c2fa1
   /* The first word of the PLT entry is the function entry point.  */
2c2fa1
-  return (CORE_ADDR) read_memory_unsigned_integer (plt, 8, byte_order);
2c2fa1
+  return read_memory_unsigned_integer (tocp + plt_off, 8, byte_order);
2c2fa1
 }
2c2fa1
 
2c2fa1
 /* Patterns for the standard linkage functions.  These are built by
2c2fa1
@@ -377,74 +392,44 @@ static struct ppc_insn_pattern ppc64_standard_linkage8[] =
2c2fa1
    the linkage function.  */
2c2fa1
 
2c2fa1
 /* If the current thread is about to execute a series of instructions
2c2fa1
-   at PC matching the ppc64_standard_linkage pattern, and INSN is the result
2c2fa1
+   matching the ppc64_standard_linkage pattern, and INSN is the result
2c2fa1
    from that pattern match, return the code address to which the
2c2fa1
    standard linkage function will send them.  (This doesn't deal with
2c2fa1
    dynamic linker lazy symbol resolution stubs.)  */
2c2fa1
 
2c2fa1
 static CORE_ADDR
2c2fa1
-ppc64_standard_linkage1_target (struct frame_info *frame,
2c2fa1
-				CORE_ADDR pc, unsigned int *insn)
2c2fa1
+ppc64_standard_linkage1_target (struct frame_info *frame, unsigned int *insn)
2c2fa1
 {
2c2fa1
-  struct gdbarch *gdbarch = get_frame_arch (frame);
2c2fa1
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2c2fa1
-
2c2fa1
-  /* The address of the PLT entry this linkage function references.  */
2c2fa1
-  CORE_ADDR plt
2c2fa1
-    = ((CORE_ADDR) get_frame_register_unsigned (frame,
2c2fa1
-						tdep->ppc_gp0_regnum + 2)
2c2fa1
-       + (ppc_insn_d_field (insn[0]) << 16)
2c2fa1
-       + ppc_insn_ds_field (insn[2]));
2c2fa1
+  CORE_ADDR plt_off = ((ppc_insn_d_field (insn[0]) << 16)
2c2fa1
+		       + ppc_insn_ds_field (insn[2]));
2c2fa1
 
2c2fa1
-  return ppc64_plt_entry_point (gdbarch, plt);
2c2fa1
+  return ppc64_plt_entry_point (frame, plt_off);
2c2fa1
 }
2c2fa1
 
2c2fa1
 static CORE_ADDR
2c2fa1
-ppc64_standard_linkage2_target (struct frame_info *frame,
2c2fa1
-				CORE_ADDR pc, unsigned int *insn)
2c2fa1
+ppc64_standard_linkage2_target (struct frame_info *frame, unsigned int *insn)
2c2fa1
 {
2c2fa1
-  struct gdbarch *gdbarch = get_frame_arch (frame);
2c2fa1
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2c2fa1
-
2c2fa1
-  /* The address of the PLT entry this linkage function references.  */
2c2fa1
-  CORE_ADDR plt
2c2fa1
-    = ((CORE_ADDR) get_frame_register_unsigned (frame,
2c2fa1
-						tdep->ppc_gp0_regnum + 2)
2c2fa1
-       + (ppc_insn_d_field (insn[1]) << 16)
2c2fa1
-       + ppc_insn_ds_field (insn[3]));
2c2fa1
+  CORE_ADDR plt_off = ((ppc_insn_d_field (insn[1]) << 16)
2c2fa1
+		       + ppc_insn_ds_field (insn[3]));
2c2fa1
 
2c2fa1
-  return ppc64_plt_entry_point (gdbarch, plt);
2c2fa1
+  return ppc64_plt_entry_point (frame, plt_off);
2c2fa1
 }
2c2fa1
 
2c2fa1
 static CORE_ADDR
2c2fa1
-ppc64_standard_linkage3_target (struct frame_info *frame,
2c2fa1
-				CORE_ADDR pc, unsigned int *insn)
2c2fa1
+ppc64_standard_linkage3_target (struct frame_info *frame, unsigned int *insn)
2c2fa1
 {
2c2fa1
-  struct gdbarch *gdbarch = get_frame_arch (frame);
2c2fa1
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2c2fa1
+  CORE_ADDR plt_off = ppc_insn_ds_field (insn[1]);
2c2fa1
 
2c2fa1
-  /* The address of the PLT entry this linkage function references.  */
2c2fa1
-  CORE_ADDR plt
2c2fa1
-    = ((CORE_ADDR) get_frame_register_unsigned (frame,
2c2fa1
-						tdep->ppc_gp0_regnum + 2)
2c2fa1
-       + ppc_insn_ds_field (insn[1]));
2c2fa1
-
2c2fa1
-  return ppc64_plt_entry_point (gdbarch, plt);
2c2fa1
+  return ppc64_plt_entry_point (frame, plt_off);
2c2fa1
 }
2c2fa1
 
2c2fa1
 static CORE_ADDR
2c2fa1
-ppc64_standard_linkage4_target (struct frame_info *frame,
2c2fa1
-				CORE_ADDR pc, unsigned int *insn)
2c2fa1
+ppc64_standard_linkage4_target (struct frame_info *frame, unsigned int *insn)
2c2fa1
 {
2c2fa1
-  struct gdbarch *gdbarch = get_frame_arch (frame);
2c2fa1
-  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2c2fa1
-
2c2fa1
-  CORE_ADDR plt
2c2fa1
-    = ((CORE_ADDR) get_frame_register_unsigned (frame, tdep->ppc_gp0_regnum + 2)
2c2fa1
-       + (ppc_insn_d_field (insn[1]) << 16)
2c2fa1
-       + ppc_insn_ds_field (insn[2]));
2c2fa1
+  CORE_ADDR plt_off = ((ppc_insn_d_field (insn[1]) << 16)
2c2fa1
+		       + ppc_insn_ds_field (insn[2]));
2c2fa1
 
2c2fa1
-  return ppc64_plt_entry_point (gdbarch, plt);
2c2fa1
+  return ppc64_plt_entry_point (frame, plt_off);
2c2fa1
 }
2c2fa1
 
2c2fa1
 
2c2fa1
@@ -480,39 +465,39 @@ ppc64_skip_trampoline_code_1 (struct frame_info *frame, CORE_ADDR pc)
2c2fa1
     {
2c2fa1
       if (i < ARRAY_SIZE (ppc64_standard_linkage8) - 1
2c2fa1
 	  && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage8, insns))
2c2fa1
-	pc = ppc64_standard_linkage4_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage4_target (frame, insns);
2c2fa1
       else if (i < ARRAY_SIZE (ppc64_standard_linkage7) - 1
2c2fa1
 	       && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage7,
2c2fa1
 					   insns))
2c2fa1
-	pc = ppc64_standard_linkage3_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage3_target (frame, insns);
2c2fa1
       else if (i < ARRAY_SIZE (ppc64_standard_linkage6) - 1
2c2fa1
 	       && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage6,
2c2fa1
 					   insns))
2c2fa1
-	pc = ppc64_standard_linkage4_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage4_target (frame, insns);
2c2fa1
       else if (i < ARRAY_SIZE (ppc64_standard_linkage5) - 1
2c2fa1
 	       && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage5,
2c2fa1
 					   insns)
2c2fa1
 	       && (insns[8] != 0 || insns[9] != 0))
2c2fa1
-	pc = ppc64_standard_linkage3_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage3_target (frame, insns);
2c2fa1
       else if (i < ARRAY_SIZE (ppc64_standard_linkage4) - 1
2c2fa1
 	       && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage4,
2c2fa1
 					   insns)
2c2fa1
 	       && (insns[9] != 0 || insns[10] != 0))
2c2fa1
-	pc = ppc64_standard_linkage4_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage4_target (frame, insns);
2c2fa1
       else if (i < ARRAY_SIZE (ppc64_standard_linkage3) - 1
2c2fa1
 	       && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage3,
2c2fa1
 					   insns)
2c2fa1
 	       && (insns[8] != 0 || insns[9] != 0))
2c2fa1
-	pc = ppc64_standard_linkage3_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage3_target (frame, insns);
2c2fa1
       else if (i < ARRAY_SIZE (ppc64_standard_linkage2) - 1
2c2fa1
 	       && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage2,
2c2fa1
 					   insns)
2c2fa1
 	       && (insns[10] != 0 || insns[11] != 0))
2c2fa1
-	pc = ppc64_standard_linkage2_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage2_target (frame, insns);
2c2fa1
       else if (i < ARRAY_SIZE (ppc64_standard_linkage1) - 1
2c2fa1
 	       && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage1,
2c2fa1
 					   insns))
2c2fa1
-	pc = ppc64_standard_linkage1_target (frame, pc, insns);
2c2fa1
+	pc = ppc64_standard_linkage1_target (frame, insns);
2c2fa1
       else
2c2fa1
 	{
2c2fa1
 	  /* Scan backward one more instructions if doesn't match.  */