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 05:21:20PM -0800, Ian Lance Taylor wrote:
> "H . J . Lu" <hjl@lucon.org> writes:
> 
> > > This kind of check would normally be handled in the
> > > merge_private_bfd_data routine of the output BFD.
> > > 
> > 
> > merge_private_bfd_data is defined as bfd_true for many ELF targets.
> > If we put this check into merge_private_bfd_data, we may have to touch
> > many ELF files.
> 
> True, but that doesn't mean it's the wrong thing to do.  We have to
> keep half an eye on the future, after all.
> 
> Perhaps there should be a generic elf merge_private_bfd_data routine,
> which should be the default in elfxx-target.h, and which should be
> called by other ELF files.

I tried it. The problem is although it does give a warning, but it
still dumps core since format mismatch is a warning for lang_check.

> 
> > lang_check calls bfd_arch_get_compatible:
> > 
> > const bfd_arch_info_type *
> > bfd_arch_get_compatible (abfd, bbfd)
> >      const bfd *abfd;
> >      const bfd *bbfd;
> > {
> >   /* If either architecture is unknown, then all we can do is assume
> >      the user knows what he's doing.  */
> >   if (abfd->arch_info->arch == bfd_arch_unknown)
> >     return bbfd->arch_info;
> >   if (bbfd->arch_info->arch == bfd_arch_unknown)
> >     return abfd->arch_info;
> > 
> >   /* Otherwise architecture-specific code has to decide.  */
> >   return abfd->arch_info->compatible (abfd->arch_info, bbfd->arch_info);
> > }
> > 
> > If the arch for abfd is not configured in BFD, abfd->arch_info->arch
> > will be bfd_arch_unknown.
> 
> I guess the problem here is that the bad file got recognized as the
> generic ELF target, which is bfd_arch_unknown?
> 
> It seems to me that we can catch that case easily enough.  Define a
> link_input_bfd routine for elf32-gen.c and elf64-gen.c.  Have that
> routine roll over and die.  We're not going to be able to link those
> files, since we don't know how to process the relocs.
> 

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. If it is defined in elf32-gen.c and
elf64-gen.c, how it will be used? Shouldn't it be called by
elf_link_input_bfd?


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/24 04:43:59
@@ -5465,10 +5465,36 @@ elf_link_input_bfd (finfo, input_bfd)
   asection **ppsection;
   asection *o;
   struct elf_backend_data *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_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
+      && bed->elf_machine_code != EM_NONE
+      && i_ehdrp->e_machine != EM_NONE
+      && (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]