Bug 30911 - [gdb/symtab] Cannot call ifunc strstr with debuginfo installed
Summary: [gdb/symtab] Cannot call ifunc strstr with debuginfo installed
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 15.1
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-09-28 10:48 UTC by Tom de Vries
Modified: 2023-10-16 14:33 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2023-09-28 10:48:51 UTC
[ This report is based on fedora patch gdb-glibc-strstr-workaround.patch (see also PR glibc/14166). ]

Consider a hello world a.out with debug info.  Without glibc debug info installed, I'm able to call strstr:
...
$ gdb -q a.out -ex start -ex "p strstr" -ex "ptype strstr"
Reading symbols from a.out...
Temporary breakpoint 1 at 0x40051b: file hello.c, line 6.
Starting program: /data/vries/gdb/a.out 

Temporary breakpoint 1, main () at hello.c:6
6	  printf ("hello\n");
$1 = {<text gnu-indirect-function variable, no debug info>} 0x7ffff7e4f9ce <strstr>
type = <unknown return type> ()
(gdb) p strstr ("haha", "ah")
'__strstr_sse2_unaligned' has unknown return type; cast the call to its declared return type
(gdb) p (char *)strstr ("haha", "ah")
$2 = 0x7ffff7dab1b1 "aha"
(gdb) 
...

Now with glibc debug info installed:
...
$ gdb -q a.out -ex start -ex "p strstr" -ex "ptype strstr"
Reading symbols from a.out...
Temporary breakpoint 1 at 0x40051b: file hello.c, line 6.
Starting program: /data/vries/gdb/a.out 

Temporary breakpoint 1, main () at hello.c:6
6	  printf ("hello\n");
$1 = {<text gnu-indirect-function variable, no debug info>} 0x7ffff7e4e9ce <__libc_strstr_ifunc>
type = <unknown return type> ()
(gdb) p strstr ("haha", "ah")
$2 = void
(gdb) p (char *)strstr ("haha", "ah")
Invalid cast.
(gdb) 
...

This is with glibc 2.31 on openSUSE Leap 15.4.

With glibc 2.38 on openSUSE Tumbleweed this doesn't happen.

This seems to be caused by the fact that in the tumbleweed case we have an unspecified_type return type:
...
 <1><11dd5e>: Abbrev Number: 1 (DW_TAG_subprogram)
    <11dd5f>   DW_AT_name        : __strstr_sse2_unaligned
    <11dd63>   DW_AT_external    : 1
    <11dd63>   DW_AT_type        : <0x11dd6e>
    <11dd64>   DW_AT_low_pc      : 0xb9c9e
    <11dd6c>   DW_AT_high_pc     : 1173
 <1><11dd6e>: Abbrev Number: 3 (DW_TAG_unspecified_type)
 <1><11dd6f>: Abbrev Number: 0
...
and in the failing case there's no type at all, which defaults to void:
...
<1><3e1e58>: Abbrev Number: 2 (DW_TAG_subprogram)
    <3e1e59>   DW_AT_name        : __strstr_sse2_unaligned
    <3e1e5d>   DW_AT_external    : 1
    <3e1e5e>   DW_AT_low_pc      : 0xbbd2e
    <3e1e66>   DW_AT_high_pc     : 0xbc1c3
...

This is basically PR gas/29517, fixed in 2.40 but present in 2.39.

What is curious though is that we do manage to find the correct type for the revolver function:
...
(gdb) p __libc_strstr_ifunc
$3 = {char *(*(void))(const char *, const char *)} 0x7ffff7e4e9ce <__libc_strstr_ifunc>
...
but we don't use it.
Comment 1 Tom de Vries 2023-09-28 10:49:42 UTC
Tentative fix: swap the order here:
...
diff --git a/gdb/infcall.c b/gdb/infcall.c
index 0f9ad34bbb4..20d0f9acca5 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -316,17 +316,20 @@ find_function_addr (struct value *function,
 	     FUNCTION_TYPE have been asked for.  */
 	  if (retval_type != NULL || function_type != NULL)
 	    {
-	      type *target_ftype = find_function_type (funaddr);
-	      /* If we don't have debug info for the target function,
-		 see if we can instead extract the target function's
-		 type from the type that the resolver returns.  */
-	      if (target_ftype == NULL)
-		target_ftype = find_gnu_ifunc_target_type (resolver_addr);
+	      /* Try to get the target function's type from the type that
+		 the resolver returns.  We do this first to work around
+		 PR gas/29517. */
+	      type *target_ftype = find_gnu_ifunc_target_type (resolver_addr);
 	      if (target_ftype != NULL)
 		{
 		  value_type = check_typedef (target_ftype)->target_type ();
 		  ftype = target_ftype;
 		}
+
+	      /* Try to get the target function's type from the target
+		 function. */
+	      if (target_ftype == NULL)
+		target_ftype = find_function_type (funaddr);
 	    }
 	}
       else
...
Comment 2 Tom de Vries 2023-09-28 11:24:22 UTC
(In reply to Tom de Vries from comment #1)
> Tentative fix: swap the order here:

And we get:
...
FAIL: gdb.base/gnu-ifunc.exp: resolver_attr=0: resolver_debug=0: final_debug=1: p gnu_ifunc (3)
FAIL: gdb.base/gnu-ifunc.exp: resolver_attr=0: resolver_debug=0: final_debug=1: p gnu_ifunc()
FAIL: gdb.base/gnu-ifunc.exp: resolver_attr=1: resolver_debug=0: final_debug=1: p gnu_ifunc (3)
FAIL: gdb.base/gnu-ifunc.exp: resolver_attr=1: resolver_debug=0: final_debug=1: p gnu_ifunc()
...
Comment 3 Tom de Vries 2023-09-28 11:54:04 UTC
Alternatively, we detect the problem while reading the debug info and fix it:
...
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 5bbc8e24cf9..e547653d7fe 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -10161,7 +10161,20 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
 
   gdb_assert (cu->get_builder () != nullptr);
   newobj = cu->get_builder ()->push_context (0, lowpc);
-  newobj->name = new_symbol (die, read_type_die (die, cu), cu, templ_func);
+  struct type *type = read_type_die (die, cu);
+  if (type->code () == TYPE_CODE_FUNC
+      && type->num_fields () == 0
+      && type->target_type ()->code () == TYPE_CODE_VOID
+      && !type->target_type ()->is_stub ()
+      && strcmp (cu->producer, "GNU AS 2.39.0") == 0)
+    {
+      /* Work around PR gas/29517.  */
+      type = (type_allocator (cu->per_objfile->objfile, cu->lang ())
+	      .new_type (TYPE_CODE_VOID, 0, nullptr));
+      type->set_is_stub (true);
+      set_die_type (die, type, cu);
+    }
+  newobj->name = new_symbol (die, type, cu, templ_func);
 
   if (dwarf2_func_is_main_p (die, cu))
     set_objfile_main_name (objfile, newobj->name->linkage_name (),
...
Comment 5 Sourceware Commits 2023-10-16 14:32:06 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1d45d90934b10862c00a22bcf4075815a785001b

commit 1d45d90934b10862c00a22bcf4075815a785001b
Author: Tom de Vries <tdevries@suse.de>
Date:   Mon Oct 16 16:32:28 2023 +0200

    [gdb/symtab] Work around PR gas/29517
    
    When using glibc debuginfo generated with gas 2.39, we run into PR gas/29517:
    ...
    $ gdb -q -batch a.out -ex start -ex "p (char *)strstr (\"haha\", \"ah\")"
    Temporary breakpoint 1 at 0x40051b: file hello.c, line 6.
    
    Temporary breakpoint 1, main () at hello.c:6
    6         printf ("hello\n");
    Invalid cast.
    ...
    while without glibc debuginfo installed we get the expected result:
    ...
    $n = 0x7ffff7daa1b1 "aha"
    ...
    and likewise with glibc debuginfo generated with gas 2.40.
    
    The strstr ifunc resolves to __strstr_sse2_unaligned.  The problem is that gas
    generates dwarf that states that the return type is void:
    ...
    <1><3e1e58>: Abbrev Number: 2 (DW_TAG_subprogram)
        <3e1e59>   DW_AT_name        : __strstr_sse2_unaligned
        <3e1e5d>   DW_AT_external    : 1
        <3e1e5e>   DW_AT_low_pc      : 0xbbd2e
        <3e1e66>   DW_AT_high_pc     : 0xbc1c3
    ...
    while the return type should be a DW_TAG_unspecified_type, as is the case
    with gas 2.40.
    
    We can still use the workaround of casting to another function type for both
    __strstr_sse2_unaligned:
    ...
    (gdb) p ((char * (*) (const char *, const char *))__strstr_sse2_unaligned) \
      ("haha", "ah")
    $n = 0x7ffff7daa211 "aha"
    ...
    and strstr (which requires using *strstr to dereference the ifunc before we
    cast):
    ...
    gdb) p ((char * (*) (const char *, const char *))*strstr) ("haha", "ah")
    $n = 0x7ffff7daa251 "aha"
    ...
    but that's a bit cumbersome to use.
    
    Work around this in the dwarf reader, such that we have instead:
    ...
    (gdb) p (char *)strstr ("haha", "ah")
    $n = 0x7ffff7daa1b1 "aha"
    ...
    
    This also requires fixing producer_is_gcc to stop returning true for
    producer "GNU AS 2.39.0".
    
    Tested on x86_64-linux.
    
    Approved-By: Andrew Burgess <aburgess@redhat.com>
    
    PR symtab/30911
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30911
Comment 6 Tom de Vries 2023-10-16 14:33:11 UTC
Fixed.