kvx: fix 32-bit build and validation

Message ID 20230822160142.ocnjgkuboicpncii@ws2202.lin.mbt.kalray.eu
State New
Headers
Series kvx: fix 32-bit build and validation |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_check--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply

Commit Message

Paul Iannetta Aug. 22, 2023, 4:01 p.m. UTC
  Hi,

This patch should fix most issues on 32-bit hosts, both the build and
the validation should work cleanly.

Can I merge it into master?
Paul
  

Comments

Alan Modra Aug. 23, 2023, 12:27 a.m. UTC | #1
On Tue, Aug 22, 2023 at 06:01:42PM +0200, Paul Iannetta via Binutils wrote:
>    kvx-*-*)
> -    targ_defvec=kvx_elf32_vec
> -#ifdef BFD64
> -    targ64_selvecs=kvx_elf64_vec
> -#endif
> +    targ_defvec=kvx_elf64_vec
> +    targ_selvecs="kvx_elf64_vec kvx_elf32_vec"
> +    want64=true

It isn't necessary to put kvx_elf64_vec into targ_selvecs, but doesn't
hurt.  I mention this only because..

> -    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> -    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> +    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
> +    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;

..I don't see kvx_elf64_linux_vec defined anywhere.  Delete it?

>    if (class == classes->imm_classes)
>      {
> -      unsigned long long uval = token_val_p
> +      uint64_t uval = token_val_p
>  	? token->val
>  	: strtoull (tok + (tok[0] == '-') + (tok[0] == '+'), NULL, 0);
> -      long long val = uval;
> -      long long pval = val < 0 ? -val : val;
> -      int neg_power2_p = val < 0 && !(pval & (pval - 1));
> +      uint64_t sign_bit = ((~0ULL >> 1) ^ ~0ULL);
> +      int64_t pval = sign_bit & uval ? 1 + (~0ULL >> 1) - (~sign_bit & uval) : (~sign_bit & uval);
> +      int neg_power2_p = (sign_bit & uval) && !(pval & (pval - 1));
>        unsigned int len = 8 * sizeof (pval) - __builtin_clzll (pval);
>        for (; class[cur].class_id != -1
>  	  && ((unsigned int) (class[cur].sz < 0 ? - class[cur].sz - !neg_power2_p : class[cur].sz) < len

I'm about to commit a patch I wrote yesterday that fixes ubsan
warnings in the above, which will make this change unnecessary (except
of course you might like to make the variables {u,}int64_t).  The
expressions you're using for sign_bit and pval to avoid signed integer
overflow will no doubt work (unless unsigned long long is larger than
uint64_t!), but are more complicated than the following.

 int64_t val = uval;
 int64_t pval = val < 0 ? -uval : uval;
 int neg_power2_p = val < 0 && !(uval & (uval - 1));

> index 3cf6b27976d..00aec6c858e 100644
> --- a/gas/config/tc-kvx.c
> +++ b/gas/config/tc-kvx.c
> @@ -1419,6 +1419,9 @@ kvx_set_cpu (void)
>    if (env.params.core == -1)
>        env.params.core = kvx_core_info->elf_core;
>  
> +  if (!strcmp (TARGET_OS, "linux-gnu"))
> +      env.params.osabi = ELFOSABI_GNU;
> +
>    int kvx_bfd_mach;
>    print_insn = kvx_print_insn;
>  

Do you really need this?  Other targets automatically use ELFOSABI_GNU
when the ELF bfd code detects known ABI extensions.  See
has_gnu_osabi.  Hmm, likely you shouldn't be setting e_ident in
tc-kvx.c:kvx_end.
  
Alan Modra Aug. 23, 2023, 3:16 a.m. UTC | #2
I've committed this part of your patch, with a small fix to targets.c
needed to get a successful 32-bit host --enable-targets=all build.

bfd/
	* Makefile.am: Move elf32-kvx.lo from BFD32_BACKENDS to
	BFD64_BACKENDS.  Remove elfxx-kvx.lo from BFD32_BACKENDS.
	Remove elfxx-kvx.c from BFD32_BACKENDS_CFILES.
	* Makefile.in: Regenerate.
	* config.bfd: Adjust targ_defvec and targ_selvecs and gate them
	behind BFD64.
	* configure.ac: Add target_size=64 to kvx_elf64_*vec.
	* configure: Regenerate.
	* elfnn-kvx.c (elfNN_kvx_stub_name): Cast rel->r_addend to
	uint64_t to match format string.
	(elfNN_kvx_relocate_section): Similarly for r_offset, and
	use PRIx64 in format string.
	* targets.c (_bfd_target_vector <kvx_elf32_vec>): Move inside
	#ifdef BFD64.
ld/
	* Makefile.am: Move eelf32kvx.c from ALL_EMULATION_SOURCES to
	ALL_64_EMULATION_SOURCES.
	* Makefile.in: Regenerate.

diff --git a/bfd/Makefile.am b/bfd/Makefile.am
index 8b0761db582..378c13198d6 100644
--- a/bfd/Makefile.am
+++ b/bfd/Makefile.am
@@ -318,7 +318,6 @@ BFD32_BACKENDS = \
 	elf32-i386.lo \
 	elf32-ip2k.lo \
 	elf32-iq2000.lo \
-	elf32-kvx.lo \
 	elf32-lm32.lo \
 	elf32-m32c.lo \
 	elf32-m32r.lo \
@@ -359,7 +358,6 @@ BFD32_BACKENDS = \
 	elf32-z80.lo \
 	elf32.lo \
 	elflink.lo \
-	elfxx-kvx.lo \
 	elfxx-sparc.lo \
 	elfxx-tilegx.lo \
 	elfxx-x86.lo \
@@ -495,7 +493,6 @@ BFD32_BACKENDS_CFILES = \
 	elf32-z80.c \
 	elf32.c \
 	elflink.c \
-	elfxx-kvx.c \
 	elfxx-sparc.c \
 	elfxx-tilegx.c \
 	elfxx-x86.c \
@@ -549,6 +546,7 @@ BFD64_BACKENDS = \
 	coff64-rs6000.lo \
 	elf32-aarch64.lo \
 	elf32-ia64.lo \
+	elf32-kvx.lo \
 	elf32-loongarch.lo \
 	elf32-mips.lo \
 	elf32-riscv.lo \
diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index cca093e1fef..bb530271fca 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -774,7 +774,6 @@ BFD32_BACKENDS = \
 	elf32-i386.lo \
 	elf32-ip2k.lo \
 	elf32-iq2000.lo \
-	elf32-kvx.lo \
 	elf32-lm32.lo \
 	elf32-m32c.lo \
 	elf32-m32r.lo \
@@ -815,7 +814,6 @@ BFD32_BACKENDS = \
 	elf32-z80.lo \
 	elf32.lo \
 	elflink.lo \
-	elfxx-kvx.lo \
 	elfxx-sparc.lo \
 	elfxx-tilegx.lo \
 	elfxx-x86.lo \
@@ -951,7 +949,6 @@ BFD32_BACKENDS_CFILES = \
 	elf32-z80.c \
 	elf32.c \
 	elflink.c \
-	elfxx-kvx.c \
 	elfxx-sparc.c \
 	elfxx-tilegx.c \
 	elfxx-x86.c \
@@ -1006,6 +1003,7 @@ BFD64_BACKENDS = \
 	coff64-rs6000.lo \
 	elf32-aarch64.lo \
 	elf32-ia64.lo \
+	elf32-kvx.lo \
 	elf32-loongarch.lo \
 	elf32-mips.lo \
 	elf32-riscv.lo \
diff --git a/bfd/config.bfd b/bfd/config.bfd
index 30927bdbe23..2a4622219ba 100644
--- a/bfd/config.bfd
+++ b/bfd/config.bfd
@@ -804,14 +804,13 @@ case "${targ}" in
     targ_selvecs=kvx_elf32_vec
     want64=true
     ;;
-#endif
 
   kvx-*-*)
-    targ_defvec=kvx_elf32_vec
-#ifdef BFD64
-    targ64_selvecs=kvx_elf64_vec
-#endif
+    targ_defvec=kvx_elf64_vec
+    targ_selvecs="kvx_elf64_vec kvx_elf32_vec"
+    want64=true
     ;;
+#endif
 
   lm32-*-elf | lm32-*-rtems*)
     targ_defvec=lm32_elf32_vec
diff --git a/bfd/configure b/bfd/configure
index 489531c0a7f..d43754c4787 100755
--- a/bfd/configure
+++ b/bfd/configure
@@ -13960,8 +13960,8 @@ do
     ip2k_elf32_vec)		 tb="$tb elf32-ip2k.lo elf32.lo $elf" ;;
     iq2000_elf32_vec)		 tb="$tb elf32-iq2000.lo elf32.lo $elf" ;;
     kvx_elf32_vec)		 tb="$tb elf32-kvx.lo elfxx-kvx.lo elf32.lo $elf $ipa" ;;
-    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
-    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
+    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
+    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
     lm32_elf32_vec)		 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     lm32_elf32_fdpic_vec)	 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     loongarch_elf32_vec)	 tb="$tb elf32-loongarch.lo elfxx-loongarch.lo elf32.lo elf-ifunc.lo $elf" ;;
diff --git a/bfd/configure.ac b/bfd/configure.ac
index 0d1cd814af6..3fdd12fe22d 100644
--- a/bfd/configure.ac
+++ b/bfd/configure.ac
@@ -496,8 +496,8 @@ do
     ip2k_elf32_vec)		 tb="$tb elf32-ip2k.lo elf32.lo $elf" ;;
     iq2000_elf32_vec)		 tb="$tb elf32-iq2000.lo elf32.lo $elf" ;;
     kvx_elf32_vec)		 tb="$tb elf32-kvx.lo elfxx-kvx.lo elf32.lo $elf $ipa" ;;
-    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
-    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
+    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
+    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
     lm32_elf32_vec)		 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     lm32_elf32_fdpic_vec)	 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     loongarch_elf32_vec)	 tb="$tb elf32-loongarch.lo elfxx-loongarch.lo elf32.lo elf-ifunc.lo $elf" ;;
diff --git a/bfd/elfnn-kvx.c b/bfd/elfnn-kvx.c
index 467d91fea53..95580d19930 100644
--- a/bfd/elfnn-kvx.c
+++ b/bfd/elfnn-kvx.c
@@ -736,7 +736,7 @@ elfNN_kvx_stub_name (const asection *input_section,
 	snprintf (stub_name, len, "%08x_%s+%" PRIx64 "x",
 		  (unsigned int) input_section->id,
 		  hash->root.root.root.string,
-		  rel->r_addend);
+		  (uint64_t) rel->r_addend);
     }
   else
     {
@@ -747,7 +747,7 @@ elfNN_kvx_stub_name (const asection *input_section,
 		  (unsigned int) input_section->id,
 		  (unsigned int) sym_sec->id,
 		  (unsigned int) ELFNN_R_SYM (rel->r_info),
-		  rel->r_addend);
+		  (uint64_t) rel->r_addend);
     }
 
   return stub_name;
@@ -2568,11 +2568,11 @@ elfNN_kvx_relocate_section (bfd *output_bfd,
 	  (*_bfd_error_handler)
 	    ((sym_type == STT_TLS
 	      /* xgettext:c-format */
-	      ? _("%pB(%pA+%#lx): %s used with TLS symbol %s")
+	      ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
 	      /* xgettext:c-format */
-	      : _("%pB(%pA+%#lx): %s used with non-TLS symbol %s")),
+	      : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
 	     input_bfd,
-	     input_section, (long) rel->r_offset, howto->name, name);
+	     input_section, (uint64_t) rel->r_offset, howto->name, name);
 	}
 
       /* Original aarch64 has relaxation handling for TLS here. */
diff --git a/bfd/targets.c b/bfd/targets.c
index 210beddaf11..63b3abbd287 100644
--- a/bfd/targets.c
+++ b/bfd/targets.c
@@ -1130,9 +1130,8 @@ static const bfd_target * const _bfd_target_vector[] =
 	&ip2k_elf32_vec,
 	&iq2000_elf32_vec,
 
-	&kvx_elf32_vec,
-
 #ifdef BFD64
+	&kvx_elf32_vec,
 	&kvx_elf64_vec,
 #endif
 
diff --git a/ld/Makefile.am b/ld/Makefile.am
index 07fac968bb7..be456275748 100644
--- a/ld/Makefile.am
+++ b/ld/Makefile.am
@@ -237,7 +237,6 @@ ALL_EMULATION_SOURCES = \
 	eelf32ip2k.c \
 	eelf32iq10.c \
 	eelf32iq2000.c \
-	eelf32kvx.c \
 	eelf32lm32.c \
 	eelf32lm32fd.c \
 	eelf32lppc.c \
@@ -408,6 +407,7 @@ ALL_64_EMULATION_SOURCES = \
 	eelf32ebmipvxworks.c \
 	eelf32elmip.c \
 	eelf32elmipvxworks.c \
+	eelf32kvx.c \
 	eelf32l4300.c \
 	eelf32lmip.c \
 	eelf32loongarch.c \
diff --git a/ld/Makefile.in b/ld/Makefile.in
index a2aa54b69d8..c6a79774f38 100644
--- a/ld/Makefile.in
+++ b/ld/Makefile.in
@@ -738,7 +738,6 @@ ALL_EMULATION_SOURCES = \
 	eelf32ip2k.c \
 	eelf32iq10.c \
 	eelf32iq2000.c \
-	eelf32kvx.c \
 	eelf32lm32.c \
 	eelf32lm32fd.c \
 	eelf32lppc.c \
@@ -908,6 +907,7 @@ ALL_64_EMULATION_SOURCES = \
 	eelf32ebmipvxworks.c \
 	eelf32elmip.c \
 	eelf32elmipvxworks.c \
+	eelf32kvx.c \
 	eelf32l4300.c \
 	eelf32lmip.c \
 	eelf32loongarch.c \
  
Paul Iannetta Aug. 23, 2023, 7:44 a.m. UTC | #3
Thank you very much for your comments!

On Wed, Aug 23, 2023 at 09:57:26AM +0930, Alan Modra wrote:
> On Tue, Aug 22, 2023 at 06:01:42PM +0200, Paul Iannetta via Binutils wrote:
> >    kvx-*-*)
> > -    targ_defvec=kvx_elf32_vec
> > -#ifdef BFD64
> > -    targ64_selvecs=kvx_elf64_vec
> > -#endif
> > +    targ_defvec=kvx_elf64_vec
> > +    targ_selvecs="kvx_elf64_vec kvx_elf32_vec"
> > +    want64=true
> 
> It isn't necessary to put kvx_elf64_vec into targ_selvecs, but doesn't
> hurt.  I mention this only because..

I'll remove it then.

> 
> > -    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> > -    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> > +    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
> > +    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
> 
> ..I don't see kvx_elf64_linux_vec defined anywhere.  Delete it?

Indeed, it somehow got missing from bfd/targets.c, it is mostly
similar to kvx_elf64_vec, and only exists to match what is done in the
linker with the emulation elf64kvx_linux.

> 
> >    if (class == classes->imm_classes)
> >      {
> > -      unsigned long long uval = token_val_p
> > +      uint64_t uval = token_val_p
> >  	? token->val
> >  	: strtoull (tok + (tok[0] == '-') + (tok[0] == '+'), NULL, 0);
> > -      long long val = uval;
> > -      long long pval = val < 0 ? -val : val;
> > -      int neg_power2_p = val < 0 && !(pval & (pval - 1));
> > +      uint64_t sign_bit = ((~0ULL >> 1) ^ ~0ULL);
> > +      int64_t pval = sign_bit & uval ? 1 + (~0ULL >> 1) - (~sign_bit & uval) : (~sign_bit & uval);
> > +      int neg_power2_p = (sign_bit & uval) && !(pval & (pval - 1));
> >        unsigned int len = 8 * sizeof (pval) - __builtin_clzll (pval);
> >        for (; class[cur].class_id != -1
> >  	  && ((unsigned int) (class[cur].sz < 0 ? - class[cur].sz - !neg_power2_p : class[cur].sz) < len
> 
> I'm about to commit a patch I wrote yesterday that fixes ubsan
> warnings in the above, which will make this change unnecessary (except
> of course you might like to make the variables {u,}int64_t).  The
> expressions you're using for sign_bit and pval to avoid signed integer
> overflow will no doubt work (unless unsigned long long is larger than
> uint64_t!), but are more complicated than the following.
> 
>  int64_t val = uval;
>  int64_t pval = val < 0 ? -uval : uval;
>  int neg_power2_p = val < 0 && !(uval & (uval - 1));

Thanks, I'll much prefer keeping something readable.

> 
> > index 3cf6b27976d..00aec6c858e 100644
> > --- a/gas/config/tc-kvx.c
> > +++ b/gas/config/tc-kvx.c
> > @@ -1419,6 +1419,9 @@ kvx_set_cpu (void)
> >    if (env.params.core == -1)
> >        env.params.core = kvx_core_info->elf_core;
> >  
> > +  if (!strcmp (TARGET_OS, "linux-gnu"))
> > +      env.params.osabi = ELFOSABI_GNU;
> > +
> >    int kvx_bfd_mach;
> >    print_insn = kvx_print_insn;
> >  
> 
> Do you really need this?  Other targets automatically use ELFOSABI_GNU
> when the ELF bfd code detects known ABI extensions.  See
> has_gnu_osabi.  Hmm, likely you shouldn't be setting e_ident in
> tc-kvx.c:kvx_end.

I thought I needed this because we may emit STB_GNU_UNIQUE symbols but
I was not aware of the fact that has_gnu_osabi updates itself
depending on the features used.  I'll fix that part accordingly.

Thanks,
Paul
  
Alan Modra Aug. 23, 2023, 12:14 p.m. UTC | #4
On Wed, Aug 23, 2023 at 09:44:25AM +0200, Paul Iannetta wrote:
> On Wed, Aug 23, 2023 at 09:57:26AM +0930, Alan Modra wrote:
> > ..I don't see kvx_elf64_linux_vec defined anywhere.  Delete it?
> 
> Indeed, it somehow got missing from bfd/targets.c, it is mostly
> similar to kvx_elf64_vec, and only exists to match what is done in the
> linker with the emulation elf64kvx_linux.

Unless you really need it, don't add it.  If you have two target
vectors that match kvx objects then people will run into "file format
is ambiguous" errors when attempting to use any of objdump, objcopy,
nm etc. on a kvx object.  You won't see that if you've built binutils
with one of the kvx targets as the default, but will if you've built
say --target=x86_64-linux-gnu --enable-targets=all.
  
Luis Machado Aug. 23, 2023, 1:39 p.m. UTC | #5
Thanks for pushing this Alan.

On 8/23/23 04:16, Alan Modra via Binutils wrote:
> I've committed this part of your patch, with a small fix to targets.c
> needed to get a successful 32-bit host --enable-targets=all build.
> 
> bfd/
> 	* Makefile.am: Move elf32-kvx.lo from BFD32_BACKENDS to
> 	BFD64_BACKENDS.  Remove elfxx-kvx.lo from BFD32_BACKENDS.
> 	Remove elfxx-kvx.c from BFD32_BACKENDS_CFILES.
> 	* Makefile.in: Regenerate.
> 	* config.bfd: Adjust targ_defvec and targ_selvecs and gate them
> 	behind BFD64.
> 	* configure.ac: Add target_size=64 to kvx_elf64_*vec.
> 	* configure: Regenerate.
> 	* elfnn-kvx.c (elfNN_kvx_stub_name): Cast rel->r_addend to
> 	uint64_t to match format string.
> 	(elfNN_kvx_relocate_section): Similarly for r_offset, and
> 	use PRIx64 in format string.
> 	* targets.c (_bfd_target_vector <kvx_elf32_vec>): Move inside
> 	#ifdef BFD64.
> ld/
> 	* Makefile.am: Move eelf32kvx.c from ALL_EMULATION_SOURCES to
> 	ALL_64_EMULATION_SOURCES.
> 	* Makefile.in: Regenerate.
> 
> diff --git a/bfd/Makefile.am b/bfd/Makefile.am
> index 8b0761db582..378c13198d6 100644
> --- a/bfd/Makefile.am
> +++ b/bfd/Makefile.am
> @@ -318,7 +318,6 @@ BFD32_BACKENDS = \
>  	elf32-i386.lo \
>  	elf32-ip2k.lo \
>  	elf32-iq2000.lo \
> -	elf32-kvx.lo \
>  	elf32-lm32.lo \
>  	elf32-m32c.lo \
>  	elf32-m32r.lo \
> @@ -359,7 +358,6 @@ BFD32_BACKENDS = \
>  	elf32-z80.lo \
>  	elf32.lo \
>  	elflink.lo \
> -	elfxx-kvx.lo \
>  	elfxx-sparc.lo \
>  	elfxx-tilegx.lo \
>  	elfxx-x86.lo \
> @@ -495,7 +493,6 @@ BFD32_BACKENDS_CFILES = \
>  	elf32-z80.c \
>  	elf32.c \
>  	elflink.c \
> -	elfxx-kvx.c \
>  	elfxx-sparc.c \
>  	elfxx-tilegx.c \
>  	elfxx-x86.c \
> @@ -549,6 +546,7 @@ BFD64_BACKENDS = \
>  	coff64-rs6000.lo \
>  	elf32-aarch64.lo \
>  	elf32-ia64.lo \
> +	elf32-kvx.lo \
>  	elf32-loongarch.lo \
>  	elf32-mips.lo \
>  	elf32-riscv.lo \
> diff --git a/bfd/Makefile.in b/bfd/Makefile.in
> index cca093e1fef..bb530271fca 100644
> --- a/bfd/Makefile.in
> +++ b/bfd/Makefile.in
> @@ -774,7 +774,6 @@ BFD32_BACKENDS = \
>  	elf32-i386.lo \
>  	elf32-ip2k.lo \
>  	elf32-iq2000.lo \
> -	elf32-kvx.lo \
>  	elf32-lm32.lo \
>  	elf32-m32c.lo \
>  	elf32-m32r.lo \
> @@ -815,7 +814,6 @@ BFD32_BACKENDS = \
>  	elf32-z80.lo \
>  	elf32.lo \
>  	elflink.lo \
> -	elfxx-kvx.lo \
>  	elfxx-sparc.lo \
>  	elfxx-tilegx.lo \
>  	elfxx-x86.lo \
> @@ -951,7 +949,6 @@ BFD32_BACKENDS_CFILES = \
>  	elf32-z80.c \
>  	elf32.c \
>  	elflink.c \
> -	elfxx-kvx.c \
>  	elfxx-sparc.c \
>  	elfxx-tilegx.c \
>  	elfxx-x86.c \
> @@ -1006,6 +1003,7 @@ BFD64_BACKENDS = \
>  	coff64-rs6000.lo \
>  	elf32-aarch64.lo \
>  	elf32-ia64.lo \
> +	elf32-kvx.lo \
>  	elf32-loongarch.lo \
>  	elf32-mips.lo \
>  	elf32-riscv.lo \
> diff --git a/bfd/config.bfd b/bfd/config.bfd
> index 30927bdbe23..2a4622219ba 100644
> --- a/bfd/config.bfd
> +++ b/bfd/config.bfd
> @@ -804,14 +804,13 @@ case "${targ}" in
>      targ_selvecs=kvx_elf32_vec
>      want64=true
>      ;;
> -#endif
>  
>    kvx-*-*)
> -    targ_defvec=kvx_elf32_vec
> -#ifdef BFD64
> -    targ64_selvecs=kvx_elf64_vec
> -#endif
> +    targ_defvec=kvx_elf64_vec
> +    targ_selvecs="kvx_elf64_vec kvx_elf32_vec"
> +    want64=true
>      ;;
> +#endif
>  
>    lm32-*-elf | lm32-*-rtems*)
>      targ_defvec=lm32_elf32_vec
> diff --git a/bfd/configure b/bfd/configure
> index 489531c0a7f..d43754c4787 100755
> --- a/bfd/configure
> +++ b/bfd/configure
> @@ -13960,8 +13960,8 @@ do
>      ip2k_elf32_vec)		 tb="$tb elf32-ip2k.lo elf32.lo $elf" ;;
>      iq2000_elf32_vec)		 tb="$tb elf32-iq2000.lo elf32.lo $elf" ;;
>      kvx_elf32_vec)		 tb="$tb elf32-kvx.lo elfxx-kvx.lo elf32.lo $elf $ipa" ;;
> -    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> -    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> +    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
> +    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
>      lm32_elf32_vec)		 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
>      lm32_elf32_fdpic_vec)	 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
>      loongarch_elf32_vec)	 tb="$tb elf32-loongarch.lo elfxx-loongarch.lo elf32.lo elf-ifunc.lo $elf" ;;
> diff --git a/bfd/configure.ac b/bfd/configure.ac
> index 0d1cd814af6..3fdd12fe22d 100644
> --- a/bfd/configure.ac
> +++ b/bfd/configure.ac
> @@ -496,8 +496,8 @@ do
>      ip2k_elf32_vec)		 tb="$tb elf32-ip2k.lo elf32.lo $elf" ;;
>      iq2000_elf32_vec)		 tb="$tb elf32-iq2000.lo elf32.lo $elf" ;;
>      kvx_elf32_vec)		 tb="$tb elf32-kvx.lo elfxx-kvx.lo elf32.lo $elf $ipa" ;;
> -    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> -    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
> +    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
> +    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
>      lm32_elf32_vec)		 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
>      lm32_elf32_fdpic_vec)	 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
>      loongarch_elf32_vec)	 tb="$tb elf32-loongarch.lo elfxx-loongarch.lo elf32.lo elf-ifunc.lo $elf" ;;
> diff --git a/bfd/elfnn-kvx.c b/bfd/elfnn-kvx.c
> index 467d91fea53..95580d19930 100644
> --- a/bfd/elfnn-kvx.c
> +++ b/bfd/elfnn-kvx.c
> @@ -736,7 +736,7 @@ elfNN_kvx_stub_name (const asection *input_section,
>  	snprintf (stub_name, len, "%08x_%s+%" PRIx64 "x",
>  		  (unsigned int) input_section->id,
>  		  hash->root.root.root.string,
> -		  rel->r_addend);
> +		  (uint64_t) rel->r_addend);
>      }
>    else
>      {
> @@ -747,7 +747,7 @@ elfNN_kvx_stub_name (const asection *input_section,
>  		  (unsigned int) input_section->id,
>  		  (unsigned int) sym_sec->id,
>  		  (unsigned int) ELFNN_R_SYM (rel->r_info),
> -		  rel->r_addend);
> +		  (uint64_t) rel->r_addend);
>      }
>  
>    return stub_name;
> @@ -2568,11 +2568,11 @@ elfNN_kvx_relocate_section (bfd *output_bfd,
>  	  (*_bfd_error_handler)
>  	    ((sym_type == STT_TLS
>  	      /* xgettext:c-format */
> -	      ? _("%pB(%pA+%#lx): %s used with TLS symbol %s")
> +	      ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
>  	      /* xgettext:c-format */
> -	      : _("%pB(%pA+%#lx): %s used with non-TLS symbol %s")),
> +	      : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
>  	     input_bfd,
> -	     input_section, (long) rel->r_offset, howto->name, name);
> +	     input_section, (uint64_t) rel->r_offset, howto->name, name);
>  	}
>  
>        /* Original aarch64 has relaxation handling for TLS here. */
> diff --git a/bfd/targets.c b/bfd/targets.c
> index 210beddaf11..63b3abbd287 100644
> --- a/bfd/targets.c
> +++ b/bfd/targets.c
> @@ -1130,9 +1130,8 @@ static const bfd_target * const _bfd_target_vector[] =
>  	&ip2k_elf32_vec,
>  	&iq2000_elf32_vec,
>  
> -	&kvx_elf32_vec,
> -
>  #ifdef BFD64
> +	&kvx_elf32_vec,
>  	&kvx_elf64_vec,
>  #endif
>  
> diff --git a/ld/Makefile.am b/ld/Makefile.am
> index 07fac968bb7..be456275748 100644
> --- a/ld/Makefile.am
> +++ b/ld/Makefile.am
> @@ -237,7 +237,6 @@ ALL_EMULATION_SOURCES = \
>  	eelf32ip2k.c \
>  	eelf32iq10.c \
>  	eelf32iq2000.c \
> -	eelf32kvx.c \
>  	eelf32lm32.c \
>  	eelf32lm32fd.c \
>  	eelf32lppc.c \
> @@ -408,6 +407,7 @@ ALL_64_EMULATION_SOURCES = \
>  	eelf32ebmipvxworks.c \
>  	eelf32elmip.c \
>  	eelf32elmipvxworks.c \
> +	eelf32kvx.c \
>  	eelf32l4300.c \
>  	eelf32lmip.c \
>  	eelf32loongarch.c \
> diff --git a/ld/Makefile.in b/ld/Makefile.in
> index a2aa54b69d8..c6a79774f38 100644
> --- a/ld/Makefile.in
> +++ b/ld/Makefile.in
> @@ -738,7 +738,6 @@ ALL_EMULATION_SOURCES = \
>  	eelf32ip2k.c \
>  	eelf32iq10.c \
>  	eelf32iq2000.c \
> -	eelf32kvx.c \
>  	eelf32lm32.c \
>  	eelf32lm32fd.c \
>  	eelf32lppc.c \
> @@ -908,6 +907,7 @@ ALL_64_EMULATION_SOURCES = \
>  	eelf32ebmipvxworks.c \
>  	eelf32elmip.c \
>  	eelf32elmipvxworks.c \
> +	eelf32kvx.c \
>  	eelf32l4300.c \
>  	eelf32lmip.c \
>  	eelf32loongarch.c \
>
  
Paul Iannetta Aug. 23, 2023, 2:40 p.m. UTC | #6
On Wed, Aug 23, 2023 at 09:44:12PM +0930, Alan Modra wrote:
> On Wed, Aug 23, 2023 at 09:44:25AM +0200, Paul Iannetta wrote:
> > On Wed, Aug 23, 2023 at 09:57:26AM +0930, Alan Modra wrote:
> > > ..I don't see kvx_elf64_linux_vec defined anywhere.  Delete it?
> > 
> > Indeed, it somehow got missing from bfd/targets.c, it is mostly
> > similar to kvx_elf64_vec, and only exists to match what is done in the
> > linker with the emulation elf64kvx_linux.
> 
> Unless you really need it, don't add it.  If you have two target
> vectors that match kvx objects then people will run into "file format
> is ambiguous" errors when attempting to use any of objdump, objcopy,
> nm etc. on a kvx object.  You won't see that if you've built binutils
> with one of the kvx targets as the default, but will if you've built
> say --target=x86_64-linux-gnu --enable-targets=all.

I'll get rid of it then.  Thanks.
  
Alan Modra Aug. 24, 2023, 3:12 a.m. UTC | #7
On Wed, Aug 23, 2023 at 04:39:19PM +0200, Paul Iannetta via Binutils wrote:
> As discussed earlier, I removed kvx_elf64_linux_vec, fix the handling of unique
> symbols by using has_gnu_osabi instead of overwriting e_ident, change (unsigned)
> long long into {u,}int64 and at some place int by int32_t.

I've committed these for you.  BTW, oss-fuzz is poking at the kvx
support and finding holes.  See pr30793.
  
Paul Iannetta Aug. 24, 2023, 6:26 a.m. UTC | #8
On Thu, Aug 24, 2023 at 12:42:33PM +0930, Alan Modra wrote:
> On Wed, Aug 23, 2023 at 04:39:19PM +0200, Paul Iannetta via Binutils wrote:
> > As discussed earlier, I removed kvx_elf64_linux_vec, fix the handling of unique
> > symbols by using has_gnu_osabi instead of overwriting e_ident, change (unsigned)
> > long long into {u,}int64 and at some place int by int32_t.
> 
> I've committed these for you.  BTW, oss-fuzz is poking at the kvx
> support and finding holes.  See pr30793.
> 

Thank you very much, I'll have a look.

Paul
  
Paul Iannetta Aug. 24, 2023, 8:49 a.m. UTC | #9
On Thu, Aug 24, 2023 at 12:42:33PM +0930, Alan Modra wrote:
> On Wed, Aug 23, 2023 at 04:39:19PM +0200, Paul Iannetta via Binutils wrote:
> > As discussed earlier, I removed kvx_elf64_linux_vec, fix the handling of unique
> > symbols by using has_gnu_osabi instead of overwriting e_ident, change (unsigned)
> > long long into {u,}int64 and at some place int by int32_t.
> 
> I've committed these for you.  BTW, oss-fuzz is poking at the kvx
> support and finding holes.  See pr30793.

The attached patch should fix pr30793.

Thanks,
Paul
  
Alan Modra Aug. 24, 2023, 9:08 a.m. UTC | #10
On Thu, Aug 24, 2023 at 10:49:06AM +0200, Paul Iannetta wrote:
> On Thu, Aug 24, 2023 at 12:42:33PM +0930, Alan Modra wrote:
> > On Wed, Aug 23, 2023 at 04:39:19PM +0200, Paul Iannetta via Binutils wrote:
> > > As discussed earlier, I removed kvx_elf64_linux_vec, fix the handling of unique
> > > symbols by using has_gnu_osabi instead of overwriting e_ident, change (unsigned)
> > > long long into {u,}int64 and at some place int by int32_t.
> > 
> > I've committed these for you.  BTW, oss-fuzz is poking at the kvx
> > support and finding holes.  See pr30793.
> 
> The attached patch should fix pr30793.

decode_prologue_epilogue_bundle too?

> 
> Thanks,
> Paul
> 
> 

> >From 03c35cf18fa689bc7d727c506d30c396244cb307 Mon Sep 17 00:00:00 2001
> From: Paul Iannetta <piannetta@kalrayinc.com>
> Date: Thu, 24 Aug 2023 10:39:14 +0200
> Subject: [PATCH] kvx: fix kvx_reassemble_bundle index 8 out of bounds
> 
> opcodes/ChangeLog:
> 
> 2023-08-24  Paul Iannetta  <piannetta@kalrayinc.com>
> 
> 	* kvx-dis.c (print_insn_kvx): Change the loop condition so that
> 	  wordcount is always less than KVXMAXBUNDLEWORDS.
> ---
>  opcodes/kvx-dis.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/opcodes/kvx-dis.c b/opcodes/kvx-dis.c
> index e63430a9e57..a78eb5f84ec 100644
> --- a/opcodes/kvx-dis.c
> +++ b/opcodes/kvx-dis.c
> @@ -1056,7 +1056,7 @@ print_insn_kvx (bfd_vma memaddr, struct disassemble_info *info)
>  	  wordcount++;
>  	}
>        while (kvx_has_parallel_bit (bundle_words[wordcount - 1])
> -	     && wordcount < KVXMAXBUNDLEWORDS);
> +	     && wordcount < KVXMAXBUNDLEWORDS - 1);
>        invalid_bundle = kvx_reassemble_bundle (wordcount, &insncount);
>      }
>  
> -- 
> 2.35.1.500.gb896f729e2
>
  
Paul Iannetta Aug. 24, 2023, 9:26 a.m. UTC | #11
On Thu, Aug 24, 2023 at 06:38:25PM +0930, Alan Modra wrote:
> On Thu, Aug 24, 2023 at 10:49:06AM +0200, Paul Iannetta wrote:
> > On Thu, Aug 24, 2023 at 12:42:33PM +0930, Alan Modra wrote:
> > > On Wed, Aug 23, 2023 at 04:39:19PM +0200, Paul Iannetta via Binutils wrote:
> > > > As discussed earlier, I removed kvx_elf64_linux_vec, fix the handling of unique
> > > > symbols by using has_gnu_osabi instead of overwriting e_ident, change (unsigned)
> > > > long long into {u,}int64 and at some place int by int32_t.
> > > 
> > > I've committed these for you.  BTW, oss-fuzz is poking at the kvx
> > > support and finding holes.  See pr30793.
> > 
> > The attached patch should fix pr30793.
> 
> decode_prologue_epilogue_bundle too?

Indeed, thanks. (Currently, this is deadcode, because it is only used
by gdb which we have not contribued back yet).

> 
> > 
> > Thanks,
> > Paul
> > 
> > 
> 
> > >From 03c35cf18fa689bc7d727c506d30c396244cb307 Mon Sep 17 00:00:00 2001
> > From: Paul Iannetta <piannetta@kalrayinc.com>
> > Date: Thu, 24 Aug 2023 10:39:14 +0200
> > Subject: [PATCH] kvx: fix kvx_reassemble_bundle index 8 out of bounds
> > 
> > opcodes/ChangeLog:
> > 
> > 2023-08-24  Paul Iannetta  <piannetta@kalrayinc.com>
> > 
> > 	* kvx-dis.c (print_insn_kvx): Change the loop condition so that
> > 	  wordcount is always less than KVXMAXBUNDLEWORDS.
> > ---
> >  opcodes/kvx-dis.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/opcodes/kvx-dis.c b/opcodes/kvx-dis.c
> > index e63430a9e57..a78eb5f84ec 100644
> > --- a/opcodes/kvx-dis.c
> > +++ b/opcodes/kvx-dis.c
> > @@ -1056,7 +1056,7 @@ print_insn_kvx (bfd_vma memaddr, struct disassemble_info *info)
> >  	  wordcount++;
> >  	}
> >        while (kvx_has_parallel_bit (bundle_words[wordcount - 1])
> > -	     && wordcount < KVXMAXBUNDLEWORDS);
> > +	     && wordcount < KVXMAXBUNDLEWORDS - 1);
> >        invalid_bundle = kvx_reassemble_bundle (wordcount, &insncount);
> >      }
> >  
> > -- 
> > 2.35.1.500.gb896f729e2
> > 
> 
> 
> -- 
> Alan Modra
> Australia Development Lab, IBM
> 
> 
> 
>
  
Alan Modra Sept. 7, 2023, 12:46 p.m. UTC | #12
On Thu, Aug 24, 2023 at 11:26:49AM +0200, Paul Iannetta wrote:
> > > 	* kvx-dis.c (print_insn_kvx): Change the loop condition so that
> > > 	  wordcount is always less than KVXMAXBUNDLEWORDS.

Hi Paul,
Does the following patch look good to you, and can you please craft a
testcase that exercises a maximum size bundle?

While the patch already committed for pr30793 prevents the asan error,
there is a problem: Now the last element of bundle_words never gets
written.  That's very likely wrong, or KVXMAXBUNDLEWORDS is too big.
So this patch rearranges things a little to support writing of all of
bundle_words and does the parallel bit checking only when filling
bundle_words.  In the normal case, kvx_reassemble_bundle will see
bundle_words[word_count-1] with the parallel bit clear and all other
words having it set.  In the error case where all words in
bundle_words have the parallel bit set, kvx_reassemble_bundle will be
passed a wordcount of KVXMAXBUNDLEWORDS + 1.  I've also made
kvx_reassemble_bundle return true for success rather than zero, and
removed the unnecessary check for zero wordcount.

	PR 30793
	* kvx-dis.c (kvx_reassemble_bundle): Return bool, true on success.
	Fail if wordcount is too large.  Don't check for wordcount zero.
	Don't check kvx_has_parallel_bit.
	(print_insn_kvx): Rewrite code reading bundle_words as a for loop.
	Don't stop reading at KVXMAXBUNDLEWORDS - 1.
	(decode_prologue_epilogue_bundle): Similarly.

diff --git a/opcodes/kvx-dis.c b/opcodes/kvx-dis.c
index ec9c7718b06..32865631569 100644
--- a/opcodes/kvx-dis.c
+++ b/opcodes/kvx-dis.c
@@ -257,7 +257,7 @@ kvx_dis_init (struct disassemble_info *info)
   env.initialized_p = 1;
 }
 
-static int
+static bool
 kvx_reassemble_bundle (int wordcount, int *_insncount)
 {
 
@@ -272,23 +272,23 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
   int mau_taken = 0;
   int lsu_taken = 0;
 
-  int i;
-  unsigned int j;
-
-  struct instr_s instr[KVXMAXBUNDLEISSUE];
-  assert (KVXMAXBUNDLEISSUE >= BundleIssue__);
-  memset (instr, 0, sizeof (instr));
-
   if (debug)
     fprintf (stderr, "kvx_reassemble_bundle: wordcount = %d\n", wordcount);
 
-  if (wordcount == 0)
+  if (wordcount > KVXMAXBUNDLEWORDS)
     {
       if (debug)
-	fprintf (stderr, "wordcount == 0\n");
-      return 1;
+	fprintf (stderr, "bundle exceeds maximum size\n");
+      return false;
     }
 
+  struct instr_s instr[KVXMAXBUNDLEISSUE];
+  memset (instr, 0, sizeof (instr));
+  assert (KVXMAXBUNDLEISSUE >= BundleIssue__);
+
+  int i;
+  unsigned int j;
+
   for (i = 0; i < wordcount; i++)
     {
       uint32_t syllable = bundle_words[i];
@@ -304,7 +304,7 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
 		    {
 		      if (debug)
 			fprintf (stderr, "Too many TCA instructions");
-		      return 1;
+		      return false;
 		    }
 		  if (debug)
 		    fprintf (stderr,
@@ -336,7 +336,7 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
 		    {
 		      if (debug)
 			fprintf (stderr, "Too many TCA instructions");
-		      return 1;
+		      return false;
 		    }
 		  if (debug)
 		    fprintf (stderr,
@@ -357,7 +357,7 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
 		    {
 		      if (debug)
 			fprintf (stderr, "Too many IMMX syllables");
-		      return 1;
+		      return false;
 		    }
 		  instr_p->immx[immx_count] = syllable;
 		  instr_p->immx_valid[immx_count] = 1;
@@ -425,7 +425,7 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
 	    {
 	      if (debug)
 		fprintf (stderr, "Too many ALU instructions");
-	      return 1;
+	      return false;
 	    }
 	  break;
 
@@ -434,7 +434,7 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
 	    {
 	      if (debug)
 		fprintf (stderr, "Too many MAU instructions");
-	      return 1;
+	      return false;
 	    }
 	  else
 	    {
@@ -453,7 +453,7 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
 	    {
 	      if (debug)
 		fprintf (stderr, "Too many LSU instructions");
-	      return 1;
+	      return false;
 	    }
 	  else
 	    {
@@ -466,21 +466,8 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
 	      lsu_taken = 1;
 	    }
 	}
-      if (!(kvx_has_parallel_bit (syllable)))
-	{
-	  if (debug)
-	    fprintf (stderr, "Stop! stop bit is set 0x%x\n", syllable);
-	  break;
-	}
       if (debug)
 	fprintf (stderr, "Continue %d < %d?\n", i, wordcount);
-
-    }
-  if (kvx_has_parallel_bit (bundle_words[i]))
-    {
-      if (debug)
-	fprintf (stderr, "bundle exceeds maximum size");
-      return 1;
     }
 
   /* Fill bundle_insn and count read syllables.  */
@@ -520,7 +507,7 @@ kvx_reassemble_bundle (int wordcount, int *_insncount)
     fprintf (stderr, "End => %d instructions\n", instr_idx);
 
   *_insncount = instr_idx;
-  return 0;
+  return true;
 }
 
 struct decoded_insn
@@ -1038,11 +1025,10 @@ print_insn_kvx (bfd_vma memaddr, struct disassemble_info *info)
      decentrifugate function.  */
   if (insnindex == 0)
     {
-      int wordcount = 0;
-      do
+      int wordcount;
+      for (wordcount = 0; wordcount < KVXMAXBUNDLEWORDS; wordcount++)
 	{
 	  int status;
-	  assert (wordcount < KVXMAXBUNDLEWORDS);
 	  status =
 	    (*info->read_memory_func) (memaddr + 4 * wordcount,
 				       (bfd_byte *) (bundle_words +
@@ -1053,11 +1039,11 @@ print_insn_kvx (bfd_vma memaddr, struct disassemble_info *info)
 					  info);
 	      return -1;
 	    }
-	  wordcount++;
+	  if (!kvx_has_parallel_bit (bundle_words[wordcount]))
+	    break;
 	}
-      while (kvx_has_parallel_bit (bundle_words[wordcount - 1])
-	     && wordcount < KVXMAXBUNDLEWORDS - 1);
-      invalid_bundle = kvx_reassemble_bundle (wordcount, &insncount);
+      wordcount++;
+      invalid_bundle = !kvx_reassemble_bundle (wordcount, &insncount);
     }
 
   assert (insnindex < KVXMAXBUNDLEISSUE);
@@ -1226,20 +1212,17 @@ decode_prologue_epilogue_bundle (bfd_vma memaddr,
     kvx_dis_init (info);
 
   /* Read the bundle.  */
-  nb_syl = 0;
-  do
+  for (nb_syl = 0; nb_syl < KVXMAXBUNDLEWORDS; nb_syl++)
     {
-      if (nb_syl >= KVXMAXBUNDLEWORDS)
-	return -1;
       if ((*info->read_memory_func) (memaddr + 4 * nb_syl,
 				     (bfd_byte *) &bundle_words[nb_syl], 4,
 				     info))
 	return -1;
-      nb_syl++;
+      if (!kvx_has_parallel_bit (bundle_words[nb_syl]))
+	break;
     }
-  while (kvx_has_parallel_bit (bundle_words[nb_syl - 1])
-	 && nb_syl < KVXMAXBUNDLEWORDS - 1);
-  if (kvx_reassemble_bundle (nb_syl, &nb_insn))
+  nb_syl++;
+  if (!kvx_reassemble_bundle (nb_syl, &nb_insn))
     return -1;
 
   /* Check for extension to right if this is not the end of bundle
  
Paul Iannetta Sept. 7, 2023, 3:27 p.m. UTC | #13
On Thu, Sep 07, 2023 at 10:16:28PM +0930, Alan Modra wrote:
> On Thu, Aug 24, 2023 at 11:26:49AM +0200, Paul Iannetta wrote:
> > > > 	* kvx-dis.c (print_insn_kvx): Change the loop condition so that
> > > > 	  wordcount is always less than KVXMAXBUNDLEWORDS.
> 
> Hi Paul,
> Does the following patch look good to you, and can you please craft a
> testcase that exercises a maximum size bundle?
> 
> While the patch already committed for pr30793 prevents the asan error,
> there is a problem: Now the last element of bundle_words never gets
> written.  That's very likely wrong, or KVXMAXBUNDLEWORDS is too big.
> So this patch rearranges things a little to support writing of all of
> bundle_words and does the parallel bit checking only when filling
> bundle_words.  In the normal case, kvx_reassemble_bundle will see
> bundle_words[word_count-1] with the parallel bit clear and all other
> words having it set.  In the error case where all words in
> bundle_words have the parallel bit set, kvx_reassemble_bundle will be
> passed a wordcount of KVXMAXBUNDLEWORDS + 1.  I've also made
> kvx_reassemble_bundle return true for success rather than zero, and
> removed the unnecessary check for zero wordcount.

Hi Alan,

Thank you for catching this, your patch looks good to me.  Below is a
patch which adds a test case, you can merge it with yours if you want.

Thanks,
--
Paul
Kalray


commit 8dbb32052cb0e1e5ea1569e3fb708582f8158290
Author: Paul Iannetta <piannetta@kalrayinc.com>
Date:   Thu Sep 7 17:10:01 2023 +0200

    kvx: Add a testcase for bundles with KVXMAXBUNDLEWORDS syllables

    The previous fix (f1917fc) for pr30793 introduced a bug when dealing
    with bundles of with KVXMAXBUNDLEWORDS syllables.  This adds a test case
    to ensure that such bundle are correctly disassembled.

    gas/ChangeLog:

    2023-09-07  Paul Iannetta  <piannetta@kalrayinc.com>

            * testsuite/gas/kvx/fat-bundles.s: New test.
            * testsuite/gas/kvx/kv3-1-fat-bundles.d: New test.
            * testsuite/gas/kvx/kv3-2-fat-bundles.d: New test.

diff --git a/gas/testsuite/gas/kvx/fat-bundles.s b/gas/testsuite/gas/kvx/fat-bundles.s
new file mode 100644
index 00000000000..8119c6043de
--- /dev/null
+++ b/gas/testsuite/gas/kvx/fat-bundles.s
@@ -0,0 +1,13 @@
+# The bundles in this file all have 8 syllables.
+
+	addd $r0 = $r0, 123456789010	# 1 ALU + 2 Immediate Extensions
+	addd $r0 = $r0, 123456789010	# 1 ALU + 2 Immediate Extensions
+	addd $r1 = $r2, 1234		# 1 ALU + 1 Immediate Extension
+	;;
+	igoto $r0			# 1 BCU
+	xmt44d $a0a1a2a3 = $a0a1a2a3	# 1 TCA
+	addd $r0 = $r0, 1234		# 1 ALU + 1 Immediate Extension
+	addd $r0 = $r0, 12345678901	# 1 ALU + 1 Immediate Extension
+	fmuld $r1 = $r2, $r3		# 1 MAU
+	lwz $r0 = 0[$r0]		# 1 LSU
+	;;
diff --git a/gas/testsuite/gas/kvx/kv3-1-fat-bundles.d b/gas/testsuite/gas/kvx/kv3-1-fat-bundles.d
new file mode 100644
index 00000000000..1f80e0444fd
--- /dev/null
+++ b/gas/testsuite/gas/kvx/kv3-1-fat-bundles.d
@@ -0,0 +1,20 @@
+#as: -march=kv3-1
+#objdump: -d
+#source: fat-bundles.s
+.*\/fat-bundles.o:     file format elf64-kvx
+
+
+Disassembly of section .text:
+
+0000000000000000 <.text>:
+   0:	82 34 04 e1 80 84 00 e1                         	addd \$r1 = \$r2, 1234 \(0x4d2\)
+   8:	80 84 00 e1 01 00 00 80 46 a6 2f 8f             	addd \$r0 = \$r0, 123456789010 \(0x1cbe991a12\)
+  14:	00 00 00 88 46 a6 2f 97 00 00 00 10             	addd \$r0 = \$r0, 123456789010 \(0x1cbe991a12\);;
+
+  20:	00 00 d8 8f                                     	igoto \$r0
+  24:	00 00 0c 84                                     	xmt44d \$a0a1a2a3 = \$a0a1a2a3
+  28:	80 34 00 e1 40 0d 00 e1                         	addd \$r0 = \$r0, 1234 \(0x4d2\)
+  30:	c2 70 05 d8 00 00 00 b0                         	addd \$r0 = \$r0, 12345678901 \(0x2dfdc1c35\)
+  38:	01 00 00 80                                     	fmuld \$r1 = \$r2, \$r3
+  3c:	07 f7 b7 08                                     	lwz \$r0 = 0 \(0x0\)\[\$r0\];;
+
diff --git a/gas/testsuite/gas/kvx/kv3-2-fat-bundles.d b/gas/testsuite/gas/kvx/kv3-2-fat-bundles.d
new file mode 100644
index 00000000000..54717eb55b4
--- /dev/null
+++ b/gas/testsuite/gas/kvx/kv3-2-fat-bundles.d
@@ -0,0 +1,20 @@
+#as: -march=kv3-2
+#objdump: -d
+#source: fat-bundles.s
+.*\/fat-bundles.o:     file format elf64-kvx
+
+
+Disassembly of section .text:
+
+0000000000000000 <.text>:
+   0:	82 34 04 e2 80 84 00 e2                         	addd \$r1 = \$r2, 1234 \(0x4d2\)
+   8:	80 84 00 e2 01 00 00 80 46 a6 2f 8f             	addd \$r0 = \$r0, 123456789010 \(0x1cbe991a12\)
+  14:	00 00 00 88 46 a6 2f 97 00 00 00 10             	addd \$r0 = \$r0, 123456789010 \(0x1cbe991a12\);;
+
+  20:	00 00 d8 8f                                     	igoto \$r0
+  24:	00 10 05 87                                     	xcopyv.td \$a0a1a2a3 = \$a0a1a2a3
+  28:	80 34 00 e2 40 0d 00 e2                         	addd \$r0 = \$r0, 1234 \(0x4d2\)
+  30:	c2 70 05 d8 00 00 00 b0                         	addd \$r0 = \$r0, 12345678901 \(0x2dfdc1c35\)
+  38:	01 00 00 80                                     	fmuld \$r1 = \$r2, \$r3
+  3c:	07 f7 b7 08                                     	lwz \$r0 = 0 \(0x0\)\[\$r0\];;
+
  

Patch

From 6900d3245c59cc4b4da026328e74beae66a05b9d Mon Sep 17 00:00:00 2001
From: Paul Iannetta <piannetta@kalrayinc.com>
Date: Thu, 17 Aug 2023 16:08:29 +0200
Subject: [PATCH] kvx: fix 32-bit build and validation

gas/ChangeLog:

2023-08-22  Paul Iannetta  <piannetta@kalrayinc.com>

	* config/tc-kvx.h (struct token_s): Use int64_t and uint64_t
	  instead of long long and unsigned long long.
	* config/tc-kvx.c (kvx_set_cpu): Use ELFOSABI_GNU for the
	  kvx-linux target.
	(struct token_list):
	* config/kvx-parse.c (promote_token): Likewise.
	(print_token_list): Likewise.
	(get_token_class): Likewise and fix immediate selection on
	32-bit hosts.

bfd/ChangeLog:

2023-08-22  Paul Iannetta  <piannetta@kalrayinc.com>

	* Makefile.am: Move elf32-kvx.lo from BFD32_BACKENDS to
	  BFD64_BACKENDS.  Remove elfxx-kvx.lo from BFD32_BACKENDS.
	  Remove elfxx-kvx.c from BFD32_BACKENDS_CFILES.
	* Makefile.in: Regenerate.
	* config.bfd: Adjust targ_defvec and targ_selvecs and gate them
	  behing BFD64.
	* configure.ac: Add target_size=64 to kvx_elf64_*vec.
	* configure: Regenerate.
	* elfnn-kvx.c (elfNN_kvx_stub_name): Cast rel->r_addend to
	  uint64_t.
	(elfNN_kvx_relocate_section): Likewise.

ld/ChangeLog:

2023-08-22  Paul Iannetta  <piannetta@kalrayinc.com>

	* Makefile.am: Move eelf32kvx.c from ALL_EMULATION_SOURCES to
	  ALL_64_EMULATION_SOURCES.
	* Makefile.in: Regenerate.
	* configure.tgt: Split long line.
---
 bfd/Makefile.am        |  4 +---
 bfd/Makefile.in        |  4 +---
 bfd/config.bfd         |  9 ++++-----
 bfd/configure          |  4 ++--
 bfd/configure.ac       |  4 ++--
 bfd/elfnn-kvx.c        | 10 +++++-----
 gas/config/kvx-parse.c | 26 +++++++++++++-------------
 gas/config/tc-kvx.c    |  3 +++
 gas/config/tc-kvx.h    | 10 +++++-----
 ld/Makefile.am         |  2 +-
 ld/Makefile.in         |  2 +-
 ld/configure.tgt       |  3 ++-
 12 files changed, 40 insertions(+), 41 deletions(-)

diff --git a/bfd/Makefile.am b/bfd/Makefile.am
index 8b0761db582..378c13198d6 100644
--- a/bfd/Makefile.am
+++ b/bfd/Makefile.am
@@ -318,7 +318,6 @@  BFD32_BACKENDS = \
 	elf32-i386.lo \
 	elf32-ip2k.lo \
 	elf32-iq2000.lo \
-	elf32-kvx.lo \
 	elf32-lm32.lo \
 	elf32-m32c.lo \
 	elf32-m32r.lo \
@@ -359,7 +358,6 @@  BFD32_BACKENDS = \
 	elf32-z80.lo \
 	elf32.lo \
 	elflink.lo \
-	elfxx-kvx.lo \
 	elfxx-sparc.lo \
 	elfxx-tilegx.lo \
 	elfxx-x86.lo \
@@ -495,7 +493,6 @@  BFD32_BACKENDS_CFILES = \
 	elf32-z80.c \
 	elf32.c \
 	elflink.c \
-	elfxx-kvx.c \
 	elfxx-sparc.c \
 	elfxx-tilegx.c \
 	elfxx-x86.c \
@@ -549,6 +546,7 @@  BFD64_BACKENDS = \
 	coff64-rs6000.lo \
 	elf32-aarch64.lo \
 	elf32-ia64.lo \
+	elf32-kvx.lo \
 	elf32-loongarch.lo \
 	elf32-mips.lo \
 	elf32-riscv.lo \
diff --git a/bfd/Makefile.in b/bfd/Makefile.in
index cca093e1fef..bb530271fca 100644
--- a/bfd/Makefile.in
+++ b/bfd/Makefile.in
@@ -774,7 +774,6 @@  BFD32_BACKENDS = \
 	elf32-i386.lo \
 	elf32-ip2k.lo \
 	elf32-iq2000.lo \
-	elf32-kvx.lo \
 	elf32-lm32.lo \
 	elf32-m32c.lo \
 	elf32-m32r.lo \
@@ -815,7 +814,6 @@  BFD32_BACKENDS = \
 	elf32-z80.lo \
 	elf32.lo \
 	elflink.lo \
-	elfxx-kvx.lo \
 	elfxx-sparc.lo \
 	elfxx-tilegx.lo \
 	elfxx-x86.lo \
@@ -951,7 +949,6 @@  BFD32_BACKENDS_CFILES = \
 	elf32-z80.c \
 	elf32.c \
 	elflink.c \
-	elfxx-kvx.c \
 	elfxx-sparc.c \
 	elfxx-tilegx.c \
 	elfxx-x86.c \
@@ -1006,6 +1003,7 @@  BFD64_BACKENDS = \
 	coff64-rs6000.lo \
 	elf32-aarch64.lo \
 	elf32-ia64.lo \
+	elf32-kvx.lo \
 	elf32-loongarch.lo \
 	elf32-mips.lo \
 	elf32-riscv.lo \
diff --git a/bfd/config.bfd b/bfd/config.bfd
index 30927bdbe23..2a4622219ba 100644
--- a/bfd/config.bfd
+++ b/bfd/config.bfd
@@ -804,14 +804,13 @@  case "${targ}" in
     targ_selvecs=kvx_elf32_vec
     want64=true
     ;;
-#endif
 
   kvx-*-*)
-    targ_defvec=kvx_elf32_vec
-#ifdef BFD64
-    targ64_selvecs=kvx_elf64_vec
-#endif
+    targ_defvec=kvx_elf64_vec
+    targ_selvecs="kvx_elf64_vec kvx_elf32_vec"
+    want64=true
     ;;
+#endif
 
   lm32-*-elf | lm32-*-rtems*)
     targ_defvec=lm32_elf32_vec
diff --git a/bfd/configure b/bfd/configure
index 489531c0a7f..d43754c4787 100755
--- a/bfd/configure
+++ b/bfd/configure
@@ -13960,8 +13960,8 @@  do
     ip2k_elf32_vec)		 tb="$tb elf32-ip2k.lo elf32.lo $elf" ;;
     iq2000_elf32_vec)		 tb="$tb elf32-iq2000.lo elf32.lo $elf" ;;
     kvx_elf32_vec)		 tb="$tb elf32-kvx.lo elfxx-kvx.lo elf32.lo $elf $ipa" ;;
-    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
-    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
+    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
+    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
     lm32_elf32_vec)		 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     lm32_elf32_fdpic_vec)	 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     loongarch_elf32_vec)	 tb="$tb elf32-loongarch.lo elfxx-loongarch.lo elf32.lo elf-ifunc.lo $elf" ;;
diff --git a/bfd/configure.ac b/bfd/configure.ac
index 0d1cd814af6..3fdd12fe22d 100644
--- a/bfd/configure.ac
+++ b/bfd/configure.ac
@@ -496,8 +496,8 @@  do
     ip2k_elf32_vec)		 tb="$tb elf32-ip2k.lo elf32.lo $elf" ;;
     iq2000_elf32_vec)		 tb="$tb elf32-iq2000.lo elf32.lo $elf" ;;
     kvx_elf32_vec)		 tb="$tb elf32-kvx.lo elfxx-kvx.lo elf32.lo $elf $ipa" ;;
-    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
-    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa" ;;
+    kvx_elf64_vec)		 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
+    kvx_elf64_linux_vec)	 tb="$tb elf64-kvx.lo elfxx-kvx.lo elf64.lo $elf $ipa"; target_size=64 ;;
     lm32_elf32_vec)		 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     lm32_elf32_fdpic_vec)	 tb="$tb elf32-lm32.lo elf32.lo $elf" ;;
     loongarch_elf32_vec)	 tb="$tb elf32-loongarch.lo elfxx-loongarch.lo elf32.lo elf-ifunc.lo $elf" ;;
diff --git a/bfd/elfnn-kvx.c b/bfd/elfnn-kvx.c
index 77ff2483a28..a0a528ef9d2 100644
--- a/bfd/elfnn-kvx.c
+++ b/bfd/elfnn-kvx.c
@@ -740,7 +740,7 @@  elfNN_kvx_stub_name (const asection *input_section,
 	snprintf (stub_name, len, "%08x_%s+%" PRIx64 "x",
 		  (unsigned int) input_section->id,
 		  hash->root.root.root.string,
-		  rel->r_addend);
+		  (uint64_t) rel->r_addend);
     }
   else
     {
@@ -751,7 +751,7 @@  elfNN_kvx_stub_name (const asection *input_section,
 		  (unsigned int) input_section->id,
 		  (unsigned int) sym_sec->id,
 		  (unsigned int) ELFNN_R_SYM (rel->r_info),
-		  rel->r_addend);
+		  (uint64_t) rel->r_addend);
     }
 
   return stub_name;
@@ -2588,11 +2588,11 @@  elfNN_kvx_relocate_section (bfd *output_bfd,
 	  (*_bfd_error_handler)
 	    ((sym_type == STT_TLS
               /* xgettext:c-format */
-              ? _("%pB(%pA+%#lx): %s used with TLS symbol %s")
+              ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
               /* xgettext:c-format */
-              : _("%pB(%pA+%#lx): %s used with non-TLS symbol %s")),
+              : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
 	     input_bfd,
-	     input_section, (long) rel->r_offset, howto->name, name);
+	     input_section, (uint64_t) rel->r_offset, howto->name, name);
 	}
 
       /* Original aarch64 has relaxation handling for TLS here. */
diff --git a/gas/config/kvx-parse.c b/gas/config/kvx-parse.c
index ec3f912c476..c386d3eb66d 100644
--- a/gas/config/kvx-parse.c
+++ b/gas/config/kvx-parse.c
@@ -396,10 +396,10 @@  print_token (struct token_s token, char *buf, int bufsz)
     buf[i] = 0;
 }
 
-static long long
+static int64_t
 promote_token (struct token_s tok)
 {
-  long long cur_class = tok.class_id & -tok.class_id;
+  int64_t cur_class = tok.class_id & -tok.class_id;
   switch (tok.category)
     {
       case CAT_REGISTER:
@@ -414,8 +414,8 @@  promote_token (struct token_s tok)
 	  input_line_pointer = tok.insn + tok.begin;
 	  expression (&exp);
 	  input_line_pointer = ilp_save;
-	  long long new_class_id = tok.class_id;
-	  long long old_class_id = tok.class_id;
+	  int64_t new_class_id = tok.class_id;
+	  int64_t old_class_id = tok.class_id;
 	  while ((new_class_id = env.promote_immediate (old_class_id)) != old_class_id
 	      && ((exp.X_op == O_symbol && !has_relocation_of_size (str_hash_find (env.reloc_hash, TOKEN_NAME (new_class_id))))
 		|| (exp.X_op == 64 && !kvx_get_pseudo_func2 (exp.X_op_symbol, str_hash_find (env.reloc_hash, TOKEN_NAME (new_class_id))))))
@@ -443,7 +443,7 @@  is_insn (const struct token_s *token, struct token_class *classes)
   return res;
 }
 
-static long long
+static int64_t
 get_token_class (struct token_s *token, struct token_classes *classes, int insn_p, int modifier_p)
 {
   int cur = 0;
@@ -511,12 +511,12 @@  get_token_class (struct token_s *token, struct token_classes *classes, int insn_
 
   if (class == classes->imm_classes)
     {
-      unsigned long long uval = token_val_p
+      uint64_t uval = token_val_p
 	? token->val
 	: strtoull (tok + (tok[0] == '-') + (tok[0] == '+'), NULL, 0);
-      long long val = uval;
-      long long pval = val < 0 ? -val : val;
-      int neg_power2_p = val < 0 && !(pval & (pval - 1));
+      uint64_t sign_bit = ((~0ULL >> 1) ^ ~0ULL);
+      int64_t pval = sign_bit & uval ? 1 + (~0ULL >> 1) - (~sign_bit & uval) : (~sign_bit & uval);
+      int neg_power2_p = (sign_bit & uval) && !(pval & (pval - 1));
       unsigned int len = 8 * sizeof (pval) - __builtin_clzll (pval);
       for (; class[cur].class_id != -1
 	  && ((unsigned int) (class[cur].sz < 0 ? - class[cur].sz - !neg_power2_p : class[cur].sz) < len
@@ -652,11 +652,11 @@  print_token_list (struct token_list *lst)
   struct token_list *cur = lst;
   while (cur)
     {
-      printf_debug (1, "%s (%d : %s : %d) / ",
+      printf_debug (0, "%s (%lld : %s : %d) / ",
 	      cur->tok, cur->val, TOKEN_NAME (cur->class_id), cur->loc);
       cur = cur->next;
     }
-  printf_debug (1, "\n");
+  printf_debug (0, "\n");
 }
 
 void
@@ -804,7 +804,7 @@  retry:;
     parse_with_restarts (tok, cur_rule[i].jump_target, rules, errs);
   /* While parsing fails but there is hope since the current token can be
      promoted.  */
-  while (!fst_part && tok.class_id != (long long) promote_token (tok))
+  while (!fst_part && tok.class_id != (int64_t) promote_token (tok))
     {
       free_token_list (fst_part);
       tok.class_id = promote_token (tok);
@@ -854,7 +854,7 @@  retry:;
 
   printf_debug (1, "snd_part: Trying to match: %s\n", TOKEN_NAME (CLASS_ID (tok)));
   struct token_list *snd_part = parse_with_restarts (tok, cur_rule[i].stack_it, rules, errs);
-  while (!snd_part && tok.class_id != (long long) promote_token (tok))
+  while (!snd_part && tok.class_id != (int64_t) promote_token (tok))
     {
       tok.class_id = promote_token (tok);
       printf_debug (1, ">> Restart with %s?\n", TOKEN_NAME (CLASS_ID (tok)));
diff --git a/gas/config/tc-kvx.c b/gas/config/tc-kvx.c
index 3cf6b27976d..00aec6c858e 100644
--- a/gas/config/tc-kvx.c
+++ b/gas/config/tc-kvx.c
@@ -1419,6 +1419,9 @@  kvx_set_cpu (void)
   if (env.params.core == -1)
       env.params.core = kvx_core_info->elf_core;
 
+  if (!strcmp (TARGET_OS, "linux-gnu"))
+      env.params.osabi = ELFOSABI_GNU;
+
   int kvx_bfd_mach;
   print_insn = kvx_print_insn;
 
diff --git a/gas/config/tc-kvx.h b/gas/config/tc-kvx.h
index 11787bf0532..f8587540ca5 100644
--- a/gas/config/tc-kvx.h
+++ b/gas/config/tc-kvx.h
@@ -43,15 +43,15 @@  struct token_s {
   char *insn;
   int begin, end;
   int category;
-  long long class_id;
-  unsigned long long val;
+  int64_t class_id;
+  uint64_t val;
 };
 
 struct token_list
 {
   char *tok;
-  unsigned long long val;
-  long long class_id;
+  uint64_t val;
+  int64_t class_id;
   int category;
   int loc;
   struct token_list *next;
@@ -61,7 +61,7 @@  struct token_list
 
 struct token_class {
   const char ** class_values;
-  long long class_id;
+  int64_t class_id;
   int sz;
 };
 
diff --git a/ld/Makefile.am b/ld/Makefile.am
index 07fac968bb7..be456275748 100644
--- a/ld/Makefile.am
+++ b/ld/Makefile.am
@@ -237,7 +237,6 @@  ALL_EMULATION_SOURCES = \
 	eelf32ip2k.c \
 	eelf32iq10.c \
 	eelf32iq2000.c \
-	eelf32kvx.c \
 	eelf32lm32.c \
 	eelf32lm32fd.c \
 	eelf32lppc.c \
@@ -408,6 +407,7 @@  ALL_64_EMULATION_SOURCES = \
 	eelf32ebmipvxworks.c \
 	eelf32elmip.c \
 	eelf32elmipvxworks.c \
+	eelf32kvx.c \
 	eelf32l4300.c \
 	eelf32lmip.c \
 	eelf32loongarch.c \
diff --git a/ld/Makefile.in b/ld/Makefile.in
index a2aa54b69d8..c6a79774f38 100644
--- a/ld/Makefile.in
+++ b/ld/Makefile.in
@@ -738,7 +738,6 @@  ALL_EMULATION_SOURCES = \
 	eelf32ip2k.c \
 	eelf32iq10.c \
 	eelf32iq2000.c \
-	eelf32kvx.c \
 	eelf32lm32.c \
 	eelf32lm32fd.c \
 	eelf32lppc.c \
@@ -908,6 +907,7 @@  ALL_64_EMULATION_SOURCES = \
 	eelf32ebmipvxworks.c \
 	eelf32elmip.c \
 	eelf32elmipvxworks.c \
+	eelf32kvx.c \
 	eelf32l4300.c \
 	eelf32lmip.c \
 	eelf32loongarch.c \
diff --git a/ld/configure.tgt b/ld/configure.tgt
index f78ba07e5ca..ff0aaaae257 100644
--- a/ld/configure.tgt
+++ b/ld/configure.tgt
@@ -476,7 +476,8 @@  lm32-*-*)		targ_emul=elf32lm32
 kvx-*-linux*)		targ_emul=elf64kvx_linux
 			targ_extra_emuls="elf64kvx"
 			;;
-kvx-*-*)		targ_emul=elf64kvx; targ_extra_emuls="elf64kvx_linux elf32kvx"
+kvx-*-*)		targ_emul=elf64kvx;
+			targ_extra_emuls="elf64kvx_linux elf32kvx"
 			;;
 m32c-*-elf)
 			targ_emul=elf32m32c
-- 
2.35.1.500.gb896f729e2