[PATCH v5 1/3] ld: Add RISC-V --relax-tbljal options

Jiawei jiawei@iscas.ac.cn
Fri Nov 20 14:25:04 GMT 2026


Parse --relax-tbljal[=zcmt] and --no-relax-tbljal and record the
request in the RISC-V emulation parameters. Reject empty and unknown
schemes and leave the feature disabled by default.

Document the link modes, precedence and JVT base initialization
requirements. The next patch uses this parameter in the BFD relaxation
passes.

bfd/ChangeLog:

	* elfxx-riscv.h (struct riscv_elf_params): Add relax_zcmt.

ld/ChangeLog:

	* NEWS: Mention the opt-in RISC-V table-jump relaxation.
	* emultempl/riscvelf.em (params): Disable it by default.
	(PARSE_AND_LIST_LONGOPTS, PARSE_AND_LIST_OPTIONS)
	(PARSE_AND_LIST_ARGS_CASES): Register, describe and handle
	--relax-tbljal[=zcmt] and --no-relax-tbljal.
	* gen-doc.texi: Define RISCV for the target-independent manual.
	* ld.texi: Document the options, supported link modes and JVT
	base initialization requirement.
	* ldlex.h (enum option_values): Add OPTION_RELAX_TBLJAL and
	OPTION_NO_RELAX_TBLJAL.
---
 bfd/elfxx-riscv.h        |  2 ++
 ld/NEWS                  |  4 ++++
 ld/emultempl/riscvelf.em | 15 +++++++++++++
 ld/gen-doc.texi          |  1 +
 ld/ld.texi               | 46 ++++++++++++++++++++++++++++++++++++++++
 ld/ldlex.h               |  2 ++
 6 files changed, 70 insertions(+)

diff --git a/bfd/elfxx-riscv.h b/bfd/elfxx-riscv.h
index 80a56445b60..56c2567083c 100644
--- a/bfd/elfxx-riscv.h
+++ b/bfd/elfxx-riscv.h
@@ -66,6 +66,8 @@ struct riscv_elf_params
 {
   /* Whether to relax code sequences to GP-relative addressing.  */
   bool relax_gp;
+  /* Whether to relax code sequences for Zcmt.  */
+  bool relax_zcmt;
   /* Whether to check if SUB_ULEB128 relocation has non-zero addend.  */
   bool check_uleb128;
   /* How to mark the output for Zicfilp and Zicfiss.  */
diff --git a/ld/NEWS b/ld/NEWS
index c6438422396..56c549b513d 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,9 @@
 -*- text -*-
 
+* Add the RISC-V --relax-tbljal[=zcmt] and --no-relax-tbljal options for
+  opt-in Zcmt table-jump relaxation in non-position-independent executable
+  links.  An omitted scheme selects zcmt; unsupported schemes are errors.
+
 * The alpha target supports indirect functions (STT_GNU_IFUNC), using the
   newly allocated R_ALPHA_IRELATIVE (42) dynamic relocation.  No dynamic
   linker or C library startup code implements that relocation yet, so a
diff --git a/ld/emultempl/riscvelf.em b/ld/emultempl/riscvelf.em
index d86045bdeaa..2936a02400d 100644
--- a/ld/emultempl/riscvelf.em
+++ b/ld/emultempl/riscvelf.em
@@ -26,6 +26,7 @@ fragment <<EOF
 #include "elfxx-riscv.h"
 
 static struct riscv_elf_params params = { .relax_gp = 1,
+					  .relax_zcmt = false,
 					  .check_uleb128 = 0,
 					  .zicfilp = RISCV_ZICFILP_IMPLICIT,
 					  .zicfiss = RISCV_ZICFISS_IMPLICIT,
@@ -52,6 +53,8 @@ EOF
 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
     { "relax-gp", no_argument, NULL, OPTION_RELAX_GP },
     { "no-relax-gp", no_argument, NULL, OPTION_NO_RELAX_GP },
+    { "relax-tbljal", optional_argument, NULL, OPTION_RELAX_TBLJAL },
+    { "no-relax-tbljal", no_argument, NULL, OPTION_NO_RELAX_TBLJAL },
     { "check-uleb128", no_argument, NULL, OPTION_CHECK_ULEB128 },
     { "no-check-uleb128", no_argument, NULL, OPTION_NO_CHECK_ULEB128 },
 '
@@ -60,6 +63,8 @@ PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
   fprintf (file, _("  --relax-gp                  Perform GP relaxation\n"));
   fprintf (file, _("  --no-relax-gp               Don'\''t perform GP relaxation\n"));
   fprintf (file, _("  --check-uleb128             Check if SUB_ULEB128 has non-zero addend\n"));
+  fprintf (file, _("  --relax-tbljal[=zcmt]       Enable RISC-V table-jump relaxation (scheme: zcmt)\n"));
+  fprintf (file, _("  --no-relax-tbljal           Disable RISC-V table-jump relaxation (default)\n"));
   fprintf (file, _("  --no-check-uleb128          Don'\''t check if SUB_ULEB128 has non-zero addend\n"));
   fprintf (file, _("\
   -z zicfilp=[implicit|unlabeled|never]\n\
@@ -129,6 +134,16 @@ PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
       params.relax_gp = 0;
       break;
 
+    case OPTION_RELAX_TBLJAL:
+      if (optarg != NULL && strcmp (optarg, "zcmt") != 0)
+	fatal (_("%P: unsupported table-jump relaxation scheme: %s\n"), optarg);
+      params.relax_zcmt = true;
+      break;
+
+    case OPTION_NO_RELAX_TBLJAL:
+      params.relax_zcmt = false;
+      break;
+
     case OPTION_CHECK_ULEB128:
       params.check_uleb128 = 1;
       break;
diff --git a/ld/gen-doc.texi b/ld/gen-doc.texi
index 561869f485a..515aa871366 100644
--- a/ld/gen-doc.texi
+++ b/ld/gen-doc.texi
@@ -21,6 +21,7 @@
 @set PDP11
 @set POWERPC
 @set POWERPC64
+@set RISCV
 @set Renesas
 @set S/390
 @set SPU
diff --git a/ld/ld.texi b/ld/ld.texi
index 963d47fd59a..522aa403833 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -33,6 +33,7 @@
 @set PDP11
 @set POWERPC
 @set POWERPC64
+@set RISCV
 @set Renesas
 @set S/390
 @set SPU
@@ -4173,6 +4174,51 @@ in the PLT entries for MIPS R6.
 @c man end
 @end ifset
 
+@ifset RISCV
+@subsection Options specific to RISC-V targets
+
+@c man begin OPTIONS
+
+@table @gcctabopt
+
+@kindex --relax-tbljal
+@item --relax-tbljal
+@itemx --relax-tbljal=@var{scheme}
+Enable RISC-V table-jump relaxation using @var{scheme}.  The only supported
+scheme is currently @samp{zcmt}; omitting the argument is equivalent to
+@samp{--relax-tbljal=zcmt}.  An explicit scheme must follow @samp{=} with
+no intervening space.  Empty or unsupported scheme names are errors.
+
+Zcmt table-jump relaxation is performed only when the input architecture
+attributes permit the Zcmt extension.  The extension requires Zca and
+Zicsr, and is not compatible with Zcd.  This relaxation is disabled by
+default and is initially supported only for non-position-independent
+executable links.  It is conservatively disabled for PIC, PIE and shared
+links.
+
+The last @samp{--relax-tbljal} or @samp{--no-relax-tbljal} option determines
+whether table-jump relaxation is requested.  Disabling relaxation with
+@samp{--no-relax} also disables table-jump relaxation, regardless of where
+the table-jump option appears.
+
+When a non-empty @code{.riscv.jvt} section is emitted, the linker defines
+a local @code{__jvt_base$} symbol at the table base.  Programs using this
+relaxation must initialize the @code{jvt} CSR to @code{__jvt_base$}
+before executing @code{cm.jt} or @code{cm.jalt}.  The symbol is not
+defined when no profitable table is emitted; generic startup code that
+unconditionally references @code{__jvt_base$} is therefore unsupported
+unless it arranges an optional reference or is linked only when a JVT is
+emitted.
+
+@kindex --no-relax-tbljal
+@item --no-relax-tbljal
+Disable RISC-V table-jump relaxation.  This is the default.
+
+@end table
+
+@c man end
+@end ifset
+
 
 @ifset PDP11
 @subsection Options specific to PDP11 targets
diff --git a/ld/ldlex.h b/ld/ldlex.h
index 1f6e6e5d387..0a3d34acf9e 100644
--- a/ld/ldlex.h
+++ b/ld/ldlex.h
@@ -431,6 +431,8 @@ enum option_values
   OPTION_NO_RELAX_GP,
   OPTION_CHECK_ULEB128,
   OPTION_NO_CHECK_ULEB128,
+  OPTION_RELAX_TBLJAL,
+  OPTION_NO_RELAX_TBLJAL,
   /* Used by emultempl/rxelf.em.  */
   OPTION_NO_FLAG_MISMATCH_WARNINGS,
   OPTION_IGNORE_LMA,
-- 
2.43.0



More information about the Binutils mailing list