如何針對 mips gcc 的解譯器進行編譯?

如何針對 mips gcc 的解譯器進行編譯?

我扔掉了以前在這裡的舊問題,因為由於下面以粗體列出的原因,它不再相關。問題本身仍然存在,只是我試圖交叉編譯的系統不再存在。老問題已複製到巴斯德賓

我發現我可以從以下位置刷新 F@ST 2704N 的韌體OpenWRT.org。我使用了 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

你好二進位原始碼:

#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 上的本指南這幫助我學習如何安裝 buildtools,我只是修改了說明以適合我的 OpenWRT 版本。

我檢查了 git commit70255e3d624cd393612069aae0a859d1acbbeeae(標籤: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

我應該要提到我必須安裝 gnu-getopt 和 gnu-time 因為我是在 OSX 上編譯的。

另外,作為額外的好處,當我測試針對現有程式(例如 BusyBox)進行編譯時,我能夠使用命令編譯 BusyBox LDFLAGS="--static" make CROSS_COMPILE="mips-openwrt-linux-"

編輯:

對於那些對 ELF 檔案為什麼有解釋器感興趣的人來說,解釋器需要在程式載入之前設定環境。引用來自部落格文章克里斯蒂安·艾辛格的思想來自“核心中的程式載入”部分。

「靜態連結的二進位檔案可以不需要解釋器;動態連結的程式總是需要/lib/ld-linux.so 作為解釋器,因為它包含一些啟動程式碼,載入二進位檔案所需的共用程式庫,並執行重定位。

我懷疑我的程式之所以一直顯示「已殺死」(在原始Sagemcom 韌體上),即使我靜態連結它,也是因為路由器的設計可能是為了迷惑試圖運行該程式的用戶,以防止未經授權的執行。我確實弄清楚瞭如何編譯 uClibc 並讓它在原始韌體和一個單獨的 Sagemcom 設備上工作(與本問題中詢問的設備無關)。

相關內容