有人能够创建 PE COFF 和 ELF 的混合体吗?

发布于 2024-08-18 16:25:29 字数 47 浏览 3 评论 0原文

我的意思是单个二进制文件可以在 Win32 和 Linux i386 中运行吗?

I mean could a single binary file run in both Win32 and Linux i386 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-08-25 16:25:29

这是不可能的,因为这两种类型的格式有冲突:

  • PE 文件的前两个字符必须是 'M' 'Z'
  • ELF 文件的前四个字符必须为 '\x7f' 'E' 'L' 'F'

显然,您无法创建一个同时满足这两种格式的文件。


作为对关于多语言二进制文件同时作为 16 位 COM 文件和 Linux ELF 文件有效的评论的回应,这是可能的(尽管 COM 文件实际上是 DOS 程序,而不是 Windows - 当然也不是 Win32)。

这是我拼凑出来的一个 - 用 NASM 编译它。它之所以有效,是因为 ELF 文件的前两个字节 ('\x7f' 'E') 碰巧也是有效的 8086 机器代码(45 字节相对跳转指令)。最小 ELF 标头抄自 Brian Raiter

BITS 32
ORG 0x08048000

  ehdr:                                                 ; Elf32_Ehdr
                db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
        times 8 db      0
                dw      2                               ;   e_type
                dw      3                               ;   e_machine
                dd      1                               ;   e_version
                dd      _start                          ;   e_entry
                dd      phdr - $                       ;   e_phoff
                dd      0                               ;   e_shoff
                dd      0                               ;   e_flags
                dw      ehdrsize                        ;   e_ehsize
                dw      phdrsize                        ;   e_phentsize
                dw      1                               ;   e_phnum
                dw      0                               ;   e_shentsize
                dw      0                               ;   e_shnum
                dw      0                               ;   e_shstrndx
  ehdrsize      equ     $ - ehdr

times 0x47-($-$) db    0

; DOS COM File code
BITS 16
    mov dx, msg1 - $ + 0x100
    mov ah, 0x09
    int 0x21
    mov ah, 0x00
    int 0x21
  msg1:         db      `Hello World (DOS).\r\n

这是不可能的,因为这两种类型的格式有冲突:

  • PE 文件的前两个字符必须是 'M' 'Z'
  • ELF 文件的前四个字符必须为 '\x7f' 'E' 'L' 'F'

显然,您无法创建一个同时满足这两种格式的文件。


作为对关于多语言二进制文件同时作为 16 位 COM 文件和 Linux ELF 文件有效的评论的回应,这是可能的(尽管 COM 文件实际上是 DOS 程序,而不是 Windows - 当然也不是 Win32)。

这是我拼凑出来的一个 - 用 NASM 编译它。它之所以有效,是因为 ELF 文件的前两个字节 ('\x7f' 'E') 碰巧也是有效的 8086 机器代码(45 字节相对跳转指令)。最小 ELF 标头抄自 Brian Raiter

BITS 32 phdr: ; Elf32_Phdr dd 1 ; p_type dd 0 ; p_offset dd $ ; p_vaddr dd $ ; p_paddr dd filesize ; p_filesz dd filesize ; p_memsz dd 5 ; p_flags dd 0x1000 ; p_align phdrsize equ $ - phdr ; Linux ELF code _start: mov eax, 4 ; SYS_write mov ebx, 1 ; stdout mov ecx, msg2 mov edx, msg2_len int 0x80 mov eax, 1 ; SYS_exit mov ebx, 0 int 0x80 msg2: db `Hello World (Linux).\n` msg2_len equ $ - msg2 filesize equ $ - $

This is not possible, because the two types have conflicting formats:

  • The initial two characters of a PE file must be 'M' 'Z';
  • The initial four characters of an ELF file must be '\x7f' 'E' 'L' 'F'.

Clearly, you can't create one file that satisifies both formats.


In response to the comment about a polyglot binary valid as both a 16 bit COM file and a Linux ELF file, that's possible (although really a COM file is a DOS program, not Windows - and certainly not Win32).

Here's one I knocked together - compile it with NASM. It works because the first two bytes of an ELF file ('\x7f' 'E') happen to also be valid 8086 machine code (a 45 byte relative jump-if-greater-than instruction). Minimal ELF headers cribbed from Brian Raiter.

BITS 32
ORG 0x08048000

  ehdr:                                                 ; Elf32_Ehdr
                db      0x7F, "ELF", 1, 1, 1, 0         ;   e_ident
        times 8 db      0
                dw      2                               ;   e_type
                dw      3                               ;   e_machine
                dd      1                               ;   e_version
                dd      _start                          ;   e_entry
                dd      phdr - $                       ;   e_phoff
                dd      0                               ;   e_shoff
                dd      0                               ;   e_flags
                dw      ehdrsize                        ;   e_ehsize
                dw      phdrsize                        ;   e_phentsize
                dw      1                               ;   e_phnum
                dw      0                               ;   e_shentsize
                dw      0                               ;   e_shnum
                dw      0                               ;   e_shstrndx
  ehdrsize      equ     $ - ehdr

times 0x47-($-$) db    0

; DOS COM File code
BITS 16
    mov dx, msg1 - $ + 0x100
    mov ah, 0x09
    int 0x21
    mov ah, 0x00
    int 0x21
  msg1:         db      `Hello World (DOS).\r\n

This is not possible, because the two types have conflicting formats:

  • The initial two characters of a PE file must be 'M' 'Z';
  • The initial four characters of an ELF file must be '\x7f' 'E' 'L' 'F'.

Clearly, you can't create one file that satisifies both formats.


In response to the comment about a polyglot binary valid as both a 16 bit COM file and a Linux ELF file, that's possible (although really a COM file is a DOS program, not Windows - and certainly not Win32).

Here's one I knocked together - compile it with NASM. It works because the first two bytes of an ELF file ('\x7f' 'E') happen to also be valid 8086 machine code (a 45 byte relative jump-if-greater-than instruction). Minimal ELF headers cribbed from Brian Raiter.

BITS 32 phdr: ; Elf32_Phdr dd 1 ; p_type dd 0 ; p_offset dd $ ; p_vaddr dd $ ; p_paddr dd filesize ; p_filesz dd filesize ; p_memsz dd 5 ; p_flags dd 0x1000 ; p_align phdrsize equ $ - phdr ; Linux ELF code _start: mov eax, 4 ; SYS_write mov ebx, 1 ; stdout mov ecx, msg2 mov edx, msg2_len int 0x80 mov eax, 1 ; SYS_exit mov ebx, 0 int 0x80 msg2: db `Hello World (Linux).\n` msg2_len equ $ - msg2 filesize equ $ - $
晨与橙与城 2024-08-25 16:25:29

这两种格式差异很大,不太可能混合。

然而,Linux支持通过“解释器”加载不同的可执行格式。例如,通过这种方式,包含 CIL(编译的 C# 或其他 .NET 语言)的编译 .exe 文件可以直接在 Linux 下执行。

The two formats are sufficiently different that a hybrid is unlikely.

However, Linux supports loading different executable formats by "interpreter". This way compiled .exe files containing CIL (compiled C# or other .NET languages) can be executed directly under Linux, for example.

你在看孤独的风景 2024-08-25 16:25:29

当然。使用Java。

Sure. Use Java.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文