I fixed the patch a bit.

The bt command does not work correctly with the core created when
calling the ABORT(3) as follows:

$ cat main.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
        printf("argc = %d\n", argc);
        abort();
        return (0);
}
$ cc -static main.c
$ ./a.out
argc = 1
Abort trap (core dumped)
$ /usr/bin/gdb a.out a.out.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "amd64-unknown-openbsd6.8"...
Core was generated by `a.out'.
Program terminated with signal 6, Aborted.
#0  0x000005c6e0400c1a in ?? ()
(gdb) bt
#0  0x000005c6e0400c1a in ?? ()
#1  0x000005c6e0400bbe in ?? ()
#2  0x0000003000000010 in ?? ()
#3  0x00007f7ffffbeec0 in ?? ()
#4  0xffffffdffffbede0 in ?? ()
#5  0x00007f7ffffbef48 in ?? ()
#6  0x00007f7ffffbeee0 in ?? ()
#7  0x000005c6e03ecf5f in ?? ()
#8  0x0000000000000007 in ?? ()
#9  0x00000009fbe83b4e in ?? ()
#10 0x00007f7ffffbef48 in ?? ()
#11 0x0000000000000001 in ?? ()
#12 0x00007f7ffffbef30 in ?? ()
#13 0x000005c6e03ecd23 in ?? ()
#14 0x000005c6e0406078 in ?? ()
#15 0x00007f7ffffbef58 in ?? ()
#16 0x0000000100000000 in ?? ()
#17 0x0000000000000000 in ?? ()
(gdb) quit

I applied the new patch to /usr/obj/gnu/usr.bin/binutils/gdb/gdb.

$ /usr/obj/gnu/usr.bin/binutils/gdb/gdb a.out a.out.core
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "amd64-unknown-openbsd6.8"...
Core was generated by `a.out'.
Program terminated with signal 6, Aborted.
#0  thrkill () at /tmp/-:3
3       /tmp/-: No such file or directory.
        in /tmp/-
(gdb) bt
#0  thrkill () at /tmp/-:3
#1  0x000005c6e0400bbe in _libc_abort () at
/usr/src/lib/libc/stdlib/abort.c:51
#2  0x000005c6e03ecf5f in main ()
Current language:  auto; currently asm
(gdb) quit
$ 

I added exec_set_section_offsets() after do_cleanups() in the previous
patch.

ok? comment?

Index: solib-svr4.c
===================================================================
RCS file: /cvs/src/gnu/usr.bin/binutils/gdb/solib-svr4.c,v
retrieving revision 1.2
diff -u -p -r1.2 solib-svr4.c
--- solib-svr4.c        11 Nov 2008 22:57:48 -0000      1.2
+++ solib-svr4.c        30 Nov 2020 23:01:42 -0000
@@ -619,7 +619,41 @@ svr4_current_sos (void)
       /* If we can't find the dynamic linker's base structure, this
         must not be a dynamically linked executable.  Hmm.  */
       if (! debug_base)
-       return 0;
+       {
+         if (exec_bfd != NULL &&
+             bfd_get_section_by_name (exec_bfd, ".interp") == NULL &&
+             (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0 &&
+             bfd_get_start_address (exec_bfd) != entry_point_address ())
+           {
+             /* this is relocatable static link.
+                cf. svr4_relocate_main_executable() */
+             struct cleanup *old_chain;
+             struct section_offsets *new_offsets;
+             int i, changed;
+             CORE_ADDR displacement;
+
+             displacement = entry_point_address () - bfd_get_start_address 
(exec_bfd);
+             changed = 0;
+
+             new_offsets = xcalloc (symfile_objfile->num_sections,
+                                    sizeof (struct section_offsets));
+             old_chain = make_cleanup (xfree, new_offsets);
+
+             for (i = 0; i < symfile_objfile->num_sections; i++)
+               {
+                 if (displacement != ANOFFSET 
(symfile_objfile->section_offsets, i))
+                   changed = 1;
+                 new_offsets->offsets[i] = displacement;
+               }
+
+             if (changed)
+               objfile_relocate (symfile_objfile, new_offsets);
+
+             do_cleanups (old_chain);
+             exec_set_section_offsets(displacement, displacement, 
displacement);
+           }
+         return 0;
+       }
     }
 
   /* Walk the inferior's link map list, and build our list of
--
ASOU Masato


From: Masato Asou <a...@soum.co.jp>
Date: Thu, 08 Oct 2020 14:07:15 +0900 (JST)

> I refferd to the core of static linked in GDB.  However, the backtrace
> command did not display the symbols correctly.
> 
> $ cat main.c
> #include <stdio.h>
> 
> void
> sub2(int argc, char *argv[])
> {
>         int i;
>         for (int i = 0; i <= argc; i++)
>                 argv[i][0] = '\0';
> }
> 
> void
> sub1(int argc, char *argv[])
> {
>         sub2(argc, argv);
> }
> 
> int
> main(int argc, char *argv[])
> {
>         sub1(argc, argv);
>         return (0);
> }
> $ cc -g -static main.c
> $ ./a.out
> Segmentation fault (core dumped)
> $ /usr/bin/gdb a.out a.out.core
> GNU gdb 6.3
> Copyright 2004 Free Software Foundation, Inc.
> GDB is free software, covered by the GNU General Public License, and you are
> welcome to change it and/or distribute copies of it under certain conditions.
> Type "show copying" to see the conditions.
> There is absolutely no warranty for GDB.  Type "show warranty" for details.
> This GDB was configured as "amd64-unknown-openbsd6.8"...
> Core was generated by `a.out'.
> Program terminated with signal 11, Segmentation fault.
> #0  0x0000060eed558f45 in ?? ()
> (gdb) bt
> #0  0x0000060eed558f45 in ?? ()
> #1  0x00007f7ffffe3480 in ?? ()
> #2  0x0000060eed558f98 in ?? ()
> #3  0x0000000000000000 in ?? ()
> (gdb) quit
> $ 
> 
> The patch is below.
> ok?
> 
> diff --git a/gnu/usr.bin/binutils/gdb/solib-svr4.c 
> b/gnu/usr.bin/binutils/gdb/solib-svr4.c
> index eebeddd..7428bda 100644
> --- a/gnu/usr.bin/binutils/gdb/solib-svr4.c
> +++ b/gnu/usr.bin/binutils/gdb/solib-svr4.c
> @@ -619,7 +619,40 @@ svr4_current_sos (void)
>        /* If we can't find the dynamic linker's base structure, this
>        must not be a dynamically linked executable.  Hmm.  */
>        if (! debug_base)
> -     return 0;
> +     {
> +       if (exec_bfd != NULL &&
> +           bfd_get_section_by_name (exec_bfd, ".interp") == NULL &&
> +           (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0 &&
> +           bfd_get_start_address (exec_bfd) != entry_point_address ())
> +         {
> +           /* this is relocatable static link.
> +              cf. svr4_relocate_main_executable() */
> +           struct cleanup *old_chain;
> +           struct section_offsets *new_offsets;
> +           int i, changed;
> +           CORE_ADDR displacement;
> +
> +           displacement = entry_point_address () - bfd_get_start_address 
> (exec_bfd);
> +           changed = 0;
> +
> +           new_offsets = xcalloc (symfile_objfile->num_sections,
> +                                  sizeof (struct section_offsets));
> +           old_chain = make_cleanup (xfree, new_offsets);
> +
> +           for (i = 0; i < symfile_objfile->num_sections; i++)
> +             {
> +               if (displacement != ANOFFSET 
> (symfile_objfile->section_offsets, i))
> +                 changed = 1;
> +               new_offsets->offsets[i] = displacement;
> +             }
> +
> +           if (changed)
> +             objfile_relocate (symfile_objfile, new_offsets);
> +
> +           do_cleanups (old_chain);
> +         }
> +       return 0;
> +     }
>      }
>  
>    /* Walk the inferior's link map list, and build our list of
> --
> ASOU Masato
> 

Reply via email to