[PATCHv3,6/9] bfd/binutils: add support for RISC-V CSRs in core files

Message ID 2175060020b8c67092b601da15ca462b48312e84.1613410057.git.andrew.burgess@embecosm.com
State New
Headers show
Series
  • Bare-metal core dumps for RISC-V
Related show

Commit Message

Andrew Burgess Feb. 15, 2021, 5:29 p.m.
Adds support for including RISC-V control and status registers into
core files.

The value for the define NT_RISCV_CSR is set to 0x900, this
corresponds to a patch I have proposed for the Linux kernel here:

  http://lists.infradead.org/pipermail/linux-riscv/2020-December/003910.html

As I have not yet heard if the above patch will be accepted into the
kernel or not I have set the note name string to "GDB", and the note
type to NT_RISCV_CSR.

This means that if the above patch is rejected from the kernel, and
the note type number 0x900 is assigned to some other note type, we
will still be able to distinguish between the GDB produced
NT_RISCV_CSR, and the kernel produced notes, where the name would be
set to "CORE".

bfd/ChangeLog:

	* elf-bfd.h (elfcore_write_riscv_csr): Declare.
	* elf.c (elfcore_grok_riscv_csr): New function.
	(elfcore_grok_note): Handle NT_RISCV_CSR.
	(elfcore_write_riscv_csr): New function.
	(elfcore_write_register_note): Handle '.reg-riscv-csr'.

binutils/ChangeLog:

	* readelf.c (get_note_type): Handle NT_RISCV_CSR.

include/ChangeLog:

	* elf/common.h (NT_RISCV_CSR): Define.
---
 bfd/ChangeLog        |  9 +++++++++
 bfd/elf-bfd.h        |  2 ++
 bfd/elf.c            | 35 +++++++++++++++++++++++++++++++++++
 binutils/ChangeLog   |  5 +++++
 binutils/readelf.c   |  2 ++
 include/ChangeLog    |  5 +++++
 include/elf/common.h |  2 ++
 7 files changed, 60 insertions(+)

-- 
2.25.4

Patch

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 8b989e71580..e4cf481965f 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2796,6 +2796,8 @@  extern char *elfcore_write_aarch_pauth
   (bfd *, char *, int *, const void *, int);
 extern char *elfcore_write_arc_v2
   (bfd *, char *, int *, const void *, int);
+extern char *elfcore_write_riscv_csr
+  (bfd *, char *, int *, const void *, int);
 extern char *elfcore_write_gdb_tdesc
   (bfd *, char *, int *, const void *, int);
 extern char *elfcore_write_lwpstatus
diff --git a/bfd/elf.c b/bfd/elf.c
index 2d66e9b999f..553fa65a118 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -9912,6 +9912,15 @@  elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note)
   return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note);
 }
 
+/* Convert NOTE into a bfd_section called ".reg-riscv-csr".  Return TRUE if
+   successful otherwise, return FALSE.  */
+
+static bfd_boolean
+elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note)
+{
+  return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note);
+}
+
 /* Convert NOTE into a bfd_section called ".gdb-tdesc".  Return TRUE if
    successful otherwise, return FALSE.  */
 
@@ -10586,6 +10595,13 @@  elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
       else
         return TRUE;
 
+    case NT_RISCV_CSR:
+      if (note->namesz == 4
+          && strcmp (note->namedata, "GDB") == 0)
+        return elfcore_grok_riscv_csr (abfd, note);
+      else
+	return TRUE;
+
     case NT_PRPSINFO:
     case NT_PSINFO:
       if (bed->elf_backend_grok_psinfo)
@@ -11967,6 +11983,23 @@  elfcore_write_arc_v2 (bfd *abfd,
 			     note_name, NT_ARC_V2, arc_v2, size);
 }
 
+/* Write the buffer of csr values in CSRS (length SIZE) into the note
+   buffer BUF and update *BUFSIZ.  ABFD is the bfd the note is being
+   written into.  Return a pointer to the new start of the note buffer, to
+   replace BUF which may no longer be valid.  */
+
+char *
+elfcore_write_riscv_csr (bfd *abfd,
+                         char *buf,
+                         int *bufsiz,
+                         const void *csrs,
+                         int size)
+{
+  const char *note_name = "GDB";
+  return elfcore_write_note (abfd, buf, bufsiz,
+			     note_name, NT_RISCV_CSR, csrs, size);
+}
+
 /* Write the target description (a string) pointed to by TDESC, length
    SIZE, into the note buffer BUF, and update *BUFSIZ.  ABFD is the bfd the
    note is being written into.  Return a pointer to the new start of the
@@ -12070,6 +12103,8 @@  elfcore_write_register_note (bfd *abfd,
     return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size);
   if (strcmp (section, ".gdb-tdesc") == 0)
     return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size);
+  if (strcmp (section, ".reg-riscv-csr") == 0)
+    return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size);
   return NULL;
 }
 
diff --git a/binutils/readelf.c b/binutils/readelf.c
index 277fcd3bc5a..ba624788633 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -18396,6 +18396,8 @@  get_note_type (Filedata * filedata, unsigned e_type)
 	return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
       case NT_ARC_V2:
 	return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
+      case NT_RISCV_CSR:
+	return _("NT_RISCV_CSR (RISC-V control and status registers)");
       case NT_PSTATUS:
 	return _("NT_PSTATUS (pstatus structure)");
       case NT_FPREGS:
diff --git a/include/elf/common.h b/include/elf/common.h
index e6e9c278faa..4cb3748e4fd 100644
--- a/include/elf/common.h
+++ b/include/elf/common.h
@@ -674,6 +674,8 @@ 
 					/*   note name must be "LINUX".  */
 #define NT_ARC_V2	0x600		/* ARC HS accumulator/extra registers.  */
 					/*   note name must be "LINUX".  */
+#define NT_RISCV_CSR    0x900		/* RISC-V Control and Status Registers */
+					/*   note name must be "CORE".  */
 #define NT_SIGINFO	0x53494749	/* Fields of siginfo_t.  */
 #define NT_FILE		0x46494c45	/* Description of mapped files.  */