This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Patch: Check input format against output
- To: Marco dot Franzen at Thyron dot com
- Subject: Patch: Check input format against output
- From: "H . J . Lu" <hjl at lucon dot org>
- Date: Fri, 23 Mar 2001 20:40:44 -0800
- Cc: binutils at sources dot redhat dot com
- References: <200103231040.KAA10476@maidavale.thyron-local> <01032318504903.11087@maidavale>
On Fri, Mar 23, 2001 at 06:24:34PM +0000, Marco Franzen wrote:
> On Fri, 23 Mar 2001, I wrote:
> > (gdb) run -m elf_i386 -o dec.e crt1.o crti.o crtbegin.o dec.o libtask.a libgcc.a libc.so libgcc.a crtend.o crtn.o
> > Starting program: /usr/bin/ld -m elf_i386 -o dec.e crt1.o crti.o crtbegin.o dec.o libtask.a libgcc.a libc.so libgcc.a crtend.o crtn.o
> >
> > Program received signal SIGSEGV, Segmentation fault.
>
> That was because one file, libtask.a, was for the wrong architecture,
> Solaris/Sparc (due to an incomplete make clean rule...).
>
> The native linker on SCO Unixware 7, different from GNU ld on GNU/Linux,
> can detect this and issues an error message:
> UX:ld: ERROR: libtask.a(tri_des.o): fatal error: libtask.a(tri_des.o): wrong machine type
>
> Could GNU ld (or libbfd) do the same?
Here is a patch. Any comments?
Thanks.
H.J.
----
2001-03-23 H.J. Lu <hjl@gnu.org>
* elflink.h (elf_link_input_bfd): Return false if the input
format doesn't matches output.
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:39:18
@@ -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 != EM_NONE
+ && i_ehdrp->e_machine != EM_NONE
+ && bed->elf_machine_code != i_ehdrp->e_machine
+ && (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