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

7d6eda
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
7d6eda
From: Keith Seitz <keiths@redhat.com>
7d6eda
Date: Mon, 27 Jul 2020 18:51:07 -0400
7d6eda
Subject: gdb-rhbz1842691-corefile-mem-access-8of15.patch
7d6eda
7d6eda
;; Use NT_FILE note section for reading core target memory
7d6eda
;; Kevin Buettner, RH BZ 1842961
7d6eda
7d6eda
   Author: Kevin Buettner <kevinb@redhat.com>
7d6eda
   Date:   Thu Jun 11 19:20:03 2020 -0700
7d6eda
7d6eda
    Use NT_FILE note section for reading core target memory
7d6eda
7d6eda
    In his reviews of my v1 and v2 corefile related patches, Pedro
7d6eda
    identified two cases which weren't handled by those patches.
7d6eda
7d6eda
    In https://sourceware.org/pipermail/gdb-patches/2020-May/168826.html,
7d6eda
    Pedro showed that debugging a core file in which mmap() is used to
7d6eda
    create a new mapping over an existing file-backed mapping will
7d6eda
    produce incorrect results.  I.e, for his example, GDB would
7d6eda
    show:
7d6eda
7d6eda
    (gdb) disassemble main
7d6eda
    Dump of assembler code for function main:
7d6eda
       0x00000000004004e6 <+0>:	push   %rbp
7d6eda
       0x00000000004004e7 <+1>:	mov    %rsp,%rbp
7d6eda
    => 0x00000000004004ea <+4>:	callq  0x4003f0 <abort@plt>
7d6eda
    End of assembler dump.
7d6eda
7d6eda
    This sort of looks like it might be correct, but is not due to the
7d6eda
    fact that mmap(...MAP_FIXED...) was used to create a mapping (of all
7d6eda
    zeros) on top of the .text section.  So, the correct result should be:
7d6eda
7d6eda
    (gdb) disassemble main
7d6eda
    Dump of assembler code for function main:
7d6eda
       0x00000000004004e6 <+0>:	add    %al,(%rax)
7d6eda
       0x00000000004004e8 <+2>:	add    %al,(%rax)
7d6eda
    => 0x00000000004004ea <+4>:	add    %al,(%rax)
7d6eda
       0x00000000004004ec <+6>:	add    %al,(%rax)
7d6eda
       0x00000000004004ee <+8>:	add    %al,(%rax)
7d6eda
    End of assembler dump.
7d6eda
7d6eda
    The other case that Pedro found involved an attempted examination of a
7d6eda
    particular section in the test case from gdb.base/corefile.exp.  On
7d6eda
    Fedora 27 or 28, the following behavior may be observed:
7d6eda
7d6eda
    (gdb) info proc mappings
7d6eda
    Mapped address spaces:
7d6eda
7d6eda
              Start Addr           End Addr       Size     Offset objfile
7d6eda
    ...
7d6eda
          0x7ffff7839000     0x7ffff7a38000   0x1ff000   0x1b5000 /usr/lib64/libc-2.27.so
7d6eda
    ...
7d6eda
    (gdb) x/4x 0x7ffff7839000
7d6eda
    0x7ffff7839000:	Cannot access memory at address 0x7ffff7839000
7d6eda
7d6eda
    FYI, this section appears to be unrelocated vtable data.  See
7d6eda
    https://sourceware.org/pipermail/gdb-patches/2020-May/168331.html for
7d6eda
    a detailed analysis.
7d6eda
7d6eda
    The important thing here is that GDB should be able to access this
7d6eda
    address since it should be backed by the shared library.  I.e. it
7d6eda
    should do this:
7d6eda
7d6eda
    (gdb) x/4x 0x7ffff7839000
7d6eda
    0x7ffff7839000:	0x0007ddf0	0x00000000	0x0007dba0	0x00000000
7d6eda
7d6eda
    Both of these cases are fixed with this commit.
7d6eda
7d6eda
    In a nutshell, this commit opens a "binary" target BFD for each of the
7d6eda
    files that are mentioned in an NT_FILE / .note.linuxcore.file note
7d6eda
    section.  It then uses these mappings instead of the file stratum
7d6eda
    mappings that GDB has used in the past.
7d6eda
7d6eda
    If this note section doesn't exist or is mangled for some reason, then
7d6eda
    GDB will use the file stratum as before.  Should this happen, then
7d6eda
    we can expect both of the above problems to again be present.
7d6eda
7d6eda
    See the code comments in the commit for other details.
7d6eda
7d6eda
    gdb/ChangeLog:
7d6eda
7d6eda
    	* corelow.c (solist.h, unordered_map): Include.
7d6eda
    	(class core_target): Add field m_core_file_mappings and
7d6eda
    	method build_file_mappings.
7d6eda
    	(core_target::core_target): Call build_file_mappings.
7d6eda
    	(core_target::~core_target): Free memory associated with
7d6eda
    	m_core_file_mappings.
7d6eda
    	(core_target::build_file_mappings): New method.
7d6eda
    	(core_target::xfer_partial): Use m_core_file_mappings
7d6eda
    	for memory transfers.
7d6eda
    	* linux-tdep.c (linux_read_core_file_mappings): New
7d6eda
    	function.
7d6eda
    	(linux_core_info_proc_mappings): Rewrite to use
7d6eda
    	linux_read_core_file_mappings.
7d6eda
    	(linux_init_abi): Register linux_read_core_file_mappings.
7d6eda
7d6eda
diff --git a/gdb/corelow.c b/gdb/corelow.c
7d6eda
--- a/gdb/corelow.c
7d6eda
+++ b/gdb/corelow.c
7d6eda
@@ -41,6 +41,7 @@
7d6eda
 #include "exec.h"
7d6eda
 #include "readline/tilde.h"
7d6eda
 #include "solib.h"
7d6eda
+#include "solist.h"
7d6eda
 #include "filenames.h"
7d6eda
 #include "progspace.h"
7d6eda
 #include "objfiles.h"
7d6eda
@@ -48,6 +49,8 @@
7d6eda
 #include "completer.h"
7d6eda
 #include "gdbsupport/filestuff.h"
7d6eda
 #include "build-id.h"
7d6eda
+#include "gdbsupport/pathstuff.h"
7d6eda
+#include <unordered_map>
7d6eda
 
7d6eda
 #ifndef O_LARGEFILE
7d6eda
 #define O_LARGEFILE 0
7d6eda
@@ -132,6 +135,13 @@ private: /* per-core data */
7d6eda
      core file currently open on core_bfd.  */
7d6eda
   core_fns *m_core_vec = NULL;
7d6eda
 
7d6eda
+  /* File-backed address space mappings: some core files include
7d6eda
+     information about memory mapped files.  */
7d6eda
+  target_section_table m_core_file_mappings {};
7d6eda
+
7d6eda
+  /* Build m_core_file_mappings.  Called from the constructor.  */
7d6eda
+  void build_file_mappings ();
7d6eda
+
7d6eda
   /* FIXME: kettenis/20031023: Eventually this field should
7d6eda
      disappear.  */
7d6eda
   struct gdbarch *m_core_gdbarch = NULL;
7d6eda
@@ -150,11 +160,120 @@ core_target::core_target ()
7d6eda
 			   &m_core_section_table.sections_end))
7d6eda
     error (_("\"%s\": Can't find sections: %s"),
7d6eda
 	   bfd_get_filename (core_bfd), bfd_errmsg (bfd_get_error ()));
7d6eda
+
7d6eda
+  build_file_mappings ();
7d6eda
 }
7d6eda
 
7d6eda
 core_target::~core_target ()
7d6eda
 {
7d6eda
   xfree (m_core_section_table.sections);
7d6eda
+  xfree (m_core_file_mappings.sections);
7d6eda
+}
7d6eda
+
7d6eda
+/* Construct the target_section_table for file-backed mappings if
7d6eda
+   they exist.
7d6eda
+
7d6eda
+   For each unique path in the note, we'll open a BFD with a bfd
7d6eda
+   target of "binary".  This is an unstructured bfd target upon which
7d6eda
+   we'll impose a structure from the mappings in the architecture-specific
7d6eda
+   mappings note.  A BFD section is allocated and initialized for each
7d6eda
+   file-backed mapping.
7d6eda
+
7d6eda
+   We take care to not share already open bfds with other parts of
7d6eda
+   GDB; in particular, we don't want to add new sections to existing
7d6eda
+   BFDs.  We do, however, ensure that the BFDs that we allocate here
7d6eda
+   will go away (be deallocated) when the core target is detached.  */
7d6eda
+
7d6eda
+void
7d6eda
+core_target::build_file_mappings ()
7d6eda
+{
7d6eda
+  std::unordered_map<std::string, struct bfd *> bfd_map;
7d6eda
+
7d6eda
+  /* See linux_read_core_file_mappings() in linux-tdep.c for an example
7d6eda
+     read_core_file_mappings method.  */
7d6eda
+  gdbarch_read_core_file_mappings (m_core_gdbarch, core_bfd,
7d6eda
+
7d6eda
+    /* After determining the number of mappings, read_core_file_mappings
7d6eda
+       will invoke this lambda which allocates target_section storage for
7d6eda
+       the mappings.  */
7d6eda
+    [&] (ULONGEST count)
7d6eda
+      {
7d6eda
+	m_core_file_mappings.sections = XNEWVEC (struct target_section, count);
7d6eda
+	m_core_file_mappings.sections_end = m_core_file_mappings.sections;
7d6eda
+      },
7d6eda
+
7d6eda
+    /* read_core_file_mappings will invoke this lambda for each mapping
7d6eda
+       that it finds.  */
7d6eda
+    [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
7d6eda
+         const char *filename, const void *other)
7d6eda
+      {
7d6eda
+	/* Architecture-specific read_core_mapping methods are expected to
7d6eda
+	   weed out non-file-backed mappings.  */
7d6eda
+	gdb_assert (filename != nullptr);
7d6eda
+
7d6eda
+	struct bfd *bfd = bfd_map[filename];
7d6eda
+	if (bfd == nullptr)
7d6eda
+	  {
7d6eda
+	    /* Use exec_file_find() to do sysroot expansion.  It'll
7d6eda
+	       also strip the potential sysroot "target:" prefix.  If
7d6eda
+	       there is no sysroot, an equivalent (possibly more
7d6eda
+	       canonical) pathname will be provided.  */
7d6eda
+	    gdb::unique_xmalloc_ptr<char> expanded_fname
7d6eda
+	      = exec_file_find (filename, NULL);
7d6eda
+	    if (expanded_fname == nullptr)
7d6eda
+	      {
7d6eda
+		warning (_("Can't open file %s during file-backed mapping "
7d6eda
+			   "note processing"),
7d6eda
+			 expanded_fname.get ());
7d6eda
+		return;
7d6eda
+	      }
7d6eda
+
7d6eda
+	    bfd = bfd_map[filename] = bfd_openr (expanded_fname.get (),
7d6eda
+	                                         "binary");
7d6eda
+
7d6eda
+	    if (bfd == nullptr || !bfd_check_format (bfd, bfd_object))
7d6eda
+	      {
7d6eda
+		/* If we get here, there's a good chance that it's due to
7d6eda
+		   an internal error.  We issue a warning instead of an
7d6eda
+		   internal error because of the possibility that the
7d6eda
+		   file was removed in between checking for its
7d6eda
+		   existence during the expansion in exec_file_find()
7d6eda
+		   and the calls to bfd_openr() / bfd_check_format(). 
7d6eda
+		   Output both the path from the core file note along
7d6eda
+		   with its expansion to make debugging this problem
7d6eda
+		   easier.  */
7d6eda
+		warning (_("Can't open file %s which was expanded to %s "
7d6eda
+			   "during file-backed mapping note processing"),
7d6eda
+			 filename, expanded_fname.get ());
7d6eda
+		if (bfd != nullptr)
7d6eda
+		  bfd_close (bfd);
7d6eda
+		return;
7d6eda
+	      }
7d6eda
+	    /* Ensure that the bfd will be closed when core_bfd is closed. 
7d6eda
+	       This can be checked before/after a core file detach via
7d6eda
+	       "maint info bfds".  */
7d6eda
+	    gdb_bfd_record_inclusion (core_bfd, bfd);
7d6eda
+	  }
7d6eda
+
7d6eda
+	/* Make new BFD section.  All sections have the same name,
7d6eda
+	   which is permitted by bfd_make_section_anyway().  */
7d6eda
+	asection *sec = bfd_make_section_anyway (bfd, "load");
7d6eda
+	if (sec == nullptr)
7d6eda
+	  error (_("Can't make section"));
7d6eda
+	sec->filepos = file_ofs;
7d6eda
+	bfd_set_section_flags (sec, SEC_READONLY | SEC_HAS_CONTENTS);
7d6eda
+	bfd_set_section_size (sec, end - start);
7d6eda
+	bfd_set_section_vma (sec, start);
7d6eda
+	bfd_set_section_lma (sec, start);
7d6eda
+	bfd_set_section_alignment (sec, 2);
7d6eda
+
7d6eda
+	/* Set target_section fields.  */
7d6eda
+	struct target_section *ts = m_core_file_mappings.sections_end++;
7d6eda
+	ts->addr = start;
7d6eda
+	ts->endaddr = end;
7d6eda
+	ts->owner = nullptr;
7d6eda
+	ts->the_bfd_section = sec;
7d6eda
+      });
7d6eda
 }
7d6eda
 
7d6eda
 /* List of all available core_fns.  On gdb startup, each core file
7d6eda
@@ -773,10 +892,21 @@ core_target::xfer_partial (enum target_object object, const char *annex,
7d6eda
 	if (xfer_status == TARGET_XFER_OK)
7d6eda
 	  return TARGET_XFER_OK;
7d6eda
 
7d6eda
-	/* Now check the stratum beneath us; this should be file_stratum.  */
7d6eda
-	xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
7d6eda
-						      writebuf, offset, len,
7d6eda
-						      xfered_len);
7d6eda
+	/* Check file backed mappings.  If they're available, use
7d6eda
+	   core file provided mappings (e.g. from .note.linuxcore.file
7d6eda
+	   or the like) as this should provide a more accurate
7d6eda
+	   result.  If not, check the stratum beneath us, which should
7d6eda
+	   be the file stratum.  */
7d6eda
+	if (m_core_file_mappings.sections != nullptr)
7d6eda
+	  xfer_status = section_table_xfer_memory_partial
7d6eda
+			  (readbuf, writebuf,
7d6eda
+			   offset, len, xfered_len,
7d6eda
+			   m_core_file_mappings.sections,
7d6eda
+			   m_core_file_mappings.sections_end);
7d6eda
+	else
7d6eda
+	  xfer_status = this->beneath ()->xfer_partial (object, annex, readbuf,
7d6eda
+							writebuf, offset, len,
7d6eda
+							xfered_len);
7d6eda
 	if (xfer_status == TARGET_XFER_OK)
7d6eda
 	  return TARGET_XFER_OK;
7d6eda
 
7d6eda
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
7d6eda
--- a/gdb/linux-tdep.c
7d6eda
+++ b/gdb/linux-tdep.c
7d6eda
@@ -1024,106 +1024,174 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
7d6eda
     }
7d6eda
 }
7d6eda
 
7d6eda
-/* Implement "info proc mappings" for a corefile.  */
7d6eda
+/* Implementation of `gdbarch_read_core_file_mappings', as defined in
7d6eda
+   gdbarch.h.
7d6eda
+   
7d6eda
+   This function reads the NT_FILE note (which BFD turns into the
7d6eda
+   section ".note.linuxcore.file").  The format of this note / section
7d6eda
+   is described as follows in the Linux kernel sources in
7d6eda
+   fs/binfmt_elf.c:
7d6eda
+   
7d6eda
+      long count     -- how many files are mapped
7d6eda
+      long page_size -- units for file_ofs
7d6eda
+      array of [COUNT] elements of
7d6eda
+	long start
7d6eda
+	long end
7d6eda
+	long file_ofs
7d6eda
+      followed by COUNT filenames in ASCII: "FILE1" NUL "FILE2" NUL...
7d6eda
+      
7d6eda
+   CBFD is the BFD of the core file.
7d6eda
+
7d6eda
+   PRE_LOOP_CB is the callback function to invoke prior to starting
7d6eda
+   the loop which processes individual entries.  This callback will
7d6eda
+   only be executed after the note has been examined in enough
7d6eda
+   detail to verify that it's not malformed in some way.
7d6eda
+   
7d6eda
+   LOOP_CB is the callback function that will be executed once
7d6eda
+   for each mapping.  */
7d6eda
 
7d6eda
 static void
7d6eda
-linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
7d6eda
+linux_read_core_file_mappings (struct gdbarch *gdbarch,
7d6eda
+			       struct bfd *cbfd,
7d6eda
+			       gdb::function_view<void (ULONGEST count)>
7d6eda
+			         pre_loop_cb,
7d6eda
+			       gdb::function_view
7d6eda
+			                                ULONGEST start,
7d6eda
+							ULONGEST end,
7d6eda
+							ULONGEST file_ofs,
7d6eda
+							const char *filename,
7d6eda
+							const void *other)>
7d6eda
+				 loop_cb)
7d6eda
 {
7d6eda
-  asection *section;
7d6eda
-  ULONGEST count, page_size;
7d6eda
-  unsigned char *descdata, *filenames, *descend;
7d6eda
-  size_t note_size;
7d6eda
-  unsigned int addr_size_bits, addr_size;
7d6eda
-  struct gdbarch *core_gdbarch = gdbarch_from_bfd (core_bfd);
7d6eda
-  /* We assume this for reading 64-bit core files.  */
7d6eda
+  /* Ensure that ULONGEST is big enough for reading 64-bit core files.  */
7d6eda
   gdb_static_assert (sizeof (ULONGEST) >= 8);
7d6eda
 
7d6eda
-  section = bfd_get_section_by_name (core_bfd, ".note.linuxcore.file");
7d6eda
-  if (section == NULL)
7d6eda
-    {
7d6eda
-      warning (_("unable to find mappings in core file"));
7d6eda
-      return;
7d6eda
-    }
7d6eda
+  /* It's not required that the NT_FILE note exists, so return silently
7d6eda
+     if it's not found.  Beyond this point though, we'll complain
7d6eda
+     if problems are found.  */
7d6eda
+  asection *section = bfd_get_section_by_name (cbfd, ".note.linuxcore.file");
7d6eda
+  if (section == nullptr)
7d6eda
+    return;
7d6eda
 
7d6eda
-  addr_size_bits = gdbarch_addr_bit (core_gdbarch);
7d6eda
-  addr_size = addr_size_bits / 8;
7d6eda
-  note_size = bfd_section_size (section);
7d6eda
+  unsigned int addr_size_bits = gdbarch_addr_bit (gdbarch);
7d6eda
+  unsigned int addr_size = addr_size_bits / 8;
7d6eda
+  size_t note_size = bfd_section_size (section);
7d6eda
 
7d6eda
   if (note_size < 2 * addr_size)
7d6eda
-    error (_("malformed core note - too short for header"));
7d6eda
+    {
7d6eda
+      warning (_("malformed core note - too short for header"));
7d6eda
+      return;
7d6eda
+    }
7d6eda
 
7d6eda
-  gdb::def_vector<unsigned char> contents (note_size);
7d6eda
+  gdb::def_vector<gdb_byte> contents (note_size);
7d6eda
   if (!bfd_get_section_contents (core_bfd, section, contents.data (),
7d6eda
 				 0, note_size))
7d6eda
-    error (_("could not get core note contents"));
7d6eda
+    {
7d6eda
+      warning (_("could not get core note contents"));
7d6eda
+      return;
7d6eda
+    }
7d6eda
 
7d6eda
-  descdata = contents.data ();
7d6eda
-  descend = descdata + note_size;
7d6eda
+  gdb_byte *descdata = contents.data ();
7d6eda
+  char *descend = (char *) descdata + note_size;
7d6eda
 
7d6eda
   if (descdata[note_size - 1] != '\0')
7d6eda
-    error (_("malformed note - does not end with \\0"));
7d6eda
+    {
7d6eda
+      warning (_("malformed note - does not end with \\0"));
7d6eda
+      return;
7d6eda
+    }
7d6eda
 
7d6eda
-  count = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
+  ULONGEST count = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
   descdata += addr_size;
7d6eda
 
7d6eda
-  page_size = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
+  ULONGEST page_size = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
   descdata += addr_size;
7d6eda
 
7d6eda
   if (note_size < 2 * addr_size + count * 3 * addr_size)
7d6eda
-    error (_("malformed note - too short for supplied file count"));
7d6eda
-
7d6eda
-  printf_filtered (_("Mapped address spaces:\n\n"));
7d6eda
-  if (gdbarch_addr_bit (gdbarch) == 32)
7d6eda
-    {
7d6eda
-      printf_filtered ("\t%10s %10s %10s %10s %s\n",
7d6eda
-		       "Start Addr",
7d6eda
-		       "  End Addr",
7d6eda
-		       "      Size", "    Offset", "objfile");
7d6eda
-    }
7d6eda
-  else
7d6eda
     {
7d6eda
-      printf_filtered ("  %18s %18s %10s %10s %s\n",
7d6eda
-		       "Start Addr",
7d6eda
-		       "  End Addr",
7d6eda
-		       "      Size", "    Offset", "objfile");
7d6eda
+      warning (_("malformed note - too short for supplied file count"));
7d6eda
+      return;
7d6eda
     }
7d6eda
 
7d6eda
-  filenames = descdata + count * 3 * addr_size;
7d6eda
-  while (--count > 0)
7d6eda
+  char *filenames = (char *) descdata + count * 3 * addr_size;
7d6eda
+
7d6eda
+  /* Make sure that the correct number of filenames exist.  Complain
7d6eda
+     if there aren't enough or are too many.  */
7d6eda
+  char *f = filenames;
7d6eda
+  for (int i = 0; i < count; i++)
7d6eda
     {
7d6eda
-      ULONGEST start, end, file_ofs;
7d6eda
+      if (f >= descend)
7d6eda
+        {
7d6eda
+	  warning (_("malformed note - filename area is too small"));
7d6eda
+	  return;
7d6eda
+	}
7d6eda
+      f += strnlen (f, descend - f) + 1;
7d6eda
+    }
7d6eda
+  /* Complain, but don't return early if the filename area is too big.  */
7d6eda
+  if (f != descend)
7d6eda
+    warning (_("malformed note - filename area is too big"));
7d6eda
 
7d6eda
-      if (filenames == descend)
7d6eda
-	error (_("malformed note - filenames end too early"));
7d6eda
+  pre_loop_cb (count);
7d6eda
 
7d6eda
-      start = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
+  for (int i = 0; i < count; i++)
7d6eda
+    {
7d6eda
+      ULONGEST start = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
       descdata += addr_size;
7d6eda
-      end = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
+      ULONGEST end = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
       descdata += addr_size;
7d6eda
-      file_ofs = bfd_get (addr_size_bits, core_bfd, descdata);
7d6eda
+      ULONGEST file_ofs
7d6eda
+        = bfd_get (addr_size_bits, core_bfd, descdata) * page_size;
7d6eda
       descdata += addr_size;
7d6eda
+      char * filename = filenames;
7d6eda
+      filenames += strlen ((char *) filenames) + 1;
7d6eda
 
7d6eda
-      file_ofs *= page_size;
7d6eda
-
7d6eda
-      if (gdbarch_addr_bit (gdbarch) == 32)
7d6eda
-	printf_filtered ("\t%10s %10s %10s %10s %s\n",
7d6eda
-			 paddress (gdbarch, start),
7d6eda
-			 paddress (gdbarch, end),
7d6eda
-			 hex_string (end - start),
7d6eda
-			 hex_string (file_ofs),
7d6eda
-			 filenames);
7d6eda
-      else
7d6eda
-	printf_filtered ("  %18s %18s %10s %10s %s\n",
7d6eda
-			 paddress (gdbarch, start),
7d6eda
-			 paddress (gdbarch, end),
7d6eda
-			 hex_string (end - start),
7d6eda
-			 hex_string (file_ofs),
7d6eda
-			 filenames);
7d6eda
-
7d6eda
-      filenames += 1 + strlen ((char *) filenames);
7d6eda
+      loop_cb (i, start, end, file_ofs, filename, nullptr);
7d6eda
     }
7d6eda
 }
7d6eda
 
7d6eda
+/* Implement "info proc mappings" for a corefile.  */
7d6eda
+
7d6eda
+static void
7d6eda
+linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
7d6eda
+{
7d6eda
+  linux_read_core_file_mappings (gdbarch, core_bfd,
7d6eda
+    [=] (ULONGEST count)
7d6eda
+      {
7d6eda
+	printf_filtered (_("Mapped address spaces:\n\n"));
7d6eda
+	if (gdbarch_addr_bit (gdbarch) == 32)
7d6eda
+	  {
7d6eda
+	    printf_filtered ("\t%10s %10s %10s %10s %s\n",
7d6eda
+			     "Start Addr",
7d6eda
+			     "  End Addr",
7d6eda
+			     "      Size", "    Offset", "objfile");
7d6eda
+	  }
7d6eda
+	else
7d6eda
+	  {
7d6eda
+	    printf_filtered ("  %18s %18s %10s %10s %s\n",
7d6eda
+			     "Start Addr",
7d6eda
+			     "  End Addr",
7d6eda
+			     "      Size", "    Offset", "objfile");
7d6eda
+	  }
7d6eda
+      },
7d6eda
+    [=] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
7d6eda
+         const char *filename, const void *other)
7d6eda
+      {
7d6eda
+	if (gdbarch_addr_bit (gdbarch) == 32)
7d6eda
+	  printf_filtered ("\t%10s %10s %10s %10s %s\n",
7d6eda
+			   paddress (gdbarch, start),
7d6eda
+			   paddress (gdbarch, end),
7d6eda
+			   hex_string (end - start),
7d6eda
+			   hex_string (file_ofs),
7d6eda
+			   filename);
7d6eda
+	else
7d6eda
+	  printf_filtered ("  %18s %18s %10s %10s %s\n",
7d6eda
+			   paddress (gdbarch, start),
7d6eda
+			   paddress (gdbarch, end),
7d6eda
+			   hex_string (end - start),
7d6eda
+			   hex_string (file_ofs),
7d6eda
+			   filename);
7d6eda
+      });
7d6eda
+}
7d6eda
+
7d6eda
 /* Implement "info proc" for a corefile.  */
7d6eda
 
7d6eda
 static void
7d6eda
@@ -2471,6 +2539,7 @@ linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
7d6eda
   set_gdbarch_info_proc (gdbarch, linux_info_proc);
7d6eda
   set_gdbarch_core_info_proc (gdbarch, linux_core_info_proc);
7d6eda
   set_gdbarch_core_xfer_siginfo (gdbarch, linux_core_xfer_siginfo);
7d6eda
+  set_gdbarch_read_core_file_mappings (gdbarch, linux_read_core_file_mappings);
7d6eda
   set_gdbarch_find_memory_regions (gdbarch, linux_find_memory_regions);
7d6eda
   set_gdbarch_make_corefile_notes (gdbarch, linux_make_corefile_notes);
7d6eda
   set_gdbarch_has_shared_address_space (gdbarch,