[v4,2/4] gdb/symtab: add lookup for trampoline functions

Message ID 20230801224744.24433-3-abdul.b.ijaz@intel.com
State New
Headers
Series GDB support for DW_AT_trampoline |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed

Commit Message

Abdul Basit Ijaz Aug. 1, 2023, 10:47 p.m. UTC
  From: Nils-Christian Kempke <nils-christian.kempke@intel.com>

In order to query information about the DW_AT_trampoline tag for
subroutines and inlined subroutines, two function were added to symtab.

First, a routine for querying whether the given pc belongs to a block
that is associated with a function (maybe inlined) marked
DW_AT_trampoline.

Second, a routine for querying a trampoline function's target.
Subroutines and inlined subroutines marked with DW_AT_trampoline usually
contain information about the target subroutine they are 'wrapping'/
passing control to.

2023-08-01 Nils-Christian Kempke <nils-christian.kempke@intel.com>
---
 gdb/symtab.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/symtab.h | 14 +++++++++++
 2 files changed, 82 insertions(+)
  

Comments

Tom Tromey Aug. 2, 2023, 8:18 p.m. UTC | #1
>>>>> "Abdul" == Abdul Basit Ijaz via Gdb-patches <gdb-patches@sourceware.org> writes:

Abdul> From: Nils-Christian Kempke <nils-christian.kempke@intel.com>
Abdul> In order to query information about the DW_AT_trampoline tag for
Abdul> subroutines and inlined subroutines, two function were added to symtab.

Thanks for the patch.

Abdul> +CORE_ADDR
Abdul> +find_function_trampoline_target (CORE_ADDR pc)
Abdul> +{
...
Abdul> +      else if (trampoline->target_kind () == TRAMPOLINE_TARGET_PHYSADDR)
Abdul> +	{
Abdul> +	  /* If the function symbol containing this trampoline target has
Abdul> +	     been relocated we assume the target_address also needs relocation.
Abdul> +	     If it has not been relocated the offset should be zero.  */
Abdul> +	  target_address
Abdul> +	    = (trampoline->target_physaddr ()
Abdul> +	       + sym->objfile ()->section_offsets[sym->section_index ()]);

First, I'm all in favor of storing unrelocated addresses in data
structures and then relocating them at point of use.

However, in this case, I think the stored address is already relocated.
From the previous patch:

+	  unrelocated_addr target_addr_reloc = attr->as_address ();
+	  CORE_ADDR target_addr
+	    = cu->per_objfile->relocate (target_addr_reloc);
+	  target_addr = gdbarch_adjust_dwarf2_addr (objfile->arch (),
+						    target_addr);
+	  TYPE_TRAMPOLINE_TARGET (ftype)->set_target_physaddr (target_addr);

... this brings up another issue, though -- if a relocated address is
stored, then it must be relocated by objfile_relocate.

So perhaps storing the unrelocated address here really would be better.
This can be done using 'adjust' rather than 'relocate' in the first
patch, and by using the unrelocated_addr type rather than CORE_ADDR for
storage.

Tom
  
Terekhov, Mikhail via Gdb-patches Aug. 7, 2023, 7:40 a.m. UTC | #2
Hi Tom,

Thanks for the feedback.

Tom> This can be done using 'adjust' rather than 'relocate' in the first patch, and by using the unrelocated_addr type rather than CORE_ADDR for storage.

Will be updated accordingly in V5 patch series.

Thanks & Best Regards
Abdul Basit

-----Original Message-----
From: Tom Tromey <tom@tromey.com> 
Sent: Wednesday, August 2, 2023 10:18 PM
To: Abdul Basit Ijaz via Gdb-patches <gdb-patches@sourceware.org>
Cc: Ijaz, Abdul B <abdul.b.ijaz@intel.com>; JiniSusan.George@amd.com; tom@tromey.com; eliz@gnu.org; blarsen@redhat.com; Nils-Christian Kempke <nils-christian.kempke@intel.com>
Subject: Re: [PATCH v4 2/4] gdb/symtab: add lookup for trampoline functions

>>>>> "Abdul" == Abdul Basit Ijaz via Gdb-patches <gdb-patches@sourceware.org> writes:

Abdul> From: Nils-Christian Kempke <nils-christian.kempke@intel.com> In 
Abdul> order to query information about the DW_AT_trampoline tag for 
Abdul> subroutines and inlined subroutines, two function were added to symtab.

Thanks for the patch.

Abdul> +CORE_ADDR
Abdul> +find_function_trampoline_target (CORE_ADDR pc) {
...
Abdul> +      else if (trampoline->target_kind () == TRAMPOLINE_TARGET_PHYSADDR)
Abdul> +	{
Abdul> +	  /* If the function symbol containing this trampoline target has
Abdul> +	     been relocated we assume the target_address also needs relocation.
Abdul> +	     If it has not been relocated the offset should be zero.  */
Abdul> +	  target_address
Abdul> +	    = (trampoline->target_physaddr ()
Abdul> +	       + sym->objfile ()->section_offsets[sym->section_index 
Abdul> +()]);

First, I'm all in favor of storing unrelocated addresses in data structures and then relocating them at point of use.

However, in this case, I think the stored address is already relocated.
From the previous patch:

+	  unrelocated_addr target_addr_reloc = attr->as_address ();
+	  CORE_ADDR target_addr
+	    = cu->per_objfile->relocate (target_addr_reloc);
+	  target_addr = gdbarch_adjust_dwarf2_addr (objfile->arch (),
+						    target_addr);
+	  TYPE_TRAMPOLINE_TARGET (ftype)->set_target_physaddr (target_addr);

... this brings up another issue, though -- if a relocated address is stored, then it must be relocated by objfile_relocate.

So perhaps storing the unrelocated address here really would be better.
This can be done using 'adjust' rather than 'relocate' in the first patch, and by using the unrelocated_addr type rather than CORE_ADDR for storage.

Tom
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
  

Patch

diff --git a/gdb/symtab.c b/gdb/symtab.c
index 0117a2a59d7..745fd7d5d25 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -73,6 +73,7 @@ 
 #include "gdbsupport/gdb_string_view.h"
 #include "gdbsupport/pathstuff.h"
 #include "gdbsupport/common-utils.h"
+#include "gdbsupport/symbol.h"
 
 /* Forward declarations for local functions.  */
 
@@ -4051,6 +4052,73 @@  find_function_alias_target (bound_minimal_symbol msymbol)
   return NULL;
 }
 
+/* See symtab.h.  */
+
+bool
+in_trampoline_function (CORE_ADDR pc)
+{
+  /* Find the innermost function containing pc.  This might be an inlined
+     function.  */
+  symbol *sym = find_pc_sect_containing_function (pc,
+						  find_pc_mapped_section (pc));
+  return sym != nullptr && TYPE_IS_TRAMPOLINE (sym->type ());
+}
+
+/* See symtab.h.  */
+
+CORE_ADDR
+find_function_trampoline_target (CORE_ADDR pc)
+{
+  /* Find the innermost function containing pc.  This might be an inlined
+     function.  */
+  symbol *sym = find_pc_sect_containing_function (pc,
+						  find_pc_mapped_section (pc));
+  CORE_ADDR target_address = 0;
+
+  if (sym != nullptr && TYPE_IS_TRAMPOLINE (sym->type ()))
+    {
+      trampoline_target *trampoline = TYPE_TRAMPOLINE_TARGET (sym->type ());
+
+      /* DW_AT_trampoline can be given as an address, name, or flag here (die
+	 references have been resolved as names at this point.  In the case
+	 where DW_AT_trampoline contains a flag we do not know the target
+	 address and return 0.  */
+      if (trampoline->target_kind () == TRAMPOLINE_TARGET_NAME)
+	{
+	  /* Handle both the mangled and demangled PHYSNAME.  */
+	  const char *physname = trampoline->target_name ();
+
+	  /* First, check whether there exists a symbol matching the
+	     physname.  If we cannot find one also check for minimal
+	     symbols.  */
+	  const block *blk = block_for_pc (pc);
+	  struct block_symbol bs = lookup_symbol (physname, blk, VAR_DOMAIN, 0);
+	  if (bs.symbol != nullptr)
+	    {
+	      const struct block *block = bs.symbol->value_block ();
+	      gdb_assert (block != nullptr);
+	      target_address = block->start ();
+	    }
+	  else
+	    {
+	      if (find_minimal_symbol_address (physname, &target_address,
+					       nullptr) != 0)
+		target_address = 0;
+	    }
+	}
+      else if (trampoline->target_kind () == TRAMPOLINE_TARGET_PHYSADDR)
+	{
+	  /* If the function symbol containing this trampoline target has
+	     been relocated we assume the target_address also needs relocation.
+	     If it has not been relocated the offset should be zero.  */
+	  target_address
+	    = (trampoline->target_physaddr ()
+	       + sym->objfile ()->section_offsets[sym->section_index ()]);
+	}
+    }
+
+  return target_address;
+}
 
 /* If P is of the form "operator[ \t]+..." where `...' is
    some legitimate operator text, return a pointer to the
diff --git a/gdb/symtab.h b/gdb/symtab.h
index ee4729b14cd..fc2d82e6567 100644
--- a/gdb/symtab.h
+++ b/gdb/symtab.h
@@ -2312,6 +2312,20 @@  extern const struct gnu_ifunc_fns *gnu_ifunc_fns_p;
 
 extern CORE_ADDR find_solib_trampoline_target (frame_info_ptr, CORE_ADDR);
 
+/* Return whether or not the current pc is within a block that belongs to a
+   function that is marked as a trampoline by the compiler.  */
+
+extern bool in_trampoline_function (CORE_ADDR pc);
+
+/* Find the target of a trampoline function marked via the DW_AT_trampoline
+   attribute and return its address.  Returns 0 if the pc is not contained
+   in a trampoline function (inlined or not).  If DW_AT_trampoline
+   is given as a flag, the target is unknown and the function will still return
+   0.  One has to additionally query in_trampoline_function to cover this
+   case.  */
+
+extern CORE_ADDR find_function_trampoline_target (CORE_ADDR pc);
+
 struct symtab_and_line
 {
   /* The program space of this sal.  */