public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Alan Modra <amodra@gmail.com>
To: binutils@sourceware.org
Cc: Paul Iannetta <piannetta@kalrayinc.com>
Subject: kvx bfd signed calculations and _bfd_kvx_elf_resolve_relocation
Date: Wed, 23 Aug 2023 11:12:03 +0930	[thread overview]
Message-ID: <ZOVj60Dhkc+eyrPE@squeak.grove.modra.org> (raw)

It is generally a good idea to avoid signed arithmetic on values
extracted from object files, to avoid ubsan warnings on overflow.
This patch replaces some uses of bfd_signed_vma in the kvx backend
with bfd_vma, and removes _bfd_kvx_elf_resolve_relocation, a
do-nothing function.  In the process of making this patch I noticed
some dead code in the GOT entry handling, setting value to
got_entry_addr but using "off" in the _bfd_final_link_relocate call.
Since kvx_calculate_got_entry_vma also returns the GOT offset, I
presume the code is correct, but I've left the dead code and comment
there for someone else to fix.

	* elfxx-kvx.h (_bfd_kvx_elf_resolve_relocation): Delete.
	* elfxx-kvx.c (kvx_signed_overflow): Rewrite using unsigned
	arithmetic.
	(_bfd_kvx_elf_resolve_relocation): Delete.
	* elfnn-kvx.c (kvx_relocate): Update for
	_bfd_kvx_elf_resolve_relocation removal.
	(elfNN_kvx_final_link_relocate): Likewise.  Don't use a signed
	addend.

diff --git a/bfd/elfnn-kvx.c b/bfd/elfnn-kvx.c
index ff2bdb64894..467d91fea53 100644
--- a/bfd/elfnn-kvx.c
+++ b/bfd/elfnn-kvx.c
@@ -670,14 +670,9 @@ kvx_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
 	      bfd_vma offset, bfd_vma value)
 {
   reloc_howto_type *howto;
-  bfd_vma place;
 
   howto = elfNN_kvx_howto_from_type (input_bfd, r_type);
-  place = (input_section->output_section->vma + input_section->output_offset
-	   + offset);
-
   r_type = elfNN_kvx_bfd_reloc_from_type (input_bfd, r_type);
-  value = _bfd_kvx_elf_resolve_relocation (r_type, place, value, 0, false);
   return _bfd_kvx_elf_put_addend (input_bfd,
 				  input_section->contents + offset, r_type,
 				  howto, value);
@@ -2000,7 +1995,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
   unsigned long r_symndx;
   bfd_byte *hit_data = contents + rel->r_offset;
   bfd_vma place, off;
-  bfd_signed_vma signed_addend;
+  bfd_vma addend;
   struct elf_kvx_link_hash_table *globals;
   bool weak_undef_p;
   asection *base_got;
@@ -2030,8 +2025,8 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 
   /* Get addend, accumulating the addend for consecutive relocs
      which refer to the same offset.  */
-  signed_addend = saved_addend ? *saved_addend : 0;
-  signed_addend += rel->r_addend;
+  addend = saved_addend ? *saved_addend : 0;
+  addend += rel->r_addend;
 
   weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
 		  : bfd_is_und_section (sym_sec));
@@ -2078,7 +2073,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 	  skip = false;
 	  relocate = false;
 
-	  outrel.r_addend = signed_addend;
+	  outrel.r_addend = addend;
 	  outrel.r_offset =
 	    _bfd_elf_section_offset (output_bfd, info, input_section,
 				     rel->r_offset);
@@ -2160,24 +2155,24 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 	    return bfd_reloc_ok;
 
 	  rret = check_signed_overflow (complain_overflow_signed, bfd_r_type,
-					input_bfd, value + signed_addend);
+					input_bfd, value + addend);
 	  if (rret != bfd_reloc_ok)
 	    return rret;
 
 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
 					   contents, rel->r_offset, value,
-					   signed_addend);
+					   addend);
 	}
 
     skip_because_pic:
       rret = check_signed_overflow (complain_overflow_signed, bfd_r_type,
-				    input_bfd, value + signed_addend);
+				    input_bfd, value + addend);
       if (rret != bfd_reloc_ok)
 	return rret;
 
       return _bfd_final_link_relocate (howto, input_bfd, input_section,
 				       contents, rel->r_offset, value,
-				       signed_addend);
+				       addend);
       break;
 
     case BFD_RELOC_KVX_PCREL17:
@@ -2231,7 +2226,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 		       + stub_entry->stub_sec->output_section->vma);
 	    /* We have redirected the destination to stub entry address,
 	       so ignore any addend record in the original rela entry.  */
-	    signed_addend = 0;
+	    addend = 0;
 	  }
       }
       *unresolved_reloc_p = false;
@@ -2250,7 +2245,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
     case BFD_RELOC_KVX_S43_PCREL_EX6:
       return _bfd_final_link_relocate (howto, input_bfd, input_section,
 				       contents, rel->r_offset, value,
-				       signed_addend);
+				       addend);
       break;
 
     case BFD_RELOC_KVX_S37_TLS_LE_LO10:
@@ -2261,8 +2256,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
     case BFD_RELOC_KVX_S43_TLS_LE_EX6:
       return _bfd_final_link_relocate (howto, input_bfd, input_section,
 				       contents, rel->r_offset,
-				       value - tpoff_base (info),
-				       signed_addend);
+				       value - tpoff_base (info), addend);
       break;
 
     case BFD_RELOC_KVX_S37_TLS_DTPOFF_LO10:
@@ -2273,8 +2267,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
     case BFD_RELOC_KVX_S43_TLS_DTPOFF_EX6:
       return _bfd_final_link_relocate (howto, input_bfd, input_section,
 				       contents, rel->r_offset,
-				       value - dtpoff_base (info),
-				       signed_addend);
+				       value - dtpoff_base (info), addend);
 
     case BFD_RELOC_KVX_S37_TLS_GD_UP27:
     case BFD_RELOC_KVX_S37_TLS_GD_LO10:
@@ -2302,8 +2295,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
       value = symbol_got_offset (input_bfd, h, r_symndx);
 
       _bfd_final_link_relocate (howto, input_bfd, input_section,
-				contents, rel->r_offset, value,
-				signed_addend);
+				contents, rel->r_offset, value, addend);
       *unresolved_reloc_p = false;
       break;
 
@@ -2326,7 +2318,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 
 	return _bfd_final_link_relocate (howto, input_bfd, input_section,
 					 contents, rel->r_offset, value,
-					 signed_addend);
+					 addend);
       }
       break;
 
@@ -2347,7 +2339,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 	value -= basegot->output_section->vma + basegot->output_offset;
 	return _bfd_final_link_relocate (howto, input_bfd, input_section,
 					 contents, rel->r_offset, value,
-					 signed_addend);
+					 addend);
       }
       break;
 
@@ -2373,18 +2365,15 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 	  printf("GOT_LO/HI for %s, value %x\n", h->root.root.string, value);
 #endif
 
-	  /* value = _bfd_kvx_elf_resolve_relocation (bfd_r_type, place, value, */
-	  /* 					       0, weak_undef_p); */
 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
 					   contents, rel->r_offset, value,
-					   signed_addend);
+					   addend);
 	}
       else
 	{
 #ifdef UGLY_DEBUG
 	  printf("GOT_LO/HI with h NULL, initial value %x\n", value);
 #endif
-	  bfd_vma addend = 0;
 	  struct elf_kvx_local_symbol *locals = elf_kvx_locals (input_bfd);
 
 	  if (locals == NULL)
@@ -2433,8 +2422,7 @@ elfNN_kvx_final_link_relocate (reloc_howto_type *howto,
 	  value = got_entry_addr;
 
 	  return _bfd_final_link_relocate (howto, input_bfd, input_section,
-					   contents, rel->r_offset, off,
-					   addend);
+					   contents, rel->r_offset, off, 0);
 	}
       break;
 
diff --git a/bfd/elfxx-kvx.c b/bfd/elfxx-kvx.c
index 8b41e26d027..c3c6cdad282 100644
--- a/bfd/elfxx-kvx.c
+++ b/bfd/elfxx-kvx.c
@@ -46,13 +46,12 @@ kvx_unsigned_overflow (bfd_vma value, unsigned int bits)
 static bfd_reloc_status_type
 kvx_signed_overflow (bfd_vma value, unsigned int bits)
 {
-  bfd_signed_vma svalue = (bfd_signed_vma) value;
-  bfd_signed_vma lim;
+  bfd_vma lim;
 
   if (bits >= sizeof (bfd_vma) * 8)
     return bfd_reloc_ok;
-  lim = (bfd_signed_vma) 1 << (bits - 1);
-  if (svalue < -lim || svalue >= lim)
+  lim = (bfd_vma) 1 << (bits - 1);
+  if (value + lim >= lim * 2)
     return bfd_reloc_overflow;
   return bfd_reloc_ok;
 }
@@ -150,14 +149,6 @@ _bfd_kvx_elf_put_addend (bfd *abfd,
   return status;
 }
 
-bfd_vma
-_bfd_kvx_elf_resolve_relocation (bfd_reloc_code_real_type r_type ATTRIBUTE_UNUSED,
-        bfd_vma place ATTRIBUTE_UNUSED, bfd_vma value,
-        bfd_vma addend ATTRIBUTE_UNUSED, bool weak_undef_p ATTRIBUTE_UNUSED)
-{
-  return value;
-}
-
 bool
 _bfd_kvx_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
 {
diff --git a/bfd/elfxx-kvx.h b/bfd/elfxx-kvx.h
index 06ddc3c8616..0e8d800dd48 100644
--- a/bfd/elfxx-kvx.h
+++ b/bfd/elfxx-kvx.h
@@ -51,11 +51,6 @@ extern bfd_reloc_status_type
 _bfd_kvx_elf_put_addend (bfd *, bfd_byte *, bfd_reloc_code_real_type,
 			     reloc_howto_type *, bfd_signed_vma);
 
-bfd_vma
-_bfd_kvx_elf_resolve_relocation (bfd_reloc_code_real_type r_type,
-				bfd_vma place, bfd_vma value,
-				bfd_vma addend, bool weak_undef_p);
-
 bool
 kvx_elf32_init_stub_bfd (struct bfd_link_info *info,
 			bfd *stub_bfd);

-- 
Alan Modra
Australia Development Lab, IBM

                 reply	other threads:[~2023-08-23  1:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZOVj60Dhkc+eyrPE@squeak.grove.modra.org \
    --to=amodra@gmail.com \
    --cc=binutils@sourceware.org \
    --cc=piannetta@kalrayinc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).