MIPS GCC9 not building executable ELFs

Hi Forum,

since some days i dig into MIPS Code and try to compile it. No success in building a MIPS Qemu System, i try now to build code for MIPS
with a Cross-Chain. For this i have installed mips-packages from the ports tree.

Code:
mips-binutils-2
mips-gcc9

The tools popup as mips-unknown-freebsd13.1-*

Code:
mips-unknown-freebsd13.1-addr2line    mips-unknown-freebsd13.1-gcov-tool9
mips-unknown-freebsd13.1-ar           mips-unknown-freebsd13.1-gcov9
mips-unknown-freebsd13.1-as           mips-unknown-freebsd13.1-gprof
mips-unknown-freebsd13.1-c++9         mips-unknown-freebsd13.1-ld
mips-unknown-freebsd13.1-c++filt      mips-unknown-freebsd13.1-ld.bfd
mips-unknown-freebsd13.1-cpp9         mips-unknown-freebsd13.1-nm
mips-unknown-freebsd13.1-elfedit      mips-unknown-freebsd13.1-objcopy
mips-unknown-freebsd13.1-g++9         mips-unknown-freebsd13.1-objdump
mips-unknown-freebsd13.1-gcc-9.5.0    mips-unknown-freebsd13.1-ranlib
mips-unknown-freebsd13.1-gcc-ar9      mips-unknown-freebsd13.1-readelf
mips-unknown-freebsd13.1-gcc-nm9      mips-unknown-freebsd13.1-size
mips-unknown-freebsd13.1-gcc-ranlib9  mips-unknown-freebsd13.1-strings
mips-unknown-freebsd13.1-gcc9         mips-unknown-freebsd13.1-strip
mips-unknown-freebsd13.1-gcov-dump9

Trying to compiling a cheap C-Program
Code:
cat hw.c
#include <stdio.h>

int main(){
printf("Hello MIPS World\n");
return 0;
}

However ends up in several error messages if i try to compile:
Code:
mips-unknown-freebsd13.1-gcc9 hw.c -o hw

/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status

However, writing a simple assembler program, compiling it with AS and linking it with the MIPS LD works.
I tried to set the library path to a Toolchain under /usr/obj/mips.mips/ but without luck. I also checked against other options of LD and GCC but was not successfull as well.

How can i fix this issue and compile C code successfully?

Thanks
bulky
 
You can also create MIPS object files with the clang compiler, using the correct switches.
 
My 2c: If I may suggest you may have better experience with using MIPS on Linux. As I mentioned in the thread you were asking about the WiKi/MIPS FreeBSD will drop the support. While you may go through the pain of setting up the cross-compiling tools you'd end up with the product you can't run/debug on FreeBSD anyway.
 
Yes, this is what i tried. But it seems i do it wrong.

MIPS crt1.o i build:
Code:
/usr/obj/usr/src/mips.mips64/tmp/usr/lib/crt1.o
/usr/obj/usr/src/mips.mips64/obj-lib32/tmp/usr/lib32/crt1.o
/usr/obj/usr/src/mips.mips64/obj-lib32/lib/csu/mips/crt1_c.o
/usr/obj/usr/src/mips.mips64/obj-lib32/lib/csu/mips/crt1.o
/usr/obj/usr/src/mips.mips64/lib/csu/mips/crt1.o
/usr/obj/usr/src/mips.mips64/lib/csu/mips/crt1_c.o
/usr/obj/usr/src/mips.mips/tmp/usr/lib/crt1.o
/usr/obj/usr/src/mips.mips/lib/csu/mips/crt1_c.o
/usr/obj/usr/src/mips.mips/lib/csu/mips/crt1.o

Example output:
Code:
/usr/obj/usr/src/mips.mips/lib/csu/mips $ file crt1.o 
crt1.o: ELF 32-bit MSB relocatable, MIPS, MIPS-II version 1 (FreeBSD), for FreeBSD 13.2, FreeBSD-style, with debug_info, not stripped

Trying again to compile with -L flag:
Code:
mips-unknown-freebsd13.1-gcc9 -L /usr/obj/usr/src/mips.mips/lib/csu/mips/ hw.c
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: Relocations in generic ELF (EM: 62)
/usr/local/bin/mips-unknown-freebsd13.1-ld: /usr/lib/crt1.o: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status

I also tried different/other paths + referencing crt1.o directly with -L. Like:
Code:
mips-unknown-freebsd13.1-gcc9 -L /usr/obj/usr/src/mips.mips/tmp/usr/lib/crt1.o hw.c
Same bad result. Sorry, those linker options make me mad. What can i do?
 
Those errors are because linker is using your host's crt (most likely amd64). Your demo program is using libc (printf), you don't have that on your system for mips target.

For demonstration purposes if I take this snippet of asm code (it's from my Linux program):
Code:
.section .text
    move $a0, $zero
    li $v0, 4000+1
    syscall
    nop
I would be able to crosscompile it on FreeBSD (be it useless):
Code:
# mips-unknown-freebsd13.1-gcc9 -c lxe.S
# mips-unknown-freebsd13.1-ld -o lxe lxe.o
# mips-unknown-freebsd13.1-objdump -d lxe
ELF Header:
  Magic:   7f 45 4c 46 01 02 01 09 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, big endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - FreeBSD
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
...
You'd need to crosscompile libc library if you want to crosscompile the program you have there. That's why I said you'd have more luck with Linux on mips.

Now look how easy it is to do this for aarch64 which is supported architecture on FreeBSD:
Code:
pkg info *aarch64*
aarch64-binutils-2.40_4,1
aarch64-freebsd-sysroot-a2023.03.23  ; <-- this does include aarch64 FreeBSD libc among other things
aarch64-gcc12-12.2.0

And to compile your demo program: aarch64-unknown-freebsd13.1-gcc12 --sysroot /usr/local/freebsd-sysroot/aarch64/ -o test test.c
 
Back
Top