任何人都已经能够创建PE COFF和ELF的混合体? [英] Has anyone been able to create a hybrid of PE COFF and ELF?

查看:188
本文介绍了任何人都已经能够创建PE COFF和ELF的混合体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是可以在Win32和Linux的I386一个二进制文件运行?

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

推荐答案

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

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


  • PE文件的最初两个字符必须是'M''Z';

  • ELF文件的最初四个字符必须是'\\ 0x7F部分''E''L''F'

  • 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'.

显然,你不能创建一个文件,该文件satisifies这两种格式。

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

在回答关于一个通晓多国语言的二进制既是一个16位的COM文件和一个Linux的ELF文件中有效的评论,这是可能的(虽然一个真正的COM文件是一个DOS程序,而不是Windows - 当然也不是Win32的)。

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).

这里有一个我拼凑 - 与NASM编译。它的工作原理,因为前两个字节一个ELF文件('\\ 0x7F部分''E')恰好也是有效的8086机code(45字节的相对跳转 - 如果-大于指令)。从布赖恩雷泰 cribbed最小ELF头。

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$`

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     $ - $$

这篇关于任何人都已经能够创建PE COFF和ELF的混合体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆