【问题标题】:Writing and Compiling Program For OpenWrtOpenWrt的编写和编译程序
【发布时间】:2019-09-16 14:33:57
【问题描述】:

我有一个在 OpenWRT 下运行 MIPS 架构的嵌入式设备

系统类型:MediaTek MT7628AN ver:1 eco:2
机器:WRTnode2P 处理器:0
CPU型号:MIPS 24KEc V5.5

我想通过我的电脑(ubuntu)用C编译一个小程序

#include <stdio.h>

int main(void){
    printf("HelloWorld");
    return 0;
}

要编译它,我使用 mips-linux-gnu-gcc 命令

mips-linux-gnu-gcc -march=24kec -mabi=32 hello.c -o hello

我将 hello 程序发送到我的设备并制作 chmod 755

chmod 755 hello

当我尝试执行它时出现错误

root@openWrt:/www# ./hello
./hello: line 1: syntax error: unexpected word (expecting ")")

我不明白发生了什么,我尝试了其他一些命令来编译它,比如带参数:-EB 或 -EL 是否,-static 或不是,-mabi=32 和不是,但我有同样的问题。

有人帮我吗?

谢谢

[更新]

我向现有文件发送文件命令,这是结果

fw3: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1, dynamically linked, interpreter /lib/ld., for GNU/Linux 3.2.0, stripped

所以我用这个命令编译我的程序

mipsel-linux-gnu-gcc -march=24kec -mips32r2  -mips16   hello.c -o hello

现在我有了这个文件命令结果

hello: ELF 32-bit LSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), dynamically linked, interpreter /lib/ld., for GNU/Linux 3.2.0, BuildID[sha1]=cd12319441c530606d52d96478719b06a7b215a7, not stripped

现在我读取了我的远程程序的 ELF

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 01 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       1
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x402c40
  Start of program headers:          52 (bytes into file)
  Start of section headers:          78592 (bytes into file)
  Flags:                             0x74001005, noreorder, cpic, o32, mips16, mips32r2
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         10
  Size of section headers:           40 (bytes)
  Number of section headers:         31
  Section header string table index: 30

这是我的 hello 程序 readelf 命令

ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              EXEC (Executable file)
  Machine:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x4005c0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          6700 (bytes into file)
  Flags:                             0x74001007, noreorder, pic, cpic, o32, mips16, mips32r2
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         11
  Size of section headers:           40 (bytes)
  Number of section headers:         33
  Section header string table index: 30

有两个不同的 ABI 版本,在标志中有图片

如果我尝试启动我的 hello 程序,我会遇到此错误

hello: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

我尝试添加 -static 参数,但在启动我的 hello 程序时出现此错误

Illegal instruction

我总是被这个问题困扰。

【问题讨论】:

  • 你想在 mips 架构上运行你的程序吗?
  • 是的,我的设备在 mips 架构下运行
  • 你能显示file ./helloldd ./hello的结果吗?
  • 你的问题解决了吗?

标签: linux embedded cross-compiling openwrt


【解决方案1】:

尝试使用 mipsle 构建

GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build

我有一个类似的问题,并通过将架构师设置为 mipsle 来解决它。我正在使用 Go 在此设备上为 linux/mips 构建二进制文件,

system type     : MediaTek MT7620A ver:2 eco:6
cpu model       : MIPS 24KEc V5.0
isa             : mips1 mips2 mips32r1 mips32r2
ASEs implemented    : mips16 dsp

第一次失败,

macbook:# cat main.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println("hello, mips")
}

#### Setting GOARCH=mips

macbook:# GOOS=linux GOARCH=mips GOMIPS=softfloat go build -o hello-mips

openwrt:# ./hello-mips
./hello-mips: line 1: syntax error: unexpected "("

然后我意识到 CPU 架构是 Little-Endian,而不是默认的 Big-Endian。所以我改变了构建参数,现在它成功了。

#### Setting GOARCH=mipsle

macbook:# GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -o hello
openwrt:# ./hello
hello, mips

你的CPU型号和我的一样,只是修订版MIPS 24KEc V5.5不同,也必须是mipsle(mipsel)。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2015-01-04
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多