gdb: Fix derive_heap_segment for PIE

Message ID 20230509112401.1043723-1-iii@linux.ibm.com
State New
Headers
Series gdb: Fix derive_heap_segment for PIE |

Commit Message

Ilya Leoshkevich May 9, 2023, 11:24 a.m. UTC
  Using gcore with qemu-user gdbstub produces huge core dumps:

    $ qemu-s390x-static -g 1234 /bin/true
    $ gdb -ex 'target remote :1234' /bin/true
    (gdb) generate-core-file
    Saved corefile core.1
    $ wc -c <core.1
    274878128632

The reason is that derive_heap_segment thinks that heap starts quite
close to 0, because it works with bfd objects, which are not aware of
actual PIE load addresses.

Fix by using objfiles instead.
---
 gdb/gcore.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)
  

Comments

Ilya Leoshkevich June 21, 2023, 10:13 p.m. UTC | #1
On Tue, 2023-05-09 at 13:24 +0200, Ilya Leoshkevich wrote:
> Using gcore with qemu-user gdbstub produces huge core dumps:
> 
>     $ qemu-s390x-static -g 1234 /bin/true
>     $ gdb -ex 'target remote :1234' /bin/true
>     (gdb) generate-core-file
>     Saved corefile core.1
>     $ wc -c <core.1
>     274878128632
> 
> The reason is that derive_heap_segment thinks that heap starts quite
> close to 0, because it works with bfd objects, which are not aware of
> actual PIE load addresses.
> 
> Fix by using objfiles instead.
> ---
>  gdb/gcore.c | 31 ++++++++++++++-----------------
>  1 file changed, 14 insertions(+), 17 deletions(-)

Ping.
  

Patch

diff --git a/gdb/gcore.c b/gdb/gcore.c
index 05cad94526e..d044c887c22 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -283,18 +283,15 @@  call_target_sbrk (int sbrk_arg)
   return top_of_heap;
 }
 
-/* Derive a reasonable heap segment for ABFD by looking at sbrk and
-   the static data sections.  Store its limits in *BOTTOM and *TOP.
+/* Derive a reasonable heap segment by looking at sbrk and the executable's
+   static data sections.  Store its limits in *BOTTOM and *TOP.
    Return non-zero if successful.  */
 
 static int
-derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
+derive_heap_segment (bfd_vma *bottom, bfd_vma *top)
 {
   bfd_vma top_of_data_memory = 0;
   bfd_vma top_of_heap = 0;
-  bfd_size_type sec_size;
-  bfd_vma sec_vaddr;
-  asection *sec;
 
   gdb_assert (bottom);
   gdb_assert (top);
@@ -315,17 +312,18 @@  derive_heap_segment (bfd *abfd, bfd_vma *bottom, bfd_vma *top)
      | heap                          |
      --------------------------------- */
 
-  for (sec = abfd->sections; sec; sec = sec->next)
-    {
-      if (bfd_section_flags (sec) & SEC_DATA
-	  || strcmp (".bss", bfd_section_name (sec)) == 0)
+  for (objfile *objfile : current_program_space->objfiles ())
+    if (objfile->obfd == current_program_space->exec_bfd ())
+      for (obj_section *objsec : objfile->sections ())
 	{
-	  sec_vaddr = bfd_section_vma (sec);
-	  sec_size = bfd_section_size (sec);
-	  if (sec_vaddr + sec_size > top_of_data_memory)
-	    top_of_data_memory = sec_vaddr + sec_size;
+	  asection *sec = objsec->the_bfd_section;
+	  if (bfd_section_flags (sec) & SEC_DATA
+	      || strcmp (".bss", bfd_section_name (sec)) == 0)
+	    {
+	      if (objsec->endaddr () > top_of_data_memory)
+		top_of_data_memory = objsec->endaddr ();
+	    }
 	}
-    }
 
   top_of_heap = call_target_sbrk (0);
   if (top_of_heap == (bfd_vma) 0)
@@ -553,8 +551,7 @@  objfile_find_memory_regions (struct target_ops *self,
 	     obfd);
 
   /* Make a heap segment.  */
-  if (derive_heap_segment (current_program_space->exec_bfd (), &temp_bottom,
-			   &temp_top))
+  if (derive_heap_segment (&temp_bottom, &temp_top))
     (*func) (temp_bottom, temp_top - temp_bottom,
 	     1, /* Heap section will be readable.  */
 	     1, /* Heap section will be writable.  */