This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: PATCH: Fix mixing MIPS object files.
- From: Richard Sandiford <rsandifo at redhat dot com>
- To: "H. J. Lu" <hjl at lucon dot org>
- Cc: "Maciej W. Rozycki" <macro at ds2 dot pg dot gda dot pl>,Daniel Jacobowitz <drow at mvista dot com>,Thiemo Seufer <ica2_ts at csv dot ica dot uni-stuttgart dot de>,binutils at sources dot redhat dot com
- Date: 03 Dec 2002 16:33:37 +0000
- Subject: Re: PATCH: Fix mixing MIPS object files.
- References: <20021106122519.A19806@lucon.org><Pine.GSO.3.96.1021107143552.5894C-100000@delta.ds2.pg.gda.pl><20021107101902.A28873@lucon.org>
<hangs head> Sorry for dropping the ball...
"H. J. Lu" <hjl@lucon.org> writes:
> Now the question is
>
> # mipsel-linux-as -mips[3|4|5|64]
> # mipsel-linux-as -march=xxx
> # mipsel-linux-gcc -mips[3|4|5|64]
> # mipsel-linux-gcc -march=xxx
>
> will ever use 64bit registers. I hope they won't. If it is the case,
> I have no problems to ignore EF_MIPS_ARCH when EF_MIPS_MACH is not
> zero.
Here's the sort of patch I had in mind. Backtracking
from what I said before, it seems like it's easy to keep
mips2/sb1-style combinations after all.
The patch shows no regressions on mips64-elf and makes it
possible to link 32-bit MIPS IV code with MIPS II code.
It also behaves the way I expected when running some tests
by hand.
It's just a request for comments at this stage. I'll whip
up something for the testsuite before seeking approval.
Richard
* elfxx-mips.c: Include libiberty.h.
(elf_mips_isa, _bfd_mips_elf_mach_extends_p): Remove.
(mips_set_isa_flags): New function, split out from...
(_bfd_mips_elf_final_write_processing): ...here. Only call
mips_set_isa_flags if the EF_MIPS_MACH bits are clear.
(mips_mach_extensions): New array.
(mips_32bit_flags_p): New function.
(_bfd_mips_elf_merge_private_bfd_data): Rework architecture checks.
Use mips_32bit_flags_p to check if one binary is 32-bit and the
other is 64-bit. When adopting IBFD's architecture, adopt the
bfd_mach as well as the flags.
Index: bfd/elfxx-mips.c
===================================================================
RCS file: /cvs/src/src/bfd/elfxx-mips.c,v
retrieving revision 1.33
diff -c -d -p -F^[(a-zA-Z0-9_^#] -r1.33 elfxx-mips.c
*** bfd/elfxx-mips.c 30 Nov 2002 08:39:39 -0000 1.33
--- bfd/elfxx-mips.c 3 Dec 2002 16:29:09 -0000
*************** Foundation, Inc., 59 Temple Place - Suit
*** 30,35 ****
--- 30,36 ----
#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"
+ #include "libiberty.h"
#include "elf-bfd.h"
#include "elfxx-mips.h"
#include "elf/mips.h"
*************** static bfd_boolean mips_elf_create_dynam
*** 360,370 ****
PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
struct mips_elf_link_hash_entry *, asection *,
bfd_vma, bfd_vma *, asection *));
! static INLINE int elf_mips_isa PARAMS ((flagword));
static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
static void mips_elf_irix6_finish_dynamic_symbol
PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
! static bfd_boolean _bfd_mips_elf_mach_extends_p PARAMS ((flagword, flagword));
/* This will be used when we sort the dynamic relocation records. */
static bfd *reldyn_sorting_bfd;
--- 361,372 ----
PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Rela *,
struct mips_elf_link_hash_entry *, asection *,
bfd_vma, bfd_vma *, asection *));
! static void mips_set_isa_flags PARAMS ((bfd *));
static INLINE char* elf_mips_abi_name PARAMS ((bfd *));
static void mips_elf_irix6_finish_dynamic_symbol
PARAMS ((bfd *, const char *, Elf_Internal_Sym *));
! static bfd_boolean mips_mach_extends_p PARAMS ((int, int));
! static bfd_boolean mips_32bit_flags_p PARAMS ((flagword));
/* This will be used when we sort the dynamic relocation records. */
static bfd *reldyn_sorting_bfd;
*************** #endif
*** 3050,3081 ****
return TRUE;
}
- /* Return the ISA for a MIPS e_flags value. */
-
- static INLINE int
- elf_mips_isa (flags)
- flagword flags;
- {
- switch (flags & EF_MIPS_ARCH)
- {
- case E_MIPS_ARCH_1:
- return 1;
- case E_MIPS_ARCH_2:
- return 2;
- case E_MIPS_ARCH_3:
- return 3;
- case E_MIPS_ARCH_4:
- return 4;
- case E_MIPS_ARCH_5:
- return 5;
- case E_MIPS_ARCH_32:
- return 32;
- case E_MIPS_ARCH_64:
- return 64;
- }
- return 4;
- }
-
/* Return the MACH for a MIPS e_flags value. */
unsigned long
--- 3052,3057 ----
*************** _bfd_mips_elf_finish_dynamic_sections (o
*** 5921,5940 ****
return TRUE;
}
- /* The final processing done just before writing out a MIPS ELF object
- file. This gets the MIPS architecture right based on the machine
- number. This is used by both the 32-bit and the 64-bit ABI. */
! void
! _bfd_mips_elf_final_write_processing (abfd, linker)
bfd *abfd;
- bfd_boolean linker ATTRIBUTE_UNUSED;
{
! unsigned long val;
! unsigned int i;
! Elf_Internal_Shdr **hdrpp;
! const char *name;
! asection *sec;
switch (bfd_get_mach (abfd))
{
--- 5897,5910 ----
return TRUE;
}
! /* Set ABFD's EF_MIPS_ARCH and EF_MIPS_MACH flags. */
!
! static void
! mips_set_isa_flags (abfd)
bfd *abfd;
{
! flagword val;
switch (bfd_get_mach (abfd))
{
*************** _bfd_mips_elf_final_write_processing (ab
*** 6007,6016 ****
case bfd_mach_mipsisa64:
val = E_MIPS_ARCH_64;
}
-
elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
elf_elfheader (abfd)->e_flags |= val;
/* Set the sh_info field for .gptab sections and other appropriate
info for each special section. */
--- 5977,6008 ----
case bfd_mach_mipsisa64:
val = E_MIPS_ARCH_64;
+ break;
}
elf_elfheader (abfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
elf_elfheader (abfd)->e_flags |= val;
+ }
+
+
+ /* The final processing done just before writing out a MIPS ELF object
+ file. This gets the MIPS architecture right based on the machine
+ number. This is used by both the 32-bit and the 64-bit ABI. */
+
+ void
+ _bfd_mips_elf_final_write_processing (abfd, linker)
+ bfd *abfd;
+ bfd_boolean linker ATTRIBUTE_UNUSED;
+ {
+ unsigned int i;
+ Elf_Internal_Shdr **hdrpp;
+ const char *name;
+ asection *sec;
+
+ /* Keep the existing EF_MIPS_MACH and EF_MIPS_ARCH flags if the former
+ is nonzero. This is for compatibility with old objects, which used
+ a combination of a 32-bit EF_MIPS_ARCH and a 64-bit EF_MIPS_MACH. */
+ if ((elf_elfheader (abfd)->e_flags & EF_MIPS_MACH) == 0)
+ mips_set_isa_flags (abfd);
/* Set the sh_info field for .gptab sections and other appropriate
info for each special section. */
*************** #endif
*** 7680,7705 ****
return TRUE;
}
! /* Return TRUE if machine EXTENSION is an extension of machine BASE,
! meaning that it should be safe to link code for the two machines
! and set the output machine to EXTENSION. EXTENSION and BASE are
! both submasks of EF_MIPS_MACH. */
static bfd_boolean
! _bfd_mips_elf_mach_extends_p (base, extension)
! flagword base, extension;
{
! /* The vr5500 ISA is an extension of the core vr5400 ISA, but doesn't
! include the multimedia stuff. It seems better to allow vr5400
! and vr5500 code to be merged anyway, since many libraries will
! just use the core ISA. Perhaps we could add some sort of ASE
! flag if this ever proves a problem. */
! return (base == 0
! || (base == E_MIPS_MACH_5400 && extension == E_MIPS_MACH_5500)
! || (base == E_MIPS_MACH_4100 && extension == E_MIPS_MACH_4111)
! || (base == E_MIPS_MACH_4100 && extension == E_MIPS_MACH_4120));
}
/* Merge backend specific data from an object file to the output
object file when linking. */
--- 7672,7758 ----
return TRUE;
}
! /* An array describing how bfd_machs relate to one another. It contains
! (X, Y) if X is an extension of Y. The entries are ordered topologically,
! with MIPS I coming last. */
!
! static const int mips_mach_extensions[][2] = {
! /* MIPS64 extensions. */
! { bfd_mach_mips_sb1, bfd_mach_mipsisa64 },
!
! /* MIPS V extensions. */
! { bfd_mach_mipsisa64, bfd_mach_mips5 },
!
! /* R10000 extensions. */
! { bfd_mach_mips12000, bfd_mach_mips10000 },
!
! /* R5000 extensions. Note: the vr5500 ISA is an extension of the core
! vr5400 ISA, but doesn't include the multimedia stuff. It seems
! better to allow vr5400 and vr5500 code to be merged anyway, since
! many libraries will just use the core ISA. Perhaps we could add
! some sort of ASE flag if this ever proves a problem. */
! { bfd_mach_mips5500, bfd_mach_mips5400 },
! { bfd_mach_mips5400, bfd_mach_mips5000 },
!
! /* MIPS IV extensions. */
! { bfd_mach_mips5, bfd_mach_mips8000 },
! { bfd_mach_mips10000, bfd_mach_mips8000 },
! { bfd_mach_mips5000, bfd_mach_mips8000 },
!
! /* VR4100 extensions. */
! { bfd_mach_mips4120, bfd_mach_mips4100 },
! { bfd_mach_mips4111, bfd_mach_mips4100 },
!
! /* MIPS III extensions. */
! { bfd_mach_mips8000, bfd_mach_mips4000 },
! { bfd_mach_mips4650, bfd_mach_mips4000 },
! { bfd_mach_mips4600, bfd_mach_mips4000 },
! { bfd_mach_mips4400, bfd_mach_mips4000 },
! { bfd_mach_mips4300, bfd_mach_mips4000 },
! { bfd_mach_mips4100, bfd_mach_mips4000 },
! { bfd_mach_mips4010, bfd_mach_mips4000 },
!
! /* MIPS II extensions. */
! { bfd_mach_mips4000, bfd_mach_mips6000 },
! { bfd_mach_mipsisa32, bfd_mach_mips6000 },
!
! /* MIPS I extensions. */
! { bfd_mach_mips6000, bfd_mach_mips3000 },
! { bfd_mach_mips3900, bfd_mach_mips3000 }
! };
!
!
! /* Return true if bfd_mach EXTENSION is an extennsion of bfd_mach BASE. */
static bfd_boolean
! mips_mach_extends_p (base, extension)
! int base, extension;
{
! size_t i;
!
! for (i = 0; extension != base && i < ARRAY_SIZE (mips_mach_extensions); i++)
! if (extension == mips_mach_extensions[i][0])
! extension = mips_mach_extensions[i][1];
!
! return extension == base;
! }
!
!
! /* Return true if the given ELF header flags describe a 32-bit binary. */
!
! static bfd_boolean
! mips_32bit_flags_p (flags)
! flagword flags;
! {
! return ((flags & EF_MIPS_32BITMODE) != 0
! || (flags & EF_MIPS_ABI) == E_MIPS_ABI_O32
! || (flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI32
! || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1
! || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2
! || (flags & EF_MIPS_ARCH) == E_MIPS_ARCH_32);
}
+
/* Merge backend specific data from an object file to the output
object file when linking. */
*************** _bfd_mips_elf_merge_private_bfd_data (ib
*** 7795,7858 ****
ok = FALSE;
}
! /* Compare the ISA's. */
! if ((new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH))
! != (old_flags & (EF_MIPS_ARCH | EF_MIPS_MACH)))
{
! int new_mach = new_flags & EF_MIPS_MACH;
! int old_mach = old_flags & EF_MIPS_MACH;
! int new_isa = elf_mips_isa (new_flags);
! int old_isa = elf_mips_isa (old_flags);
!
! /* If either has no machine specified, just compare the general isa's.
! Some combinations of machines are ok, if the isa's match. */
! if (new_mach == old_mach
! || _bfd_mips_elf_mach_extends_p (new_mach, old_mach)
! || _bfd_mips_elf_mach_extends_p (old_mach, new_mach))
{
! /* Don't warn about mixing code using 32-bit ISAs, or mixing code
! using 64-bit ISAs. They will normally use the same data sizes
! and calling conventions. */
!
! if (( (new_isa == 1 || new_isa == 2 || new_isa == 32)
! ^ (old_isa == 1 || old_isa == 2 || old_isa == 32)) != 0)
! {
! (*_bfd_error_handler)
! (_("%s: ISA mismatch (-mips%d) with previous modules (-mips%d)"),
! bfd_archive_filename (ibfd), new_isa, old_isa);
! ok = FALSE;
! }
! else
! {
! /* Do we need to update the mach field? */
! if (_bfd_mips_elf_mach_extends_p (old_mach, new_mach))
! {
! elf_elfheader (obfd)->e_flags &= ~EF_MIPS_MACH;
! elf_elfheader (obfd)->e_flags |= new_mach;
! }
! /* Do we need to update the ISA field? */
! if (new_isa > old_isa)
! {
! elf_elfheader (obfd)->e_flags &= ~EF_MIPS_ARCH;
! elf_elfheader (obfd)->e_flags
! |= new_flags & EF_MIPS_ARCH;
! }
! }
}
else
{
(*_bfd_error_handler)
! (_("%s: ISA mismatch (%d) with previous modules (%d)"),
bfd_archive_filename (ibfd),
! _bfd_elf_mips_mach (new_flags),
! _bfd_elf_mips_mach (old_flags));
ok = FALSE;
}
-
- new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
- old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
}
/* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
does set EI_CLASS differently from any 32-bit ABI. */
--- 7848,7893 ----
ok = FALSE;
}
! if (mips_32bit_flags_p (old_flags) != mips_32bit_flags_p (new_flags))
{
! (*_bfd_error_handler)
! (_("%s: linking 32-bit code with 64-bit code"),
! bfd_archive_filename (ibfd));
! ok = FALSE;
! }
! else if (!mips_mach_extends_p (bfd_get_mach (ibfd), bfd_get_mach (obfd)))
! {
! if (mips_mach_extends_p (bfd_get_mach (obfd), bfd_get_mach (ibfd)))
{
! /* Copy the architecture info from IBFD to OBFD. Also copy
! the 32-bit flag (if set) so that we continue to recognise
! OBFD as a 32-bit binary. */
! bfd_set_arch_info (obfd, bfd_get_arch_info (ibfd));
! elf_elfheader (obfd)->e_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH);
! elf_elfheader (obfd)->e_flags
! |= new_flags & (EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
! /* Copy across the ABI flags if OBFD doesn't use them
! and if that was what caused us to treat IBFD as 32-bit. */
! if ((old_flags & EF_MIPS_ABI) == 0
! && mips_32bit_flags_p (new_flags)
! && !mips_32bit_flags_p (new_flags & ~EF_MIPS_ABI))
! elf_elfheader (obfd)->e_flags |= new_flags & EF_MIPS_ABI;
}
else
{
+ /* The ISAs aren't compatible. */
(*_bfd_error_handler)
! (_("%s: this module's ISA (%s) conflicts with previous ones (%s)"),
bfd_archive_filename (ibfd),
! bfd_printable_name (ibfd),
! bfd_printable_name (obfd));
ok = FALSE;
}
}
+
+ new_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
+ old_flags &= ~(EF_MIPS_ARCH | EF_MIPS_MACH | EF_MIPS_32BITMODE);
/* Compare ABI's. The 64-bit ABI does not use EF_MIPS_ABI. But, it
does set EI_CLASS differently from any 32-bit ABI. */