mips gcc のインタープリターに対してコンパイルするにはどうすればいいですか?

mips gcc のインタープリターに対してコンパイルするにはどうすればいいですか?

以前ここにあった古い質問は、太字で記載した理由により、もはや関連性がなくなったため、削除しました。質問自体はまだ残っていますが、クロスコンパイルしようとしていたシステムはもう存在しません。古い質問は、ペーストビン

F@ST 2704Nのファームウェアをフラッシュできることがわかりました。翻訳元. luci Web インターフェイスのリンクを使用して、ルーターに OpenWRT をインストールしました。ルーターのプログラムをクロスコンパイルする方法を知りたいので、この質問はまだ有効ですが、組み込みファームウェアに対して作業しようとしていないので、より簡単になるはずです。

OpenWRT を実行している Mips32 バージョン 1 プロセッサで実行できるようにプログラムをコンパイルするにはどうすればよいでしょうか?

インタープリターを直接使用する:

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情報:

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 バージョン:

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

オンボードバイナリ:

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

私のバイナリ:

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 バイナリ ソース コード:

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

hello バイナリをコンパイルするために使用したコマンドは です/opt/cross/gcc-mips/bin/mips-netbsd-elf-gcc -mips32 -Tidt.ld -static hello.c -o hello

コマンドを使用してプログラムをコンパイルしようとしました/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が、多くのファイルが生成され、新しいリンカー オプションを追加しなかった場合と同じバイナリが生成されます。ソースからバイナリをリンクする必要があると思いますが、間違っている可能性があります。

答え1

私は見つけたOpenWRT.orgのこのガイドこれにより、ビルドツールのインストール方法を学ぶことができ、OpenWRT のバージョンに合わせて手順を変更しました。

git commitをチェックアウトしました70255e3d624cd393612069aae0a859d1acbbeeae (タグ: v18.06.1)そして設定を以下のようにしました:

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

また、PATH 環境変数を に設定しpath/to/project/source/staging_dir/toolchain-mips_mips32_gcc-7.3.0_musl/bin、コマンドを実行しましたmips-openwrt-linux-gcc hello.c -o hello

OSX 上でコンパイルしているため、gnu-getopt と gnu-time をインストールする必要があったことを述べておきます。

また、追加のボーナスとして、BusyBox などの既存のプログラムに対してコンパイルをテストしたところ、コマンドを使用して BusyBox をコンパイルできましたLDFLAGS="--static" make CROSS_COMPILE="mips-openwrt-linux-"

編集:

ELFファイルにインタープリタがある理由に興味がある人のために、インタープリタはプログラムをロードする前に環境を設定するために必要です。引用はブログの投稿から引用したものです。クリスチャン・アイヒンガーの考え「カーネルへのプログラムのロード」セクションより。

「静的にリンクされたバイナリはインタープリターなしでも実行できますが、動的にリンクされたプログラムには、起動コードが含まれ、バイナリに必要な共有ライブラリがロードされ、再配置が実行されるため、インタープリターとして常に /lib/ld-linux.so が必要です。」

私のプログラムを静的にリンクしたにもかかわらず、プログラムが (元の Sagemcom ファームウェアで) 「Kill​​ed」と表示され続けた理由は、ルーターが、プログラムを実行しようとしているユーザーを混乱させて不正な実行を防ぐように設計されていたためではないかと考えています。私は uClibc をコンパイルする方法を見つけ出し、元のファームウェアと別の Sagemcom デバイス (この質問で尋ねられたデバイスとは無関係) で動作させました。

関連情報