Bug 4634 - Bigendian binary is output even if it is configured to output little endian
Summary: Bigendian binary is output even if it is configured to output little endian
Status: RESOLVED WONTFIX
Alias: None
Product: buildroot
Classification: Unclassified
Component: Other (show other bugs)
Version: 2011.11
Hardware: PC Linux
: P5 major
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-12-18 05:28 UTC by matusita
Modified: 2012-02-17 09:42 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments
Patch to properly create symlinks when ARCH_SUBDIR is not a single directory. (1.64 KB, patch)
2012-02-17 09:37 UTC, Aristotelis Iordanidis
Details

Note You need to log in before you can comment on or make changes to this bug.
Description matusita 2011-12-18 05:28:37 UTC
I set build options as below in "make menuconfig" to build root filesystem:
  Target Architecture (mipsel)
  Target Architecture Variant (mips 32r2)
  Toolcain-Toolchain type (External toolchain)
  Toolcain-Toolchain (CodeSourcery MIPS 2011.03)

I expected that little enfian binary will be output because "mipsel" is selected. But it was big endian.
I confirmed it by objdump with "-f" option like below.

  % mips-linux-gnu-objdump -f busybox
    busybox:     file format elf32-tradbigmips      <-- Big endian !!!
    architecture: mips:isa32r2, flags 0x00000112:
    EXEC_P, HAS_SYMS, D_PAGED
    start address 0x00403f00

So, I saw following file:
  toolchain/toolchain-external/ext-tool.mk

And I understand that this script get libraries' location by calling (prefix)-gcc with following options:
  -print-sysroot
  -print-file-name=libc.a

But as for this toolchain(CodeSourcery MIPS 2011.03), it is need to set "-EL" option as well to get correct location of little endian libraries.
Comment 1 Thomas Petazzoni 2012-01-31 19:46:56 UTC
Buildroot does not automatically add the needed CFLAGS and LDFLAGS to use the adequate multilib variant, because those CFLAGS and LDFLAGS are very toolchain-specific and architecture-specific. So it is the user's job to add explictly those flags.

I have posted a patch, http://lists.busybox.net/pipermail/buildroot/2012-January/049556.html, which adds some help text on the different external toolchains about their respective multilib variants and how to select between them. Hopefully, this patch should be merged for 2012.02.
Comment 2 Aristotelis Iordanidis 2012-02-17 09:37:52 UTC
Created attachment 4040 [details]
Patch to properly create symlinks when ARCH_SUBDIR is not a single directory.
Comment 3 Aristotelis Iordanidis 2012-02-17 09:42:09 UTC
(In reply to comment #1)
> Buildroot does not automatically add the needed CFLAGS and LDFLAGS to use the
> adequate multilib variant, because those CFLAGS and LDFLAGS are very
> toolchain-specific and architecture-specific. So it is the user's job to add
> explictly those flags.
> 
> I have posted a patch,
> http://lists.busybox.net/pipermail/buildroot/2012-January/049556.html, which
> adds some help text on the different external toolchains about their respective
> multilib variants and how to select between them. Hopefully, this patch should
> be merged for 2012.02.

(I'm sorry I posted a patch before the explanation)

I don't think this is enough (it didn't work for me).
The TOOLCHAIN_EXTERNAL_CFLAGS in toolchain/toolchain-external/ext-tool.mk does not take into account BR2_TARGET_OPTIMIZATION.
So, in the end it always copies the big-endian libc/ from multilib.

From toolchain/toolchain-external/ext-tool.mk:

# march/mtune/floating point mode needs to be passed to the external toolchain
# to select the right multilib variant
ifneq ($(CC_TARGET_TUNE_),)
TOOLCHAIN_EXTERNAL_CFLAGS += -mtune=$(CC_TARGET_TUNE_)
TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += -DBR_TUNE='"$(CC_TARGET_TUNE_)"'
endif
ifneq ($(CC_TARGET_ARCH_),)
TOOLCHAIN_EXTERNAL_CFLAGS += -march=$(CC_TARGET_ARCH_)
TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += -DBR_ARCH='"$(CC_TARGET_ARCH_)"'
endif
ifneq ($(CC_TARGET_CPU_),)
TOOLCHAIN_EXTERNAL_CFLAGS += -mcpu=$(CC_TARGET_CPU_)
TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += -DBR_CPU='"$(CC_TARGET_CPU_)"'
endif
ifneq ($(CC_TARGET_ABI_),)
TOOLCHAIN_EXTERNAL_CFLAGS += -mabi=$(CC_TARGET_ABI_)
TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += -DBR_ABI='"$(CC_TARGET_ABI_)"'
endif

ifeq ($(BR2_SOFT_FLOAT),y)
TOOLCHAIN_EXTERNAL_CFLAGS += -msoft-float
TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += -DBR_SOFTFLOAT=1
endif

ifeq ($(BR2_VFP_FLOAT),y)
TOOLCHAIN_EXTERNAL_CFLAGS += -mfpu=vfp
TOOLCHAIN_EXTERNAL_WRAPPER_ARGS += -DBR_VFPFLOAT=1
endif

...

        $(Q)SYSROOT_DIR=`$(TOOLCHAIN_EXTERNAL_CC) -print-sysroot 2>/dev/null` ; \

...

        ARCH_SUBDIR=`$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS) -print-multi-directory` ; \
        ARCH_SYSROOT_DIR=$${SYSROOT_DIR}/$${ARCH_SUBDIR} ; \

...

        echo "Copy external toolchain sysroot to staging..." ; \
        $(call copy_toolchain_sysroot,$${SYSROOT_DIR},$${ARCH_SYSROOT_DIR},$${ARCH_SUBDIR}) ; \


I have selected the following in menuconfig (I've also selected soft-float):

BR2_TARGET_OPTIMIZATION="-pipe -EL -march=m14kc -mmcu"
BR2_TARGET_LDFLAGS="-EL"


I attempted to fix the problem by passing $(TARGET_LDFLAGS) in ARCH_SUBDIR:

        ARCH_SUBDIR=`$(TOOLCHAIN_EXTERNAL_CC) $(TOOLCHAIN_EXTERNAL_CFLAGS) $(TARGET_LDFLAGS) -print-multi-directory` ; \

After this change it seems that the little-endian version of libc is copied in buildroot's sysroot, but I still get errors about crt1.o and crti.o (ld can't find them).

This happens because in my case ARCH_SUBDIR=soft-float/el .
copy_toolchain_sysroot doesn't handle the case where ARCH_SUBDIR is not a single directory:

ln: failed to create symbolic link `/home/iordanidis/src/buildroot/buildroot/output/host/usr/mipsel-unknown-linux-gnu/sysroot/soft-float/el': No such file or directory

I corrected this by slightly modifying copy_toolchain_sysroot. After that, busybox was built correctly with -EL -soft-float.