linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan-Benedict Glaw <jbglaw@lug-owl.de>
To: linux-c-programming@vger.kernel.org
Subject: Re: sequence of steps that goes from compilation to execution
Date: Wed, 18 Aug 2004 14:10:58 +0200	[thread overview]
Message-ID: <20040818121058.GG16935@lug-owl.de> (raw)
In-Reply-To: <55ee29e004081712346075d5b4@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2981 bytes --]

On Wed, 2004-08-18 01:04:51 +0530, Sudheer Vutukuru <sudheer.vutukuru@gmail.com>
wrote in message <55ee29e004081712346075d5b4@mail.gmail.com>:
> 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.

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

>  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

-- 
Jan-Benedict Glaw       jbglaw@lug-owl.de    . +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));

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

  reply	other threads:[~2004-08-18 12:10 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-17 19:34 sequence of steps that goes from compilation to execution Sudheer Vutukuru
2004-08-18 12:10 ` Jan-Benedict Glaw [this message]
  -- strict thread matches above, loose matches on Subject: below --
2004-08-23 15:50 Huber, George K RDECOM CERDEC STCD SRI

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040818121058.GG16935@lug-owl.de \
    --to=jbglaw@lug-owl.de \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).