jq icon indicating copy to clipboard operation
jq copied to clipboard

[bug]mips version jq cannot run on my mips devices

Open KonkaTV514 opened this issue 7 months ago • 6 comments

Describe the bug

mips version of jq cannot run on my mips devices: Illegal instruction

To Reproduce

  1. run jq
  2. Illegal instruction

Expected behavior

Show jq's help.

Environment (please complete the following information):

  • OS and Version: Linux TianyiGateway 4.1.45
  • jq version 1.8.0

Additional context This is the file and readelf of my device's built-in busybox and downloaded jq:

0|u0_a990@localhost: ~/jq $ file busybox
busybox: ELF 32-bit MSB executable, MIPS, MIPS32 version 1 (SYSV), dynamically linked, interpreter /lib/ld-uClibc.so.0, stripped
0|u0_a990@localhost: ~/jq $ file jq                                        jq: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), statically linked, BuildID[sha1]=c136e7128099ea6e63d2f7fb3d3f849e2d0e64de, for GNU/Linux 3.2.0, stripped                                                      0|u0_a990@localhost: ~/jq $ readelf -h busybox                                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
  Version:                           0x1
  Entry point address:               0x4037f0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          181600 (bytes into file)
  Flags:                             0x50001007
  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:         30
  Section header string table index: 29
0|u0_a990@localhost: ~/jq $ readelf -h jq
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
  Version:                           0x1                                     Entry point address:               0x402f70
  Start of program headers:          52 (bytes into file)
  Start of section headers:          2006136 (bytes into file)
  Flags:                             0x70001007
  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:         26
  Section header string table index: 25
0|u0_a990@localhost: ~/jq $

KonkaTV514 avatar Jun 20 '25 04:06 KonkaTV514

Hi, the jq binary seem to be "rel2" which faster some searching i think could mean it has some extra instructions. At least qemu-mips runs it fine. Possible for you to run in a gdb etc and see where and what instructions it fails on? what hardware is this btw?

$ sha256sum ./jq-linux-mips
34069dfcaee4168fdced283e5732de8c558d696cad1910e607d9b68f80211a16  ./jq-linux-mips

$ file jq-linux-mips
jq-linux-mips: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1 (SYSV), statically linked, BuildID[sha1]=c136e7128099ea6e63d2f7fb3d3f849e2d0e64de, for GNU/Linux 3.2.0, stripped

$ qemu-mips ./jq-linux-mips
jq - commandline JSON processor [version 1.8.0]

Usage:	jq [options] <jq filter> [file...]
	jq [options] --args <jq filter> [strings...]
	jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified except for formatting. For more advanced filters see
the jq(1) manpage ("man jq") and/or https://jqlang.org/.

Example:

	$ echo '{"foo": 0}' | jq .
	{
	  "foo": 0
	}

For listing the command options, use jq --help.

$ qemu-mips ./jq-linux-mips --version
jq-1.8.0

$ qemu-mips ./jq-linux-mips -n 1+2
3

wader avatar Jun 20 '25 10:06 wader

I wonder if we should build mips with -march=mips1 etc for max compatibility? is march mips32r3 default somehow?

wader avatar Jun 20 '25 10:06 wader

My device doesn't have commands like gdb, nor any package manager.

/proc/cpuinfo:
system type             : 968488X41
machine                 : Unknown
processor               : 0
cpu model               : Broadcom BMIPS4350 V8.1
BogoMIPS                : 598.01
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 32
extra interrupt vector  : no
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

processor               : 1
cpu model               : Broadcom BMIPS4350 V8.1
BogoMIPS                : 606.20
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 32
extra interrupt vector  : no
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

KonkaTV514 avatar Jun 20 '25 10:06 KonkaTV514

I see. If i tell qemu to emulate the oldest cpu model i could find it fails with illegal instruction, but not sure if same instruction or not. Maybe will get some time to look into it later in the weekend.

$ qemu-mips -cpu 4Kc jq-linux-mips
qemu: uncaught target signal 4 (Illegal instruction) - core dumped
Illegal instruction

wader avatar Jun 20 '25 19:06 wader

You can refer to this issue: https://github.com/rkd77/elinks/issues/379

KonkaTV514 avatar Jun 21 '25 01:06 KonkaTV514

Tried building as mips1 but qemu still fails. Also made sure the binary is big endian. My main suspect is that your device does not support hard float. I failed to figure out how to cross build mips soft float, is a bit of a mess.

wader avatar Jun 21 '25 10:06 wader