2

I tossed out the old question that used to be here as it is no longer relevant for the reasons listed below in bold. The question itself still stands, it's just the system I was trying to cross-compile for no longer exists. The old question has been copied to Pastebin.

I figured out that I could flash firmware for F@ST 2704N from OpenWRT.org. I used the link with the luci web interface and I now have OpenWRT on my router. This question is still valid because I would like to learn how to cross compile programs for my router, but now it should be easier since I am not trying to work against the builtin firmware.

How can I compile my programs to run on Mips32 version 1 processor that is running OpenWRT?

Use Interpreter Directly:

root@OpenWrt:~# /lib/ld-musl-mips-sf.so.1 hello
/lib/ld-musl-mips-sf.so.1: hello: Not a valid dynamic program

root@OpenWrt:~# ./hello
Segmentation fault

CPU INFO:

root@OpenWrt:~# cat /proc/cpuinfo
system type     : bcm63xx/F@ST2704N (0x6318/0xB0)
machine         : Sagem F@ST2704N
processor       : 0
cpu model       : Broadcom BMIPS3300 V3.3
BogoMIPS        : 332.54
wait instruction    : yes
microsecond timers  : yes
tlb_entries     : 32
extra interrupt vector  : yes
hardware watchpoint : no
isa         : mips1 mips2 mips32r1
ASEs implemented    :
shadow register sets    : 1
kscratch registers  : 0
package         : 0
core            : 0
VCED exceptions     : not available
VCEI exceptions     : not available

Linux Version:

root@OpenWrt:~# cat /proc/version
Linux version 4.1.4 (thepeople@viasatpilot) (gcc version 4.8.3 (OpenWrt/Linaro GCC 4.8-2014.04 r46566) ) #1 Fri Aug 7 05:54:20 CEST 2015

Onboard Binary:

Alexs-MacBook-Air:hello-world senor$ file ls
ls: ELF 32-bit MSB executable, MIPS, MIPS32 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-mips-sf.so.1, stripped

Alexs-MacBook-Air:hello-world senor$ /opt/cross/gcc-mips/bin/mips-netbsd-elf-readelf -h ls
ELF Header:
  Magic:   7f 45 4c 46 01 02 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, big endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x403990
  Start of program headers:          52 (bytes into file)
  Start of section headers:          427656 (bytes into file)
  Flags:                             0x50001005, noreorder, cpic, o32, mips32
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         7
  Size of section headers:           40 (bytes)
  Number of section headers:         29
  Section header string table index: 28

My Binary:

Alexs-MacBook-Air:hello-world senor$ file hello
hello: ELF 32-bit MSB executable, MIPS, MIPS32 version 1 (SYSV), statically linked, with debug_info, not stripped

Alexs-MacBook-Air:hello-world senor$ /opt/cross/gcc-mips/bin/mips-netbsd-elf-readelf -h hello
ELF Header:
  Magic:   7f 45 4c 46 01 02 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, big endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0xa0020004
  Start of program headers:          52 (bytes into file)
  Start of section headers:          200884 (bytes into file)
  Flags:                             0x50001001, noreorder, o32, mips32
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         2
  Size of section headers:           40 (bytes)
  Number of section headers:         29
  Section header string table index: 28

Hello Binary Source Code:

#include <stdio.h>
int main() {
  printf("Hello MIPS! \n");
  return 0;
}

The command I used to compile the hello binary is /opt/cross/gcc-mips/bin/mips-netbsd-elf-gcc -mips32 -Tidt.ld -static hello.c -o hello.

I tried compiling the program using the command /opt/cross/gcc-mips/bin/mips-netbsd-elf-gcc -mips32 -Tidt.ld -dynamic-linker=/lib/ld-musl-mips-sf.so.1 -static hello.c -o hello-inter, but it generates a lot of files and produces the same binary as if I didn't add that new linker option. I think I need to link the binary from source, but I could be wrong.

  • I've got to the bottom of your question and I'm still not sure what it is you're actually asking. – roaima Jan 22 at 21:52
  • I am trying to compile a program for the F@ST1704N router. I am currently working on a hello world program to just get things started, but I am having trouble getting the hello world program to run. – SenorContento Jan 22 at 22:17
  • 1
    Could you compile it statically? Maybe so it won't die on the spot. Reopen vote given. – peterh Jan 25 at 1:24
  • I archived some of my comments in the same Pastebin link that I archived the old question. For those that don't know, the old question was for the stock firmware on my router. I replaced the firmware, so I updated the question accordingly. – SenorContento Jan 27 at 16:10
  • 1
    I have reopened your question. Please go ahead and post your answer. – terdon Jan 28 at 9:51
0

I found this guide on OpenWRT.org which helped me learn how to install buildtools and I just modified the instructions to fit my version of OpenWRT.

I checked out git commit 70255e3d624cd393612069aae0a859d1acbbeeae (tag: v18.06.1) and I set the settings:

system "Broadcom BCM63xx"
subtarget "generic"
profile "Sagem F@ST2704N"

I also set the PATH environment variable to have path/to/project/source/staging_dir/toolchain-mips_mips32_gcc-7.3.0_musl/bin and executed the command mips-openwrt-linux-gcc hello.c -o hello.

I should mention that I had to install gnu-getopt and gnu-time because I am compiling on OSX.

Also, as an added bonus, when I tested compiling against an existing program, such as BusyBox, I was able to compile BusyBox using the command LDFLAGS="--static" make CROSS_COMPILE="mips-openwrt-linux-".

Edit:

For those that are interested in why ELF files have interpreters, the interpreter is needed to setup the environment before the program is loaded. The quote comes from a blog post at Christian Aichinger's thoughts from the section "Program loading in the kernel".

"Statically linked binaries can do without an interpreter; dynamically linked programs always need /lib/ld-linux.so as interpreter because it includes some startup code, loads shared libraries needed by the binary, and performs relocations."

I suspect the reason why my program kept saying "Killed" (on the original Sagemcom firmware) even when I linked it statically is because the router may have been designed to confuse the user that is trying to run the program to prevent unauthorized execution. I did figure out how to compile uClibc and got it working on the original firmware and a separate Sagemcom device (unrelated to the one asked about in this question).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.