Why an executable program for a specific CPU does not work on Linux and Windows?

后端 未结 6 1545
滥情空心
滥情空心 2020-12-01 09:29

An executable problem like exe does not work on Linux (without wine). When compiling source code compiler produce object code which is specific to a particular cpu architect

相关标签:
6条回答
  • 2020-12-01 10:11

    It's due to the difference of how the program is loaded into memory and given resources to run. Even the simplest programs need to have code space, data space and the ability to acquire runtime memory and do I/O. The infrastructure to do these low-level tasks is completely different between the platforms, unless you have some kind of adaptation layer, like WINE or Cygwin.

    Assuming, however, that you could just inject arbitrary assembled CPU instructions into the code segment of a running process and get that code to execute, then, yes, the same code would run on either platform. However, it would be quite restricted, and doing complex things like even jumps to external modules would fail, due to how these things are done differently on different platforms.

    0 讨论(0)
  • 2020-12-01 10:13

    Yes, but, the code invariably calls out to library functions to do just about anything -- like printing "4" to the terminal. And these libraries are platform-specific, and differ between Linux and Windows. This is why it's not portable -- not, indeed, an instruction-level problem.

    0 讨论(0)
  • 2020-12-01 10:19

    There are many differences. Among them:

    1. Executable Format: Every OS requires the binaries to conform to a specific binary format. For Windows, this is Portable Executable (PE) format. For Linux, it's ELF most of the time (it supports other types too).

    2. Application Binary Interface: Each OS defines a set of primary system functions and the way a program calls them. This is fundamentally different between Linux and Windows. While the instructions that compute 2 + 2 are identical on Linux and Windows in x86 architecture, the way the application starts, the way it prints out the output, and the way it exits differs between the operating systems.

    Yes, both Linux and Windows programs on x86 architecture use the instruction set that the CPU supports which is defined by Intel.

    0 讨论(0)
  • 2020-12-01 10:21

    To the second question: Windows only runs on x86, x64, and IA64 (not sure about the mobile versions). For Linux, see here.

    0 讨论(0)
  • 2020-12-01 10:26

    Here are some of the reasons I can think of off the top of my head:

    1. Different container formats (which so far seems to be the leading differentiator in this answer -- however its not the only reason).
    2. different dynamic linker semantics.
    3. different ABI.
    4. different exception handling mechanisms -- windows has SEH -- upon which C++ exception handling is built
    5. different system call semantics and different system calls -- hence different low-level libraries.
    0 讨论(0)
  • 2020-12-01 10:31

    Problem 1 is the image format. When an application is launched into execution the Os has to load the applicaiton image, find out its entry point and launch it from there. That means that the OS must understand the image format and there are different formats between various OS.

    Problem 2 is access to devices. Once launched an application can read and write registries in the CPU and that's about it. To do anything interesting, like to display a character on a console, it needs access to devices and that means it has to ask for such access from the OS. Each Os has a different API that is offered to access such devices.

    Problem 3 is priviledges instructions. The newly launched process would perhaps need a memory location to store something, can't accomplish everything with regiestries. This means it needs to allocate RAM and set up the translation from VA to physical address. These are priviledges operations only the OS can do and again, the API to access these services vary between OSs.

    Bottom line is that applications are not writen for a CPU, but for a set of primitive services the OS offer. the alternative is to write the apps against a set of primitive services a Virtual Machine offers, and this leads to apps that are more or less portable, like Java apps.

    0 讨论(0)
提交回复
热议问题