![](/img/trans.png)
[英]Assembled program gives error: /a.out: cannot execute binary file: Exec format error
[英]OpenBSD fails to execute a.out
我編寫了這個程序,它應該以退出代碼 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)上做同樣的事情,kernel 拒絕執行代碼,它被傳遞給 shell,其中課程失敗:
openbsd$ ./a.out
./a.out[1]: syntax error: `(' unexpected
奇怪的是,該文件說它是一個 ELF 二進制文件,因此通常應該由 kernel 執行
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:在 OpenBSD 和 NetBSD 上,使用 main 更改 _start 並在沒有 -nostdlib 的情況下進行編譯都可以正常工作
我發現 OpenBSD 檢查 .section ".note.openbsd.ident", "a"。 如果它不存在,則不會執行該文件。 如果我將 _start 替換為 main 並且不使用 -nostdlib 進行鏈接,則可以在 libc 中找到 .note.openbsd.ident。 對於 NetBSD 也是如此。
要了解更多信息,請訪問https://www.exploit-db.com/papers/13219
在最新版本的 NetBSD 上,您還可以通過僅包含/usr/lib/sysident.o
來鏈接以_start
符號和-nostdlib
開頭的匯編語言程序,例如:
as -o thello.o thello.s && ld -Bstatic -o thello /usr/lib/sysident.o thello.o
在最近的 NetBSD 上,這個 object 還包含一個.note.netbsd.pax
部分,用於控制 PaX 可執行文件的安全功能,例如 Mprotect、ASLR 和 Segvguard。
查看#include <elf/common.h>
文件,了解其他類型目標系統的類似注釋部分的定義。
另見https://polprog.net/blog.netbsdasmprog/
另見https://github.com/robohack/experiments/blob/master/thello.s
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.