Amandil wrote:
> I was wondering if anyone out there can point me in the right
> direction, and tell me which newsgroup I can go to where my question
> can be answered. I do realize that my question may be off-topic, and
> is definitely operating system and compiler specific. So, here goes.
>
> I wanted to know, who calls main(). Many C books have the line, "In
> effect, the operating system calls main()." Actually, main() is called
> by some function (probably) within the C library (not programmmer-
> accessible), which is responsible for setting up the environment in
> which the program executes, like allocating FILE objects for stdin/
> stdout and setting up argc and argv (and envp - IS). This function
> also performs clean up afterwards, like calling exit() with the return
> value of main(). Obviously, this needn't be a function, and the
> compiler can just place code that does this at the beginning and end
> of main(). (I would see this as an inline function.)
>
> Obviously, there is no one answer - it's very implementation specific
> - and within standard C, there is no need to know, and it wouldn't
> help me if I did. On the other hand, someone writing a compiler does
> need to know that information, and in practice write the code as well.
> I am looking for specific code examples, for different OS's, so I can
> compare and perhaps write my own.
>
> Does anyone have an address for me? Thanks, and I'll appreciate not
> being flamed.
>
> -- Marty Wolfe
When the linker builds an executable, it writes somewhere the
"entry point" of the program.
This indicates the OS where in the executable parts of the file, is the
first instruction
When the OS loads the executable, either because you type its name in a
shell or double clicked in its icon, the OS loads the executable in
memory, creates a process for it, loads the associated dlls (or shared
objects) and sets the instruction pointer to that first instruction
and the program starts.
That first instructions normally setup the environment of the program,
and initialize the run time library. Then, they call the "main"
function.
When main returns, the "atexit" functions are called, and the program
finishes when the exit function is called.
This code is called "crt startup code" and each compiler has one.
If the program is designed for a circuit where there isno OS, it
will be located at a specific address. When the CPU starts, it will
look at some address, and it will find there either the program in eprom
or a jump to the first instruction of the program. It is up to the
program to establish the environment.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32