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

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