Blame SOURCES/gdb-rhbz1870017-p10-plt-prologue-skipping.patch

e22c64
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
e22c64
From: Carl Love <cel@us.ibm.com>
e22c64
Date: Thu, 29 Apr 2021 17:19:13 -0500
e22c64
Subject: gdb-rhbz1870017-p10-plt-prologue-skipping.patch
e22c64
e22c64
;; Backport "Add Power 10 PLT instruction patterns"
e22c64
;; (Carl Love, RHBZ 1870017)
e22c64
e22c64
gdb/ChangeLog:
e22c64
e22c64
2021-06-07  Carl Love  <cel@us.ibm.com>
e22c64
e22c64
	* ppc-tdep.h (ppc_insn_prefix_dform): Declare.
e22c64
	* ppc64-tdep.c(insn_md, insn_x, insn_xo): New macros.
e22c64
	(ppc64_plt_pcrel_entry_point, ppc64_pcrel_linkage1_target,
e22c64
	ppc64_pcrel_linkage2_target): New functions.
e22c64
	(ppc64_standard_linkage9, ppc64_standard_linkage10,
e22c64
	ppc64_standard_linkage11, ppc64_standard_linkage12): New ppc
e22c64
	instruction patterns.
e22c64
	(ppc64_standard_linkage9, ppc64_standard_linkage10,
e22c64
	ppc64_standard_linkage11, ppc64_standard_linkage12): New variables
e22c64
	in define MAX expression.
e22c64
	(ppc64_skip_trampoline_code_1): Handle ppc64_standard_linkage9,
e22c64
	ppc64_standard_linkage10, ppc64_standard_linkage11,
e22c64
	ppc64_standard_linkage12.
e22c64
	* (ppc_insn_prefix_dform): New function.
e22c64
e22c64
diff --git a/gdb/ppc-tdep.h b/gdb/ppc-tdep.h
e22c64
--- a/gdb/ppc-tdep.h
e22c64
+++ b/gdb/ppc-tdep.h
e22c64
@@ -426,6 +426,8 @@ extern int ppc_insns_match_pattern (struct frame_info *frame, CORE_ADDR pc,
e22c64
 extern CORE_ADDR ppc_insn_d_field (unsigned int insn);
e22c64
 
e22c64
 extern CORE_ADDR ppc_insn_ds_field (unsigned int insn);
e22c64
+extern CORE_ADDR ppc_insn_prefix_dform (unsigned int insn1,
e22c64
+					unsigned int insn2);
e22c64
 
e22c64
 extern int ppc_process_record (struct gdbarch *gdbarch,
e22c64
 			       struct regcache *regcache, CORE_ADDR addr);
e22c64
diff --git a/gdb/ppc64-tdep.c b/gdb/ppc64-tdep.c
e22c64
--- a/gdb/ppc64-tdep.c
e22c64
+++ b/gdb/ppc64-tdep.c
e22c64
@@ -49,6 +49,38 @@
e22c64
    | (((unsigned (spr)) & 0x3e0) << 6)                     \
e22c64
    | (((unsigned (xo)) & 0x3ff) << 1))
e22c64
 
e22c64
+#define prefix(a, b, R, do)				   \
e22c64
+  (((0x1) << 26)					   \
e22c64
+   | (((unsigned (a)) & 0x3) << 24)			   \
e22c64
+   | (((unsigned (b)) & 0x1) << 23)			   \
e22c64
+   | (((unsigned (R)) & 0x1) << 20)			   \
e22c64
+   | ((unsigned (do)) & 0x3ffff))
e22c64
+
e22c64
+#define insn_md(opcd, ra, rs, sh, me, rc)	       	   \
e22c64
+  ((((unsigned (opcd)) & 0x3f) << 26)			   \
e22c64
+   | (((unsigned (rs)) & 0x1f) << 21)			   \
e22c64
+   | (((unsigned (ra)) & 0x1f) << 16)			   \
e22c64
+   | (((unsigned (sh)) & 0x3e) << 11)			   \
e22c64
+   | (((unsigned (me)) & 0x3f) << 25)			   \
e22c64
+   | (((unsigned (sh)) & 0x1)  << 1)			   \
e22c64
+   | ((unsigned (rc)) & 0x1))
e22c64
+
e22c64
+#define insn_x(opcd, rt, ra, rb, opc2)			   \
e22c64
+  ((((unsigned (opcd)) & 0x3f) << 26)			   \
e22c64
+   | (((unsigned (rt)) & 0x1f) << 21)			   \
e22c64
+   | (((unsigned (ra)) & 0x1f) << 16)			   \
e22c64
+   | (((unsigned (rb)) & 0x3e) << 11)			   \
e22c64
+   | (((unsigned (opc2)) & 0x3FF) << 1))
e22c64
+
e22c64
+#define insn_xo(opcd, rt, ra, rb, oe, rc, opc2)		   \
e22c64
+  ((((unsigned (opcd)) & 0x3f) << 26)			   \
e22c64
+   | (((unsigned (rt)) & 0x1f) << 21)			   \
e22c64
+   | (((unsigned (ra)) & 0x1f) << 16)			   \
e22c64
+   | (((unsigned (rb)) & 0x3e) << 11)			   \
e22c64
+   | (((unsigned (oe)) & 0x1) << 10)			   \
e22c64
+   | (((unsigned (opc2)) & 0x1FF) << 1)			   \
e22c64
+   | (((unsigned (rc)))))
e22c64
+
e22c64
 /* PLT_OFF is the TOC-relative offset of a 64-bit PowerPC PLT entry.
e22c64
    Return the function's entry point.  */
e22c64
 
e22c64
@@ -75,6 +107,18 @@ ppc64_plt_entry_point (struct frame_info *frame, CORE_ADDR plt_off)
e22c64
   return read_memory_unsigned_integer (tocp + plt_off, 8, byte_order);
e22c64
 }
e22c64
 
e22c64
+static CORE_ADDR
e22c64
+ppc64_plt_pcrel_entry_point (struct frame_info *frame, CORE_ADDR plt_off,
e22c64
+			     CORE_ADDR pc)
e22c64
+{
e22c64
+  struct gdbarch *gdbarch = get_frame_arch (frame);
e22c64
+  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
e22c64
+
e22c64
+  /* Execution direction doesn't matter, entry is pc + plt_off either way.
e22c64
+     The first word of the PLT entry is the function entry point.  */
e22c64
+  return read_memory_unsigned_integer (pc + plt_off, 8, byte_order);
e22c64
+}
e22c64
+
e22c64
 /* Patterns for the standard linkage functions.  These are built by
e22c64
    build_plt_stub in bfd/elf64-ppc.c.  */
e22c64
 
e22c64
@@ -342,6 +386,110 @@ static const struct ppc_insn_pattern ppc64_standard_linkage8[] =
e22c64
     { 0, 0, 0 }
e22c64
   };
e22c64
 
e22c64
+/* Power 10 ELFv2 PLT call stubs */
e22c64
+static const struct ppc_insn_pattern ppc64_standard_linkage9[] =
e22c64
+  {
e22c64
+    /* std   %r2,0+40(%r1)   <optional> */
e22c64
+    { insn_ds (-1, -1, -1, 0, 1), insn_ds (62, 2, 1, 40, 0), 1 },
e22c64
+
e22c64
+    /* pld r12, <any> */
e22c64
+    { prefix (-1, -1, 1, 0), prefix (0, 0, 1, 0), 0 },
e22c64
+    { insn_d (-1, -1, -1, 0), insn_d (57, 12, 0, 0), 0 },
e22c64
+
e22c64
+    /* mtctr r12  */
e22c64
+    { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
e22c64
+
e22c64
+    /* bctr   */
e22c64
+    { (unsigned) -1, 0x4e800420, 0 },
e22c64
+
e22c64
+    { 0, 0, 0 }
e22c64
+  };
e22c64
+
e22c64
+static const struct ppc_insn_pattern ppc64_standard_linkage10[] =
e22c64
+  {
e22c64
+    /* std   %r2,0+40(%r1)    <optional> */
e22c64
+    { insn_ds (-1, -1, -1, 0, 1), insn_ds (62, 2, 1, 40, 0), 1 },
e22c64
+
e22c64
+    /* paddi r12,<any> */
e22c64
+    { prefix (-1, -1, 1, 0), prefix (2, 0, 1, 0), 0 },
e22c64
+    { insn_d (-1, -1, -1, 0), insn_d (14, 12, 0, 0), 0 },
e22c64
+
e22c64
+    /* mtctr r12  <optional> */
e22c64
+    { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
e22c64
+
e22c64
+    /* bctr   */
e22c64
+    { (unsigned) -1, 0x4e800420, 0 },
e22c64
+
e22c64
+    { 0, 0, 0 }
e22c64
+  };
e22c64
+
e22c64
+static const struct ppc_insn_pattern ppc64_standard_linkage11[] =
e22c64
+  {
e22c64
+    /* std   %r2,0+40(%r1)   <optional> */
e22c64
+    { insn_ds (-1, -1, -1, 0, 1), insn_ds (62, 2, 1, 40, 0), 1 },
e22c64
+
e22c64
+    /* li %r11,0     <optional> */
e22c64
+    { insn_d (-1, -1, -1, 0), insn_d (14, 11, 0, 0), 1 },
e22c64
+
e22c64
+    /* sldi  %r11,%r11,34   <eq to rldicr rx,ry,n, 63-n> <optional>  */
e22c64
+    { insn_md (-1, -1, -1, 0, 0, 1), insn_md (30, 11, 11, 34, 63-34, 0), 1 },
e22c64
+
e22c64
+    /* paddi r12, <any> */
e22c64
+    { prefix (-1, -1, 1, 0), prefix (2, 0, 1, 0), 0 },
e22c64
+    { insn_d (-1, -1, -1, 0), insn_d (14, 12, 0, 0), 0 },
e22c64
+
e22c64
+    /* ldx   %r12,%r11,%r12  <optional> */
e22c64
+    { (unsigned) -1, insn_x (31, 12, 11, 12, 21), 1 },
e22c64
+
e22c64
+    /* add   %r12,%r11,%r12  <optional> */
e22c64
+    { (unsigned) -1, insn_xo (31, 12, 11, 12, 0, 0, 40), 1 },
e22c64
+
e22c64
+    /* mtctr r12   */
e22c64
+    { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
e22c64
+
e22c64
+    /* bctr   */ // 13, 14, 15, 16
e22c64
+    { (unsigned) -1, 0x4e800420, 0 },
e22c64
+
e22c64
+    { 0, 0, 0 }
e22c64
+  };
e22c64
+
e22c64
+static const struct ppc_insn_pattern ppc64_standard_linkage12[] =
e22c64
+  {
e22c64
+    /* std   %r2,0+40(%r1)    <optional>  */
e22c64
+    { insn_ds (-1, -1, -1, 0, 1), insn_ds (62, 2, 1, 40, 0), 1 },
e22c64
+
e22c64
+    /* lis %r11,xxx@ha <equivalent addis rx, 0, val> */
e22c64
+    /* addis r12, r2, <any> */
e22c64
+    { insn_d (-1, -1, -1, 0), insn_d (15, 12, 2, 0), 0 },
e22c64
+
e22c64
+    /* ori   %r11,%r11,xxx@l */
e22c64
+    { insn_d (-1, -1, -1, 0), insn_d (24, 11, 11, 0), 0 },
e22c64
+
e22c64
+    /* sldi  %r11,%r11,34 <optional> */
e22c64
+    { (unsigned) -1, insn_md (30, 11, 11, 34, 63-34, 0), 1 },
e22c64
+
e22c64
+    /*paddi r12,<any> */
e22c64
+    { prefix (-1, -1, 1, 0), prefix (2, 0, 1, 0), 0 },
e22c64
+    { insn_d (-1, -1, -1, 0), insn_d (14, 12, 0, 0), 0 },
e22c64
+
e22c64
+    /* sldi  %r11,%r11,34 <optional> */
e22c64
+    { (unsigned) -1, insn_md (30, 11, 11, 34, 63-34, 0), 1 },
e22c64
+
e22c64
+    /* ldx   %r12,%r11,%r12 <optional> */
e22c64
+    { (unsigned) -1, insn_x (31, 12, 11, 12, 21), 1 },
e22c64
+
e22c64
+    /* add   %r12,%r11,%r12 <optional> */
e22c64
+    { (unsigned) -1, insn_xo (31, 12, 11, 12, 0, 0, 40), 1 },
e22c64
+
e22c64
+    /* mtctr r12  */
e22c64
+    { insn_xfx (-1, -1, -1, -1), insn_xfx (31, 12, 9, 467), 0 },
e22c64
+
e22c64
+    /* bctr  */ // 17, 18, 19, 20
e22c64
+    { (unsigned) -1, 0x4e800420, 0 },
e22c64
+
e22c64
+    { 0, 0, 0 }
e22c64
+  };
e22c64
+
e22c64
 /* When the dynamic linker is doing lazy symbol resolution, the first
e22c64
    call to a function in another object will go like this:
e22c64
 
e22c64
@@ -432,6 +580,29 @@ ppc64_standard_linkage4_target (struct frame_info *frame, unsigned int *insn)
e22c64
   return ppc64_plt_entry_point (frame, plt_off);
e22c64
 }
e22c64
 
e22c64
+static CORE_ADDR
e22c64
+ppc64_pcrel_linkage1_target (struct frame_info *frame, unsigned int *insn,
e22c64
+			     CORE_ADDR pc)
e22c64
+{
e22c64
+  /* insn[0] is for the std instruction.  */
e22c64
+  CORE_ADDR plt_off = ppc_insn_prefix_dform (insn[1], insn[2]);
e22c64
+
e22c64
+  return ppc64_plt_pcrel_entry_point (frame, plt_off, pc);
e22c64
+}
e22c64
+
e22c64
+static CORE_ADDR
e22c64
+ppc64_pcrel_linkage2_target (struct frame_info *frame, unsigned int *insn,
e22c64
+			     CORE_ADDR pc)
e22c64
+{
e22c64
+  CORE_ADDR plt_off;
e22c64
+
e22c64
+  /* insn[0] is for the std instruction.
e22c64
+     insn[1] is for the  li r11 instruction  */
e22c64
+  plt_off = ppc_insn_prefix_dform (insn[2], insn[3]);
e22c64
+
e22c64
+  return ppc64_plt_pcrel_entry_point (frame, plt_off, pc);
e22c64
+}
e22c64
+
e22c64
 
e22c64
 /* Given that we've begun executing a call trampoline at PC, return
e22c64
    the entry point of the function the trampoline will go to.
e22c64
@@ -447,10 +618,15 @@ ppc64_skip_trampoline_code_1 (struct frame_info *frame, CORE_ADDR pc)
e22c64
 				    ARRAY_SIZE (ppc64_standard_linkage2)),
e22c64
 			       MAX (ARRAY_SIZE (ppc64_standard_linkage3),
e22c64
 				    ARRAY_SIZE (ppc64_standard_linkage4))),
e22c64
-			  MAX (MAX (ARRAY_SIZE (ppc64_standard_linkage5),
e22c64
+		      MAX(MAX (MAX (ARRAY_SIZE (ppc64_standard_linkage5),
e22c64
 				    ARRAY_SIZE (ppc64_standard_linkage6)),
e22c64
 			       MAX (ARRAY_SIZE (ppc64_standard_linkage7),
e22c64
-				    ARRAY_SIZE (ppc64_standard_linkage8))))
e22c64
+				    ARRAY_SIZE (ppc64_standard_linkage8))),
e22c64
+		          MAX (MAX (ARRAY_SIZE (ppc64_standard_linkage9),
e22c64
+				    ARRAY_SIZE (ppc64_standard_linkage10)),
e22c64
+		               MAX (ARRAY_SIZE (ppc64_standard_linkage11),
e22c64
+				    ARRAY_SIZE (ppc64_standard_linkage12)))))
e22c64
+
e22c64
 		     - 1];
e22c64
   CORE_ADDR target;
e22c64
   int scan_limit, i;
e22c64
@@ -463,7 +639,19 @@ ppc64_skip_trampoline_code_1 (struct frame_info *frame, CORE_ADDR pc)
e22c64
 
e22c64
   for (i = 0; i < scan_limit; i++)
e22c64
     {
e22c64
-      if (i < ARRAY_SIZE (ppc64_standard_linkage8) - 1
e22c64
+      if (i < ARRAY_SIZE (ppc64_standard_linkage12) - 1
e22c64
+	  && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage12, insns))
e22c64
+	pc = ppc64_pcrel_linkage1_target (frame, insns, pc);
e22c64
+      else if (i < ARRAY_SIZE (ppc64_standard_linkage11) - 1
e22c64
+	  && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage11, insns))
e22c64
+	pc = ppc64_pcrel_linkage2_target (frame, insns, pc);
e22c64
+      else if (i < ARRAY_SIZE (ppc64_standard_linkage10) - 1
e22c64
+	  && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage10, insns))
e22c64
+	pc = ppc64_pcrel_linkage1_target (frame, insns, pc);
e22c64
+      else if (i < ARRAY_SIZE (ppc64_standard_linkage9) - 1
e22c64
+	  && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage9, insns))
e22c64
+	pc = ppc64_pcrel_linkage1_target (frame, insns, pc);
e22c64
+      else if (i < ARRAY_SIZE (ppc64_standard_linkage8) - 1
e22c64
 	  && ppc_insns_match_pattern (frame, pc, ppc64_standard_linkage8, insns))
e22c64
 	pc = ppc64_standard_linkage4_target (frame, insns);
e22c64
       else if (i < ARRAY_SIZE (ppc64_standard_linkage7) - 1
e22c64
diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c
e22c64
--- a/gdb/rs6000-tdep.c
e22c64
+++ b/gdb/rs6000-tdep.c
e22c64
@@ -7361,6 +7361,14 @@ ppc_insn_ds_field (unsigned int insn)
e22c64
   return ((((CORE_ADDR) insn & 0xfffc) ^ 0x8000) - 0x8000);
e22c64
 }
e22c64
 
e22c64
+CORE_ADDR
e22c64
+ppc_insn_prefix_dform (unsigned int insn1, unsigned int insn2)
e22c64
+{
e22c64
+  /* result is 34-bits  */
e22c64
+  return (CORE_ADDR) ((((insn1 & 0x3ffff) ^ 0x20000) - 0x20000) << 16)
e22c64
+    | (CORE_ADDR)(insn2 & 0xffff);
e22c64
+}
e22c64
+
e22c64
 /* Initialization code.  */
e22c64
 
e22c64
 void _initialize_rs6000_tdep ();