[RFC,v2,4/5] RISC-V: Decode Zcmt JVT entries in objdump

Message ID 20260924155402.420585-5-jiawei@iscas.ac.cn
State Committed
Headers
Series RISC-V: Add Zcmt table-jump relaxation |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 warning Skipped because it is an RFC

Commit Message

Jiawei Sept. 24, 2026, 3:54 p.m. UTC
  Decode JVT entries and annotate table-jump targets. Add manually populated
RV32 and RV64 tables, with and without a base symbol. The tests use plain
--no-relax links solely to resolve addresses and do not depend on Zcmt
linker optimization. Preserve upstream Zdinx and Zqinx register checks.

opcodes/ChangeLog:

	* riscv-dis.c (struct riscv_private_data): Add JVT fields.
	(riscv_set_xlen_from_elf): New function.
	(maybe_print_jvt_address): Ditto.
	(print_insn_args): Annotate JVT target.
	(riscv_disassemble_insn): Handle Zcmt decode.
	(riscv_disassemble_jvt): New function.
	(riscv_init_disasm_info): Init JVT data.
	(print_insn_riscv): Handle JVT section.
	(disassemble_free_riscv): Free JVT content.
---
 .../binutils-all/riscv/zcmt-jvt-base-rv32.d   |  22 +++
 .../binutils-all/riscv/zcmt-jvt-base-rv64.d   |  22 +++
 .../binutils-all/riscv/zcmt-jvt-rv32.d        |  22 +++
 .../binutils-all/riscv/zcmt-jvt-rv64.d        |  22 +++
 .../testsuite/binutils-all/riscv/zcmt-jvt.ld  |   5 +
 .../testsuite/binutils-all/riscv/zcmt-jvt.s   |  29 ++++
 opcodes/riscv-dis.c                           | 164 ++++++++++++++++--
 7 files changed, 271 insertions(+), 15 deletions(-)
 create mode 100644 binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv32.d
 create mode 100644 binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv64.d
 create mode 100644 binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv32.d
 create mode 100644 binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv64.d
 create mode 100644 binutils/testsuite/binutils-all/riscv/zcmt-jvt.ld
 create mode 100644 binutils/testsuite/binutils-all/riscv/zcmt-jvt.s
  

Patch

diff --git a/binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv32.d b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv32.d
new file mode 100644
index 00000000000..b52636baf88
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv32.d
@@ -0,0 +1,22 @@ 
+#name: RISC-V Zcmt RV32 manually constructed JVT with base symbol
+#source: zcmt-jvt.s
+#as: -march=rv32i_zca_zicsr_zcmt -mabi=ilp32 -mlittle-endian --defsym entry_size=4 --defsym with_base=1
+#ld: -m elf32lriscv --no-relax -e foo -T zcmt-jvt.ld
+#objdump: -d -j .text -j .riscv.jvt
+
+.*:     file format elf32-littleriscv
+
+Disassembly of section \.text:
+
+0*1000 <foo>:
+[ \t]+1000:[ \t]+a002[ \t]+cm\.jt[ \t]+0 # 1000 <foo>
+
+0*1002 <bar>:
+[ \t]+1002:[ \t]+a082[ \t]+cm\.jalt[ \t]+32 # 1002 <bar>
+
+Disassembly of section \.riscv\.jvt:
+
+0*2000 <__jvt_base\$>:
+[ \t]+2000:[ \t]+00001000[ \t]+jvt\.jt\[0\]:[ \t]+foo
+#...
+[ \t]+2080:[ \t]+00001002[ \t]+jvt\.jalt\[32\]:[ \t]+bar
diff --git a/binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv64.d b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv64.d
new file mode 100644
index 00000000000..943265b7a22
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-base-rv64.d
@@ -0,0 +1,22 @@ 
+#name: RISC-V Zcmt RV64 manually constructed JVT with base symbol
+#source: zcmt-jvt.s
+#as: -march=rv64i_zca_zicsr_zcmt -mabi=lp64 -mlittle-endian --defsym entry_size=8 --defsym with_base=1
+#ld: -m elf64lriscv --no-relax -e foo -T zcmt-jvt.ld
+#objdump: -d -j .text -j .riscv.jvt
+
+.*:     file format elf64-littleriscv
+
+Disassembly of section \.text:
+
+0*1000 <foo>:
+[ \t]+1000:[ \t]+a002[ \t]+cm\.jt[ \t]+0 # 1000 <foo>
+
+0*1002 <bar>:
+[ \t]+1002:[ \t]+a082[ \t]+cm\.jalt[ \t]+32 # 1002 <bar>
+
+Disassembly of section \.riscv\.jvt:
+
+0*2000 <__jvt_base\$>:
+[ \t]+2000:[ \t]+0000000000001000[ \t]+jvt\.jt\[0\]:[ \t]+foo
+#...
+[ \t]+2100:[ \t]+0000000000001002[ \t]+jvt\.jalt\[32\]:[ \t]+bar
diff --git a/binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv32.d b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv32.d
new file mode 100644
index 00000000000..e2091130a58
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv32.d
@@ -0,0 +1,22 @@ 
+#name: RISC-V Zcmt RV32 manually constructed JVT
+#source: zcmt-jvt.s
+#as: -march=rv32i_zca_zicsr_zcmt -mabi=ilp32 -mlittle-endian --defsym entry_size=4
+#ld: -m elf32lriscv --no-relax -e foo -T zcmt-jvt.ld
+#objdump: -d -j .text -j .riscv.jvt
+
+.*:     file format elf32-littleriscv
+
+Disassembly of section \.text:
+
+0*1000 <foo>:
+[ \t]+1000:[ \t]+a002[ \t]+cm\.jt[ \t]+0[ \t]*$
+
+0*1002 <bar>:
+[ \t]+1002:[ \t]+a082[ \t]+cm\.jalt[ \t]+32[ \t]*$
+
+Disassembly of section \.riscv\.jvt:
+
+0*2000 <[^>]+>:
+[ \t]+2000:[ \t]+00001000[ \t]+jvt\.jt\[0\]:[ \t]+foo
+#...
+[ \t]+2080:[ \t]+00001002[ \t]+jvt\.jalt\[32\]:[ \t]+bar
diff --git a/binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv64.d b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv64.d
new file mode 100644
index 00000000000..fe1693574a3
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/zcmt-jvt-rv64.d
@@ -0,0 +1,22 @@ 
+#name: RISC-V Zcmt RV64 manually constructed JVT
+#source: zcmt-jvt.s
+#as: -march=rv64i_zca_zicsr_zcmt -mabi=lp64 -mlittle-endian --defsym entry_size=8
+#ld: -m elf64lriscv --no-relax -e foo -T zcmt-jvt.ld
+#objdump: -d -j .text -j .riscv.jvt
+
+.*:     file format elf64-littleriscv
+
+Disassembly of section \.text:
+
+0*1000 <foo>:
+[ \t]+1000:[ \t]+a002[ \t]+cm\.jt[ \t]+0[ \t]*$
+
+0*1002 <bar>:
+[ \t]+1002:[ \t]+a082[ \t]+cm\.jalt[ \t]+32[ \t]*$
+
+Disassembly of section \.riscv\.jvt:
+
+0*2000 <[^>]+>:
+[ \t]+2000:[ \t]+0000000000001000[ \t]+jvt\.jt\[0\]:[ \t]+foo
+#...
+[ \t]+2100:[ \t]+0000000000001002[ \t]+jvt\.jalt\[32\]:[ \t]+bar
diff --git a/binutils/testsuite/binutils-all/riscv/zcmt-jvt.ld b/binutils/testsuite/binutils-all/riscv/zcmt-jvt.ld
new file mode 100644
index 00000000000..0883dd4c31e
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/zcmt-jvt.ld
@@ -0,0 +1,5 @@ 
+SECTIONS
+{
+  .text 0x1000 : { *(.text) }
+  .riscv.jvt 0x2000 : { *(.riscv.jvt) }
+}
diff --git a/binutils/testsuite/binutils-all/riscv/zcmt-jvt.s b/binutils/testsuite/binutils-all/riscv/zcmt-jvt.s
new file mode 100644
index 00000000000..0da44cc26b9
--- /dev/null
+++ b/binutils/testsuite/binutils-all/riscv/zcmt-jvt.s
@@ -0,0 +1,29 @@ 
+	# Construct the instructions and table directly.  Linking only resolves
+	# the table addresses; no linker relaxation is needed by this test.
+	.option norelax
+	.text
+	.globl foo, bar
+foo:
+	cm.jt 0
+bar:
+	cm.jalt 32
+
+	.section .riscv.jvt, "ax"
+	# The linker script places the table at a 64-byte-aligned address.
+	.ifdef with_base
+	.globl __jvt_base$
+__jvt_base$:
+	.endif
+	.if entry_size == 8
+	.dword foo
+	.rept 31
+	.dword 0
+	.endr
+	.dword bar
+	.else
+	.word foo
+	.rept 31
+	.word 0
+	.endr
+	.word bar
+	.endif
diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c
index 42b0dd9c9fc..9c7dc881069 100644
--- a/opcodes/riscv-dis.c
+++ b/opcodes/riscv-dis.c
@@ -47,6 +47,10 @@  struct riscv_private_data
   bfd_vma hi_addr[OP_MASK_RD + 1];
   bool to_print_addr;
   bool has_gp;
+  bool has_jvt_base;
+  asection *jvt_section;
+  bfd_byte *jvt_content;
+  bfd_size_type jvt_size;
   /* Current XLEN for the disassembler.  */
   unsigned xlen;
   /* Default ISA specification version.  */
@@ -236,6 +240,48 @@  maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
     pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
 }
 
+/* Set pd->xlen from ELF class if not already known.  */
+
+static void
+riscv_set_xlen_from_elf (struct riscv_private_data *pd,
+			 struct disassemble_info *info)
+{
+  if (pd->xlen != 0)
+    return;
+
+  if (info->mach == bfd_mach_riscv64)
+    pd->xlen = 64;
+  else if (info->mach == bfd_mach_riscv32)
+    pd->xlen = 32;
+  else if (info->section != NULL)
+    {
+      Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
+      pd->xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
+    }
+}
+
+/* Try to print target address of cm.jalt and cm.jt.  */
+
+static void
+maybe_print_jvt_address (struct riscv_private_data *pd, int index)
+{
+  if (!pd->has_jvt_base || pd->jvt_section == NULL || pd->jvt_content == NULL
+      || pd->xlen == 0)
+    return;
+
+  bfd_size_type entry_size = pd->xlen / 8;
+  bfd_size_type offset = entry_size * (bfd_size_type) index;
+  if (index < 0 || index > (ZCMT_TOTAL_ENTRIES - 1)
+      || offset + entry_size > pd->jvt_size)
+    return;
+
+  bfd_byte *packet = pd->jvt_content + offset;
+
+  pd->to_print_addr = true;
+  pd->print_addr = bfd_get_bits (packet, pd->xlen,
+				 bfd_big_endian (pd->jvt_section->owner));
+}
+
 /* Get Zcmp reg_list field.  */
 
 static void
@@ -789,6 +835,7 @@  print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info
 		case 'I':
 		  print (info->stream, dis_style_address_offset,
 			 "%" PRIu64, EXTRACT_ZCMT_INDEX (l));
+		  maybe_print_jvt_address (pd, EXTRACT_ZCMT_INDEX (l));
 		  break;
 		default:
 		  goto undefined_modifier;
@@ -1098,17 +1145,7 @@  riscv_disassemble_insn (bfd_vma memaddr,
   if (op != NULL)
     {
       /* If XLEN is not known, get its value from the ELF class.  */
-      if (pd->xlen != 0)
-	;
-      else if (info->mach == bfd_mach_riscv64)
-	pd->xlen = 64;
-      else if (info->mach == bfd_mach_riscv32)
-	pd->xlen = 32;
-      else if (info->section != NULL)
-	{
-	  Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
-	  pd->xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
-	}
+      riscv_set_xlen_from_elf (pd, info);
 
       /* If arch has the Zfinx extension, replace FPR with GPR.  */
       if (riscv_subset_supports (&pd->riscv_rps_dis, "zfinx"))
@@ -1132,11 +1169,13 @@  riscv_disassemble_insn (bfd_vma memaddr,
 	  if ((op->xlen_requirement != 0)
 	      && (op->xlen_requirement != pd->xlen))
 	    continue;
-	  /* Is this instruction supported by the current architecture?  */
+	  /* Is this instruction supported by the current architecture?
+	     Also recognize Zcmt when the JVT base symbol identifies its table.  */
 	  if (!pd->all_ext)
 	    {
 	      if (!riscv_multi_subset_supports (&pd->riscv_rps_dis,
-						op->insn_class))
+						op->insn_class)
+		  && !(pd->has_jvt_base && op->insn_class == INSN_CLASS_ZCMT))
 		continue;
 
 	      if (pd->xlen == 32
@@ -1565,6 +1604,51 @@  riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
   return info->bytes_per_chunk;
 }
 
+/* DATA is the function address in jump table entry.
+   Lookup the address in symbols and print the function name.  */
+static int
+riscv_disassemble_jvt (bfd_vma memaddr, insn_t data,
+		       const bfd_byte *packet,
+		       disassemble_info *info)
+{
+  struct riscv_private_data *pd = info->private_data;
+  if (pd == NULL || pd->xlen == 0 || info->section == NULL)
+    return -1;
+
+  bfd_size_type entry_size = pd->xlen / 8;
+  bfd_vma offset = memaddr - info->section->vma;
+  bfd_size_type entry_offset = (offset / entry_size) * entry_size;
+  bfd_size_type entry_delta = offset - entry_offset;
+  int idx = entry_offset / entry_size;
+  if (idx < 0 || idx > (ZCMT_TOTAL_ENTRIES - 1))
+    opcodes_error_handler (_ ("invalid jvt index: %i"), idx);
+
+  const char *inst_str;
+  if (idx <= ZCMT_JT_END)
+    inst_str = "jvt.jt";
+  else
+    inst_str = "jvt.jalt";
+
+  if (pd->jvt_content != NULL && entry_offset + entry_size <= pd->jvt_size)
+    packet = pd->jvt_content + entry_offset;
+  data = bfd_get_bits (packet, pd->xlen,
+		       bfd_big_endian (info->section->owner));
+
+  const char *funcname = "<unknown>";
+  for (int i = 0; i < info->symtab_size; i++)
+    {
+      bfd_vma addr = bfd_asymbol_value (info->symtab[i]);
+      if (addr == (bfd_vma) data)
+	{
+	  funcname = info->symtab[i]->name;
+	  break;
+	}
+    }
+  (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s[%i]:\t%s",
+				inst_str, idx, funcname);
+  return entry_size - entry_delta;
+}
+
 static bool
 riscv_init_disasm_info (struct disassemble_info *info)
 {
@@ -1577,7 +1661,34 @@  riscv_init_disasm_info (struct disassemble_info *info)
     pd->hi_addr[i] = -1;
   pd->to_print_addr = false;
 
+  pd->has_jvt_base = false;
   pd->has_gp = false;
+  pd->jvt_section = NULL;
+  pd->jvt_content = NULL;
+  pd->jvt_size = 0;
+
+  if (info->section != NULL)
+    {
+      bfd *abfd = info->section->owner;
+      if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
+	{
+	  asection *s = bfd_get_section_by_name (abfd, TABLE_JUMP_SEC_NAME);
+	  if (s != NULL && bfd_section_size (s) > 0)
+	    {
+	      bfd_size_type size = bfd_section_size (s);
+	      bfd_byte *buf = (bfd_byte *) xmalloc (size);
+	      if (bfd_get_section_contents (abfd, s, buf, 0, size))
+		{
+		  pd->jvt_section = s;
+		  pd->jvt_content = buf;
+		  pd->jvt_size = size;
+		}
+	      else
+		free (buf);
+	    }
+	}
+    }
+
   for (i = 0; i < info->symtab_size; i++)
     {
       asymbol *sym = info->symtab[i];
@@ -1586,9 +1697,18 @@  riscv_init_disasm_info (struct disassemble_info *info)
 	  pd->gp = bfd_asymbol_value (sym);
 	  pd->has_gp = true;
 	}
+
+      if (strcmp (bfd_asymbol_name (sym), RISCV_TABLE_JUMP_BASE_SYMBOL) == 0)
+	{
+	  bfd_vma jvt_base = bfd_asymbol_value (sym);
+	  if (pd->jvt_section != NULL
+	      && jvt_base == bfd_section_vma (pd->jvt_section))
+	    pd->has_jvt_base = true;
+	}
     }
 
   pd->xlen = 0;
+  riscv_set_xlen_from_elf (pd, info);
   pd->default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
   pd->default_priv_spec = PRIV_SPEC_CLASS_NONE;
 
@@ -1682,8 +1802,21 @@  print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
   ((struct riscv_private_data *) info->private_data)->last_map_state = mstate;
 
   /* Set the size to dump.  */
-  if (mstate == MAP_DATA
-      && (info->flags & DISASSEMBLE_DATA) == 0)
+  if (info->section != NULL
+      && strcmp (info->section->name, TABLE_JUMP_SEC_NAME) == 0)
+    {
+      struct riscv_private_data *pd = info->private_data;
+      riscv_set_xlen_from_elf (pd, info);
+
+      dump_size = pd->xlen / 8;
+      info->bytes_per_chunk = dump_size;
+      info->bytes_per_line = dump_size;
+      info->endian_code = bfd_big_endian (info->section->owner)
+			   ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
+      info->display_endian = info->endian_code;
+      riscv_disassembler = riscv_disassemble_jvt;
+    }
+  else if (mstate == MAP_DATA && (info->flags & DISASSEMBLE_DATA) == 0)
     {
       dump_size = riscv_data_length (memaddr, info);
       info->bytes_per_chunk = dump_size;
@@ -1913,5 +2046,6 @@  void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED)
     {
       riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
       free (pd->riscv_rps_dis.subset_list);
+      free (pd->jvt_content);
     }
 }