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