I wrote this program that should just exit with exitcode 44:
// prog.S
#include <sys/syscall.h>
.text
.globl _start
_start:
subl $8, %esp
pushl $44
pushl $0
movl $SYS_exit, %eax
int $0x80
I compiled with
$ cc prog.S -nostdlib -o a.out
and run
$./a.out
Doing so on FreeBSD 13.0-RELEASE i386 (clang 11.0.1) worked fine. In fact, the executable runs and the exit code of the program is 44 as it should be.
However, doing the same on OpenBSD 7.0 GENERIC.MP#5 i386 (clang version 11.1.0) and on NetBSD 9.2 i386 (gcc 7.5.0), the kernel refused to execute the code and it was passed to the shell, which of course failed:
openbsd$ ./a.out
./a.out[1]: syntax error: `(' unexpected
The strange thing is also that file says it's an ELF binary and therefore should be normally executed by the kernel
openbsd$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
Even objdump prints what is expected to print
openbsd$ objdump -d a.out
a.out: file format elf32-i386
Disassembly of section .text:
00001184 <_start>:
1184: 83 ec 08 sub $0x8,%esp
1187: 6a 2c push $0x2c
1189: b8 01 00 00 00 mov $0x1,%eax
118e: 6a 00 push $0x0
1190: cd 80 int $0x80
Any idea about what I'm doing wrong?
PS: Changing _start with main and compiling without -nostdlib works fine both on OpenBSD and NetBSD
file a.out
say – Plainsong