[PATCH] bfd: fix -Wunused errors with clang 13+

Simon Marchi simon.marchi@polymtl.ca
Fri Dec 3 21:31:09 GMT 2021


Clang 13 and 14 produce some -Wunused-but-set-{variable,parameter} for
situations where gcc doesn't.  In particular, when a variable is set and
then used in a way to update its own value.  For example, if `i` is only
used in this way:

  int i = 2;
  i++;
  i = i + 1;

gcc won't warn, but clang will.

Fix all such errors found in an --enable-targets=all build.  It would be
important for somebody who knows what they're doing to just make sure
that these variables can indeed be deleted, and that there a no cases
where it's a bug, and the variable should actually be used.

The first instance of this error fix by this patch is:

      CC       elf32-score.lo
    /home/simark/src/binutils-gdb/bfd/elf32-score.c:450:11: error: variable 'relocation' set but not used [-Werror,-Wunused-but-set-variable]
      bfd_vma relocation;
              ^

Change-Id: I2f233ce20352645cf388aff3dfa08a651d21a6b6
---
 bfd/ecofflink.c        |  7 -------
 bfd/elf-m10200.c       |  6 ------
 bfd/elf-m10300.c       |  6 ------
 bfd/elf32-bfin.c       |  5 +----
 bfd/elf32-cr16.c       |  5 +----
 bfd/elf32-lm32.c       |  5 +----
 bfd/elf32-microblaze.c | 14 --------------
 bfd/elf32-nds32.c      | 12 ++----------
 bfd/elf32-nios2.c      |  6 +-----
 bfd/elf32-score.c      | 13 ++-----------
 bfd/elf32-score7.c     | 11 +----------
 bfd/elf64-ia64-vms.c   |  6 ------
 12 files changed, 9 insertions(+), 87 deletions(-)

diff --git a/bfd/ecofflink.c b/bfd/ecofflink.c
index 9f72409d6b16..44cb1f3a5a68 100644
--- a/bfd/ecofflink.c
+++ b/bfd/ecofflink.c
@@ -2402,9 +2402,6 @@ _bfd_ecoff_locate_line (bfd *abfd,
 static bool
 ecoff_collect_shuffle (struct shuffle *l, bfd_byte *buff)
 {
-  unsigned long total;
-
-  total = 0;
   for (; l != (struct shuffle *) NULL; l = l->next)
     {
       if (! l->filep)
@@ -2416,7 +2413,6 @@ ecoff_collect_shuffle (struct shuffle *l, bfd_byte *buff)
 		  != l->size))
 	    return false;
 	}
-      total += l->size;
       buff += l->size;
     }
 
@@ -2451,13 +2447,11 @@ _bfd_ecoff_get_accumulated_ss (void * handle, bfd_byte *buff)
 {
   struct accumulate *ainfo = (struct accumulate *) handle;
   struct string_hash_entry *sh;
-  unsigned long total;
 
   /* The string table is written out from the hash table if this is a
      final link.  */
   BFD_ASSERT (ainfo->ss == (struct shuffle *) NULL);
   *buff++ = '\0';
-  total = 1;
   BFD_ASSERT (ainfo->ss_hash == NULL || ainfo->ss_hash->val == 1);
   for (sh = ainfo->ss_hash;
        sh != (struct string_hash_entry *) NULL;
@@ -2467,7 +2461,6 @@ _bfd_ecoff_get_accumulated_ss (void * handle, bfd_byte *buff)
 
       len = strlen (sh->root.string);
       memcpy (buff, sh->root.string, len + 1);
-      total += len + 1;
       buff += len + 1;
     }
 
diff --git a/bfd/elf-m10200.c b/bfd/elf-m10200.c
index 9c1e4eb70fc5..9cca778e7db2 100644
--- a/bfd/elf-m10200.c
+++ b/bfd/elf-m10200.c
@@ -795,14 +795,8 @@ mn10200_elf_relax_section (bfd *abfd,
       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
 	{
 	  Elf_Internal_Rela *nrel;
-	  bfd_vma value = symval;
 	  unsigned char code;
 
-	  /* Deal with pc-relative gunk.  */
-	  value -= (sec->output_section->vma + sec->output_offset);
-	  value -= (irel->r_offset + 1);
-	  value += irel->r_addend;
-
 	  /* Do nothing if this reloc is the last byte in the section.  */
 	  if (irel->r_offset == sec->size)
 	    continue;
diff --git a/bfd/elf-m10300.c b/bfd/elf-m10300.c
index fadd9ab0e82c..ab2c1304855b 100644
--- a/bfd/elf-m10300.c
+++ b/bfd/elf-m10300.c
@@ -3702,14 +3702,8 @@ mn10300_elf_relax_section (bfd *abfd,
       if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
 	{
 	  Elf_Internal_Rela *nrel;
-	  bfd_vma value = symval;
 	  unsigned char code;
 
-	  /* Deal with pc-relative gunk.  */
-	  value -= (sec->output_section->vma + sec->output_offset);
-	  value -= irel->r_offset;
-	  value += irel->r_addend;
-
 	  /* Do nothing if this reloc is the last byte in the section.  */
 	  if (irel->r_offset == sec->size)
 	    continue;
diff --git a/bfd/elf32-bfin.c b/bfd/elf32-bfin.c
index 037da60ef74e..9cf0064447e1 100644
--- a/bfd/elf32-bfin.c
+++ b/bfd/elf32-bfin.c
@@ -4109,7 +4109,7 @@ _bfinfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
 				   bool *changed)
 {
   Elf_Internal_Shdr *symtab_hdr;
-  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
+  struct elf_link_hash_entry **sym_hashes;
   Elf_Internal_Rela *rel, *erel;
 
   if ((sec->flags & SEC_RELOC) == 0
@@ -4118,9 +4118,6 @@ _bfinfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-  sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
-  if (!elf_bad_symtab (abfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
 
   rel = elf_section_data (sec)->relocs;
 
diff --git a/bfd/elf32-cr16.c b/bfd/elf32-cr16.c
index 2a1c8be624e4..b23ff34c10aa 100644
--- a/bfd/elf32-cr16.c
+++ b/bfd/elf32-cr16.c
@@ -700,7 +700,7 @@ cr16_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
 {
   Elf_Internal_Shdr *symtab_hdr;
   Elf_Internal_Sym * isymbuf = NULL;
-  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
+  struct elf_link_hash_entry **sym_hashes;
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
   bfd *      dynobj;
@@ -717,9 +717,6 @@ cr16_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-  sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
-  if (!elf_bad_symtab (abfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
 
   dynobj = elf_hash_table (info)->dynobj;
   local_got_offsets = elf_local_got_offsets (abfd);
diff --git a/bfd/elf32-lm32.c b/bfd/elf32-lm32.c
index be8d41632c17..779b6817dfd7 100644
--- a/bfd/elf32-lm32.c
+++ b/bfd/elf32-lm32.c
@@ -1120,7 +1120,7 @@ lm32_elf_check_relocs (bfd *abfd,
 		       const Elf_Internal_Rela *relocs)
 {
   Elf_Internal_Shdr *symtab_hdr;
-  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
+  struct elf_link_hash_entry **sym_hashes;
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
   struct elf_lm32_link_hash_table *htab;
@@ -1131,9 +1131,6 @@ lm32_elf_check_relocs (bfd *abfd,
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-  sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
-  if (!elf_bad_symtab (abfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
 
   htab = lm32_elf_hash_table (info);
   if (htab == NULL)
diff --git a/bfd/elf32-microblaze.c b/bfd/elf32-microblaze.c
index d9c0d93a01a6..b05ee9bca2b1 100644
--- a/bfd/elf32-microblaze.c
+++ b/bfd/elf32-microblaze.c
@@ -2088,8 +2088,6 @@ microblaze_elf_relax_section (bfd *abfd,
 		  if (isym->st_shndx == shndx
 		      && (ELF32_ST_TYPE (isym->st_info) == STT_SECTION))
 		    {
-		      bfd_vma immediate;
-
 		      if (ocontents == NULL)
 			{
 			  if (elf_section_data (o)->this_hdr.contents != NULL)
@@ -2112,15 +2110,7 @@ microblaze_elf_relax_section (bfd *abfd,
 			      elf_section_data (o)->this_hdr.contents = ocontents;
 			    }
 			}
-	  unsigned long instr_hi =  bfd_get_32 (abfd, ocontents
-						+ irelscan->r_offset);
-	  unsigned long instr_lo =  bfd_get_32 (abfd, ocontents
-						+ irelscan->r_offset
-						+ INST_WORD_SIZE);
-	  immediate = (instr_hi & 0x0000ffff) << 16;
-	  immediate |= (instr_lo & 0x0000ffff);
 		      offset = calc_fixup (irelscan->r_addend, 0, sec);
-		      immediate -= offset;
 		      irelscan->r_addend -= offset;
 		    }
 		}
@@ -2326,7 +2316,6 @@ microblaze_elf_check_relocs (bfd * abfd,
 {
   Elf_Internal_Shdr *		symtab_hdr;
   struct elf_link_hash_entry ** sym_hashes;
-  struct elf_link_hash_entry ** sym_hashes_end;
   const Elf_Internal_Rela *	rel;
   const Elf_Internal_Rela *	rel_end;
   struct elf32_mb_link_hash_table *htab;
@@ -2341,9 +2330,6 @@ microblaze_elf_check_relocs (bfd * abfd,
 
   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-  sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
-  if (!elf_bad_symtab (abfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
 
   rel_end = relocs + sec->reloc_count;
 
diff --git a/bfd/elf32-nds32.c b/bfd/elf32-nds32.c
index 0aa7c9ee564c..5b43ca978035 100644
--- a/bfd/elf32-nds32.c
+++ b/bfd/elf32-nds32.c
@@ -6973,7 +6973,7 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 			asection *sec, const Elf_Internal_Rela *relocs)
 {
   Elf_Internal_Shdr *symtab_hdr;
-  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
+  struct elf_link_hash_entry **sym_hashes;
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
   struct elf_link_hash_table *ehtab;
@@ -6990,10 +6990,6 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-  sym_hashes_end =
-    sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
-  if (!elf_bad_symtab (abfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
 
   ehtab = elf_hash_table (info);
   htab = nds32_elf_hash_table (info);
@@ -13478,12 +13474,8 @@ nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
   relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
 
   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
-  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
+  struct elf_link_hash_entry **sym_hashes;
   sym_hashes = elf_sym_hashes (inbfd);
-  sym_hashes_end =
-    sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
-  if (!elf_bad_symtab (inbfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
 
   /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
   if (bfd_link_relocatable (lnkinfo))
diff --git a/bfd/elf32-nios2.c b/bfd/elf32-nios2.c
index 5fb2e5dddf6d..c1a450b61d6a 100644
--- a/bfd/elf32-nios2.c
+++ b/bfd/elf32-nios2.c
@@ -4676,7 +4676,7 @@ nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
 			  asection *sec, const Elf_Internal_Rela *relocs)
 {
   Elf_Internal_Shdr *symtab_hdr;
-  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
+  struct elf_link_hash_entry **sym_hashes;
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
   struct elf32_nios2_link_hash_table *htab;
@@ -4688,10 +4688,6 @@ nios2_elf32_check_relocs (bfd *abfd, struct bfd_link_info *info,
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-  sym_hashes_end = (sym_hashes
-		    + symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
-  if (!elf_bad_symtab (abfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
   local_got_refcounts = elf_local_got_refcounts (abfd);
 
   htab = elf32_nios2_hash_table (info);
diff --git a/bfd/elf32-score.c b/bfd/elf32-score.c
index e8a7e6388afb..cf2a3a43a848 100644
--- a/bfd/elf32-score.c
+++ b/bfd/elf32-score.c
@@ -440,23 +440,14 @@ score_elf_final_gp (bfd *output_bfd,
 
 static bfd_reloc_status_type
 score_elf_gprel15_with_gp (bfd *abfd,
-			   asymbol *symbol,
 			   arelent *reloc_entry,
 			   asection *input_section,
 			   bool relocateable,
 			   void * data,
 			   bfd_vma gp ATTRIBUTE_UNUSED)
 {
-  bfd_vma relocation;
   unsigned long insn;
 
-  if (bfd_is_com_section (symbol->section))
-    relocation = 0;
-  else
-    relocation = symbol->value;
-
-  relocation += symbol->section->output_section->vma;
-  relocation += symbol->section->output_offset;
   if (reloc_entry->address > input_section->size)
     return bfd_reloc_outofrange;
 
@@ -547,8 +538,8 @@ score_elf_gprel15_reloc (bfd *abfd,
   if (ret != bfd_reloc_ok)
     return ret;
 
-  return score_elf_gprel15_with_gp (abfd, symbol, reloc_entry,
-					 input_section, relocateable, data, gp);
+  return score_elf_gprel15_with_gp (abfd, reloc_entry,
+				    input_section, relocateable, data, gp);
 }
 
 /* Do a R_SCORE_GPREL32 relocation.  This is a 32 bit value which must
diff --git a/bfd/elf32-score7.c b/bfd/elf32-score7.c
index 50d607de1561..b3eac5604825 100644
--- a/bfd/elf32-score7.c
+++ b/bfd/elf32-score7.c
@@ -340,23 +340,14 @@ score_elf_final_gp (bfd *output_bfd,
 
 static bfd_reloc_status_type
 score_elf_gprel15_with_gp (bfd *abfd,
-			   asymbol *symbol,
 			   arelent *reloc_entry,
 			   asection *input_section,
 			   bool relocateable,
 			   void * data,
 			   bfd_vma gp ATTRIBUTE_UNUSED)
 {
-  bfd_vma relocation;
   unsigned long insn;
 
-  if (bfd_is_com_section (symbol->section))
-    relocation = 0;
-  else
-    relocation = symbol->value;
-
-  relocation += symbol->section->output_section->vma;
-  relocation += symbol->section->output_offset;
   if (reloc_entry->address > input_section->size)
     return bfd_reloc_outofrange;
 
@@ -449,7 +440,7 @@ score_elf_gprel15_reloc (bfd *abfd,
   if (ret != bfd_reloc_ok)
     return ret;
 
-  return score_elf_gprel15_with_gp (abfd, symbol, reloc_entry,
+  return score_elf_gprel15_with_gp (abfd, reloc_entry,
 				    input_section, relocateable, data, gp);
 }
 
diff --git a/bfd/elf64-ia64-vms.c b/bfd/elf64-ia64-vms.c
index c72015b450c5..54645f00ce4f 100644
--- a/bfd/elf64-ia64-vms.c
+++ b/bfd/elf64-ia64-vms.c
@@ -2512,8 +2512,6 @@ allocate_dynrel_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
 
   for (rent = dyn_i->reloc_entries; rent; rent = rent->next)
     {
-      int count = rent->count;
-
       switch (rent->type)
 	{
 	case R_IA64_FPTR32LSB:
@@ -2538,10 +2536,6 @@ allocate_dynrel_entries (struct elf64_ia64_dyn_sym_info *dyn_i,
 	case R_IA64_IPLTLSB:
 	  if (!dynamic_symbol && !shared)
 	    continue;
-	  /* Use two REL relocations for IPLT relocations
-	     against local symbols.  */
-	  if (!dynamic_symbol)
-	    count *= 2;
 	  break;
 	case R_IA64_DTPREL32LSB:
 	case R_IA64_TPREL64LSB:
-- 
2.33.1



More information about the Binutils mailing list