Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Muhammad Kamran <muhammad.kamran@arm.com>
To: <gdb-patches@sourceware.org>
Cc: Andrew Burgess <aburgess@redhat.com>,
	Wilco Dijkstra <Wilco.Dijkstra@arm.com>,
	Yury Khrustalev <Yury.Khrustalev@arm.com>,
	"Thiago Jung Bauermann" <thiago.bauermann@linaro.org>,
	Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>,
	Carlos O'Donell <carlos@redhat.com>,
	Muhammad Kamran <muhammad.kamran@arm.com>
Subject: [PATCH v2 1/1] gdb: Preserve IFUNC marker when finding inferior functions
Date: Thu, 25 Jun 2026 15:20:36 +0000	[thread overview]
Message-ID: <20260625152036.6149-2-muhammad.kamran@arm.com> (raw)
In-Reply-To: <20260625152036.6149-1-muhammad.kamran@arm.com>

GDB calls find_function_in_inferior ("malloc") when expression
evaluation needs to allocate memory in the inferior, e.g. for string
literal arguments.

The minimal-symbol fallback created a synthetic ordinary function
pointer from msymbol.value_address ().  If the symbol was a GNU IFUNC,
this discarded the IFUNC marker, so call_function_by_hand did not
resolve the symbol before calling it.

Use find_minsym_type_and_address to classify the minimal symbol and
propagate the GNU IFUNC marker to the synthetic function type.  This
keeps the existing fallback return type while allowing inferior calls
through IFUNC symbols to be resolved correctly.
---
 .../gdb.base/gnu-ifunc-inferior-call-malloc.c | 27 +++++++++++
 .../gdb.base/gnu-ifunc-inferior-call.c        | 45 +++++++++++++++++++
 gdb/testsuite/gdb.base/gnu-ifunc.exp          | 40 +++++++++++++++++
 gdb/valops.c                                  |  6 ++-
 4 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 gdb/testsuite/gdb.base/gnu-ifunc-inferior-call-malloc.c
 create mode 100644 gdb/testsuite/gdb.base/gnu-ifunc-inferior-call.c

diff --git a/gdb/testsuite/gdb.base/gnu-ifunc-inferior-call-malloc.c b/gdb/testsuite/gdb.base/gnu-ifunc-inferior-call-malloc.c
new file mode 100644
index 00000000000..2547fee20a9
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gnu-ifunc-inferior-call-malloc.c
@@ -0,0 +1,27 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stddef.h>
+
+extern void *dummy_malloc (size_t size);
+
+static void *(*resolve_malloc (void)) (size_t)
+{
+  return dummy_malloc;
+}
+
+void *malloc (size_t size) __attribute__ ((ifunc ("resolve_malloc")));
diff --git a/gdb/testsuite/gdb.base/gnu-ifunc-inferior-call.c b/gdb/testsuite/gdb.base/gnu-ifunc-inferior-call.c
new file mode 100644
index 00000000000..3b70811b69b
--- /dev/null
+++ b/gdb/testsuite/gdb.base/gnu-ifunc-inferior-call.c
@@ -0,0 +1,45 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2026 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stddef.h>
+
+static char arena[32];
+const char *str;
+
+int
+str_in_arena (void)
+{
+  return str == arena;
+}
+
+void *
+dummy_malloc (size_t size)
+{
+  return arena;
+}
+
+void
+get_string (const char *s)
+{
+  str = s;
+}
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/gnu-ifunc.exp b/gdb/testsuite/gdb.base/gnu-ifunc.exp
index e6389102ae3..85f57071140 100644
--- a/gdb/testsuite/gdb.base/gnu-ifunc.exp
+++ b/gdb/testsuite/gdb.base/gnu-ifunc.exp
@@ -25,6 +25,11 @@ set libsrc ${libfile}.c
 set final_file "${testfile}-final"
 set final_src ${final_file}.c
 
+set infcall_file "${testfile}-inferior-call"
+set infcall_src ${infcall_file}.c
+set infcall_malloc_file "${testfile}-inferior-call-malloc"
+set infcall_malloc_src ${infcall_malloc_file}.c
+
 # Return the binary suffix appended to program and library names to
 # make each testcase variant unique.
 proc make_binsuffix {resolver_attr resolver_debug final_debug} {
@@ -356,6 +361,39 @@ proc misc_tests {resolver_attr resolver_debug final_debug} {
     }
 }
 
+# Test that GDB resolves a GNU IFUNC minimal symbol when it uses
+# find_function_in_inferior to make an internal inferior call.  String
+# literals are copied into the inferior with a call to malloc, so a
+# no-debug IFUNC malloc exercises the minimal-symbol fallback.
+
+proc_with_prefix test_inferior_call {} {
+    global srcdir subdir
+    global infcall_file infcall_src
+    global infcall_malloc_file infcall_malloc_src
+
+    set executable $infcall_file
+    set binfile [standard_output_file $executable]
+    set malloc_obj [standard_output_file ${infcall_malloc_file}.o]
+
+    if { [gdb_compile ${srcdir}/${subdir}/${infcall_malloc_src} \
+	      $malloc_obj object {}] != ""
+	 || [gdb_compile [list ${srcdir}/${subdir}/${infcall_src} \
+			      $malloc_obj] \
+		 $binfile executable {debug}] != "" } {
+	untested "failed to compile inferior call testcase"
+	return
+    }
+
+    clean_restart $executable
+    if {![runto_main]} {
+	return
+    }
+
+    gdb_test "print (get_string (\"hello-ifunc\"), str_in_arena ())" \
+	" = 1" \
+	"internal call resolves no-debug IFUNC malloc"
+}
+
 # Test all the combinations of:
 #
 # - An ifunc resolver with the same name as the ifunc symbol vs an
@@ -379,6 +417,8 @@ foreach_with_prefix resolver_attr {0 1} {
     }
 }
 
+test_inferior_call
+
 # Test statically linked ifunc resolving during inferior start.
 # https://bugzilla.redhat.com/show_bug.cgi?id=624967
 
diff --git a/gdb/valops.c b/gdb/valops.c
index ab6fd5079e1..7d305871efc 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -133,11 +133,15 @@ find_function_in_inferior (const char *name, struct objfile **objf_p)
 	  struct gdbarch *gdbarch = objfile->arch ();
 
 	  struct type *type;
+	  struct type *resolved_type;
 	  CORE_ADDR maddr;
 	  type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
 	  type = lookup_function_type (type);
 	  type = lookup_pointer_type (type);
-	  maddr = msymbol.value_address ();
+	  resolved_type = find_minsym_type_and_address (msymbol.minsym, objfile,
+							&maddr);
+	  if (resolved_type->is_gnu_ifunc ())
+	    type->target_type ()->set_is_gnu_ifunc (true);
 
 	  if (objf_p)
 	    *objf_p = objfile;
-- 
2.43.0


  reply	other threads:[~2026-06-25 15:22 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-25 15:20 [PATCH v2 0/1] " Muhammad Kamran
2026-06-25 15:20 ` Muhammad Kamran [this message]
2026-06-25 20:05   ` [PATCH v2 1/1] " Simon Marchi
2026-06-26  9:24     ` Muhammad Kamran
2026-06-26 14:20       ` Simon Marchi
2026-06-29 15:43         ` Muhammad Kamran
2026-06-29 21:11   ` Florian Weimer
2026-06-30  9:13     ` Yury Khrustalev
2026-06-30  9:18     ` Yury Khrustalev
2026-06-30  9:42       ` Wilco Dijkstra
2026-06-30 11:53         ` Florian Weimer
2026-07-21 17:10           ` Tom Tromey
2026-07-21 19:45             ` Florian Weimer
2026-07-22 13:36               ` Tom Tromey
2026-06-30 14:15     ` Muhammad Kamran
2026-06-30 15:29       ` Wilco Dijkstra
2026-07-03 12:56         ` Simon Marchi
2026-07-07 12:15           ` Muhammad Kamran

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=20260625152036.6149-2-muhammad.kamran@arm.com \
    --to=muhammad.kamran@arm.com \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=Yury.Khrustalev@arm.com \
    --cc=aburgess@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=carlos@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=thiago.bauermann@linaro.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