我编写了这个程序,它应该以 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
我编译了
$ cc prog.S -nostdlib -o a.out
并运行
$./a.out
在 FreeBSD 13.0-RELEASE i386 (clang 11.0.1) 上这样做可以正常工作。事实上,可执行文件运行并且程序的退出代码应该是 44。
然而,在 OpenBSD 7.0 GENERIC.MP#5 i386(clang 版本 11.1.0)和 NetBSD 9.2 i386(gcc 7.5.0)上做同样的事情,内核拒绝执行代码并且它被传递给 shell,哪个课程失败:
openbsd$ ./a.out
./a.out[1]: syntax error: `(' unexpected
奇怪的是那个文件说它是一个 ELF 二进制文件,因此应该由内核正常执行
openbsd$ file a.out
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
甚至 objdump 也会打印预期打印的内容
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
知道我做错了什么吗?
PS:使用 main 更改 _start 并在没有 -nostdlib 的情况下进行编译在 OpenBSD 和 NetBSD 上都可以正常工作