Blame SOURCES/gdb-rhbz1842691-corefile-mem-access-4of15.patch

0efd7d
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
0efd7d
From: Keith Seitz <keiths@redhat.com>
0efd7d
Date: Mon, 27 Jul 2020 17:11:49 -0400
0efd7d
Subject: gdb-rhbz1842691-corefile-mem-access-4of15.patch
0efd7d
0efd7d
;; Provide access to non SEC_HAS_CONTENTS core file sections
0efd7d
;; Kevin Buettner, RH BZ 1842961
0efd7d
0efd7d
   Author: Kevin Buettner <kevinb@redhat.com>
0efd7d
   Date:   Wed Mar 4 17:42:42 2020 -0700
0efd7d
0efd7d
    Provide access to non SEC_HAS_CONTENTS core file sections
0efd7d
0efd7d
    Consider the following program:
0efd7d
0efd7d
    - - - mkmmapcore.c - - -
0efd7d
0efd7d
    static char *buf;
0efd7d
0efd7d
    int
0efd7d
    main (int argc, char **argv)
0efd7d
    {
0efd7d
      buf = mmap (NULL, 8192, PROT_READ | PROT_WRITE,
0efd7d
                  MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
0efd7d
      abort ();
0efd7d
    }
0efd7d
    - - - end mkmmapcore.c - - -
0efd7d
0efd7d
    Compile it like this:
0efd7d
0efd7d
    gcc -g -o mkmmapcore mkmmapcore.c
0efd7d
0efd7d
    Now let's run it from GDB.  I've already placed a breakpoint on the
0efd7d
    line with the abort() call and have run to that breakpoint.
0efd7d
0efd7d
    Breakpoint 1, main (argc=1, argv=0x7fffffffd678) at mkmmapcore.c:11
0efd7d
    11	  abort ();
0efd7d
    (gdb) x/x buf
0efd7d
    0x7ffff7fcb000:	0x00000000
0efd7d
0efd7d
    Note that we can examine the memory allocated via the call to mmap().
0efd7d
0efd7d
    Now let's try debugging a core file created by running this program.
0efd7d
    Depending on your system, in order to make a core file, you may have to
0efd7d
    run the following as root (or using sudo):
0efd7d
0efd7d
        echo core > /proc/sys/kernel/core_pattern
0efd7d
0efd7d
    It may also be necessary to do:
0efd7d
0efd7d
        ulimit -c unlimited
0efd7d
0efd7d
    I'm using Fedora 31. YMMV if you're using one of the BSDs or some other
0efd7d
    (non-Linux) system.
0efd7d
0efd7d
    This is what things look like when we debug the core file:
0efd7d
0efd7d
        [kev@f31-1 tmp]$ gdb -q ./mkmmapcore core.304767
0efd7d
        Reading symbols from ./mkmmapcore...
0efd7d
        [New LWP 304767]
0efd7d
        Core was generated by `/tmp/mkmmapcore'.
0efd7d
        Program terminated with signal SIGABRT, Aborted.
0efd7d
        #0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
0efd7d
        50	  return ret;
0efd7d
        (gdb) x/x buf
0efd7d
        0x7ffff7fcb000:	Cannot access memory at address 0x7ffff7fcb000
0efd7d
0efd7d
    Note that we can no longer access the memory region allocated by mmap().
0efd7d
0efd7d
    Back in 2007, a hack for GDB was added to _bfd_elf_make_section_from_phdr()
0efd7d
    in bfd/elf.c:
0efd7d
0efd7d
    	  /* Hack for gdb.  Segments that have not been modified do
0efd7d
    	     not have their contents written to a core file, on the
0efd7d
    	     assumption that a debugger can find the contents in the
0efd7d
    	     executable.  We flag this case by setting the fake
0efd7d
    	     section size to zero.  Note that "real" bss sections will
0efd7d
    	     always have their contents dumped to the core file.  */
0efd7d
    	  if (bfd_get_format (abfd) == bfd_core)
0efd7d
    	    newsect->size = 0;
0efd7d
0efd7d
    You can find the entire patch plus links to other discussion starting
0efd7d
    here:
0efd7d
0efd7d
        https://sourceware.org/ml/binutils/2007-08/msg00047.html
0efd7d
0efd7d
    This hack sets the size of certain BFD sections to 0, which
0efd7d
    effectively causes GDB to ignore them.  I think it's likely that the
0efd7d
    bug described above existed even before this hack was added, but I
0efd7d
    have no easy way to test this now.
0efd7d
0efd7d
    The output from objdump -h shows the result of this hack:
0efd7d
0efd7d
     25 load13        00000000  00007ffff7fcb000  0000000000000000  00013000  2**12
0efd7d
                      ALLOC
0efd7d
0efd7d
    (The first field, after load13, shows the size of 0.)
0efd7d
0efd7d
    Once the hack is removed, the output from objdump -h shows the correct
0efd7d
    size:
0efd7d
0efd7d
     25 load13        00002000  00007ffff7fcb000  0000000000000000  00013000  2**12
0efd7d
                      ALLOC
0efd7d
0efd7d
    (This is a digression, but I think it's good that objdump will now show
0efd7d
    the correct size.)
0efd7d
0efd7d
    If we remove the hack from bfd/elf.c, but do nothing to GDB, we'll
0efd7d
    see the following regression:
0efd7d
0efd7d
    FAIL: gdb.base/corefile.exp: print coremaker_ro
0efd7d
0efd7d
    The reason for this is that all sections which have the BFD flag
0efd7d
    SEC_ALLOC set, but for which SEC_HAS_CONTENTS is not set no longer
0efd7d
    have zero size.  Some of these sections have data that can (and should)
0efd7d
    be read from the executable.  (Sections for which SEC_HAS_CONTENTS
0efd7d
    is set should be read from the core file; sections which do not have
0efd7d
    this flag set need to either be read from the executable or, failing
0efd7d
    that, from the core file using whatever BFD decides is the best value
0efd7d
    to present to the user - it uses zeros.)
0efd7d
0efd7d
    At present, due to the way that the target strata are traversed when
0efd7d
    attempting to access memory, the non-SEC_HAS_CONTENTS sections will be
0efd7d
    read as zeroes from the process_stratum (which in this case is the
0efd7d
    core file stratum) without first checking the file stratum, which is
0efd7d
    where the data might actually be found.
0efd7d
0efd7d
    What we should be doing is this:
0efd7d
0efd7d
    - Attempt to access core file data for SEC_HAS_CONTENTS sections.
0efd7d
    - Attempt to access executable file data if the above fails.
0efd7d
    - Attempt to access core file data for non SEC_HAS_CONTENTS sections, if
0efd7d
      both of the above fail.
0efd7d
0efd7d
    This corresponds to the analysis of Daniel Jacobowitz back in 2007
0efd7d
    when the hack was added to BFD:
0efd7d
0efd7d
        https://sourceware.org/legacy-ml/binutils/2007-08/msg00045.html
0efd7d
0efd7d
    The difference, observed by Pedro in his review of my v1 patches, is
0efd7d
    that I'm using "the section flags as proxy for the p_filesz/p_memsz
0efd7d
    checks."
0efd7d
0efd7d
    gdb/ChangeLog:
0efd7d
0efd7d
    	PR corefiles/25631
0efd7d
    	* corelow.c (core_target:xfer_partial):  Revise
0efd7d
    	TARGET_OBJECT_MEMORY case to consider non-SEC_HAS_CONTENTS
0efd7d
    	case after first checking the stratum beneath the core
0efd7d
    	target.
0efd7d
    	(has_all_memory): Return true.
0efd7d
    	* target.c (raw_memory_xfer_partial): Revise comment
0efd7d
    	regarding use of has_all_memory.
0efd7d
0efd7d
diff --git a/gdb/corelow.c b/gdb/corelow.c
0efd7d
--- a/gdb/corelow.c
0efd7d
+++ b/gdb/corelow.c
0efd7d
@@ -93,7 +93,7 @@ public:
0efd7d
 
0efd7d
   const char *thread_name (struct thread_info *) override;
0efd7d
 
0efd7d
-  bool has_all_memory () override { return false; }
0efd7d
+  bool has_all_memory () override { return true; }
0efd7d
   bool has_memory () override;
0efd7d
   bool has_stack () override;
0efd7d
   bool has_registers () override;
0efd7d
@@ -754,12 +754,47 @@ core_target::xfer_partial (enum target_object object, const char *annex,
0efd7d
   switch (object)
0efd7d
     {
0efd7d
     case TARGET_OBJECT_MEMORY:
0efd7d
-      return (section_table_xfer_memory_partial
0efd7d
-	      (readbuf, writebuf,
0efd7d
-	       offset, len, xfered_len,
0efd7d
-	       m_core_section_table.sections,
0efd7d
-	       m_core_section_table.sections_end));
0efd7d
+      {
0efd7d
+	enum target_xfer_status xfer_status;
0efd7d
+
0efd7d
+	/* Try accessing memory contents from core file data,
0efd7d
+	   restricting consideration to those sections for which
0efd7d
+	   the BFD section flag SEC_HAS_CONTENTS is set.  */
0efd7d
+	auto has_contents_cb = [] (const struct target_section *s)
0efd7d
+	  {
0efd7d
+	    return ((s->the_bfd_section->flags & SEC_HAS_CONTENTS) != 0);
0efd7d
+	  };
0efd7d
+	xfer_status = section_table_xfer_memory_partial
0efd7d
+			(readbuf, writebuf,
0efd7d
+			 offset, len, xfered_len,
0efd7d
+			 m_core_section_table.sections,
0efd7d
+			 m_core_section_table.sections_end,
0efd7d
+			 has_contents_cb);
0efd7d
+	if (xfer_status == TARGET_XFER_OK)
0efd7d
+	  return TARGET_XFER_OK;
0efd7d
+
0efd7d
+	/* Now check the stratum beneath us; this should be file_stratum.  */
0efd7d
+	xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
0efd7d
+						      writebuf, offset, len,
0efd7d
+						      xfered_len);
0efd7d
+	if (xfer_status == TARGET_XFER_OK)
0efd7d
+	  return TARGET_XFER_OK;
0efd7d
 
0efd7d
+	/* Finally, attempt to access data in core file sections with
0efd7d
+	   no contents.  These will typically read as all zero.  */
0efd7d
+	auto no_contents_cb = [&] (const struct target_section *s)
0efd7d
+	  {
0efd7d
+	    return !has_contents_cb (s);
0efd7d
+	  };
0efd7d
+	xfer_status = section_table_xfer_memory_partial
0efd7d
+			(readbuf, writebuf,
0efd7d
+			 offset, len, xfered_len,
0efd7d
+			 m_core_section_table.sections,
0efd7d
+			 m_core_section_table.sections_end,
0efd7d
+			 no_contents_cb);
0efd7d
+
0efd7d
+	return xfer_status;
0efd7d
+      }
0efd7d
     case TARGET_OBJECT_AUXV:
0efd7d
       if (readbuf)
0efd7d
 	{
0efd7d
diff --git a/gdb/target.c b/gdb/target.c
0efd7d
--- a/gdb/target.c
0efd7d
+++ b/gdb/target.c
0efd7d
@@ -967,8 +967,11 @@ raw_memory_xfer_partial (struct target_ops *ops, gdb_byte *readbuf,
0efd7d
       if (res == TARGET_XFER_UNAVAILABLE)
0efd7d
 	break;
0efd7d
 
0efd7d
-      /* We want to continue past core files to executables, but not
0efd7d
-	 past a running target's memory.  */
0efd7d
+      /* Don't continue past targets which have all the memory.
0efd7d
+         At one time, this code was necessary to read data from
0efd7d
+	 executables / shared libraries when data for the requested
0efd7d
+	 addresses weren't available in the core file.  But now the
0efd7d
+	 core target handles this case itself.  */
0efd7d
       if (ops->has_all_memory ())
0efd7d
 	break;
0efd7d