Executable

靜態 MIPS 二進製文件上的“非法指令”

  • March 28, 2019

在我的 MIPS 盒子上,我正在嘗試執行一個程序。我對 mips 使用交叉編譯器。當我執行我的程序時,我得到

Illegal instruction

我從中提取了一個二進製文件,名為cputest. 它基本上會延遲列印“hello world”。以下是readelf關於它的​​內容:

readelf -a ./cputest.mips 
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:               0x4004e0
 Start of program headers:          52 (bytes into file)
 Start of section headers:          1956 (bytes into file)
 Flags:                             0x1007, noreorder, pic, cpic, o32, mips1
 Size of this header:               52 (bytes)
 Size of program headers:           32 (bytes)
 Number of program headers:         8
 Size of section headers:           40 (bytes)
 Number of section headers:         20
 Section header string table index: 19

Section Headers:
 [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
 [ 0]                   NULL            00000000 000000 000000 00      0   0  0
 [ 1] .interp           PROGBITS        00400134 000134 000014 00   A  0   0  1
 [ 2] .reginfo          MIPS_REGINFO    00400148 000148 000018 18   A  0   0  4
 [ 3] .dynamic          DYNAMIC         00400160 000160 0000c8 08   A  6   0  4
 [ 4] .hash             HASH            00400228 000228 000058 04   A  5   0  4
 [ 5] .dynsym           DYNSYM          00400280 000280 000110 10   A  6   1  4
 [ 6] .dynstr           STRTAB          00400390 000390 0000d0 00   A  0   0  1
 [ 7] .init             PROGBITS        00400460 000460 000028 00  AX  0   0  4
 [ 8] .text             PROGBITS        00400490 000490 0000b0 00  AX  0   0 16
 [ 9] .MIPS.stubs       PROGBITS        00400540 000540 000040 00  AX  0   0  4
 [10] .fini             PROGBITS        00400580 000580 000028 00  AX  0   0  4
 [11] .rodata           PROGBITS        004005a8 0005a8 000010 01 AMS  0   0  4
 [12] .data             PROGBITS        004105c0 0005c0 000010 00  WA  0   0 16
 [13] .rld_map          PROGBITS        004105d0 0005d0 000004 00  WA  0   0  4
 [14] .got              PROGBITS        004105e0 0005e0 000020 04 WAp  0   0 16
 [15] .pdr              PROGBITS        00000000 000600 0000c0 00      0   0  4
 [16] .comment          PROGBITS        00000000 0006c0 000033 01  MS  0   0  1
 [17] .gnu.attributes   LOOS+ffffff5    00000000 0006f3 000010 00      0   0  1
 [18] .mdebug.abi32     PROGBITS        00000010 000703 000000 00      0   0  1
 [19] .shstrtab         STRTAB          00000000 000703 0000a1 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.

Program Headers:
 Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
 PHDR           0x000034 0x00400034 0x00400034 0x00100 0x00100 R E 0x4
 INTERP         0x000134 0x00400134 0x00400134 0x00014 0x00014 R   0x1
     [Requesting program interpreter: /lib/ld-uClibc.so.0]
 REGINFO        0x000148 0x00400148 0x00400148 0x00018 0x00018 R   0x4
 LOAD           0x000000 0x00400000 0x00400000 0x005b8 0x005b8 R E 0x10000
 LOAD           0x0005c0 0x004105c0 0x004105c0 0x00040 0x00040 RW  0x10000
 DYNAMIC        0x000160 0x00400160 0x00400160 0x000c8 0x000c8 RWE 0x4
 GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RWE 0x4
 NULL           0x000000 0x00000000 0x00000000 0x00000 0x00000     0x4

Section to Segment mapping:
 Segment Sections...
  00     
  01     .interp 
  02     .reginfo 
  03     .interp .reginfo .dynamic .hash .dynsym .dynstr .init .text .MIPS.stubs .fini .rodata 
  04     .data .rld_map .got 
  05     .dynamic 
  06     
  07     

Dynamic section at offset 0x160 contains 20 entries:
 Tag        Type                         Name/Value
0x00000001 (NEEDED)                     Shared library: [libc.so.0]
0x0000000f (RPATH)                      Library rpath: [/home/xia/Builds/H208N_V1.0_Dev/csp/release/tools/uclibc/lib]
0x0000000c (INIT)                       0x400460
0x0000000d (FINI)                       0x400580
0x00000004 (HASH)                       0x400228
0x00000005 (STRTAB)                     0x400390
0x00000006 (SYMTAB)                     0x400280
0x0000000a (STRSZ)                      208 (bytes)
0x0000000b (SYMENT)                     16 (bytes)
0x70000016 (MIPS_RLD_MAP)               0x4105d0
0x00000015 (DEBUG)                      0x0
0x00000003 (PLTGOT)                     0x4105e0
0x70000001 (MIPS_RLD_VERSION)           1
0x70000005 (MIPS_FLAGS)                 NOTPOT
0x70000006 (MIPS_BASE_ADDRESS)          0x400000
0x7000000a (MIPS_LOCAL_GOTNO)           2
0x70000011 (MIPS_SYMTABNO)              17
0x70000012 (MIPS_UNREFEXTNO)            19
0x70000013 (MIPS_GOTSYM)                0xb
0x00000000 (NULL)                       0x0

There are no relocations in this file.

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

Symbol table '.dynsym' contains 17 entries:
  Num:    Value  Size Type    Bind   Vis      Ndx Name
    0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
    1: 004105c0     0 NOTYPE  GLOBAL DEFAULT   12 _fdata
    2: 00000001     0 SECTION GLOBAL DEFAULT  ABS _DYNAMIC_LINKING
    3: 004185d0     0 NOTYPE  GLOBAL DEFAULT  ABS _gp
    4: 00400490     0 NOTYPE  GLOBAL DEFAULT    8 _ftext
    5: 004105d0     0 OBJECT  GLOBAL DEFAULT   13 __RLD_MAP
    6: 00410600     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    7: 00410600     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
    8: 004105e0     0 OBJECT  GLOBAL DEFAULT  ABS _GLOBAL_OFFSET_TABLE_
    9: 00410600     0 NOTYPE  GLOBAL DEFAULT  ABS _end
   10: 00410600     0 NOTYPE  GLOBAL DEFAULT  ABS _fbss
   11: 00400580    28 FUNC    GLOBAL DEFAULT   10 _fini
   12: 00400490    72 FUNC    GLOBAL DEFAULT    8 main
   13: 00400560     0 FUNC    GLOBAL DEFAULT  UND __uClibc_main
   14: 00400460    28 FUNC    GLOBAL DEFAULT    7 _init
   15: 00400550     0 FUNC    GLOBAL DEFAULT  UND sleep
   16: 00400540     0 FUNC    GLOBAL DEFAULT  UND printf

Histogram for bucket list length (total of 3 buckets):
Length  Number     % of total  Coverage
     0  0          (  0.0%)
     1  0          (  0.0%)      0.0%
     2  0          (  0.0%)      0.0%
     3  0          (  0.0%)      0.0%
     4  0          (  0.0%)      0.0%
     5  2          ( 66.7%)     62.5%
     6  1          ( 33.3%)    100.0%

No version information found in this file.
Attribute Section: gnu
File Attributes
 Tag_GNU_MIPS_ABI_FP: Soft float

Primary GOT:
Canonical gp value: 004185d0

Reserved entries:
  Address     Access  Initial Purpose
 004105e0 -32752(gp) 00000000 Lazy resolver
 004105e4 -32748(gp) 80000000 Module pointer (GNU extension)

Global entries:
  Address     Access  Initial Sym.Val. Type    Ndx Name
 004105e8 -32744(gp) 00400580 00400580 FUNC     10 _fini
 004105ec -32740(gp) 00400490 00400490 FUNC      8 main
 004105f0 -32736(gp) 00400560 00400560 FUNC    UND __uClibc_main
 004105f4 -32732(gp) 00400460 00400460 FUNC      7 _init
 004105f8 -32728(gp) 00400550 00400550 FUNC    UND sleep
 004105fc -32724(gp) 00400540 00400540 FUNC    UND printf

當我在沒有標誌的情況下交叉編譯我的程序(它只列印“hello world”)-static並嘗試執行它時,會發生以下情況:

# ls
hello.mips
# ./hello.mips
/bin/sh: ./hello.mips: Permission denied
# chmod +x hello.mips 
# ./hello.mips
/bin/sh: ./hello.mips: not found
# ls -la
drwxrwxrwx    2 zhangxia root            0 Aug  8 00:01 .
drwxr-xr-x    3 zhangxia root            0 Aug  7 22:46 ..
-rwsrwsrwx    1 888      root         5743 Aug  8 00:01 hello.mips

為什麼我在它那裡時找不到它?

所以我用-static標誌編譯它,這是readelf輸出(因為這裡的大小限制,我只放一部分)

readelf -a hello.static 
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:               0x400280
 Start of program headers:          52 (bytes into file)
 Start of section headers:          647608 (bytes into file)
 Flags:                             0x1007, noreorder, pic, cpic, o32, mips1
 Size of this header:               52 (bytes)
 Size of program headers:           32 (bytes)
 Number of program headers:         6
 Size of section headers:           40 (bytes)
 Number of section headers:         33
 Section header string table index: 30

Section Headers:
 [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
 [ 0]                   NULL            00000000 000000 000000 00      0   0  0
 [ 1] .note.ABI-tag     NOTE            004000f4 0000f4 000020 00   A  0   0  4
 [ 2] .reginfo          MIPS_REGINFO    00400114 000114 000018 18   A  0   0  4
 [ 3] .note.gnu.build-i NOTE            0040012c 00012c 000024 00   A  0   0  4
 [ 4] .rel.dyn          REL             00400150 000150 000098 08   A  0   0  4
 [ 5] .init             PROGBITS        004001e8 0001e8 000098 00  AX  0   0  4
 [ 6] .text             PROGBITS        00400280 000280 07b5a0 00  AX  0   0 16
 [ 7] __libc_freeres_fn PROGBITS        0047b820 07b820 0013a8 00  AX  0   0  4
 [ 8] .fini             PROGBITS        0047cbc8 07cbc8 000054 00  AX  0   0  4
 [ 9] .rodata           PROGBITS        0047cc20 07cc20 015a00 00   A  0   0 16
 [10] .eh_frame         PROGBITS        004a2620 092620 0019a4 00  WA  0   0  4
 [11] .gcc_except_table PROGBITS        004a3fc4 093fc4 00014e 00  WA  0   0  1
 [12] .tdata            PROGBITS        004a4114 094114 000010 00 WAT  0   0  4
 [13] .tbss             NOBITS          004a4124 094124 000018 00 WAT  0   0  4
 [14] .ctors            PROGBITS        004a4124 094124 000008 00  WA  0   0  4
 [15] .dtors            PROGBITS        004a412c 09412c 00000c 00  WA  0   0  4
 [16] .jcr              PROGBITS        004a4138 094138 000004 00  WA  0   0  4
 [17] .data.rel.ro      PROGBITS        004a413c 09413c 00259c 00  WA  0   0  4
 [18] .data             PROGBITS        004a66e0 0966e0 0007c0 00  WA  0   0 16
 [19] __libc_subfreeres PROGBITS        004a6ea0 096ea0 000030 00  WA  0   0  4
 [20] __libc_atexit     PROGBITS        004a6ed0 096ed0 000004 00  WA  0   0  4
 [21] .got              PROGBITS        004a6ee0 096ee0 000a48 04 WAp  0   0 16
 [22] .sdata            PROGBITS        004a7928 097928 000004 00 WAp  0   0  4
 [23] .sbss             NOBITS          004a7930 09792c 0000fc 00 WAp  0   0  8
 [24] .bss              NOBITS          004a7a30 09792c 001c10 00  WA  0   0 16
 [25] __libc_freeres_pt NOBITS          004a9640 09792c 000018 00  WA  0   0  4
 [26] .pdr              PROGBITS        00000000 09792c 006700 00      0   0  4
 [27] .comment          PROGBITS        00000000 09e02c 000039 01  MS  0   0  1
 [28] .gnu.attributes   LOOS+ffffff5    00000000 09e065 000010 00      0   0  1
 [29] .mdebug.abi32     PROGBITS        00001320 09e075 000000 00      0   0  1
 [30] .shstrtab         STRTAB          00000000 09e075 000140 00      0   0  1
 [31] .symtab           SYMTAB          00000000 09e6e0 006d70 10     32 655  4
 [32] .strtab           STRTAB          00000000 0a5450 0065f4 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.

Program Headers:
 Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
 REGINFO        0x000114 0x00400114 0x00400114 0x00018 0x00018 R   0x4
 LOAD           0x000000 0x00400000 0x00400000 0x92620 0x92620 R E 0x10000
 LOAD           0x092620 0x004a2620 0x004a2620 0x0530c 0x07038 RW  0x10000
 NOTE           0x0000f4 0x004000f4 0x004000f4 0x00020 0x00020 R   0x4
 NOTE           0x00012c 0x0040012c 0x0040012c 0x00024 0x00024 R   0x4
 TLS            0x094114 0x004a4114 0x004a4114 0x00010 0x00028 R   0x4

Section to Segment mapping:
 Segment Sections...
  00     .reginfo 
  01     .note.ABI-tag .reginfo .note.gnu.build-id .rel.dyn .init .text __libc_freeres_fn .fini .rodata 
  02     .eh_frame .gcc_except_table .tdata .ctors .dtors .jcr .data.rel.ro .data __libc_subfreeres __libc_atexit .got .sdata .sbss .bss __libc_freeres_ptrs 
  03     .note.ABI-tag 
  04     .note.gnu.build-id 
  05     .tdata .tbss 

There is no dynamic section in this file.

Relocation section '.rel.dyn' at offset 0x150 contains 19 entries:
Offset     Info    Type            Sym.Value  Sym. Name
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      
00000000  00000000 R_MIPS_NONE      

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

Symbol table '.symtab' contains 1751 entries:
  Num:    Value  Size Type    Bind   Vis      Ndx Name
    0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
    1: 004000f4     0 SECTION LOCAL  DEFAULT    1 
    2: 00400114     0 SECTION LOCAL  DEFAULT    2 


      1747: 004a5d58    36 OBJECT  GLOBAL DEFAULT   17 _nl_C_LC_IDENTIFICATION
 1748: 004a9580    76 OBJECT  GLOBAL DEFAULT   24 _dl_ns
 1749: 00450f20  3016 FUNC    GLOBAL DEFAULT    6 _nl_load_locale_from_arch
 1750: 004380e0   248 FUNC    WEAK   DEFAULT    6 wctrans

No version information found in this file.

Displaying notes found at file offset 0x000000f4 with length 0x00000020:
 Owner                 Data size   Description
 GNU                  0x00000010   NT_GNU_ABI_TAG (ABI version tag)
   OS: Linux, ABI: 2.6.18

Displaying notes found at file offset 0x0000012c with length 0x00000024:
 Owner                 Data size   Description
 GNU                  0x00000014   NT_GNU_BUILD_ID (unique build ID bitstring)
   Build ID: a56a4b258e108ec9affb61c4a8ba46527052bca9
Attribute Section: gnu
File Attributes
 Tag_GNU_MIPS_ABI_FP: Hard float (double precision)

兩個二進製文件(靜態和動態)在 quemu 和我的第二個 MIPS 機器上執行得很好。有什麼想法嗎?

“非法指令”可能是由於不同ld-uClibc的 libc 名稱/版本造成的嗎?

@Stephen-Kitt 在這裡

# ldd ./hello.mips 
/bin/sh: ldd: not found

cat proc/version 
Linux version 2.6.30.9 (xia@njzd) (gcc version 4.4.6 (Realtek RSDK-1.5.6p2) ) #2 Wed Apr 29 18:57:54 CST 2015

# cat proc/cpuinfo 
system type: RTL8672 
processor: 0 
cpu model               : 56322 
BogoMIPS: 619.31 
tlb_entries             : 64 
mips16 implemented      : yes

我基本上在執行 Ubuntu14.04

mips-gcc -o hello.mips hello.c
mips-gcc -static -o hello.static hello.c

mips gcc 來自 Debian 代表

$ mips-linux-gnu-gcc -v
Using built-in specs.
Target: mips-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/mips-linux-gnu/include/c++/4.4.5 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --disable-libssp --enable-targets=all --enable-checking=release --program-prefix=mips-linux-gnu- --includedir=/usr/mips-linux-gnu/include --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=mips-linux-gnu --with-headers=/usr/mips-linux-gnu/include --with-libs=/usr/mips-linux-gnu/lib
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8)

我從這裡獲得它http://www.emdebian.org/debian/

# cat proc/cpuinfo 
system type: RTL8672 
processor: 0 
cpu model: 56322 

RTL8672 不是完整的 MIPS 實現,而是 Lexra。您將需要一個知道如何處理此問題的定制工具鏈。

這樣的東西,或者對於僅二進制工具鏈,尋找rsdk; 例如,這個

引用自:https://unix.stackexchange.com/questions/220894