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

46818e
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
46818e
From: Keith Seitz <keiths@redhat.com>
46818e
Date: Mon, 27 Jul 2020 16:47:19 -0400
46818e
Subject: gdb-rhbz1842691-corefile-mem-access-2of15.patch
46818e
46818e
;; Adjust corefile.exp test to show regression after bfd hack removal
46818e
;; Kevin Buettner, RH BZ 1842691
46818e
46818e
   Author: Kevin Buettner <kevinb@redhat.com>
46818e
   Date:   Tue May 12 17:44:19 2020 -0700
46818e
46818e
    Adjust corefile.exp test to show regression after bfd hack removal
46818e
46818e
    In his review of my BZ 25631 patch series, Pedro was unable to
46818e
    reproduce the regression which should occur after patch #1, "Remove
46818e
    hack for GDB which sets the section size to 0", is applied.
46818e
46818e
    Pedro was using an ld version older than 2.30.  Version 2.30
46818e
    introduced the linker option -z separate-code.  Here's what the man
46818e
    page has to say about it:
46818e
46818e
        Create separate code "PT_LOAD" segment header in the object.  This
46818e
        specifies a memory segment that should contain only instructions
46818e
        and must be in wholly disjoint pages from any other data.
46818e
46818e
    In ld version 2.31, use of separate-code became the default for
46818e
    Linux/x86.  So, really, 2.31 or later is required in order to see the
46818e
    regression that occurs in recent Linux distributions when only the
46818e
    bfd hack removal patch is applied.
46818e
46818e
    For the test case in question, use of the separate-code linker option
46818e
    means that the global variable "coremaker_ro" ends up in a separate
46818e
    load segment (though potentially with other read-only data).  The
46818e
    upshot of this is that when only patch #1 is applied, GDB won't be
46818e
    able to correctly access coremaker_ro.  The reason for this is due
46818e
    to the fact that this section will now have a non-zero size, but
46818e
    will not have contents from the core file to find this data.
46818e
    So GDB will ask BFD for the contents and BFD will respond with
46818e
    zeroes for anything from those sections.  GDB should instead be
46818e
    looking in the executable for this data.  Failing that, it can
46818e
    then ask BFD for a reasonable value.  This is what a later patch
46818e
    in this series does.
46818e
46818e
    When using ld versions earlier than 2.31 (or 2.30 w/ the
46818e
    -z separate-code option explicitly provided to the linker), there is
46818e
    the possibility that coremaker_ro ends up being placed near other data
46818e
    which is recorded in the core file.  That means that the correct value
46818e
    will end up in the core file, simply because it resides on a page that
46818e
    the kernel chooses to put in the core file.  This is why Pedro wasn't
46818e
    able to reproduce the regression that should occur after fixing the
46818e
    BFD hack.
46818e
46818e
    This patch places a big chunk of memory, two pages worth on x86, in
46818e
    front of "coremaker_ro" to attempt to force it onto another page
46818e
    without requiring use of that new-fangled linker switch.
46818e
46818e
    Speaking of which, I considered changing the test to use
46818e
    -z separate-code, but this won't work because it didn't
46818e
    exist prior to version 2.30.  The linker would probably complain
46818e
    of an unrecognized switch.  Also, it likely won't be available in
46818e
    other linkers not based on current binutils.  I.e. it probably won't
46818e
    work in FreeBSD, NetBSD, etc.
46818e
46818e
    To make this more concrete, this is what *should* happen when
46818e
    attempting to access coremaker_ro when only patch #1 is applied:
46818e
46818e
        Core was generated by `/mesquite2/sourceware-git/f28-coresegs/bld/gdb/testsuite/outputs/gdb.base/coref'.
46818e
        Program terminated with signal SIGABRT, Aborted.
46818e
        #0  0x00007f68205deefb in raise () from /lib64/libc.so.6
46818e
        (gdb) p coremaker_ro
46818e
        $1 = 0
46818e
46818e
    Note that this result is wrong; 201 should have been printed instead.
46818e
    But that's the point of the rest of the patch series.
46818e
46818e
    However, without this commit, or when using an old Linux distro with
46818e
    a pre-2.31 ld, this is what you might see instead:
46818e
46818e
        Core was generated by `/mesquite2/sourceware-git/f28-coresegs/bld/gdb/testsuite/outputs/gdb.base/coref'.
46818e
        Program terminated with signal SIGABRT, Aborted.
46818e
        #0  0x00007f63dd658efb in raise () from /lib64/libc.so.6
46818e
        (gdb) p coremaker_ro
46818e
        $1 = 201
46818e
46818e
    I.e. it prints the right answer, which sort of makes it seem like the
46818e
    rest of the series isn't required.
46818e
46818e
    Now, back to the patch itself... what should be the size of the memory
46818e
    chunk placed before coremaker_ro?
46818e
46818e
    It needs to be at least as big as the page size (PAGE_SIZE) from
46818e
    the kernel.  For x86 and several other architectures this value is
46818e
    4096.  I used MAPSIZE which is defined to be 8192 in coremaker.c.
46818e
    So it's twice as big as what's currently needed for most Linux
46818e
    architectures.  The constant PAGE_SIZE is available from <sys/user.h>,
46818e
    but this isn't portable either.  In the end, it seemed simpler to
46818e
    just pick a value and hope that it's big enough.  (Running a separate
46818e
    program which finds the page size via sysconf(_SC_PAGESIZE) and then
46818e
    passes it to the compilation via a -D switch seemed like overkill
46818e
    for a case which is rendered moot by recent linker versions.)
46818e
46818e
    Further information can be found here:
46818e
46818e
       https://sourceware.org/pipermail/gdb-patches/2020-May/168168.html
46818e
       https://sourceware.org/pipermail/gdb-patches/2020-May/168170.html
46818e
46818e
    Thanks to H.J. Lu for telling me about the '-z separate-code' linker
46818e
    switch.
46818e
46818e
    gdb/testsuite/ChangeLog:
46818e
46818e
    	* gdb.base/coremaker.c (filler_ro): New global constant.
46818e
46818e
diff --git a/gdb/testsuite/gdb.base/coremaker.c b/gdb/testsuite/gdb.base/coremaker.c
46818e
--- a/gdb/testsuite/gdb.base/coremaker.c
46818e
+++ b/gdb/testsuite/gdb.base/coremaker.c
46818e
@@ -42,6 +42,12 @@ char *buf2;
46818e
 int coremaker_data = 1;	/* In Data section */
46818e
 int coremaker_bss;	/* In BSS section */
46818e
 
46818e
+/* Place a chunk of memory before coremaker_ro to improve the chances
46818e
+   that coremaker_ro will end up on it's own page.  See:
46818e
+
46818e
+   https://sourceware.org/pipermail/gdb-patches/2020-May/168168.html
46818e
+   https://sourceware.org/pipermail/gdb-patches/2020-May/168170.html  */
46818e
+const unsigned char filler_ro[MAPSIZE] = {1, 2, 3, 4, 5, 6, 7, 8};
46818e
 const int coremaker_ro = 201;	/* In Read-Only Data section */
46818e
 
46818e
 /* Note that if the mapping fails for any reason, we set buf2