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

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