public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Ilya Leoshkevich <iii@linux.ibm.com>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org, Andreas Arnez <arnez@linux.ibm.com>,
	Ilya Leoshkevich <iii@linux.ibm.com>
Subject: [PATCH] gdb: Fix derive_heap_segment for PIE
Date: Tue,  9 May 2023 13:24:01 +0200	[thread overview]
Message-ID: <20230509112401.1043723-1-iii@linux.ibm.com> (raw)

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


             reply	other threads:[~2023-05-09 11:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-09 11:24 Ilya Leoshkevich [this message]
2023-06-21 22:13 ` [PING] " Ilya Leoshkevich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230509112401.1043723-1-iii@linux.ibm.com \
    --to=iii@linux.ibm.com \
    --cc=arnez@linux.ibm.com \
    --cc=gdb-patches@sourceware.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).