public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: Fix derive_heap_segment for PIE
@ 2023-05-09 11:24 Ilya Leoshkevich
  2023-06-21 22:13 ` [PING] " Ilya Leoshkevich
  0 siblings, 1 reply; 2+ messages in thread
From: Ilya Leoshkevich @ 2023-05-09 11:24 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, Andreas Arnez, Ilya Leoshkevich

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(-)

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.  */
-- 
2.40.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PING] [PATCH] gdb: Fix derive_heap_segment for PIE
  2023-05-09 11:24 [PATCH] gdb: Fix derive_heap_segment for PIE Ilya Leoshkevich
@ 2023-06-21 22:13 ` Ilya Leoshkevich
  0 siblings, 0 replies; 2+ messages in thread
From: Ilya Leoshkevich @ 2023-06-21 22:13 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches, Andreas Arnez

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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-06-21 22:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-09 11:24 [PATCH] gdb: Fix derive_heap_segment for PIE Ilya Leoshkevich
2023-06-21 22:13 ` [PING] " Ilya Leoshkevich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).