Skip to contentSkip to conversation

sequence of steps that goes from compilation to execution

DISCUSSION Sudheer Vutukuru 2 participants 2 messages Aug 17, 2004 – Aug 18, 2004

lore
Share & tools
lore
#0 lore raw

Hi all,

Why a program which is compiled on linux does not execute on windows..
(though the machine architecture is same)..

Also Can u please suggest me references or help me in knowing the
details of what goes on, from compilation to execution (i.e how loading,linking libraries is done.. how addresses
are known dynamically etc.. ).

for string literals whether memory is allocated in heap or in code
segment only..
i.e char* ptr = "Hello World";
the ptr points into code segement or heap..

Thanks,

***************************************************************************
V V N Sudheer,
Master of Technology,
Dept. of Computer Science,
IIT Kanpur.
***************************************************************************

#1 lore raw

On Wed, 2004-08-18 01:04:51 +0530, Sudheer Vutukuru <[email protected]> wrote in message <[email protected]>:

Quoted 4 lines

Hi all,

Why a program which is compiled on linux does not execute on windows..
(though the machine architecture is same)..

It's not enough that both operating system / game loader use the same (or at least widely compatible) CPUs. Also, the whole ABI (Application Binary Interface) and the backing file format need to be supported.

Linux uses the ELF format, while Windows uses something that's IIRC called PEXEC (paged executable) format. That is, Windows doesn't simply know where to load the binary to, how to initialize BSS and the like. However, one can try to play some dirty tricks like Wine (running Windows binaries on Linux) does. You do all the loading manually and just start it. Upon access violations (and other things that would go wrong), control is given back to Wine to emulate the missing parts.

Quoted 4 lines

Also Can u please suggest me references or help me in knowing the
details of what goes on, from compilation to execution (i.e how loading,linking libraries is done.. how addresses
are known dynamically etc.. ).

C code gets translated to assembler, which is then compiled to object code. (This format is already different on Windows vs. Linux.) The object code then gets linked to the resulting final binary.

(The Linuxish view) upon startup of a dynamically linked ELF binary, binfmt_elf.c starts the binary's "interpreter", which has to finally (dynamically) link the binary. This is basically done by mmap()ing the needed libraries and correcting all symbol references (so by now, the program really knows about "printf").

After linking, control is transferred to the libc's startup function, which will initialize the buffered IO subsystem (fprintf and friends), call any constructor functions and (finally) jump to main(), your program's user-visible start function.

After main() returns, control is given back to libc's shutdown functions which may do some cleanups (like calling destructors, flushing buffers, calling atexit functions and so on).

I guess it's quite similar on Windows, but a different file format is used for binaries and libraries.

Quoted 4 lines

for string literals whether memory is allocated in heap or in code
segment only..
i.e char* ptr = "Hello World";
the ptr points into code segement or heap..

It points to a read-only data segment:)

Basically, it depends on where you define a variable and how/if you initialize it at all where backing storage is taken from.

MfG, JBG

Signature · 4 lines
Jan-Benedict Glaw       [email protected]    . +49-172-7608481             _ O _
"Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg  _ _ O
 fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!   O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));