Blame SOURCES/gdb-rhbz1125820-ppc64le-enablement-33of37.patch

01917d
commit 591a12a1d4c8843343eb999145d8bcc1efedf408
01917d
Author: Ulrich Weigand <ulrich.weigand@de.ibm.com>
01917d
Date:   Tue Feb 4 18:44:14 2014 +0100
01917d
01917d
    PowerPC64 ELFv2 ABI: skip global entry point code
01917d
    
01917d
    This patch handles another aspect of the ELFv2 ABI, which unfortunately
01917d
    requires common code changes.
01917d
    
01917d
    In ELFv2, functions may provide both a global and a local entry point.
01917d
    The global entry point (where the function symbol points to) is intended
01917d
    to be used for function-pointer or cross-module (PLT) calls, and requires
01917d
    r12 to be set up to the entry point address itself.   The local entry
01917d
    point (which is found at a fixed offset after the global entry point,
01917d
    as defined by bits in the symbol table entries' st_other field), instead
01917d
    expects r2 to be set up to the current TOC.
01917d
    
01917d
    Now, when setting a breakpoint on a function by name, you really want
01917d
    that breakpoint to trigger either way, no matter whether the function
01917d
    is called via its local or global entry point.  Since the global entry
01917d
    point will always fall through into the local entry point, the way to
01917d
    achieve that is to simply set the breakpoint at the local entry point.
01917d
    
01917d
    One way to do that would be to have prologue parsing skip the code
01917d
    sequence that makes up the global entry point.  Unfortunately, this
01917d
    does not work reliably, since -for optimized code- GDB these days
01917d
    will not actuall invoke the prologue parsing code but instead just
01917d
    set the breakpoint at the symbol address and rely on DWARF being
01917d
    correct at any point throughout the function ...
01917d
    
01917d
    Unfortunately, I don't really see any way to express the notion of
01917d
    local entry points with the current set of gdbarch callbacks.
01917d
    
01917d
    Thus this patch adds a new callback, skip_entrypoint, that is
01917d
    somewhat analogous to skip_prologue, but is called every time
01917d
    GDB needs to determine a function start address, even in those
01917d
    cases where GDB decides to not call skip_prologue.
01917d
    
01917d
    As a side effect, the skip_entrypoint implementation on ppc64
01917d
    does not need to perform any instruction parsing; it can simply
01917d
    rely on the local entry point flags in the symbol table entry.
01917d
    
01917d
    With this implemented, two test cases would still fail to set
01917d
    the breakpoint correctly, but that's because they use the construct:
01917d
    
01917d
     gdb_test "break *hello"
01917d
    
01917d
    Now, using "*hello" explicitly instructs GDB to set the breakpoint
01917d
    at the numerical value of "hello" treated as function pointer, so
01917d
    it will by definition only hit the global entry point.
01917d
    
01917d
    I think this behaviour is unavoidable, but acceptable -- most people
01917d
    do not use this construct, and if they do, they get what they
01917d
    asked for ...
01917d
    
01917d
    In one of those two test cases, use of this construct is really
01917d
    not appropriate.  I think this was added way back when as a means
01917d
    to work around prologue skipping problems on some platforms.  These
01917d
    days that shouldn't really be necessary any more ...
01917d
    
01917d
    For the other (step-bt), we really want to make sure backtracing
01917d
    works on the very first instruction of the routine.  To enable that
01917d
    test also on powerpc64le-linux, we can modify the code to call the
01917d
    test function via function pointer (which makes it use the global
01917d
    entry point in the ELFv2 ABI).
01917d
    
01917d
    gdb/ChangeLog:
01917d
    
01917d
    	* gdbarch.sh (skip_entrypoint): New callback.
01917d
    	* gdbarch.c, gdbarch.h: Regenerate.
01917d
    	* symtab.c (skip_prologue_sal): Call gdbarch_skip_entrypoint.
01917d
    	* infrun.c (fill_in_stop_func): Likewise.
01917d
    	* ppc-linux-tdep.c: Include "elf/ppc64.h".
01917d
    	(ppc_elfv2_elf_make_msymbol_special): New function.
01917d
    	(ppc_elfv2_skip_entrypoint): Likewise.
01917d
    	(ppc_linux_init_abi): Install them for ELFv2.
01917d
    
01917d
    gdb/testsuite/ChangeLog:
01917d
    
01917d
    	* gdb.base/sigbpt.exp: Do not use "*" when setting breakpoint
01917d
    	on a function.
01917d
    	* gdb.base/step-bt.c: Call hello via function pointer to make
01917d
    	sure its first instruction is executed on powerpc64le-linux.
01917d
01917d
Index: gdb-7.6.1/gdb/gdbarch.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbarch.c
01917d
+++ gdb-7.6.1/gdb/gdbarch.c
01917d
@@ -200,6 +200,7 @@ struct gdbarch
01917d
   gdbarch_return_in_first_hidden_param_p_ftype *return_in_first_hidden_param_p;
01917d
   gdbarch_skip_prologue_ftype *skip_prologue;
01917d
   gdbarch_skip_main_prologue_ftype *skip_main_prologue;
01917d
+  gdbarch_skip_entrypoint_ftype *skip_entrypoint;
01917d
   gdbarch_inner_than_ftype *inner_than;
01917d
   gdbarch_breakpoint_from_pc_ftype *breakpoint_from_pc;
01917d
   gdbarch_remote_breakpoint_from_pc_ftype *remote_breakpoint_from_pc;
01917d
@@ -371,6 +372,7 @@ struct gdbarch startup_gdbarch =
01917d
   default_return_in_first_hidden_param_p,  /* return_in_first_hidden_param_p */
01917d
   0,  /* skip_prologue */
01917d
   0,  /* skip_main_prologue */
01917d
+  0,  /* skip_entrypoint */
01917d
   0,  /* inner_than */
01917d
   0,  /* breakpoint_from_pc */
01917d
   default_remote_breakpoint_from_pc,  /* remote_breakpoint_from_pc */
01917d
@@ -672,6 +674,7 @@ verify_gdbarch (struct gdbarch *gdbarch)
01917d
   if (gdbarch->skip_prologue == 0)
01917d
     fprintf_unfiltered (log, "\n\tskip_prologue");
01917d
   /* Skip verify of skip_main_prologue, has predicate.  */
01917d
+  /* Skip verify of skip_entrypoint, has predicate.  */
01917d
   if (gdbarch->inner_than == 0)
01917d
     fprintf_unfiltered (log, "\n\tinner_than");
01917d
   if (gdbarch->breakpoint_from_pc == 0)
01917d
@@ -1285,6 +1288,12 @@ gdbarch_dump (struct gdbarch *gdbarch, s
01917d
                       "gdbarch_dump: single_step_through_delay = <%s>\n",
01917d
                       host_address_to_string (gdbarch->single_step_through_delay));
01917d
   fprintf_unfiltered (file,
01917d
+                      "gdbarch_dump: gdbarch_skip_entrypoint_p() = %d\n",
01917d
+                      gdbarch_skip_entrypoint_p (gdbarch));
01917d
+  fprintf_unfiltered (file,
01917d
+                      "gdbarch_dump: skip_entrypoint = <%s>\n",
01917d
+                      host_address_to_string (gdbarch->skip_entrypoint));
01917d
+  fprintf_unfiltered (file,
01917d
                       "gdbarch_dump: gdbarch_skip_main_prologue_p() = %d\n",
01917d
                       gdbarch_skip_main_prologue_p (gdbarch));
01917d
   fprintf_unfiltered (file,
01917d
@@ -2635,6 +2644,30 @@ set_gdbarch_skip_main_prologue (struct g
01917d
 }
01917d
 
01917d
 int
01917d
+gdbarch_skip_entrypoint_p (struct gdbarch *gdbarch)
01917d
+{
01917d
+  gdb_assert (gdbarch != NULL);
01917d
+  return gdbarch->skip_entrypoint != NULL;
01917d
+}
01917d
+
01917d
+CORE_ADDR
01917d
+gdbarch_skip_entrypoint (struct gdbarch *gdbarch, CORE_ADDR ip)
01917d
+{
01917d
+  gdb_assert (gdbarch != NULL);
01917d
+  gdb_assert (gdbarch->skip_entrypoint != NULL);
01917d
+  if (gdbarch_debug >= 2)
01917d
+    fprintf_unfiltered (gdb_stdlog, "gdbarch_skip_entrypoint called\n");
01917d
+  return gdbarch->skip_entrypoint (gdbarch, ip);
01917d
+}
01917d
+
01917d
+void
01917d
+set_gdbarch_skip_entrypoint (struct gdbarch *gdbarch,
01917d
+                             gdbarch_skip_entrypoint_ftype skip_entrypoint)
01917d
+{
01917d
+  gdbarch->skip_entrypoint = skip_entrypoint;
01917d
+}
01917d
+
01917d
+int
01917d
 gdbarch_inner_than (struct gdbarch *gdbarch, CORE_ADDR lhs, CORE_ADDR rhs)
01917d
 {
01917d
   gdb_assert (gdbarch != NULL);
01917d
Index: gdb-7.6.1/gdb/gdbarch.h
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbarch.h
01917d
+++ gdb-7.6.1/gdb/gdbarch.h
01917d
@@ -487,6 +487,24 @@ typedef CORE_ADDR (gdbarch_skip_main_pro
01917d
 extern CORE_ADDR gdbarch_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR ip);
01917d
 extern void set_gdbarch_skip_main_prologue (struct gdbarch *gdbarch, gdbarch_skip_main_prologue_ftype *skip_main_prologue);
01917d
 
01917d
+/* On some platforms, a single function may provide multiple entry points,
01917d
+   e.g. one that is used for function-pointer calls and a different one
01917d
+   that is used for direct function calls.
01917d
+   In order to ensure that breakpoints set on the function will trigger
01917d
+   no matter via which entry point the function is entered, a platform
01917d
+   may provide the skip_entrypoint callback.  It is called with IP set
01917d
+   to the main entry point of a function (as determined by the symbol table),
01917d
+   and should return the address of the innermost entry point, where the
01917d
+   actual breakpoint needs to be set.  Note that skip_entrypoint is used
01917d
+   by GDB common code even when debugging optimized code, where skip_prologue
01917d
+   is not used. */
01917d
+
01917d
+extern int gdbarch_skip_entrypoint_p (struct gdbarch *gdbarch);
01917d
+
01917d
+typedef CORE_ADDR (gdbarch_skip_entrypoint_ftype) (struct gdbarch *gdbarch, CORE_ADDR ip);
01917d
+extern CORE_ADDR gdbarch_skip_entrypoint (struct gdbarch *gdbarch, CORE_ADDR ip);
01917d
+extern void set_gdbarch_skip_entrypoint (struct gdbarch *gdbarch, gdbarch_skip_entrypoint_ftype *skip_entrypoint);
01917d
+
01917d
 typedef int (gdbarch_inner_than_ftype) (CORE_ADDR lhs, CORE_ADDR rhs);
01917d
 extern int gdbarch_inner_than (struct gdbarch *gdbarch, CORE_ADDR lhs, CORE_ADDR rhs);
01917d
 extern void set_gdbarch_inner_than (struct gdbarch *gdbarch, gdbarch_inner_than_ftype *inner_than);
01917d
Index: gdb-7.6.1/gdb/gdbarch.sh
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbarch.sh
01917d
+++ gdb-7.6.1/gdb/gdbarch.sh
01917d
@@ -527,6 +527,19 @@ m:int:return_in_first_hidden_param_p:str
01917d
 
01917d
 m:CORE_ADDR:skip_prologue:CORE_ADDR ip:ip:0:0
01917d
 M:CORE_ADDR:skip_main_prologue:CORE_ADDR ip:ip
01917d
+# On some platforms, a single function may provide multiple entry points,
01917d
+# e.g. one that is used for function-pointer calls and a different one
01917d
+# that is used for direct function calls.
01917d
+# In order to ensure that breakpoints set on the function will trigger
01917d
+# no matter via which entry point the function is entered, a platform
01917d
+# may provide the skip_entrypoint callback.  It is called with IP set
01917d
+# to the main entry point of a function (as determined by the symbol table),
01917d
+# and should return the address of the innermost entry point, where the
01917d
+# actual breakpoint needs to be set.  Note that skip_entrypoint is used
01917d
+# by GDB common code even when debugging optimized code, where skip_prologue
01917d
+# is not used.
01917d
+M:CORE_ADDR:skip_entrypoint:CORE_ADDR ip:ip
01917d
+
01917d
 f:int:inner_than:CORE_ADDR lhs, CORE_ADDR rhs:lhs, rhs:0:0
01917d
 m:const gdb_byte *:breakpoint_from_pc:CORE_ADDR *pcptr, int *lenptr:pcptr, lenptr::0:
01917d
 # Return the adjusted address and kind to use for Z0/Z1 packets.
01917d
Index: gdb-7.6.1/gdb/infrun.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/infrun.c
01917d
+++ gdb-7.6.1/gdb/infrun.c
01917d
@@ -3162,6 +3162,10 @@ fill_in_stop_func (struct gdbarch *gdbar
01917d
       ecs->stop_func_start
01917d
 	+= gdbarch_deprecated_function_start_offset (gdbarch);
01917d
 
01917d
+      if (gdbarch_skip_entrypoint_p (gdbarch))
01917d
+	ecs->stop_func_start = gdbarch_skip_entrypoint (gdbarch,
01917d
+							ecs->stop_func_start);
01917d
+
01917d
       ecs->stop_func_filled_in = 1;
01917d
     }
01917d
 }
01917d
Index: gdb-7.6.1/gdb/ppc-linux-tdep.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/ppc-linux-tdep.c
01917d
+++ gdb-7.6.1/gdb/ppc-linux-tdep.c
01917d
@@ -44,6 +44,7 @@
01917d
 #include "observer.h"
01917d
 #include "auxv.h"
01917d
 #include "elf/common.h"
01917d
+#include "elf/ppc64.h"
01917d
 #include "exceptions.h"
01917d
 #include "arch-utils.h"
01917d
 #include "spu-tdep.h"
01917d
@@ -875,6 +876,55 @@ ppc_linux_core_read_description (struct
01917d
     }
01917d
 }
01917d
 
01917d
+
01917d
+/* Implementation of `gdbarch_elf_make_msymbol_special', as defined in
01917d
+   gdbarch.h.  This implementation is used for the ELFv2 ABI only.  */
01917d
+
01917d
+static void
01917d
+ppc_elfv2_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
01917d
+{
01917d
+  elf_symbol_type *elf_sym = (elf_symbol_type *)sym;
01917d
+
01917d
+  /* If the symbol is marked as having a local entry point, set a target
01917d
+     flag in the msymbol.  We currently only support local entry point
01917d
+     offsets of 8 bytes, which is the only entry point offset ever used
01917d
+     by current compilers.  If/when other offsets are ever used, we will
01917d
+     have to use additional target flag bits to store them.  */
01917d
+  switch (PPC64_LOCAL_ENTRY_OFFSET (elf_sym->internal_elf_sym.st_other))
01917d
+    {
01917d
+    default:
01917d
+      break;
01917d
+    case 8:
01917d
+      MSYMBOL_TARGET_FLAG_1 (msym) = 1;
01917d
+      break;
01917d
+    }
01917d
+}
01917d
+
01917d
+/* Implementation of `gdbarch_skip_entrypoint', as defined in
01917d
+   gdbarch.h.  This implementation is used for the ELFv2 ABI only.  */
01917d
+
01917d
+static CORE_ADDR
01917d
+ppc_elfv2_skip_entrypoint (struct gdbarch *gdbarch, CORE_ADDR pc)
01917d
+{
01917d
+  struct minimal_symbol *fun;
01917d
+  int local_entry_offset = 0;
01917d
+
01917d
+  fun = lookup_minimal_symbol_by_pc (pc);
01917d
+  if (fun == NULL)
01917d
+    return pc;
01917d
+
01917d
+  /* See ppc_elfv2_elf_make_msymbol_special for how local entry point
01917d
+     offset values are encoded.  */
01917d
+  if (MSYMBOL_TARGET_FLAG_1 (fun))
01917d
+    local_entry_offset = 8;
01917d
+
01917d
+  if (SYMBOL_VALUE_ADDRESS (fun) <= pc
01917d
+      && pc < SYMBOL_VALUE_ADDRESS (fun) + local_entry_offset)
01917d
+    return SYMBOL_VALUE_ADDRESS (fun) + local_entry_offset;
01917d
+
01917d
+  return pc;
01917d
+}
01917d
+
01917d
 /* Implementation of `gdbarch_stap_is_single_operand', as defined in
01917d
    gdbarch.h.  */
01917d
 
01917d
@@ -1341,6 +1391,13 @@ ppc_linux_init_abi (struct gdbarch_info
01917d
 	  set_gdbarch_elf_make_msymbol_special
01917d
 	    (gdbarch, ppc64_elf_make_msymbol_special);
01917d
 	}
01917d
+      else
01917d
+	{
01917d
+	  set_gdbarch_elf_make_msymbol_special
01917d
+	    (gdbarch, ppc_elfv2_elf_make_msymbol_special);
01917d
+
01917d
+	  set_gdbarch_skip_entrypoint (gdbarch, ppc_elfv2_skip_entrypoint);
01917d
+	}
01917d
 
01917d
       /* Shared library handling.  */
01917d
       set_gdbarch_skip_trampoline_code (gdbarch, ppc64_skip_trampoline_code);
01917d
Index: gdb-7.6.1/gdb/symtab.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/symtab.c
01917d
+++ gdb-7.6.1/gdb/symtab.c
01917d
@@ -2872,6 +2872,8 @@ skip_prologue_sal (struct symtab_and_lin
01917d
 
01917d
       /* Skip "first line" of function (which is actually its prologue).  */
01917d
       pc += gdbarch_deprecated_function_start_offset (gdbarch);
01917d
+      if (gdbarch_skip_entrypoint_p (gdbarch))
01917d
+        pc = gdbarch_skip_entrypoint (gdbarch, pc);
01917d
       if (skip)
01917d
 	pc = gdbarch_skip_prologue (gdbarch, pc);
01917d
 
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.base/sigbpt.exp
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/testsuite/gdb.base/sigbpt.exp
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.base/sigbpt.exp
01917d
@@ -82,7 +82,7 @@ gdb_test "break keeper"
01917d
 set bowler_addrs bowler
01917d
 set segv_addr none
01917d
 gdb_test {display/i $pc}
01917d
-gdb_test "advance *bowler" "bowler.*" "advance to the bowler"
01917d
+gdb_test "advance bowler" "bowler.*" "advance to the bowler"
01917d
 set test "stepping to fault"
01917d
 set signame "SIGSEGV"
01917d
 gdb_test_multiple "stepi" "$test" {
01917d
Index: gdb-7.6.1/gdb/testsuite/gdb.base/step-bt.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/testsuite/gdb.base/step-bt.c
01917d
+++ gdb-7.6.1/gdb/testsuite/gdb.base/step-bt.c
01917d
@@ -23,10 +23,19 @@ hello (void)
01917d
   printf ("Hello world.\n");
01917d
 }
01917d
 
01917d
+/* The test case uses "break *hello" to make sure to step at the very
01917d
+   first instruction of the function.  This causes a problem running
01917d
+   the test on powerpc64le-linux, since the first instruction belongs
01917d
+   to the global entry point prologue, which is skipped when doing a
01917d
+   local direct function call.  To make sure that first instruction is
01917d
+   indeed being executed and the breakpoint hits, we make sure to call
01917d
+   the routine via an indirect call.  */
01917d
+void (*ptr) (void) = hello;
01917d
+
01917d
 int
01917d
 main (void)
01917d
 {
01917d
-  hello ();
01917d
+  ptr ();
01917d
 
01917d
   return 0;
01917d
 }