This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Patch: Check input format against output


On Sat, Mar 24, 2001 at 10:23:34PM -0800, Ian Lance Taylor wrote:
> "H . J . Lu" <hjl@lucon.org> writes:
> 
> > Isn't that what my patch does? I am enclosing here again. I don't see
> > the significant difference between link_input_bfd in elf32.c/elf64.c
> > and elf32-gen.c/elf64-gen.c.
> 
> Your patch prohibits me from linking a SPARC ELF input into a i386 ELF
> output file.  I believe that is wrong.  When I do that, I should get a
> warning, but it should be permitted.  The GNU linker in general
> permits linking different types of object files together.
> 
> If we decide that the GNU linker should not permit linking different
> types of object files together, then we can easily check that in
> lang_check in ld/ldlang.c, in which case your patch is not necessary.
> 
> I think the problem in this case is that the generic ELF target can
> not be linked, because there is no information about relocations.
> 

How about this patch? You can link a SPARC ELF input into a i386
ELF output file as long as your BFD supports SPARC ELF.


H.J.
---
Index: elflink.h
===================================================================
RCS file: /work/cvs/gnu/binutils/bfd/elflink.h,v
retrieving revision 1.53
diff -u -p -r1.53 elflink.h
--- elflink.h	2001/03/09 19:37:59	1.53
+++ elflink.h	2001/03/25 06:46:49
@@ -5465,10 +5465,43 @@ elf_link_input_bfd (finfo, input_bfd)
   asection **ppsection;
   asection *o;
   struct elf_backend_data *bed;
+  struct elf_backend_data *i_bed;
+  Elf_Internal_Ehdr *i_ehdrp;
 
   output_bfd = finfo->output_bfd;
   bed = get_elf_backend_data (output_bfd);
   relocate_section = bed->elf_backend_relocate_section;
+  i_bed = get_elf_backend_data (input_bfd);
+  i_ehdrp = elf_elfheader (input_bfd);
+
+  /* Check if the input ELF e_machine field matches the output
+     ELF e_machine field.  */
+  if (bed->elf_machine_code != i_ehdrp->e_machine
+      && i_bed->elf_machine_code != i_ehdrp->e_machine
+      && bed->elf_machine_code != EM_NONE
+      && i_ehdrp->e_machine != EM_NONE
+      && (i_bed->elf_machine_alt1 == 0
+	  || i_ehdrp->e_machine != i_bed->elf_machine_alt1)
+      && (i_bed->elf_machine_alt2 == 0
+	  || i_ehdrp->e_machine != i_bed->elf_machine_alt2)
+      && (bed->elf_machine_alt1 == 0
+	  || i_ehdrp->e_machine != bed->elf_machine_alt1)
+      && (bed->elf_machine_alt2 == 0
+	  || i_ehdrp->e_machine != bed->elf_machine_alt2))
+    {
+      if (input_bfd->my_archive)
+	(*_bfd_error_handler) (_("%s(%s): File in wrong format (EM: %d)"),
+			       bfd_get_filename (input_bfd->my_archive),
+			       bfd_get_filename (input_bfd),
+			       i_ehdrp->e_machine);
+      else
+	(*_bfd_error_handler) (_("%s: File in wrong format (EM: %d)"),
+			       bfd_get_filename (input_bfd),
+			       i_ehdrp->e_machine);
+
+      bfd_set_error (bfd_error_wrong_format);
+      return false;
+    }
 
   /* If this is a dynamic object, we don't want to do anything here:
      we don't want the local symbols, and we don't want the section


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]