Bug 31622 - [gdb/symtab] find_epilogue_using_linetable fails to find entry in function with two linetables
Summary: [gdb/symtab] find_epilogue_using_linetable fails to find entry in function wi...
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: 2024-04-09 06:59 UTC by Tom de Vries
Modified: 2024-04-24 21:13 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 2024-04-09 06:59:17 UTC
Bernd Edlinger reported the following problem and fix in find_epilogue_using_linetable ( https://sourceware.org/pipermail/gdb-patches/2024-April/207928.html ).

This came up in a discussion for a patch for PR31268, but it's an independent issue.

Consider the following test-case:
...
$ cat hello.c 
#include <stdio.h>
int main()
{
  printf("hello ");
  #include "world.inc"
/*** End of hello.c ***/

$ cat world.inc 
  printf("world\n");
  return 0;
}
/*** End of world.inc ***/

$ gcc -g -o hello hello.c
...

The corresponding disassembly for main is:
...
0000000000400557 <main>:
  400557:	55                   	push   %rbp
  400558:	48 89 e5             	mov    %rsp,%rbp
  40055b:	bf 24 06 40 00       	mov    $0x400624,%edi
  400560:	b8 00 00 00 00       	mov    $0x0,%eax
  400565:	e8 f6 fe ff ff       	call   400460 <printf@plt>
  40056a:	bf 2b 06 40 00       	mov    $0x40062b,%edi
  40056f:	e8 dc fe ff ff       	call   400450 <puts@plt>
  400574:	b8 00 00 00 00       	mov    $0x0,%eax
  400579:	5d                   	pop    %rbp
  40057a:	c3                   	ret
...

And the corresponding line table is:
...
hello.c:
File name                Line number    Starting address    View    Stmt
hello.c                            3            0x400557               x
hello.c                            4            0x40055b               x

./world.inc:[++]
world.inc                          1            0x40056a               x
world.inc                          2            0x400574               x
world.inc                          3            0x400579               x
world.inc                          -            0x40057b
...

Now, say we have an epilogue_begin marker at 0x400579 in world.inc.

We won't find it using find_epilogue_using_linetable, because it does:
...
  const struct symtab_and_line sal = find_pc_line (start_pc, 0);
...
which gets us the line table for hello.c.

Proposed patch:
...
-  const struct symtab_and_line sal = find_pc_line (start_pc, 0);
+  const struct symtab_and_line sal = find_pc_line (end_pc - 1, 0);
...
which gets us the linetable for world.inc instead.
Comment 2 Sourceware Commits 2024-04-24 14:27:07 UTC
The master branch has been updated by Bernd Edlinger <edlinger@sourceware.org>:

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

commit 730f5068f5fb6613e4faa2efc2309cd107562cbd
Author: Bernd Edlinger <bernd.edlinger@hotmail.de>
Date:   Tue Apr 9 09:27:53 2024 +0000

    Handle two-linetable function in find_epilogue_using_linetable
    
    Consider the following test-case:
    ...
    $ cat hello.c
    int main()
    {
      printf("hello ");
      #include "world.inc"
    $ cat world.inc
      printf("world\n");
      return 0;
    }
    $ gcc -g hello.c
    ...
    
    The line table for the compilation unit, consisting just of
    function main, is translated into these two gdb line tables, one for hello.c
    and one for world.inc:
    ...
    compunit_symtab: hello.c
    symtab: hello.c
    INDEX  LINE   REL-ADDRESS UNREL-ADDRESS IS-STMT PROLOGUE-END EPILOGUE-BEGIN
    0      3      0x400557    0x400557      Y
    1      4      0x40055b    0x40055b      Y
    2      END    0x40056a    0x40056a      Y
    
    compunit_symtab: hello.c
    symtab: world.inc
    INDEX  LINE   REL-ADDRESS UNREL-ADDRESS IS-STMT PROLOGUE-END EPILOGUE-BEGIN
    0      1      0x40056a    0x40056a      Y
    1      2      0x400574    0x400574      Y
    2      3      0x400579    0x400579      Y
    3      END    0x40057b    0x40057b      Y
    ...
    
    The epilogue of main starts at 0x400579:
    ...
      400579:       5d                      pop    %rbp
      40057a:       c3                      ret
    ...
    
    Now, say we have an epilogue_begin marker in the line table at 0x400579.
    
    We won't find it using find_epilogue_using_linetable, because it does:
    ...
      const struct symtab_and_line sal = find_pc_line (start_pc, 0);
    ...
    which gets us the line table for hello.c.
    
    Fix this by using "find_pc_line (end_pc - 1, 0)" instead.
    
    Tested on x86_64-linux.
    
    Co-Authored-By: Tom de Vries <tdevries@suse.de>
    
    PR symtab/31622
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31622
Comment 3 Bernd Edlinger 2024-04-24 15:20:45 UTC
fixed on master
Comment 4 Tom de Vries 2024-04-24 21:13:45 UTC
Fixed.