Valgrind: Unrecognised instruction at address 0x... in tiny assembly program

Viewed 126

I have a tiny x86 Assembly program:

.global _start

.text

_start:
    movl $1, %eax
    movl $0, %ebx
    int $0x80

I assemble & link it with:

as demo.s -o demo.o
ld demo.o -o demo

And when I execute it with valgrind I get this error:

$ valgrind ./demo
...
==NNNNN==
vex amd64->IR: unhandled instruction bytes: 0xCD 0x80 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0
vex amd64->IR:   REX=0 REX.W=0 REX.R=0 REX.X=0 REX.B=0
vex amd64->IR:   VEX=0 VEX.L=0 VEX.nVVVV=0x0 ESC=NONE
vex amd64->IR:   PFX.66=0 PFX.F2=0 PFX.F3=0
==NNNNN== valgrind: Unrecognised instruction at address 0x40100a.
==NNNNN==    at 0x40100A: ??? (in /[...]/demo)
==NNNNN== Your program just tried to execute an instruction that Valgrind
==NNNNN== did not recognise.  There are two possible reasons for this.
==NNNNN== 1. Your program has a bug and erroneously jumped to a non-code
==NNNNN==    location.  If you are running Memcheck and you just saw a
==NNNNN==    warning about a bad jump, it's probably your program's fault.
==NNNNN== 2. The instruction is legitimate but Valgrind doesn't handle it,
==NNNNN==    i.e. it's Valgrind's fault.  If you think this is the case or
==NNNNN==    you are not sure, please let us know and we'll try to fix it.
==NNNNN== Either way, Valgrind will now raise a SIGILL signal which will
==NNNNN== probably kill your program.
==NNNNN==
==NNNNN== Process terminating with default action of signal 4 (SIGILL)
==NNNNN==  Illegal opcode at address 0x40100A
==NNNNN==    at 0x40100A: ??? (in /[...]/demo)
==NNNNN==
...

Without valgrind the program works (at least I get the correct exit code) — I'm obviously doing something wrong here.

1 Answers

Problem was that I was executing a 32-bit syscall in a 64-bit binary.

Just changing the syscall to the 64-bit version solved it:

_start:
    movq $60, %rax
    movq $0, %rdi
    syscall
Related