Linux 上的 COFF 或 Windows 上的 ELF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1892677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:34:48  来源:igfitidea点击:

COFF on Linux or ELF on Windows

windowsunixelfcoff

提问by PJT

Is it possible to run the COFF executable files on UNIX or the ELF executable files on Windows? And what would be the steps to be able to run either file type on Windows and UNIX. I'm just curious.

是否可以在 UNIX 上运行 COFF 可执行文件或在 Windows 上运行 ELF 可执行文件?以及能够在 Windows 和 UNIX 上运行任一文件类型的步骤是什么。我只是好奇。

采纳答案by Michael Dillon

To actually run executables and have them do useful stuff, you need to worry about the API, not just the executable file format. On a Linux machine with WINE installed, you can run Windows .EXE files from the command line and they do the same thing that they do on Windows.

要实际运行可执行文件并让它们做有用的事情,您需要担心 API,而不仅仅是可执行文件格式。在安装了 WINE 的 Linux 机器上,您可以从命令行运行 Windows .EXE 文件,它们执行的操作与在 Windows 上执行的操作相同。

The other way around is not really possible, however if you install CYGWIN on a Windows machine, and then rebuild the application from source with CYGWIN compilers, you will get an executable that runs on Windows and does the same thing that the Linux executable does on Linux. Lots of standard Linux tools are already ported and in the CYGWIN repository including stuff like X-Windows and GIMP.

另一种方法是不可能的,但是,如果您在 Windows 机器上安装 CYGWIN,然后使用 CYGWIN 编译器从源代码重建应用程序,您将获得一个在 Windows 上运行的可执行文件,并执行与 Linux 可执行文件相同的操作Linux。许多标准的 Linux 工具已经被移植并在 CYGWIN 存储库中,包括像 X-Windows 和 GIMP 这样的东西。

回答by Micah W

To answer your question properly, it is relevant to review what ELF, COFF, and PE are. These binary formats are essentially just containers that give directions to the operating system about how to execute the raw CPU instructions contained in the file. They are very much like audio/video containers like MKV, WMV, and OGG. Support for the executable format is either in the operating system or not. Microsoft Windows has consistently not given any support for COFF or ELF, until recently. With Windows 10, Microsoft has provided indirect support for ELF by building into the Windows kernel UserMode-Linux compatible system routines. A UserMode Linux kernel runs on top of the Windows kernel and runs all ELF binary formats almost as if it were running independent of MS Windows.

要正确回答您的问题,有必要回顾一下 ELF、COFF 和 PE 是什么。这些二进制格式本质上只是向操作系统提供有关如何执行文件中包含的原始 CPU 指令的指示的容器。它们非常像 MKV、WMV 和 OGG 等音频/视频容器。操作系统是否支持可执行格式。Microsoft Windows 一直不支持 COFF 或 ELF,直到最近。在 Windows 10 中,Microsoft 通过构建到 Windows 内核 UserMode-Linux 兼容系统例程中,为 ELF 提供了间接支持。UserMode Linux 内核运行在 Windows 内核之上,运行所有 ELF 二进制格式,几乎就像它独立于 MS Windows 运行一样。

The alternative to using the UserMode-Linux (sub-kernel) being for Microsoft to rewrite the majority of the Linux API in a completely compatible format, their choice solves one other compatibility issue: The API. "A" stands for Application and "I" for Interface, however the API as an interface is mainly just a set of executable routines and environment assumptions. Access to the filesystem and most basic system routines is provided by the Windows kernel, while everything else is provided in the UserMode Linux kernel. This way not only can Windows run ELF formatted executables, but in can run the most popular ELF executables that are already made to run on the Linux API.

Microsoft 使用 UserMode-Linux(子内核)的替代方案是以完全兼容的格式重写大部分 Linux API,他们的选择解决了另一个兼容性问题: API. “A”代表Application,“I”代表Interface,但是API作为接口主要只是一组可执行的例程和环境假设。对文件系统和大多数基本系统例程的访问由 Windows 内核提供,而其他一切都由 UserMode Linux 内核提供。通过这种方式,Windows 不仅可以运行 ELF 格式的可执行文件,而且可以运行已经在 Linux API 上运行的最流行的 ELF 可执行文件。

The reverse, the other half of the question, running PE (most Microsoft Windows executables) on Linux is possible as well. There are two runtime wrapping libraries that can run MSIL (virtual machine application) and Win32 (normal CPU application). Because the Linux kernel is extendable to recognize a certain byte format, then run an appropriate wrapper program, in effect the kernel supports PE and potentially more executable container formats. Therefore, Linux can run some PE programs either in the mono runtime (.NET/C# applications) or in the WINE runtime (Win32 C/C++).

反过来,问题的另一半,在 Linux 上运行 PE(大多数 Microsoft Windows 可执行文件)也是可能的。有两个运行时包装库可以运行 MSIL(虚拟机应用程序)和 Win32(普通 CPU 应用程序)。由于 Linux 内核可扩展以识别特定字节格式,然后运行适当的包装程序,因此内核实际上支持 PE 和可能更可执行的容器格式。因此,Linux 可以在 Mono 运行时(.NET/C# 应用程序)或 WINE 运行时(Win32 C/C++)中运行一些 PE 程序。

To install the UserMode-Linux environment you can follow instructions provided on Microsoft's Development Network. To summarize:

要安装 UserMode-Linux 环境,您可以按照Microsoft's Development Network上提供的说明进行操作。总结一下:

  1. Turn on Developer Mode: Settings | Update & Security | For Developers | Check the Developer Mode radio button
  2. From the start menu, open “Turn Windows Features on or off”
  3. Scroll down and check the “Windows Subsystem for Linux (Beta)” feature
  4. Hit okay and reboot (required step)
  5. Once rebooted, open a PowerShell/command prompt and run “Bash” and follow the simple prompts to accept Canonical's license and kick-off the download of the Ubuntu image
  6. After download has completed, you'll be able to start “Bash on Ubuntu on Windows” from the Start menu
  1. 打开开发者模式:设置 | 更新与安全 | 对于开发者 | 检查开发人员模式单选按钮
  2. 在开始菜单中,打开“打开或关闭 Windows 功能”
  3. 向下滚动并检查“Windows Subsystem for Linux (Beta)”功能
  4. 点击 OK 并重新启动(必需的步骤)
  5. 重新启动后,打开 PowerShell/命令提示符并运行“Bash”并按照简单的提示接受 Canonical 的许可并开始下载 Ubuntu 映像
  6. 下载完成后,您将能够从“开始”菜单启动“Windows 上的 Ubuntu 上的 Bash”

Be aware this method only works on Windows 10 and is still limited to text-mode console and a Win32 port of Xorg like vcXsrv for anything graphical. Cygwin or MSYS2 systems are not able to run ELF binaries, but make it possible to port and run the same applications that are normally ELF binaries on a Linux system.

请注意,此方法仅适用于 Windows 10,并且仍然仅限于文本模式控制台和 Xorg 的 Win32 端口,例如 vcXsrv,用于任何图形。Cygwin 或 MSYS2 系统无法运行 ELF 二进制文件,但可以移植和运行与 Linux 系统上通常是 ELF 二进制文件相同的应用程序。

回答by sebbu

http://lbw.sourceforge.net/works better than line. low was another project for doing the same thing, but that was the less working.

http://lbw.sourceforge.net/比线路更好用。low 是另一个做同样事情的项目,但效果较差。

EDIT: http://atratus.org/seems to do the same as well, without the need to have Interix/SFU.

编辑:http: //atratus.org/似乎也做同样的事情,不需要 Interix/SFU。

回答by Steven Schlansker

COFF was originally introduced by UNIX (around System V or thereabouts) so yes, some UNIX probably still supports COFF format. It's been deprecated by Linux at least for a while, and presumably most other Unices have also deprecated or outright dropped support.

COFF 最初是由 UNIX 引入的(在 System V 或附近)所以是的,一些 UNIX 可能仍然支持 COFF 格式。它已被 Linux 弃用至少有一段时间了,据推测,大多数其他 Unices 也已弃用或完全放弃支持。

Windows ELF support is a bit more iffy - almost certainly not there without some deep trickery. You should be more specific about what you're trying to do here...

Windows ELF 支持有点不确定 - 几乎可以肯定没有一些深奥的技巧就不会存在。您应该更具体地了解您要在这里做什么...