public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] or1k: Avoid R_OR1K_GOT16 signed overflow by using special howto
@ 2022-01-02  0:03 Stafford Horne
  2022-01-02 20:21 ` Giulio Benetti
  0 siblings, 1 reply; 2+ messages in thread
From: Stafford Horne @ 2022-01-02  0:03 UTC (permalink / raw)
  To: GNU Binutils; +Cc: giulio.benetti, Openrisc, Stafford Horne

Previously when fixing PR 21464 we masked out upper bits of the
relocation value in order to avoid overflow complaints when acceptable.
It turns out this does not work when the relocation value ends up being
signed.

To fix this this patch introduces a special howto with
complain_on_overflow set to complain_overflow_dont.  This is used in
place of the normal R_OR1K_GOT16 howto when we detect R_OR1K_GOT_AHI16
relocations.

bfd/ChangeLog:

	PR 28735
	* elf32-or1k.c (or1k_elf_got16_no_overflow_howto): Define.
	(or1k_elf_relocate_section): Use new howto instead of trying to
	mask out relocation bits.
---
 bfd/elf32-or1k.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/bfd/elf32-or1k.c b/bfd/elf32-or1k.c
index 4f9092539f5..a2ff47473a7 100644
--- a/bfd/elf32-or1k.c
+++ b/bfd/elf32-or1k.c
@@ -828,6 +828,23 @@ static reloc_howto_type or1k_elf_howto_table[] =
 	 false),		/* pcrel_offset */
 };
 
+/* A copy of the R_OR1K_GOT16 used in the presense of R_OR1K_GOT_AHI16
+   relocations when we know we can ignore overflows.  */
+static reloc_howto_type or1k_elf_got16_no_overflow_howto =
+  HOWTO (R_OR1K_GOT16,		/* type */
+	 0,			/* rightshift */
+	 2,			/* size (0 = byte, 1 = short, 2 = long) */
+	 16,			/* bitsize */
+	 false,			/* pc_relative */
+	 0,			/* bitpos */
+	 complain_overflow_dont, /* complain_on_overflow */
+	 bfd_elf_generic_reloc, /* special_function */
+	 "R_OR1K_GOT16",	/* name */
+	 false,			/* partial_inplace */
+	 0,			/* src_mask */
+	 0xffff,		/* dst_mask */
+	 false);		/* pcrel_offset */
+
 /* Map BFD reloc types to Or1k ELF reloc types.  */
 
 struct or1k_reloc_map
@@ -1506,12 +1523,11 @@ or1k_elf_relocate_section (bfd *output_bfd,
 	    if (r_type == R_OR1K_GOT_AHI16)
 	      saw_gotha = true;
 
-	    /* If we have a R_OR1K_GOT16 followed by a R_OR1K_GOT_AHI16
+	    /* If we have a R_OR1K_GOT16 following a R_OR1K_GOT_AHI16
 	       relocation we assume the code is doing the right thing to avoid
-	       overflows.  Here we mask the lower 16-bit of the relocation to
-	       avoid overflow validation failures.  */
+	       overflows.  */
 	    if (r_type == R_OR1K_GOT16 && saw_gotha)
-	      relocation &= 0xffff;
+	      howto = &or1k_elf_got16_no_overflow_howto;
 
 	  /* Addend should be zero.  */
 	  if (rel->r_addend != 0)
-- 
2.31.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] or1k: Avoid R_OR1K_GOT16 signed overflow by using special howto
  2022-01-02  0:03 [PATCH] or1k: Avoid R_OR1K_GOT16 signed overflow by using special howto Stafford Horne
@ 2022-01-02 20:21 ` Giulio Benetti
  0 siblings, 0 replies; 2+ messages in thread
From: Giulio Benetti @ 2022-01-02 20:21 UTC (permalink / raw)
  To: Stafford Horne, GNU Binutils; +Cc: Openrisc

Hi Stafford, All,

On 02/01/22 01:03, Stafford Horne wrote:
> Previously when fixing PR 21464 we masked out upper bits of the
> relocation value in order to avoid overflow complaints when acceptable.
> It turns out this does not work when the relocation value ends up being
> signed.
> 
> To fix this this patch introduces a special howto with
> complain_on_overflow set to complain_overflow_dont.  This is used in
> place of the normal R_OR1K_GOT16 howto when we detect R_OR1K_GOT_AHI16
> relocations.
> 
> bfd/ChangeLog:
> 
> 	PR 28735
> 	* elf32-or1k.c (or1k_elf_got16_no_overflow_howto): Define.
> 	(or1k_elf_relocate_section): Use new howto instead of trying to
> 	mask out relocation bits.
> ---
>   bfd/elf32-or1k.c | 24 ++++++++++++++++++++----
>   1 file changed, 20 insertions(+), 4 deletions(-)
> 
> diff --git a/bfd/elf32-or1k.c b/bfd/elf32-or1k.c
> index 4f9092539f5..a2ff47473a7 100644
> --- a/bfd/elf32-or1k.c
> +++ b/bfd/elf32-or1k.c
> @@ -828,6 +828,23 @@ static reloc_howto_type or1k_elf_howto_table[] =
>   	 false),		/* pcrel_offset */
>   };
>   
> +/* A copy of the R_OR1K_GOT16 used in the presense of R_OR1K_GOT_AHI16
> +   relocations when we know we can ignore overflows.  */
> +static reloc_howto_type or1k_elf_got16_no_overflow_howto =
> +  HOWTO (R_OR1K_GOT16,		/* type */
> +	 0,			/* rightshift */
> +	 2,			/* size (0 = byte, 1 = short, 2 = long) */
> +	 16,			/* bitsize */
> +	 false,			/* pc_relative */
> +	 0,			/* bitpos */
> +	 complain_overflow_dont, /* complain_on_overflow */
> +	 bfd_elf_generic_reloc, /* special_function */
> +	 "R_OR1K_GOT16",	/* name */
> +	 false,			/* partial_inplace */
> +	 0,			/* src_mask */
> +	 0xffff,		/* dst_mask */
> +	 false);		/* pcrel_offset */
> +
>   /* Map BFD reloc types to Or1k ELF reloc types.  */
>   
>   struct or1k_reloc_map
> @@ -1506,12 +1523,11 @@ or1k_elf_relocate_section (bfd *output_bfd,
>   	    if (r_type == R_OR1K_GOT_AHI16)
>   	      saw_gotha = true;
>   
> -	    /* If we have a R_OR1K_GOT16 followed by a R_OR1K_GOT_AHI16
> +	    /* If we have a R_OR1K_GOT16 following a R_OR1K_GOT_AHI16
>   	       relocation we assume the code is doing the right thing to avoid
> -	       overflows.  Here we mask the lower 16-bit of the relocation to
> -	       avoid overflow validation failures.  */
> +	       overflows.  */
>   	    if (r_type == R_OR1K_GOT16 && saw_gotha)
> -	      relocation &= 0xffff;
> +	      howto = &or1k_elf_got16_no_overflow_howto;
>   
>   	  /* Addend should be zero.  */
>   	  if (rel->r_addend != 0)
> 

this patch fixes the problem.

Thanks a lot
Best regards
-- 
Giulio Benetti
Benetti Engineering sas

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-02 20:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-02  0:03 [PATCH] or1k: Avoid R_OR1K_GOT16 signed overflow by using special howto Stafford Horne
2022-01-02 20:21 ` Giulio Benetti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).