[PATCH 5/6] PR binutils/22875: i860/ELF: Report unsupported relocation types

Maciej W. Rozycki macro@mips.com
Thu Mar 29 20:21:00 GMT 2018


Complement commit f3185997ac09 ("PR 22875: Stop strip corrupting unknown 
relocs"), <https://sourceware.org/ml/binutils/2018-02/msg00445.html>, 
and also set the `bfd_error_bad_value' error and report an unsupported 
relocation type if a howto lookup fails with the i860 backend, fixing a 
confusing `no error' error message and removing a binutils test failure:

failed with: <.../binutils/strip-new: tmpdir/bintest.o: no error>, expected: <.* bad value>
.../binutils/strip-new: tmpdir/bintest.o: no error
FAIL: binutils-all/strip-13

with the `i860-stardent-elf' target.

	bfd/
	* elf32-i860.c (lookup_howto): Add `abfd' parameter.  Set the 
	`bfd_error_bad_value' error and call `_bfd_error_handler' on a 
	howto lookup failure.
	(elf32_i860_reloc_type_lookup): Adjust `lookup_howto' call 
	accordingly.
	(elf32_i860_info_to_howto_rela): Likewise.
	(elf32_i860_relocate_splitn): Likewise.
	(elf32_i860_relocate_pc16): Likewise.
	(elf32_i860_relocate_pc26): Likewise.
	(elf32_i860_relocate_section): Likewise.
---
Hi,

 Regression-tested successfully.  OK to apply?

  Maciej
---
 bfd/elf32-i860.c |   39 +++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

binutils-i860-bfd-unsupp-reloc.diff
Index: binutils/bfd/elf32-i860.c
===================================================================
--- binutils.orig/bfd/elf32-i860.c	2018-03-02 12:47:50.000000000 +0000
+++ binutils/bfd/elf32-i860.c	2018-03-29 00:22:05.937257348 +0100
@@ -745,12 +745,13 @@ static reloc_howto_type elf32_i860_howto
 static unsigned char elf_code_to_howto_index[R_860_max + 1];
 
 static reloc_howto_type *
-lookup_howto (unsigned int rtype)
+lookup_howto (bfd *abfd, unsigned int rtype)
 {
   static int initialized = 0;
   int i;
   int howto_tbl_size = (int) (sizeof (elf32_i860_howto_table)
 			/ sizeof (elf32_i860_howto_table[0]));
+  reloc_howto_type *howto = NULL;
 
   if (! initialized)
     {
@@ -761,18 +762,24 @@ lookup_howto (unsigned int rtype)
 	elf_code_to_howto_index[elf32_i860_howto_table[i].type] = i;
     }
 
-  if (rtype > R_860_max)
-    return NULL;
-  i = elf_code_to_howto_index[rtype];
-  if (i >= howto_tbl_size)
-    return NULL;
-  return elf32_i860_howto_table + i;
+  if (rtype < R_860_max)
+    {
+      i = elf_code_to_howto_index[rtype];
+      if (i < howto_tbl_size)
+	howto = elf32_i860_howto_table + i;
+    }
+  if (howto == NULL)
+    {
+      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
+			  abfd, rtype);
+      bfd_set_error (bfd_error_bad_value);
+    }
+  return howto;
 }
 
 /* Given a BFD reloc, return the matching HOWTO structure.  */
 static reloc_howto_type *
-elf32_i860_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
-			      bfd_reloc_code_real_type code)
+elf32_i860_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
 {
   unsigned int rtype;
 
@@ -883,7 +890,7 @@ elf32_i860_reloc_type_lookup (bfd *abfd 
     default:
       return NULL;
     }
-  return lookup_howto (rtype);
+  return lookup_howto (abfd, rtype);
 }
 
 static reloc_howto_type *
@@ -906,12 +913,12 @@ elf32_i860_reloc_name_lookup (bfd *abfd 
 /* Given a ELF reloc, return the matching HOWTO structure.  */
 
 static bfd_boolean
-elf32_i860_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
+elf32_i860_info_to_howto_rela (bfd *abfd,
 			       arelent *bfd_reloc,
 			       Elf_Internal_Rela *elf_reloc)
 {
   bfd_reloc->howto
-    = lookup_howto ((unsigned) ELF32_R_TYPE (elf_reloc->r_info));
+    = lookup_howto (abfd, (unsigned) ELF32_R_TYPE (elf_reloc->r_info));
   return bfd_reloc->howto != NULL;
 }
 
@@ -925,7 +932,7 @@ elf32_i860_relocate_splitn (bfd *input_b
 {
   bfd_vma insn;
   reloc_howto_type *howto;
-  howto = lookup_howto ((unsigned) ELF32_R_TYPE (rello->r_info));
+  howto = lookup_howto (input_bfd, (unsigned) ELF32_R_TYPE (rello->r_info));
   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
 
   /* Relocate.  */
@@ -951,7 +958,7 @@ elf32_i860_relocate_pc16 (bfd *input_bfd
 {
   bfd_vma insn;
   reloc_howto_type *howto;
-  howto = lookup_howto ((unsigned) ELF32_R_TYPE (rello->r_info));
+  howto = lookup_howto (input_bfd, (unsigned) ELF32_R_TYPE (rello->r_info));
   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
 
   /* Adjust for PC-relative relocation.  */
@@ -983,7 +990,7 @@ elf32_i860_relocate_pc26 (bfd *input_bfd
 {
   bfd_vma insn;
   reloc_howto_type *howto;
-  howto = lookup_howto ((unsigned) ELF32_R_TYPE (rello->r_info));
+  howto = lookup_howto (input_bfd, (unsigned) ELF32_R_TYPE (rello->r_info));
   insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
 
   /* Adjust for PC-relative relocation.  */
@@ -1104,7 +1111,7 @@ elf32_i860_relocate_section (bfd *output
       r_type = ELF32_R_TYPE (rel->r_info);
       r_symndx = ELF32_R_SYM (rel->r_info);
 
-      howto = lookup_howto ((unsigned) ELF32_R_TYPE (rel->r_info));
+      howto = lookup_howto (input_bfd, (unsigned) ELF32_R_TYPE (rel->r_info));
       h     = NULL;
       sym   = NULL;
       sec   = NULL;



More information about the Binutils mailing list