由于无效的 ELF header,MIPS 仿真器无法 运行 mips 可执行文件

MIPS emulator cannot run mips executable due to invalid ELF header

所以我想开始编写一些 mips 程序集,并设置我的工具。 我正在使用 Arch Linux,并从用户存储库安装了 cross-mipsel-linux-gnu-gcc 和 binutils,它们运行良好并成功地将我的简单测试程序编译成 mipsel 二进制文件。

我不想要任何花哨的 gui 或类似的东西用于我的仿真,而是选择了 qemu。这是我遇到的问题。当我尝试 运行 我的可执行文件 qemu-mipsel ./test 时,qemu 只是回复 ./test: Invalid ELF image for this architecture.

我首先想到的是 "Can qemu really just run base mips binaries like that? That seems almost like magic, this can't be right.",所以我开始谷歌搜索是否真的可行 found a tutorial on how to to similar compilation for a openwrt but with C code<,在看了那个教程之后,我的步骤似乎也应该有效。

这是我编译程序集的命令,简洁明了 mipsel-linux-gnu-as test.asm -o test; chmod +x ./test

这是 file 命令的输出 ./test: ELF 32-bit LSB relocatable, MIPS, MIPS-I version 1 (SYSV), not stripped

这是readelf -a ./test

的输出
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:                              REL (Relocatable file)
  Machine:                           MIPS R3000
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          164 (bytes into file)
  Flags:                             0x1000, o32, mips1
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         9
  Section header string table index: 6

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000000 000040 000010 00  AX  0   0 16
  [ 2] .data             PROGBITS        00000000 000050 000000 00  WA  0   0 16
  [ 3] .bss              NOBITS          00000000 000050 000000 00  WA  0   0 16
  [ 4] .reginfo          MIPS_REGINFO    00000000 000050 000018 18   A  0   0  4
  [ 5] .pdr              PROGBITS        00000000 000068 000000 00      0   0  4
  [ 6] .shstrtab         STRTAB          00000000 000068 00003a 00      0   0  1
  [ 7] .symtab           SYMTAB          00000000 00020c 000060 10      8   6  4
  [ 8] .strtab           STRTAB          00000000 00026c 000001 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

There are no section groups in this file.

There are no program headers in this file.

There are no relocations in this file.

The decoding of unwind sections for machine type MIPS R3000 is not currently supported.

Symbol table '.symtab' contains 6 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 SECTION LOCAL  DEFAULT    1 
     2: 00000000     0 SECTION LOCAL  DEFAULT    2 
     3: 00000000     0 SECTION LOCAL  DEFAULT    3 
     4: 00000000     0 SECTION LOCAL  DEFAULT    4 
     5: 00000000     0 SECTION LOCAL  DEFAULT    5 

No version information found in this file.

您忘记了 link 您的代码。汇编程序 as 只生成目标文件,您需要 link 它。大概你想要这样的东西:

mipsel-linux-gnu-as test.asm -o test.o
mipsel-linux-gnu-ld test.o -o test

如果操作正确,file命令输出应该包括executable,如:

ELF 32-bit LSB executable, MIPS, MIPS-I version 1 (SYSV), statically linked, not stripped