views:

349

answers:

5

What does executable actually contain ? .. Does it contain instructions to processor in the form of Opcode and Operands ? If so why we have different executables for different operating systems ?

+8  A: 

Yes, code in the form of opcodes and operands, and data of course. Anything you want to do that involves the operating system in any way depends on the operating system, not on the CPU. That is why you need different programs for different operating systems. Opening a window in Windows is not done with the same sequence of instructions as in Linux, and so on.

unwind
Executables oft-times contain meta-data about the program itself which the OS can use for some things. I think `.com` files were the last remnant of executable programs that were only code and data where the OS just would load the file into memory, point the IP over there and let it run :-)
Joey
+6  A: 

As unwind implied in his answer, an executable file contains calls to routines in the Operating System.

It would be extremely inefficient for an executable file to try to implement functions already provided by the OS (for example, writing to disk, accepting input) so heavy use is made of calls to the OS functions.

Different Operating Systems provide functions which do similar things, but the details of how to call those functions (and where they are) may be different.

So, apart from the major differences of processor type, executables written for one OS won't work with another.

pavium
+3  A: 

To do any form of IO, an executable needs to interface with the Operating System using sys-calls. in Windows these are calls to the Win32 API and on linux/unit these are mostly posix calls.

Furthermore, the executable file format differs with the OS the same way a PNG file differs from a GIF file. the data is ordered differently and there are different headers and sub-headers.

shoosh
In Windows the system calls make up the Native API, not the Win32 API. Win32 is a remnant from the pre-NT days, and is just a wrapper around the Native API.
wj32
True, other than the fact that the native API is not officially documented and you're not supposed to use it ever.
shoosh
A: 

Check these links also:

Groo
+3  A: 

Processors understand programs in terms of opcodes - so your intution about executables containing opcodes is correct, and you guessed correctly that any executable has to have opcodes and operands for executing the program on a processor.

However, programs mostly execute with the help of operating systems (you can write programs which do not use an OS to execute, but that would be a lot of unnecessary work) - which provide abstractions on top of the hardware which the programs can use. The OS is responsible for setting up a "context" for any program to run i.e. provide the program the memory it needs, provide general purpose libraries which the program can use for doing common stuff such as write to files, print to console etc.

However, to set up the context for the program (provide it memory, load its data, set up a stack for it), the OS needs to read a program's executable file and needs to know a few things about the program such as the data which the program expects to use, size of that data, the initial values stored in that data region, the list of opcodes that make up the program (also called the text region of a process), their size etc. All of this data and a lot more (debugging information, readonly data such as hardcoded strings in the program, symbol tables etc) is stored within the executable file. Each OS understands a different format of this executable file, since they expect all this info to be stored in the executable in different ways. Check out the links provided by Groo.

A couple of formats that have been used for storing information in an executable file are ELF and COFF on UNIX systems and PE on Windows.

P.S. - Not all programs need executable formats. Look up bootloaders on Google. These are special programs which occupy the first sector of a bootable partition on the hard-disk and are used to load the OS itself.

Sudhanshu