Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Mark Kettenis <mark.kettenis@xs4all.nl>
To: gdb-patches@sourceware.org
Subject: [RFC] i386 PLT stub unwinder
Date: Mon, 13 Jun 2011 15:56:00 -0000	[thread overview]
Message-ID: <201106131555.p5DFtucc019690@glazunov.sibelius.xs4all.nl> (raw)

Jan's mail about DWARF CFI for PLT stubs prompted me to write an
undinder for the PLT stubs as defined by the i386 ABI.  With this
change I can step through the PLT stubs and always have a proper
backtrace.

The reason that this is an RFC, is that I'm a little bit confused
about the TRY_CATCH stuff that was introduced in some of the i386
unwinders by Pedro.  It isn't entirely clear to me when that is
needed.  Unwinding the PC should always succeed at this point, since
the sniffers already rely on that.  And unwinding the stack pointer
needs to be working as well, otherwise unwinding the PC will never
have worked.  So I don't think I need to worry about unavailable
registers here.


2011-06-12  Mark Kettenis  <kettenis@gnu.org>
 
	* i386-tdep.c (i386_plt0_stub_insns, i386_plt_stub_insns)
	(i386_pic_plt0_stub_insns, i386_pic_plt_stub_insns): New
	variables.
	(i386_plt_stub_frame_cache, i386_plt_stub_frame_this_id)
	(i386_plt_stub_frame_prev_register, i386_plt_stub_frame_sniffer):
	New functions.
	(i386_plt_stub_frame_unwind): New variable.
	(i386_elf_init_abi): Append PLT stub unwinder.

Index: i386-tdep.c
===================================================================
RCS file: /cvs/src/src/gdb/i386-tdep.c,v
retrieving revision 1.334
diff -u -p -r1.334 i386-tdep.c
--- i386-tdep.c	12 Jun 2011 20:46:11 -0000	1.334
+++ i386-tdep.c	13 Jun 2011 15:43:26 -0000
@@ -2070,6 +2070,200 @@ static const struct frame_unwind i386_st
 };
 \f
 
+/* PLT stubs.  */
+
+struct i386_insn i386_plt0_stub_insns[] =
+{
+  /* `pushl imm32' */
+  { 6, { 0xff, 0x35 }, { 0xff, 0xff } },
+
+  /* `jmp *disp32' */
+  { 6, { 0xff, 0x25 }, { 0xff, 0xff } },
+
+  {0}
+};
+
+struct i386_insn i386_plt_stub_insns[] =
+{
+  /* `jmp *disp32' */
+  { 6, { 0xff, 0x25 }, { 0xff, 0xff } },
+
+  /* `pushl imm32' */
+  { 5, { 0x68 }, { 0xff } },
+
+  /* `jmp rel32' */
+  { 5, { 0xe9 }, { 0xff } },
+
+  {0}
+};
+
+struct i386_insn i386_pic_plt0_stub_insns[] =
+{
+  /* `pushl imm32' */
+  { 6, { 0xff, 0xb3, 4, 0, 0, 0 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } },
+
+  /* `jmp *imm32' */
+  { 6, { 0xff, 0xa3, 8, 0, 0, 0 }, { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } },
+
+  {0}
+};
+
+struct i386_insn i386_pic_plt_stub_insns[] =
+{
+  /* `jmp *disp32(%ebx)' */
+  { 6, { 0xff, 0xa3 }, { 0xff, 0xff } },
+
+  /* `pushl imm32' */
+  { 5, { 0x68 }, { 0xff } },
+
+  /* `jmp rel32' */
+  { 5, { 0xe9 }, { 0xff } },
+
+  {0}
+};
+
+static struct i386_frame_cache *
+i386_plt_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
+{
+  struct i386_frame_cache *cache;
+  struct i386_insn *insn;
+  LONGEST sp_offset = -4;
+  CORE_ADDR pc, sp;
+  int i;
+
+  if (*this_cache)
+    return *this_cache;
+
+  cache = i386_alloc_frame_cache ();
+  *this_cache = cache;
+
+  pc = get_frame_pc (this_frame);
+  if (i386_match_insn_block (pc, i386_plt_stub_insns))
+    {
+      insn = i386_match_insn (pc, i386_plt_stub_insns);
+
+      for (i = 0; i < ARRAY_SIZE(i386_plt_stub_insns); i++)
+	{
+	  if (insn == &i386_plt_stub_insns[i])
+	    break;
+
+	  pc -= i386_plt_stub_insns[i].len;
+	}
+
+      if (insn == &i386_plt_stub_insns[2])
+	cache->sp_offset = 0;
+    }
+  else if (i386_match_insn_block (pc, i386_plt0_stub_insns))
+    {
+      insn = i386_match_insn (pc, i386_plt0_stub_insns);
+
+      for (i = 0; i < ARRAY_SIZE(i386_plt0_stub_insns); i++)
+	{
+	  if (insn == &i386_plt0_stub_insns[i])
+	    break;
+
+	  pc -= i386_plt0_stub_insns[i].len;
+	}
+
+      if (insn == &i386_plt0_stub_insns[0])
+	cache->sp_offset = 0;
+      else
+	cache->sp_offset = 4;
+    }
+  else if (i386_match_insn_block (pc, i386_pic_plt_stub_insns))
+    {
+      insn = i386_match_insn (pc, i386_pic_plt_stub_insns);
+
+      for (i = 0; i < ARRAY_SIZE(i386_pic_plt_stub_insns); i++)
+	{
+	  if (insn == &i386_pic_plt_stub_insns[i])
+	    break;
+
+	  pc -= i386_pic_plt_stub_insns[i].len;
+	}
+
+      if (insn == &i386_pic_plt_stub_insns[2])
+	cache->sp_offset = 0;
+    }
+  else if (i386_match_insn_block (pc, i386_pic_plt0_stub_insns))
+    {
+      insn = i386_match_insn (pc, i386_pic_plt0_stub_insns);
+
+      for (i = 0; i < ARRAY_SIZE(i386_pic_plt0_stub_insns); i++)
+	{
+	  if (insn == &i386_pic_plt0_stub_insns[i])
+	    break;
+
+	  pc -= i386_pic_plt0_stub_insns[i].len;
+	}
+
+      if (insn == &i386_pic_plt0_stub_insns[0])
+	cache->sp_offset = 0;
+      else
+	cache->sp_offset = 4;
+    }
+
+  cache->pc = pc;
+
+  sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
+  cache->base = sp + cache->sp_offset;
+  cache->saved_sp = cache->base + 8;
+  cache->saved_regs[I386_EIP_REGNUM] = cache->base + 4;
+
+  cache->base_p = 1;
+  return cache;
+}
+
+static void
+i386_plt_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
+			     struct frame_id *this_id)
+{
+  struct i386_frame_cache *cache =
+    i386_plt_stub_frame_cache (this_frame, this_cache);
+
+  /* See the end of i386_push_dummy_call.  */
+  (*this_id) = frame_id_build (cache->base + 8, cache->pc);
+}
+
+static struct value *
+i386_plt_stub_frame_prev_register (struct frame_info *this_frame,
+				   void **this_cache, int regnum)
+{
+  /* Make sure we've initialized the cache.  */
+  i386_plt_stub_frame_cache (this_frame, this_cache);
+
+  return i386_frame_prev_register (this_frame, this_cache, regnum);
+}
+
+static int
+i386_plt_stub_frame_sniffer (const struct frame_unwind *self,
+			     struct frame_info *this_frame, void **this_cache)
+{
+  CORE_ADDR pc = get_frame_pc (this_frame);
+
+  if (!in_plt_section(pc, NULL))
+    return 0;
+
+  if (!i386_match_insn_block (pc, i386_plt_stub_insns)
+      && !i386_match_insn_block (pc, i386_plt0_stub_insns)
+      && !i386_match_insn_block (pc, i386_pic_plt_stub_insns)
+      && !i386_match_insn_block (pc, i386_pic_plt0_stub_insns))
+    return 0;
+
+  return 1;
+}
+
+static const struct frame_unwind i386_plt_stub_frame_unwind =
+{
+  NORMAL_FRAME,
+  default_frame_unwind_stop_reason,
+  i386_plt_stub_frame_this_id,
+  i386_plt_stub_frame_prev_register,
+  NULL,
+  i386_plt_stub_frame_sniffer
+};
+\f
+
 /* Signal trampolines.  */
 
 static struct i386_frame_cache *
@@ -3305,6 +3499,8 @@ i386_elf_init_abi (struct gdbarch_info i
 {
   /* We typically use stabs-in-ELF with the SVR4 register numbering.  */
   set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
+
+  frame_unwind_append_unwinder (gdbarch, &i386_plt_stub_frame_unwind);
 }
 
 /* System V Release 4 (SVR4).  */


             reply	other threads:[~2011-06-13 15:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-13 15:56 Mark Kettenis [this message]
2011-06-13 16:14 ` Jan Kratochvil
2011-06-13 16:32   ` Joel Brobecker
2011-06-13 16:43   ` Tom Tromey
2011-06-13 19:15     ` Mark Kettenis
2011-06-14 11:49 ` Pedro Alves
2011-06-14 11:53   ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201106131555.p5DFtucc019690@glazunov.sibelius.xs4all.nl \
    --to=mark.kettenis@xs4all.nl \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox